SNIC UART Interface library: Serial to Wi-Fi library for Murata TypeYD Wi-Fi module. For more information about TypeYD: http://www.murata.co.jp/products/microwave/module/lbwb1zzydz/index.html

Dependents:   SNIC-xively-jumpstart-demo SNIC-FluentLogger-example TCPEchoServer murataDemo ... more

Fork of YDwifiInterface by Takao Kishino

Revision:
12:0254eaccfda2
Parent:
4:99cc93fe7d88
Child:
13:53e6471d5753
--- a/Socket/TCPSocketConnection.cpp	Wed Mar 19 01:48:37 2014 +0000
+++ b/Socket/TCPSocketConnection.cpp	Tue Mar 25 01:42:25 2014 +0000
@@ -20,29 +20,143 @@
 
 using namespace murata_wifi;
 
-TCPSocketConnection::TCPSocketConnection() :
-        _is_connected(false) {
+TCPSocketConnection::TCPSocketConnection()
+{
 }
 
-int TCPSocketConnection::connect(const char* host, const int port)
+int TCPSocketConnection::connect( unsigned int ip_addr, unsigned short port)
 {
-    _is_connected = true;
+    int ret;
+
+    // Socket create
+    ret = createSocket();
+    if( ret != 0 )
+    {
+        printf("createSocket error : %d\r\n", ret);
+        return -1;
+    }
+
+    // Get buffer for response payload from MemoryPool
+    tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->getAlocCmdBuf();
+    if( payload_buf == NULL )
+    {
+        printf("connect payload_buf NULL\r\n");
+        return -1;
+    }
+
+    tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req;
+    // Make request
+    req.cmd_sid      = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ;
+    req.seq          = mUartRequestSeq++;
+    req.socket_id    = mSocketID;
+    
+    // set ip addr ( byte order )
+    req.remote_addr[0] = ( (ip_addr & 0xFF000000) >> 24 );
+    req.remote_addr[1] = ( (ip_addr & 0xFF0000) >> 16 );
+    req.remote_addr[2] = ( (ip_addr & 0xFF00) >> 8 );
+    req.remote_addr[3] = (ip_addr & 0xFF);
+    req.remote_port[0] = ( (port & 0xFF00) >> 8 );
+    req.remote_port[1] = (port & 0xFF);
+    req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
+    req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
+    req.timeout         = 60;
+
+    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned int  command_len;
+    // Preparation of command
+    command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
+                            , sizeof(tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf->buf, command_array );
+
+    // Send uart command request
+    mSnicWifi_p->sendUart( command_len, command_array );
+
+    mSnicWifi_p->mUartCommand.setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND );
+
+    // Wait UART response
+    ret = mSnicWifi_p->mUartCommand.wait();
+    if( ret != 0 )
+    {
+        printf( "connect failed\r\n" );
+        mSnicWifi_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    if( mSnicWifi_p->mUartCommand.getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP )
+    {
+        printf("connect status:%02x\r\n", mSnicWifi_p->mUartCommand.getCommandStatus());
+        mSnicWifi_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    mSnicWifi_p->freeCmdBuf( payload_buf );
+
+    // Initialize connection information
+    tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID );
+    if( con_info_p->recvbuf_p == NULL )
+    {
+        printf( "create recv buffer[socket:%d]\r\n", mSocketID);
+        con_info_p->recvbuf_p = new CircBuffer<unsigned char>(SNIC_UART_RECVBUF_SIZE);
+    }
+    con_info_p->is_connected = true;
+    con_info_p->is_received  = false;
     
     return 0;
 }
 
 bool TCPSocketConnection::is_connected(void)
 {
-    return _is_connected;
+    tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID );
+    return con_info_p->is_connected;
 }
 
-int TCPSocketConnection::send(char* data, int length)
+int TCPSocketConnection::send(unsigned char* data_p, int length)
 {
     return 0;
 }
 
-int TCPSocketConnection::receive(char* data, int length)
+int TCPSocketConnection::receive(unsigned char* data_p, int length)
 {
-    return 0;
+    int i = 0;
+    
+    if( (data_p == NULL) || (length < 1) )
+    {
+        printf("TCPSocketConnection::receive parameter error\r\n");
+        return -1;
+    }
+    
+    // Initialize connection information
+    tagCONNECT_INFO_T *con_info_p = mSnicWifi_p->getConnectInfo( mSocketID );
+    if( con_info_p->recvbuf_p == NULL )
+    {
+        printf("TCPSocketConnection::receive Conncection info error\r\n");
+        return -1;
+    }
+
+    // Check connection
+    if( con_info_p->is_connected == false )
+    {
+        printf(" Socket id \"%d\" is not connected\r\n", mSocketID);
+        return -1;
+    }
+    
+    if( con_info_p->is_received == false )
+    {
+//        printf(" Socket id \"%d\" is not received\r\n", mSocketID);
+        return 0;
+    }
+    
+    // Get packet data from buffer for receive.
+    for (i = 0; i < length; i ++) 
+    {
+        if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
+        {
+            break;
+        }
+    }
+
+    if( con_info_p->recvbuf_p->isEmpty() )
+    {
+        con_info_p->is_received = false;
+    }
+
+    return i;
 }
-