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:
Fri Nov 14 00:52:31 2014 +0000
Parent:
42:17d89443d899
Child:
44:9d901fec86b8
Child:
45:50d60753ade3
Commit message:
It was reviewed and modified the Receive logic of socket communication(TCP and UDP).

Changed in this revision

SNIC/MurataObject.h Show annotated file Show diff for this revision Revisions of this file
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_UartCommandManager.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
SNIC_WifiInterface.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/TCPSocketConnection.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/MurataObject.h	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC/MurataObject.h	Fri Nov 14 00:52:31 2014 +0000
@@ -20,13 +20,25 @@
 #define _MURATA_OBJECT_H_
 #include "mbed.h"
 
-//#define _DEBUG      /* If this definition is enabled, debug log is output. */
+#define _DEBUG      /* If this definition is enabled, debug log is output. */
+//#define _FUNC_TRACE
 
 #ifdef _DEBUG
 extern Serial pc;
 #define DEBUG_PRINT(...)    { pc.printf(__VA_ARGS__);}
+
+#ifdef _FUNC_TRACE
+#define FUNC_IN()             { pc.printf( "%s[%d]%s[tid:%x] IN\r\n", __FILE__, __LINE__, __FUNCTION__, Thread::gettid());}
+#define FUNC_OUT()            { pc.printf( "%s[%d]%s[tid:%x] OUT\r\n", __FILE__, __LINE__, __FUNCTION__, Thread::gettid() );}
+#else
+#define FUNC_IN()
+#define FUNC_OUT()
+#endif
+
 #else
 #define DEBUG_PRINT(...)
+#define FUNC_IN()
+#define FUNC_OUT()
 #endif
 
 class C_MurataObject{
--- a/SNIC/SNIC_Core.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC/SNIC_Core.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -42,12 +42,15 @@
 unsigned char  gUART_TEMP_BUF[UART_RECVBUF_SIZE]                __attribute__((section("AHBSRAM1")));
 unsigned char  gUART_COMMAND_BUF[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<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM>     mMemPoolPayload  __attribute__((section("AHBSRAM1")));
+MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM>     mMemPoolPayload  __attribute__((section("AHBSRAM0")));
 /** MemoryPool for UART receive */
-MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM>   mMemPoolUartRecv __attribute__((section("AHBSRAM1")));
+MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM>   mMemPoolUartRecv __attribute__((section("AHBSRAM0")));
 Queue<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM>        mUartRecvQueue;
 
 tagMEMPOOL_BLOCK_T   *gUART_RCVBUF_p;
+int gUART_RECV_COUNT = 0;
+
 C_SNIC_Core *C_SNIC_Core::mInstance_p = NULL;
 
 C_SNIC_Core *C_SNIC_Core::getInstance()
@@ -68,6 +71,7 @@
     {
         mConnectInfo[i].recvbuf_p    = NULL;
         mConnectInfo[i].is_connected = false;
+        mConnectInfo[i].is_receive_complete = true;
         
         mUdpRecvInfo[i].recvbuf_p    = NULL;
         mUdpRecvInfo[i].is_received  = false;
@@ -86,9 +90,9 @@
     DigitalOut reset_pin( reset );
 
     reset_pin = 0;
-    wait(0.1);
+    wait(0.3);
     reset_pin = 1;
-    wait(0.1);
+    wait(0.3);
     
     return 0;
 }
@@ -200,6 +204,16 @@
     return gUART_COMMAND_BUF;
 }
 
+void C_SNIC_Core::lockAPI( void )
+{
+    mAPIMutex.lock();
+}
+
+void C_SNIC_Core::unlockAPI( void )
+{
+    mAPIMutex.unlock();
+}
+
 void C_SNIC_Core::uartRecvCallback( void )
 {
     C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
@@ -240,11 +254,15 @@
                         
                         gUART_RCVBUF_p = NULL;
                     
+                        if( gUART_RECV_COUNT >= MEMPOOL_UART_RECV_NUM )
+                        {
+                            instance_p->mUart_p->attach( NULL );
+                        }
                         // set signal for dispatch thread
                         instance_p->mUartRecvDispatchThread_p->signal_set( UART_DISPATCH_SIGNAL );
+                        break;
                     }
                 }
