Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:7251441ac366, committed 2015-02-10
- Comitter:
- komoritan
- Date:
- Tue Feb 10 12:22:17 2015 +0000
- Commit message:
- Customized
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/CBuffer.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,76 @@ +/* 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/MurataObject.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,22 @@ +/* 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 "MurataObject.h" +#include "stdarg.h" + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/MurataObject.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,48 @@ +/* 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(...) { pc.printf(__VA_ARGS__);} + +#ifdef _FUNC_TRACE +#define FUNC_IN() { pc.printf( "%s[%d]%s[tid:%x] IN\r\n", __FILE__, __LINE__, __FUNCTION__, Thread::gettid());} +#define FUNC_OUT() { pc.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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/SNIC_Core.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,378 @@ +/* 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 "SNIC_Core.h" +#include "SNIC_UartMsgUtil.h" +#include <string> + +/** Wait signal ID of UART recv */ +#define UART_DISPATCH_SIGNAL 0x00000002 + +#define UART_RECVBUF_SIZE 2048 +#define UART_THREAD_STACK_SIZE 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. +*/ +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("AHBSRAM1"))); +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"))); +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; + + 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(); + 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 ); + // 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 ); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/SNIC_Core.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,432 @@ +/* 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 "MurataObject.h" +#include "mbed.h" +#include "rtos.h" +#include "RawSerial.h" +#include "CBuffer.h" + +#include "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 + +/** 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 UDPSocket; +friend class Socket; + +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; + bool is_received; + bool is_receive_complete; + int parent_socket; + int from_ip; + short from_port; + bool is_accept; + Mutex mutex; + }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_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_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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/SNIC_UartCommandManager.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,282 @@ +/* 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 "SNIC_UartCommandManager.h" +#include "SNIC_Core.h" + +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.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 ) +{ + if( (payload_p == NULL) || (payload_len == 0) ) + { + return; + } + + C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); + + int socket_id; + unsigned short recv_len; + + // 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]; + + while( con_info_p->is_receive_complete == false ) + { + Thread::yield(); + } + +// 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.lock(); + con_info_p->is_receive_complete = false; + con_info_p->is_received = true; + con_info_p->mutex.unlock(); +// Thread::yield(); +} + +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; + + // 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 = payload_p[2]; +} + +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] ); + 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; + con_info_p->mutex.unlock(); + + // 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] ); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/SNIC_UartCommandManager.h Tue Feb 10 12:22:17 2015 +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. + */ +#ifndef _SNIC_UART_COMMAND_MANAGER_H_ +#define _SNIC_UART_COMMAND_MANAGER_H_ +#include "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 UDPSocket; +friend class Socket; + +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/SNIC_UartMsgUtil.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,149 @@ +/* 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 "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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC/SNIC_UartMsgUtil.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,185 @@ +/* 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 "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 UDPSocket; +friend class TCPSocketConnection; +friend class Socket; + +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_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC_WifiInterface.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,664 @@ +/* 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 "SNIC_WifiInterface.h" +#include "SNIC_UartMsgUtil.h" + +#define UART_CONNECT_BUF_SIZE 512 +unsigned char gCONNECT_BUF[UART_CONNECT_BUF_SIZE]; + +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; +} + +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 ) +{ + 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("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]; + memcpy( version_p, &payload_buf_p->buf[4], version_len ); + } + 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::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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNIC_WifiInterface.h Tue Feb 10 12:22:17 2015 +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. + */ +#ifndef _SNIC_WIFIINTERFACE_H_ +#define _SNIC_WIFIINTERFACE_H_ + +#include "SNIC_Core.h" +#include "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: + /** 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(); + + /** 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 ); + + /** 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(); + + /** 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 ); + +private: + PinName mUART_tx; + PinName mUART_rx; + PinName mUART_cts; + PinName mUART_rts; + int mUART_baud; + PinName mModuleReset; +}; +#endif /* _YD_WIFIINTERFACE_H_ */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,58 @@ +/* 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 "Socket.h" +#include "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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,66 @@ +/* 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 ENDPOINT_H +#define ENDPOINT_H + +class UDPSocket; + +/** +IP Endpoint (address, port) +*/ +class Endpoint { + friend class UDPSocket; + +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,242 @@ +/* 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 "Socket.h" +#include <cstring> + +char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1"))); + +Socket::Socket() +{ + mSocketID = -1; +} + +void Socket::set_blocking(bool blocking, unsigned int timeout) { + _blocking = blocking; + _timeout = timeout; +} + +Socket::~Socket() { +// close(); //Don't want to leak +} + +int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { + return 0; +} + +int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { + return 0; +} + +int Socket::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("socket 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( "socket close failed\r\n" ); + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return -1; + } + + if( uartCmdMgr_p->getCommandStatus() != 0 ) + { + DEBUG_PRINT("socket 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 Socket::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]; + snic_core_p->freeCmdBuf( payload_buf ); + FUNC_OUT(); + return 0; +} + +int Socket::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 *Socket::getSocketSendBuf() +{ + return gSOCKET_SEND_BUF; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,90 @@ +/* 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 SOCKET_H_ +#define SOCKET_H_ + +#include "SNIC_Core.h" +#include "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 Socket { +public: + /** Socket + */ + Socket(); + + /** 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 ~Socket(); + + int createSocket( unsigned char bind = 0, unsigned int local_addr = 0, unsigned short port = 0 ); + +protected: + int resolveHostName( const char *host_p ); + +protected: + int mSocketID; + bool _blocking; + int _timeout; + char *getSocketSendBuf(); + +private: + +// int select(struct timeval *timeout, bool read, bool write); +}; + +#endif /* SOCKET_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,273 @@ +/* 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 "TCPSocketConnection.h" +#include <cstring> + +TCPSocketConnection::TCPSocketConnection() +{ +} + +TCPSocketConnection::~TCPSocketConnection() +{ +} + +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 + 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 ); + + // 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; + 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\r\n" ); + 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) +{ + int i = 0; + + 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; + } + con_info_p->is_receive_complete = true; + if( con_info_p->is_received == false ) + { + // Try receive + Thread::yield(); + + if( con_info_p->is_received == false ) + { + // No data received. + FUNC_OUT(); + return 0; + } + } + // Get packet data from buffer for receive. + for (i = 0; i < length; i ++) + { + if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) + { + break; + } + } + + if( con_info_p->recvbuf_p->isEmpty() ) + { + con_info_p->mutex.lock(); + con_info_p->is_received = false; + 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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,84 @@ +/* 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 TCPSOCKET_H +#define TCPSOCKET_H + +#include "Socket.h" +#include "Endpoint.h" + +/** + Interface class for TCP socket of using SNIC UART. +*/ +class TCPSocketConnection : public Socket, public Endpoint { + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + virtual ~TCPSocketConnection(); + + /** 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,181 @@ +/* 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 "TCPSocketServer.h" +#include "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) +{ + C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); + int i; + int ret = -1; + + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p; + 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; + } + } + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.h Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,58 @@ +/* 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 TCPSOCKETSERVER_H +#define TCPSOCKETSERVER_H + +#include "Socket.h" +#include "TCPSocketConnection.h" + +/** + Interface class for TCP server socket of using SNIC UART. + +*/ +class TCPSocketServer : public Socket { + 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); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.cpp Tue Feb 10 12:22:17 2015 +0000 @@ -0,0 +1,335 @@ +/* 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 "Socket/UDPSocket.h" +#include <cstring> + +UDPSocket::UDPSocket() { +} + +UDPSocket::~UDPSocket() +{ +} + +int UDPSocket::init(void) +{ + return 0; +} + +// Server initialization +int UDPSocket::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->is_received = false; + + 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 UDPSocket::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(); + + osThreadId tid = Thread::gettid(); + +// 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 = 1; + + // 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 + wait(0.05); + + return length; +// return 0; +} + +// -1 if unsuccessful, else number of bytes received +int UDPSocket::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; + } + + 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 ); + + con_info_p->mutex.lock(); + con_info_p->is_receive_complete = true; + con_info_p->mutex.unlock(); + if( con_info_p->is_received == false ) + { + // Try receive + Thread::yield(); + + if( con_info_p->is_received == false ) + { + // No data received. + FUNC_OUT(); + return 0; + } + } + // Get packet data from buffer for receive. + int i; + for (i = 0; i < length; i ++) + { + if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) + { + break; + } + } + if( con_info_p->recvbuf_p->isEmpty() ) + { + con_info_p->mutex.lock(); + con_info_p->is_received = false; + con_info_p->mutex.unlock(); + } + FUNC_OUT(); + return i; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.h Tue Feb 10 12:22:17 2015 +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 UDPSOCKET_H +#define UDPSOCKET_H + +#include "Socket.h" +#include "Endpoint.h" + +/** +Interface class for UDP socket of using SNIC UART. +*/ +class UDPSocket : public Socket { + +public: + /** Instantiate an UDP Socket. + */ + UDPSocket(); + virtual ~UDPSocket(); + + /** 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