static int ReadPemFile(uchar *pszAppFileName, uchar *psCA, uchar *psCert, uchar *psPrivatekey) { int fd, iRet, iTempLen; uchar *ptr1, *ptr2 ; uchar ucBuf[MAX_PEM_ELN+1]; uchar pos_ca_buf[2048]; uchar pos_cer_buf[2048]; uchar pos_privatekey_buf[2048]; if (pszAppFileName == NULL) { return -1; } //打开文件 fd = open(pszAppFileName, O_RDWR); if (fd < 0) { return fd; } //读取文件 memset(ucBuf, 0, sizeof(ucBuf)); iRet = read(fd, ucBuf, MAX_PEM_ELN); if (iRet < 0) { ScrClrLine(6,9); ScrPrint(0, 6, 1, "READ FILE ERR:%d", iRet); PubWaitKey(30); close(fd); return iRet; } close(fd); //Get CERTIFICATE if ((ptr1 = strstr(ucBuf, "-----BEGIN CERTIFICATE-----")) && (ptr2 = strstr(ucBuf, "-----END CERTIFICATE-----")) ) { memset(pos_cer_buf, 0, sizeof(pos_cer_buf)); iTempLen = strlen(ptr1) - strlen(ptr2) - strlen("-----BEGIN CERTIFICATE-----\n"); memcpy(pos_cer_buf, ptr1+strlen("-----BEGIN CERTIFICATE-----\n"), iTempLen); } else { ScrClrLine(6,9); ScrPrint(0, 6, 1, "UNFOUND CERT"); PubWaitKey(30); return -2; } memset(pos_privatekey_buf, 0, sizeof(pos_privatekey_buf)); memset(pos_ca_buf, 0, sizeof(pos_ca_buf)); //out data memcpy(psCA, pos_ca_buf, strlen(pos_ca_buf)); memcpy(psCert, pos_cer_buf, strlen(pos_cer_buf)); memcpy(psPrivatekey, pos_privatekey_buf, strlen(pos_privatekey_buf)); return 0; }
//选择WIFI打开模式, 是扫描周围的热点,还是手工输入SSID int SelectWifiMode(void) { int iRet = 0; uchar ucKey; while(1) { kbflush(); ScrCls(); PubDispString(_T("WIFI"), DISP_LINE_CENTER|0); ScrPrint(0, 1, 0, _T("1 - SEARCH NETWORK")); ScrPrint(0, 2, 0, _T("2 - MANUAL")); ucKey = PubWaitKey(USER_OPER_TIMEOUT); if (ucKey == KEY1) { iRet = ScanWifiAps(); if (iRet) { //add by wuc 2014.3.24 if(WIFI_ERR_SCAN == iRet) { WifiClose(); iRet = WifiOpen(); if(iRet) { ScrClrBelow(1); ScrPrint(0, 2, 0, "OpenWifi err=%d", iRet); PubWaitKey(5); break; } } continue; } break; } if (ucKey == KEY2) { iRet = ManualWifiAps(); if (iRet) { continue; } break; } if (ucKey == KEYCANCEL || ucKey == NOKEY) { return ucKey; } } return iRet; }
//设置WIFI 本地IP参数 int SetWifiLocalIpPara(void) { int iRet = 0; uchar ucKey; while(1) { kbflush(); ScrCls(); PubDispString(_T("PROTOCOL"), DISP_LINE_CENTER|0); ScrPrint(0, 1, 0, _T("1 - DHCP")); ScrPrint(0, 2, 0, _T("2 - MANUAL")); ucKey = PubWaitKey(USER_OPER_TIMEOUT); if (ucKey == KEY1) { glSysCtrl.stAppWifiPara.DhcpEnable= 1; glConfiged = 1; break; } if (ucKey == KEY2) { glSysCtrl.stAppWifiPara.DhcpEnable = 0; iRet = ManualWifiIps(); if (iRet) { return iRet; } iRet = ConfirmConfig(); if(iRet) { return iRet; } break; } if (ucKey == KEYCANCEL || ucKey == NOKEY) { return ucKey; } /*if(ucKey == KEYENTER) { break; }*/ } return iRet; }
int main(void) { uchar ucKey; ushort ret; uchar *data="\x12\x34\x56\x78"; int len=strlen(data); SystemInit(); while(1) { ScrCls(); kbflush(); ScrPrint(0,0,0x01|0x80,"CRC-16 "); do{ ucKey=getkey(); }while(ucKey!=KEY1 && ucKey!=KEYENTER && ucKey!=KEYCANCEL); if(ucKey==KEY1 || ucKey==KEYENTER) { ScrCls(); ret=get_crc16(data,len); Lcdprintf("0x%x\n",ret); getkey(); } if(ucKey==KEYCANCEL) return 1; } return 0; }
//输入WIFI 加密方式 int GetWifiSecMode(uchar *psName, uchar *psOut) { uchar ucKey; ScrCls(); PubDispString(_T("ENCRYPTION"), DISP_LINE_CENTER|1); ScrPrint(0, 3, 0, _T("1 - NONE")); ScrPrint(0, 4, 0, _T("2 - WEP")); ScrPrint(0, 5, 0, _T("3 - WPA/WPA2")); ScrPrint(0, 6, 0, _T("4 - WPA-PSK/WPA2-PSK")); do { ucKey = PubWaitKey(USER_OPER_TIMEOUT); } while(ucKey!=KEY1 && ucKey!=KEY2 && ucKey!=KEY3 && ucKey!=KEY4 && ucKey!=KEYCANCEL); if (ucKey == KEY1) { *psOut = 0; } if (ucKey == KEY2) { *psOut = 1; } if (ucKey == KEY3) { *psOut = 2; } if (ucKey == KEY4) { *psOut = 3; } if (ucKey == KEYCANCEL || ucKey == NOKEY) { return ucKey; } return 0; }
//输入WIFI 密码 int GetWifiKey(uchar *psName, int iSecMode, uchar *psOut) { uchar ucRet; uchar ucTempBuf[100]; uchar ucWEPMode; if (iSecMode < 1 || iSecMode > 3) { return 0; } ScrCls(); PubDispString(psName, DISP_LINE_CENTER|1); if(iSecMode == 1) { PubDispString(_T("SELECT WEP MODE"), DISP_LINE_CENTER|2); ScrPrint(0, 3, 0, _T("1. ASCII")); ScrPrint(0, 4, 0, _T("2. HEX")); while(1) { ucRet = PubWaitKey(USER_OPER_TIMEOUT); if(ucRet == KEY1) { ucWEPMode = 0; break; } else if(ucRet == KEY2) { ucWEPMode = 1; break; } else if(ucRet == KEYCANCEL || ucRet == NOKEY) { return ucRet; } } ScrClrBelow(2); } PubDispString(_T("TYPE PASSWORD:"), DISP_LINE_CENTER|2); ScrGotoxyEx (0, 96); memset(ucTempBuf, 0, sizeof(ucTempBuf)); if (iSecMode == 1) //WEP { if(glSysCtrl.ucWepMode == 0) //ascii { strcpy(ucTempBuf, glSysCtrl.stAppWifiPara.Wep); } else //hex { PubBcd2Asc0(glSysCtrl.stAppWifiPara.Wep, 5, ucTempBuf); } ucRet = GetString(ucTempBuf, 0xF5, (uchar)(ucWEPMode==0 ? 5:10), (uchar)(ucWEPMode==0 ? 5:10)); glSysCtrl.ucWepMode = ucWEPMode; } else //WPA { strcpy(ucTempBuf, glSysCtrl.stAppWifiPara.Wpa); ucRet = GetString(ucTempBuf, 0xF5, 1, KEY_WPA_MAXLEN); } if (ucRet == 0x00) { if((iSecMode == 1) && (ucWEPMode == 1)) { PubAsc2Bcd(&ucTempBuf[1], ucTempBuf[0], psOut); } else { memcpy(psOut, &ucTempBuf[1], ucTempBuf[0]); } return 0; } if (ucRet == 0x0D) { return 0; } return ucRet; }
//扫描周围的热点 int ScanWifiAps(void) { int i, iRet, iApCount; uchar ucKey, ucTempBuf[100]; ST_WIFI_AP stWifiAps[16]; ScrClrBelow(1); ScrPrint(0, 1, 0, _T("SCANNING AP...")); //scanning ap if(strlen(glSysCtrl.stAppWifiPara.Ssid) != 0) { WifiDisconAp(); } iApCount = 8; memset(&stWifiAps, 0, sizeof(stWifiAps)); iRet = WifiScanAps(&stWifiAps[0], iApCount); if(iRet == 0) { ScrClrBelow(1); ScrPrint(0, 2, 0, _T("NO AVAILABLE AP")); getkey(); return WIFI_ERR_NULL; } if(iRet < 0) { ScrClrBelow(1); ScrPrint(0, 2, 0, "ScanAps err=%d", iRet); getkey(); return WIFI_ERR_SCAN; } ScrCls(); PubDispString(_T("SEARCH NETWORK"), DISP_LINE_CENTER|0); //please select //显示搜索到的热点 for (i=0; i<iRet; i++) { ScrPrint(0, (uchar)(i + 1), 0, "%d - %s",i, stWifiAps[i].Ssid); } while (1) { kbflush(); ucKey = PubWaitKey(USER_OPER_TIMEOUT); if (ucKey == KEYCANCEL || ucKey == NOKEY) { return ucKey; //modify by wuc 2014.4.1 } i = ucKey - 0x30; if(i<0 || i>iRet) { continue; } break; } strcpy(glSysCtrl.stAppWifiPara.Ssid, stWifiAps[i].Ssid); //判断是否需要密码 if(stWifiAps[i].SecMode == 0) //无加密 { glSysCtrl.stAppWifiPara.SecMode = 0; } else if(stWifiAps[i].SecMode == 1) //WEP加密 { glSysCtrl.stAppWifiPara.SecMode = 1; memset(ucTempBuf, 0, sizeof(ucTempBuf)); iRet = GetWifiKey(stWifiAps[i].Ssid, stWifiAps[i].SecMode, ucTempBuf); if(iRet) { return iRet; } strcpy(glSysCtrl.stAppWifiPara.Wep, ucTempBuf); } else if(stWifiAps[i].SecMode == 2) //WPA/WPA2加密 TKIP { glSysCtrl.stAppWifiPara.SecMode = 2; memset(ucTempBuf, 0, sizeof(ucTempBuf)); iRet = GetWifiKey(stWifiAps[i].Ssid, stWifiAps[i].SecMode, ucTempBuf); if(iRet) { return iRet; } strcpy(glSysCtrl.stAppWifiPara.Wpa, ucTempBuf); } else if(stWifiAps[i].SecMode == 3) //WPA-PSK/WPA2-PSK加密 AES { glSysCtrl.stAppWifiPara.SecMode = 3; memset(ucTempBuf, 0, sizeof(ucTempBuf)); iRet = GetWifiKey(stWifiAps[i].Ssid, stWifiAps[i].SecMode, ucTempBuf); if(iRet) { return iRet; } strcpy(glSysCtrl.stAppWifiPara.Wpa, ucTempBuf); } else { ScrClrRect(0, 24, 319, 215); ScrGotoxyEx(0, 24); Lcdprintf("%s\n", stWifiAps[i].Ssid); Lcdprintf(_T("UNSUPPORTED MODE\n")); getkey(); return WIFI_ERR_CANCEL; } return 0; }
int ShowLastAP(void) //add by wuc 2014.9.9 { int iRet; //读取参数并显示 kbflush(); ScrCls(); //add by wuc 2014.1.10 PubDispString(_T("CONFIGURATIONS"), DISP_LINE_CENTER|0); if(strlen(glSysCtrl.stAppWifiPara.Ssid) == 0) { ScrPrint(0, 1, 0, _T("NO AP CONNECTED")); } else { ScrPrint(0, 1, 0, _T("NETWORK: %s\n"), glSysCtrl.stAppWifiPara.Ssid); if (glSysCtrl.stAppWifiPara.SecMode == 1) { if(glSysCtrl.ucWepMode == 0) { ScrPrint(0, 2, 0, _T("MODE:WEP(ASCII)")); ScrPrint(0, 3, 0, _T("PASSWORD:%s\n"), glSysCtrl.stAppWifiPara.Wep); } else { ScrPrint(0, 2, 0, _T("MODE:WEP(HEX)")); ScrPrint(0, 3, 0, _T("PASSWORD:%02x%02x%02x%02x%02x\n"), glSysCtrl.stAppWifiPara.Wep[0], glSysCtrl.stAppWifiPara.Wep[1], glSysCtrl.stAppWifiPara.Wep[2], glSysCtrl.stAppWifiPara.Wep[3], glSysCtrl.stAppWifiPara.Wep[4]); } } else if (glSysCtrl.stAppWifiPara.SecMode == 2) { ScrPrint(0, 2, 0, _T("MODE:WPA/WPA2")); ScrPrint(0, 3, 0, _T("PASSWORD:%s\n"), glSysCtrl.stAppWifiPara.Wpa); } else if (glSysCtrl.stAppWifiPara.SecMode == 3) { ScrPrint(0, 2, 0, _T("MODE:WPA-PSK/WPA2-PSK")); ScrPrint(0, 3, 0, _T("PASSWORD:%s\n"), glSysCtrl.stAppWifiPara.Wpa); } else { ScrPrint(0, 2, 0, _T("MODE:NONE")); } if (glSysCtrl.stAppWifiPara.DhcpEnable == 1) { ScrPrint(0, 4, 0, _T("DHCP: OPEN")); } else { ScrPrint(0, 4, 0, _T("DHCP: CLOSE")); } } PubDispString(_T("(PRESS ENTER TO CONTINUE)"), DISP_LINE_CENTER|6); while(1) { iRet = PubWaitKey(USER_OPER_TIMEOUT); if(iRet == KEYCANCEL || iRet == NOKEY) { return iRet; } if(iRet == KEYENTER) { break; } } return 0; }
int Ssl_Connect( char *szRemoteIP, short RemotePort, int TimeOutSec ) { int iRet, iEvent; int length; SSL_BUF_T pos_ca; SSL_BUF_T pos_cert; SSL_BUF_T pos_privatekey; SSL_BUF_T pos_certs[2]; char pos_ca_pem[2048]; char pos_cert_pem[2048]; char pos_privatekey_pem[2048]; // char pos_ca_buf[2048]; char pos_cert_buf[2048]; // char pos_privatekey_buf[2048]; //linzhao 0331 DebugOutput("%s--%d iSSLSocket:%d\n", __FILE__, __LINE__, iSSLSocket); if (iSSLSocket>=0) { /* iEvent = Netioctl(iSSLSocket, CMD_TO_GET, 0); DebugOutput("%s--%d iEvent:%d\n", __FILE__, __LINE__, iEvent); if (iEvent&(SOCK_EVENT_CONN|SOCK_EVENT_READ|SOCK_EVENT_WRITE)) { return 0; } */ return 0; } SslSetNetOps( &stSslNetOps ); //Let SSL module know how to perform TCP/IP operations(open, send, receive and close) DebugOutput("%s--%d,SslSetNetOps\n", __FILE__, __LINE__); memset( &stSslSysOps, 0, sizeof(SSL_SYS_OPS) ); stSslSysOps.ServCertAck = OurServCertAck; SslSetSysOps( &stSslSysOps ); //Define some system related call back functions to use by SSL module //* // begin: 证书验证 length = 0; memset(pos_ca_pem, 0, sizeof(pos_ca_pem)); memset(pos_cert_pem, 0, sizeof(pos_cert_pem)); memset(pos_privatekey_pem, 0, sizeof(pos_privatekey_pem)); iRet = ReadPemFile(CER_FILE, pos_ca_pem, pos_cert_pem, pos_privatekey_pem); DebugOutput("%s--%d--%s, ReadPemFile iRet:%d\n", __FILE__, __LINE__, __FUNCTION__, iRet); if (iRet == 0) { pos_ca.size = 0; pos_ca.ptr = NULL; length = 0; memset(pos_cert_buf, 0, sizeof(pos_cert_buf)); length = SslDecodePem(pos_cert_pem, strlen(pos_cert_pem), pos_cert_buf, sizeof(pos_cert_buf)); if (length <= 0) { ScrPrint(0, 6, 1, _T("ssl Cert wrong!")); PubWaitKey(30); return ERR_NO_DISP; } pos_cert.size = length; pos_cert.ptr = pos_cert_buf; pos_privatekey.size = 0; pos_privatekey.ptr = NULL; } // end: for 证书验证 //*/ iRet = SslCreate(); if ( iRet < 0 ) { return iRet; } iSSLSocket = iRet; DebugOutput("%s--%d--%s, iSSLSocket:%d\n", __FILE__, __LINE__, __FUNCTION__, iSSLSocket); pos_certs[0] = pos_cert; pos_certs[1] = pos_ca; iRet = SslCertsSet(iSSLSocket, 0, 0, NULL, pos_certs, 1, &pos_privatekey); DebugOutput("%s--%d--%s, SslCertsSet iRet:%d\n", __FILE__, __LINE__, __FUNCTION__, iRet); if (iRet < 0) { ScrPrint(0, 6, 1, "SslCertsSet=%d", iRet); PubWaitKey(30); return iRet; } iRet = SslConnect( iSSLSocket, szRemoteIP, RemotePort, 0, 0 ); DebugOutput("%s--%d--%s, sslConnect:[%s:%d]=[%d], iSSLSocket:%d\n", __FILE__, __LINE__, __FUNCTION__, szRemoteIP, RemotePort, iRet, iSSLSocket); //you wen ti if ( iRet != 0 && 0==SslClose( iSSLSocket ))//linzhao { DebugOutput("%s--%d, iSSLSocket:%d, s_iSocket:%d\n", __FILE__, __LINE__, iSSLSocket, s_iSocket); iSSLSocket = -1; s_iSocket = -1;//linzhao } DebugOutput("%s--%d--%s, iSSLSocket:%d\n", __FILE__, __LINE__, __FUNCTION__, iSSLSocket); return iRet; }
/* At last: THE FUNCTION */ int ScrEnter(char *Prompt_Text, int Prompt_Attr, int Row, int Column, int Box_Attr, char *Text, char *Default_Text, int *Text_Length, int Text_Attr, int *Exit_Keys) { int ch, /* character just input */ Pos, /* position in Text */ Index, /* index for shuffling elements */ Buffer_Size, /* size of the screen saving buffer*/ Prompt_Length = strlen(Prompt_Text), /* length of the prompt */ Text_Start = Column+Prompt_Length+1, /* column text input starts at */ Text_Max = *Text_Length, /* local variable for Text_Length */ Cur_Row, Cur_Col, /* old cursor coordinates */ Tmp = (Text_Max<0) ? 0 : 1, /* for people who just want a key */ No_Prompt = Text_Max == 0; /* for people who just want a box */ char *Save_Buffer; /* area for storing the space used */ enum Ins_Mode Mode = Insert; /* what mode the user is in */ if (!Tmp) Text_Max = 0; if (!Text_Max) Text_Start -= 1; /* Store the current cursor position */ ScrGetCursorPos (&Cur_Row, &Cur_Col); if (Box_Attr == NO_BOX) { /* Only save area used by prompt and text */ Buffer_Size = Prompt_Length+Text_Max+2; Buffer_Size *= 2; Save_Buffer = (char *) malloc (Buffer_Size*sizeof(char)); ScrRead (Row, Column, Prompt_Length+Text_Max+2, 1, Save_Buffer); } else { /* save whole area */ Buffer_Size = (Prompt_Length+Text_Max+4) * 3; Buffer_Size *= 2; Save_Buffer = (char *) malloc (Buffer_Size*sizeof(char)); ScrRead (Row-1, Column-1, Prompt_Length+Text_Max+4, 3, Save_Buffer); /* Draw the box if requested */ ScrBox (Row-1, Column-1, Row+1, Column + Prompt_Length + Text_Max + 1 - 2*No_Prompt + Tmp, Box_Attr); } /* Put the prompt where it belongs */ ScrPrint (Row, Column, Prompt_Length, Prompt_Attr, Prompt_Text); /* Put up the default text */ if (Text_Max) { ScrClear (Row, Text_Start - 1, Text_Max + Tmp + 1, 1, Text_Attr); ScrPrintText (Row, Text_Start, Text_Max, Default_Text); } /* Put the cursor at the end of the default text */ Pos = strlen (Default_Text); if (Pos > Text_Max) Pos = Text_Max; ScrSetCursorPos(Row, Text_Start+Pos); if (!No_Prompt) { /* Initialize the array to 0's */ for (Pos = 0; Pos <= Text_Max; Text[Pos++] = '\000'); Pos = -1; /* Do the actual input of the string */ // if (Mode == Replace) // ScrSetCursorSize (1,8); // else // ScrSetCursorSize (7,8); // // ScrSetCursorSize() is no longer supported in portscr, but it may be added // again before too long. do { ch = ScrKey(); if (Pos == -1) { Pos = 0; } switch (ch) { case K_RETURN: case K_UP: case K_DOWN: /* Ignore these keys */ break; case K_LEFT: if (Pos > 0) Pos--; break; case K_RIGHT: if (Pos < strlen(Text)) Pos++; break; case K_CRIGHT: while ((!isspace(Text[Pos])) && (Pos < strlen(Text))) Pos++; while ((isspace(Text[Pos])) && (Pos < strlen(Text))) Pos++; break; case K_CLEFT: if (Pos > 0) Pos--; while ((isspace(Text[Pos])) && (Pos > 0)) Pos--; while ((!isspace(Text[Pos])) && (Pos > 0)) Pos--; if (Pos > 0) Pos++; break; case K_HOME: Pos = 0; break; case K_END: Pos = strlen(Text); break; case K_INS: if (Mode == Insert) { Mode = Replace; // ScrSetCursorSize (1,8); } else { Mode = Insert; // ScrSetCursorSize (7,8); } break; case K_BACKSPACE: if (Pos == 0) break; Pos--; case K_DEL: for (Index = Pos; Index < Text_Max; Text[Index++] = Text[Index+1]); Text[Text_Max] = '\000'; break; default: if ((!isprint(ch)) || (In (ch,Exit_Keys))) break; if (Mode == Insert) { if (strlen(Text) == Text_Max) break; for (Index = Text_Max-1; Index > Pos; Index--) Text[Index] = Text[Index-1]; } if (Pos < Text_Max) Text[Pos++] = ch; break; } /* Clear entry area, show current text, and put the cursor at its end */ ScrClear (Row, Text_Start-1, Text_Max + Tmp + 1, 1, Text_Attr); ScrPrintText (Row, Text_Start, strlen(Text), Text); ScrSetCursorPos(Row, Text_Start+Pos); } while ((ch != K_RETURN) && (!In (ch, Exit_Keys))); /* Put back onto the screen what was there before */ if (Box_Attr == NO_BOX) ScrWrite (Row, Column, Prompt_Length+Text_Max+2, 1, Save_Buffer); else ScrWrite (Row-1, Column-1, Prompt_Length+Text_Max+4, 3, Save_Buffer); free (Save_Buffer); /* Clean up the string by truncating trailing spaces */ if (Tmp) while (Text[--Text_Max] == ' ') Text[Text_Max] = '\000'; if (strlen(Text) == 0) strcpy (Text, Default_Text); *Text_Length = strlen(Text); } /* Restore the original cursor position */ ScrSetCursorPos (Cur_Row, Cur_Col); // ScrSetCursorSize (7,8); return ch; }