-
             }
             else
             {
@@ -252,8 +270,10 @@
                 if( recvdata == UART_CMD_SOM )
                 {
                     gUART_RCVBUF_p = instance_p->allocUartRcvBuf();
+                    gUART_RECV_COUNT++;
                     gUART_RCVBUF_p->size = 0;
                     gUART_RCVBUF_p->demand_size = 0;
+
                     // get buffer for Uart receive
                     gUART_RCVBUF_p->buf[ 0 ] = (unsigned char)recvdata;
                     
@@ -340,11 +360,16 @@
                 }
                 else
                 {
-                    DEBUG_PRINT(" The received data is not expected.\r\n");
+                    //DEBUG_PRINT(" The received data is not expected.\r\n");
                 }
                 
                 // 
                 instance_p->freeUartRecvBuf( uartRecvBuf_p );
+                gUART_RECV_COUNT--;
+                if( gUART_RECV_COUNT == (MEMPOOL_UART_RECV_NUM-1) )
+                {
+                    instance_p->mUart_p->attach( C_SNIC_Core::uartRecvCallback );   //debug
+                }
                 
                 evt = mUartRecvQueue.get(500);
             } while( evt.status == osEventMessage );
--- a/SNIC/SNIC_Core.h	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC/SNIC_Core.h	Fri Nov 14 00:52:31 2014 +0000
@@ -33,7 +33,7 @@
 #define MEMPOOL_PAYLOAD_NUM 1
 #define MAX_SOCKET_ID   5
 
-#define MEMPOOL_UART_RECV_NUM 2
+#define MEMPOOL_UART_RECV_NUM 6
 #define SNIC_UART_RECVBUF_SIZE         2048
 
 /** Wi-Fi security
@@ -102,7 +102,10 @@
         CircBuffer<char>    *recvbuf_p;
         bool                is_connected;
         bool                is_received;
+        bool                is_receive_complete;
         int                 parent_socket;
+        int                 from_ip;
+        short               from_port;
         bool                is_accept;
         Mutex               mutex;
     }tagCONNECT_INFO_T;
@@ -224,6 +227,26 @@
         unsigned char  local_port[2];
     }tagSNIC_UDP_CREATE_SOCKET_REQ_T;
     
+    /** SNIC_UDP_CREATE_SOCKET_REQ */
+    typedef struct
+    {
+        unsigned char  cmd_sid;
+        unsigned char  seq;
+        unsigned char  bind;
+    }tagSNIC_UDP_CREATE_SOCKET_REQ_CLIENT_T;
+
+    /** SNIC_UDP_SEND_FROM_SOCKET_REQ */
+    typedef struct
+    {
+        unsigned char cmd_sid;
+        unsigned char seq;
+        unsigned char remote_ip[4];
+        unsigned char remote_port[2];
+        unsigned char socket_id;
+        unsigned char connection_mode;
+        unsigned char payload_len[2];
+    }tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T;
+
     /** SNIC_UDP_START_RECV_REQ */
     typedef struct
     {
@@ -367,12 +390,21 @@
     */
     static C_SNIC_Core *getInstance();
 
+    /** Mutex lock of API calls
+    */
+    void lockAPI( void );
+
+    /** Mutex unlock of API calls
+    */
+    void unlockAPI( void );
+
 private:
     static C_SNIC_Core        *mInstance_p;
     Thread                    *mUartRecvThread_p;
     Thread                    *mUartRecvDispatchThread_p;
     RawSerial                 *mUart_p;
     Mutex                      mUartMutex;
+    Mutex                      mAPIMutex;
     
 //    DigitalInOut             mModuleReset;
     C_SNIC_UartCommandManager *mUartCommand_p;
@@ -397,4 +429,4 @@
     static void uartRecvDispatchThread( void const *args_p );
 };
 
-#endif
\ No newline at end of file
+#endif
--- a/SNIC/SNIC_UartCommandManager.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC/SNIC_UartCommandManager.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -160,6 +160,8 @@
     
     // Get socket id from payload
     socket_id = payload_p[2];
+    
+//  DEBUG_PRINT("bufferredPacket socket id:%d\r\n", socket_id);
     // Get Connection information
     C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id );
     if( con_info_p == NULL )
@@ -175,20 +177,30 @@
     
     // Get receive length from payload
     recv_len= ((payload_p[3]<<8) & 0xFF00) | payload_p[4];
