SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
lesmin
Date:
Thu Aug 13 06:44:01 2015 +0000
Revision:
54:cb4c105a2ad2
Parent:
48:ee7e07fb401f
retry codes are added by STKorea

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 "TCPSocketConnection.h"
kishino 20:dd736d328de6 22 #include <cstring>
kishino 20:dd736d328de6 23
kishino 20:dd736d328de6 24 TCPSocketConnection::TCPSocketConnection()
kishino 20:dd736d328de6 25 {
kishino 20:dd736d328de6 26 }
kishino 20:dd736d328de6 27
kishino 26:f2e1030964e4 28 TCPSocketConnection::~TCPSocketConnection()
kishino 26:f2e1030964e4 29 {
kishino 26:f2e1030964e4 30 }
kishino 26:f2e1030964e4 31
kishino 29:6a0ba999597d 32 int TCPSocketConnection::connect( const char *host_p, unsigned short port)
kishino 20:dd736d328de6 33 {
kishino 20:dd736d328de6 34 int ret;
kishino 24:987e412ae879 35 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 36 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 29:6a0ba999597d 37
lesmin 54:cb4c105a2ad2 38 int retry_cnt=5;
lesmin 54:cb4c105a2ad2 39
kishino 43:d80bbb12ffe6 40 FUNC_IN();
lesmin 54:cb4c105a2ad2 41 conn_again:
kishino 20:dd736d328de6 42 // Socket create
kishino 20:dd736d328de6 43 ret = createSocket();
kishino 20:dd736d328de6 44 if( ret != 0 )
kishino 20:dd736d328de6 45 {
kishino 40:b6b10c22a121 46 DEBUG_PRINT("createSocket error : %d\r\n", ret);
kishino 43:d80bbb12ffe6 47 FUNC_OUT();
kishino 20:dd736d328de6 48 return -1;
kishino 20:dd736d328de6 49 }
kishino 20:dd736d328de6 50
kishino 29:6a0ba999597d 51 int ip_addr = resolveHostName( host_p );
kishino 30:944b8c04b5ff 52 //lcd_printf("connect to [%s](%08x)\r\n", host_p, ip_addr);
kishino 29:6a0ba999597d 53 if( ( ip_addr == 0) || (ip_addr == -1) )
kishino 29:6a0ba999597d 54 {
kishino 43:d80bbb12ffe6 55 DEBUG_PRINT("connect resolveHostName failed\r\n");
kishino 43:d80bbb12ffe6 56 FUNC_OUT();
kishino 29:6a0ba999597d 57 return -1;
kishino 29:6a0ba999597d 58 }
kishino 29:6a0ba999597d 59
kishino 20:dd736d328de6 60 // Get buffer for response payload from MemoryPool
kishino 38:f13e4e563d65 61 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
kishino 38:f13e4e563d65 62 if( payload_buf_p == NULL )
kishino 20:dd736d328de6 63 {
kishino 40:b6b10c22a121 64 DEBUG_PRINT("connect payload_buf_p NULL\r\n");
kishino 43:d80bbb12ffe6 65 FUNC_OUT();
kishino 20:dd736d328de6 66 return -1;
kishino 20:dd736d328de6 67 }
kishino 29:6a0ba999597d 68
kishino 27:dcc4f34448f0 69 // IP address convert to number from strings.
kishino 29:6a0ba999597d 70 // unsigned int ip_addr = addrToInteger(ip_addr_p);
kishino 27:dcc4f34448f0 71
kishino 29:6a0ba999597d 72 //
kishino 22:a9ec0cad4f84 73 C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req;
kishino 20:dd736d328de6 74 // Make request
kishino 20:dd736d328de6 75 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ;
kishino 20:dd736d328de6 76 req.seq = mUartRequestSeq++;
kishino 20:dd736d328de6 77 req.socket_id = mSocketID;
kishino 20:dd736d328de6 78
kishino 20:dd736d328de6 79 // set ip addr ( byte order )
kishino 34:8c3527b8f44e 80 C_SNIC_UartMsgUtil::convertIntToByteAdday( ip_addr, (char *)req.remote_addr );
kishino 20:dd736d328de6 81 req.remote_port[0] = ( (port & 0xFF00) >> 8 );
kishino 20:dd736d328de6 82 req.remote_port[1] = (port & 0xFF);
kishino 20:dd736d328de6 83 req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
kishino 20:dd736d328de6 84 req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
kishino 20:dd736d328de6 85 req.timeout = 60;
kishino 20:dd736d328de6 86
kishino 38:f13e4e563d65 87 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 20:dd736d328de6 88 unsigned int command_len;
kishino 20:dd736d328de6 89 // Preparation of command
kishino 24:987e412ae879 90 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 38:f13e4e563d65 91 , sizeof(C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf_p->buf, command_array_p );
kishino 20:dd736d328de6 92
kishino 29:6a0ba999597d 93 uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND );
kishino 29:6a0ba999597d 94
kishino 20:dd736d328de6 95 // Send uart command request
kishino 38:f13e4e563d65 96 snic_core_p->sendUart( command_len, command_array_p );
kishino 20:dd736d328de6 97
kishino 20:dd736d328de6 98 // Wait UART response
kishino 20:dd736d328de6 99 ret = uartCmdMgr_p->wait();
kishino 20:dd736d328de6 100 if( ret != 0 )
kishino 20:dd736d328de6 101 {
kishino 40:b6b10c22a121 102 DEBUG_PRINT( "connect failed\r\n" );
kishino 38:f13e4e563d65 103 snic_core_p->freeCmdBuf( payload_buf_p );
lesmin 54:cb4c105a2ad2 104
lesmin 54:cb4c105a2ad2 105 retry_cnt--;
lesmin 54:cb4c105a2ad2 106 printf("retry_wait(%d)\r\n", retry_cnt);
lesmin 54:cb4c105a2ad2 107 if(retry_cnt>0){
lesmin 54:cb4c105a2ad2 108 close();
lesmin 54:cb4c105a2ad2 109 goto conn_again;
lesmin 54:cb4c105a2ad2 110 }
lesmin 54:cb4c105a2ad2 111
kishino 43:d80bbb12ffe6 112 FUNC_OUT();
kishino 20:dd736d328de6 113 return -1;
kishino 20:dd736d328de6 114 }
kishino 20:dd736d328de6 115
kishino 20:dd736d328de6 116 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP )
kishino 20:dd736d328de6 117 {
kishino 40:b6b10c22a121 118 DEBUG_PRINT("connect status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 38:f13e4e563d65 119 snic_core_p->freeCmdBuf( payload_buf_p );
lesmin 54:cb4c105a2ad2 120
lesmin 54:cb4c105a2ad2 121 retry_cnt--;
lesmin 54:cb4c105a2ad2 122 printf("retry_status(%d)\r\n", retry_cnt);
lesmin 54:cb4c105a2ad2 123 if(retry_cnt>0){
lesmin 54:cb4c105a2ad2 124 close();
lesmin 54:cb4c105a2ad2 125 goto conn_again;
lesmin 54:cb4c105a2ad2 126 }
lesmin 54:cb4c105a2ad2 127
kishino 43:d80bbb12ffe6 128 FUNC_OUT();
kishino 20:dd736d328de6 129 return -1;
kishino 20:dd736d328de6 130 }
kishino 29:6a0ba999597d 131
kishino 38:f13e4e563d65 132 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 20:dd736d328de6 133
kishino 20:dd736d328de6 134 // Initialize connection information
kishino 24:987e412ae879 135 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
kishino 20:dd736d328de6 136 if( con_info_p->recvbuf_p == NULL )
kishino 20:dd736d328de6 137 {
kishino 40:b6b10c22a121 138 DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID);
kishino 29:6a0ba999597d 139 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
kishino 20:dd736d328de6 140 }
kishino 20:dd736d328de6 141 con_info_p->is_connected = true;
kishino 20:dd736d328de6 142 con_info_p->is_received = false;
kishino 43:d80bbb12ffe6 143 FUNC_OUT();
kishino 20:dd736d328de6 144 return 0;
kishino 20:dd736d328de6 145 }
kishino 20:dd736d328de6 146
kishino 20:dd736d328de6 147 bool TCPSocketConnection::is_connected(void)
kishino 20:dd736d328de6 148 {
kishino 24:987e412ae879 149 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 150 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
kishino 20:dd736d328de6 151 return con_info_p->is_connected;
kishino 20:dd736d328de6 152 }
kishino 20:dd736d328de6 153
kishino 29:6a0ba999597d 154 int TCPSocketConnection::send(char* data_p, int length)
kishino 20:dd736d328de6 155 {
kishino 24:987e412ae879 156 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 157 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 20:dd736d328de6 158
kishino 43:d80bbb12ffe6 159 FUNC_IN();
kishino 20:dd736d328de6 160 // Get buffer for response payload from MemoryPool
kishino 38:f13e4e563d65 161 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
kishino 38:f13e4e563d65 162 if( payload_buf_p == NULL )
kishino 20:dd736d328de6 163 {
kishino 40:b6b10c22a121 164 DEBUG_PRINT("connect payload_buf_p NULL\r\n");
kishino 43:d80bbb12ffe6 165 FUNC_OUT();
kishino 20:dd736d328de6 166 return -1;
kishino 20:dd736d328de6 167 }
kishino 29:6a0ba999597d 168
kishino 22:a9ec0cad4f84 169 C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T req;
kishino 20:dd736d328de6 170 // Make request
kishino 20:dd736d328de6 171 req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ;
kishino 20:dd736d328de6 172 req.seq = mUartRequestSeq++;
kishino 20:dd736d328de6 173 req.socket_id = mSocketID;
kishino 20:dd736d328de6 174 req.option = 0;
kishino 20:dd736d328de6 175 req.payload_len[0]= ( (length & 0xFF00) >> 8 );
kishino 20:dd736d328de6 176 req.payload_len[1]= (length & 0xFF);
kishino 20:dd736d328de6 177
kishino 34:8c3527b8f44e 178 int req_size = sizeof(C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T);
kishino 34:8c3527b8f44e 179 char *send_buf_p = getSocketSendBuf();
kishino 34:8c3527b8f44e 180 memcpy( send_buf_p, &req, req_size );
kishino 34:8c3527b8f44e 181 memcpy( &send_buf_p[req_size], data_p, length );
kishino 20:dd736d328de6 182
kishino 38:f13e4e563d65 183 unsigned char *command_array_p = snic_core_p->getCommandBuf();
kishino 29:6a0ba999597d 184 unsigned int command_len;
kishino 20:dd736d328de6 185 // Preparation of command
kishino 34:8c3527b8f44e 186 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)send_buf_p
kishino 38:f13e4e563d65 187 , req_size + length, payload_buf_p->buf, command_array_p );
kishino 20:dd736d328de6 188
kishino 20:dd736d328de6 189 // Send uart command request
kishino 38:f13e4e563d65 190 snic_core_p->sendUart( command_len, command_array_p );
kishino 20:dd736d328de6 191
kishino 20:dd736d328de6 192 // Wait UART response
kishino 20:dd736d328de6 193 int ret = uartCmdMgr_p->wait();
kishino 20:dd736d328de6 194 if( ret != 0 )
kishino 20:dd736d328de6 195 {
kishino 40:b6b10c22a121 196 DEBUG_PRINT( "send failed\r\n" );
kishino 38:f13e4e563d65 197 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 43:d80bbb12ffe6 198 FUNC_OUT();
kishino 20:dd736d328de6 199 return -1;
kishino 20:dd736d328de6 200 }
kishino 20:dd736d328de6 201
kishino 20:dd736d328de6 202 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
kishino 20:dd736d328de6 203 {
kishino 40:b6b10c22a121 204 DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 38:f13e4e563d65 205 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 43:d80bbb12ffe6 206 FUNC_OUT();
kishino 20:dd736d328de6 207 return -1;
kishino 20:dd736d328de6 208 }
kishino 38:f13e4e563d65 209 snic_core_p->freeCmdBuf( payload_buf_p );
kishino 20:dd736d328de6 210
kishino 20:dd736d328de6 211 // SNIC_SEND_FROM_SOCKET_REQ
kishino 43:d80bbb12ffe6 212 FUNC_OUT();
MACRUM 48:ee7e07fb401f 213 return length;
kishino 20:dd736d328de6 214 }
kishino 20:dd736d328de6 215
kishino 29:6a0ba999597d 216 int TCPSocketConnection::send_all(char *data_p, int length)
kishino 29:6a0ba999597d 217 {
kishino 29:6a0ba999597d 218 return send( data_p, length );
kishino 29:6a0ba999597d 219 }
kishino 29:6a0ba999597d 220
kishino 29:6a0ba999597d 221 int TCPSocketConnection::receive(char* data_p, int length)
kishino 20:dd736d328de6 222 {
kishino 20:dd736d328de6 223 int i = 0;
kishino 20:dd736d328de6 224
kishino 43:d80bbb12ffe6 225 FUNC_IN();
kishino 20:dd736d328de6 226 if( (data_p == NULL) || (length < 1) )
kishino 20:dd736d328de6 227 {
kishino 40:b6b10c22a121 228 DEBUG_PRINT("TCPSocketConnection::receive parameter error\r\n");
kishino 43:d80bbb12ffe6 229 FUNC_OUT();
kishino 20:dd736d328de6 230 return -1;
kishino 20:dd736d328de6 231 }
kishino 20:dd736d328de6 232
kishino 24:987e412ae879 233 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 20:dd736d328de6 234 // Initialize connection information
kishino 24:987e412ae879 235 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
kishino 20:dd736d328de6 236 if( con_info_p->recvbuf_p == NULL )
kishino 20:dd736d328de6 237 {
kishino 40:b6b10c22a121 238 DEBUG_PRINT("TCPSocketConnection::receive Conncection info error\r\n");
kishino 43:d80bbb12ffe6 239 FUNC_OUT();
kishino 20:dd736d328de6 240 return -1;
kishino 20:dd736d328de6 241 }
kishino 20:dd736d328de6 242
kishino 20:dd736d328de6 243 // Check connection
kishino 20:dd736d328de6 244 if( con_info_p->is_connected == false )
kishino 20:dd736d328de6 245 {
kishino 40:b6b10c22a121 246 DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", mSocketID);
kishino 43:d80bbb12ffe6 247 FUNC_OUT();
kishino 20:dd736d328de6 248 return -1;
kishino 20:dd736d328de6 249 }
kishino 43:d80bbb12ffe6 250 con_info_p->is_receive_complete = true;
MACRUM 48:ee7e07fb401f 251 if( con_info_p->is_received == false )
kishino 20:dd736d328de6 252 {
MACRUM 48:ee7e07fb401f 253 // Try receive
kishino 43:d80bbb12ffe6 254 Thread::yield();
MACRUM 48:ee7e07fb401f 255
MACRUM 48:ee7e07fb401f 256 if( con_info_p->is_received == false )
MACRUM 48:ee7e07fb401f 257 {
MACRUM 48:ee7e07fb401f 258 // No data received.
MACRUM 48:ee7e07fb401f 259 FUNC_OUT();
MACRUM 48:ee7e07fb401f 260 return 0;
MACRUM 48:ee7e07fb401f 261 }
kishino 20:dd736d328de6 262 }
kishino 20:dd736d328de6 263 // Get packet data from buffer for receive.
kishino 20:dd736d328de6 264 for (i = 0; i < length; i ++)
kishino 20:dd736d328de6 265 {
kishino 20:dd736d328de6 266 if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
kishino 20:dd736d328de6 267 {
kishino 20:dd736d328de6 268 break;
kishino 20:dd736d328de6 269 }
kishino 20:dd736d328de6 270 }
kishino 20:dd736d328de6 271
kishino 20:dd736d328de6 272 if( con_info_p->recvbuf_p->isEmpty() )
kishino 20:dd736d328de6 273 {
kishino 39:a1233ca02edf 274 con_info_p->mutex.lock();
kishino 20:dd736d328de6 275 con_info_p->is_received = false;
kishino 39:a1233ca02edf 276 con_info_p->mutex.unlock();
kishino 20:dd736d328de6 277 }
kishino 20:dd736d328de6 278
kishino 43:d80bbb12ffe6 279 FUNC_OUT();
kishino 20:dd736d328de6 280 return i;
kishino 20:dd736d328de6 281 }
kishino 32:ae95309643aa 282
MACRUM 48:ee7e07fb401f 283 int TCPSocketConnection::receive_all(char* data_p, int length)
MACRUM 48:ee7e07fb401f 284 {
MACRUM 48:ee7e07fb401f 285 return receive( data_p, length );
MACRUM 48:ee7e07fb401f 286 }
MACRUM 48:ee7e07fb401f 287
kishino 32:ae95309643aa 288 void TCPSocketConnection::setAcceptSocket( int socket_id )
kishino 32:ae95309643aa 289 {
kishino 43:d80bbb12ffe6 290 FUNC_IN();
kishino 32:ae95309643aa 291 mSocketID = socket_id;
kishino 32:ae95309643aa 292 }