KDDI Fx0 hackathon / SNICInterface_Tweet
Committer:
komoritan
Date:
Tue Feb 10 12:26:31 2015 +0000
Revision:
0:edaa24c1f5cd
Debug Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 0:edaa24c1f5cd 1 /* Copyright (C) 2012 mbed.org, MIT License
komoritan 0:edaa24c1f5cd 2 *
komoritan 0:edaa24c1f5cd 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
komoritan 0:edaa24c1f5cd 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
komoritan 0:edaa24c1f5cd 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
komoritan 0:edaa24c1f5cd 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
komoritan 0:edaa24c1f5cd 7 * furnished to do so, subject to the following conditions:
komoritan 0:edaa24c1f5cd 8 *
komoritan 0:edaa24c1f5cd 9 * The above copyright notice and this permission notice shall be included in all copies or
komoritan 0:edaa24c1f5cd 10 * substantial portions of the Software.
komoritan 0:edaa24c1f5cd 11 *
komoritan 0:edaa24c1f5cd 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
komoritan 0:edaa24c1f5cd 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
komoritan 0:edaa24c1f5cd 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
komoritan 0:edaa24c1f5cd 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
komoritan 0:edaa24c1f5cd 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
komoritan 0:edaa24c1f5cd 17 */
komoritan 0:edaa24c1f5cd 18 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
komoritan 0:edaa24c1f5cd 19 * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
komoritan 0:edaa24c1f5cd 20 */
komoritan 0:edaa24c1f5cd 21 #include "TCPSocketConnection.h"
komoritan 0:edaa24c1f5cd 22 #include <cstring>
komoritan 0:edaa24c1f5cd 23
komoritan 0:edaa24c1f5cd 24 TCPSocketConnection::TCPSocketConnection()
komoritan 0:edaa24c1f5cd 25 {
komoritan 0:edaa24c1f5cd 26 }
komoritan 0:edaa24c1f5cd 27
komoritan 0:edaa24c1f5cd 28 TCPSocketConnection::~TCPSocketConnection()
komoritan 0:edaa24c1f5cd 29 {
komoritan 0:edaa24c1f5cd 30 }
komoritan 0:edaa24c1f5cd 31
komoritan 0:edaa24c1f5cd 32 int TCPSocketConnection::connect( const char *host_p, unsigned short port)
komoritan 0:edaa24c1f5cd 33 {
komoritan 0:edaa24c1f5cd 34 int ret;
komoritan 0:edaa24c1f5cd 35 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 36 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:edaa24c1f5cd 37
komoritan 0:edaa24c1f5cd 38 FUNC_IN();
komoritan 0:edaa24c1f5cd 39 // Socket create
komoritan 0:edaa24c1f5cd 40 ret = createSocket();
komoritan 0:edaa24c1f5cd 41 if( ret != 0 )
komoritan 0:edaa24c1f5cd 42 {
komoritan 0:edaa24c1f5cd 43 DEBUG_PRINT("createSocket error : %d\r\n", ret);
komoritan 0:edaa24c1f5cd 44 FUNC_OUT();
komoritan 0:edaa24c1f5cd 45 return -1;
komoritan 0:edaa24c1f5cd 46 }
komoritan 0:edaa24c1f5cd 47
komoritan 0:edaa24c1f5cd 48 int ip_addr = resolveHostName( host_p );
komoritan 0:edaa24c1f5cd 49 //lcd_printf("connect to [%s](%08x)\r\n", host_p, ip_addr);
komoritan 0:edaa24c1f5cd 50 if( ( ip_addr == 0) || (ip_addr == -1) )
komoritan 0:edaa24c1f5cd 51 {
komoritan 0:edaa24c1f5cd 52 DEBUG_PRINT("connect resolveHostName failed\r\n");
komoritan 0:edaa24c1f5cd 53 FUNC_OUT();
komoritan 0:edaa24c1f5cd 54 return -1;
komoritan 0:edaa24c1f5cd 55 }
komoritan 0:edaa24c1f5cd 56
komoritan 0:edaa24c1f5cd 57 // Get buffer for response payload from MemoryPool
komoritan 0:edaa24c1f5cd 58 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:edaa24c1f5cd 59 if( payload_buf_p == NULL )
komoritan 0:edaa24c1f5cd 60 {
komoritan 0:edaa24c1f5cd 61 DEBUG_PRINT("connect payload_buf_p NULL\r\n");
komoritan 0:edaa24c1f5cd 62 FUNC_OUT();
komoritan 0:edaa24c1f5cd 63 return -1;
komoritan 0:edaa24c1f5cd 64 }
komoritan 0:edaa24c1f5cd 65
komoritan 0:edaa24c1f5cd 66 // IP address convert to number from strings.
komoritan 0:edaa24c1f5cd 67 // unsigned int ip_addr = addrToInteger(ip_addr_p);
komoritan 0:edaa24c1f5cd 68
komoritan 0:edaa24c1f5cd 69 //
komoritan 0:edaa24c1f5cd 70 C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req;
komoritan 0:edaa24c1f5cd 71 // Make request
komoritan 0:edaa24c1f5cd 72 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ;
komoritan 0:edaa24c1f5cd 73 req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 74 req.socket_id = mSocketID;
komoritan 0:edaa24c1f5cd 75
komoritan 0:edaa24c1f5cd 76 // set ip addr ( byte order )
komoritan 0:edaa24c1f5cd 77 C_SNIC_UartMsgUtil::convertIntToByteAdday( ip_addr, (char *)req.remote_addr );
komoritan 0:edaa24c1f5cd 78 req.remote_port[0] = ( (port & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 79 req.remote_port[1] = (port & 0xFF);
komoritan 0:edaa24c1f5cd 80 req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 81 req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
komoritan 0:edaa24c1f5cd 82 req.timeout = 60;
komoritan 0:edaa24c1f5cd 83
komoritan 0:edaa24c1f5cd 84 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:edaa24c1f5cd 85 unsigned int command_len;
komoritan 0:edaa24c1f5cd 86 // Preparation of command
komoritan 0:edaa24c1f5cd 87 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
komoritan 0:edaa24c1f5cd 88 , sizeof(C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 89
komoritan 0:edaa24c1f5cd 90 uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND );
komoritan 0:edaa24c1f5cd 91
komoritan 0:edaa24c1f5cd 92 // Send uart command request
komoritan 0:edaa24c1f5cd 93 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 94
komoritan 0:edaa24c1f5cd 95 // Wait UART response
komoritan 0:edaa24c1f5cd 96 ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 97 if( ret != 0 )
komoritan 0:edaa24c1f5cd 98 {
komoritan 0:edaa24c1f5cd 99 DEBUG_PRINT( "connect failed\r\n" );
komoritan 0:edaa24c1f5cd 100 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 101 FUNC_OUT();
komoritan 0:edaa24c1f5cd 102 return -1;
komoritan 0:edaa24c1f5cd 103 }
komoritan 0:edaa24c1f5cd 104
komoritan 0:edaa24c1f5cd 105 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP )
komoritan 0:edaa24c1f5cd 106 {
komoritan 0:edaa24c1f5cd 107 DEBUG_PRINT("connect status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 108 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 109 FUNC_OUT();
komoritan 0:edaa24c1f5cd 110 return -1;
komoritan 0:edaa24c1f5cd 111 }
komoritan 0:edaa24c1f5cd 112
komoritan 0:edaa24c1f5cd 113 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 114
komoritan 0:edaa24c1f5cd 115 // Initialize connection information
komoritan 0:edaa24c1f5cd 116 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
komoritan 0:edaa24c1f5cd 117 if( con_info_p->recvbuf_p == NULL )
komoritan 0:edaa24c1f5cd 118 {
komoritan 0:edaa24c1f5cd 119 DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID);
komoritan 0:edaa24c1f5cd 120 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
komoritan 0:edaa24c1f5cd 121 }
komoritan 0:edaa24c1f5cd 122 con_info_p->is_connected = true;
komoritan 0:edaa24c1f5cd 123 con_info_p->is_received = false;
komoritan 0:edaa24c1f5cd 124 FUNC_OUT();
komoritan 0:edaa24c1f5cd 125 return 0;
komoritan 0:edaa24c1f5cd 126 }
komoritan 0:edaa24c1f5cd 127
komoritan 0:edaa24c1f5cd 128 bool TCPSocketConnection::is_connected(void)
komoritan 0:edaa24c1f5cd 129 {
komoritan 0:edaa24c1f5cd 130 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 131 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
komoritan 0:edaa24c1f5cd 132 return con_info_p->is_connected;
komoritan 0:edaa24c1f5cd 133 }
komoritan 0:edaa24c1f5cd 134
komoritan 0:edaa24c1f5cd 135 int TCPSocketConnection::send(char* data_p, int length)
komoritan 0:edaa24c1f5cd 136 {
komoritan 0:edaa24c1f5cd 137 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 138 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
komoritan 0:edaa24c1f5cd 139
komoritan 0:edaa24c1f5cd 140 FUNC_IN();
komoritan 0:edaa24c1f5cd 141 // Get buffer for response payload from MemoryPool
komoritan 0:edaa24c1f5cd 142 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
komoritan 0:edaa24c1f5cd 143 if( payload_buf_p == NULL )
komoritan 0:edaa24c1f5cd 144 {
komoritan 0:edaa24c1f5cd 145 DEBUG_PRINT("connect payload_buf_p NULL\r\n");
komoritan 0:edaa24c1f5cd 146 FUNC_OUT();
komoritan 0:edaa24c1f5cd 147 return -1;
komoritan 0:edaa24c1f5cd 148 }
komoritan 0:edaa24c1f5cd 149
komoritan 0:edaa24c1f5cd 150 C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T req;
komoritan 0:edaa24c1f5cd 151 // Make request
komoritan 0:edaa24c1f5cd 152 req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ;
komoritan 0:edaa24c1f5cd 153 req.seq = mUartRequestSeq++;
komoritan 0:edaa24c1f5cd 154 req.socket_id = mSocketID;
komoritan 0:edaa24c1f5cd 155 req.option = 0;
komoritan 0:edaa24c1f5cd 156 req.payload_len[0]= ( (length & 0xFF00) >> 8 );
komoritan 0:edaa24c1f5cd 157 req.payload_len[1]= (length & 0xFF);
komoritan 0:edaa24c1f5cd 158
komoritan 0:edaa24c1f5cd 159 int req_size = sizeof(C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T);
komoritan 0:edaa24c1f5cd 160 char *send_buf_p = getSocketSendBuf();
komoritan 0:edaa24c1f5cd 161 memcpy( send_buf_p, &req, req_size );
komoritan 0:edaa24c1f5cd 162 memcpy( &send_buf_p[req_size], data_p, length );
komoritan 0:edaa24c1f5cd 163
komoritan 0:edaa24c1f5cd 164 unsigned char *command_array_p = snic_core_p->getCommandBuf();
komoritan 0:edaa24c1f5cd 165 unsigned int command_len;
komoritan 0:edaa24c1f5cd 166 // Preparation of command
komoritan 0:edaa24c1f5cd 167 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)send_buf_p
komoritan 0:edaa24c1f5cd 168 , req_size + length, payload_buf_p->buf, command_array_p );
komoritan 0:edaa24c1f5cd 169
komoritan 0:edaa24c1f5cd 170 // Send uart command request
komoritan 0:edaa24c1f5cd 171 snic_core_p->sendUart( command_len, command_array_p );
komoritan 0:edaa24c1f5cd 172
komoritan 0:edaa24c1f5cd 173 // Wait UART response
komoritan 0:edaa24c1f5cd 174 int ret = uartCmdMgr_p->wait();
komoritan 0:edaa24c1f5cd 175 if( ret != 0 )
komoritan 0:edaa24c1f5cd 176 {
komoritan 0:edaa24c1f5cd 177 DEBUG_PRINT( "send failed\r\n" );
komoritan 0:edaa24c1f5cd 178 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 179 FUNC_OUT();
komoritan 0:edaa24c1f5cd 180 return -1;
komoritan 0:edaa24c1f5cd 181 }
komoritan 0:edaa24c1f5cd 182
komoritan 0:edaa24c1f5cd 183 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
komoritan 0:edaa24c1f5cd 184 {
komoritan 0:edaa24c1f5cd 185 DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
komoritan 0:edaa24c1f5cd 186 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 187 FUNC_OUT();
komoritan 0:edaa24c1f5cd 188 return -1;
komoritan 0:edaa24c1f5cd 189 }
komoritan 0:edaa24c1f5cd 190 snic_core_p->freeCmdBuf( payload_buf_p );
komoritan 0:edaa24c1f5cd 191
komoritan 0:edaa24c1f5cd 192 // SNIC_SEND_FROM_SOCKET_REQ
komoritan 0:edaa24c1f5cd 193 FUNC_OUT();
komoritan 0:edaa24c1f5cd 194 return length;
komoritan 0:edaa24c1f5cd 195 }
komoritan 0:edaa24c1f5cd 196
komoritan 0:edaa24c1f5cd 197 int TCPSocketConnection::send_all(char *data_p, int length)
komoritan 0:edaa24c1f5cd 198 {
komoritan 0:edaa24c1f5cd 199 return send( data_p, length );
komoritan 0:edaa24c1f5cd 200 }
komoritan 0:edaa24c1f5cd 201
komoritan 0:edaa24c1f5cd 202 int TCPSocketConnection::receive(char* data_p, int length)
komoritan 0:edaa24c1f5cd 203 {
komoritan 0:edaa24c1f5cd 204 int i = 0;
komoritan 0:edaa24c1f5cd 205
komoritan 0:edaa24c1f5cd 206 FUNC_IN();
komoritan 0:edaa24c1f5cd 207 if( (data_p == NULL) || (length < 1) )
komoritan 0:edaa24c1f5cd 208 {
komoritan 0:edaa24c1f5cd 209 DEBUG_PRINT("TCPSocketConnection::receive parameter error\r\n");
komoritan 0:edaa24c1f5cd 210 FUNC_OUT();
komoritan 0:edaa24c1f5cd 211 return -1;
komoritan 0:edaa24c1f5cd 212 }
komoritan 0:edaa24c1f5cd 213
komoritan 0:edaa24c1f5cd 214 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
komoritan 0:edaa24c1f5cd 215 // Initialize connection information
komoritan 0:edaa24c1f5cd 216 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
komoritan 0:edaa24c1f5cd 217 if( con_info_p->recvbuf_p == NULL )
komoritan 0:edaa24c1f5cd 218 {
komoritan 0:edaa24c1f5cd 219 DEBUG_PRINT("TCPSocketConnection::receive Conncection info error\r\n");
komoritan 0:edaa24c1f5cd 220 FUNC_OUT();
komoritan 0:edaa24c1f5cd 221 return -1;
komoritan 0:edaa24c1f5cd 222 }
komoritan 0:edaa24c1f5cd 223
komoritan 0:edaa24c1f5cd 224 // Check connection
komoritan 0:edaa24c1f5cd 225 if( con_info_p->is_connected == false )
komoritan 0:edaa24c1f5cd 226 {
komoritan 0:edaa24c1f5cd 227 DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", mSocketID);
komoritan 0:edaa24c1f5cd 228 FUNC_OUT();
komoritan 0:edaa24c1f5cd 229 return -1;
komoritan 0:edaa24c1f5cd 230 }
komoritan 0:edaa24c1f5cd 231 con_info_p->is_receive_complete = true;
komoritan 0:edaa24c1f5cd 232 if( con_info_p->is_received == false )
komoritan 0:edaa24c1f5cd 233 {
komoritan 0:edaa24c1f5cd 234 // Try receive
komoritan 0:edaa24c1f5cd 235 Thread::yield();
komoritan 0:edaa24c1f5cd 236
komoritan 0:edaa24c1f5cd 237 if( con_info_p->is_received == false )
komoritan 0:edaa24c1f5cd 238 {
komoritan 0:edaa24c1f5cd 239 // No data received.
komoritan 0:edaa24c1f5cd 240 FUNC_OUT();
komoritan 0:edaa24c1f5cd 241 return 0;
komoritan 0:edaa24c1f5cd 242 }
komoritan 0:edaa24c1f5cd 243 }
komoritan 0:edaa24c1f5cd 244 // Get packet data from buffer for receive.
komoritan 0:edaa24c1f5cd 245 for (i = 0; i < length; i ++)
komoritan 0:edaa24c1f5cd 246 {
komoritan 0:edaa24c1f5cd 247 if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
komoritan 0:edaa24c1f5cd 248 {
komoritan 0:edaa24c1f5cd 249 break;
komoritan 0:edaa24c1f5cd 250 }
komoritan 0:edaa24c1f5cd 251 }
komoritan 0:edaa24c1f5cd 252
komoritan 0:edaa24c1f5cd 253 if( con_info_p->recvbuf_p->isEmpty() )
komoritan 0:edaa24c1f5cd 254 {
komoritan 0:edaa24c1f5cd 255 con_info_p->mutex.lock();
komoritan 0:edaa24c1f5cd 256 con_info_p->is_received = false;
komoritan 0:edaa24c1f5cd 257 con_info_p->mutex.unlock();
komoritan 0:edaa24c1f5cd 258 }
komoritan 0:edaa24c1f5cd 259
komoritan 0:edaa24c1f5cd 260 FUNC_OUT();
komoritan 0:edaa24c1f5cd 261 return i;
komoritan 0:edaa24c1f5cd 262 }
komoritan 0:edaa24c1f5cd 263
komoritan 0:edaa24c1f5cd 264 int TCPSocketConnection::receive_all(char* data_p, int length)
komoritan 0:edaa24c1f5cd 265 {
komoritan 0:edaa24c1f5cd 266 return receive( data_p, length );
komoritan 0:edaa24c1f5cd 267 }
komoritan 0:edaa24c1f5cd 268
komoritan 0:edaa24c1f5cd 269 void TCPSocketConnection::setAcceptSocket( int socket_id )
komoritan 0:edaa24c1f5cd 270 {
komoritan 0:edaa24c1f5cd 271 FUNC_IN();
komoritan 0:edaa24c1f5cd 272 mSocketID = socket_id;
komoritan 0:edaa24c1f5cd 273 }