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:
29:6a0ba999597d
Parent:
28:b796031f6519
Child:
31:15c22824cc46
--- a/SNIC_WifiInterface.cpp	Tue Apr 01 07:19:19 2014 +0000
+++ b/SNIC_WifiInterface.cpp	Mon May 26 05:17:28 2014 +0000
@@ -13,7 +13,6 @@
 #include "SNIC_WifiInterface.h"
 #include "SNIC_UartMsgUtil.h"
 
-using namespace murata_wifi;
 
 C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud)
 {
@@ -34,12 +33,15 @@
     C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
     C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
     
+    /* Module reset */
+    snic_core_p->resetModule( mModuleReset );
+    
     /* Initialize UART */
     snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud );
 
     /* Initialize SNIC API */
     // Get buffer for response payload from MemoryPool
-    C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("snic_init payload_buf NULL\r\n");
@@ -53,7 +55,7 @@
     req.buf_size[0] = 0x08;
     req.buf_size[1] = 0x00;
 
-    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
@@ -88,7 +90,7 @@
     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();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("getFWVersion payload_buf NULL\r\n");
@@ -100,7 +102,7 @@
     req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ;
     req.seq     = mUartRequestSeq++;
     
-    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_GEN, req.cmd_sid, (unsigned char *)&req
@@ -129,6 +131,7 @@
     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)
 {
@@ -150,16 +153,15 @@
     }
     
     // Get buffer for response payload from MemoryPool
-    C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("connect payload_buf NULL\r\n");
         return -1;
     }
 
-    unsigned char buf[UART_REQUEST_PAYLOAD_MAX];
+    unsigned char *buf = &gCONNECT_BUF[0];
     unsigned int  buf_len = 0;
-    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
     unsigned int  command_len;
 
     memset( buf, 0, UART_REQUEST_PAYLOAD_MAX );
@@ -189,6 +191,7 @@
         }
     }
 
+    unsigned char *command_array = snic_core_p->getCommandBuf();
     // Preparation of command
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf
                         , buf_len, payload_buf->buf, command_array );
@@ -205,8 +208,10 @@
         snic_core_p->freeCmdBuf( payload_buf );
         return -1;
     }
+    printf("join OK\r\n");
     
-    if( uartCmdMgr_p->getCommandStatus() != 0 )
+    if( (uartCmdMgr_p->getCommandStatus() != 0) && 
+        (uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) )
     {
         printf("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
         ret = -1;
@@ -222,7 +227,7 @@
     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();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("disconnect payload_buf NULL\r\n");
@@ -234,7 +239,7 @@
     req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ;
     req.seq = mUartRequestSeq++;
     
-    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_WIFI, req.cmd_sid, (unsigned char *)&req
@@ -269,7 +274,7 @@
     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();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("scan payload_buf NULL\r\n");
@@ -309,7 +314,7 @@
     }
     buf_len++;
 
-    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_WIFI, req.cmd_sid, (unsigned char *)&req
@@ -356,7 +361,7 @@
     }
     
     // Get buffer for response payload from MemoryPool
-    C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("wifi_on payload_buf NULL\r\n");
@@ -369,7 +374,7 @@
     req.seq = mUartRequestSeq++;
     memcpy( req.country, country_p, COUNTRYC_CODE_LENTH );
     
-    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_WIFI, req.cmd_sid, (unsigned char *)&req
@@ -404,7 +409,7 @@
     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();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("wifi_off payload_buf NULL\r\n");
@@ -416,7 +421,7 @@
     req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ;
     req.seq = mUartRequestSeq++;
     
-    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_WIFI, req.cmd_sid, (unsigned char *)&req
@@ -456,7 +461,7 @@
     }
     
     // Get buffer for response payload from MemoryPool
-    C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("getRssi payload_buf NULL\r\n");
@@ -469,7 +474,7 @@
     req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ;
     req.seq     = mUartRequestSeq++;
     
-    unsigned char  command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned char *command_array = snic_core_p->getCommandBuf();
     unsigned int   command_len;
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
                         , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf->buf, command_array );
@@ -505,7 +510,7 @@
     }
     
     // Get buffer for response payload from MemoryPool
-    C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
     if( payload_buf == NULL )
     {
         printf("getWifiStatus payload_buf NULL\r\n");
@@ -518,7 +523,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;
     command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
                         , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf->buf, command_array );