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