Delta / Mbed OS Delta_CLI

Fork of NNN40_CLI by Delta

CLI_Source/wifi_cli.cpp

Committer:
gillwei7
Date:
2015-09-11
Revision:
0:5c195ab2f696
Child:
3:38ec8ad317f4

File content as of revision 0:5c195ab2f696:

/**
 * 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 <stdlib.h>
#include <string.h>
#include "mbed.h"
#include "wifi_cli.h"
#include "command-interpreter.h"
#include "WIFIDevice.h"
#include "EthernetInterface.h"

#define AP_SSID_MAX_LEN 33
#define AP_PW_MAX_LEN 64
#define STATIC_IP_MAX_LEN 15
#define TCP_SEND_MAX_LEN 128
#define UDP_SEND_MAX_LEN 128

uint8_t static_ip[STATIC_IP_MAX_LEN] = "192.168.1.100";

WIFIDevice wifiDevice;
extern Serial console;
extern const char* cyntecCommandErrorNames[];

typedef struct deviceNetwork_s {
	char ap_ssid[AP_SSID_MAX_LEN+1];
	char ap_pw[AP_PW_MAX_LEN+1];
} deviceNetwork_t;

deviceNetwork_t devNetwork[3];

TCPSocketConnection tcpConnect;
UDPSocket udpSocket;
Endpoint cliEndpoint;
TCPSocketServer tcpServer;

static uint8_t is_Listen = false;

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()
{
		cyntecPrintOk();
		wifiDevice.sleep();
}
static void cyn_wifi_device_switch()
{
//Set/Get device switch for BLE module
		/* BLE => devSwitched:0 , WIFI => devSwitched:1 */
		int devSwitched = 0;
		if (cyntecGetCommandTokenCnt() == 3)
		{
				uint8_t *arg = cyntecGetCommandArgument(0, NULL);
				if (*arg == '1')
				{
					/* set switched to WIFI */
					if ( wifiDevice.setSwitch(1) == 0 )
							cyntecPrintOk();
					else
							cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
				} else if (*arg == '0') {
					/* set switched to BLE */
					if ( wifiDevice.setSwitch(0) == 0 )
							cyntecPrintOk();
					else
							cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
				} else {
					cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
				}
		} else {
				devSwitched = wifiDevice.getSwitch();
				console.printf("\r\nOK;");
				if ( devSwitched )
					console.printf("WIFI\r\n\r\n");
				else
					console.printf("BLE\r\n\r\n");
		}
}
static void cyn_wifi_device_network()
{
		if (cyntecGetCommandTokenCnt() == 5) 
		{
				uint8_t argLen = 0;
				uint8_t *argSSID;
				uint8_t *argPW;
				uint8_t *argPriority;
				/* 0~2, 0 is highest */
				uint8_t priority = 0;
				
				/* handle priority */
				argPriority = cyntecGetCommandArgument(2, NULL);
				switch(*argPriority) {
					case '0':
						priority = 0;
						break;
					case '1':
						priority = 1;
						break;
					case '2':
						priority = 2;
						break;
					default:
						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);
				
				/* call setup API */
				wifiDevice.setNetwork(devNetwork[priority].ap_ssid, devNetwork[priority].ap_pw, priority);
				cyntecPrintOk();
		} else {
				cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
		}
}
/////////**** WIFI Ethernet Implement ****//////////
static void cyn_wifi_ethernet_init()
{
		uint8_t argLen = 0;
		uint8_t *argIP;		
		EthernetInterface ethInterface;
	
		if (cyntecGetCommandTokenCnt() == 2) 
		{
			/* use DHCP to get IP */
			if ( ethInterface.init() == 0 )
				cyntecPrintOk();
			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 ( ethInterface.init((const char *)static_ip, NULL, NULL) == 0)
				cyntecPrintOk();
			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 = 35000;
		uint8_t *argTimeout;
		EthernetInterface ethInterface;
	
		if (cyntecGetCommandTokenCnt() == 2) 	
		{
#ifdef DELTA_WIFI_DEBUG			
			simple_uart_putstring((const uint8_t *)"\r\n");
			simple_uart_putstring((const uint8_t *)"Connecting..., Waiting for 35000 ms...");
			simple_uart_putstring((const uint8_t *)"\r\n\r\n");			
#endif			
			
			if ( ethInterface.connect() == 0 )
				cyntecPrintOk();
			else
				cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
			
		}	else 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			
			simple_uart_putstring((const uint8_t *)"\r\nConnecting..., Waiting for ");
			simple_uart_putstring((const uint8_t *)argTimeout);
			simple_uart_putstring((const uint8_t *)" ms...\r\n\r\n");
#endif			
			if ( ethInterface.connect(timeout_ms) == 0 )
				cyntecPrintOk();
			else
				cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);	
			
		}	else {
			cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
		}
		return;
}
static void cyn_wifi_ethernet_disconnect()
{
		EthernetInterface ethInterface;
	
		ethInterface.disconnect();
		cyntecPrintOk();
		return;
}
static void cyn_wifi_ethernet_mac()
{
	EthernetInterface ethInterface;
	
	char mac_addr[19];
	memset(mac_addr, 0, 19);
	memcpy(mac_addr, ethInterface.getMACAddress(), 19);

	console.printf("\r\nOK;%s\r\n\r\n",mac_addr);
	
	return;
}
static void cyn_wifi_ethernet_ip()
{
	EthernetInterface ethInterface;
	
	char ip_addr[15] = "\0";
	memset(ip_addr, 0, 15);
	memcpy(ip_addr, ethInterface.getIPAddress(), 15);

	console.printf("\r\nOK;%s\r\n\r\n",ip_addr);
	
	return;
}
/* not implement yet... */
////static void cyn_wifi_ethernet_gateway(){}
////static void cyn_wifi_ethernet_mask(){}
/* not implement yet... */

