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:
Thu Jun 19 10:15:47 2014 +0000
Parent:
35:e4e7f86fd975
Child:
37:f3a2053627c2
Commit message:
Supported a new command format of SNIC UART.

Changed in this revision

SNIC/SNIC_Core.cpp Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_Core.h Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_UartMsgUtil.cpp Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_UartMsgUtil.h Show annotated file Show diff for this revision Revisions of this file
SNIC_WifiInterface.cpp 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/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SNIC/SNIC_Core.cpp	Tue Jun 10 10:19:48 2014 +0000
+++ b/SNIC/SNIC_Core.cpp	Thu Jun 19 10:15:47 2014 +0000
@@ -32,7 +32,6 @@
 */
 unsigned char  gUART_TEMP_BUF[UART_RECVBUF_SIZE]                __attribute__((section("AHBSRAM1")));
 unsigned char  gUART_COMMAND_BUF[UART_REQUEST_PAYLOAD_MAX]      __attribute__((section("AHBSRAM1")));
-unsigned char  gPAYLOAD_ARRAY[UART_REQUEST_PAYLOAD_MAX]         __attribute__((section("AHBSRAM1")));
 /** MemoryPool for payload of UART response */
 MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM>     mMemPoolPayload  __attribute__((section("AHBSRAM1")));
 /** MemoryPool for UART receive */
@@ -114,10 +113,8 @@
     unsigned short payload_len;
     unsigned int   command_len = 0;
     
-    // Make command payload
-    payload_len = C_SNIC_UartMsgUtil::makePayload( req_buf_len, req_buf_p, gPAYLOAD_ARRAY );
     // Make all command request
-    command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, gPAYLOAD_ARRAY, payload_len, command_p );
+    command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, req_buf_p, req_buf_len, command_p );
 
     // Set data for response
     mUartCommand_p->setCommandID( cmd_id );
@@ -141,6 +138,7 @@
         }
     }
     mUartMutex.unlock();
+
     return ret;
 }
 
@@ -215,17 +213,29 @@
                 gUART_RCVBUF_p->buf[ gUART_RCVBUF_p->size ] = (unsigned char)recvdata;
                 gUART_RCVBUF_p->size++;
                 
-                // Check  received data is EOM.
-                if( recvdata == UART_CMD_EOM )
+                if( gUART_RCVBUF_p->size == 3 )
                 {
-                    // Add queue 
-                    mUartRecvQueue.put( gUART_RCVBUF_p );
+                    // get demand size
+                    unsigned short  payload_len = ( ( (gUART_RCVBUF_p->buf[1] & ~0x80) & 0xff) | ( ( (gUART_RCVBUF_p->buf[2] & ~0xC0) << 7) & 0xff80) );
+                    gUART_RCVBUF_p->demand_size = payload_len + 6;
+                        
+                }
+
+                if( gUART_RCVBUF_p->demand_size > 0 )
+                {
+                    // Check size of received data.
+                    if( gUART_RCVBUF_p->size >= gUART_RCVBUF_p->demand_size )
+                    {
+                        // Add queue 
+                        mUartRecvQueue.put( gUART_RCVBUF_p );
+                        
+                        gUART_RCVBUF_p = NULL;
                     
-                    gUART_RCVBUF_p = NULL;
-                
-                    // set signal for dispatch thread
-                    instance_p->mUartRecvDispatchThread_p->signal_set( UART_DISPATCH_SIGNAL );
+                        // set signal for dispatch thread
+                        instance_p->mUartRecvDispatchThread_p->signal_set( UART_DISPATCH_SIGNAL );
+                    }
                 }
