SNICInterface for ThingPlug Example
Dependents: ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2
Fork of SNICInterface by
Socket/TCPSocketConnection.cpp@16:6100acfeb1f1, 2014-03-27 (annotated)
- Committer:
- kishino
- Date:
- Thu Mar 27 05:43:54 2014 +0000
- Revision:
- 16:6100acfeb1f1
- Parent:
- 15:5eb637414df2
The instance of C_SNIC_UartCommandManager in C_SNIC_Core class was changed from public to private.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 4:99cc93fe7d88 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
kishino | 4:99cc93fe7d88 | 2 | * |
kishino | 4:99cc93fe7d88 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kishino | 4:99cc93fe7d88 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kishino | 4:99cc93fe7d88 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kishino | 4:99cc93fe7d88 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kishino | 4:99cc93fe7d88 | 7 | * furnished to do so, subject to the following conditions: |
kishino | 4:99cc93fe7d88 | 8 | * |
kishino | 4:99cc93fe7d88 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
kishino | 4:99cc93fe7d88 | 10 | * substantial portions of the Software. |
kishino | 4:99cc93fe7d88 | 11 | * |
kishino | 4:99cc93fe7d88 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kishino | 4:99cc93fe7d88 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kishino | 4:99cc93fe7d88 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kishino | 4:99cc93fe7d88 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kishino | 4:99cc93fe7d88 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kishino | 4:99cc93fe7d88 | 17 | */ |
kishino | 14:54378c96d285 | 18 | /******************* Murata Manufacturing Co.,Ltd. 2014 ***************** |
kishino | 14:54378c96d285 | 19 | * |
kishino | 14:54378c96d285 | 20 | * Filename: TCPSocketConnection.cpp |
kishino | 14:54378c96d285 | 21 | * |
kishino | 14:54378c96d285 | 22 | * Purpose: This module has implementation of TCP connection. |
kishino | 14:54378c96d285 | 23 | * |
kishino | 14:54378c96d285 | 24 | * $Author: kishino $ |
kishino | 14:54378c96d285 | 25 | * |
kishino | 14:54378c96d285 | 26 | * $Date: 2014/03/26 $ |
kishino | 14:54378c96d285 | 27 | * |
kishino | 14:54378c96d285 | 28 | * $Revision: 0.0.0.1 $ |
kishino | 14:54378c96d285 | 29 | * ***********************************************************************/ |
kishino | 4:99cc93fe7d88 | 30 | #include "TCPSocketConnection.h" |
kishino | 4:99cc93fe7d88 | 31 | #include <cstring> |
kishino | 4:99cc93fe7d88 | 32 | |
kishino | 4:99cc93fe7d88 | 33 | using namespace murata_wifi; |
kishino | 4:99cc93fe7d88 | 34 | |
kishino | 12:0254eaccfda2 | 35 | TCPSocketConnection::TCPSocketConnection() |
kishino | 12:0254eaccfda2 | 36 | { |
kishino | 4:99cc93fe7d88 | 37 | } |
kishino | 4:99cc93fe7d88 | 38 | |
kishino | 12:0254eaccfda2 | 39 | int TCPSocketConnection::connect( unsigned int ip_addr, unsigned short port) |
kishino | 4:99cc93fe7d88 | 40 | { |
kishino | 12:0254eaccfda2 | 41 | int ret; |
kishino | 16:6100acfeb1f1 | 42 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSnicWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 43 | |
kishino | 12:0254eaccfda2 | 44 | // Socket create |
kishino | 12:0254eaccfda2 | 45 | ret = createSocket(); |
kishino | 12:0254eaccfda2 | 46 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 47 | { |
kishino | 12:0254eaccfda2 | 48 | printf("createSocket error : %d\r\n", ret); |
kishino | 12:0254eaccfda2 | 49 | return -1; |
kishino | 12:0254eaccfda2 | 50 | } |
kishino | 12:0254eaccfda2 | 51 | |
kishino | 12:0254eaccfda2 | 52 | // Get buffer for response payload from MemoryPool |
kishino | 15:5eb637414df2 | 53 | tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->allocCmdBuf(); |
kishino | 12:0254eaccfda2 | 54 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 55 | { |
kishino | 12:0254eaccfda2 | 56 | printf("connect payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 57 | return -1; |
kishino | 12:0254eaccfda2 | 58 | } |
kishino | 12:0254eaccfda2 | 59 | |
kishino | 12:0254eaccfda2 | 60 | tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req; |
kishino | 12:0254eaccfda2 | 61 | // Make request |
kishino | 12:0254eaccfda2 | 62 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ; |
kishino | 12:0254eaccfda2 | 63 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 64 | req.socket_id = mSocketID; |
kishino | 12:0254eaccfda2 | 65 | |
kishino | 12:0254eaccfda2 | 66 | // set ip addr ( byte order ) |
kishino | 12:0254eaccfda2 | 67 | req.remote_addr[0] = ( (ip_addr & 0xFF000000) >> 24 ); |
kishino | 12:0254eaccfda2 | 68 | req.remote_addr[1] = ( (ip_addr & 0xFF0000) >> 16 ); |
kishino | 12:0254eaccfda2 | 69 | req.remote_addr[2] = ( (ip_addr & 0xFF00) >> 8 ); |
kishino | 12:0254eaccfda2 | 70 | req.remote_addr[3] = (ip_addr & 0xFF); |
kishino | 12:0254eaccfda2 | 71 | req.remote_port[0] = ( (port & 0xFF00) >> 8 ); |
kishino | 12:0254eaccfda2 | 72 | req.remote_port[1] = (port & 0xFF); |
kishino | 12:0254eaccfda2 | 73 | req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); |
kishino | 12:0254eaccfda2 | 74 | req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); |
kishino | 12:0254eaccfda2 | 75 | req.timeout = 60; |
kishino | 12:0254eaccfda2 | 76 | |
kishino | 12:0254eaccfda2 | 77 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 78 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 79 | // Preparation of command |
kishino | 12:0254eaccfda2 | 80 | command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 12:0254eaccfda2 | 81 | , sizeof(tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 82 | |
kishino | 12:0254eaccfda2 | 83 | // Send uart command request |
kishino | 12:0254eaccfda2 | 84 | mSnicWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 85 | |
kishino | 16:6100acfeb1f1 | 86 | uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND ); |
kishino | 12:0254eaccfda2 | 87 | |
kishino | 12:0254eaccfda2 | 88 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 89 | ret = uartCmdMgr_p->wait(); |
kishino | 12:0254eaccfda2 | 90 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 91 | { |
kishino | 12:0254eaccfda2 | 92 | printf( "connect failed\r\n" ); |
kishino | 12:0254eaccfda2 | 93 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 94 | return -1; |
kishino | 12:0254eaccfda2 | 95 | } |
kishino | 12:0254eaccfda2 | 96 | |
kishino | 16:6100acfeb1f1 | 97 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP ) |
kishino | 12:0254eaccfda2 | 98 | { |
kishino | 16:6100acfeb1f1 | 99 | printf("connect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 12:0254eaccfda2 | 100 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 101 | return -1; |
kishino | 12:0254eaccfda2 | 102 | } |
kishino | 12:0254eaccfda2 | 103 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 104 | |
kishino | 12:0254eaccfda2 | 105 | // Initialize connection information |
kishino | 12:0254eaccfda2 | 106 | tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID ); |
kishino | 12:0254eaccfda2 | 107 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 12:0254eaccfda2 | 108 | { |
kishino | 12:0254eaccfda2 | 109 | printf( "create recv buffer[socket:%d]\r\n", mSocketID); |
kishino | 12:0254eaccfda2 | 110 | con_info_p->recvbuf_p = new CircBuffer<unsigned char>(SNIC_UART_RECVBUF_SIZE); |
kishino | 12:0254eaccfda2 | 111 | } |
kishino | 12:0254eaccfda2 | 112 | con_info_p->is_connected = true; |
kishino | 12:0254eaccfda2 | 113 | con_info_p->is_received = false; |
kishino | 4:99cc93fe7d88 | 114 | |
kishino | 4:99cc93fe7d88 | 115 | return 0; |
kishino | 4:99cc93fe7d88 | 116 | } |
kishino | 4:99cc93fe7d88 | 117 | |
kishino | 4:99cc93fe7d88 | 118 | bool TCPSocketConnection::is_connected(void) |
kishino | 4:99cc93fe7d88 | 119 | { |
kishino | 12:0254eaccfda2 | 120 | tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID ); |
kishino | 12:0254eaccfda2 | 121 | return con_info_p->is_connected; |
kishino | 4:99cc93fe7d88 | 122 | } |
kishino | 4:99cc93fe7d88 | 123 | |
kishino | 13:53e6471d5753 | 124 | unsigned char gTCP_SEND_BUF[2048]; |
kishino | 12:0254eaccfda2 | 125 | int TCPSocketConnection::send(unsigned char* data_p, int length) |
kishino | 4:99cc93fe7d88 | 126 | { |
kishino | 16:6100acfeb1f1 | 127 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSnicWifi_p->getUartCommand(); |
kishino | 16:6100acfeb1f1 | 128 | |
kishino | 13:53e6471d5753 | 129 | // Get buffer for response payload from MemoryPool |
kishino | 15:5eb637414df2 | 130 | tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->allocCmdBuf(); |
kishino | 13:53e6471d5753 | 131 | if( payload_buf == NULL ) |
kishino | 13:53e6471d5753 | 132 | { |
kishino | 13:53e6471d5753 | 133 | printf("connect payload_buf NULL\r\n"); |
kishino | 13:53e6471d5753 | 134 | return -1; |
kishino | 13:53e6471d5753 | 135 | } |
kishino | 13:53e6471d5753 | 136 | |
kishino | 13:53e6471d5753 | 137 | tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T req; |
kishino | 13:53e6471d5753 | 138 | // Make request |
kishino | 13:53e6471d5753 | 139 | req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ; |
kishino | 13:53e6471d5753 | 140 | req.seq = mUartRequestSeq++; |
kishino | 13:53e6471d5753 | 141 | req.socket_id = mSocketID; |
kishino | 13:53e6471d5753 | 142 | req.option = 0; |
kishino | 13:53e6471d5753 | 143 | req.payload_len[0]= ( (length & 0xFF00) >> 8 ); |
kishino | 13:53e6471d5753 | 144 | req.payload_len[1]= (length & 0xFF); |
kishino | 13:53e6471d5753 | 145 | |
kishino | 13:53e6471d5753 | 146 | int req_size = sizeof(tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T); |
kishino | 13:53e6471d5753 | 147 | memcpy( gTCP_SEND_BUF, &req, req_size ); |
kishino | 13:53e6471d5753 | 148 | memcpy( &gTCP_SEND_BUF[req_size], data_p, length ); |
kishino | 13:53e6471d5753 | 149 | |
kishino | 13:53e6471d5753 | 150 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 13:53e6471d5753 | 151 | unsigned int command_len; |
kishino | 13:53e6471d5753 | 152 | // Preparation of command |
kishino | 13:53e6471d5753 | 153 | command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, gTCP_SEND_BUF |
kishino | 13:53e6471d5753 | 154 | , req_size + length, payload_buf->buf, command_array ); |
kishino | 13:53e6471d5753 | 155 | |
kishino | 13:53e6471d5753 | 156 | // Send uart command request |
kishino | 13:53e6471d5753 | 157 | mSnicWifi_p->sendUart( command_len, command_array ); |
kishino | 13:53e6471d5753 | 158 | |
kishino | 13:53e6471d5753 | 159 | // mSnicWifi_p->mUartCommand.setCommandSID( req.cmd_sid ); |
kishino | 13:53e6471d5753 | 160 | |
kishino | 13:53e6471d5753 | 161 | // Wait UART response |
kishino | 16:6100acfeb1f1 | 162 | int ret = uartCmdMgr_p->wait(); |
kishino | 13:53e6471d5753 | 163 | if( ret != 0 ) |
kishino | 13:53e6471d5753 | 164 | { |
kishino | 13:53e6471d5753 | 165 | printf( "send failed\r\n" ); |
kishino | 13:53e6471d5753 | 166 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 13:53e6471d5753 | 167 | return -1; |
kishino | 13:53e6471d5753 | 168 | } |
kishino | 13:53e6471d5753 | 169 | |
kishino | 16:6100acfeb1f1 | 170 | if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
kishino | 13:53e6471d5753 | 171 | { |
kishino | 16:6100acfeb1f1 | 172 | printf("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 13:53e6471d5753 | 173 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 13:53e6471d5753 | 174 | return -1; |
kishino | 13:53e6471d5753 | 175 | } |
kishino | 13:53e6471d5753 | 176 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 13:53e6471d5753 | 177 | |
kishino | 13:53e6471d5753 | 178 | // SNIC_SEND_FROM_SOCKET_REQ |
kishino | 4:99cc93fe7d88 | 179 | return 0; |
kishino | 4:99cc93fe7d88 | 180 | } |
kishino | 4:99cc93fe7d88 | 181 | |
kishino | 12:0254eaccfda2 | 182 | int TCPSocketConnection::receive(unsigned char* data_p, int length) |
kishino | 4:99cc93fe7d88 | 183 | { |
kishino | 12:0254eaccfda2 | 184 | int i = 0; |
kishino | 12:0254eaccfda2 | 185 | |
kishino | 12:0254eaccfda2 | 186 | if( (data_p == NULL) || (length < 1) ) |
kishino | 12:0254eaccfda2 | 187 | { |
kishino | 12:0254eaccfda2 | 188 | printf("TCPSocketConnection::receive parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 189 | return -1; |
kishino | 12:0254eaccfda2 | 190 | } |
kishino | 12:0254eaccfda2 | 191 | |
kishino | 12:0254eaccfda2 | 192 | // Initialize connection information |
kishino | 12:0254eaccfda2 | 193 | tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID ); |
kishino | 12:0254eaccfda2 | 194 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 12:0254eaccfda2 | 195 | { |
kishino | 12:0254eaccfda2 | 196 | printf("TCPSocketConnection::receive Conncection info error\r\n"); |
kishino | 12:0254eaccfda2 | 197 | return -1; |
kishino | 12:0254eaccfda2 | 198 | } |
kishino | 12:0254eaccfda2 | 199 | |
kishino | 12:0254eaccfda2 | 200 | // Check connection |
kishino | 12:0254eaccfda2 | 201 | if( con_info_p->is_connected == false ) |
kishino | 12:0254eaccfda2 | 202 | { |
kishino | 12:0254eaccfda2 | 203 | printf(" Socket id \"%d\" is not connected\r\n", mSocketID); |
kishino | 12:0254eaccfda2 | 204 | return -1; |
kishino | 12:0254eaccfda2 | 205 | } |
kishino | 12:0254eaccfda2 | 206 | |
kishino | 12:0254eaccfda2 | 207 | if( con_info_p->is_received == false ) |
kishino | 12:0254eaccfda2 | 208 | { |
kishino | 12:0254eaccfda2 | 209 | // printf(" Socket id \"%d\" is not received\r\n", mSocketID); |
kishino | 12:0254eaccfda2 | 210 | return 0; |
kishino | 12:0254eaccfda2 | 211 | } |
kishino | 12:0254eaccfda2 | 212 | |
kishino | 12:0254eaccfda2 | 213 | // Get packet data from buffer for receive. |
kishino | 12:0254eaccfda2 | 214 | for (i = 0; i < length; i ++) |
kishino | 12:0254eaccfda2 | 215 | { |
kishino | 12:0254eaccfda2 | 216 | if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) |
kishino | 12:0254eaccfda2 | 217 | { |
kishino | 12:0254eaccfda2 | 218 | break; |
kishino | 12:0254eaccfda2 | 219 | } |
kishino | 12:0254eaccfda2 | 220 | } |
kishino | 12:0254eaccfda2 | 221 | |
kishino | 12:0254eaccfda2 | 222 | if( con_info_p->recvbuf_p->isEmpty() ) |
kishino | 12:0254eaccfda2 | 223 | { |
kishino | 12:0254eaccfda2 | 224 | con_info_p->is_received = false; |
kishino | 12:0254eaccfda2 | 225 | } |
kishino | 12:0254eaccfda2 | 226 | |
kishino | 12:0254eaccfda2 | 227 | return i; |
kishino | 4:99cc93fe7d88 | 228 | } |