debug version
Dependents: HTTPClient_WiFi_HelloWorld_src
Fork of SNICInterface by
Socket/TCPSocketConnection.cpp@14:54378c96d285, 2014-03-26 (annotated)
- Committer:
- kishino
- Date:
- Wed Mar 26 04:38:12 2014 +0000
- Revision:
- 14:54378c96d285
- Parent:
- 13:53e6471d5753
- Child:
- 15:5eb637414df2
Added a comment of copyright.
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 | 12:0254eaccfda2 | 42 | |
kishino | 12:0254eaccfda2 | 43 | // Socket create |
kishino | 12:0254eaccfda2 | 44 | ret = createSocket(); |
kishino | 12:0254eaccfda2 | 45 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 46 | { |
kishino | 12:0254eaccfda2 | 47 | printf("createSocket error : %d\r\n", ret); |
kishino | 12:0254eaccfda2 | 48 | return -1; |
kishino | 12:0254eaccfda2 | 49 | } |
kishino | 12:0254eaccfda2 | 50 | |
kishino | 12:0254eaccfda2 | 51 | // Get buffer for response payload from MemoryPool |
kishino | 12:0254eaccfda2 | 52 | tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->getAlocCmdBuf(); |
kishino | 12:0254eaccfda2 | 53 | if( payload_buf == NULL ) |
kishino | 12:0254eaccfda2 | 54 | { |
kishino | 12:0254eaccfda2 | 55 | printf("connect payload_buf NULL\r\n"); |
kishino | 12:0254eaccfda2 | 56 | return -1; |
kishino | 12:0254eaccfda2 | 57 | } |
kishino | 12:0254eaccfda2 | 58 | |
kishino | 12:0254eaccfda2 | 59 | tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req; |
kishino | 12:0254eaccfda2 | 60 | // Make request |
kishino | 12:0254eaccfda2 | 61 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ; |
kishino | 12:0254eaccfda2 | 62 | req.seq = mUartRequestSeq++; |
kishino | 12:0254eaccfda2 | 63 | req.socket_id = mSocketID; |
kishino | 12:0254eaccfda2 | 64 | |
kishino | 12:0254eaccfda2 | 65 | // set ip addr ( byte order ) |
kishino | 12:0254eaccfda2 | 66 | req.remote_addr[0] = ( (ip_addr & 0xFF000000) >> 24 ); |
kishino | 12:0254eaccfda2 | 67 | req.remote_addr[1] = ( (ip_addr & 0xFF0000) >> 16 ); |
kishino | 12:0254eaccfda2 | 68 | req.remote_addr[2] = ( (ip_addr & 0xFF00) >> 8 ); |
kishino | 12:0254eaccfda2 | 69 | req.remote_addr[3] = (ip_addr & 0xFF); |
kishino | 12:0254eaccfda2 | 70 | req.remote_port[0] = ( (port & 0xFF00) >> 8 ); |
kishino | 12:0254eaccfda2 | 71 | req.remote_port[1] = (port & 0xFF); |
kishino | 12:0254eaccfda2 | 72 | req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); |
kishino | 12:0254eaccfda2 | 73 | req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); |
kishino | 12:0254eaccfda2 | 74 | req.timeout = 60; |
kishino | 12:0254eaccfda2 | 75 | |
kishino | 12:0254eaccfda2 | 76 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 77 | unsigned int command_len; |
kishino | 12:0254eaccfda2 | 78 | // Preparation of command |
kishino | 12:0254eaccfda2 | 79 | command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 12:0254eaccfda2 | 80 | , sizeof(tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf->buf, command_array ); |
kishino | 12:0254eaccfda2 | 81 | |
kishino | 12:0254eaccfda2 | 82 | // Send uart command request |
kishino | 12:0254eaccfda2 | 83 | mSnicWifi_p->sendUart( command_len, command_array ); |
kishino | 12:0254eaccfda2 | 84 | |
kishino | 12:0254eaccfda2 | 85 | mSnicWifi_p->mUartCommand.setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND ); |
kishino | 12:0254eaccfda2 | 86 | |
kishino | 12:0254eaccfda2 | 87 | // Wait UART response |
kishino | 12:0254eaccfda2 | 88 | ret = mSnicWifi_p->mUartCommand.wait(); |
kishino | 12:0254eaccfda2 | 89 | if( ret != 0 ) |
kishino | 12:0254eaccfda2 | 90 | { |
kishino | 12:0254eaccfda2 | 91 | printf( "connect failed\r\n" ); |
kishino | 12:0254eaccfda2 | 92 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 93 | return -1; |
kishino | 12:0254eaccfda2 | 94 | } |
kishino | 12:0254eaccfda2 | 95 | |
kishino | 12:0254eaccfda2 | 96 | if( mSnicWifi_p->mUartCommand.getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP ) |
kishino | 12:0254eaccfda2 | 97 | { |
kishino | 12:0254eaccfda2 | 98 | printf("connect status:%02x\r\n", mSnicWifi_p->mUartCommand.getCommandStatus()); |
kishino | 12:0254eaccfda2 | 99 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 100 | return -1; |
kishino | 12:0254eaccfda2 | 101 | } |
kishino | 12:0254eaccfda2 | 102 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 12:0254eaccfda2 | 103 | |
kishino | 12:0254eaccfda2 | 104 | // Initialize connection information |
kishino | 12:0254eaccfda2 | 105 | tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID ); |
kishino | 12:0254eaccfda2 | 106 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 12:0254eaccfda2 | 107 | { |
kishino | 12:0254eaccfda2 | 108 | printf( "create recv buffer[socket:%d]\r\n", mSocketID); |
kishino | 12:0254eaccfda2 | 109 | con_info_p->recvbuf_p = new CircBuffer<unsigned char>(SNIC_UART_RECVBUF_SIZE); |
kishino | 12:0254eaccfda2 | 110 | } |
kishino | 12:0254eaccfda2 | 111 | con_info_p->is_connected = true; |
kishino | 12:0254eaccfda2 | 112 | con_info_p->is_received = false; |
kishino | 4:99cc93fe7d88 | 113 | |
kishino | 4:99cc93fe7d88 | 114 | return 0; |
kishino | 4:99cc93fe7d88 | 115 | } |
kishino | 4:99cc93fe7d88 | 116 | |
kishino | 4:99cc93fe7d88 | 117 | bool TCPSocketConnection::is_connected(void) |
kishino | 4:99cc93fe7d88 | 118 | { |
kishino | 12:0254eaccfda2 | 119 | tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID ); |
kishino | 12:0254eaccfda2 | 120 | return con_info_p->is_connected; |
kishino | 4:99cc93fe7d88 | 121 | } |
kishino | 4:99cc93fe7d88 | 122 | |
kishino | 13:53e6471d5753 | 123 | unsigned char gTCP_SEND_BUF[2048]; |
kishino | 12:0254eaccfda2 | 124 | int TCPSocketConnection::send(unsigned char* data_p, int length) |
kishino | 4:99cc93fe7d88 | 125 | { |
kishino | 13:53e6471d5753 | 126 | // Get buffer for response payload from MemoryPool |
kishino | 13:53e6471d5753 | 127 | tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->getAlocCmdBuf(); |
kishino | 13:53e6471d5753 | 128 | if( payload_buf == NULL ) |
kishino | 13:53e6471d5753 | 129 | { |
kishino | 13:53e6471d5753 | 130 | printf("connect payload_buf NULL\r\n"); |
kishino | 13:53e6471d5753 | 131 | return -1; |
kishino | 13:53e6471d5753 | 132 | } |
kishino | 13:53e6471d5753 | 133 | |
kishino | 13:53e6471d5753 | 134 | tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T req; |
kishino | 13:53e6471d5753 | 135 | // Make request |
kishino | 13:53e6471d5753 | 136 | req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ; |
kishino | 13:53e6471d5753 | 137 | req.seq = mUartRequestSeq++; |
kishino | 13:53e6471d5753 | 138 | req.socket_id = mSocketID; |
kishino | 13:53e6471d5753 | 139 | req.option = 0; |
kishino | 13:53e6471d5753 | 140 | req.payload_len[0]= ( (length & 0xFF00) >> 8 ); |
kishino | 13:53e6471d5753 | 141 | req.payload_len[1]= (length & 0xFF); |
kishino | 13:53e6471d5753 | 142 | |
kishino | 13:53e6471d5753 | 143 | int req_size = sizeof(tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T); |
kishino | 13:53e6471d5753 | 144 | memcpy( gTCP_SEND_BUF, &req, req_size ); |
kishino | 13:53e6471d5753 | 145 | memcpy( &gTCP_SEND_BUF[req_size], data_p, length ); |
kishino | 13:53e6471d5753 | 146 | |
kishino | 13:53e6471d5753 | 147 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 13:53e6471d5753 | 148 | unsigned int command_len; |
kishino | 13:53e6471d5753 | 149 | // Preparation of command |
kishino | 13:53e6471d5753 | 150 | command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, gTCP_SEND_BUF |
kishino | 13:53e6471d5753 | 151 | , req_size + length, payload_buf->buf, command_array ); |
kishino | 13:53e6471d5753 | 152 | |
kishino | 13:53e6471d5753 | 153 | // Send uart command request |
kishino | 13:53e6471d5753 | 154 | mSnicWifi_p->sendUart( command_len, command_array ); |
kishino | 13:53e6471d5753 | 155 | |
kishino | 13:53e6471d5753 | 156 | // mSnicWifi_p->mUartCommand.setCommandSID( req.cmd_sid ); |
kishino | 13:53e6471d5753 | 157 | |
kishino | 13:53e6471d5753 | 158 | // Wait UART response |
kishino | 13:53e6471d5753 | 159 | int ret = mSnicWifi_p->mUartCommand.wait(); |
kishino | 13:53e6471d5753 | 160 | if( ret != 0 ) |
kishino | 13:53e6471d5753 | 161 | { |
kishino | 13:53e6471d5753 | 162 | printf( "send failed\r\n" ); |
kishino | 13:53e6471d5753 | 163 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 13:53e6471d5753 | 164 | return -1; |
kishino | 13:53e6471d5753 | 165 | } |
kishino | 13:53e6471d5753 | 166 | |
kishino | 13:53e6471d5753 | 167 | if( mSnicWifi_p->mUartCommand.getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) |
kishino | 13:53e6471d5753 | 168 | { |
kishino | 13:53e6471d5753 | 169 | printf("send status:%02x\r\n", mSnicWifi_p->mUartCommand.getCommandStatus()); |
kishino | 13:53e6471d5753 | 170 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 13:53e6471d5753 | 171 | return -1; |
kishino | 13:53e6471d5753 | 172 | } |
kishino | 13:53e6471d5753 | 173 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 13:53e6471d5753 | 174 | |
kishino | 13:53e6471d5753 | 175 | // SNIC_SEND_FROM_SOCKET_REQ |
kishino | 4:99cc93fe7d88 | 176 | return 0; |
kishino | 4:99cc93fe7d88 | 177 | } |
kishino | 4:99cc93fe7d88 | 178 | |
kishino | 12:0254eaccfda2 | 179 | int TCPSocketConnection::receive(unsigned char* data_p, int length) |
kishino | 4:99cc93fe7d88 | 180 | { |
kishino | 12:0254eaccfda2 | 181 | int i = 0; |
kishino | 12:0254eaccfda2 | 182 | |
kishino | 12:0254eaccfda2 | 183 | if( (data_p == NULL) || (length < 1) ) |
kishino | 12:0254eaccfda2 | 184 | { |
kishino | 12:0254eaccfda2 | 185 | printf("TCPSocketConnection::receive parameter error\r\n"); |
kishino | 12:0254eaccfda2 | 186 | return -1; |
kishino | 12:0254eaccfda2 | 187 | } |
kishino | 12:0254eaccfda2 | 188 | |
kishino | 12:0254eaccfda2 | 189 | // Initialize connection information |
kishino | 12:0254eaccfda2 | 190 | tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID ); |
kishino | 12:0254eaccfda2 | 191 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 12:0254eaccfda2 | 192 | { |
kishino | 12:0254eaccfda2 | 193 | printf("TCPSocketConnection::receive Conncection info error\r\n"); |
kishino | 12:0254eaccfda2 | 194 | return -1; |
kishino | 12:0254eaccfda2 | 195 | } |
kishino | 12:0254eaccfda2 | 196 | |
kishino | 12:0254eaccfda2 | 197 | // Check connection |
kishino | 12:0254eaccfda2 | 198 | if( con_info_p->is_connected == false ) |
kishino | 12:0254eaccfda2 | 199 | { |
kishino | 12:0254eaccfda2 | 200 | printf(" Socket id \"%d\" is not connected\r\n", mSocketID); |
kishino | 12:0254eaccfda2 | 201 | return -1; |
kishino | 12:0254eaccfda2 | 202 | } |
kishino | 12:0254eaccfda2 | 203 | |
kishino | 12:0254eaccfda2 | 204 | if( con_info_p->is_received == false ) |
kishino | 12:0254eaccfda2 | 205 | { |
kishino | 12:0254eaccfda2 | 206 | // printf(" Socket id \"%d\" is not received\r\n", mSocketID); |
kishino | 12:0254eaccfda2 | 207 | return 0; |
kishino | 12:0254eaccfda2 | 208 | } |
kishino | 12:0254eaccfda2 | 209 | |
kishino | 12:0254eaccfda2 | 210 | // Get packet data from buffer for receive. |
kishino | 12:0254eaccfda2 | 211 | for (i = 0; i < length; i ++) |
kishino | 12:0254eaccfda2 | 212 | { |
kishino | 12:0254eaccfda2 | 213 | if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) |
kishino | 12:0254eaccfda2 | 214 | { |
kishino | 12:0254eaccfda2 | 215 | break; |
kishino | 12:0254eaccfda2 | 216 | } |
kishino | 12:0254eaccfda2 | 217 | } |
kishino | 12:0254eaccfda2 | 218 | |
kishino | 12:0254eaccfda2 | 219 | if( con_info_p->recvbuf_p->isEmpty() ) |
kishino | 12:0254eaccfda2 | 220 | { |
kishino | 12:0254eaccfda2 | 221 | con_info_p->is_received = false; |
kishino | 12:0254eaccfda2 | 222 | } |
kishino | 12:0254eaccfda2 | 223 | |
kishino | 12:0254eaccfda2 | 224 | return i; |
kishino | 4:99cc93fe7d88 | 225 | } |