/////////**** WIFI TCP Socket Server Implement ****//////////
static void cyn_wifi_tcp_server_bind()
{
//		TCPSocketServer tcpServer;
	
		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)));
			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()
{
//		TCPSocketServer tcpServer;
	
		int backlog = 1;
		if (cyntecGetCommandTokenCnt() == 2) 	
		{
			if ( tcpServer.listen(backlog) == 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()
{
//		TCPSocketServer tcpServer;
	
		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()
{
//		TCPSocketServer tcpServer;
	
		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()
{
//		TCPSocketServer tcpServer;
	
		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];
	
		uint8_t *arg;
		int argLen = 0;

		if (cyntecGetCommandTokenCnt() == 3) 	
		{
			if ( tcpConnect.is_connected() == false )
			{
				cyntecPrintError(CYNTEC_CMD_ERR_INVALID_STATE_TO_PERFORM_OPERATION);
				return;
			}
						
			/* handle Message arg */
			arg = cyntecGetCommandArgument(0, (uint8_t *)&argLen);
	
			if (argLen > TCP_SEND_MAX_LEN)
			{
				cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
				return;
			}			
			
			memset( msg, 0, TCP_SEND_MAX_LEN+1);
			memcpy( msg, arg, argLen);
			
			if ( tcpConnect.send(msg, argLen) >= 0 )
				cyntecPrintOk();
			else
				cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
			
		}	else {
			cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
		}
}
static void cyn_wifi_tcp_connection_send_all()
{
    char msg[TCP_SEND_MAX_LEN+1];
	
		uint8_t *arg;
		int argLen = 0;

		if (cyntecGetCommandTokenCnt() == 3) 	
		{
			/* handle Message arg */
			arg = cyntecGetCommandArgument(0, (uint8_t *)&argLen);
			
			if (argLen > TCP_SEND_MAX_LEN)
			{
				cyntecPrintError(CYNTEC_CMD_ERR_ARGUMENT_OUT_OF_RANGE);
				return;
			}
			
			memset( msg, 0, TCP_SEND_MAX_LEN+1);
			memcpy( msg, arg, argLen);
			
			if ( tcpConnect.send_all(msg, argLen) >= 0 )
				cyntecPrintOk();
			else
				cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
			
		}	else {
			cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
		}
}
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));
			
			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);
		}	
}
//static void cyn_wifi_tcp_connection_reset_address(){}
//static void cyn_wifi_tcp_connection_set_address(){}
//static void cyn_wifi_tcp_connection_address(){}
//static void cyn_wifi_tcp_connection_port(){}	

	
	
