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