-    for( int i = 0; i < recv_len; i++ )
+    
+    while( con_info_p->is_receive_complete == false )
+    {
+        Thread::yield();
+    }
+    
+//  DEBUG_PRINT("bufferredPacket recv_len:%d\r\n", recv_len);
+    int i;
+    for(i = 0; i < recv_len; i++ )
     {
         if( con_info_p->recvbuf_p->isFull() )
         {
             DEBUG_PRINT("Receive buffer is full.\r\n");
             break;
         }
-        
         // Add to receive buffer
         con_info_p->recvbuf_p->queue( payload_p[5+i] );
     }
+    //DEBUG_PRINT("###Receive queue[%d]\r\n", i);
     con_info_p->mutex.lock();
+    con_info_p->is_receive_complete = false;
     con_info_p->is_received = true;
     con_info_p->mutex.unlock();
+//  Thread::yield();
 }
 
 void C_SNIC_UartCommandManager::connectedTCPClient( unsigned char *payload_p, int payload_len )
--- a/SNIC/SNIC_UartMsgUtil.h	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC/SNIC_UartMsgUtil.h	Fri Nov 14 00:52:31 2014 +0000
@@ -55,7 +55,7 @@
 #define UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ         0x13    // Create UDP socket
 #define UART_CMD_SID_SNIC_UDP_START_RECV_REQ            0x14    // Start UDP receive on socket
 #define UART_CMD_SID_SNIC_UDP_SIMPLE_SEND_REQ           0x15    // Send UDP packet
-#define UART_CMD_SID_SNIC_UDP_SEND_ FROM_SOCKET_REQ     0x16    // Send UDP packet from socket
+#define UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_REQ      0x16    // Send UDP packet from socket
 #define UART_CMD_SID_SNIC_HTTP_REQ                      0x17    // Send HTTP request
 #define UART_CMD_SID_SNIC_HTTP_MORE_REQ                 0x18    // Send HTTP more data request
 #define UART_CMD_SID_SNIC_HTTPS_REQ                     0x19    // Send HTTPS request
--- a/SNIC_WifiInterface.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC_WifiInterface.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -211,15 +211,21 @@
     int ret;
     // Wait UART response
     ret = uartCmdMgr_p->wait();
-    if( ret != 0 )
+    if(uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED)
+    {
+        DEBUG_PRINT( "Already connected\r\n" );
+    }
+    else
     {
-        DEBUG_PRINT( "join failed\r\n" );
-        snic_core_p->freeCmdBuf( payload_buf_p );
-        return -1;
+        if( ret != 0 )
+        {
+            DEBUG_PRINT( "join failed\r\n" );
+            snic_core_p->freeCmdBuf( payload_buf_p );
+            return -1;
+        }
     }
     
