SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Fri Nov 14 00:52:31 2014 +0000
Revision:
43:d80bbb12ffe6
Parent:
41:1c1b5ad4d491
It was reviewed and modified the Receive logic of socket communication(TCP and UDP).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 20:dd736d328de6 1 /* Copyright (C) 2012 mbed.org, MIT License
kishino 20:dd736d328de6 2 *
kishino 20:dd736d328de6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kishino 20:dd736d328de6 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kishino 20:dd736d328de6 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kishino 20:dd736d328de6 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kishino 20:dd736d328de6 7 * furnished to do so, subject to the following conditions:
kishino 20:dd736d328de6 8 *
kishino 20:dd736d328de6 9 * The above copyright notice and this permission notice shall be included in all copies or
kishino 20:dd736d328de6 10 * substantial portions of the Software.
kishino 20:dd736d328de6 11 *
kishino 20:dd736d328de6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kishino 20:dd736d328de6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kishino 20:dd736d328de6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kishino 20:dd736d328de6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kishino 20:dd736d328de6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kishino 20:dd736d328de6 17 */
kishino 39:a1233ca02edf 18 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
kishino 41:1c1b5ad4d491 19 * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
kishino 39:a1233ca02edf 20 */
kishino 20:dd736d328de6 21 #include "Socket.h"
kishino 20:dd736d328de6 22 #include <cstring>
kishino 20:dd736d328de6 23
kishino 33:33f1bc919486 24 char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1")));
kishino 36:f33fcf5975ab 25
kishino 20:dd736d328de6 26 Socket::Socket()
kishino 20:dd736d328de6 27 {
kishino 20:dd736d328de6 28 mSocketID = -1;
kishino 20:dd736d328de6 29 }
kishino 20:dd736d328de6 30
kishino 29:6a0ba999597d 31 void Socket::set_blocking(bool blocking, unsigned int timeout) {
kishino 29:6a0ba999597d 32 _blocking = blocking;
kishino 29:6a0ba999597d 33 _timeout = timeout;
kishino 29:6a0ba999597d 34 }
kishino 29:6a0ba999597d 35
kishino 20:dd736d328de6 36 Socket::~Socket() {
kishino 20:dd736d328de6 37 // close(); //Don't want to leak
kishino 20:dd736d328de6 38 }
kishino 20:dd736d328de6 39
kishino 20:dd736d328de6 40 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
kishino 20:dd736d328de6 41 return 0;
kishino 20:dd736d328de6 42 }
kishino 20:dd736d328de6 43
kishino 20:dd736d328de6 44 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
kishino 20:dd736d328de6 45 return 0;
kishino 20:dd736d328de6 46 }
kishino 20:dd736d328de6 47
kishino 20:dd736d328de6 48 int Socket::close(bool shutdown)
kishino 20:dd736d328de6 49 {
kishino 27:dcc4f34448f0 50 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 27:dcc4f34448f0 51 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 43:d80bbb12ffe6 52
kishino 43:d80bbb12ffe6 53 FUNC_IN();
kishino 27:dcc4f34448f0 54 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 55 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 27:dcc4f34448f0 56 if( payload_buf == NULL )
kishino 27:dcc4f34448f0 57 {
kishino 40:b6b10c22a121 58 DEBUG_PRINT("socket close payload_buf NULL\r\n");
kishino 43:d80bbb12ffe6 59 FUNC_OUT();
kishino 27:dcc4f34448f0 60 return -1;
kishino 27:dcc4f34448f0 61 }
kishino 27:dcc4f34448f0 62
kishino 27:dcc4f34448f0 63 C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req;
kishino 20:dd736d328de6 64
kishino 27:dcc4f34448f0 65 // Make request
kishino 27:dcc4f34448f0 66 req.cmd_sid = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ;
kishino 27:dcc4f34448f0 67 req.seq = mUartRequestSeq++;
kishino 27:dcc4f34448f0 68 req.socket_id = mSocketID;
kishino 27:dcc4f34448f0 69
kishino 38:f13e4e563d65 70 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 27:dcc4f34448f0 71 unsigned int command_len;
kishino 27:dcc4f34448f0 72 // Preparation of command
kishino 27:dcc4f34448f0 73 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 38:f13e4e563d65 74 , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array_p );
kishino 27:dcc4f34448f0 75
kishino 27:dcc4f34448f0 76 // Send uart command request
kishino 38:f13e4e563d65 77 snic_core_p->sendUart( command_len, command_array_p );
kishino 27:dcc4f34448f0 78
kishino 27:dcc4f34448f0 79 int ret;
kishino 27:dcc4f34448f0 80 // Wait UART response
kishino 27:dcc4f34448f0 81 ret = uartCmdMgr_p->wait();
kishino 27:dcc4f34448f0 82 if( ret != 0 )
kishino 27:dcc4f34448f0 83 {
kishino 40:b6b10c22a121 84 DEBUG_PRINT( "socket close failed\r\n" );
kishino 27:dcc4f34448f0 85 snic_core_p->freeCmdBuf( payload_buf );
kishino 43:d80bbb12ffe6 86 FUNC_OUT();
kishino 27:dcc4f34448f0 87 return -1;
kishino 27:dcc4f34448f0 88 }
kishino 27:dcc4f34448f0 89
kishino 27:dcc4f34448f0 90 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 27:dcc4f34448f0 91 {
kishino 40:b6b10c22a121 92 DEBUG_PRINT("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 27:dcc4f34448f0 93 snic_core_p->freeCmdBuf( payload_buf );
kishino 43:d80bbb12ffe6 94 FUNC_OUT();
kishino 27:dcc4f34448f0 95 return -1;
kishino 27:dcc4f34448f0 96 }
kishino 27:dcc4f34448f0 97 snic_core_p->freeCmdBuf( payload_buf );
kishino 43:d80bbb12ffe6 98 FUNC_OUT();
kishino 20:dd736d328de6 99 return 0;
kishino 20:dd736d328de6 100 }
kishino 20:dd736d328de6 101
kishino 20:dd736d328de6 102 int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port )
kishino 20:dd736d328de6 103 {
kishino 24:987e412ae879 104 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 105 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 43:d80bbb12ffe6 106
kishino 43:d80bbb12ffe6 107 FUNC_IN();
kishino 20:dd736d328de6 108 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 109 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 20:dd736d328de6 110 if( payload_buf == NULL )
kishino 20:dd736d328de6 111 {
kishino 40:b6b10c22a121 112 DEBUG_PRINT("createSocket payload_buf NULL\r\n");
kishino 43:d80bbb12ffe6 113 FUNC_OUT();
kishino 20:dd736d328de6 114 return -1;
kishino 20:dd736d328de6 115 }
kishino 20:dd736d328de6 116
kishino 22:a9ec0cad4f84 117 C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req;
kishino 20:dd736d328de6 118 int req_len = 0;
kishino 20:dd736d328de6 119
kishino 20:dd736d328de6 120 // Make request
kishino 20:dd736d328de6 121 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ;
kishino 20:dd736d328de6 122 req_len++;
kishino 20:dd736d328de6 123 req.seq = mUartRequestSeq++;
kishino 20:dd736d328de6 124 req_len++;
kishino 20:dd736d328de6 125 req.bind = bind;
kishino 20:dd736d328de6 126 req_len++;
kishino 20:dd736d328de6 127 if( bind != 0 )
kishino 20:dd736d328de6 128 {
kishino 32:ae95309643aa 129 // set ip addr ( byte order )
kishino 34:8c3527b8f44e 130 C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)req.local_addr );
kishino 32:ae95309643aa 131 req.local_port[0] = ( (port & 0xFF00) >> 8 );
kishino 32:ae95309643aa 132 req.local_port[1] = (port & 0xFF);
kishino 32:ae95309643aa 133
kishino 32:ae95309643aa 134 req_len = sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T);
kishino 20:dd736d328de6 135 }
kishino 20:dd736d328de6 136
kishino 38:f13e4e563d65 137 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 20:dd736d328de6 138 unsigned int command_len;
kishino 20:dd736d328de6 139 // Preparation of command
kishino 24:987e412ae879 140 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 38:f13e4e563d65 141 , req_len, payload_buf->buf, command_array_p );
kishino 20:dd736d328de6 142 // Send uart command request
kishino 38:f13e4e563d65 143 snic_core_p->sendUart( command_len, command_array_p );
kishino 20:dd736d328de6 144
kishino 20:dd736d328de6 145 int ret;
kishino 20:dd736d328de6 146 // Wait UART response
kishino 20:dd736d328de6 147 ret = uartCmdMgr_p->wait();
kishino 20:dd736d328de6 148 if( ret != 0 )
kishino 20:dd736d328de6 149 {
kishino 40:b6b10c22a121 150 DEBUG_PRINT( "createSocket failed\r\n" );
kishino 24:987e412ae879 151 snic_core_p->freeCmdBuf( payload_buf );
kishino 43:d80bbb12ffe6 152 FUNC_OUT();
kishino 20:dd736d328de6 153 return -1;
kishino 20:dd736d328de6 154 }
kishino 29:6a0ba999597d 155
kishino 20:dd736d328de6 156 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 20:dd736d328de6 157 {
kishino 40:b6b10c22a121 158 DEBUG_PRINT("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 24:987e412ae879 159 snic_core_p->freeCmdBuf( payload_buf );
kishino 43:d80bbb12ffe6 160 FUNC_OUT();
kishino 20:dd736d328de6 161 return -1;
kishino 20:dd736d328de6 162 }
kishino 20:dd736d328de6 163 mSocketID = payload_buf->buf[3];
kishino 24:987e412ae879 164 snic_core_p->freeCmdBuf( payload_buf );
kishino 43:d80bbb12ffe6 165 FUNC_OUT();
kishino 20:dd736d328de6 166 return 0;
kishino 20:dd736d328de6 167 }
kishino 27:dcc4f34448f0 168
kishino 29:6a0ba999597d 169 int Socket::resolveHostName( const char *host_p )
kishino 29:6a0ba999597d 170 {
kishino 29:6a0ba999597d 171 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 29:6a0ba999597d 172 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 29:6a0ba999597d 173 int ip_addr = 0;
kishino 29:6a0ba999597d 174
kishino 29:6a0ba999597d 175 if( host_p == NULL )
kishino 29:6a0ba999597d 176 {
kishino 40:b6b10c22a121 177 DEBUG_PRINT("resolveHostName parameter error\r\n");
kishino 29:6a0ba999597d 178 return -1;
kishino 29:6a0ba999597d 179 }
kishino 29:6a0ba999597d 180
kishino 29:6a0ba999597d 181 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 182 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 29:6a0ba999597d 183 if( payload_buf == NULL )
kishino 29:6a0ba999597d 184 {
kishino 40:b6b10c22a121 185 DEBUG_PRINT("resolveHostName payload_buf NULL\r\n");
kishino 29:6a0ba999597d 186 return -1;
kishino 29:6a0ba999597d 187 }
kishino 36:f33fcf5975ab 188
kishino 38:f13e4e563d65 189 unsigned char *buf_p = (unsigned char *)getSocketSendBuf();
kishino 29:6a0ba999597d 190 unsigned int buf_len = 0;
kishino 29:6a0ba999597d 191
kishino 38:f13e4e563d65 192 memset( buf_p, 0, UART_REQUEST_PAYLOAD_MAX );
kishino 29:6a0ba999597d 193 // Make request
kishino 38:f13e4e563d65 194 buf_p[0] = UART_CMD_SID_SNIC_RESOLVE_NAME_REQ;
kishino 29:6a0ba999597d 195 buf_len++;
kishino 38:f13e4e563d65 196 buf_p[1] = mUartRequestSeq++;
kishino 29:6a0ba999597d 197 buf_len++;
kishino 29:6a0ba999597d 198 // Interface
kishino 38:f13e4e563d65 199 buf_p[2] = 0;
kishino 29:6a0ba999597d 200 buf_len++;
kishino 29:6a0ba999597d 201
kishino 29:6a0ba999597d 202 // Host name length
kishino 29:6a0ba999597d 203 int hostname_len = strlen(host_p);
kishino 38:f13e4e563d65 204 buf_p[3] = (unsigned char)hostname_len;
kishino 29:6a0ba999597d 205 buf_len++;
kishino 38:f13e4e563d65 206 memcpy( &buf_p[4], host_p, hostname_len );
kishino 29:6a0ba999597d 207 buf_len += hostname_len;
kishino 29:6a0ba999597d 208
kishino 38:f13e4e563d65 209 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 29:6a0ba999597d 210 unsigned int command_len;
kishino 38:f13e4e563d65 211 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf_p
kishino 38:f13e4e563d65 212 , buf_len, payload_buf->buf, command_array_p );
kishino 29:6a0ba999597d 213 // Send uart command request
kishino 38:f13e4e563d65 214 snic_core_p->sendUart( command_len, command_array_p );
kishino 29:6a0ba999597d 215
kishino 29:6a0ba999597d 216 int ret;
kishino 29:6a0ba999597d 217 // Wait UART response
kishino 29:6a0ba999597d 218 ret = uartCmdMgr_p->wait();
kishino 29:6a0ba999597d 219 if( ret != 0 )
kishino 29:6a0ba999597d 220 {
kishino 40:b6b10c22a121 221 DEBUG_PRINT( "resolveHostName failed\r\n" );
kishino 29:6a0ba999597d 222 snic_core_p->freeCmdBuf( payload_buf );
kishino 29:6a0ba999597d 223 return -1;
kishino 29:6a0ba999597d 224 }
kishino 29:6a0ba999597d 225
kishino 29:6a0ba999597d 226 // check status
kishino 29:6a0ba999597d 227 if( uartCmdMgr_p->getCommandStatus() == 0 )
kishino 29:6a0ba999597d 228 {
kishino 29:6a0ba999597d 229 ip_addr = ((payload_buf->buf[3] << 24) & 0xFF000000)
kishino 29:6a0ba999597d 230 | ((payload_buf->buf[4] << 16) & 0xFF0000)
kishino 29:6a0ba999597d 231 | ((payload_buf->buf[5] << 8) & 0xFF00)
kishino 29:6a0ba999597d 232 | (payload_buf->buf[6]);
kishino 29:6a0ba999597d 233 }
kishino 29:6a0ba999597d 234
kishino 29:6a0ba999597d 235 snic_core_p->freeCmdBuf( payload_buf );
kishino 29:6a0ba999597d 236 return ip_addr;
kishino 29:6a0ba999597d 237 }
kishino 33:33f1bc919486 238
kishino 33:33f1bc919486 239 char *Socket::getSocketSendBuf()
kishino 33:33f1bc919486 240 {
kishino 33:33f1bc919486 241 return gSOCKET_SEND_BUF;
kishino 33:33f1bc919486 242 }