+
             }
             else
             {
@@ -234,6 +244,7 @@
                 {
                     gUART_RCVBUF_p = instance_p->allocUartRcvBuf();
                     gUART_RCVBUF_p->size = 0;
+                    gUART_RCVBUF_p->demand_size = 0;
                     // get buffer for Uart receive
                     gUART_RCVBUF_p->buf[ 0 ] = (unsigned char)recvdata;
                     
--- a/SNIC/SNIC_Core.h	Tue Jun 10 10:19:48 2014 +0000
+++ b/SNIC/SNIC_Core.h	Thu Jun 19 10:15:47 2014 +0000
@@ -21,7 +21,7 @@
 
 #include "SNIC_UartCommandManager.h"
 
-#define UART_REQUEST_PAYLOAD_MAX 1024
+#define UART_REQUEST_PAYLOAD_MAX 2048
 
 #define MEMPOOL_BLOCK_SIZE  2048
 #define MEMPOOL_PAYLOAD_NUM 1
@@ -65,6 +65,7 @@
 typedef struct
 {
     unsigned int  size;
+    unsigned int  demand_size;
     unsigned char buf[MEMPOOL_BLOCK_SIZE];
 }tagMEMPOOL_BLOCK_T;
 
@@ -376,7 +377,7 @@
     /** UDP Information */
     tagUDP_RECVINFO_T   mUdpRecvInfo[MAX_SOCKET_ID+1];
 
-/** Constructor
+    /** Constructor
      */
     C_SNIC_Core();
 
--- a/SNIC/SNIC_UartMsgUtil.cpp	Tue Jun 10 10:19:48 2014 +0000
+++ b/SNIC/SNIC_UartMsgUtil.cpp	Thu Jun 19 10:15:47 2014 +0000
@@ -21,32 +21,6 @@
 {
 }
 
-unsigned short C_SNIC_UartMsgUtil::makePayload( unsigned int cmd_len, unsigned char *cmd_p, unsigned char *payload_p )
-{
-    unsigned short payload_len = 0;
-    int i;
-    
-    for( i = 0; i < cmd_len; i++, payload_p++, payload_len++ )
-    {
-        /* check Escape code */
-        if( ( cmd_p[i] == UART_CMD_SOM ) || ( cmd_p[i] == UART_CMD_EOM ) || ( cmd_p[i] == UART_CMD_ESC ) )
-        {
-            /* Add ESC */
-            *payload_p = UART_CMD_ESC;
-            payload_len++;
-            
-            payload_p++;
-            *payload_p = (0x80 | cmd_p[i]);
-        }
-        else
-        {
-            *payload_p = cmd_p[i];
-        }
-    }
-    
-    return payload_len;
-}
-
 unsigned int C_SNIC_UartMsgUtil::makeRequest( unsigned char cmd_id,unsigned char *payload_p
                                 , unsigned short payload_len, unsigned char *uart_command_p )
 {
@@ -81,7 +55,6 @@
     for( i = 0; i < payload_len; i++, uart_command_p++, uart_cmd_len++ )
     {
         *uart_command_p = payload_p[i];
-        check_sum += *uart_command_p;
     }
 
     // set Check sum
@@ -100,7 +73,6 @@
                                             , unsigned char *command_id_p,  unsigned char *payload_p )
 {
     unsigned short payload_len  = 0;
-    unsigned int   response_len = 0;
     unsigned char *buf = NULL;
     bool isESC = false;
     int i;
@@ -116,31 +88,11 @@
     // get payload data
     for( i = 0; i < payload_len; i++, buf++ )
     {
-        if( isESC )
-        {
-            *payload_p = (*buf & ~0x80);
-            payload_p++;
-            response_len++;
-            isESC = false;
-        }
-        else
-        {
-            // Check Escape code
-            if( *buf == UART_CMD_ESC )
-            {
-                isESC = true;
-                continue;
-            }
-            else
-            {
-                *payload_p = *buf;
-                payload_p++;
-                response_len++;
-            }
-        }
+        *payload_p = *buf;
+        payload_p++;
     }
 
-    return response_len;
+    return payload_len;
 }
 
 int C_SNIC_UartMsgUtil::addrToInteger( const char *addr_p )
@@ -190,4 +142,4 @@
     addr_array_p[1] = ((addr & 0xFF0000) >> 16 );
     addr_array_p[2] = ((addr & 0xFF00) >> 8 );
     addr_array_p[3] = ( addr & 0xFF);
-}
\ No newline at end of file
+}
--- a/SNIC/SNIC_UartMsgUtil.h	Tue Jun 10 10:19:48 2014 +0000
+++ b/SNIC/SNIC_UartMsgUtil.h	Thu Jun 19 10:15:47 2014 +0000
@@ -142,14 +142,6 @@
     C_SNIC_UartMsgUtil();
     virtual ~C_SNIC_UartMsgUtil();
     