-    if( (uartCmdMgr_p->getCommandStatus() != 0) && 
-        (uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) )
+    if(uartCmdMgr_p->getCommandStatus() != 0)
     {
         DEBUG_PRINT("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
--- a/SNIC_WifiInterface.h	Fri Sep 19 01:46:17 2014 +0000
+++ b/SNIC_WifiInterface.h	Fri Nov 14 00:52:31 2014 +0000
@@ -45,7 +45,7 @@
         @param rts mbed pin to use for rts line of Serial interface
         @param reset reset pin of the wifi module
         @param alarm alarm pin of the wifi module (default: NC)
-        @param baud baud rate of Serial interface (default: 9600)
+        @param baud baud rate of Serial interface (default: 115200)
     */
     C_SNIC_WifiInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
     virtual ~C_SNIC_WifiInterface();
@@ -92,7 +92,7 @@
         @note   This function is blocked until a returns.
                 When you use it by UI thread, be careful. 
                 Scan results will be notified by asynchronous callback function.
-                If there is no continuity data, result_handler_p will be set NULL..
+                If there is no continuity data, scan_result will be set NULL..
     */
     int scan( const char *ssid_p, unsigned char *bssid_p
             ,void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) );
--- a/Socket/Socket.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/Socket/Socket.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -49,11 +49,14 @@
 {
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
+    
+    FUNC_IN();
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         DEBUG_PRINT("socket close payload_buf NULL\r\n");
+        FUNC_OUT();
         return -1;
     }
 
@@ -80,6 +83,7 @@
     {
         DEBUG_PRINT( "socket close failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf );
+        FUNC_OUT();
         return -1;
     }
     
@@ -87,10 +91,11 @@
     {
         DEBUG_PRINT("socket close status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf );
+        FUNC_OUT();
         return -1;
     }
     snic_core_p->freeCmdBuf( payload_buf );
-
+    FUNC_OUT();
     return 0;
 }
 
@@ -98,11 +103,14 @@
 {
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
+    
+    FUNC_IN();
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         DEBUG_PRINT("createSocket payload_buf NULL\r\n");
+        FUNC_OUT();
         return -1;
     }
 
@@ -141,6 +149,7 @@
     {
         DEBUG_PRINT( "createSocket failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf );
+        FUNC_OUT();
         return -1;
     }
 
@@ -148,11 +157,12 @@
     {
         DEBUG_PRINT("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf );
+        FUNC_OUT();
         return -1;
     }
     mSocketID = payload_buf->buf[3];
     snic_core_p->freeCmdBuf( payload_buf );
-
+    FUNC_OUT();
     return 0;
 }
 
@@ -223,7 +233,6 @@
     }
 
     snic_core_p->freeCmdBuf( payload_buf );
-    
     return ip_addr;
 }
 
--- a/Socket/TCPSocketConnection.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/Socket/TCPSocketConnection.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -35,11 +35,13 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
 
+    FUNC_IN();
     // Socket create
     ret = createSocket();
     if( ret != 0 )
     {
         DEBUG_PRINT("createSocket error : %d\r\n", ret);
+        FUNC_OUT();
         return -1;
     }
 
@@ -47,7 +49,8 @@
     //lcd_printf("connect to [%s](%08x)\r\n", host_p, ip_addr);
     if( ( ip_addr == 0) || (ip_addr == -1) )
     {
-          DEBUG_PRINT("connect resolveHostName failed\r\n");
+        DEBUG_PRINT("connect resolveHostName failed\r\n");
+        FUNC_OUT();
         return -1;
     }
         
@@ -56,6 +59,7 @@
     if( payload_buf_p == NULL )
     {
         DEBUG_PRINT("connect payload_buf_p NULL\r\n");
+        FUNC_OUT();
         return -1;
     }
     
@@ -94,6 +98,7 @@
     {
         DEBUG_PRINT( "connect failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
     
@@ -101,6 +106,7 @@
     {
         DEBUG_PRINT("connect status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
 
@@ -115,7 +121,7 @@
     }
     con_info_p->is_connected = true;
     con_info_p->is_received  = false;
-
+    FUNC_OUT();
     return 0;
 }
 
@@ -131,11 +137,13 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
 
+    FUNC_IN();
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
     if( payload_buf_p == NULL )
     {
         DEBUG_PRINT("connect payload_buf_p NULL\r\n");
+        FUNC_OUT();
         return -1;
     }
     
@@ -168,6 +176,7 @@
     {
         DEBUG_PRINT( "send failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
     
@@ -175,11 +184,13 @@
     {
         DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
     snic_core_p->freeCmdBuf( payload_buf_p );
 
     // SNIC_SEND_FROM_SOCKET_REQ
+    FUNC_OUT();
     return 0;
 }
 
@@ -192,9 +203,11 @@
 {
     int i = 0;
     
+    FUNC_IN();
     if( (data_p == NULL) || (length < 1) )
     {
         DEBUG_PRINT("TCPSocketConnection::receive parameter error\r\n");
+        FUNC_OUT();
         return -1;
     }
     
@@ -204,6 +217,7 @@
     if( con_info_p->recvbuf_p == NULL )
     {
         DEBUG_PRINT("TCPSocketConnection::receive Conncection info error\r\n");
+        FUNC_OUT();
         return -1;
     }
 
@@ -211,15 +225,14 @@
     if( con_info_p->is_connected == false )
     {
         DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", mSocketID);
+        FUNC_OUT();
         return -1;
     }
-    
-    if( con_info_p->is_received == false )
+    con_info_p->is_receive_complete = true;
+    while( con_info_p->is_received == false )
     {
-//        DEBUG_PRINT(" Socket id \"%d\" is not received\r\n", mSocketID);
-        return 0;
+        Thread::yield();
     }
-    
     // Get packet data from buffer for receive.
     for (i = 0; i < length; i ++) 
     {
@@ -236,10 +249,12 @@
         con_info_p->mutex.unlock();
     }
 
+    FUNC_OUT();
     return i;
 }
 
 void TCPSocketConnection::setAcceptSocket( int socket_id )
 {
+    FUNC_IN();
     mSocketID = socket_id;
 }
--- a/Socket/TCPSocketServer.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/Socket/TCPSocketServer.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -37,12 +37,14 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
     
+    snic_core_p->lockAPI();
     // Get local ip address.
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
     if( payload_buf_p == NULL )
     {
         DEBUG_PRINT("bind payload_buf_p NULL\r\n");
+        snic_core_p->unlockAPI();
         return -1;
     }
 
@@ -65,6 +67,7 @@
     {
         DEBUG_PRINT( "bind failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        snic_core_p->unlockAPI();
         return -1;
     }
     
@@ -72,10 +75,12 @@
     {
         DEBUG_PRINT("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        snic_core_p->unlockAPI();
         return -1;
     }
     
     snic_core_p->freeCmdBuf( payload_buf_p );
+    snic_core_p->unlockAPI();
     
     unsigned int local_addr = (payload_buf_p->buf[9]  << 24)
                             | (payload_buf_p->buf[10] << 16)
@@ -98,11 +103,13 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
     
+    snic_core_p->lockAPI();
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
     if( payload_buf_p == NULL )
     {
         DEBUG_PRINT("listen payload_buf_p NULL\r\n");
+        snic_core_p->unlockAPI();
         return -1;
     }
 
@@ -132,6 +139,7 @@
     {
         DEBUG_PRINT( "listen failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        snic_core_p->unlockAPI();
         return -1;
     }
     
@@ -139,10 +147,12 @@
     {
         DEBUG_PRINT("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        snic_core_p->unlockAPI();
         return -1;
     }
 
     snic_core_p->freeCmdBuf( payload_buf_p );
+    snic_core_p->unlockAPI();
     return 0;
 }
 
--- a/Socket/UDPSocket.cpp	Fri Sep 19 01:46:17 2014 +0000
+++ b/Socket/UDPSocket.cpp	Fri Nov 14 00:52:31 2014 +0000
@@ -40,12 +40,14 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
     
+    FUNC_IN();
     // Get local ip address.
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
     if( payload_buf_p == NULL )
     {
         DEBUG_PRINT("UDP bind payload_buf_p NULL\r\n");
+        FUNC_OUT();
         return -1;
     }
 
@@ -68,6 +70,7 @@
     {
         DEBUG_PRINT( "UDP bind failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
     
@@ -75,6 +78,7 @@
     {
         DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
     
@@ -107,6 +111,7 @@
     {
         DEBUG_PRINT( "UDP bind failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
 
@@ -114,10 +119,21 @@
     {
         DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
     mSocketID = payload_buf_p->buf[3];
     
+    // Initialize connection information
+    C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
+    if( con_info_p->recvbuf_p == NULL )
+    {
+        DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID);
+        con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
+    }
+    con_info_p->is_connected = true;
+    con_info_p->is_received  = false;
+
     C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req;
     
     // Make request
@@ -139,6 +155,7 @@
     {
         DEBUG_PRINT( "UDP recv start failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
 
@@ -146,11 +163,12 @@
     {
         DEBUG_PRINT("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
         return -1;
     }
 
     snic_core_p->freeCmdBuf( payload_buf_p );
-    
+    FUNC_OUT();
     return 0;
 }
 
@@ -160,28 +178,51 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
 
+    osThreadId tid = Thread::gettid();
+    
+//  pc.printf("send[%08x] len:%d(%04x)\r\n", tid, length, length);
+    
+#if 0   // TODO: Not wait for command response(Tentative)    
+    snic_core_p->lockAPI();
+#endif
+    FUNC_IN();
+
+#if 0   // TODO: Not wait for command response(Tentative)    
     // Get buffer for response payload from MemoryPool
     tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf();
     if( payload_buf_p == NULL )
     {
         DEBUG_PRINT("connect payload_buf_p NULL\r\n");
+        FUNC_OUT();
+        snic_core_p->unlockAPI();
         return -1;
     }
-    
-    C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T req;
+#endif
+
+    C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T req;
     // Make request
-    req.cmd_sid       = UART_CMD_SID_SNIC_UDP_SIMPLE_SEND_REQ;
+    req.cmd_sid       = UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_REQ;
     req.seq           = mUartRequestSeq++;
 
     int addr_temp;
     addr_temp = C_SNIC_UartMsgUtil::addrToInteger( remote.get_address() );
     C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.remote_ip );
-    req.remote_port[0]= ( (remote.get_port() & 0xFF00) >> 8 );
-    req.remote_port[1]= (remote.get_port() & 0xFF);
-    req.payload_len[0]= ( (length & 0xFF00) >> 8 );
-    req.payload_len[1]= (length & 0xFF);
+    req.remote_port[0]  = ( (remote.get_port() & 0xFF00) >> 8 );
+    req.remote_port[1]  = (remote.get_port() & 0xFF);
+    req.payload_len[0]  = ( (length & 0xFF00) >> 8 );
+    req.payload_len[1]  = (length & 0xFF);
+    req.socket_id       = mSocketID;
+    req.connection_mode = 1;
     
-    int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T);
+    // Initialize connection information
+    C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
+    if( con_info_p != NULL )
+    {
+        con_info_p->from_ip   = addr_temp;
+        con_info_p->from_port = remote.get_port();
+    }
+
+    int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T);
     
     char *send_buf_p = getSocketSendBuf();
     memcpy( send_buf_p, &req, req_size );
@@ -189,19 +230,22 @@
     
     unsigned char *command_array_p = 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 *)send_buf_p
-                            , req_size + length, payload_buf_p->buf, command_array_p );
+
+    // Make all command request
+    command_len = C_SNIC_UartMsgUtil::makeRequest( UART_CMD_ID_SNIC, (unsigned char *)send_buf_p, req_size + length, command_array_p );
 
     // Send uart command request
     snic_core_p->sendUart( command_len, command_array_p );
 
+#if 0   // TODO: Not wait for command response(Tentative)
     // Wait UART response
     int ret = uartCmdMgr_p->wait();
     if( ret != 0 )
     {
         DEBUG_PRINT( "send failed\r\n" );
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
+        snic_core_p->unlockAPI();
         return -1;
     }
     
@@ -209,34 +253,43 @@
     {
         DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         snic_core_p->freeCmdBuf( payload_buf_p );
+        FUNC_OUT();
+        snic_core_p->unlockAPI();
         return -1;
     }
     snic_core_p->freeCmdBuf( payload_buf_p );
-
+#endif
+    
+    FUNC_OUT();
+#if 0   // TODO: Not wait for command response(Tentative)    
+    snic_core_p->unlockAPI();
+#endif
     // SNIC_SEND_FROM_SOCKET_REQ
-    return 0;
+    wait(0.05);
+
+    return length;
+//    return 0;
 }
 
 // -1 if unsuccessful, else number of bytes received
 int UDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length)
 {
+    FUNC_IN();
     if( (data_p == NULL) || (length < 1) )
     {
         DEBUG_PRINT("UDPSocket::receiveFrom parameter error\r\n");
+        FUNC_OUT();
         return -1;
     }
-    
+
     C_SNIC_Core                    *snic_core_p  = C_SNIC_Core::getInstance();
     // Initialize connection information
-    C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = snic_core_p->getUdpRecvInfo( mSocketID );
+    C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID );
     if( con_info_p->recvbuf_p == NULL )
     {
-//        DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n");
-        return 0;
-    }
-    if( con_info_p->is_received == false )
-    {
-        return 0;
+        DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n");
+        FUNC_OUT();
+        return -1;
     }
 
     char remote_ip[20] = {'\0'};
@@ -247,8 +300,15 @@
         , (con_info_p->from_ip)      & 0x000000ff );
     remote.set_address( remote_ip, con_info_p->from_port );
     
+    con_info_p->mutex.lock();
+    con_info_p->is_receive_complete = true;
+    con_info_p->mutex.unlock();
+    while( con_info_p->is_received == false )
+    {
+        Thread::yield();
+    }
+    // Get packet data from buffer for receive.
     int i;
-    // Get packet data from buffer for receive.
     for (i = 0; i < length; i ++) 
     {
         if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
@@ -256,13 +316,12 @@
             break;
         }
     }
-
     if( con_info_p->recvbuf_p->isEmpty() )
     {
         con_info_p->mutex.lock();
         con_info_p->is_received = false;
         con_info_p->mutex.unlock();
     }
-
+    FUNC_OUT();
     return i;
 }