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