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

Files at this revision

API Documentation at this revision

Comitter:
kishino
Date:
Mon Mar 31 02:06:14 2014 +0000
Parent:
26:f2e1030964e4
Child:
28:b796031f6519
Commit message:
Created a SNIC_CLOSE_SOCKET_REQ command.

Changed in this revision

SNIC/SNIC_Core.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
--- a/SNIC/SNIC_Core.h	Fri Mar 28 03:03:40 2014 +0000
+++ b/SNIC/SNIC_Core.h	Mon Mar 31 02:06:14 2014 +0000
@@ -117,6 +117,14 @@
         unsigned char  local_addr[4];
         unsigned char  local_port[2];
     }tagSNIC_TCP_CREATE_SOCKET_REQ_T;
+
+    /** SNIC_CLOSE_SOCKET_REQ */
+    typedef struct
+    {
+        unsigned char  cmd_sid;
+        unsigned char  seq;
+        unsigned char  socket_id;
+    }tagSNIC_CLOSE_SOCKET_REQ_T;
     
     /** SNIC_TCP_SEND_FROM_SOCKET_REQ */
     typedef struct
--- a/Socket/Socket.cpp	Fri Mar 28 03:03:40 2014 +0000
+++ b/Socket/Socket.cpp	Mon Mar 31 02:06:14 2014 +0000
@@ -51,7 +51,50 @@
 
 int Socket::close(bool shutdown)
 {
+    C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
+    C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
+    // Get buffer for response payload from MemoryPool
+    C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    if( payload_buf == NULL )
+    {
+        printf("socket close payload_buf NULL\r\n");
+        return -1;
+    }
+
+    C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T req;
     
+    // Make request
+    req.cmd_sid   = UART_CMD_SID_SNIC_CLOSE_SOCKET_REQ;
+    req.seq       = mUartRequestSeq++;
+    req.socket_id = mSocketID;
+
+    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned int  command_len;
+    // Preparation of command
+    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
+                            , sizeof(C_SNIC_Core::tagSNIC_CLOSE_SOCKET_REQ_T), payload_buf->buf, command_array );
+
+    // Send uart command request
+    snic_core_p->sendUart( command_len, command_array );
+
+    int ret;
+    // Wait UART response
+    ret = uartCmdMgr_p->wait();
+    if( ret != 0 )
+    {
+        printf( "socket close failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    if( uartCmdMgr_p->getCommandStatus() != 0 )
+    {
+        printf("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    snic_core_p->freeCmdBuf( payload_buf );
+
     return 0;
 }
 
@@ -124,3 +167,44 @@
 
     return 0;
 }
+
+int Socket::addrToInteger( const char *addr_p )
+{
+    if( addr_p == NULL )
+    {
+        printf("addrToInteger parameter error\r\n");
+        return 0;
+    }
+    
+    unsigned char ipadr[4];
+    unsigned char temp= 0;
+    int i,j,k;
+    
+    /* convert to char[4] */
+    k=0;
+    for(i=0; i<4; i ++)
+    {
+        for(j=0; j<4; j ++)
+        {
+            if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A))
+            {
+                temp = (temp * 10) + addr_p[k]-0x30;
+            }
+            else if((addr_p[k] == 0x20)&&(temp == 0))
+            {
+            }
+            else
+            {
+                ipadr[i]=temp;
+                temp = 0;
+                k++;
+                break;
+            }
+            k++;
+        }
+    }
+
+    int   addr  = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] );
+
+    return addr;
+}
--- a/Socket/Socket.h	Fri Mar 28 03:03:40 2014 +0000
+++ b/Socket/Socket.h	Mon Mar 31 02:06:14 2014 +0000
@@ -77,6 +77,9 @@
     int createSocket( unsigned char bind = 0, unsigned int local_addr = 0, unsigned short port = 0 );
 
 protected:
+    int addrToInteger( const char *addr_p );
+
+protected:
     int         mSocketID;
 #if 0
     bool _blocking;
--- a/Socket/TCPSocketConnection.cpp	Fri Mar 28 03:03:40 2014 +0000
+++ b/Socket/TCPSocketConnection.cpp	Mon Mar 31 02:06:14 2014 +0000
@@ -40,7 +40,7 @@
 {
 }
 
-int TCPSocketConnection::connect( unsigned int ip_addr, unsigned short port)
+int TCPSocketConnection::connect( const char *ip_addr_p, unsigned short port)
 {
     int ret;
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
@@ -62,6 +62,10 @@
         return -1;
     }
 
+    // IP address convert to number from strings.     
+    unsigned int ip_addr = addrToInteger(ip_addr_p);
+    printf("connect to [%s](%08x)\r\n", ip_addr_p, ip_addr);
+
     C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req;
     // Make request
     req.cmd_sid      = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ;
--- a/Socket/TCPSocketConnection.h	Fri Mar 28 03:03:40 2014 +0000
+++ b/Socket/TCPSocketConnection.h	Mon Mar 31 02:06:14 2014 +0000
@@ -50,13 +50,13 @@
     virtual ~TCPSocketConnection();
     
     /** Connects this TCP socket to the server
-        @param host The ip address to connect to.(hexadecimal)("192.168.0.1"->0xC0A80001)
+        @param host The strings of ip address.
         @param port The host's port to connect to.
         @return 0 on success, -1 on failure.
         @note   This function is blocked until a returns.
                 When you use it by UI thread, be careful. 
     */
-    int connect( unsigned int ip_addr, unsigned short port );
+    int connect( const char *ip_addr_p, unsigned short port );
     
     /** Check if the socket is connected
         @return true if connected, false otherwise.