-    /** Make SNIC UART command payload.
-            @param cmd_len      Command length
-            @param cmd_p        Command pointer
-            @param payload_p    Payload pointer[output]
-            @return payload length    
-    */
-    static unsigned short makePayload( unsigned int cmd_len, unsigned char *cmd_p, unsigned char *payload_p );
-    
     /** Make SNIC UART command.
             @param cmd_id         Command ID
             @param payload_p      Payload pointer
--- a/SNIC_WifiInterface.cpp	Tue Jun 10 10:19:48 2014 +0000
+++ b/SNIC_WifiInterface.cpp	Thu Jun 19 10:15:47 2014 +0000
@@ -13,6 +13,8 @@
 #include "SNIC_WifiInterface.h"
 #include "SNIC_UartMsgUtil.h"
 
+#define UART_CONNECT_BUF_SIZE   512
+unsigned char gCONNECT_BUF[UART_CONNECT_BUF_SIZE];
 
 C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud)
 {
@@ -39,6 +41,7 @@
     /* Initialize UART */
     snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud );
 
+    wait(0.5);
     /* Initialize SNIC API */
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
@@ -131,7 +134,6 @@
     return 0;
 }
 
-unsigned char gCONNECT_BUF[UART_REQUEST_PAYLOAD_MAX];
 int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type
                             , const char *sec_key_p, unsigned char sec_key_len)
 {
@@ -164,7 +166,7 @@
     unsigned int  buf_len = 0;
     unsigned int  command_len;
 
-    memset( buf, 0, UART_REQUEST_PAYLOAD_MAX );
+    memset( buf, 0, UART_CONNECT_BUF_SIZE );
     // Make request
     buf[0] = UART_CMD_SID_WIFI_JOIN_REQ;
     buf_len++;
@@ -641,5 +643,5 @@
     }
 
     snic_core_p->freeCmdBuf( payload_buf );
-    return 0;
+    return ret;
 }
--- a/Socket/Socket.cpp	Tue Jun 10 10:19:48 2014 +0000
+++ b/Socket/Socket.cpp	Thu Jun 19 10:15:47 2014 +0000
@@ -31,6 +31,7 @@
 #include <cstring>
 
 char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1")));
+
 Socket::Socket()
 {
     mSocketID = -1;
@@ -190,8 +191,8 @@
         printf("resolveHostName payload_buf NULL\r\n");
         return -1;
     }
-
-    unsigned char buf[UART_REQUEST_PAYLOAD_MAX];
+    
+    unsigned char *buf = (unsigned char *)getSocketSendBuf();
     unsigned int  buf_len = 0;
 
     memset( buf, 0, UART_REQUEST_PAYLOAD_MAX );
@@ -215,7 +216,6 @@
     unsigned int   command_len;
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, UART_CMD_SID_SNIC_RESOLVE_NAME_REQ, buf
                         , buf_len, payload_buf->buf, command_array );
-
     // Send uart command request
     snic_core_p->sendUart( command_len, command_array );
     
--- a/Socket/TCPSocketServer.cpp	Tue Jun 10 10:19:48 2014 +0000
+++ b/Socket/TCPSocketServer.cpp	Thu Jun 19 10:15:47 2014 +0000
@@ -61,7 +61,7 @@
     req.seq          = mUartRequestSeq++;
     req.interface    = 0;
     
-    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned char *command_array = snic_core_p->getCommandBuf();
     unsigned int  command_len;
     // Preparation of command
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
@@ -124,7 +124,7 @@
     req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
     req.max_client      = max;
     
-    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned char *command_array = snic_core_p->getCommandBuf();
     unsigned int  command_len;
     // Preparation of command
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
--- a/Socket/UDPSocket.cpp	Tue Jun 10 10:19:48 2014 +0000
+++ b/Socket/UDPSocket.cpp	Thu Jun 19 10:15:47 2014 +0000
@@ -65,7 +65,7 @@
     req.seq          = mUartRequestSeq++;
     req.interface    = 0;
     
-    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned char *command_array = snic_core_p->getCommandBuf();
     unsigned int  command_len;
     // Preparation of command
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req