CLI example for NNN50

Dependencies:   NNN50_WIFI_API

Fork of NNN50_WiFi_HelloWorld by Delta

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wifi_cli.cpp Source File

wifi_cli.cpp

00001 /**
00002  * File: wifi-cli.c
00003  * Description: WIFI CLI commands used by all applications WIFI of profile.
00004  *
00005  * Copyright 2015 by CYNTEC Corporation.  All rights reserved.
00006  * Author: Lester Lee
00007  */
00008 
00009 #include "wifi_cli.h"
00010 
00011 #define AP_SSID_MAX_LEN 33
00012 #define AP_PW_MAX_LEN 64
00013 #define STATIC_IP_MAX_LEN 15
00014 #define TCP_SEND_MAX_LEN 1400
00015 #define UDP_SEND_MAX_LEN 1400
00016 #define DELTA_WIFI_DEBUG
00017 #define WIFI_MAX_AP_NUMBER 15
00018 
00019 // General configuration parameters
00020 WIFIDevice wifiDevice;
00021 EthernetInterface eth;
00022 TCPSocketConnection tcpConnect;
00023 UDPSocket udpSocket;
00024 Endpoint cliEndpoint;
00025 TCPSocketServer tcpServer;
00026 char static_ip[STATIC_IP_MAX_LEN] = "192.168.1.1";
00027 extern Serial console;
00028 extern const char* cyntecCommandErrorNames[];
00029 
00030 static uint8_t is_Listen = false;
00031 static tstrM2mWifiscanResult saveAP[WIFI_MAX_AP_NUMBER];
00032 static bool wifiIniState = false;
00033 
00034 typedef struct deviceNetwork_s {
00035     char ap_ssid[AP_SSID_MAX_LEN+1];
00036     char ap_pw[AP_PW_MAX_LEN+1];
00037     uint8_t ap_sec;
00038 } deviceNetwork_t;
00039 deviceNetwork_t devNetwork[4]; // 0-2 for setNetwork, 3 for set_ap
00040 
00041 static uint8_t cyntecIsValidIP(uint8_t *startIdx, uint8_t totalLen)
00042 {
00043     uint8_t *ipIdx = startIdx;
00044     uint8_t *tempIdx = ipIdx;
00045     uint8_t IPAddr[3];
00046     //uint8_t IPTokenLen = 0;
00047     int16_t ipToken;
00048     while ( (tempIdx - startIdx) <= totalLen ) {
00049         if (*tempIdx == '.' || ((tempIdx - startIdx) == totalLen)) {
00050             memset( IPAddr, 0, 3);
00051             memcpy( IPAddr, ipIdx, (tempIdx - ipIdx));
00052 
00053             ipToken = atoi((const char *)IPAddr);
00054             if (ipToken > 255 || ipToken < 0)
00055                 return 1;
00056 
00057             ipIdx = tempIdx + 1;
00058         }
00059         tempIdx++;
00060     }
00061     return 0;
00062 }
00063 
00064 static void cyntecPrintOk(void)
00065 {
00066     console.printf("\r\nOK\r\n\r\n");
00067 }
00068 
00069 static void cyntecPrintError(uint8_t errIdx)
00070 {
00071     console.printf("\r\nERROR;%s\r\n\r\n",cyntecCommandErrorNames[errIdx]);
00072 }
00073 
00074 /////////**** WIFI Device Implement ****//////////
00075 static void cyn_wifi_device_sleep()
00076 {
00077     if (cyntecGetCommandTokenCnt() != 2) {
00078         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00079         return;
00080     }
00081     uint8_t err_code = wifiDevice.sleep();
00082     if (err_code == 0)
00083         cyntecPrintOk();
00084     else {
00085         console.printf("ERROR;%d\r\n",err_code);
00086         return;
00087     }
00088 }
00089 
00090 static void cyn_wifi_device_network()
00091 {
00092     //if (wifiIniState == true) {
00093 //        cyntecPrintError(CYNTEC_CMD_ERR_WRONG_CMD_ORDER);
00094 //        return;
00095 //    }
00096     if (cyntecGetCommandTokenCnt() == 6) {
00097         uint8_t argLen = 0;
00098         uint8_t *argSSID;
00099         uint8_t *argPW;
00100         uint8_t *argSec;
00101         uint8_t security = 0;
00102         uint8_t *argPriority;
00103         /* 0~2, 0 is highest */
00104         uint8_t priority = 0;
00105 
00106         /* handle priority */
00107         argPriority = cyntecGetCommandArgument(3, NULL);
00108         priority = atoi((const char*)argPriority);
00109         
00110         if(priority > 2) {
00111             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00112             return;
00113         }
00114 
00115         /* handle SSID */
00116         argSSID = cyntecGetCommandArgument(0, &argLen);
00117         if ( argLen > AP_SSID_MAX_LEN ) {
00118             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00119             return;
00120         }
00121         memset( devNetwork[priority].ap_ssid , 0, AP_SSID_MAX_LEN+1);
00122         memcpy( devNetwork[priority].ap_ssid, argSSID, argLen);
00123 
00124         /* handle Password */
00125         argPW = cyntecGetCommandArgument(1, &argLen);
00126         if ( argLen > AP_PW_MAX_LEN ) {
00127             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00128             return;
00129         }
00130         memset( devNetwork[priority].ap_pw, 0, AP_PW_MAX_LEN+1);
00131         memcpy( devNetwork[priority].ap_pw, argPW, argLen);
00132         
00133         /* handle security */
00134         argSec = cyntecGetCommandArgument(2, &argLen);
00135         //security = atoi((const char*)argSec);
00136         char numSec[1];
00137         memcpy( numSec, argSec, 1);
00138         security = atoi(numSec);
00139         
00140         if(security > 4) {
00141             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00142             return;
00143         }
00144         
00145         devNetwork[priority].ap_sec = security;
00146 
00147         /* call setup API */
00148         wifiDevice.setNetwork(devNetwork[priority].ap_sec, devNetwork[priority].ap_ssid, devNetwork[priority].ap_pw, priority);
00149         cyntecPrintOk();
00150     } else {
00151         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00152     }
00153 }
00154 
00155 void scanCallback(tstrM2mWifiscanResult result)
00156 {
00157     console.printf("scanCallback\r\n");
00158     console.printf("[%d], ", result.u8index);
00159     console.printf("%d, ", result.s8rssi);
00160     console.printf("%d, ", result.u8AuthType);
00161     console.printf("%d,", result.u8ch);
00162     for(int i=0;i<6;i++) {
00163         console.printf(" %02x", result.au8BSSID[i]);
00164     }
00165     
00166     console.printf(", ");
00167     console.printf("%s\r\n", result.au8SSID);
00168 }
00169 
00170 static void cyn_wifi_device_ap_scan()
00171 {
00172     if (wifiIniState == false) {
00173         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_CMD_ORDER);
00174         return;
00175     }
00176     memset(saveAP,0,sizeof(saveAP));
00177     int scanResult = 0;
00178     scanResult = wifiDevice.apScan(scanCallback);
00179     if (scanResult >= 0) {
00180 #ifdef DELTA_WIFI_DEBUG
00181         console.printf("scan AP number:%d\r\n",scanResult);
00182 #endif
00183         cyntecPrintOk();
00184         return;
00185     } else {
00186         cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00187         return;
00188     }
00189 }
00190 
00191 static void cyn_wifi_set_ap()
00192 {
00193     if (cyntecGetCommandTokenCnt() != 6) {
00194         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00195     }
00196     uint8_t argLen = 0;
00197     uint8_t * argSSID;
00198     uint8_t * argPW;
00199     uint8_t * argSEC;
00200     uint8_t * argCH;
00201 
00202     /* handle SSID */
00203     argSSID = cyntecGetCommandArgument(0, &argLen);
00204     if ( argLen > AP_SSID_MAX_LEN ) {
00205         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00206         return;
00207     }
00208     memset( devNetwork[3].ap_ssid , 0, AP_SSID_MAX_LEN+1);
00209     memcpy( devNetwork[3].ap_ssid, argSSID, argLen);
00210 
00211     /* handle Password */
00212     argPW = cyntecGetCommandArgument(1, &argLen);
00213     if ( argLen > AP_PW_MAX_LEN ) {
00214         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00215         return;
00216     }
00217     memset( devNetwork[3].ap_pw, 0, AP_PW_MAX_LEN+1);
00218     memcpy( devNetwork[3].ap_pw, argPW, argLen);
00219 
00220     /* handle Security */
00221     tenuM2mSecType setSec  = M2M_WIFI_SEC_OPEN;
00222     argSEC = cyntecGetCommandArgument(2, &argLen);
00223     char numSec[argLen];
00224     memcpy( numSec, argSEC, argLen);
00225     int sec = atoi(numSec);
00226     
00227     if(sec == 0)
00228         setSec = M2M_WIFI_SEC_OPEN;
00229     else if(sec == 1)
00230         setSec = M2M_WIFI_SEC_WEP;
00231     else {
00232         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00233         return;
00234     }
00235 
00236     /* handle Channel */
00237     argCH = cyntecGetCommandArgument(3, &argLen);
00238     int setChan = atoi((char *)argCH);
00239 
00240     if (setChan > 14 || setChan < 1) {
00241         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00242         return;
00243     }
00244 #ifdef DELTA_WIFI_DEBUG
00245     console.printf("argSSID:%s\r\n",devNetwork[3].ap_ssid);
00246     console.printf("argPW:%s\r\n",devNetwork[3].ap_pw);
00247     console.printf("sec:%i\r\n",sec);
00248     console.printf("setChan:%i\r\n",setChan);
00249 #endif
00250 
00251     /* call Enable Access Point API */
00252     if (wifiDevice.enableAccessPoint(devNetwork[3].ap_ssid, devNetwork[3].ap_pw, static_ip, setSec, setChan) != 0)
00253         cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00254     else
00255         cyntecPrintOk();
00256 
00257 }
00258 
00259 /////////**** WIFI Ethernet Implement ****//////////
00260 static void cyn_wifi_ethernet_init()
00261 {
00262     uint8_t argLen = 0;
00263     uint8_t *argIP;
00264     //EthernetInterface ethInterface;
00265     uint8_t result;
00266     if (cyntecGetCommandTokenCnt() == 2) {
00267         /* use DHCP to get IP */
00268         result = eth.init();
00269         if ( result == 0 ) {
00270             cyntecPrintOk();
00271             wifiIniState = true;
00272         } else {
00273             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00274         }
00275 
00276     } else if ( cyntecGetCommandTokenCnt() == 3 ) {
00277         /* use static to get IP */
00278         argIP = cyntecGetCommandArgument(0, &argLen);
00279 
00280         if ( cyntecIsValidIP(argIP,argLen) != 0) {
00281             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00282             return;
00283         }
00284 
00285         memset( static_ip, 0, STATIC_IP_MAX_LEN);
00286         memcpy( static_ip, argIP, argLen);
00287         if ( eth.init((const char *)static_ip, NULL, NULL) == 0) {
00288             cyntecPrintOk();
00289             wifiIniState = true;
00290         } else
00291             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00292     } else {
00293         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00294     }
00295 
00296     return;
00297 }
00298 static void cyn_wifi_ethernet_connect()
00299 {
00300     int timeout_ms = 5000;
00301     uint8_t *argTimeout;
00302     //EthernetInterface ethInterface;
00303     if (cyntecGetCommandTokenCnt() != 2 & cyntecGetCommandTokenCnt() != 3)
00304         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00305     if (cyntecGetCommandTokenCnt() == 2) {
00306 #ifdef DELTA_WIFI_DEBUG
00307         console.printf("\r\n");
00308         console.printf("Connecting..., Waiting for 5000 ms...");
00309         console.printf("\r\n\r\n");
00310 #endif
00311         if (eth.connect() == 0 )
00312             cyntecPrintOk();
00313         else
00314             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00315     }
00316     if (cyntecGetCommandTokenCnt() == 3) {
00317         argTimeout = cyntecGetCommandArgument(0, NULL);
00318         timeout_ms = atoi((const char*)argTimeout);
00319 
00320         if (timeout_ms < 0) {
00321             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00322             return;
00323         }
00324 #ifdef DELTA_WIFI_DEBUG
00325         console.printf("\r\nConnecting..., Waiting for ");
00326         console.printf((char*)argTimeout);
00327         console.printf(" ms...\r\n\r\n");
00328 #endif
00329         if (eth.connect(timeout_ms) == 0 )
00330             cyntecPrintOk();
00331         else
00332             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00333     }
00334     return;
00335 }
00336 static void cyn_wifi_ethernet_disconnect()
00337 {
00338     if (eth.disconnect() == 0)
00339         cyntecPrintOk();
00340     else
00341         cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00342 }
00343 static void cyn_wifi_ethernet_mac()
00344 {
00345     char mac_addr[19];
00346     memset(mac_addr, 0, 19);
00347     memcpy(mac_addr, eth.getMACAddress(), 19);
00348 
00349     console.printf("\r\nOK;%s\r\n\r\n",mac_addr);
00350 
00351     return;
00352 }
00353 static void cyn_wifi_ethernet_ip()
00354 {
00355     char ip_addr[15] = "\0";
00356     memset(ip_addr, 0, 15);
00357     memcpy(ip_addr, eth.getIPAddress(), 15);
00358 
00359     console.printf("\r\nOK;%s\r\n\r\n",ip_addr);
00360 
00361     return;
00362 }
00363 
00364 static void cyn_wifi_ethernet_isConnected()
00365 {
00366     bool is_connected = false;
00367     is_connected = wifiDevice.is_AP_connected();
00368 
00369     if (is_connected == true)
00370         console.printf("\r\nOK;TRUE\r\n\r\n");
00371     else
00372         console.printf("\r\nOK;FALSE\r\n\r\n");
00373 }
00374 
00375 /////////**** WIFI TCP Socket Server Implement ****//////////
00376 static void cyn_wifi_tcp_server_bind()
00377 {
00378     int port = 1;
00379     if (cyntecGetCommandTokenCnt() == 2) {
00380         if (tcpServer.bind(port) == 0)
00381             cyntecPrintOk();
00382         else
00383             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00384 
00385     } else if (cyntecGetCommandTokenCnt() == 3) {
00386         port = atoi((const char*)(cyntecGetCommandArgument(0, NULL)));
00387 #ifdef DELTA_WIFI_DEBUG
00388         console.printf("port:%i\r\n",port);
00389 #endif
00390         if (port < 0) {
00391             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00392             return;
00393         }
00394 
00395         if ( tcpServer.bind(port) == 0 )
00396             cyntecPrintOk();
00397         else
00398             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00399 
00400     }   else {
00401         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00402     }
00403 }
00404 static void cyn_wifi_tcp_server_listen()
00405 {
00406     if (cyntecGetCommandTokenCnt() == 2) {
00407         if ( tcpServer.listen() == 0 ) {
00408             cyntecPrintOk();
00409             is_Listen = true;
00410         } else
00411             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00412     } else {
00413         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00414     }
00415 }
00416 static void cyn_wifi_tcp_server_accept()
00417 {
00418     if (cyntecGetCommandTokenCnt() == 2) {
00419         if (is_Listen == false) {
00420             cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION);
00421             return;
00422         }
00423 
00424         if ( tcpServer.accept(tcpConnect) == 0 ) {
00425             cyntecPrintOk();
00426             is_Listen = false;
00427         } else
00428             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00429 
00430     }   else {
00431         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00432     }
00433 }
00434 static void cyn_wifi_tcp_server_blocking()
00435 {
00436     bool blocking = false;
00437     unsigned int timeout;
00438 
00439     uint8_t *arg;
00440 
00441     if (cyntecGetCommandTokenCnt() == 3) {
00442         arg = cyntecGetCommandArgument(0, NULL);
00443         if (arg[0] == '1')
00444             blocking = true;
00445 
00446         tcpServer.set_blocking(blocking, 1500);
00447         cyntecPrintOk();
00448 
00449     } else if (cyntecGetCommandTokenCnt() == 4) {
00450 
00451         arg = cyntecGetCommandArgument(0, NULL);
00452         if (arg[0] == '1')
00453             blocking = true;
00454 
00455         arg = cyntecGetCommandArgument(1, NULL);
00456         timeout = (unsigned int)atoi((const char *)arg);
00457 
00458         tcpServer.set_blocking(blocking, timeout);
00459         cyntecPrintOk();
00460 
00461     }   else {
00462         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00463     }
00464 }
00465 static void cyn_wifi_tcp_server_close()
00466 {
00467     bool shutdown = true;
00468     uint8_t *arg;
00469 
00470     if (cyntecGetCommandTokenCnt() == 2) {
00471         if ( tcpServer.close(shutdown) == 0 )
00472             cyntecPrintOk();
00473         else
00474             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00475 
00476     } else if (cyntecGetCommandTokenCnt() == 3) {
00477         arg = cyntecGetCommandArgument(0, NULL);
00478         if (arg[0] == '0')
00479             shutdown = false;
00480 
00481         if ( tcpServer.close(shutdown) == 0 )
00482             cyntecPrintOk();
00483         else
00484             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00485 
00486     }   else {
00487         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00488     }
00489 }
00490 /////////**** WIFI TCP Socket Connection ****//////////
00491 static void cyn_wifi_tcp_connection_connect()
00492 {
00493     uint8_t connect_ip[STATIC_IP_MAX_LEN];
00494     int port;
00495 
00496     uint8_t argLen = 0;
00497     uint8_t *argIP;
00498 
00499     if (cyntecGetCommandTokenCnt() == 4) {
00500         /* handle IP arg */
00501         argIP = cyntecGetCommandArgument(0, &argLen);
00502         memset( connect_ip, 0, STATIC_IP_MAX_LEN);
00503         memcpy( connect_ip, argIP, argLen);
00504 
00505         /* handle Port arg */
00506         port = atoi((const char *)cyntecGetCommandArgument(1, NULL));
00507 
00508         if ( tcpConnect.connect((const char *)connect_ip, port) == 0 )
00509             cyntecPrintOk();
00510         else
00511             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00512 
00513     }   else {
00514         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00515     }
00516 }
00517 static void cyn_wifi_tcp_connection_is_connected()
00518 {
00519     bool is_connected = false;
00520     is_connected = tcpConnect.is_connected();
00521 
00522     if (is_connected == true)
00523         console.printf("\r\nOK;TRUE\r\n\r\n");
00524     else
00525         console.printf("\r\nOK;FALSE\r\n\r\n");
00526 }
00527 
00528 static void cyn_wifi_tcp_connection_send()
00529 {
00530     char msg[TCP_SEND_MAX_LEN+1];
00531     int sendData;
00532     uint8_t * argAllBuf = cyntecGetCommandTotalBuffer();
00533     int sendLen = 0; // Maximum 1400
00534     uint8_t sendLenCharNum = 0; // Maximum 4
00535     uint8_t *argLeng = cyntecGetCommandArgument(0,&sendLenCharNum);
00536     sendLen = cyntecAtoi(argLeng,sendLenCharNum);
00537     //sendLen = cyntecAtoInt(argLeng);
00538     console.printf("len:%d\r\n", sendLen);
00539     if (sendLen > TCP_SEND_MAX_LEN || sendLen < 0) {
00540         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00541         return;
00542     }
00543     if (cyntecGetCommandTokenCnt() <4 ) {
00544         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00545         return;
00546     }
00547     if (cyntecGetCommandTokenCnt() >= 4) {
00548         if ( tcpConnect.is_connected() == false ) {
00549             cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION);
00550             return;
00551         }
00552 
00553         /* handle Message arg */
00554 #ifdef DELTA_WIFI_DEBUG
00555         console.printf("sendLen:%i,Buf:%s,Index:%i\r\n",sendLen,&argAllBuf[26+sendLenCharNum],cyntecGetTotalIndex());
00556 #endif
00557         memset( msg, 0, TCP_SEND_MAX_LEN+1);
00558         for (uint8_t i=0; i<sendLen; i++)
00559             msg[i] = argAllBuf[26+sendLenCharNum+i];
00560 #ifdef DELTA_WIFI_DEBUG
00561         console.printf("msg:%s\r\n",msg);
00562 #endif
00563         sendData = tcpConnect.send(msg, sendLen);
00564         if ( sendData >= 0 ) {
00565             console.printf("\r\nOK;");
00566             console.printf("%i\r\n\r\n",sendData);
00567         } else
00568             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00569     }
00570 }
00571 
00572 static void cyn_wifi_tcp_connection_send_all()
00573 {
00574     char msg[TCP_SEND_MAX_LEN+1];
00575     int sendData;
00576     uint8_t * argAllBuf = cyntecGetCommandTotalBuffer();
00577     int sendLen = 0; // Maximum 1400
00578     uint8_t sendLenCharNum = 0; // Maximum 4
00579     uint8_t *argLeng = cyntecGetCommandArgument(0,&sendLenCharNum);
00580     sendLen = cyntecAtoi(argLeng,sendLenCharNum);
00581     if (sendLen > TCP_SEND_MAX_LEN) {
00582         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00583         return;
00584     }
00585     if (cyntecGetCommandTokenCnt() <4 ) {
00586         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00587         return;
00588     }
00589     if (cyntecGetCommandTokenCnt() >= 4) {
00590         if ( tcpConnect.is_connected() == false ) {
00591             cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION);
00592             return;
00593         }
00594 
00595         /* handle Message arg */
00596 #ifdef DELTA_WIFI_DEBUG
00597         console.printf("sendLen:%i,Buf:%s,Index:%i\r\n",sendLen,&argAllBuf[30+sendLenCharNum],cyntecGetTotalIndex());
00598 #endif
00599         memset( msg, 0, TCP_SEND_MAX_LEN+1);
00600         for (uint8_t i=0; i<sendLen; i++)
00601             msg[i] = argAllBuf[30+sendLenCharNum+i];
00602 #ifdef DELTA_WIFI_DEBUG
00603         console.printf("msg:%s\r\n",msg);
00604 #endif
00605         sendData = tcpConnect.send_all(msg, sendLen);
00606         if ( sendData >= 0 ) {
00607             console.printf("\r\nOK;");
00608             console.printf("%i\r\n\r\n",sendData);
00609         } else
00610             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00611     }
00612 }
00613 
00614 static void cyn_wifi_tcp_connection_receive()
00615 {
00616     char msg[TCP_SEND_MAX_LEN+1];
00617     int argLen = 0;
00618 
00619     if (cyntecGetCommandTokenCnt() == 3) {
00620         /* handle Message arg */
00621         argLen = atoi((const char *)cyntecGetCommandArgument(0, NULL));
00622         //console.printf("argLen:%d\r\n",argLen);
00623         if (argLen > TCP_SEND_MAX_LEN || argLen < 0) {
00624             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00625             return;
00626         }
00627 
00628         memset( msg, 0, TCP_SEND_MAX_LEN+1);
00629 
00630         if ( tcpConnect.receive(msg, argLen) >= 0 ) {
00631             console.printf("\r\nOK;%s\r\n\r\n",msg);
00632         } else
00633             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00634 
00635     }   else {
00636         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00637     }
00638 }
00639 static void cyn_wifi_tcp_connection_receive_all()
00640 {
00641     char msg[TCP_SEND_MAX_LEN+1];
00642     int argLen = 0;
00643 
00644     if (cyntecGetCommandTokenCnt() == 3) {
00645         /* handle Message arg */
00646         argLen = atoi((const char *)cyntecGetCommandArgument(0, NULL));
00647 
00648         if (argLen > TCP_SEND_MAX_LEN || argLen < 0) {
00649             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00650             return;
00651         }
00652 
00653         memset( msg, 0, TCP_SEND_MAX_LEN+1);
00654 
00655         if ( tcpConnect.receive_all(msg, argLen) >= 0 ) {
00656             console.printf("\r\nOK;%s\r\n\r\n",msg);
00657         } else
00658             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00659 
00660     }   else {
00661         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00662     }
00663 }
00664 
00665 static void cyn_wifi_tcp_connection_blocking()
00666 {
00667     bool blocking = false;
00668     unsigned int timeout;
00669 
00670     uint8_t *arg;
00671 
00672     if (cyntecGetCommandTokenCnt() == 3) {
00673 
00674         arg = cyntecGetCommandArgument(0, NULL);
00675         if (arg[0] == '1')
00676             blocking = true;
00677 
00678         tcpConnect.set_blocking(blocking, 1500);
00679         cyntecPrintOk();
00680 
00681     } else if (cyntecGetCommandTokenCnt() == 4) {
00682         arg = cyntecGetCommandArgument(0, NULL);
00683         if (arg[0] == '1')
00684             blocking = true;
00685 
00686         arg = cyntecGetCommandArgument(1, NULL);
00687         timeout = (unsigned int)atoi((const char *)arg);
00688 
00689         tcpConnect.set_blocking(blocking, timeout);
00690         cyntecPrintOk();
00691 
00692     }   else {
00693         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00694     }
00695 }
00696 
00697 static void cyn_wifi_tcp_connection_close()
00698 {
00699     bool shutdown = true;
00700     uint8_t *arg;
00701 
00702     if (cyntecGetCommandTokenCnt() == 2) {
00703         if ( tcpConnect.close(shutdown) == 0 )
00704             cyntecPrintOk();
00705         else
00706             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00707 
00708     }   else if (cyntecGetCommandTokenCnt() == 3) {
00709         arg = cyntecGetCommandArgument(0, NULL);
00710         if (arg[0] == '0')
00711             shutdown = false;
00712 
00713         if ( tcpConnect.close(shutdown) == 0 )
00714             cyntecPrintOk();
00715         else
00716             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00717 
00718     }   else {
00719         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00720     }
00721 }
00722 
00723 /////////**** WIFI UDP Socket Implement ****//////////
00724 static void cyn_wifi_udp_init()
00725 {
00726     if (cyntecGetCommandTokenCnt() == 2) {
00727         if ( udpSocket.init() == 0 )
00728             cyntecPrintOk();
00729         else
00730             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00731 
00732     } else {
00733         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00734     }
00735 }
00736 static void cyn_wifi_udp_bind()
00737 {
00738     int port = 1;
00739     if (cyntecGetCommandTokenCnt() == 2) {
00740         udpSocket.bind(port);
00741         cyntecPrintOk();
00742 //          if ( udpSocket.bind(port) == 0 )
00743 //              cyntecPrintOk();
00744 //          else
00745 //              cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00746 
00747     }   else if (cyntecGetCommandTokenCnt() == 3) {
00748         port = atoi((const char*)(cyntecGetCommandArgument(0, NULL)));
00749 
00750         if (port < 0) {
00751             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00752             return;
00753         }
00754 
00755         udpSocket.bind(port);
00756         cyntecPrintOk();
00757 //          if ( udpSocket.bind(port) == 0 )
00758 //              cyntecPrintOk();
00759 //          else
00760 //              cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00761 
00762     }   else {
00763         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00764     }
00765 }
00766 static void cyn_wifi_udp_set_broadcasting()
00767 {
00768     bool broadcast = true;
00769     uint8_t *arg;
00770 
00771     if (cyntecGetCommandTokenCnt() == 2) {
00772         if ( udpSocket.set_broadcasting(broadcast) == 0 )
00773             cyntecPrintOk();
00774         else
00775             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00776 
00777     }   else if (cyntecGetCommandTokenCnt() == 3) {
00778         arg = cyntecGetCommandArgument(0, NULL);
00779         if (arg[0] == '0')
00780             broadcast = false;
00781 
00782         if ( udpSocket.set_broadcasting(broadcast) == 0 )
00783             cyntecPrintOk();
00784         else
00785             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00786 
00787     }   else {
00788         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00789     }
00790 }
00791 
00792 static void cyn_wifi_udp_send_to()
00793 {
00794     char msg[UDP_SEND_MAX_LEN+1];
00795     int sendData;
00796     uint8_t * argAllBuf = cyntecGetCommandTotalBuffer();
00797     int sendLen = 0;  // Maximum 1400, need to be integer
00798     uint8_t sendLenCharNum = 0; // Maximum 4
00799     uint8_t *argLeng = cyntecGetCommandArgument(0,&sendLenCharNum);
00800     sendLen = cyntecAtoi(argLeng,sendLenCharNum);
00801     //sendLen = cyntecAtoInt(argLeng); 
00802     if (sendLen > UDP_SEND_MAX_LEN || sendLen < 0) {
00803         cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00804         return;
00805     }
00806     if (cyntecGetCommandTokenCnt() <4 ) {
00807         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00808         return;
00809     }
00810     if (cyntecGetCommandTokenCnt() >= 4) {
00811 
00812         /* handle Message arg */
00813 #ifdef DELTA_WIFI_DEBUG
00814         console.printf("sendLen:%i,Buf:%s,Index:%i\r\n",sendLen,&argAllBuf[15+sendLenCharNum],cyntecGetTotalIndex());
00815 #endif
00816         memset( msg, 0, UDP_SEND_MAX_LEN+1);
00817         for (int i=0; i<sendLen; i++)
00818             msg[i] = argAllBuf[15+sendLenCharNum+i];
00819 #ifdef DELTA_WIFI_DEBUG
00820         console.printf("msg:%s\r\n",msg);
00821 #endif
00822         sendData = udpSocket.sendTo(cliEndpoint, msg, sendLen);
00823         if ( sendData >= 0 ) {
00824             console.printf("\r\nOK;");
00825             console.printf("%i\r\n\r\n",sendData);
00826         } else
00827             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00828     }
00829 }
00830 
00831 static void cyn_wifi_udp_received_from()
00832 {
00833     char msg[UDP_SEND_MAX_LEN+1];
00834     int argLen = 0;
00835 
00836     if (cyntecGetCommandTokenCnt() == 3) {
00837         /* handle Message arg */
00838         argLen = atoi((const char *)cyntecGetCommandArgument(0, NULL));
00839 
00840         if (argLen > UDP_SEND_MAX_LEN || argLen < 0) {
00841             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00842             return;
00843         }
00844 
00845         memset( msg, 0, UDP_SEND_MAX_LEN+1);
00846 
00847         if ( udpSocket.receiveFrom(cliEndpoint, msg, argLen) >= 0 ) {
00848             console.printf("\r\nOK;%s\r\n\r\n",msg);
00849         } else
00850             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00851 
00852     }   else {
00853         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00854     }
00855 }
00856 static void cyn_wifi_udp_blocking()
00857 {
00858     bool blocking = false;
00859     unsigned int timeout;
00860 
00861     uint8_t *arg;
00862     if (cyntecGetCommandTokenCnt() == 3) {
00863 
00864         arg = cyntecGetCommandArgument(0, NULL);
00865         if (arg[0] == '1')
00866             blocking = true;
00867 
00868         udpSocket.set_blocking(blocking, 1500);
00869         cyntecPrintOk();
00870 
00871     } else if (cyntecGetCommandTokenCnt() == 4) {
00872         arg = cyntecGetCommandArgument(0, NULL);
00873         if (arg[0] == '1')
00874             blocking = true;
00875 
00876         arg = cyntecGetCommandArgument(1, NULL);
00877         timeout = (unsigned int)atoi((const char *)arg);
00878 
00879         udpSocket.set_blocking(blocking, timeout);
00880         cyntecPrintOk();
00881 
00882     }   else {
00883         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00884     }
00885 }
00886 static void cyn_wifi_udp_close()
00887 {
00888     bool shutdown = true;
00889     uint8_t *arg;
00890 
00891     if (cyntecGetCommandTokenCnt() == 2) {
00892         if ( udpSocket.close(shutdown) == 0 )
00893             cyntecPrintOk();
00894         else
00895             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00896 
00897     }   else if (cyntecGetCommandTokenCnt() == 3) {
00898         arg = cyntecGetCommandArgument(0, NULL);
00899         if (arg[0] == '0')
00900             shutdown = false;
00901 
00902         if ( udpSocket.close(shutdown) == 0 )
00903             cyntecPrintOk();
00904         else
00905             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00906 
00907     }   else {
00908         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00909     }
00910 }
00911 
00912 
00913 
00914 /////////**** WIFI UDP Endpoint Implement ****//////////
00915 static void cyn_wifi_udp_endpoint_reset_address()
00916 {
00917     if (cyntecGetCommandTokenCnt() == 2) {
00918         cliEndpoint.reset_address();
00919         cyntecPrintOk();
00920     } else {
00921         cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
00922     }
00923 }
00924 static void cyn_wifi_udp_endpoint_address()
00925 {
00926     uint8_t argHost[STATIC_IP_MAX_LEN];
00927     int port;
00928 
00929     uint8_t *arg;
00930     uint8_t argLen;
00931 
00932     if ( cyntecGetCommandTokenCnt() == 4 ) {
00933         /* handle Host addr */
00934         arg = cyntecGetCommandArgument(0, &argLen);
00935 
00936         if ( cyntecIsValidIP(arg,argLen) != 0) {
00937             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00938             return;
00939         }
00940 
00941         memset( argHost, 0, STATIC_IP_MAX_LEN);
00942         memcpy( argHost, arg, argLen);
00943 
00944         /* Handle Port */
00945         port = atoi((const char *)(cyntecGetCommandArgument(1, NULL)));
00946         if (port < 0) {
00947             cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
00948             return;
00949         }
00950 
00951         if ( cliEndpoint.set_address((const char *)argHost, (const int)port) == 0)
00952             cyntecPrintOk();
00953         else
00954             cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
00955 
00956     } else {
00957         console.printf("\r\nOK;%s\r\n\r\n",cliEndpoint.get_address());
00958     }
00959 }
00960 //static void cyn_wifi_udp_endpoint_get_address(){}
00961 static void cyn_wifi_udp_endpoint_port()   // 2015/1/20: Lester add
00962 {
00963     console.printf("\r\nOK;%d\r\n\r\n",cliEndpoint.get_port());
00964 }
00965 
00966 
00967 CyntecCommandEntry wifiCommandSets[] = {
00968     /////////**** WIFI Device ****//////////
00969     {"device_sleep", cyn_wifi_device_sleep, NULL, "Set WIFI module to sleep mode"},
00970     {"device_network", cyn_wifi_device_network, NULL, " <SSID> <PASSWORD> <SECURITY> <PRIORITY> Set SSID and PASSWORD for WIFI module"},
00971     {"device_apscan", cyn_wifi_device_ap_scan, NULL, "Scan for available access point on all channels."},
00972     {"device_setap",cyn_wifi_set_ap, NULL, " <SSID> <PASSWORD> <SECURITY> <CHANNEL>Set Access Point in given configuration"},
00973 
00974     /////////**** WIFI Ethernet ****//////////
00975     {"eth_init", cyn_wifi_ethernet_init, NULL, " <STATIC IP> Initialize the interface to use DHCP"},
00976     {"eth_connect", cyn_wifi_ethernet_connect, NULL, "<TIMEOUT MS> Bring up the WiFi connection"},
00977     {"eth_disconnect", cyn_wifi_ethernet_disconnect, NULL, "Bring the interface down"},
00978     {"eth_mac", cyn_wifi_ethernet_mac, NULL, "Get MAC addr of Ethernet Interface"},
00979     {"eth_ip", cyn_wifi_ethernet_ip, NULL, "Get IP addr of Ehternet Interface"},
00980     {"eth_isConnect", cyn_wifi_ethernet_isConnected, NULL, "Check if the device is connected to Access Point"},
00981 
00982     /////////**** WIFI TCP Socket Server ****//////////
00983     {"tcp_server_bind", cyn_wifi_tcp_server_bind, NULL, " <PORT> Bind a socket to a port"},
00984     {"tcp_server_listen", cyn_wifi_tcp_server_listen, NULL, "Start listening for incomming connections"},
00985     {"tcp_server_accept", cyn_wifi_tcp_server_accept, NULL, "Accept a new connection"},
00986     {"tcp_server_blocking", cyn_wifi_tcp_server_blocking, NULL, " <SETTING> <TIMEOUT MS> Set blocking mode and timeout"},
00987     {"tcp_server_close", cyn_wifi_tcp_server_close, NULL, " <SHUTDOWN> Close the socket"},
00988 
00989     /////////**** WIFI TCP Socket Connection ****//////////
00990     {"tcp_connection_connect", cyn_wifi_tcp_connection_connect, NULL, " <IPADDR> <PORT> Connects TCP socket to the server"},
00991     {"tcp_connection_is_connect", cyn_wifi_tcp_connection_is_connected, NULL, "Check if the socket is connected"},
00992     {"tcp_connection_send", cyn_wifi_tcp_connection_send, NULL, " <DATALEN> <DATA> Send data to the remote host"},
00993     {"tcp_connection_send_all", cyn_wifi_tcp_connection_send_all, NULL, " <DATALEN> <DATA> Send all the data to the remote host"},
00994     {"tcp_connection_receive", cyn_wifi_tcp_connection_receive, NULL, " <DATALEN> Receive data from the remote host"},
00995     {"tcp_connection_receive_all", cyn_wifi_tcp_connection_receive_all, NULL, "<DATALEN> Receive all the data from the remote host"},
00996     {"tcp_connection_blocking", cyn_wifi_tcp_connection_blocking, NULL, "<SETTING> <TIMEOUT MS> Set blocking mode and timeout"},
00997     {"tcp_connection_close", cyn_wifi_tcp_connection_close, NULL, "<SHUTDOWN> Close the connection"},
00998 
00999     /////////**** WIFI UDP Socket ****//////////
01000     {"udp_init", cyn_wifi_udp_init, NULL, "Init UDP Client Socket"},
01001     {"udp_bind", cyn_wifi_udp_bind, NULL, " <PORT> Bind UDP Server Socket to a port"},
01002     {"udp_set_broadcasting", cyn_wifi_udp_set_broadcasting, NULL, " <IS_BROADCAST> Set socket in broadcasting"},
01003     {"udp_send_to", cyn_wifi_udp_send_to, NULL, " <DATALEN> <DATA> Send a packet to a remote endpoint"},
01004     {"udp_received_from", cyn_wifi_udp_received_from, NULL, " <DATALEN> Receive a packet from a remote endpont"},
01005     {"udp_blocking", cyn_wifi_udp_blocking, NULL, " <DATALEN> Set blocking mode and timeout"},
01006     {"udp_close", cyn_wifi_udp_close, NULL, " <SHUTDOWN> Close the socket"},
01007 
01008     /////////**** WIFI UDP Endpoint ****//////////
01009     {"udp_endpoint_reset", cyn_wifi_udp_endpoint_reset_address, NULL, "Reset the address of this endpoint"},
01010     {"udp_endpoint_address", cyn_wifi_udp_endpoint_address, NULL, " <IPADDR> <PORT> Set/Get the address of this endpoint"},
01011     {"udp_endpoint_port", cyn_wifi_udp_endpoint_port, NULL, "Get the port of this endpoint"},
01012 
01013     {NULL, NULL, NULL, NULL},
01014 };
01015 
01016 
01017