SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Tue Mar 25 01:42:25 2014 +0000
Revision:
12:0254eaccfda2
Parent:
4:99cc93fe7d88
Child:
13:53e6471d5753
Refactoring of the class name

Who changed what in which revision?

UserRevisionLine numberNew 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 12:0254eaccfda2 111 int TCPSocketConnection::send(unsigned char* data_p, int length)
kishino 4:99cc93fe7d88 112 {
kishino 4:99cc93fe7d88 113 return 0;
kishino 4:99cc93fe7d88 114 }
kishino 4:99cc93fe7d88 115
kishino 12:0254eaccfda2 116 int TCPSocketConnection::receive(unsigned char* data_p, int length)
kishino 4:99cc93fe7d88 117 {
kishino 12:0254eaccfda2 118 int i = 0;
kishino 12:0254eaccfda2 119
kishino 12:0254eaccfda2 120 if( (data_p == NULL) || (length < 1) )
kishino 12:0254eaccfda2 121 {
kishino 12:0254eaccfda2 122 printf("TCPSocketConnection::receive parameter error\r\n");
kishino 12:0254eaccfda2 123 return -1;
kishino 12:0254eaccfda2 124 }
kishino 12:0254eaccfda2 125
kishino 12:0254eaccfda2 126 // Initialize connection information
kishino 12:0254eaccfda2 127 tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID );
kishino 12:0254eaccfda2 128 if( con_info_p->recvbuf_p == NULL )
kishino 12:0254eaccfda2 129 {
kishino 12:0254eaccfda2 130 printf("TCPSocketConnection::receive Conncection info error\r\n");
kishino 12:0254eaccfda2 131 return -1;
kishino 12:0254eaccfda2 132 }
kishino 12:0254eaccfda2 133
kishino 12:0254eaccfda2 134 // Check connection
kishino 12:0254eaccfda2 135 if( con_info_p->is_connected == false )
kishino 12:0254eaccfda2 136 {
kishino 12:0254eaccfda2 137 printf(" Socket id \"%d\" is not connected\r\n", mSocketID);
kishino 12:0254eaccfda2 138 return -1;
kishino 12:0254eaccfda2 139 }
kishino 12:0254eaccfda2 140
kishino 12:0254eaccfda2 141 if( con_info_p->is_received == false )
kishino 12:0254eaccfda2 142 {
kishino 12:0254eaccfda2 143 // printf(" Socket id \"%d\" is not received\r\n", mSocketID);
kishino 12:0254eaccfda2 144 return 0;
kishino 12:0254eaccfda2 145 }
kishino 12:0254eaccfda2 146
kishino 12:0254eaccfda2 147 // Get packet data from buffer for receive.
kishino 12:0254eaccfda2 148 for (i = 0; i < length; i ++)
kishino 12:0254eaccfda2 149 {
kishino 12:0254eaccfda2 150 if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
kishino 12:0254eaccfda2 151 {
kishino 12:0254eaccfda2 152 break;
kishino 12:0254eaccfda2 153 }
kishino 12:0254eaccfda2 154 }
kishino 12:0254eaccfda2 155
kishino 12:0254eaccfda2 156 if( con_info_p->recvbuf_p->isEmpty() )
kishino 12:0254eaccfda2 157 {
kishino 12:0254eaccfda2 158 con_info_p->is_received = false;
kishino 12:0254eaccfda2 159 }
kishino 12:0254eaccfda2 160
kishino 12:0254eaccfda2 161 return i;
kishino 4:99cc93fe7d88 162 }