CLI example for NNN50
Dependencies: NNN50_WIFI_API
Fork of NNN50_WiFi_HelloWorld by
CLI_Source/wifi_cli.cpp
- Committer:
- silviaChen
- Date:
- 2017-09-14
- Revision:
- 9:871fc0231c7f
File content as of revision 9:871fc0231c7f:
/** * File: wifi-cli.c * Description: WIFI CLI commands used by all applications WIFI of profile. * * Copyright 2015 by CYNTEC Corporation. All rights reserved. * Author: Lester Lee */ #include "wifi_cli.h" #define AP_SSID_MAX_LEN 33 #define AP_PW_MAX_LEN 64 #define STATIC_IP_MAX_LEN 15 #define TCP_SEND_MAX_LEN 1400 #define UDP_SEND_MAX_LEN 1400 #define DELTA_WIFI_DEBUG #define WIFI_MAX_AP_NUMBER 15 // General configuration parameters WIFIDevice wifiDevice; EthernetInterface eth; TCPSocketConnection tcpConnect; UDPSocket udpSocket; Endpoint cliEndpoint; TCPSocketServer tcpServer; char static_ip[STATIC_IP_MAX_LEN] = "192.168.1.1"; extern Serial console; extern const char* cyntecCommandErrorNames[]; static uint8_t is_Listen = false; static tstrM2mWifiscanResult saveAP[WIFI_MAX_AP_NUMBER]; static bool wifiIniState = false; typedef struct deviceNetwork_s { char ap_ssid[AP_SSID_MAX_LEN+1]; char ap_pw[AP_PW_MAX_LEN+1]; uint8_t ap_sec; } deviceNetwork_t; deviceNetwork_t devNetwork[4]; // 0-2 for setNetwork, 3 for set_ap static uint8_t cyntecIsValidIP(uint8_t *startIdx, uint8_t totalLen) { uint8_t *ipIdx = startIdx; uint8_t *tempIdx = ipIdx; uint8_t IPAddr[3]; //uint8_t IPTokenLen = 0; int16_t ipToken; while ( (tempIdx - startIdx) <= totalLen ) { if (*tempIdx == '.' || ((tempIdx - startIdx) == totalLen)) { memset( IPAddr, 0, 3); memcpy( IPAddr, ipIdx, (tempIdx - ipIdx)); ipToken = atoi((const char *)IPAddr); if (ipToken > 255 || ipToken < 0) return 1; ipIdx = tempIdx + 1; } tempIdx++; } return 0; } static void cyntecPrintOk(void) { console.printf("\r\nOK\r\n\r\n"); } static void cyntecPrintError(uint8_t errIdx) { console.printf("\r\nERROR;%s\r\n\r\n",cyntecCommandErrorNames[errIdx]); } /////////**** WIFI Device Implement ****////////// static void cyn_wifi_device_sleep() { if (cyntecGetCommandTokenCnt() != 2) { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); return; } uint8_t err_code = wifiDevice.sleep(); if (err_code == 0) cyntecPrintOk(); else { console.printf("ERROR;%d\r\n",err_code); return; } } static void cyn_wifi_device_network() { //if (wifiIniState == true) { // cyntecPrintError(CYNTEC_CMD_ERR_WRONG_CMD_ORDER); // return; // } if (cyntecGetCommandTokenCnt() == 6) { uint8_t argLen = 0; uint8_t *argSSID; uint8_t *argPW; uint8_t *argSec; uint8_t security = 0; uint8_t *argPriority; /* 0~2, 0 is highest */ uint8_t priority = 0; /* handle priority */ argPriority = cyntecGetCommandArgument(3, NULL); priority = atoi((const char*)argPriority); if(priority > 2) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } /* handle SSID */ argSSID = cyntecGetCommandArgument(0, &argLen); if ( argLen > AP_SSID_MAX_LEN ) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( devNetwork[priority].ap_ssid , 0, AP_SSID_MAX_LEN+1); memcpy( devNetwork[priority].ap_ssid, argSSID, argLen); /* handle Password */ argPW = cyntecGetCommandArgument(1, &argLen); if ( argLen > AP_PW_MAX_LEN ) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( devNetwork[priority].ap_pw, 0, AP_PW_MAX_LEN+1); memcpy( devNetwork[priority].ap_pw, argPW, argLen); /* handle security */ argSec = cyntecGetCommandArgument(2, &argLen); //security = atoi((const char*)argSec); char numSec[1]; memcpy( numSec, argSec, 1); security = atoi(numSec); if(security > 4) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } devNetwork[priority].ap_sec = security; /* call setup API */ wifiDevice.setNetwork(devNetwork[priority].ap_sec, devNetwork[priority].ap_ssid, devNetwork[priority].ap_pw, priority); cyntecPrintOk(); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } void scanCallback(tstrM2mWifiscanResult result) { console.printf("scanCallback\r\n"); console.printf("[%d], ", result.u8index); console.printf("%d, ", result.s8rssi); console.printf("%d, ", result.u8AuthType); console.printf("%d,", result.u8ch); for(int i=0;i<6;i++) { console.printf(" %02x", result.au8BSSID[i]); } console.printf(", "); console.printf("%s\r\n", result.au8SSID); } static void cyn_wifi_device_ap_scan() { if (wifiIniState == false) { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_CMD_ORDER); return; } memset(saveAP,0,sizeof(saveAP)); int scanResult = 0; scanResult = wifiDevice.apScan(scanCallback); if (scanResult >= 0) { #ifdef DELTA_WIFI_DEBUG console.printf("scan AP number:%d\r\n",scanResult); #endif cyntecPrintOk(); return; } else { cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); return; } } static void cyn_wifi_set_ap() { if (cyntecGetCommandTokenCnt() != 6) { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } uint8_t argLen = 0; uint8_t * argSSID; uint8_t * argPW; uint8_t * argSEC; uint8_t * argCH; /* handle SSID */ argSSID = cyntecGetCommandArgument(0, &argLen); if ( argLen > AP_SSID_MAX_LEN ) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( devNetwork[3].ap_ssid , 0, AP_SSID_MAX_LEN+1); memcpy( devNetwork[3].ap_ssid, argSSID, argLen); /* handle Password */ argPW = cyntecGetCommandArgument(1, &argLen); if ( argLen > AP_PW_MAX_LEN ) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( devNetwork[3].ap_pw, 0, AP_PW_MAX_LEN+1); memcpy( devNetwork[3].ap_pw, argPW, argLen); /* handle Security */ tenuM2mSecType setSec = M2M_WIFI_SEC_OPEN; argSEC = cyntecGetCommandArgument(2, &argLen); char numSec[argLen]; memcpy( numSec, argSEC, argLen); int sec = atoi(numSec); if(sec == 0) setSec = M2M_WIFI_SEC_OPEN; else if(sec == 1) setSec = M2M_WIFI_SEC_WEP; else { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } /* handle Channel */ argCH = cyntecGetCommandArgument(3, &argLen); int setChan = atoi((char *)argCH); if (setChan > 14 || setChan < 1) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } #ifdef DELTA_WIFI_DEBUG console.printf("argSSID:%s\r\n",devNetwork[3].ap_ssid); console.printf("argPW:%s\r\n",devNetwork[3].ap_pw); console.printf("sec:%i\r\n",sec); console.printf("setChan:%i\r\n",setChan); #endif /* call Enable Access Point API */ if (wifiDevice.enableAccessPoint(devNetwork[3].ap_ssid, devNetwork[3].ap_pw, static_ip, setSec, setChan) != 0) cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); else cyntecPrintOk(); } /////////**** WIFI Ethernet Implement ****////////// static void cyn_wifi_ethernet_init() { uint8_t argLen = 0; uint8_t *argIP; //EthernetInterface ethInterface; uint8_t result; if (cyntecGetCommandTokenCnt() == 2) { /* use DHCP to get IP */ result = eth.init(); if ( result == 0 ) { cyntecPrintOk(); wifiIniState = true; } else { cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } } else if ( cyntecGetCommandTokenCnt() == 3 ) { /* use static to get IP */ argIP = cyntecGetCommandArgument(0, &argLen); if ( cyntecIsValidIP(argIP,argLen) != 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( static_ip, 0, STATIC_IP_MAX_LEN); memcpy( static_ip, argIP, argLen); if ( eth.init((const char *)static_ip, NULL, NULL) == 0) { cyntecPrintOk(); wifiIniState = true; } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } return; } static void cyn_wifi_ethernet_connect() { int timeout_ms = 5000; uint8_t *argTimeout; //EthernetInterface ethInterface; if (cyntecGetCommandTokenCnt() != 2 & cyntecGetCommandTokenCnt() != 3) cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); if (cyntecGetCommandTokenCnt() == 2) { #ifdef DELTA_WIFI_DEBUG console.printf("\r\n"); console.printf("Connecting..., Waiting for 5000 ms..."); console.printf("\r\n\r\n"); #endif if (eth.connect() == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } if (cyntecGetCommandTokenCnt() == 3) { argTimeout = cyntecGetCommandArgument(0, NULL); timeout_ms = atoi((const char*)argTimeout); if (timeout_ms < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } #ifdef DELTA_WIFI_DEBUG console.printf("\r\nConnecting..., Waiting for "); console.printf((char*)argTimeout); console.printf(" ms...\r\n\r\n"); #endif if (eth.connect(timeout_ms) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } return; } static void cyn_wifi_ethernet_disconnect() { if (eth.disconnect() == 0) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } static void cyn_wifi_ethernet_mac() { char mac_addr[19]; memset(mac_addr, 0, 19); memcpy(mac_addr, eth.getMACAddress(), 19); console.printf("\r\nOK;%s\r\n\r\n",mac_addr); return; } static void cyn_wifi_ethernet_ip() { char ip_addr[15] = "\0"; memset(ip_addr, 0, 15); memcpy(ip_addr, eth.getIPAddress(), 15); console.printf("\r\nOK;%s\r\n\r\n",ip_addr); return; } static void cyn_wifi_ethernet_isConnected() { bool is_connected = false; is_connected = wifiDevice.is_AP_connected(); if (is_connected == true) console.printf("\r\nOK;TRUE\r\n\r\n"); else console.printf("\r\nOK;FALSE\r\n\r\n"); } /////////**** WIFI TCP Socket Server Implement ****////////// static void cyn_wifi_tcp_server_bind() { int port = 1; if (cyntecGetCommandTokenCnt() == 2) { if (tcpServer.bind(port) == 0) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else if (cyntecGetCommandTokenCnt() == 3) { port = atoi((const char*)(cyntecGetCommandArgument(0, NULL))); #ifdef DELTA_WIFI_DEBUG console.printf("port:%i\r\n",port); #endif if (port < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } if ( tcpServer.bind(port) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_server_listen() { if (cyntecGetCommandTokenCnt() == 2) { if ( tcpServer.listen() == 0 ) { cyntecPrintOk(); is_Listen = true; } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_server_accept() { if (cyntecGetCommandTokenCnt() == 2) { if (is_Listen == false) { cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION); return; } if ( tcpServer.accept(tcpConnect) == 0 ) { cyntecPrintOk(); is_Listen = false; } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_server_blocking() { bool blocking = false; unsigned int timeout; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '1') blocking = true; tcpServer.set_blocking(blocking, 1500); cyntecPrintOk(); } else if (cyntecGetCommandTokenCnt() == 4) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '1') blocking = true; arg = cyntecGetCommandArgument(1, NULL); timeout = (unsigned int)atoi((const char *)arg); tcpServer.set_blocking(blocking, timeout); cyntecPrintOk(); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_server_close() { bool shutdown = true; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 2) { if ( tcpServer.close(shutdown) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '0') shutdown = false; if ( tcpServer.close(shutdown) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } /////////**** WIFI TCP Socket Connection ****////////// static void cyn_wifi_tcp_connection_connect() { uint8_t connect_ip[STATIC_IP_MAX_LEN]; int port; uint8_t argLen = 0; uint8_t *argIP; if (cyntecGetCommandTokenCnt() == 4) { /* handle IP arg */ argIP = cyntecGetCommandArgument(0, &argLen); memset( connect_ip, 0, STATIC_IP_MAX_LEN); memcpy( connect_ip, argIP, argLen); /* handle Port arg */ port = atoi((const char *)cyntecGetCommandArgument(1, NULL)); if ( tcpConnect.connect((const char *)connect_ip, port) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_connection_is_connected() { bool is_connected = false; is_connected = tcpConnect.is_connected(); if (is_connected == true) console.printf("\r\nOK;TRUE\r\n\r\n"); else console.printf("\r\nOK;FALSE\r\n\r\n"); } static void cyn_wifi_tcp_connection_send() { char msg[TCP_SEND_MAX_LEN+1]; int sendData; uint8_t * argAllBuf = cyntecGetCommandTotalBuffer(); int sendLen = 0; // Maximum 1400 uint8_t sendLenCharNum = 0; // Maximum 4 uint8_t *argLeng = cyntecGetCommandArgument(0,&sendLenCharNum); sendLen = cyntecAtoi(argLeng,sendLenCharNum); //sendLen = cyntecAtoInt(argLeng); console.printf("len:%d\r\n", sendLen); if (sendLen > TCP_SEND_MAX_LEN || sendLen < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } if (cyntecGetCommandTokenCnt() <4 ) { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); return; } if (cyntecGetCommandTokenCnt() >= 4) { if ( tcpConnect.is_connected() == false ) { cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION); return; } /* handle Message arg */ #ifdef DELTA_WIFI_DEBUG console.printf("sendLen:%i,Buf:%s,Index:%i\r\n",sendLen,&argAllBuf[26+sendLenCharNum],cyntecGetTotalIndex()); #endif memset( msg, 0, TCP_SEND_MAX_LEN+1); for (uint8_t i=0; i<sendLen; i++) msg[i] = argAllBuf[26+sendLenCharNum+i]; #ifdef DELTA_WIFI_DEBUG console.printf("msg:%s\r\n",msg); #endif sendData = tcpConnect.send(msg, sendLen); if ( sendData >= 0 ) { console.printf("\r\nOK;"); console.printf("%i\r\n\r\n",sendData); } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } } static void cyn_wifi_tcp_connection_send_all() { char msg[TCP_SEND_MAX_LEN+1]; int sendData; uint8_t * argAllBuf = cyntecGetCommandTotalBuffer(); int sendLen = 0; // Maximum 1400 uint8_t sendLenCharNum = 0; // Maximum 4 uint8_t *argLeng = cyntecGetCommandArgument(0,&sendLenCharNum); sendLen = cyntecAtoi(argLeng,sendLenCharNum); if (sendLen > TCP_SEND_MAX_LEN) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } if (cyntecGetCommandTokenCnt() <4 ) { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); return; } if (cyntecGetCommandTokenCnt() >= 4) { if ( tcpConnect.is_connected() == false ) { cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION); return; } /* handle Message arg */ #ifdef DELTA_WIFI_DEBUG console.printf("sendLen:%i,Buf:%s,Index:%i\r\n",sendLen,&argAllBuf[30+sendLenCharNum],cyntecGetTotalIndex()); #endif memset( msg, 0, TCP_SEND_MAX_LEN+1); for (uint8_t i=0; i<sendLen; i++) msg[i] = argAllBuf[30+sendLenCharNum+i]; #ifdef DELTA_WIFI_DEBUG console.printf("msg:%s\r\n",msg); #endif sendData = tcpConnect.send_all(msg, sendLen); if ( sendData >= 0 ) { console.printf("\r\nOK;"); console.printf("%i\r\n\r\n",sendData); } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } } static void cyn_wifi_tcp_connection_receive() { char msg[TCP_SEND_MAX_LEN+1]; int argLen = 0; if (cyntecGetCommandTokenCnt() == 3) { /* handle Message arg */ argLen = atoi((const char *)cyntecGetCommandArgument(0, NULL)); //console.printf("argLen:%d\r\n",argLen); if (argLen > TCP_SEND_MAX_LEN || argLen < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( msg, 0, TCP_SEND_MAX_LEN+1); if ( tcpConnect.receive(msg, argLen) >= 0 ) { console.printf("\r\nOK;%s\r\n\r\n",msg); } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_connection_receive_all() { char msg[TCP_SEND_MAX_LEN+1]; int argLen = 0; if (cyntecGetCommandTokenCnt() == 3) { /* handle Message arg */ argLen = atoi((const char *)cyntecGetCommandArgument(0, NULL)); if (argLen > TCP_SEND_MAX_LEN || argLen < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( msg, 0, TCP_SEND_MAX_LEN+1); if ( tcpConnect.receive_all(msg, argLen) >= 0 ) { console.printf("\r\nOK;%s\r\n\r\n",msg); } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_connection_blocking() { bool blocking = false; unsigned int timeout; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '1') blocking = true; tcpConnect.set_blocking(blocking, 1500); cyntecPrintOk(); } else if (cyntecGetCommandTokenCnt() == 4) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '1') blocking = true; arg = cyntecGetCommandArgument(1, NULL); timeout = (unsigned int)atoi((const char *)arg); tcpConnect.set_blocking(blocking, timeout); cyntecPrintOk(); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_tcp_connection_close() { bool shutdown = true; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 2) { if ( tcpConnect.close(shutdown) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '0') shutdown = false; if ( tcpConnect.close(shutdown) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } /////////**** WIFI UDP Socket Implement ****////////// static void cyn_wifi_udp_init() { if (cyntecGetCommandTokenCnt() == 2) { if ( udpSocket.init() == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_udp_bind() { int port = 1; if (cyntecGetCommandTokenCnt() == 2) { udpSocket.bind(port); cyntecPrintOk(); // if ( udpSocket.bind(port) == 0 ) // cyntecPrintOk(); // else // cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else if (cyntecGetCommandTokenCnt() == 3) { port = atoi((const char*)(cyntecGetCommandArgument(0, NULL))); if (port < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } udpSocket.bind(port); cyntecPrintOk(); // if ( udpSocket.bind(port) == 0 ) // cyntecPrintOk(); // else // cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_udp_set_broadcasting() { bool broadcast = true; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 2) { if ( udpSocket.set_broadcasting(broadcast) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '0') broadcast = false; if ( udpSocket.set_broadcasting(broadcast) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_udp_send_to() { char msg[UDP_SEND_MAX_LEN+1]; int sendData; uint8_t * argAllBuf = cyntecGetCommandTotalBuffer(); int sendLen = 0; // Maximum 1400, need to be integer uint8_t sendLenCharNum = 0; // Maximum 4 uint8_t *argLeng = cyntecGetCommandArgument(0,&sendLenCharNum); sendLen = cyntecAtoi(argLeng,sendLenCharNum); //sendLen = cyntecAtoInt(argLeng); if (sendLen > UDP_SEND_MAX_LEN || sendLen < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } if (cyntecGetCommandTokenCnt() <4 ) { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); return; } if (cyntecGetCommandTokenCnt() >= 4) { /* handle Message arg */ #ifdef DELTA_WIFI_DEBUG console.printf("sendLen:%i,Buf:%s,Index:%i\r\n",sendLen,&argAllBuf[15+sendLenCharNum],cyntecGetTotalIndex()); #endif memset( msg, 0, UDP_SEND_MAX_LEN+1); for (int i=0; i<sendLen; i++) msg[i] = argAllBuf[15+sendLenCharNum+i]; #ifdef DELTA_WIFI_DEBUG console.printf("msg:%s\r\n",msg); #endif sendData = udpSocket.sendTo(cliEndpoint, msg, sendLen); if ( sendData >= 0 ) { console.printf("\r\nOK;"); console.printf("%i\r\n\r\n",sendData); } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } } static void cyn_wifi_udp_received_from() { char msg[UDP_SEND_MAX_LEN+1]; int argLen = 0; if (cyntecGetCommandTokenCnt() == 3) { /* handle Message arg */ argLen = atoi((const char *)cyntecGetCommandArgument(0, NULL)); if (argLen > UDP_SEND_MAX_LEN || argLen < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( msg, 0, UDP_SEND_MAX_LEN+1); if ( udpSocket.receiveFrom(cliEndpoint, msg, argLen) >= 0 ) { console.printf("\r\nOK;%s\r\n\r\n",msg); } else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_udp_blocking() { bool blocking = false; unsigned int timeout; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '1') blocking = true; udpSocket.set_blocking(blocking, 1500); cyntecPrintOk(); } else if (cyntecGetCommandTokenCnt() == 4) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '1') blocking = true; arg = cyntecGetCommandArgument(1, NULL); timeout = (unsigned int)atoi((const char *)arg); udpSocket.set_blocking(blocking, timeout); cyntecPrintOk(); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_udp_close() { bool shutdown = true; uint8_t *arg; if (cyntecGetCommandTokenCnt() == 2) { if ( udpSocket.close(shutdown) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else if (cyntecGetCommandTokenCnt() == 3) { arg = cyntecGetCommandArgument(0, NULL); if (arg[0] == '0') shutdown = false; if ( udpSocket.close(shutdown) == 0 ) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } /////////**** WIFI UDP Endpoint Implement ****////////// static void cyn_wifi_udp_endpoint_reset_address() { if (cyntecGetCommandTokenCnt() == 2) { cliEndpoint.reset_address(); cyntecPrintOk(); } else { cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS); } } static void cyn_wifi_udp_endpoint_address() { uint8_t argHost[STATIC_IP_MAX_LEN]; int port; uint8_t *arg; uint8_t argLen; if ( cyntecGetCommandTokenCnt() == 4 ) { /* handle Host addr */ arg = cyntecGetCommandArgument(0, &argLen); if ( cyntecIsValidIP(arg,argLen) != 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } memset( argHost, 0, STATIC_IP_MAX_LEN); memcpy( argHost, arg, argLen); /* Handle Port */ port = atoi((const char *)(cyntecGetCommandArgument(1, NULL))); if (port < 0) { cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE); return; } if ( cliEndpoint.set_address((const char *)argHost, (const int)port) == 0) cyntecPrintOk(); else cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL); } else { console.printf("\r\nOK;%s\r\n\r\n",cliEndpoint.get_address()); } } //static void cyn_wifi_udp_endpoint_get_address(){} static void cyn_wifi_udp_endpoint_port() // 2015/1/20: Lester add { console.printf("\r\nOK;%d\r\n\r\n",cliEndpoint.get_port()); } CyntecCommandEntry wifiCommandSets[] = { /////////**** WIFI Device ****////////// {"device_sleep", cyn_wifi_device_sleep, NULL, "Set WIFI module to sleep mode"}, {"device_network", cyn_wifi_device_network, NULL, " <SSID> <PASSWORD> <SECURITY> <PRIORITY> Set SSID and PASSWORD for WIFI module"}, {"device_apscan", cyn_wifi_device_ap_scan, NULL, "Scan for available access point on all channels."}, {"device_setap",cyn_wifi_set_ap, NULL, " <SSID> <PASSWORD> <SECURITY> <CHANNEL>Set Access Point in given configuration"}, /////////**** WIFI Ethernet ****////////// {"eth_init", cyn_wifi_ethernet_init, NULL, " <STATIC IP> Initialize the interface to use DHCP"}, {"eth_connect", cyn_wifi_ethernet_connect, NULL, "<TIMEOUT MS> Bring up the WiFi connection"}, {"eth_disconnect", cyn_wifi_ethernet_disconnect, NULL, "Bring the interface down"}, {"eth_mac", cyn_wifi_ethernet_mac, NULL, "Get MAC addr of Ethernet Interface"}, {"eth_ip", cyn_wifi_ethernet_ip, NULL, "Get IP addr of Ehternet Interface"}, {"eth_isConnect", cyn_wifi_ethernet_isConnected, NULL, "Check if the device is connected to Access Point"}, /////////**** WIFI TCP Socket Server ****////////// {"tcp_server_bind", cyn_wifi_tcp_server_bind, NULL, " <PORT> Bind a socket to a port"}, {"tcp_server_listen", cyn_wifi_tcp_server_listen, NULL, "Start listening for incomming connections"}, {"tcp_server_accept", cyn_wifi_tcp_server_accept, NULL, "Accept a new connection"}, {"tcp_server_blocking", cyn_wifi_tcp_server_blocking, NULL, " <SETTING> <TIMEOUT MS> Set blocking mode and timeout"}, {"tcp_server_close", cyn_wifi_tcp_server_close, NULL, " <SHUTDOWN> Close the socket"}, /////////**** WIFI TCP Socket Connection ****////////// {"tcp_connection_connect", cyn_wifi_tcp_connection_connect, NULL, " <IPADDR> <PORT> Connects TCP socket to the server"}, {"tcp_connection_is_connect", cyn_wifi_tcp_connection_is_connected, NULL, "Check if the socket is connected"}, {"tcp_connection_send", cyn_wifi_tcp_connection_send, NULL, " <DATALEN> <DATA> Send data to the remote host"}, {"tcp_connection_send_all", cyn_wifi_tcp_connection_send_all, NULL, " <DATALEN> <DATA> Send all the data to the remote host"}, {"tcp_connection_receive", cyn_wifi_tcp_connection_receive, NULL, " <DATALEN> Receive data from the remote host"}, {"tcp_connection_receive_all", cyn_wifi_tcp_connection_receive_all, NULL, "<DATALEN> Receive all the data from the remote host"}, {"tcp_connection_blocking", cyn_wifi_tcp_connection_blocking, NULL, "<SETTING> <TIMEOUT MS> Set blocking mode and timeout"}, {"tcp_connection_close", cyn_wifi_tcp_connection_close, NULL, "<SHUTDOWN> Close the connection"}, /////////**** WIFI UDP Socket ****////////// {"udp_init", cyn_wifi_udp_init, NULL, "Init UDP Client Socket"}, {"udp_bind", cyn_wifi_udp_bind, NULL, " <PORT> Bind UDP Server Socket to a port"}, {"udp_set_broadcasting", cyn_wifi_udp_set_broadcasting, NULL, " <IS_BROADCAST> Set socket in broadcasting"}, {"udp_send_to", cyn_wifi_udp_send_to, NULL, " <DATALEN> <DATA> Send a packet to a remote endpoint"}, {"udp_received_from", cyn_wifi_udp_received_from, NULL, " <DATALEN> Receive a packet from a remote endpont"}, {"udp_blocking", cyn_wifi_udp_blocking, NULL, " <DATALEN> Set blocking mode and timeout"}, {"udp_close", cyn_wifi_udp_close, NULL, " <SHUTDOWN> Close the socket"}, /////////**** WIFI UDP Endpoint ****////////// {"udp_endpoint_reset", cyn_wifi_udp_endpoint_reset_address, NULL, "Reset the address of this endpoint"}, {"udp_endpoint_address", cyn_wifi_udp_endpoint_address, NULL, " <IPADDR> <PORT> Set/Get the address of this endpoint"}, {"udp_endpoint_port", cyn_wifi_udp_endpoint_port, NULL, "Get the port of this endpoint"}, {NULL, NULL, NULL, NULL}, };