SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Fri May 30 08:30:40 2014 +0000
Revision:
32:ae95309643aa
Parent:
30:944b8c04b5ff
Child:
34:8c3527b8f44e
Implemented  a API of TCP server.

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