/////////**** 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];
	
		uint8_t *arg;
		int argLen = 0;

		if (cyntecGetCommandTokenCnt() == 3) 	
		{
			/* handle Message arg */
			arg = cyntecGetCommandArgument(0, (uint8_t *)&argLen);
			
			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);
			memcpy( msg, arg, argLen);
			
			if ( udpSocket.sendTo(cliEndpoint, msg, argLen) >= 0 )
				cyntecPrintOk();
			else
				cyntecPrintError(CYNTEC_CMD_ERR_CALL_FAIL);
			
		}	else {
			cyntecPrintError(CYNTEC_CMD_ERR_WRONG_NUMBER_OF_ARGUMENTS);
		}
}	
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()
{
	console.printf("\r\nOK;%d\r\n\r\n",cliEndpoint.get_port());
}	

// 2015/1/20: Lester add

CyntecCommandEntry wifiCommandSets[] = {

	/////////**** WIFI Device ****//////////
	{"device_sleep", cyn_wifi_device_sleep, NULL, "Set WIFI module to sleep mode"},
	{"device_switch", cyn_wifi_device_switch, NULL, "Set/Get device switch for WIFI module"},
	{"device_network", cyn_wifi_device_network, NULL, "Set SSID and PASSWORD for WIFI module"},
	
	/////////**** WIFI Ethernet ****//////////
	{"ethernet_init", cyn_wifi_ethernet_init, NULL, "Initialize the interface to use DHCP"},
	{"ethernet_connect", cyn_wifi_ethernet_connect, NULL, "Bring up the WiFi connection"},
	{"ethernet_disconnect", cyn_wifi_ethernet_disconnect, NULL, "Bring the interface down"},
	{"ethernet_mac", cyn_wifi_ethernet_mac, NULL, "Get MAC addr of Ehternet Interface"},
	{"ethernet_ip", cyn_wifi_ethernet_ip, NULL, "Get IP addr of Ehternet Interface"},
	/* not implement yet... */
	////{"ethernet_gateway", cyn_wifi_ethernet_gateway, NULL, "........."},
	////{"ethernet_mask", cyn_wifi_ethernet_mask, NULL, "........."},
	/* not implement yet... */
	
	/////////**** WIFI TCP Socket Server ****//////////
	{"tcp_server_bind", cyn_wifi_tcp_server_bind, NULL, "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, "Set blocking mode and timeout"},
	{"tcp_server_close", cyn_wifi_tcp_server_close, NULL, "Close the socket"},
	
	/////////**** WIFI TCP Socket Connection ****//////////
	{"tcp_connection_connect", cyn_wifi_tcp_connection_connect, NULL, "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, "Send data to the remote host"},
	{"tcp_connection_send_all", cyn_wifi_tcp_connection_send_all, NULL, "Send all the data to the remote host"},
	{"tcp_connection_receive", cyn_wifi_tcp_connection_receive, NULL, "Receive data from the remote host"},
	{"tcp_connection_receive_all", cyn_wifi_tcp_connection_receive_all, NULL, "Receive all the data from the remote host"},
	{"tcp_connection_blocking", cyn_wifi_tcp_connection_blocking, NULL, "Set blocking mode and timeout"},
	{"tcp_connection_close", cyn_wifi_tcp_connection_close, NULL, "Close the connection"},
//	{"tcp_connection_reset_address", cyn_wifi_tcp_connection_reset_address, NULL, "........."},
//	{"tcp_connection_address", cyn_wifi_tcp_connection_address, NULL, "........."},
//	{"tcp_connection_port", cyn_wifi_tcp_connection_port, NULL, "........."},
	
	/////////**** WIFI UDP Socket ****//////////
	{"udp_init", cyn_wifi_udp_init, NULL, "Init UDP Client Socket"},
	{"udp_bind", cyn_wifi_udp_bind, NULL, "Bind UDP Server Socket to a port"},
	{"udp_set_broadcasting", cyn_wifi_udp_set_broadcasting, NULL, "Set socket in broadcasting"},
	{"udp_send_to", cyn_wifi_udp_send_to, NULL, "Send a packet to a remote endpoint"},
	{"udp_received_from", cyn_wifi_udp_received_from, NULL, "Receive a packet from a remote endpont"},
	{"udp_blocking", cyn_wifi_udp_blocking, NULL, "Set blocking mode and timeout"},
	{"udp_close", cyn_wifi_udp_close, NULL, "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, "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},

};