Murata Type YD Wi-Fi driver
Dependents: easy-connect-type-yd
Revision 0:35a2186cf186, committed 2017-07-12
- Comitter:
- MACRUM
- Date:
- Wed Jul 12 10:49:10 2017 +0000
- Commit message:
- Initial commit
Changed in this revision
diff -r 000000000000 -r 35a2186cf186 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,3 @@ +# Type YD mbed-os driver +The driver for the murata type YD WiFi module +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/CBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/CBuffer.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,77 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef CIRCBUFFER_H_ +#define CIRCBUFFER_H_ + +template <class T> +class CircBuffer { +public: + CircBuffer(int length) { + write = 0; + read = 0; + size = length + 1; + buf = (T *)malloc(size * sizeof(T)); + }; + + bool isFull() { + return (((write + 1) % size) == read); + }; + + bool isEmpty() { + return (read == write); + }; + + void queue(T k) { + + if (isFull()) { + read++; + read %= size; + } + buf[write++] = k; + write %= size; + } + + void flush() { + read = 0; + write = 0; + } + + + uint32_t available() { + return (write >= read) ? write - read : size - read + write; + }; + + bool dequeue(T * c) { + bool empty = isEmpty(); + if (!empty) { + *c = buf[read++]; + read %= size; + } + return(!empty); + }; + +private: + volatile uint32_t write; + volatile uint32_t read; + uint32_t size; + T * buf; +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/MurataObject.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/MurataObject.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,23 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "SNICInterface/SNIC/MurataObject.h" +#include "stdarg.h" + + +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/MurataObject.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/MurataObject.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,49 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef _MURATA_OBJECT_H_ +#define _MURATA_OBJECT_H_ +#include "mbed.h" + +#define _DEBUG /* If this definition is enabled, debug log is output. */ +//#define _FUNC_TRACE + +#ifdef _DEBUG +//extern Serial pc; +#define DEBUG_PRINT(...) { printf(__VA_ARGS__);} + +#ifdef _FUNC_TRACE +#define FUNC_IN() { printf( "%s[%d]%s[tid:%x] IN\r\n", __FILE__, __LINE__, __FUNCTION__, Thread::gettid());} +#define FUNC_OUT() { printf( "%s[%d]%s[tid:%x] OUT\r\n", __FILE__, __LINE__, __FUNCTION__, Thread::gettid() );} +#else +#define FUNC_IN() +#define FUNC_OUT() +#endif + +#else +#define DEBUG_PRINT(...) +#define FUNC_IN() +#define FUNC_OUT() +#endif + +class C_MurataObject{ + +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/SNIC_Core.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/SNIC_Core.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,395 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "mbed.h" +#include "SNICInterface/SNIC/SNIC_Core.h" +#include "SNICInterface/SNIC/SNIC_UartMsgUtil.h" +#include <string> + +#include "mbed-trace/mbed_trace.h" +#define TRACE_GROUP "SNIC" + +/** Wait signal ID of UART recv */ +#define UART_DISPATCH_SIGNAL 0x00000002 + +#define UART_RECVBUF_SIZE 2048 +#define UART_THREAD_STACK_SIZE 1024//512 +#define UART_FIXED_HEADER_SIZE 3 +#define UART_FIXED_SIZE_IN_FRAME 6 +#define UART_RECV_QUEUE_TIMEOUT 500 + + +typedef struct +{ + tagMEMPOOL_BLOCK_T *mem_p; + unsigned int size; +}tagUART_RECVBUF_T; + +/* + Define the global buffer using the area for Ethernet. +*/ +#if defined(TARGET_LPC1768) +unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE] __attribute__((section("AHBSRAM1"))); +unsigned char gUART_COMMAND_BUF[UART_REQUEST_PAYLOAD_MAX] __attribute__((section("AHBSRAM1"))); +/** MemoryPool for payload of UART response */ +MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM> mMemPoolPayload __attribute__((section("AHBSRAM0"))); +/** MemoryPool for UART receive */ +MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mMemPoolUartRecv __attribute__((section("AHBSRAM0"))); +#else +unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE]; +unsigned char gUART_COMMAND_BUF[UART_REQUEST_PAYLOAD_MAX]; +/** MemoryPool for payload of UART response */ +MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM> mMemPoolPayload; +/** MemoryPool for UART receive */ +MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mMemPoolUartRecv; +#endif +Queue<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mUartRecvQueue; + +tagMEMPOOL_BLOCK_T *gUART_RCVBUF_p; +int gUART_RECV_COUNT = 0; + +C_SNIC_Core *C_SNIC_Core::mInstance_p = NULL; + +C_SNIC_Core *C_SNIC_Core::getInstance() +{ + if( mInstance_p == NULL ) + { + mInstance_p = new C_SNIC_Core(); + } + return mInstance_p; +} + +C_SNIC_Core::C_SNIC_Core() +{ + int i; + + mUartCommand_p = new C_SNIC_UartCommandManager(); + for( i = 0; i < MAX_SOCKET_ID+1; i++ ) + { + mConnectInfo[i].recvbuf_p = NULL; + mConnectInfo[i].is_connected = false; + mConnectInfo[i].is_receive_complete = true; + mConnectInfo[i].snic_socket = NULL; + + mUdpRecvInfo[i].recvbuf_p = NULL; + mUdpRecvInfo[i].is_received = false; + } + + mUartRecvThread_p = NULL; + mUartRecvDispatchThread_p = NULL; +} + +C_SNIC_Core::~C_SNIC_Core() +{ +} + +int C_SNIC_Core::resetModule( PinName reset ) +{ + DigitalOut reset_pin( reset ); + + reset_pin = 0; + wait(0.3); + reset_pin = 1; + wait(0.3); + + return 0; +} + +int C_SNIC_Core::initUart(PinName tx, PinName rx, int baud) +{ + mUartRequestSeq = 0; + + mUart_p = new RawSerial( tx, rx ); + mUart_p->baud( baud ); + mUart_p->format(8, SerialBase::None, 1); + + // Initialize uart + gUART_RCVBUF_p = NULL; + + mUart_p->attach( C_SNIC_Core::uartRecvCallback ); + // Create UART recv dispatch thread + mUartRecvDispatchThread_p = new Thread( C_SNIC_Core::uartRecvDispatchThread, NULL, osPriorityNormal, UART_THREAD_STACK_SIZE); + if( mUartRecvDispatchThread_p == NULL ) + { + DEBUG_PRINT("[C_SNIC_Core::initUart] thread create failed\r\n"); + return -1; + } + + return 0; +} +unsigned int C_SNIC_Core::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid + , unsigned char *req_buf_p, unsigned int req_buf_len + , unsigned char *response_buf_p, unsigned char *command_p ) +{ + unsigned int command_len = 0; + + // Make all command request + command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, req_buf_p, req_buf_len, command_p ); + + // Set data for response + mUartCommand_p->setCommandID( cmd_id ); + mUartCommand_p->setCommandSID( cmd_sid | 0x80 ); + mUartCommand_p->setResponseBuf( response_buf_p ); + + return command_len; +} + +int C_SNIC_Core::sendUart( unsigned int len, unsigned char *data ) +{ + int ret = 0; + mUartMutex.lock(); + //tr_info("[SEND] %02x:%02x:%02x", data[3], data[4], data[5]); + for( int i = 0; i < len; i++ ) + { + // Write to UART + ret = mUart_p->putc( data[i] ); + if( ret == -1 ) + { + break; + } + } + mUartMutex.unlock(); + + return ret; +} + +tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocCmdBuf() +{ + // Get buffer from MemoryPool + return mMemPoolPayload.alloc(); +} + +void C_SNIC_Core::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p ) +{ + mMemPoolPayload.free( buf_p ); +} + +tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocUartRcvBuf() +{ + // Get buffer from MemoryPool + return mMemPoolUartRecv.alloc(); +} + +void C_SNIC_Core::freeUartRecvBuf( tagMEMPOOL_BLOCK_T *buf_p ) +{ + mMemPoolUartRecv.free( buf_p ); +} + +C_SNIC_Core::tagCONNECT_INFO_T *C_SNIC_Core::getConnectInfo( int socket_id ) +{ + if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) ) + { + return NULL; + } + return &mConnectInfo[socket_id]; +} + +C_SNIC_Core::tagUDP_RECVINFO_T *C_SNIC_Core::getUdpRecvInfo( int socket_id ) +{ + if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) ) + { + return NULL; + } + return &mUdpRecvInfo[socket_id]; +} + +C_SNIC_UartCommandManager *C_SNIC_Core::getUartCommand() +{ + return mUartCommand_p; +} + +unsigned char *C_SNIC_Core::getCommandBuf() +{ + return gUART_COMMAND_BUF; +} + +void C_SNIC_Core::lockAPI( void ) +{ + mAPIMutex.lock(); +} + +void C_SNIC_Core::unlockAPI( void ) +{ + mAPIMutex.unlock(); +} + +void C_SNIC_Core::uartRecvCallback( void ) +{ + C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); + if( instance_p != NULL ) + { + int recvdata = 0; + + // Check received data from UART. + while( instance_p->mUart_p->readable() ) + { + // Receive data from UART. + recvdata = instance_p->mUart_p->getc(); + + // Check UART receiving buffer + if( gUART_RCVBUF_p != NULL ) + { + gUART_RCVBUF_p->buf[ gUART_RCVBUF_p->size ] = (unsigned char)recvdata; + gUART_RCVBUF_p->size++; + + if( gUART_RCVBUF_p->size == UART_FIXED_HEADER_SIZE ) + { + // get demand size + unsigned short payload_len = ( ( (gUART_RCVBUF_p->buf[1] & ~0x80) & 0xff) | ( ( (gUART_RCVBUF_p->buf[2] & ~0xC0) << 7) & 0xff80) ); + gUART_RCVBUF_p->demand_size = payload_len + UART_FIXED_SIZE_IN_FRAME; + if( gUART_RCVBUF_p->demand_size > MEMPOOL_BLOCK_SIZE ) + { + gUART_RCVBUF_p->demand_size = MEMPOOL_BLOCK_SIZE; + } + } + + if( gUART_RCVBUF_p->demand_size > 0 ) + { + // Check size of received data. + if( gUART_RCVBUF_p->size >= gUART_RCVBUF_p->demand_size ) + { + // Add queue + mUartRecvQueue.put( gUART_RCVBUF_p ); + + gUART_RCVBUF_p = NULL; + + if( gUART_RECV_COUNT >= MEMPOOL_UART_RECV_NUM ) + { + instance_p->mUart_p->attach( NULL ); + } + // set signal for dispatch thread + instance_p->mUartRecvDispatchThread_p->signal_set( UART_DISPATCH_SIGNAL ); + break; + } + } + } + else + { + // Check received data is SOM. + if( recvdata == UART_CMD_SOM ) + { + gUART_RCVBUF_p = instance_p->allocUartRcvBuf(); + gUART_RECV_COUNT++; + gUART_RCVBUF_p->size = 0; + gUART_RCVBUF_p->demand_size = 0; + + // get buffer for Uart receive + gUART_RCVBUF_p->buf[ 0 ] = (unsigned char)recvdata; + + gUART_RCVBUF_p->size++; + } + } + } + } +} + +void C_SNIC_Core::uartRecvDispatchThread (void const *args_p) +{ + C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = instance_p->getUartCommand(); + + tagMEMPOOL_BLOCK_T *uartRecvBuf_p; + osEvent evt; + + for(;;) + { + // wait + Thread::signal_wait( UART_DISPATCH_SIGNAL ); + + // Get scanresults from queue + evt = mUartRecvQueue.get(UART_RECV_QUEUE_TIMEOUT); + if (evt.status == osEventMessage) + { + do + { + uartRecvBuf_p = (tagMEMPOOL_BLOCK_T *)evt.value.p; + +#if 0 /* for Debug */ + { + int i; + for(i=0;i<uartRecvBuf_p->size;i++) + { + DEBUG_PRINT("%02x", uartRecvBuf_p->buf[i]); + } + DEBUG_PRINT("\r\n"); + } +#endif + unsigned char command_id; + // Get payload from received data from UART. + int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( uartRecvBuf_p->size, uartRecvBuf_p->buf + , &command_id, gUART_TEMP_BUF ); + //tr_debug("[RECV]%02x:%02x", command_id, gUART_TEMP_BUF[0]); + + // Check receive a TCP packet + if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) ) + { + // Packet buffering + uartCmdMgr_p->bufferredPacket( gUART_TEMP_BUF, payload_len ); + } + // Check connected from TCP client + else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_TCP_CLIENT_SOCKET_IND) ) + { + // Connected from TCP client + uartCmdMgr_p->connectedTCPClient( gUART_TEMP_BUF, payload_len ); + } + // Check receive UDP packet + else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_UDP_RECV_IND) ) + { + // UDP packet buffering + uartCmdMgr_p->bufferredUDPPacket( gUART_TEMP_BUF, payload_len ); + } + // Check scan results indication + else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) ) + { + // Scan result indicate + uartCmdMgr_p->scanResultIndicate( gUART_TEMP_BUF, payload_len ); + } + // Checks in the command which is waiting. + else if( uartCmdMgr_p->isWaitingCommand(command_id, gUART_TEMP_BUF) ) + { + // Get buffer for payload data + unsigned char *payload_buf_p = uartCmdMgr_p->getResponseBuf(); + if( payload_buf_p != NULL ) + { + memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len ); + uartCmdMgr_p->setResponseBuf( NULL ); + } + // Set status + uartCmdMgr_p->setCommandStatus( gUART_TEMP_BUF[2] ); + // Set signal for command response wait. + uartCmdMgr_p->signal(); + } + else + { + //DEBUG_PRINT(" The received data is not expected.\r\n"); + } + + // + instance_p->freeUartRecvBuf( uartRecvBuf_p ); + gUART_RECV_COUNT--; + if( gUART_RECV_COUNT == (MEMPOOL_UART_RECV_NUM-1) ) + { + instance_p->mUart_p->attach( C_SNIC_Core::uartRecvCallback ); //debug + } + + evt = mUartRecvQueue.get(500); + } while( evt.status == osEventMessage ); + } + } +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/SNIC_Core.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/SNIC_Core.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,460 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef _SNIC_CORE_H_ +#define _SNIC_CORE_H_ + +#include "SNICInterface/SNIC/MurataObject.h" +#include "mbed.h" +#include "rtos.h" +#include "RawSerial.h" +#include "SNICInterface/SNIC/CBuffer.h" + +#include "SNICInterface/SNIC/SNIC_UartCommandManager.h" + +#define UART_REQUEST_PAYLOAD_MAX 2048 + +#define MEMPOOL_BLOCK_SIZE 2048 +#define MEMPOOL_PAYLOAD_NUM 1 +#define MAX_SOCKET_ID 5 + +#define MEMPOOL_UART_RECV_NUM 6 +#define SNIC_UART_RECVBUF_SIZE 2048 + +class SnicSocket; +/** Wi-Fi security + */ +typedef enum SECURITY { + /** Securiry Open */ + e_SEC_OPEN = 0x00, + /** Securiry WEP */ + e_SEC_WEP = 0x01, + /** Securiry WPA-PSK(TKIP) */ + e_SEC_WPA_TKIP = 0x02, + /** Securiry WPA2-PSK(AES) */ + e_SEC_WPA2_AES = 0x04, + /** Securiry WPA2-PSK(TKIP/AES) */ + e_SEC_WPA2_MIXED = 0x06, + /** Securiry WPA-PSK(AES) */ + e_SEC_WPA_AES = 0x07 +}E_SECURITY; + +/** Wi-Fi status + */ +typedef enum WIFI_STATUS { + /** Wi-Fi OFF */ + e_STATUS_OFF = 0, + /** No network */ + e_NO_NETWORK, + /** Connected to AP (STA mode) */ + e_STA_JOINED, + /** Started on AP mode */ + e_AP_STARTED +}E_WIFI_STATUS; + +/** Memorypool + */ +typedef struct +{ + unsigned int size; + unsigned int demand_size; + unsigned char buf[MEMPOOL_BLOCK_SIZE]; +}tagMEMPOOL_BLOCK_T; + +/** Internal class used by any other classes. This class is singleton. + */ +class C_SNIC_Core: public C_MurataObject +{ +friend class C_SNIC_UartCommandManager; +friend class C_SNIC_WifiInterface; +friend class TCPSocketConnection; +friend class TCPSocketServer; +friend class SnicUDPSocket; +friend class SnicSocket; + +private: + /** Wi-Fi Network type + */ + typedef enum NETWORK_TYPE { + /** Infrastructure */ + e_INFRA = 0, + /** Adhoc */ + e_ADHOC = 1 + }E_NETWORK_TYPE; + + /** Connection information + */ + typedef struct { + CircBuffer<char> *recvbuf_p; + bool is_connected; + volatile bool is_received; + volatile bool is_receive_complete; + int parent_socket; + int from_ip; + short from_port; + volatile bool is_accept; + Mutex mutex; + SnicSocket *snic_socket; + SnicSocket *client_socket; + }tagCONNECT_INFO_T; + + /** UDP Recv information + */ + typedef struct { + CircBuffer<char> *recvbuf_p; + int from_ip; + short from_port; + int parent_socket; + bool is_received; + Mutex mutex; + }tagUDP_RECVINFO_T; + + /** GEN_FW_VER_GET_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + }tagGEN_FW_VER_GET_REQ_T; + + /** SNIC_INIT_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char buf_size[2]; + }tagSNIC_INIT_REQ_T; + + /** SNIC_RESOLVE_NAME_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char interface; + unsigned char name_length; + unsigned char name[100]; + }tagSNIC_RESOLVE_NAME_REQ_T; + + /** SNIC_IP_CONFIG_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char interface; + unsigned char dhcp; + }tagSNIC_IP_CONFIG_REQ_DHCP_T; + + /** SNIC_IP_CONFIG_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char interface; + unsigned char dhcp; + unsigned char ip_addr[4]; + unsigned char netmask[4]; + unsigned char gateway[4]; + }tagSNIC_IP_CONFIG_REQ_STATIC_T; + + /** SNIC_TCP_CREATE_SOCKET_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char bind; + unsigned char local_addr[4]; + unsigned char local_port[2]; + }tagSNIC_TCP_CREATE_SOCKET_REQ_T; + + /** SNIC_CLOSE_SOCKET_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char socket_id; + }tagSNIC_CLOSE_SOCKET_REQ_T; + + /** SNIC_TCP_SEND_FROM_SOCKET_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char socket_id; + unsigned char option; + unsigned char payload_len[2]; + }tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T; + + /** SNIC_TCP_CREATE_CONNECTION_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char socket_id; + unsigned char recv_bufsize[2]; + unsigned char max_client; + }tagSNIC_TCP_CREATE_CONNECTION_REQ_T; + + /** SNIC_TCP_CONNECT_TO_SERVER_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char socket_id; + unsigned char remote_addr[4]; + unsigned char remote_port[2]; + unsigned char recv_bufsize[2]; + unsigned char timeout; + }tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T; + + /** SNIC_UDP_SIMPLE_SEND_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char remote_ip[4]; + unsigned char remote_port[2]; + unsigned char payload_len[2]; + }tagSNIC_UDP_SIMPLE_SEND_REQ_T; + + /** SNIC_UDP_CREATE_SOCKET_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char bind; + unsigned char local_addr[4]; + unsigned char local_port[2]; + }tagSNIC_UDP_CREATE_SOCKET_REQ_T; + + /** SNIC_UDP_CREATE_SOCKET_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char bind; + }tagSNIC_UDP_CREATE_SOCKET_REQ_CLIENT_T; + + /** SNIC_UDP_SEND_FROM_SOCKET_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char remote_ip[4]; + unsigned char remote_port[2]; + unsigned char socket_id; + unsigned char connection_mode; + unsigned char payload_len[2]; + }tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T; + + /** SNIC_UDP_START_RECV_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char socket_id; + unsigned char recv_bufsize[2]; + }tagSNIC_UDP_START_RECV_REQ_T; + + /** SNIC_GET_DHCP_INFO_REQ */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char interface; + }tagSNIC_GET_DHCP_INFO_REQ_T; + + /** WIFI_ON_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + char country[COUNTRYC_CODE_LENTH]; + }tagWIFI_ON_REQ_T; + + /** WIFI_OFF_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + }tagWIFI_OFF_REQ_T; + + /** WIFI_AP_CTRL_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char on_off; + unsigned char persistency; + unsigned char ssid[33]; + unsigned char channel; + unsigned char security_mode; + unsigned char security_keylen; + unsigned char security_key[64]; + }tagWIFI_AP_CTRL_REQ_T; + + /** WIFI_DISCONNECT_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + }tagWIFI_DISCONNECT_REQ_T; + + /** WIFI_GET_STA_RSSI_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + }tagWIFI_GET_STA_RSSI_REQ_T; + + /** WIFI_SCAN_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char scan_type; + unsigned char bss_type; + unsigned char bssid[BSSID_MAC_LENTH]; + unsigned char chan_list; + unsigned char ssid[SSID_MAX_LENGTH+1]; + }tagWIFI_SCAN_REQ_T; + + /** WIFI_GET_STATUS_REQ Command */ + typedef struct + { + unsigned char cmd_sid; + unsigned char seq; + unsigned char interface; + }tagWIFI_GET_STATUS_REQ_T; + + /** Get buffer for command from memory pool. + @return Pointer of buffer + */ + tagMEMPOOL_BLOCK_T *allocCmdBuf(); + + /** Release buffer to memory pool. + @param buf_p Pointer of buffer + */ + void freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p ); + + /** Get buffer for command from memory pool. + @return Pointer of buffer + */ + tagMEMPOOL_BLOCK_T *allocUartRcvBuf(); + + /** Release buffer to memory pool. + @param buf_p Pointer of buffer + */ + void freeUartRecvBuf( tagMEMPOOL_BLOCK_T *buf_p ); + + /** Module Reset + */ + int resetModule( PinName reset ); + +/** Initialize UART + */ + int initUart( PinName tx, PinName rx, int baud ); + + /** Send data to UART + @param len Length of send data + @param data Pointer of send data + @return 0:success/other:fail + */ + int sendUart( unsigned int len, unsigned char *data ); + + /** Preparation of the UART command + @param cmd_id UART Command ID + @param cmd_sid UART Command SubID + @param req_buf_p Pointer of UART request buffer + @param req_buf_len Length of UART request buffer + @param response_buf_p Pointer of UART response buffer + @param command_p Pointer of UART command[output] + @return Length of UART command. + */ + unsigned int preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid + , unsigned char *req_buf_p, unsigned int req_buf_len + , unsigned char *response_buf_p, unsigned char *command_p ); + + /** + Get pointer of connection information. + @param socket_id Socket ID + @return The pointer of connection information + */ + C_SNIC_Core::tagCONNECT_INFO_T *getConnectInfo( int socket_id ); + + /** + Get pointer of UDP Recv information. + @param socket_id Socket ID + @return The pointer of UDP Recv information + */ + C_SNIC_Core::tagUDP_RECVINFO_T *getUdpRecvInfo( int socket_id ); + + /** + Get pointer of the instance of C_SNIC_UartCommandManager. + @return The pointer of the instance of C_SNIC_UartCommandManager. + */ + C_SNIC_UartCommandManager *getUartCommand(); + + unsigned char *getCommandBuf(); + + /** Get an instance of the C_SNIC_Core class. + @return Instance of the C_SNIC_Core class + @note Please do not create an instance in the default constructor this class. + Please use this method when you want to get an instance. + */ + static C_SNIC_Core *getInstance(); + + /** Mutex lock of API calls + */ + void lockAPI( void ); + + /** Mutex unlock of API calls + */ + void unlockAPI( void ); + +private: + static C_SNIC_Core *mInstance_p; + Thread *mUartRecvThread_p; + Thread *mUartRecvDispatchThread_p; + RawSerial *mUart_p; + Mutex mUartMutex; + Mutex mAPIMutex; + +// DigitalInOut mModuleReset; + C_SNIC_UartCommandManager *mUartCommand_p; + + CircBuffer<char> *mUartRecvBuf_p; // UART RecvBuffer + + /** Socket buffer */ + tagCONNECT_INFO_T mConnectInfo[MAX_SOCKET_ID+1]; + + /** UDP Information */ + tagUDP_RECVINFO_T mUdpRecvInfo[MAX_SOCKET_ID+1]; + + /** Constructor + */ + C_SNIC_Core(); + + virtual ~C_SNIC_Core(); + + static void uartRecvCallback( void ); + /** Receiving thread of UART + */ + static void uartRecvDispatchThread( void const *args_p ); +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/SNIC_UartCommandManager.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/SNIC_UartCommandManager.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,309 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "mbed.h" +#include "rtos.h" +#include "mbed-trace/mbed_trace.h" +#define TRACE_GROUP "SNIC" + +#include "SNICInterface/SNIC/SNIC_UartCommandManager.h" +#include "SNICInterface/SNIC/SNIC_Core.h" +#include "SNICInterface/Socket/SNIC_Socket.h" +#include "SNICInterface/Socket/TCPSocketServer.h" + +extern "C" void socket_event_handler(void); + +C_SNIC_UartCommandManager::~C_SNIC_UartCommandManager() +{ +} + +void C_SNIC_UartCommandManager::setCommandID( unsigned char cmd_id ) +{ + mCommandID = cmd_id; +} + +unsigned char C_SNIC_UartCommandManager::getCommandID() +{ + return mCommandID; +} + +void C_SNIC_UartCommandManager::setCommandSID( unsigned char cmd_sid ) +{ + mCommandSID = cmd_sid; +} + +unsigned char C_SNIC_UartCommandManager::getCommandSID() +{ + return mCommandSID; +} + +void C_SNIC_UartCommandManager::setCommandStatus( unsigned char status ) +{ + mCommandStatus = status; +} + +unsigned char C_SNIC_UartCommandManager::getCommandStatus() +{ + return mCommandStatus; +} + +void C_SNIC_UartCommandManager::setResponseBuf( unsigned char *buf_p ) +{ + mResponseBuf_p = buf_p; +} + +unsigned char *C_SNIC_UartCommandManager::getResponseBuf() +{ + return mResponseBuf_p; +} + +void C_SNIC_UartCommandManager::setScanResultHandler( void (*handler_p)(tagSCAN_RESULT_T *scan_result) ) +{ + mScanResultHandler_p = handler_p; +} + + +int C_SNIC_UartCommandManager::wait() +{ + int ret = 0; + + // Get thread ID + mCommandThreadID = osThreadGetId(); + + // Signal flags that are reported as event are automatically cleared. + osEvent event_ret = osSignalWait( UART_COMMAND_SIGNAL, UART_COMMAND_WAIT_TIMEOUT); + if( event_ret.status != osEventSignal ) + { + ret = -1; + } + + return ret; +} + +int C_SNIC_UartCommandManager::signal() +{ + // set signal + return osSignalSet(mCommandThreadID, UART_COMMAND_SIGNAL); +} + +bool C_SNIC_UartCommandManager::isWaitingCommand( unsigned int command_id, unsigned char *payload_p ) +{ + bool ret = false; + + if( (command_id == getCommandID()) + && (payload_p[0] == getCommandSID()) ) + { + ret = true; + } + return ret; +} + +void C_SNIC_UartCommandManager::scanResultIndicate( unsigned char *payload_p, int payload_len ) +{ + if( (payload_p == NULL) || (mScanResultHandler_p == NULL) ) + { + return; + } + + tagSCAN_RESULT_T scan_result; + int ap_count = payload_p[2]; + + if( ap_count == 0 ) + { + mScanResultHandler_p( NULL ); + } + + unsigned char *ap_info_p = &payload_p[3]; + int ap_info_idx = 0; + + for( int i = 0; i < ap_count; i++ ) + { + scan_result.is_complete = ap_count - i - 1; + scan_result.channel = ap_info_p[ap_info_idx]; + ap_info_idx++; + scan_result.rssi = (signed)ap_info_p[ap_info_idx]; + ap_info_idx++; + scan_result.security= ap_info_p[ap_info_idx]; + ap_info_idx++; + memcpy( scan_result.bssid, &ap_info_p[ap_info_idx], BSSID_MAC_LENTH ); + ap_info_idx += BSSID_MAC_LENTH; + scan_result.network_type= ap_info_p[ap_info_idx]; + ap_info_idx++; + scan_result.max_rate= ap_info_p[ap_info_idx]; + ap_info_idx++; + ap_info_idx++; // reserved + strcpy( scan_result.ssid, (char *)&ap_info_p[ap_info_idx] ); + ap_info_idx += strlen( (char *)&ap_info_p[ap_info_idx] ); + ap_info_idx++; + + // Scanresult callback + mScanResultHandler_p( &scan_result ); + } +} + +void C_SNIC_UartCommandManager::bufferredPacket( unsigned char *payload_p, int payload_len ) +{ + int socket_id; + unsigned short recv_len; + + if( (payload_p == NULL) || (payload_len == 0) ) + { + return; + } + recv_len= ((payload_p[3]<<8) & 0xFF00) | payload_p[4]; + + C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); + + // Get socket id from payload + socket_id = payload_p[2]; + +// DEBUG_PRINT("bufferredPacket socket id:%d\r\n", socket_id); + // Get Connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id ); + if( con_info_p == NULL ) + { + return; + } + + if( con_info_p->is_connected == false ) + { + DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", socket_id); + return; + } + + // Get receive length from payload + recv_len= ((payload_p[3]<<8) & 0xFF00) | payload_p[4]; + + con_info_p->mutex.lock(); + + //DEBUG_PRINT("bufferredPacket recv_len:%d\r\n", recv_len); + int i; + for(i = 0; i < recv_len; i++ ) + { + if( con_info_p->recvbuf_p->isFull() ) + { + DEBUG_PRINT("Receive buffer is full.\r\n"); + break; + } + // Add to receive buffer + con_info_p->recvbuf_p->queue( payload_p[5+i] ); + } + //DEBUG_PRINT("###Receive queue[%d]\r\n", i); + + con_info_p->mutex.unlock(); + + con_info_p->snic_socket->event(); +} + +void C_SNIC_UartCommandManager::connectedTCPClient( unsigned char *payload_p, int payload_len ) +{ + if( (payload_p == NULL) || (payload_len == 0) ) + { + return; + } + + C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); + int socket_id; + int server_id; + + // Get socket id of client from payload + server_id = payload_p[2]; + + // Get Connection information + C_SNIC_Core::tagCONNECT_INFO_T *srv_info_p = instance_p->getConnectInfo( server_id ); + if( srv_info_p == NULL || srv_info_p->is_accept == false) + { + return; + } + + DEBUG_PRINT("[connectedTCPServer] socket id:%d\r\n", server_id); + + // Get socket id of client from payload + socket_id = payload_p[3]; + + DEBUG_PRINT("[connectedTCPClient] socket id:%d\r\n", socket_id); + + // Get Connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id ); + if( con_info_p == NULL ) + { + return; + } + + if( con_info_p->recvbuf_p == NULL ) + { +// DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", socket_id); + con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); + } + con_info_p->is_connected = true; + con_info_p->is_received = false; + con_info_p->is_accept = true; + con_info_p->parent_socket = server_id; + con_info_p->from_ip = ((payload_p[4] << 24) | (payload_p[5] << 16) | (payload_p[6] << 8) | payload_p[7]); + con_info_p->from_port = ((payload_p[8] << 8) | payload_p[9]); +} + +void C_SNIC_UartCommandManager::bufferredUDPPacket( unsigned char *payload_p, int payload_len ) +{ + if( (payload_p == NULL) || (payload_len == 0) ) + { + return; + } + + C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); + + // Get Connection information +// C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = instance_p->getUdpRecvInfo( payload_p[2] ); + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( payload_p[2] ); + if( con_info_p == NULL ) + { + return; + } + + if( con_info_p->recvbuf_p == NULL ) + { +// DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", payload_p[2]); + con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); + } + con_info_p->mutex.lock(); + con_info_p->is_received = true; + + // Set remote IP address and remote port + con_info_p->from_ip = ((payload_p[3] << 24) | (payload_p[4] << 16) | (payload_p[5] << 8) | payload_p[6]); + con_info_p->from_port = ((payload_p[7] << 8) | payload_p[8]); + + unsigned short recv_len; + // Get receive length from payload + recv_len= ((payload_p[9]<<8) & 0xFF00) | payload_p[10]; + for( int i = 0; i < recv_len; i++ ) + { + if( con_info_p->recvbuf_p->isFull() ) + { + DEBUG_PRINT("Receive buffer is full.\r\n"); + break; + } + + // Add to receive buffer + con_info_p->recvbuf_p->queue( payload_p[11+i] ); + } + + con_info_p->mutex.unlock(); + + con_info_p->snic_socket->event(); +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/SNIC_UartCommandManager.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/SNIC_UartCommandManager.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,152 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef _SNIC_UART_COMMAND_MANAGER_H_ +#define _SNIC_UART_COMMAND_MANAGER_H_ +#include "SNICInterface/SNIC/MurataObject.h" +#include "mbed.h" +#include "rtos.h" + +/** Max length of SSID */ +#define SSID_MAX_LENGTH 32 +/** Max length of BSSID */ +#define BSSID_MAC_LENTH 6 +/** Length of Country code */ +#define COUNTRYC_CODE_LENTH 2 + +/** Wait signal ID of UART command */ +#define UART_COMMAND_SIGNAL 0x00000001 +/** Timeout of UART command wait(ms)*/ +#define UART_COMMAND_WAIT_TIMEOUT 10000 + +/** Scan result structure used by scanresults handler +*/ +typedef struct { + bool is_complete; + /** Channel */ + unsigned char channel; + + /** RSSI */ + signed char rssi; + /** Security type */ + unsigned char security; + /** BSSID */ + unsigned char bssid[BSSID_MAC_LENTH]; + /** Network type */ + unsigned char network_type; + /** Max data rate */ + unsigned char max_rate; + /** SSID */ + char ssid[SSID_MAX_LENGTH+1]; +}tagSCAN_RESULT_T; + +/** Internal class for managing the SNIC UART command. + */ +class C_SNIC_UartCommandManager: public C_MurataObject +{ +friend class C_SNIC_Core; +friend class C_SNIC_WifiInterface; +friend class TCPSocketConnection; +friend class TCPSocketServer; +friend class SnicUDPSocket; +friend class SnicSocket; + +private: + virtual ~C_SNIC_UartCommandManager(); + + /** Set Command ID + @param cmd_id Command ID + */ + void setCommandID( unsigned char cmd_id ); + + /** Get Command ID + @return Command ID + */ + unsigned char getCommandID(); + + /** Set Command SubID + @param cmd_sid Command Sub ID + */ + void setCommandSID( unsigned char cmd_sid ); + + /** Get Command SubID + @return Command Sub ID + */ + unsigned char getCommandSID(); + + /** Set Command status + @param status Command status + */ + void setCommandStatus( unsigned char status ); + + /** Get Command status + @return Command status + */ + unsigned char getCommandStatus(); + + /** Set Response buffer + @param buf_p Pointer of response buffer + */ + void setResponseBuf( unsigned char *buf_p ); + + /** Get Response buffer + @return Pointer of response buffer + */ + unsigned char *getResponseBuf(); + + /** Set scan result callback hander + @param handler_p Pointer of callback function + */ + void setScanResultHandler( void (*handler_p)(tagSCAN_RESULT_T *scan_result) ); + + void bufferredPacket( unsigned char *payload_p, int payload_len ); + + void bufferredUDPPacket( unsigned char *payload_p, int payload_len ); + + void scanResultIndicate( unsigned char *payload_p, int payload_len ); + + void connectedTCPClient( unsigned char *payload_p, int payload_len ); + + /** Checks in the command which is waiting from Command ID and Sub ID. + @param command_id Command ID + @param payload_p Command payload + @return true: Waiting command / false: Not waiting command + */ + bool isWaitingCommand( unsigned int command_id, unsigned char *payload_p ); + + int wait(); + + int signal(); + +private: + /** Command request thread ID */ + osThreadId mCommandThreadID; + /** Command ID */ + unsigned char mCommandID; + /** Command SubID */ + unsigned char mCommandSID; + /** Status of command response */ + unsigned char mCommandStatus; + /** ResponseData of command response */ + unsigned char *mResponseBuf_p; + /** Scan result handler */ + void (*mScanResultHandler_p)(tagSCAN_RESULT_T *scan_result); +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/SNIC_UartMsgUtil.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/SNIC_UartMsgUtil.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,150 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "SNICInterface/SNIC/SNIC_UartMsgUtil.h" + +C_SNIC_UartMsgUtil::C_SNIC_UartMsgUtil() +{ +} + +C_SNIC_UartMsgUtil::~C_SNIC_UartMsgUtil() +{ +} + +unsigned int C_SNIC_UartMsgUtil::makeRequest( unsigned char cmd_id,unsigned char *payload_p + , unsigned short payload_len, unsigned char *uart_command_p ) +{ + unsigned char check_sum = 0; // Check Sum + unsigned int uart_cmd_len = 0; + int i; + + // set SOM + *uart_command_p = UART_CMD_SOM; + uart_command_p++; + uart_cmd_len++; + + // set payload length L0 + *uart_command_p = (0x80 | (payload_len & 0x007f)); + check_sum += *uart_command_p; + uart_command_p++; + uart_cmd_len++; + + // set payload length L1 + *uart_command_p = (0x80 | ( (payload_len >> 7) & 0x003f)); + check_sum += *uart_command_p; + uart_command_p++; + uart_cmd_len++; + + // set Command ID + *uart_command_p = (0x80 | cmd_id); + check_sum += *uart_command_p; + uart_command_p++; + uart_cmd_len++; + + // set Payload + for( i = 0; i < payload_len; i++, uart_command_p++, uart_cmd_len++ ) + { + *uart_command_p = payload_p[i]; + } + + // set Check sum + *uart_command_p = (0x80 | check_sum); + uart_command_p++; + uart_cmd_len++; + + // set EOM + *uart_command_p = UART_CMD_EOM; + uart_cmd_len++; + + return uart_cmd_len; +} + +unsigned int C_SNIC_UartMsgUtil::getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p + , unsigned char *command_id_p, unsigned char *payload_p ) +{ + unsigned short payload_len = 0; + unsigned char *buf_p = NULL; + int i; + + // get payload length + payload_len = ( ( (recvdata_p[1] & ~0x80) & 0xff) | ( ( (recvdata_p[2] & ~0xC0) << 7) & 0xff80) ); + + // get Command ID + *command_id_p = (recvdata_p[3] & ~0x80); + + buf_p = &recvdata_p[4]; + + // get payload data + for( i = 0; i < payload_len; i++, buf_p++ ) + { + *payload_p = *buf_p; + payload_p++; + } + + return payload_len; +} + +int C_SNIC_UartMsgUtil::addrToInteger( const char *addr_p ) +{ + if( addr_p == NULL ) + { + DEBUG_PRINT("addrToInteger parameter error\r\n"); + return 0; + } + + unsigned char ipadr[4]; + unsigned char temp= 0; + int i,j,k; + + /* convert to char[4] */ + k=0; + for(i=0; i<4; i ++) + { + for(j=0; j<4; j ++) + { + if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A)) + { + temp = (temp * 10) + addr_p[k]-0x30; + } + else if((addr_p[k] == 0x20)&&(temp == 0)) + { + } + else + { + ipadr[i]=temp; + temp = 0; + k++; + break; + } + k++; + } + } + + int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] ); + + return addr; +} + +void C_SNIC_UartMsgUtil::convertIntToByteAdday( int addr, char *addr_array_p ) +{ + addr_array_p[0] = ((addr & 0xFF000000) >> 24 ); + addr_array_p[1] = ((addr & 0xFF0000) >> 16 ); + addr_array_p[2] = ((addr & 0xFF00) >> 8 ); + addr_array_p[3] = ( addr & 0xFF); +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC/SNIC_UartMsgUtil.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC/SNIC_UartMsgUtil.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,186 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef _SNIC_WIFI_UART_MSG_UTIL_H_ +#define _SNIC_WIFI_UART_MSG_UTILH_ +#include "SNICInterface/SNIC/MurataObject.h" +#include "mbed.h" +#include "rtos.h" +#include "RawSerial.h" + +#define UART_CMD_SOM 0x02 +#define UART_CMD_EOM 0x04 +#define UART_CMD_ESC 0x10 + +/* SNIC UART Command ID */ +#define UART_CMD_ID_GEN 0x01 //General command +#define UART_CMD_ID_SNIC 0x70 //SNIC command +#define UART_CMD_ID_WIFI 0x50 //Wi-Fi command + +/* SNIC UART Subcommand ID */ +#define UART_CMD_SID_GEN_PWR_UP_IND 0x00 //Power up indication +#define UART_CMD_SID_GEN_FW_VER_GET_REQ 0x08 //Get firmware version string + +#define UART_CMD_SID_SNIC_INIT_REQ 0x00 // SNIC API initialization +#define UART_CMD_SID_SNIC_CLEANUP_REQ 0x01 // SNIC API cleanup +#define UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ 0x02 // Send from socket +#define UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ 0x03 // Close socket +#define UART_CMD_SID_SNIC_SOCKET_PARTIAL_CLOSE_ REQ 0x04 // Socket partial close +#define UART_CMD_SID_SNIC_GETSOCKOPT_REQ 0x05 // Get socket option +#define UART_CMD_SID_SNIC_SETSOCKOPT_REQ 0x06 // Set socket option +#define UART_CMD_SID_SNIC_SOCKET_GETNAME_REQ 0x07 // Get name or peer name +#define UART_CMD_SID_SNIC_SEND_ARP_REQ 0x08 // Send ARP request +#define UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ 0x09 // Get DHCP info +#define UART_CMD_SID_SNIC_RESOLVE_NAME_REQ 0x0A // Resolve a host name to IP address +#define UART_CMD_SID_SNIC_IP_CONFIG_REQ 0x0B // Configure DHCP or static IP +#define UART_CMD_SID_SNIC_DATA_IND_ACK_CONFIG_REQ 0x0C // ACK configuration for data indications +#define UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ 0x10 // Create TCP socket +#define UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ 0x11 // Create TCP connection server +#define UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ 0x12 // Connect to TCP server +#define UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ 0x13 // Create UDP socket +#define UART_CMD_SID_SNIC_UDP_START_RECV_REQ 0x14 // Start UDP receive on socket +#define UART_CMD_SID_SNIC_UDP_SIMPLE_SEND_REQ 0x15 // Send UDP packet +#define UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_REQ 0x16 // Send UDP packet from socket +#define UART_CMD_SID_SNIC_HTTP_REQ 0x17 // Send HTTP request +#define UART_CMD_SID_SNIC_HTTP_MORE_REQ 0x18 // Send HTTP more data request +#define UART_CMD_SID_SNIC_HTTPS_REQ 0x19 // Send HTTPS request +#define UART_CMD_SID_SNIC_TCP_CREATE_ADV_TLS_SOCKET_REQ 0x1A // Create advanced TLS TCP socket +#define UART_CMD_SID_SNIC_TCP_CREAET_SIMPLE_TLS_SOCKET_REQ 0x1B // Create simple TLS TCP socket +#define UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND 0x20 // Connection status indication +#define UART_CMD_SID_SNIC_TCP_CLIENT_SOCKET_IND 0x21 // TCP client socket indication +#define UART_CMD_SID_SNIC_CONNECTION_RECV_IND 0x22 // TCP or connected UDP packet received indication +#define UART_CMD_SID_SNIC_UDP_RECV_IND 0x23 // UCP packet received indication +#define UART_CMD_SID_SNIC_ARP_REPLY_IND 0x24 // ARP reply indication +#define UART_CMD_SID_SNIC_HTTP_RSP_IND 0x25 // HTTP response indication + +#define UART_CMD_SID_WIFI_ON_REQ 0x00 // Turn on Wifi +#define UART_CMD_SID_WIFI_OFF_REQ 0x01 // Turn off Wifi +#define UART_CMD_SID_WIFI_JOIN_REQ 0x02 // Associate to a network +#define UART_CMD_SID_WIFI_DISCONNECT_REQ 0x03 // Disconnect from a network +#define UART_CMD_SID_WIFI_GET_STATUS_REQ 0x04 // Get WiFi status +#define UART_CMD_SID_WIFI_SCAN_REQ 0x05 // Scan WiFi networks +#define UART_CMD_SID_WIFI_GET_STA_RSSI_REQ 0x06 // Get STA signal strength (RSSI) +#define UART_CMD_SID_WIFI_AP_CTRL_REQ 0x07 // Soft AP on-off control +#define UART_CMD_SID_WIFI_WPS_REQ 0x08 // Start WPS process +#define UART_CMD_SID_WIFI_AP_GET_CLIENT_REQ 0x0A // Get clients that are associated to the soft AP. +#define UART_CMD_SID_WIFI_NETWORK_STATUS_IND 0x10 // Network status indication +#define UART_CMD_SID_WIFI_SCAN_RESULT_IND 0x11 // Scan result indication + +/* SNIC UART Command response status code */ +#define UART_CMD_RES_SNIC_SUCCESS 0x00 +#define UART_CMD_RES_SNIC_FAIL 0x01 +#define UART_CMD_RES_SNIC_INIT_FAIL 0x02 +#define UART_CMD_RES_SNIC_CLEANUP_FAIL 0x03 +#define UART_CMD_RES_SNIC_GETADDRINFO_FAIL 0x04 +#define UART_CMD_RES_SNIC_CREATE_SOCKET_FAIL 0x05 +#define UART_CMD_RES_SNIC_BIND_SOCKET_FAIL 0x06 +#define UART_CMD_RES_SNIC_LISTEN_SOCKET_FAIL 0x07 +#define UART_CMD_RES_SNIC_ACCEPT_SOCKET_FAIL 0x08 +#define UART_CMD_RES_SNIC_PARTIAL_CLOSE_FAIL 0x09 +#define UART_CMD_RES_SNIC_SOCKET_PARTIALLY_CLOSED 0x0A +#define UART_CMD_RES_SNIC_SOCKET_CLOSED 0x0B +#define UART_CMD_RES_SNIC_CLOSE_SOCKET_FAIL 0x0C +#define UART_CMD_RES_SNIC_PACKET_TOO_LARGE 0x0D +#define UART_CMD_RES_SNIC_SEND_FAIL 0x0E +#define UART_CMD_RES_SNIC_CONNECT_TO_SERVER_FAIL 0x0F +#define UART_CMD_RES_SNIC_NOT_ENOUGH_MEMORY 0x10 +#define UART_CMD_RES_SNIC_TIMEOUT 0x11 +#define UART_CMD_RES_SNIC_CONNECTION_UP 0x12 +#define UART_CMD_RES_SNIC_GETSOCKOPT_FAIL 0x13 +#define UART_CMD_RES_SNIC_SETSOCKOPT_FAIL 0x14 +#define UART_CMD_RES_SNIC_INVALID_ARGUMENT 0x15 +#define UART_CMD_RES_SNIC_SEND_ARP_FAIL 0x16 +#define UART_CMD_RES_SNIC_INVALID_SOCKET 0x17 +#define UART_CMD_RES_SNIC_COMMAND_PENDING 0x18 +#define UART_CMD_RES_SNIC_SOCKET_NOT_BOUND 0x19 +#define UART_CMD_RES_SNIC_SOCKET_NOT_CONNECTED 0x1A +#define UART_CMD_RES_SNIC_NO_NETWORK 0x20 +#define UART_CMD_RES_SNIC_INIT_NOT_DONE 0x21 +#define UART_CMD_RES_SNIC_NET_IF_FAIL 0x22 +#define UART_CMD_RES_SNIC_NET_IF_NOT_UP 0x23 +#define UART_CMD_RES_SNIC_DHCP_START_FAIL 0x24 + +#define UART_CMD_RES_WIFI_SUCCESS 0x00 +#define UART_CMD_RES_WIFI_ERR_UNKNOWN_COUNTRY 0x01 +#define UART_CMD_RES_WIFI_ERR_INIT_FAIL 0x02 +#define UART_CMD_RES_WIFI_ERR_ALREADY_JOINED 0x03 +#define UART_CMD_RES_WIFI_ERR_AUTH_TYPE 0x04 +#define UART_CMD_RES_WIFI_ERR_JOIN_FAIL 0x05 +#define UART_CMD_RES_WIFI_ERR_NOT_JOINED 0x06 +#define UART_CMD_RES_WIFI_ERR_LEAVE_FAILED 0x07 +#define UART_CMD_RES_WIFI_COMMAND_PENDING 0x08 +#define UART_CMD_RES_WIFI_WPS_NO_CONFIG 0x09 +#define UART_CMD_RES_WIFI_NETWORK_UP 0x10 +#define UART_CMD_RES_WIFI_NETWORK_DOWN 0x11 +#define UART_CMD_RES_WIFI_FAIL 0xFF + +/** UART Command sequence number +*/ +static unsigned char mUartRequestSeq; + +/** Internal utility class used by any other classes. + */ +class C_SNIC_UartMsgUtil: public C_MurataObject +{ +friend class C_SNIC_Core; +friend class C_SNIC_WifiInterface; +friend class SnicUDPSocket; +friend class TCPSocketConnection; +friend class SnicSocket; + +private: + C_SNIC_UartMsgUtil(); + virtual ~C_SNIC_UartMsgUtil(); + + /** Make SNIC UART command. + @param cmd_id Command ID + @param payload_p Payload pointer + @param uart_command_p UART Command pointer [output] + @return UART Command length + */ + static unsigned int makeRequest( unsigned char cmd_id, unsigned char *payload_p, unsigned short payload_len, unsigned char *uart_command_p ); + + + /** Get uart command from receive data. + @param recvdata_len Receive data length + @param recvdata_p Pointer of received data from UART + @param command_id_p Pointer of command ID[output] + @param payload_p Pointer of payload[output] + @return Payload length + */ + static unsigned int getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p + , unsigned char *command_id_p, unsigned char *payload_p ); + + /** Convert a string to number of ip address. + @param recvdata_p Pointer of string of IP address + @return Number of ip address + */ + static int addrToInteger( const char *addr_p ); + + /** Convert a integer to byte array of ip address. + @param recvdata_p Pointer of string of IP address + @return Number of ip address + */ + static void convertIntToByteAdday( int addr, char *addr_array_p ); + +protected: + +}; + +#endif /* _YD_WIFI_UART_MSG_H_ */ +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC_WifiInterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC_WifiInterface.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,965 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD-SNIC UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "SNICInterface/SNIC_WifiInterface.h" +#include "SNICInterface/SNIC/SNIC_UartMsgUtil.h" + +#define UART_CONNECT_BUF_SIZE 512 +unsigned char gCONNECT_BUF[UART_CONNECT_BUF_SIZE]; +static char ip_addr[17] = "\0"; +static char gateway_addr[17] = "\0"; +static char subnet_addr[17] = "\0"; + +C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) +{ + mUART_tx = tx; + mUART_rx = rx; + mUART_cts = cts; + mUART_rts = rts;; + mUART_baud = baud; + mModuleReset = reset; +} + +void C_SNIC_WifiInterface::create( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) +{ + mUART_tx = tx; + mUART_rx = rx; + mUART_cts = cts; + mUART_rts = rts;; + mUART_baud = baud; + mModuleReset = reset; +} + +C_SNIC_WifiInterface::~C_SNIC_WifiInterface() +{ +} + +int C_SNIC_WifiInterface::init() +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + /* Initialize UART */ + snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud ); + + /* Module reset */ + snic_core_p->resetModule( mModuleReset ); + + wait(1); + /* Initialize SNIC API */ + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("snic_init payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagSNIC_INIT_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_INIT_REQ; + req.seq = mUartRequestSeq++; + req.buf_size[0] = 0x08; + req.buf_size[1] = 0x00; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_INIT_REQ_T), payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "snic_init failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("snic_init status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + + return ret; +} + +int C_SNIC_WifiInterface::getFWVersion( unsigned char *version_p, int *length_p ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + if ((version_p == NULL) || (length_p == NULL)) { + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("getFWVersion payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ; + req.seq = mUartRequestSeq++; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_GEN, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T), payload_buf_p->buf, command_array_p ); + + int ret; + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "getFWversion failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() == 0 ) + { + unsigned char version_len = payload_buf_p->buf[3]; + if (*length_p <= (int)version_len) { + DEBUG_PRINT( "getFWversion verion_p is not enough memory\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + memcpy( version_p, &payload_buf_p->buf[4], version_len ); + version_p[version_len] = 0; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + return 0; +} + +int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type + , const char *sec_key_p, unsigned char sec_key_len) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Parameter check(SSID) + if( (ssid_p == NULL) || (ssid_len == 0) ) + { + DEBUG_PRINT( "connect failed [ parameter NG:SSID ]\r\n" ); + return -1; + } + + // Parameter check(Security key) + if( (sec_type != e_SEC_OPEN) && ( (sec_key_len == 0) || (sec_key_p == NULL) ) ) + { + DEBUG_PRINT( "connect failed [ parameter NG:Security key ]\r\n" ); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("connect payload_buf_p NULL\r\n"); + return -1; + } + + unsigned char *buf = &gCONNECT_BUF[0]; + unsigned int buf_len = 0; + unsigned int command_len; + + memset( buf, 0, UART_CONNECT_BUF_SIZE ); + // Make request + buf[0] = UART_CMD_SID_WIFI_JOIN_REQ; + buf_len++; + buf[1] = mUartRequestSeq++; + buf_len++; + // SSID + memcpy( &buf[2], ssid_p, ssid_len ); + buf_len += ssid_len; + buf_len++; + + // Security mode + buf[ buf_len ] = (unsigned char)sec_type; + buf_len++; + + // Security key + if( sec_type != e_SEC_OPEN ) + { + buf[ buf_len ] = sec_key_len; + buf_len++; + if( sec_key_len > 0 ) + { + memcpy( &buf[buf_len], sec_key_p, sec_key_len ); + buf_len += sec_key_len; + } + } + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf + , buf_len, payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if(uartCmdMgr_p->getCommandStatus() == UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) + { + DEBUG_PRINT( "Already connected\r\n" ); + } + else + { + if( ret != 0 ) + { + DEBUG_PRINT( "join failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + } + + if(uartCmdMgr_p->getCommandStatus() != 0) + { + DEBUG_PRINT("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + + return ret; +} + +int C_SNIC_WifiInterface::disconnect() +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("disconnect payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ; + req.seq = mUartRequestSeq++; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T), payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "disconnect failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("disconnect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + ret = -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + return ret; +} + +int C_SNIC_WifiInterface::scan( const char *ssid_p, unsigned char *bssid_p + , void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("scan payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_SCAN_REQ_T req; + unsigned int buf_len = 0; + + memset( &req, 0, sizeof(C_SNIC_Core::tagWIFI_SCAN_REQ_T) ); + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_SCAN_REQ; + buf_len++; + req.seq = mUartRequestSeq++; + buf_len++; + + // Set scan type(Active scan) + req.scan_type = 0; + buf_len++; + // Set bss type(any) + req.bss_type = 2; + buf_len++; + // Set BSSID + if( bssid_p != NULL ) + { + memcpy( req.bssid, bssid_p, BSSID_MAC_LENTH ); + } + buf_len += BSSID_MAC_LENTH; + // Set channel list(0) + req.chan_list = 0; + buf_len++; + //Set SSID + if( ssid_p != NULL ) + { + strcpy( (char *)req.ssid, ssid_p ); + buf_len += strlen(ssid_p); + } + buf_len++; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , buf_len, payload_buf_p->buf, command_array_p ); + + // Set scan result callback + uartCmdMgr_p->setScanResultHandler( result_handler_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + DEBUG_PRINT( "scan wait:%d\r\n", ret ); + if( ret != 0 ) + { + DEBUG_PRINT( "scan failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("scan status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + + return ret; +} + +int C_SNIC_WifiInterface::wifi_on( const char *country_p ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Parameter check + if( country_p == NULL ) + { + DEBUG_PRINT("wifi_on parameter error\r\n"); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("wifi_on payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_ON_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_ON_REQ; + req.seq = mUartRequestSeq++; + memcpy( req.country, country_p, COUNTRYC_CODE_LENTH ); + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagWIFI_ON_REQ_T), payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "wifi_on failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("wifi_on status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + + return ret; +} + +int C_SNIC_WifiInterface::wifi_off() +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("wifi_off payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_OFF_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ; + req.seq = mUartRequestSeq++; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagWIFI_OFF_REQ_T), payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "wifi_off failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("wifi_off status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + + return ret; +} + +int C_SNIC_WifiInterface::wifi_softap_off() +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("wifi_off payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_AP_CTRL_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_AP_CTRL_REQ; + req.seq = mUartRequestSeq++; + req.on_off = 0; + req.persistency = 1; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagWIFI_AP_CTRL_REQ_T), payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "wifi_ap_crtl failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("wifi_ap_crtl status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + + return ret; +} + +int C_SNIC_WifiInterface::getRssi( signed char *rssi_p ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + if( rssi_p == NULL ) + { + DEBUG_PRINT("getRssi parameter error\r\n"); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("getRssi payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T req; + + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ; + req.seq = mUartRequestSeq++; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf_p->buf, command_array_p ); + + int ret; + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "getRssi failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + *rssi_p = (signed char)payload_buf_p->buf[2]; + + snic_core_p->freeCmdBuf( payload_buf_p ); + return 0; +} + +int C_SNIC_WifiInterface::getWifiStatus( tagWIFI_STATUS_T *status_p) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + if( status_p == NULL ) + { + DEBUG_PRINT("getWifiStatus parameter error\r\n"); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("getWifiStatus payload_buf_p NULL\r\n"); + return -1; + } + + C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_WIFI_GET_STATUS_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "getWifiStatus failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + // set status + status_p->status = (E_WIFI_STATUS)payload_buf_p->buf[2]; + + // set Mac address + if( status_p->status != e_STATUS_OFF ) + { + memcpy( status_p->mac_address, &payload_buf_p->buf[3], BSSID_MAC_LENTH ); + } + + // set SSID + if( ( status_p->status == e_STA_JOINED ) || ( status_p->status == e_AP_STARTED ) ) + { + memcpy( status_p->ssid, &payload_buf_p->buf[9], strlen( (char *)&payload_buf_p->buf[9]) ); + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + return 0; +} + +int C_SNIC_WifiInterface::setIPConfig( bool is_DHCP + , const char *ip_p, const char *mask_p, const char *gateway_p ) +{ + // Parameter check + if( is_DHCP == false ) + { + if( (ip_p == NULL) || (mask_p == NULL) ||(gateway_p == NULL) ) + { + DEBUG_PRINT("setIPConfig parameter error\r\n"); + return -1; + } + } + + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("setIPConfig payload_buf_p NULL\r\n"); + return -1; + } + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + if( is_DHCP == true ) + { + C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + req.dhcp = 1; + + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T), payload_buf_p->buf, command_array_p ); + } + else + { + C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + req.dhcp = 0; + + // Set paramter of address + int addr_temp; + addr_temp = C_SNIC_UartMsgUtil::addrToInteger( ip_p ); + C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.ip_addr ); + addr_temp = C_SNIC_UartMsgUtil::addrToInteger( mask_p ); + C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.netmask ); + addr_temp = C_SNIC_UartMsgUtil::addrToInteger( gateway_p ); + C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.gateway ); + + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T), payload_buf_p->buf, command_array_p ); + } + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "setIPConfig failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("setIPConfig status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + return ret; +} + +char* C_SNIC_WifiInterface::getIPAddress() { + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + snic_core_p->lockAPI(); + // Get local ip address. + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("getIPAddress payload_buf_p NULL\r\n"); + snic_core_p->unlockAPI(); + return 0; + } + + C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + // Wait UART response + int ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "getIPAddress failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; + } + + sprintf(ip_addr, "%d.%d.%d.%d\0", payload_buf_p->buf[9], payload_buf_p->buf[10], payload_buf_p->buf[11], payload_buf_p->buf[12]); + + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + + return ip_addr; +} + +char* C_SNIC_WifiInterface::getSubnetAddress() { + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + snic_core_p->lockAPI(); + // Get local ip address. + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("getIPAddress payload_buf_p NULL\r\n"); + snic_core_p->unlockAPI(); + return 0; + } + + C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + // Wait UART response + int ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "getIPAddress failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; + } + + sprintf(subnet_addr, "%d.%d.%d.%d\0", payload_buf_p->buf[17], payload_buf_p->buf[18], payload_buf_p->buf[19], payload_buf_p->buf[20]); + + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + + return subnet_addr; +} + +char* C_SNIC_WifiInterface::getGatewayAddress() { + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + snic_core_p->lockAPI(); + // Get local ip address. + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("getIPAddress payload_buf_p NULL\r\n"); + snic_core_p->unlockAPI(); + return 0; + } + + C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + // Wait UART response + int ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "getIPAddress failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; + } + + sprintf(gateway_addr, "%d.%d.%d.%d\0", payload_buf_p->buf[13], payload_buf_p->buf[14], payload_buf_p->buf[15], payload_buf_p->buf[16]); + + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + + return gateway_addr; +} + +int C_SNIC_WifiInterface::resolveName(const char *name, unsigned char *ipaddr) +{ + int name_length; + + // Parameter check + if( (name == NULL) || (ipaddr == NULL) || + ((name_length = strlen(name)) > 100)) + { + DEBUG_PRINT("resolveName parameter error\r\n"); + return -1; + } + + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("resolveName payload_buf_p NULL\r\n"); + return -1; + } + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + + C_SNIC_Core::tagSNIC_RESOLVE_NAME_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + req.name_length = name_length; + strcpy((char*)req.name, name); + + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_RESOLVE_NAME_REQ_T) - sizeof(req.name) + name_length + 1, payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "resolveName failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("resolveName status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + return -1; + } + + ipaddr[0] = payload_buf_p->buf[3]; + ipaddr[1] = payload_buf_p->buf[4]; + ipaddr[2] = payload_buf_p->buf[5]; + ipaddr[3] = payload_buf_p->buf[6]; + + snic_core_p->freeCmdBuf( payload_buf_p ); + return ret; +} + +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/SNIC_WifiInterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/SNIC_WifiInterface.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,169 @@ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * muRata, SWITCH SCIENCE Wi-FI module TypeYD-SNIC UART. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef _SNIC_WIFIINTERFACE_H_ +#define _SNIC_WIFIINTERFACE_H_ + +#include "SNICInterface/SNIC/SNIC_Core.h" +#include "SNICInterface/SNIC/MurataObject.h" + +/** Wi-Fi status used by getWifiStatus(). */ +typedef struct +{ + /** status */ + E_WIFI_STATUS status; + /** Mac address */ + char mac_address[BSSID_MAC_LENTH]; + /** SSID */ + char ssid[SSID_MAX_LENGTH+1]; +}tagWIFI_STATUS_T; + +/** Interface class for using SNIC UART. + */ +class C_SNIC_WifiInterface : public C_MurataObject { + +public: + /** Default constructor + */ + C_SNIC_WifiInterface() {}; + + /** Constructor + @param tx mbed pin to use for tx line of Serial interface + @param rx mbed pin to use for rx line of Serial interface + @param cts mbed pin to use for cts line of Serial interface + @param rts mbed pin to use for rts line of Serial interface + @param reset reset pin of the wifi module + @param alarm alarm pin of the wifi module (default: NC) + @param baud baud rate of Serial interface (default: 115200) + */ + C_SNIC_WifiInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200); + virtual ~C_SNIC_WifiInterface(); + + /** Create instance. + */ + void create(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200); + + /** Initialize the interface. + @return 0 on success, a negative number on failure + */ + int init(); + + /** Get Firmware version string. + @param version_p Pointer of FW version string.(null terminated)[output] + @return 0:success/other:fail + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int getFWVersion( unsigned char *version_p, int *length_p); + + /** Connect to AP + @param ssid_p Wi-Fi SSID(null terminated) + @param ssid_len Wi-Fi SSID length + @param sec_type Wi-Fi security type. + @param sec_key_len Wi-Fi passphrase or security key length + @param sec_key_p Wi-Fi passphrase or security key + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type, const char *sec_key_p, unsigned char sec_key_len); + + /** Disconnect from AP + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int disconnect(); + + /** Scan AP + @param ssid_p Wi-Fi SSID(null terminated) + If do not specify SSID, set to NULL. + @param bssid_p Wi-Fi BSSID(null terminated) + If do not specify SSID, set to NULL. + @param result_handler_p Pointer of scan result callback function. + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + Scan results will be notified by asynchronous callback function. + If there is no continuity data, scan_result will be set NULL.. + */ + int scan( const char *ssid_p, unsigned char *bssid_p + ,void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) ); + + /** Wi-Fi Turn on + @param country_p Pointer of country code. + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int wifi_on( const char *country_p ); + + /** Wi-Fi Turn off + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int wifi_off(); + + int wifi_softap_off(); + + /** Get Wi-Fi RSSI + @param rssi_p Pointer of RSSI.[output] + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int getRssi( signed char *rssi_p ); + + /** Get Wi-Fi status + @param status_p Pointer of status structure.[output] + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int getWifiStatus( tagWIFI_STATUS_T *status_p); + + /** Set IP configuration + @param is_DHCP true:DHCP false:static IP. + @param ip_p Pointer of strings of IP address.(null terminate). + @param mask_p Pointer of strings of Netmask.(null terminate). + @param gateway_p Pointer of strings of gateway address.(null terminate). + @return 0 on success, a negative number on failure + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int setIPConfig( bool is_DHCP, const char *ip_p=NULL, const char *mask_p=NULL, const char *gateway_p=NULL ); + + /** Get the IP address of your SNIC interface + * \return a pointer to a string containing the IP address + */ + static char* getIPAddress(); + static char* getSubnetAddress(); + static char* getGatewayAddress(); + + int resolveName(const char *name, unsigned char *ipaddr); + +private: + PinName mUART_tx; + PinName mUART_rx; + PinName mUART_cts; + PinName mUART_rts; + int mUART_baud; + PinName mModuleReset; +}; +#endif /* _YD_WIFIINTERFACE_H_ */
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/Endpoint.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/Endpoint.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,59 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#include "SNICInterface/Socket/SNIC_Socket.h" +#include "SNICInterface/Socket/Endpoint.h" +#include <cstring> +#include <cstdio> + +Endpoint::Endpoint() +{ + reset_address(); +} + +Endpoint::~Endpoint() +{ +} + +void Endpoint::reset_address(void) +{ + mIpAddress[0] = '\0'; +} + +int Endpoint::set_address(const char* host_p, const int port) +{ + reset_address(); + + strcpy( mIpAddress, host_p ); + mPort = port; + + return 0; +} + +char* Endpoint::get_address() +{ + return mIpAddress; +} + +int Endpoint::get_port() +{ + return mPort; +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/Endpoint.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/Endpoint.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,67 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#ifndef SNIC_ENDPOINT_H +#define SNIC_ENDPOINT_H + +class UDPSocket; + +/** +IP Endpoint (address, port) +*/ +class Endpoint { + friend class SnicUDPSocket; + +public: + /** IP Endpoint (address, port) + */ + Endpoint(void); + + ~Endpoint(void); + + /** Reset the address of this endpoint + */ + void reset_address(void); + + /** Set the address of this endpoint + \param host_p The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). + \param port The endpoint port + \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + */ + int set_address(const char* host_p, const int port); + + /** Get the IP address of this endpoint + \return The IP address of this endpoint. + */ + char* get_address(void); + + /** Get the port of this endpoint + \return The port of this endpoint + */ + int get_port(void); + +protected: + char mIpAddress[17]; + int mPort; +// struct sockaddr_in _remoteHost; + +}; +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/SNIC_Socket.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/SNIC_Socket.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,262 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#include "SNICInterface/Socket/SNIC_Socket.h" +#include <cstring> + +#include "mbed.h" +#include "mbed-trace/mbed_trace.h" +#define TRACE_GROUP "SNIC" + +char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1"))); + +SnicSocket::SnicSocket() +{ + mSocketID = -1; + _callback = NULL; + _data = NULL; +} + +void SnicSocket::set_blocking(bool blocking, unsigned int timeout) { + _blocking = blocking; + _timeout = timeout; +} + +SnicSocket::~SnicSocket() { +// close(); //Don't want to leak +} + +int SnicSocket::set_option(int level, int optname, const void *optval, socklen_t optlen) { + return 0; +} + +int SnicSocket::get_option(int level, int optname, void *optval, socklen_t *optlen) { + return 0; +} + +int SnicSocket::close(bool shutdown) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + FUNC_IN(); + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + if( payload_buf == NULL ) + { + DEBUG_PRINT("SnicSocket close payload_buf NULL\r\n"); + FUNC_OUT(); + return -1; + } + + C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req; + + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ; + req.seq = mUartRequestSeq++; + req.socket_id = mSocketID; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "SnicSocket close failed:%d\r\n", ret ); + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("SnicSocket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return 0; +} + +int SnicSocket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + FUNC_IN(); + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + if( payload_buf == NULL ) + { + DEBUG_PRINT("createSocket payload_buf NULL\r\n"); + FUNC_OUT(); + return -1; + } + + C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req; + int req_len = 0; + + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ; + req_len++; + req.seq = mUartRequestSeq++; + req_len++; + req.bind = bind; + req_len++; + if( bind != 0 ) + { + // set ip addr ( byte order ) + C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)req.local_addr ); + req.local_port[0] = ( (port & 0xFF00) >> 8 ); + req.local_port[1] = (port & 0xFF); + + req_len = sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T); + } + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , req_len, payload_buf->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "createSocket failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return -1; + } + mSocketID = payload_buf->buf[3]; + tr_debug("createSocket:mSocketID=%d", mSocketID); + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return 0; +} + +int SnicSocket::resolveHostName( const char *host_p ) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + int ip_addr = 0; + + if( host_p == NULL ) + { + DEBUG_PRINT("resolveHostName parameter error\r\n"); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + if( payload_buf == NULL ) + { + DEBUG_PRINT("resolveHostName payload_buf NULL\r\n"); + return -1; + } + + unsigned char *buf_p = (unsigned char *)getSocketSendBuf(); + unsigned int buf_len = 0; + + memset( buf_p, 0, UART_REQUEST_PAYLOAD_MAX ); + // Make request + buf_p[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ; + buf_len++; + buf_p[1] = mUartRequestSeq++; + buf_len++; + // Interface + buf_p[2] = 0; + buf_len++; + + // Host name length + int hostname_len = strlen(host_p); + buf_p[3] = (unsigned char)hostname_len; + buf_len++; + memcpy( &buf_p[4], host_p, hostname_len ); + buf_len += hostname_len; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf_p + , buf_len, payload_buf->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + int ret; + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "resolveHostName failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf ); + return -1; + } + + // check status + if( uartCmdMgr_p->getCommandStatus() == 0 ) + { + ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000) + | ((payload_buf->buf[4] << 16) & 0xFF0000) + | ((payload_buf->buf[5] << 8) & 0xFF00) + | (payload_buf->buf[6]); + } + + snic_core_p->freeCmdBuf( payload_buf ); + return ip_addr; +} + +char *SnicSocket::getSocketSendBuf() +{ + return gSOCKET_SEND_BUF; +} + +void SnicSocket::socket_attach(void (*callback)(void *), void *data) +{ + _callback = callback; + _data = data; +} + +void SnicSocket::event() +{ + if (_callback) { + (*_callback)(_data); + } +}
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/SNIC_Socket.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/SNIC_Socket.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,99 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#ifndef SNIC_SOCKET_H_ +#define SNIC_SOCKET_H_ + +#include "SNICInterface/SNIC/SNIC_Core.h" +#include "SNICInterface/SNIC/SNIC_UartMsgUtil.h" + +#define htons(x) __REV16(x) +#define ntohs(x) __REV16(x) +#define htonl(x) __REV(x) +#define ntohl(x) __REV(x) + +typedef unsigned long socklen_t; + +/** Socket file descriptor and select wrapper + */ +class SnicSocket { +public: + /** Socket + */ + SnicSocket(); + + /** Set blocking or non-blocking mode of the socket and a timeout on + blocking socket operations + \param blocking true for blocking mode, false for non-blocking mode. + \param timeout timeout in ms [Default: (1500)ms]. + */ + void set_blocking(bool blocking, unsigned int timeout=1500); + + /** Set socket options + @param level stack level (see: lwip/sockets.h) + @param optname option ID + @param optval option value + @param socklen_t length of the option value + @return 0 on success, -1 on failure + */ + int set_option(int level, int optname, const void *optval, socklen_t optlen); + + /** Get socket options + @param level stack level (see: lwip/sockets.h) + @param optname option ID + \param optval buffer pointer where to write the option value + \param socklen_t length of the option value + \return 0 on success, -1 on failure + */ + int get_option(int level, int optname, void *optval, socklen_t *optlen); + + /** Close the socket + \param shutdown free the left-over data in message queues + */ + int close(bool shutdown=true); + + virtual ~SnicSocket(); + + int createSocket( unsigned char bind = 0, unsigned int local_addr = 0, unsigned short port = 0 ); + + void socket_attach(void (*callback)(void *), void *data); + + void event(); + +protected: + int resolveHostName( const char *host_p ); + +protected: + int mSocketID; + bool _blocking; + int _timeout; + char *getSocketSendBuf(); +public: + void (*_callback)(void *); + void *_data; + +private: + +// int select(struct timeval *timeout, bool read, bool write); + +}; + +#endif /* SNIC_SOCKET_H_ */ +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/SNIC_UDPSocket.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/SNIC_UDPSocket.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,327 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#include "SNICInterface/Socket/SNIC_UDPSocket.h" +#include <cstring> +#include "mbed.h" +#include "mbed-trace/mbed_trace.h" + +#define TRACE_GROUP "SNIC" + +SnicUDPSocket::SnicUDPSocket() { +} + +SnicUDPSocket::~SnicUDPSocket() +{ +} + +int SnicUDPSocket::init(void) +{ + return 0; +} + +// Server initialization +int SnicUDPSocket::bind(short port) +{ + int ret; + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + FUNC_IN(); + // Get local ip address. + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("UDP bind payload_buf_p NULL\r\n"); + FUNC_OUT(); + return -1; + } + + C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "UDP bind failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + unsigned int local_addr = (payload_buf_p->buf[9] << 24) + | (payload_buf_p->buf[10] << 16) + | (payload_buf_p->buf[11] << 8) + | (payload_buf_p->buf[12]); + + + C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T create_req; + + // Make request + create_req.cmd_sid = UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ; + create_req.seq = mUartRequestSeq++; + create_req.bind = 1; + // set ip addr ( byte order ) + C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)create_req.local_addr ); + create_req.local_port[0] = ( (port & 0xFF00) >> 8 ); + create_req.local_port[1] = (port & 0xFF); + + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, create_req.cmd_sid, (unsigned char *)&create_req + , sizeof(C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "UDP bind failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + mSocketID = payload_buf_p->buf[3]; + + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p->recvbuf_p == NULL ) + { + //DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID); + con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); + } + con_info_p->is_connected = true; + con_info_p->snic_socket = this; + + C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req; + + // Make request + recv_start_req.cmd_sid = UART_CMD_SID_SNIC_UDP_START_RECV_REQ; + recv_start_req.seq = mUartRequestSeq++; + recv_start_req.socket_id = mSocketID; + recv_start_req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); + recv_start_req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); + + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, recv_start_req.cmd_sid, (unsigned char *)&recv_start_req + , sizeof(C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "UDP recv start failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return 0; +} + +// -1 if unsuccessful, else number of bytes written +int SnicUDPSocket::sendTo(Endpoint &remote, char *packet, int length) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + +// pc.printf("send[%08x] len:%d(%04x)\r\n", tid, length, length); + +#if 0 // TODO: Not wait for command response(Tentative) + snic_core_p->lockAPI(); +#endif + FUNC_IN(); + +#if 0 // TODO: Not wait for command response(Tentative) + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("connect payload_buf_p NULL\r\n"); + FUNC_OUT(); + snic_core_p->unlockAPI(); + return -1; + } +#endif + + C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_REQ; + req.seq = mUartRequestSeq++; + + int addr_temp; + addr_temp = C_SNIC_UartMsgUtil::addrToInteger( remote.get_address() ); + C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.remote_ip ); + req.remote_port[0] = ( (remote.get_port() & 0xFF00) >> 8 ); + req.remote_port[1] = (remote.get_port() & 0xFF); + req.payload_len[0] = ( (length & 0xFF00) >> 8 ); + req.payload_len[1] = (length & 0xFF); + req.socket_id = mSocketID; + req.connection_mode = 0; + + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p != NULL ) + { + con_info_p->from_ip = addr_temp; + con_info_p->from_port = remote.get_port(); + } + + int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T); + + char *send_buf_p = getSocketSendBuf(); + memcpy( send_buf_p, &req, req_size ); + memcpy( &send_buf_p[req_size], packet, length ); + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + + // Make all command request + command_len = C_SNIC_UartMsgUtil::makeRequest( UART_CMD_ID_SNIC, (unsigned char *)send_buf_p, req_size + length, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + +#if 0 // TODO: Not wait for command response(Tentative) + // Wait UART response + int ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "send failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + snic_core_p->unlockAPI(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + snic_core_p->unlockAPI(); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); +#endif + + FUNC_OUT(); +#if 0 // TODO: Not wait for command response(Tentative) + snic_core_p->unlockAPI(); +#endif + // SNIC_SEND_FROM_SOCKET_REQ + DEBUG_PRINT("sendTo %d\r\n", length); + + return length; +// return 0; +} + +// -1 if unsuccessful, else number of bytes received +int SnicUDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length) +{ + FUNC_IN(); + if( (data_p == NULL) || (length < 1) ) + { + DEBUG_PRINT("UDPSocket::receiveFrom parameter error\r\n"); + FUNC_OUT(); + return -1; + } + + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p->recvbuf_p == NULL ) + { + DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n"); + FUNC_OUT(); + return -1; + } + + // TODO : wait receive timeout + if (con_info_p->recvbuf_p->isEmpty()) { + return NSAPI_ERROR_WOULD_BLOCK; + } + + con_info_p->mutex.lock(); + + char remote_ip[20] = {'\0'}; + sprintf( remote_ip, "%d.%d.%d.%d" + , (con_info_p->from_ip >>24) & 0x000000ff + , (con_info_p->from_ip >>16) & 0x000000ff + , (con_info_p->from_ip >>8) & 0x000000ff + , (con_info_p->from_ip) & 0x000000ff ); + remote.set_address( remote_ip, con_info_p->from_port ); + + // Get packet data from buffer for receive. + int i = 0; + for (i = 0; i < length; i ++) + { + if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) + { + break; + } + } + + con_info_p->mutex.unlock(); + + FUNC_OUT(); + return i; +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/SNIC_UDPSocket.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/SNIC_UDPSocket.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#ifndef SNIC_UDPSOCKET_H +#define SNIC_UDPSOCKET_H + +#include "SNICInterface/Socket/SNIC_Socket.h" +#include "SNICInterface/Socket/Endpoint.h" + +/** +Interface class for UDP socket of using SNIC UART. +*/ +class SnicUDPSocket : public SnicSocket { + +public: + /** Instantiate an UDP Socket. + */ + SnicUDPSocket(); + virtual ~SnicUDPSocket(); + + /** Init the UDP Client Socket without binding it to any specific port + \return 0 on success, -1 on failure. + */ + int init(void); + + /** Bind a UDP Server Socket to a specific port + \param port The port to listen for incoming connections on + \return 0 on success, -1 on failure. + */ + int bind(short port); + + /** Send a packet to a remote endpoint + \param remote The remote endpoint + \param packet The packet to be sent + \param length The length of the packet to be sent + \return the number of written bytes on success (>=0) or -1 on failure + */ + int sendTo(Endpoint &remote, char *packet, int length); + + /** Receive a packet from a remote endpoint + \param remote The remote endpoint + \param buffer The buffer for storing the incoming packet data. If a packet + is too long to fit in the supplied buffer, excess bytes are discarded + \param length The length of the buffer + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receiveFrom(Endpoint &remote, char *buffer, int length); +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/TCPSocketConnection.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/TCPSocketConnection.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,294 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#include "SNICInterface/Socket/TCPSocketConnection.h" +#include <cstring> + +#include "mbed.h" +#include "mbed-trace/mbed_trace.h" +#define TRACE_GROUP "SNIC" + +TCPSocketConnection::TCPSocketConnection() +{ +} + +TCPSocketConnection::~TCPSocketConnection() +{ +} + +int TCPSocketConnection::bind(short port) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + + FUNC_IN(); + + // TODO : bind to specified port + // but Type YD dosn't accept when UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ +// int ret = createSocket(1, 0, port); + int ret = createSocket(); + if (ret != 0) + { + DEBUG_PRINT( "TCP bind failed\r\n" ); + FUNC_OUT(); + return -1; + } + + FUNC_OUT(); + return 0; +} + +int TCPSocketConnection::connect( const char *host_p, unsigned short port) +{ + int ret; + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + FUNC_IN(); + // Socket create + if (mSocketID < 0) { + ret = createSocket(); + if( ret != 0 ) + { + DEBUG_PRINT("createSocket error : %d\r\n", ret); + FUNC_OUT(); + return -1; + } + } + + int ip_addr = resolveHostName( host_p ); + //lcd_printf("connect to [%s](%08x)\r\n", host_p, ip_addr); + if( ( ip_addr == 0) || (ip_addr == -1) ) + { + DEBUG_PRINT("connect resolveHostName failed\r\n"); + FUNC_OUT(); + return -1; + } + + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("connect payload_buf_p NULL\r\n"); + FUNC_OUT(); + return -1; + } + + // IP address convert to number from strings. +// unsigned int ip_addr = addrToInteger(ip_addr_p); + + // + C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ; + req.seq = mUartRequestSeq++; + req.socket_id = mSocketID; + + // set ip addr ( byte order ) + C_SNIC_UartMsgUtil::convertIntToByteAdday( ip_addr, (char *)req.remote_addr ); + req.remote_port[0] = ( (port & 0xFF00) >> 8 ); + req.remote_port[1] = (port & 0xFF); + req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); + req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); + req.timeout = 60; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf_p->buf, command_array_p ); + + uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "connect failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP ) + { + DEBUG_PRINT("connect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + + //DEBUG_PRINT( "TCP connection success[%d]\n", mSocketID); + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p->recvbuf_p == NULL ) + { + //DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID); + con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); + } + con_info_p->is_connected = true; + con_info_p->is_received = false; + con_info_p->snic_socket = this; + FUNC_OUT(); + return 0; +} + +bool TCPSocketConnection::is_connected(void) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + return con_info_p->is_connected; +} + +int TCPSocketConnection::send(char* data_p, int length) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + FUNC_IN(); + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("connect payload_buf_p NULL\r\n"); + FUNC_OUT(); + return -1; + } + + C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ; + req.seq = mUartRequestSeq++; + req.socket_id = mSocketID; + req.option = 0; + req.payload_len[0]= ( (length & 0xFF00) >> 8 ); + req.payload_len[1]= (length & 0xFF); + + int req_size = sizeof(C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T); + char *send_buf_p = getSocketSendBuf(); + memcpy( send_buf_p, &req, req_size ); + memcpy( &send_buf_p[req_size], data_p, length ); + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)send_buf_p + , req_size + length, payload_buf_p->buf, command_array_p ); + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + int ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "send failed:%d\r\n", ret ); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + return -1; + } + snic_core_p->freeCmdBuf( payload_buf_p ); + + // SNIC_SEND_FROM_SOCKET_REQ + FUNC_OUT(); + return length; +} + +int TCPSocketConnection::send_all(char *data_p, int length) +{ + return send( data_p, length ); +} + +int TCPSocketConnection::receive(char* data_p, int length) +{ + FUNC_IN(); + if( (data_p == NULL) || (length < 1) ) + { + DEBUG_PRINT("TCPSocketConnection::receive parameter error\r\n"); + FUNC_OUT(); + return -1; + } + + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p->recvbuf_p == NULL ) + { + DEBUG_PRINT("TCPSocketConnection::receive Conncection info error\r\n"); + FUNC_OUT(); + return -1; + } + + // Check connection + if( con_info_p->is_connected == false ) + { + DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", mSocketID); + FUNC_OUT(); + return -1; + } + + // TODO : wait receive timeout + if (con_info_p->recvbuf_p->isEmpty()) { + return NSAPI_ERROR_WOULD_BLOCK; + } + + con_info_p->mutex.lock(); + + // Get packet data from buffer for receive. + int i = 0; + for (i = 0; i < length; i ++) + { + if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) + { + break; + } + } + + con_info_p->mutex.unlock(); + + FUNC_OUT(); + return i; +} + +int TCPSocketConnection::receive_all(char* data_p, int length) +{ + return receive( data_p, length ); +} + +void TCPSocketConnection::setAcceptSocket( int socket_id ) +{ + FUNC_IN(); + mSocketID = socket_id; + tr_debug("setAcceptSocket:mSocketID=%d", mSocketID); + FUNC_OUT(); +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/TCPSocketConnection.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/TCPSocketConnection.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,91 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#ifndef SNIC_TCPSOCKET_H +#define SNIC_TCPSOCKET_H + +#include "SNICInterface/Socket/SNIC_Socket.h" +#include "SNICInterface/Socket/Endpoint.h" + +/** + Interface class for TCP socket of using SNIC UART. +*/ +class TCPSocketConnection : public SnicSocket, public Endpoint { + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + virtual ~TCPSocketConnection(); + + /** Bind a TCP Socket to a specific port + \param port The port to listen for incoming connections on + \return 0 on success, -1 on failure. + */ + int bind(short port); + + /** Connects this TCP socket to the server + @param host The strings of ip address. + @param port The host's port to connect to. + @return 0 on success, -1 on failure. + @note This function is blocked until a returns. + When you use it by UI thread, be careful. + */ + int connect( const char *ip_addr_p, unsigned short port ); + + /** Check if the socket is connected + @return true if connected, false otherwise. + */ + bool is_connected(void); + + /** Send data to the remote host. + @param data The buffer to send to the host. + @param length The length of the buffer to send. + @return the number of written bytes on success (>=0) or -1 on failure + */ + int send(char *data_p, int length); + + /** Send data to the remote host. + @param data The buffer to send to the host. + @param length The length of the buffer to send. + @return the number of written bytes on success (>=0) or -1 on failure + */ + int send_all(char *data_p, int length); + + /** Receive data from the remote host. + @param data The buffer in which to store the data received from the host. + @param length The maximum length of the buffer. + @return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(char *data_p, int length); + + /** Send data to the remote host. + @param data The buffer to send to the host. + @param length The length of the buffer to send. + @return the number of written bytes on success (>=0) or -1 on failure + */ + int receive_all(char *data_p, int length); + + void setAcceptSocket( int socket_id ); +private: + +}; +#endif +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/TCPSocketServer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/TCPSocketServer.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,201 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#include "SNICInterface/Socket/TCPSocketServer.h" +#include "SNICInterface/SNIC/SNIC_Core.h" + +#include <cstring> + +TCPSocketServer::TCPSocketServer() +{ +} + +TCPSocketServer::~TCPSocketServer() +{ +} + +int TCPSocketServer::bind(unsigned short port) +{ + int ret; + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + snic_core_p->lockAPI(); + // Get local ip address. + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("bind payload_buf_p NULL\r\n"); + snic_core_p->unlockAPI(); + return -1; + } + + C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ; + req.seq = mUartRequestSeq++; + req.interface = 0; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf_p->buf, command_array_p ); + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "bind failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) + { + DEBUG_PRINT("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return -1; + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + + unsigned int local_addr = (payload_buf_p->buf[9] << 24) + | (payload_buf_p->buf[10] << 16) + | (payload_buf_p->buf[11] << 8) + | (payload_buf_p->buf[12]); + + // Socket create + ret = createSocket( 1, local_addr, port ); + if( ret != 0 ) + { + DEBUG_PRINT("bind error : %d\r\n", ret); + return -1; + } + + return 0; +} + +int TCPSocketServer::listen(int max) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + + snic_core_p->lockAPI(); + // Get buffer for response payload from MemoryPool + tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); + if( payload_buf_p == NULL ) + { + DEBUG_PRINT("listen payload_buf_p NULL\r\n"); + snic_core_p->unlockAPI(); + return -1; + } + + C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T req; + // Make request + req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ; + req.seq = mUartRequestSeq++; + req.socket_id = mSocketID; + req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); + req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); + req.max_client = max; + + unsigned char *command_array_p = snic_core_p->getCommandBuf(); + unsigned int command_len; + // Preparation of command + command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req + , sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T), payload_buf_p->buf, command_array_p ); + + int ret; + + // Send uart command request + snic_core_p->sendUart( command_len, command_array_p ); + + // Wait UART response + ret = uartCmdMgr_p->wait(); + if( ret != 0 ) + { + DEBUG_PRINT( "listen failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return -1; + } + + snic_core_p->freeCmdBuf( payload_buf_p ); + snic_core_p->unlockAPI(); + return 0; +} + +int TCPSocketServer::accept(TCPSocketConnection& connection, SocketAddress *address) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + int i; + int ret = -1; + bool accepted = false; + + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p; + + do + { + for( i = 0; i < MAX_SOCKET_ID+1; i++ ) + { + // Get connection information + con_info_p = snic_core_p->getConnectInfo( i ); + if( (con_info_p->is_connected == true) + && (con_info_p->is_accept == true) + && (con_info_p->parent_socket == mSocketID) ) + { + // Set socket id + connection.setAcceptSocket( i ); + ret = 0; + accepted = true; + break; + } + } + } while( accepted == false ); + con_info_p->is_accept = false; + + char ip_bytes[4] = { + (con_info_p->from_ip >> 24 ) & 0xff, + (con_info_p->from_ip >> 16 ) & 0xff, + (con_info_p->from_ip >> 8 ) & 0xff, + con_info_p->from_ip & 0xff }; + + address->set_ip_bytes(ip_bytes, NSAPI_IPv4); + address->set_port(con_info_p->from_port); + + connection.set_address( address->get_ip_address(), (int)address->get_port() ); + + return ret; +} +
diff -r 000000000000 -r 35a2186cf186 SNICInterface/Socket/TCPSocketServer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNICInterface/Socket/TCPSocketServer.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,62 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License + * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. + */ +#ifndef SNIC_TCPSOCKETSERVER_H +#define SNIC_TCPSOCKETSERVER_H + +#include "SNICInterface/Socket/SNIC_Socket.h" +#include "SNICInterface/Socket/TCPSocketConnection.h" + +/** + Interface class for TCP server socket of using SNIC UART. + +*/ +class TCPSocketServer : public SnicSocket { + public: + /** Instantiate a TCP Server. + */ + TCPSocketServer(); + virtual ~TCPSocketServer(); + + /** Bind a socket to a specific port. + \param port The port to listen for incoming connections on. + \return 0 on success, -1 on failure. + */ + int bind(unsigned short port); + + /** Start listening for incoming connections. + \param backlog number of pending connections that can be queued up at any + one time [Default: 1]. + \return 0 on success, -1 on failure. + */ + int listen(int backlog=1); + + /** Accept a new connection. + \param connection A TCPSocketConnection instance that will handle the incoming connection. + \return 0 on success, -1 on failure. + */ + int accept(TCPSocketConnection& connection, SocketAddress *address); + +private: + SocketAddress *_connected_address; +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 TypeYDInterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TypeYDInterface.cpp Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,650 @@ +/* Murata Type-YD implementation of NetworkInterfaceAPI + * Copyright (c) 2017 Murat Manufacturing CO., LTD. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string.h> +#include "TypeYDInterface.h" +#include "SNICInterface/Socket/SNIC_Socket.h" +#include "SNICInterface/Socket/Endpoint.h" +#include "SNICInterface/Socket/SNIC_UDPSocket.h" +#include "SNICInterface/Socket/TCPSocketConnection.h" +#include "SNICInterface/Socket/TCPSocketServer.h" + +#include "mbed.h" +#include "mbed-trace/mbed_trace.h" +#define TRACE_GROUP "SNIC" + +//#define USE_DYNAMIC_CAST + +// socket structure +struct managed_socket { + nsapi_protocol_t proto; + bool connected; + SnicSocket *snic_socket; + void (*callback)(void *); + void *data; +}; + + +// TypeYDInterface implementation +TypeYDInterface::TypeYDInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) + : _snic(tx, rx, cts, rts, reset, alarm, baud) +{ +} + +int TypeYDInterface::init() +{ + int ret = _snic.init(); + if (ret == 0) { + tagWIFI_STATUS_T status; + _snic.getWifiStatus(&status); + memcpy(mac_address, status.mac_address, sizeof(mac_address)); + sprintf(str_macaddr, "%02x:%02x:%02x:%02x:%02x:%02x", + mac_address[0], mac_address[1], mac_address[2], + mac_address[3], mac_address[4], mac_address[5]); + + ret = _snic.wifi_softap_off(); + if (ret != 0) { + tr_error("wifi off error"); + } + } + return ret; +} + +int TypeYDInterface::getFWVersion(unsigned char *version, int length) +{ + int ret = _snic.getFWVersion(version, &length); + return ret; +} + +int TypeYDInterface::connect() +{ + int ret; + + if( _snic.disconnect() != 0 ) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + int ssid_length = strlen(ap_ssid); + int pass_length = strlen(ap_pass); + + if (_snic.connect(ap_ssid, ssid_length, ap_esec, ap_pass, pass_length) != 0) { + ret = NSAPI_ERROR_NO_CONNECTION; + } + else if (_snic.setIPConfig(true) != 0) { + ret = NSAPI_ERROR_DHCP_FAILURE; + } + else { + ret = NSAPI_ERROR_OK; + } + + return ret; +} + + +int TypeYDInterface::connect(const char *ssid, const char *pass, nsapi_security_t security, + uint8_t channel) +{ + int ret; + + if (channel != 0) { + return NSAPI_ERROR_UNSUPPORTED; + } + + set_credentials(ssid, pass, security); + int ssid_length = strlen(ap_ssid); + int pass_length = strlen(ap_pass); +#if 0 + int repeat = 0; + bool bFound = false; + do { + tr_info("scan...%d\n", repeat); + WiFiAccessPoint res[10]; + ret = scan(res,10); + if (ret < 0) { + tr_info("scan fail %d\n",ret); + } + else { + tr_info("scan result = %d\n", ret); + for (int i = 0; i < ret; i++) { + tr_info("ssid :%s\n", res[i].get_ssid()); + tr_info("channel :%d\n", res[i].get_channel()); + if (strcmp(ap_ssid, res[i].get_ssid()) == 0) { + bFound = true; + tr_info("-------- BSS ---------\n"); + tr_info("ssid :%s\n", res[i].get_ssid()); + tr_info("channel :%d\n", res[i].get_channel()); + tr_info("rssi :%d\n", res[i].get_rssi()); + tr_info("security :%d\n", res[i].get_security()); + const uint8_t *bssid = res[i].get_bssid(); + tr_info("bssid :%02x-%02x-%02x-%02x-%02x-%02x\n", + bssid[0],bssid[1],bssid[2], + bssid[3],bssid[4],bssid[5]); + break; + } + } + } + }while((bFound == false) && (repeat++ < 10)); +#endif + do { + wait(0.5); + if( _snic.disconnect() != 0 ) { + return NSAPI_ERROR_DEVICE_ERROR; + } + wait(0.5); + + /* + ret = _snic.getWifiStatus(&status); + if (ret == 0) { + printf("status:%02x\n", status.status); + printf("mac :%02x-%02x-%02x-%02x-%02x-%02x\n", + status.mac_address[0],status.mac_address[1],status.mac_address[2], + status.mac_address[3],status.mac_address[4],status.mac_address[5]); + }*/ + tr_info("TryConnect\r\n"); + + if (_snic.connect(ap_ssid, ssid_length, ap_esec, ap_pass, pass_length) != 0) { + ret = NSAPI_ERROR_NO_CONNECTION; + } + else if (_snic.setIPConfig(true) != 0) { + ret = NSAPI_ERROR_DHCP_FAILURE; + } + else { + ret = NSAPI_ERROR_OK; + } + } while (ret != NSAPI_ERROR_OK); + + return ret; +} + + +int TypeYDInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) +{ + ap_esec = e_SEC_WPA2_MIXED; + switch(security) { + case NSAPI_SECURITY_NONE: /*!< open access point */ + ap_esec = e_SEC_OPEN; + break; + case NSAPI_SECURITY_WEP: /*!< phrase conforms to WEP */ + ap_esec = e_SEC_WEP; + break; + case NSAPI_SECURITY_WPA: /*!< phrase conforms to WPA */ + ap_esec = e_SEC_WPA_TKIP; + break; + case NSAPI_SECURITY_WPA2: /*!< phrase conforms to WPA2 */ + ap_esec = e_SEC_WPA2_MIXED; + break; + case NSAPI_SECURITY_WPA_WPA2: /*!< phrase conforms to WPA/WPA2 */ + ap_esec = e_SEC_WPA2_MIXED; + break; + default: + break; + } + + strncpy(ap_ssid, ssid, sizeof(ap_ssid)); + ap_ssid[sizeof(ap_ssid)-1] = 0; + + strncpy(ap_pass, pass, sizeof(ap_pass)); + ap_pass[sizeof(ap_pass)-1] = 0; + + return 0; +} + +int TypeYDInterface::set_channel(uint8_t channel) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +int TypeYDInterface::disconnect() +{ + if (!_snic.disconnect()) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + return NSAPI_ERROR_OK; +} + +const char *TypeYDInterface::get_ip_address() +{ + return _snic.getIPAddress(); +} + +const char *TypeYDInterface::get_mac_address() +{ + + return (const char*)str_macaddr; +} + +const char *TypeYDInterface::get_gateway() +{ + return _snic.getGatewayAddress(); +} + +const char *TypeYDInterface::get_netmask() +{ + return _snic.getSubnetAddress(); +} + +int8_t TypeYDInterface::get_rssi() +{ + signed char rssi; + _snic.getRssi(&rssi); + + return (int8_t)rssi; +} + +static volatile int _scan_wait = 0; +static WiFiAccessPoint *_scan_results = 0; +static int _scan_count; +static int _scan_limit; +static void scan_results_callback(tagSCAN_RESULT_T *scan_result) +{ + nsapi_wifi_ap_t ap; + + if (_scan_count < _scan_limit) { + memcpy(ap.ssid, scan_result->ssid, 33); + memcpy(ap.bssid, scan_result->bssid, 6); + switch(scan_result->security) { + case e_SEC_OPEN: + ap.security = NSAPI_SECURITY_NONE; + break; + case e_SEC_WEP: + ap.security = NSAPI_SECURITY_WEP; + break; + case e_SEC_WPA_TKIP: + ap.security = NSAPI_SECURITY_WPA; + break; + case e_SEC_WPA2_AES: + ap.security = NSAPI_SECURITY_WPA2; + break; + case e_SEC_WPA2_MIXED: + ap.security = NSAPI_SECURITY_WPA_WPA2; + break; + case e_SEC_WPA_AES: + ap.security = NSAPI_SECURITY_WPA; + break; + default: + ap.security = NSAPI_SECURITY_UNKNOWN; + break; + } + ap.rssi = scan_result->rssi; + ap.channel = scan_result->channel; + _scan_results[_scan_count++] = WiFiAccessPoint(ap); + } + + if (scan_result->is_complete == 0) _scan_wait = 1; +} + +int TypeYDInterface::scan(WiFiAccessPoint *res, unsigned count) +{ + _scan_results = res; + _scan_limit = count; + _scan_count = 0; + int ret = _snic.scan(NULL, NULL, scan_results_callback); + if (ret == 0) { + _scan_wait = 0; + while(_scan_wait == 0) { + wait(1); + } + } + + return _scan_count; +} + +nsapi_error_t TypeYDInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) +{ + int ret; + unsigned char ipaddr[4]; + + ret = _snic.resolveName(name, ipaddr); + if (ret != 0) { + tr_error("DNS failure\n"); + return NSAPI_ERROR_DNS_FAILURE; + } + + tr_info("DNS res:%u.%u.%u.%u\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); + + address->set_ip_bytes(ipaddr, NSAPI_IPv4); + + return NSAPI_ERROR_OK; +} + +int TypeYDInterface::socket_open(void **handle, nsapi_protocol_t proto) +{ + tr_info("socket_open"); + + struct managed_socket *socket = new struct managed_socket; + if (!socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + socket->proto = proto; + socket->connected = false; + socket->snic_socket = NULL; + socket->callback = NULL; + socket->data = NULL; + *handle = socket; + + return 0; +} + +int TypeYDInterface::socket_close(void *handle) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + + tr_info("socket_close"); + + int err = 0; + if (socket->snic_socket != NULL) { + err = socket->snic_socket->close(); + delete socket->snic_socket; + } + + delete socket; + return err; +} + +int TypeYDInterface::socket_bind(void *handle, const SocketAddress &address) +{ + int ret; + uint16_t port; + + struct managed_socket *socket = (struct managed_socket *)handle; + + port = address.get_port(); + if (port == 0) { +#if defined(ST) + srand(HAL_GetTick()); +#else + srand(0); +#endif + port = (rand() % 10240) + 20000; + } + + tr_info("socket_bind IP:%s:%u\n", address.get_ip_address(), port); + + if (socket->proto == NSAPI_UDP) { + SnicUDPSocket *udp = new SnicUDPSocket(); + socket->snic_socket = udp; + if (socket->snic_socket == NULL) { + return NSAPI_ERROR_DEVICE_ERROR; + } + socket->snic_socket->socket_attach(socket->callback, socket->data); + ret = udp->bind(port); + if (ret != 0) { + tr_info("bind err %d\n", ret); + return NSAPI_ERROR_DEVICE_ERROR; + } + socket->connected = true; + } + else { + TCPSocketConnection *tcp = new TCPSocketConnection(); + socket->snic_socket = tcp; + if (socket->snic_socket == NULL) { + tr_error("tcp bind err\n"); + return NSAPI_ERROR_DEVICE_ERROR; + } + socket->snic_socket->socket_attach(socket->callback, socket->data); + ret = tcp->bind(port); + if (ret != 0) { + tr_error("tcp bind err %d\n", ret); + return NSAPI_ERROR_DEVICE_ERROR; + } + socket->connected = false; + } + + return ret; +} + +int TypeYDInterface::socket_listen(void *handle, int backlog) +{ + int ret; + struct managed_socket *socket = (struct managed_socket *)handle; + + if (socket->proto == NSAPI_UDP) { + tr_info("unsupport:socket_listen\n"); + return NSAPI_ERROR_UNSUPPORTED; + } + else { + TCPSocketServer* tcp; + #ifdef USE_DYNAMIC_CAST + tcp = dynamic_cast<TCPSocketServer*>(socket->snic_socket); + if (tcp == NULL){ + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + tcp = (TCPSocketServer*)(socket->snic_socket); + #endif + ret = tcp->listen(backlog); + if (ret != 0) { + tr_error("tcp listen err %d\n",ret); + return NSAPI_ERROR_DEVICE_ERROR; + } + } + return 0; +} + +int TypeYDInterface::socket_connect(void *handle, const SocketAddress &addr) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + + //tr_info("socket_connect(%d)\n",socket->proto); + + if (socket->proto == NSAPI_UDP) { + tr_error("un sup:socket_connect"); + return NSAPI_ERROR_DEVICE_ERROR; + } + else { + TCPSocketConnection *tcp; + if (socket->snic_socket == NULL) { + tcp = new TCPSocketConnection(); + socket->snic_socket = tcp; + if (socket->snic_socket == NULL) { + tr_error("tcp connect err\n"); + return NSAPI_ERROR_DEVICE_ERROR; + } + socket->snic_socket->socket_attach(socket->callback, socket->data); + } + else { + #ifdef USE_DYNAMIC_CAST + tcp = dynamic_cast<TCPSocketConnection*>(socket->snic_socket); + if (tcp == NULL){ + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + tcp = (TCPSocketConnection*)(socket->snic_socket); + #endif + } + + tr_info("socket_connect IP:%s:%u\n", addr.get_ip_address(), addr.get_port()); + if (tcp->connect(addr.get_ip_address(), addr.get_port()) != 0) { + return NSAPI_ERROR_DEVICE_ERROR; + } + } + + //tr_info("socket_connect success\n"); + socket->connected = true; + return 0; +} + +int TypeYDInterface::socket_accept(void *handle, void **client_socket, SocketAddress *addr) +{ + int ret; + struct managed_socket *server = (struct managed_socket *)handle; + + if (server->proto == NSAPI_UDP) { + tr_info("unsupport:accept"); + return NSAPI_ERROR_UNSUPPORTED; + } + else { + TCPSocketServer *tcp; + #ifdef USE_DYNAMIC_CAST + tcp = dynamic_cast<TCPSocketServer*>(server->snic_socket); + if (tcp == NULL){ + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + tcp = (TCPSocketServer*)(server->snic_socket); + #endif + socket_open(client_socket, NSAPI_TCP); + struct managed_socket *client = (struct managed_socket *)*client_socket; + TCPSocketConnection *conn = new TCPSocketConnection(); + client->snic_socket = conn; + if (client->snic_socket == NULL) { + tr_error("tcp accept err\n"); + return NSAPI_ERROR_DEVICE_ERROR; + } + ret = tcp->accept(*conn, addr); + if (ret != 0) { + tr_error("tcp listen err %d",ret); + return NSAPI_ERROR_DEVICE_ERROR; + } + } + return 0; +} + +int TypeYDInterface::socket_send(void *handle, const void *data, unsigned size) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + + //tr_info("socket_send"); + + if (socket->proto != NSAPI_TCP) { + tr_error("un sup:socket_send"); + return NSAPI_ERROR_DEVICE_ERROR; + } + + #ifdef USE_DYNAMIC_CAST + TCPSocketConnection *tcp = dynamic_cast<TCPSocketConnection*>(socket->snic_socket); + if ((tcp == NULL) || ) { + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + TCPSocketConnection *tcp = (TCPSocketConnection*)(socket->snic_socket); + #endif + if (tcp->send((char*)data, size) < 0) { + tr_error("socket_send"); + return NSAPI_ERROR_DEVICE_ERROR; + } + + return size; +} + +int TypeYDInterface::socket_recv(void *handle, void *data, unsigned size) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + + //tr_info("socket_recv"); + + if (socket->proto != NSAPI_TCP) { + tr_error("not sup:socket_recv"); + return NSAPI_ERROR_DEVICE_ERROR; + } + + int recv; + #ifdef USE_DYNAMIC_CAST + TCPSocketConnection *tcp = dynamic_cast<TCPSocketConnection*>(socket->snic_socket); + if (tcp == NULL){ + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + TCPSocketConnection *tcp = (TCPSocketConnection*)(socket->snic_socket); + #endif + if ((recv = tcp->receive((char*)data, size)) < 0) { + return NSAPI_ERROR_WOULD_BLOCK; + } + + return recv; +} + +int TypeYDInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + int ret; + + //tr_info("socket_sendto"); + + if (!socket->connected) { + int err = socket_connect(socket, addr); + if (err < 0) { + return err; + } + } + + if (socket->proto == NSAPI_UDP) { + Endpoint remote; + remote.set_address(addr.get_ip_address(), addr.get_port()); + #ifdef USE_DYNAMIC_CAST + SnicUDPSocket *udp = dynamic_cast<SnicUDPSocket*>(socket->snic_socket); + if (udp == NULL) { + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + SnicUDPSocket *udp = (SnicUDPSocket*)(socket->snic_socket); + #endif + ret = udp->sendTo(remote, (char*)data, size); + } + else { + tr_error("TCP sendto\n"); + return -1; + } + + return ret; +} + +int TypeYDInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + int recv; + + //tr_info("socket_recvfrom"); + + if (socket->proto == NSAPI_UDP) { + Endpoint remote; + #ifdef USE_DYNAMIC_CAST + SnicUDPSocket *udp = dynamic_cast<SnicUDPSocket*>(socket->snic_socket); + if (udp == NULL) { + return NSAPI_ERROR_DEVICE_ERROR; + } + #else + SnicUDPSocket *udp = (SnicUDPSocket*)(socket->snic_socket); + #endif + recv = udp->receiveFrom(remote, (char*)data, size); + if (recv > 0) { + addr->set_ip_address(remote.get_address()); + addr->set_port(remote.get_port()); + } + } + else { + tr_error("TCP recvfrom\n"); + return -1; + } + + return recv; +} + + +void TypeYDInterface::socket_attach(void *handle, void (*callback)(void *), void *data) +{ + struct managed_socket *socket = (struct managed_socket *)handle; + + //tr_info("socket_attach"); + socket->callback = callback; + socket->data = data; + if (socket->snic_socket != NULL) { + socket->snic_socket->socket_attach(callback, data); + } +} +
diff -r 000000000000 -r 35a2186cf186 TypeYDInterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TypeYDInterface.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,287 @@ +/* Murata Type-YD implementation of NetworkInterfaceAPI + * Copyright (c) 2017 Murata Manufacturing Co., LTD. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TYPEYD_INTERFACE_H +#define TYPEYD_INTERFACE_H + +#include "mbed.h" +#include "SNICInterface/SNIC_WifiInterface.h" + +/** TypeYDInterface class + * Implementation of the NetworkStack for the Type-YD + */ +class TypeYDInterface : public NetworkStack, public WiFiInterface +{ +public: + /** TypeYDInterface lifetime + * @param tx TX pin + * @param rx RX pin + * @param debug Enable debugging + */ + TypeYDInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200); + + /** Initialize TypeYD wifi module + * + * Attempts to start up Type YD module + * + * @return 0 on success, negative error code on failure + */ + int init(); + + /** Get firmware version + * + * Attempts to get Type-YD firmware version + * + * @param version Buffer stored firmware version + * @param length Length of version buffer + * @return 0 on success, negative error code on failure + */ + int getFWVersion(unsigned char *version, int length); + + /** Start the interface + * + * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. + * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. + * + * @return 0 on success, negative error code on failure + */ + virtual int connect(); + + /** Start the interface + * + * Attempts to connect to a WiFi network. + * + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) + * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED + * @return 0 on success, or error code on failure + */ + virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, + uint8_t channel = 0); + + /** Set the WiFi network credentials + * + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * (defaults to NSAPI_SECURITY_NONE) + * @return 0 on success, or error code on failure + */ + virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); + + /** Set the WiFi network channel - NOT SUPPORTED + * + * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED + * + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) + * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED + */ + virtual int set_channel(uint8_t channel); + + /** Stop the interface + * @return 0 on success, negative on failure + */ + virtual int disconnect(); + + /** Get the internally stored IP address + * @return IP address of the interface or null if not yet connected + */ + virtual const char *get_ip_address(); + + /** Get the internally stored MAC address + * @return MAC address of the interface + */ + virtual const char *get_mac_address(); + + /** Get the local gateway + * + * @return Null-terminated representation of the local gateway + * or null if no network mask has been recieved + */ + virtual const char *get_gateway(); + + /** Get the local network mask + * + * @return Null-terminated representation of the local network mask + * or null if no network mask has been recieved + */ + virtual const char *get_netmask(); + + /** Gets the current radio signal strength for active connection + * + * @return Connection strength in dBm (negative value) + */ + virtual int8_t get_rssi(); + + /** Scan for available networks + * + * This function will block. + * + * @param ap Pointer to allocated array to store discovered AP + * @param count Size of allocated @a res array, or 0 to only count available AP + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) + * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error + * see @a nsapi_error + */ + virtual int scan(WiFiAccessPoint *res, unsigned count); + + /** Translates a hostname to an IP address with specific version + * + * The hostname may be either a domain name or an IP address. If the + * hostname is an IP address, no network transactions will be performed. + * + * If no stack-specific DNS resolution is provided, the hostname + * will be resolve using a UDP socket on the stack. + * + * @param address Destination for the host SocketAddress + * @param host Hostname to resolve + * @param version IP version of address to resolve, NSAPI_UNSPEC indicates + * version is chosen by the stack (defaults to NSAPI_UNSPEC) + * @return 0 on success, negative error code on failure + */ + //using NetworkInterface::gethostbyname; + nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version); + + /** Add a domain name server to list of servers to query + * + * @param addr Destination for the host address + * @return 0 on success, negative error code on failure + */ + using NetworkInterface::add_dns_server; + +protected: + /** Open a socket + * @param handle Handle in which to store new socket + * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP + * @return 0 on success, negative on failure + */ + virtual int socket_open(void **handle, nsapi_protocol_t proto); + + /** Close the socket + * @param handle Socket handle + * @return 0 on success, negative on failure + * @note On failure, any memory associated with the socket must still + * be cleaned up + */ + virtual int socket_close(void *handle); + + /** Bind a server socket to a specific port + * @param handle Socket handle + * @param address Local address to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + virtual int socket_bind(void *handle, const SocketAddress &address); + + /** Start listening for incoming connections + * @param handle Socket handle + * @param backlog Number of pending connections that can be queued up at any + * one time [Default: 1] + * @return 0 on success, negative on failure + */ + virtual int socket_listen(void *handle, int backlog); + + /** Connects this TCP socket to the server + * @param handle Socket handle + * @param address SocketAddress to connect to + * @return 0 on success, negative on failure + */ + virtual int socket_connect(void *handle, const SocketAddress &address); + + /** Accept a new connection. + * @param handle Handle in which to store new socket + * @param server Socket handle to server to accept from + * @return 0 on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_accept(void *handle, void **client_socket, SocketAddress *address); + + /** Send data to the remote host + * @param handle Socket handle + * @param data The buffer to send to the host + * @param size The length of the buffer to send + * @return Number of written bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_send(void *handle, const void *data, unsigned size); + + /** Receive data from the remote host + * @param handle Socket handle + * @param data The buffer in which to store the data received from the host + * @param size The maximum length of the buffer + * @return Number of received bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_recv(void *handle, void *data, unsigned size); + + /** Send a packet to a remote endpoint + * @param handle Socket handle + * @param address The remote SocketAddress + * @param data The packet to be sent + * @param size The length of the packet to be sent + * @return The number of written bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); + + /** Receive a packet from a remote endpoint + * @param handle Socket handle + * @param address Destination for the remote SocketAddress or null + * @param buffer The buffer for storing the incoming packet data + * If a packet is too long to fit in the supplied buffer, + * excess bytes are discarded + * @param size The length of the buffer + * @return The number of received bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); + + /** Register a callback on state change of the socket + * @param handle Socket handle + * @param callback Function to call on state change + * @param data Argument to pass to callback + * @note Callback may be called in an interrupt context. + */ + virtual void socket_attach(void *handle, void (*callback)(void *), void *data); + + /** Provide access to the NetworkStack object + * + * @return The underlying NetworkStack object + */ + virtual NetworkStack *get_stack() + { + return this; + } + +private: + C_SNIC_WifiInterface _snic; + + char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ + E_SECURITY ap_esec; + uint8_t ap_ch; + char ap_pass[65]; /* The longest allowed passphrase */ + + char mac_address[6]; + char str_macaddr[3*6]; /* 00:00:00:00:00:00 */ +}; + +#endif +
diff -r 000000000000 -r 35a2186cf186 TypeYDPins.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TypeYDPins.h Wed Jul 12 10:49:10 2017 +0000 @@ -0,0 +1,45 @@ +/* Murata Type-YD implementation of NetworkInterfaceAPI + * Copyright (c) 2017 Murata Manufacturing Co., LTD. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TYPEYD_PINS_H +#define TYPEYD_PINS_H + +#include "mbed.h" + +#if !defined(MBED_CONF_APP_WIFI_TX) +#define MBED_CONF_APP_WIFI_TX PA_9 +#endif +#if !defined(MBED_CONF_APP_WIFI_RX) +#define MBED_CONF_APP_WIFI_RX PA_10 +#endif +#if !defined(MBED_CONF_APP_WIFI_CTS) +#define MBED_CONF_APP_WIFI_CTS NC // PA11 +#endif +#if !defined(MBED_CONF_APP_WIFI_RTS) +#define MBED_CONF_APP_WIFI_RTS NC // PA12 +#endif +#if !defined(MBED_CONF_APP_WIFI_RESET) +#define MBED_CONF_APP_WIFI_RESET PA_8 +#endif +#if !defined(MBED_CONF_APP_WIFI_ALARM) +#define MBED_CONF_APP_WIFI_ALARM NC +#endif +#if !defined(MBED_CONF_APP_WIFI_BAUD) +#define MBED_CONF_APP_WIFI_BAUD 115200 +#endif + +#endif +