SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Tue Jun 24 06:24:23 2014 +0000
Revision:
37:f3a2053627c2
Parent:
36:f33fcf5975ab
Child:
38:f13e4e563d65
Fixed bugs that was found in the API test.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 14:54378c96d285 1 /******************* Murata Manufacturing Co.,Ltd. 2014 *****************
kishino 14:54378c96d285 2 *
kishino 14:54378c96d285 3 * Filename: SNIC_WifiInterface.cpp
kishino 14:54378c96d285 4 *
kishino 14:54378c96d285 5 * Purpose: This module has implementation of API for SNIC UART of Wi-Fi.
kishino 14:54378c96d285 6 *
kishino 14:54378c96d285 7 * $Author: kishino $
kishino 14:54378c96d285 8 *
kishino 14:54378c96d285 9 * $Date: 2014/03/26 $
kishino 14:54378c96d285 10 *
kishino 14:54378c96d285 11 * $Revision: 0.0.0.1 $
kishino 14:54378c96d285 12 * ***********************************************************************/
kishino 12:0254eaccfda2 13 #include "SNIC_WifiInterface.h"
kishino 12:0254eaccfda2 14 #include "SNIC_UartMsgUtil.h"
kishino 12:0254eaccfda2 15
kishino 36:f33fcf5975ab 16 #define UART_CONNECT_BUF_SIZE 512
kishino 36:f33fcf5975ab 17 unsigned char gCONNECT_BUF[UART_CONNECT_BUF_SIZE];
kishino 12:0254eaccfda2 18
kishino 12:0254eaccfda2 19 C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud)
kishino 12:0254eaccfda2 20 {
kishino 12:0254eaccfda2 21 mUART_tx = tx;
kishino 12:0254eaccfda2 22 mUART_rx = rx;
kishino 12:0254eaccfda2 23 mUART_cts = cts;
kishino 12:0254eaccfda2 24 mUART_rts = rts;;
kishino 12:0254eaccfda2 25 mUART_baud = baud;
kishino 12:0254eaccfda2 26 mModuleReset = reset;
kishino 12:0254eaccfda2 27 }
kishino 12:0254eaccfda2 28
kishino 26:f2e1030964e4 29 C_SNIC_WifiInterface::~C_SNIC_WifiInterface()
kishino 26:f2e1030964e4 30 {
kishino 26:f2e1030964e4 31 }
kishino 26:f2e1030964e4 32
kishino 12:0254eaccfda2 33 int C_SNIC_WifiInterface::init()
kishino 24:987e412ae879 34 {
kishino 24:987e412ae879 35 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 36 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 37
kishino 37:f3a2053627c2 38 /* Initialize UART */
kishino 37:f3a2053627c2 39 snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud );
kishino 37:f3a2053627c2 40
kishino 29:6a0ba999597d 41 /* Module reset */
kishino 29:6a0ba999597d 42 snic_core_p->resetModule( mModuleReset );
kishino 29:6a0ba999597d 43
kishino 37:f3a2053627c2 44 wait(1);
kishino 12:0254eaccfda2 45 /* Initialize SNIC API */
kishino 12:0254eaccfda2 46 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 47 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 48 if( payload_buf == NULL )
kishino 12:0254eaccfda2 49 {
kishino 12:0254eaccfda2 50 printf("snic_init payload_buf NULL\r\n");
kishino 12:0254eaccfda2 51 return -1;
kishino 12:0254eaccfda2 52 }
kishino 12:0254eaccfda2 53
kishino 22:a9ec0cad4f84 54 C_SNIC_Core::tagSNIC_INIT_REQ_T req;
kishino 12:0254eaccfda2 55 // Make request
kishino 12:0254eaccfda2 56 req.cmd_sid = UART_CMD_SID_SNIC_INIT_REQ;
kishino 12:0254eaccfda2 57 req.seq = mUartRequestSeq++;
kishino 28:b796031f6519 58 req.buf_size[0] = 0x08;
kishino 28:b796031f6519 59 req.buf_size[1] = 0x00;
kishino 12:0254eaccfda2 60
kishino 29:6a0ba999597d 61 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 62 unsigned int command_len;
kishino 12:0254eaccfda2 63 // Preparation of command
kishino 24:987e412ae879 64 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 65 , sizeof(C_SNIC_Core::tagSNIC_INIT_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 66
kishino 12:0254eaccfda2 67 // Send uart command request
kishino 24:987e412ae879 68 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 69
kishino 12:0254eaccfda2 70 int ret;
kishino 12:0254eaccfda2 71 // Wait UART response
kishino 16:6100acfeb1f1 72 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 73 if( ret != 0 )
kishino 12:0254eaccfda2 74 {
kishino 12:0254eaccfda2 75 printf( "snic_init failed\r\n" );
kishino 24:987e412ae879 76 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 77 return -1;
kishino 12:0254eaccfda2 78 }
kishino 12:0254eaccfda2 79
kishino 16:6100acfeb1f1 80 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 12:0254eaccfda2 81 {
kishino 16:6100acfeb1f1 82 printf("snic_init status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 12:0254eaccfda2 83 ret = -1;
kishino 12:0254eaccfda2 84 }
kishino 24:987e412ae879 85 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 86
kishino 12:0254eaccfda2 87 return ret;
kishino 12:0254eaccfda2 88 }
kishino 12:0254eaccfda2 89
kishino 12:0254eaccfda2 90 int C_SNIC_WifiInterface::getFWVersion( unsigned char *version_p )
kishino 12:0254eaccfda2 91 {
kishino 24:987e412ae879 92 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 93 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 94
kishino 12:0254eaccfda2 95 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 96 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 97 if( payload_buf == NULL )
kishino 12:0254eaccfda2 98 {
kishino 12:0254eaccfda2 99 printf("getFWVersion payload_buf NULL\r\n");
kishino 12:0254eaccfda2 100 return -1;
kishino 12:0254eaccfda2 101 }
kishino 12:0254eaccfda2 102
kishino 22:a9ec0cad4f84 103 C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T req;
kishino 12:0254eaccfda2 104 // Make request
kishino 12:0254eaccfda2 105 req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ;
kishino 12:0254eaccfda2 106 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 107
kishino 29:6a0ba999597d 108 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 109 unsigned int command_len;
kishino 12:0254eaccfda2 110 // Preparation of command
kishino 24:987e412ae879 111 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_GEN, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 112 , sizeof(C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 113
kishino 12:0254eaccfda2 114 int ret;
kishino 12:0254eaccfda2 115
kishino 12:0254eaccfda2 116 // Send uart command request
kishino 24:987e412ae879 117 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 118
kishino 12:0254eaccfda2 119 // Wait UART response
kishino 16:6100acfeb1f1 120 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 121 if( ret != 0 )
kishino 12:0254eaccfda2 122 {
kishino 12:0254eaccfda2 123 printf( "getFWversion failed\r\n" );
kishino 24:987e412ae879 124 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 125 return -1;
kishino 12:0254eaccfda2 126 }
kishino 12:0254eaccfda2 127
kishino 16:6100acfeb1f1 128 if( uartCmdMgr_p->getCommandStatus() == 0 )
kishino 12:0254eaccfda2 129 {
kishino 12:0254eaccfda2 130 unsigned char version_len = payload_buf->buf[3];
kishino 12:0254eaccfda2 131 memcpy( version_p, &payload_buf->buf[4], version_len );
kishino 12:0254eaccfda2 132 }
kishino 24:987e412ae879 133 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 134 return 0;
kishino 12:0254eaccfda2 135 }
kishino 12:0254eaccfda2 136
kishino 12:0254eaccfda2 137 int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type
kishino 12:0254eaccfda2 138 , const char *sec_key_p, unsigned char sec_key_len)
kishino 12:0254eaccfda2 139 {
kishino 24:987e412ae879 140 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 141 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 142
kishino 12:0254eaccfda2 143 // Parameter check(SSID)
kishino 12:0254eaccfda2 144 if( (ssid_p == NULL) || (ssid_len == 0) )
kishino 12:0254eaccfda2 145 {
kishino 12:0254eaccfda2 146 printf( "connect failed [ parameter NG:SSID ]\r\n" );
kishino 12:0254eaccfda2 147 return -1;
kishino 12:0254eaccfda2 148 }
kishino 12:0254eaccfda2 149
kishino 12:0254eaccfda2 150 // Parameter check(Security key)
kishino 12:0254eaccfda2 151 if( (sec_type != e_SEC_OPEN) && ( (sec_key_len == 0) || (sec_key_p == NULL) ) )
kishino 12:0254eaccfda2 152 {
kishino 12:0254eaccfda2 153 printf( "connect failed [ parameter NG:Security key ]\r\n" );
kishino 12:0254eaccfda2 154 return -1;
kishino 12:0254eaccfda2 155 }
kishino 12:0254eaccfda2 156
kishino 12:0254eaccfda2 157 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 158 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 159 if( payload_buf == NULL )
kishino 12:0254eaccfda2 160 {
kishino 12:0254eaccfda2 161 printf("connect payload_buf NULL\r\n");
kishino 12:0254eaccfda2 162 return -1;
kishino 12:0254eaccfda2 163 }
kishino 12:0254eaccfda2 164
kishino 29:6a0ba999597d 165 unsigned char *buf = &gCONNECT_BUF[0];
kishino 12:0254eaccfda2 166 unsigned int buf_len = 0;
kishino 12:0254eaccfda2 167 unsigned int command_len;
kishino 12:0254eaccfda2 168
kishino 36:f33fcf5975ab 169 memset( buf, 0, UART_CONNECT_BUF_SIZE );
kishino 12:0254eaccfda2 170 // Make request
kishino 12:0254eaccfda2 171 buf[0] = UART_CMD_SID_WIFI_JOIN_REQ;
kishino 12:0254eaccfda2 172 buf_len++;
kishino 12:0254eaccfda2 173 buf[1] = mUartRequestSeq++;
kishino 12:0254eaccfda2 174 buf_len++;
kishino 12:0254eaccfda2 175 // SSID
kishino 12:0254eaccfda2 176 memcpy( &buf[2], ssid_p, ssid_len );
kishino 12:0254eaccfda2 177 buf_len += ssid_len;
kishino 12:0254eaccfda2 178 buf_len++;
kishino 12:0254eaccfda2 179
kishino 12:0254eaccfda2 180 // Security mode
kishino 12:0254eaccfda2 181 buf[ buf_len ] = (unsigned char)sec_type;
kishino 12:0254eaccfda2 182 buf_len++;
kishino 12:0254eaccfda2 183
kishino 12:0254eaccfda2 184 // Security key
kishino 12:0254eaccfda2 185 if( sec_type != e_SEC_OPEN )
kishino 12:0254eaccfda2 186 {
kishino 12:0254eaccfda2 187 buf[ buf_len ] = sec_key_len;
kishino 12:0254eaccfda2 188 buf_len++;
kishino 12:0254eaccfda2 189 if( sec_key_len > 0 )
kishino 12:0254eaccfda2 190 {
kishino 12:0254eaccfda2 191 memcpy( &buf[buf_len], sec_key_p, sec_key_len );
kishino 12:0254eaccfda2 192 buf_len += sec_key_len;
kishino 12:0254eaccfda2 193 }
kishino 12:0254eaccfda2 194 }
kishino 12:0254eaccfda2 195
kishino 29:6a0ba999597d 196 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 197 // Preparation of command
kishino 24:987e412ae879 198 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf
kishino 12:0254eaccfda2 199 , buf_len, payload_buf->buf, command_array );
kishino 12:0254eaccfda2 200
kishino 12:0254eaccfda2 201 // Send uart command request
kishino 24:987e412ae879 202 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 203
kishino 12:0254eaccfda2 204 int ret;
kishino 12:0254eaccfda2 205 // Wait UART response
kishino 16:6100acfeb1f1 206 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 207 if( ret != 0 )
kishino 12:0254eaccfda2 208 {
kishino 12:0254eaccfda2 209 printf( "join failed\r\n" );
kishino 24:987e412ae879 210 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 211 return -1;
kishino 12:0254eaccfda2 212 }
kishino 12:0254eaccfda2 213
kishino 29:6a0ba999597d 214 if( (uartCmdMgr_p->getCommandStatus() != 0) &&
kishino 29:6a0ba999597d 215 (uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) )
kishino 12:0254eaccfda2 216 {
kishino 16:6100acfeb1f1 217 printf("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 12:0254eaccfda2 218 ret = -1;
kishino 12:0254eaccfda2 219 }
kishino 24:987e412ae879 220 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 221
kishino 12:0254eaccfda2 222 return ret;
kishino 12:0254eaccfda2 223 }
kishino 12:0254eaccfda2 224
kishino 12:0254eaccfda2 225 int C_SNIC_WifiInterface::disconnect()
kishino 12:0254eaccfda2 226 {
kishino 24:987e412ae879 227 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 228 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 229
kishino 12:0254eaccfda2 230 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 231 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 232 if( payload_buf == NULL )
kishino 12:0254eaccfda2 233 {
kishino 12:0254eaccfda2 234 printf("disconnect payload_buf NULL\r\n");
kishino 12:0254eaccfda2 235 return -1;
kishino 12:0254eaccfda2 236 }
kishino 12:0254eaccfda2 237
kishino 22:a9ec0cad4f84 238 C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T req;
kishino 12:0254eaccfda2 239 // Make request
kishino 12:0254eaccfda2 240 req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ;
kishino 12:0254eaccfda2 241 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 242
kishino 29:6a0ba999597d 243 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 244 unsigned int command_len;
kishino 12:0254eaccfda2 245 // Preparation of command
kishino 24:987e412ae879 246 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 247 , sizeof(C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 248
kishino 12:0254eaccfda2 249 // Send uart command request
kishino 24:987e412ae879 250 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 251
kishino 12:0254eaccfda2 252 int ret;
kishino 12:0254eaccfda2 253 // Wait UART response
kishino 16:6100acfeb1f1 254 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 255 if( ret != 0 )
kishino 12:0254eaccfda2 256 {
kishino 12:0254eaccfda2 257 printf( "disconnect failed\r\n" );
kishino 24:987e412ae879 258 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 259 return -1;
kishino 12:0254eaccfda2 260 }
kishino 12:0254eaccfda2 261
kishino 16:6100acfeb1f1 262 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 12:0254eaccfda2 263 {
kishino 16:6100acfeb1f1 264 printf("disconnect status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 12:0254eaccfda2 265 ret = -1;
kishino 12:0254eaccfda2 266 }
kishino 24:987e412ae879 267 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 268 return ret;
kishino 12:0254eaccfda2 269 }
kishino 12:0254eaccfda2 270
kishino 12:0254eaccfda2 271 int C_SNIC_WifiInterface::scan( const char *ssid_p, unsigned char *bssid_p
kishino 12:0254eaccfda2 272 , void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) )
kishino 12:0254eaccfda2 273 {
kishino 24:987e412ae879 274 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 275 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 276
kishino 12:0254eaccfda2 277 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 278 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 279 if( payload_buf == NULL )
kishino 12:0254eaccfda2 280 {
kishino 12:0254eaccfda2 281 printf("scan payload_buf NULL\r\n");
kishino 12:0254eaccfda2 282 return -1;
kishino 12:0254eaccfda2 283 }
kishino 12:0254eaccfda2 284
kishino 22:a9ec0cad4f84 285 C_SNIC_Core::tagWIFI_SCAN_REQ_T req;
kishino 12:0254eaccfda2 286 unsigned int buf_len = 0;
kishino 12:0254eaccfda2 287
kishino 22:a9ec0cad4f84 288 memset( &req, 0, sizeof(C_SNIC_Core::tagWIFI_SCAN_REQ_T) );
kishino 12:0254eaccfda2 289 // Make request
kishino 12:0254eaccfda2 290 req.cmd_sid = UART_CMD_SID_WIFI_SCAN_REQ;
kishino 12:0254eaccfda2 291 buf_len++;
kishino 12:0254eaccfda2 292 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 293 buf_len++;
kishino 12:0254eaccfda2 294
kishino 12:0254eaccfda2 295 // Set scan type(Active scan)
kishino 12:0254eaccfda2 296 req.scan_type = 0;
kishino 12:0254eaccfda2 297 buf_len++;
kishino 12:0254eaccfda2 298 // Set bss type(any)
kishino 12:0254eaccfda2 299 req.bss_type = 2;
kishino 12:0254eaccfda2 300 buf_len++;
kishino 12:0254eaccfda2 301 // Set BSSID
kishino 12:0254eaccfda2 302 if( bssid_p != NULL )
kishino 12:0254eaccfda2 303 {
kishino 12:0254eaccfda2 304 memcpy( req.bssid, bssid_p, BSSID_MAC_LENTH );
kishino 12:0254eaccfda2 305 }
kishino 12:0254eaccfda2 306 buf_len += BSSID_MAC_LENTH;
kishino 12:0254eaccfda2 307 // Set channel list(0)
kishino 12:0254eaccfda2 308 req.chan_list = 0;
kishino 12:0254eaccfda2 309 buf_len++;
kishino 12:0254eaccfda2 310 //Set SSID
kishino 12:0254eaccfda2 311 if( ssid_p != NULL )
kishino 12:0254eaccfda2 312 {
kishino 12:0254eaccfda2 313 strcpy( (char *)req.ssid, ssid_p );
kishino 12:0254eaccfda2 314 buf_len += strlen(ssid_p);
kishino 12:0254eaccfda2 315 }
kishino 12:0254eaccfda2 316 buf_len++;
kishino 12:0254eaccfda2 317
kishino 29:6a0ba999597d 318 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 319 unsigned int command_len;
kishino 12:0254eaccfda2 320 // Preparation of command
kishino 24:987e412ae879 321 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
kishino 12:0254eaccfda2 322 , buf_len, payload_buf->buf, command_array );
kishino 12:0254eaccfda2 323
kishino 12:0254eaccfda2 324 // Set scan result callback
kishino 16:6100acfeb1f1 325 uartCmdMgr_p->setScanResultHandler( result_handler_p );
kishino 12:0254eaccfda2 326
kishino 12:0254eaccfda2 327 // Send uart command request
kishino 24:987e412ae879 328 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 329
kishino 12:0254eaccfda2 330 int ret;
kishino 12:0254eaccfda2 331 // Wait UART response
kishino 16:6100acfeb1f1 332 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 333 printf( "scan wait:%d\r\n", ret );
kishino 12:0254eaccfda2 334 if( ret != 0 )
kishino 12:0254eaccfda2 335 {
kishino 12:0254eaccfda2 336 printf( "scan failed\r\n" );
kishino 24:987e412ae879 337 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 338 return -1;
kishino 12:0254eaccfda2 339 }
kishino 12:0254eaccfda2 340
kishino 16:6100acfeb1f1 341 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 12:0254eaccfda2 342 {
kishino 16:6100acfeb1f1 343 printf("scan status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 12:0254eaccfda2 344 ret = -1;
kishino 12:0254eaccfda2 345 }
kishino 12:0254eaccfda2 346
kishino 24:987e412ae879 347 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 348
kishino 12:0254eaccfda2 349 return ret;
kishino 12:0254eaccfda2 350 }
kishino 12:0254eaccfda2 351
kishino 12:0254eaccfda2 352 int C_SNIC_WifiInterface::wifi_on( const char *country_p )
kishino 12:0254eaccfda2 353 {
kishino 24:987e412ae879 354 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 355 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 356
kishino 12:0254eaccfda2 357 // Parameter check
kishino 12:0254eaccfda2 358 if( country_p == NULL )
kishino 12:0254eaccfda2 359 {
kishino 12:0254eaccfda2 360 printf("wifi_on parameter error\r\n");
kishino 12:0254eaccfda2 361 return -1;
kishino 12:0254eaccfda2 362 }
kishino 12:0254eaccfda2 363
kishino 12:0254eaccfda2 364 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 365 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 366 if( payload_buf == NULL )
kishino 12:0254eaccfda2 367 {
kishino 12:0254eaccfda2 368 printf("wifi_on payload_buf NULL\r\n");
kishino 12:0254eaccfda2 369 return -1;
kishino 12:0254eaccfda2 370 }
kishino 12:0254eaccfda2 371
kishino 22:a9ec0cad4f84 372 C_SNIC_Core::tagWIFI_ON_REQ_T req;
kishino 12:0254eaccfda2 373 // Make request
kishino 12:0254eaccfda2 374 req.cmd_sid = UART_CMD_SID_WIFI_ON_REQ;
kishino 12:0254eaccfda2 375 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 376 memcpy( req.country, country_p, COUNTRYC_CODE_LENTH );
kishino 12:0254eaccfda2 377
kishino 29:6a0ba999597d 378 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 379 unsigned int command_len;
kishino 12:0254eaccfda2 380 // Preparation of command
kishino 24:987e412ae879 381 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 382 , sizeof(C_SNIC_Core::tagWIFI_ON_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 383
kishino 12:0254eaccfda2 384 // Send uart command request
kishino 24:987e412ae879 385 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 386
kishino 12:0254eaccfda2 387 int ret;
kishino 12:0254eaccfda2 388 // Wait UART response
kishino 16:6100acfeb1f1 389 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 390 if( ret != 0 )
kishino 12:0254eaccfda2 391 {
kishino 12:0254eaccfda2 392 printf( "wifi_on failed\r\n" );
kishino 24:987e412ae879 393 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 394 return -1;
kishino 12:0254eaccfda2 395 }
kishino 12:0254eaccfda2 396
kishino 16:6100acfeb1f1 397 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 12:0254eaccfda2 398 {
kishino 16:6100acfeb1f1 399 printf("wifi_on status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 12:0254eaccfda2 400 ret = -1;
kishino 12:0254eaccfda2 401 }
kishino 24:987e412ae879 402 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 403
kishino 12:0254eaccfda2 404 return ret;
kishino 12:0254eaccfda2 405 }
kishino 12:0254eaccfda2 406
kishino 12:0254eaccfda2 407 int C_SNIC_WifiInterface::wifi_off()
kishino 12:0254eaccfda2 408 {
kishino 24:987e412ae879 409 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 410 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 411
kishino 12:0254eaccfda2 412 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 413 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 414 if( payload_buf == NULL )
kishino 12:0254eaccfda2 415 {
kishino 12:0254eaccfda2 416 printf("wifi_off payload_buf NULL\r\n");
kishino 12:0254eaccfda2 417 return -1;
kishino 12:0254eaccfda2 418 }
kishino 12:0254eaccfda2 419
kishino 22:a9ec0cad4f84 420 C_SNIC_Core::tagWIFI_OFF_REQ_T req;
kishino 12:0254eaccfda2 421 // Make request
kishino 12:0254eaccfda2 422 req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ;
kishino 12:0254eaccfda2 423 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 424
kishino 29:6a0ba999597d 425 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 426 unsigned int command_len;
kishino 12:0254eaccfda2 427 // Preparation of command
kishino 24:987e412ae879 428 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 429 , sizeof(C_SNIC_Core::tagWIFI_OFF_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 430
kishino 12:0254eaccfda2 431 // Send uart command request
kishino 24:987e412ae879 432 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 433
kishino 12:0254eaccfda2 434 int ret;
kishino 12:0254eaccfda2 435 // Wait UART response
kishino 16:6100acfeb1f1 436 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 437 if( ret != 0 )
kishino 12:0254eaccfda2 438 {
kishino 12:0254eaccfda2 439 printf( "wifi_off failed\r\n" );
kishino 24:987e412ae879 440 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 441 return -1;
kishino 12:0254eaccfda2 442 }
kishino 12:0254eaccfda2 443
kishino 16:6100acfeb1f1 444 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 12:0254eaccfda2 445 {
kishino 16:6100acfeb1f1 446 printf("wifi_off status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 12:0254eaccfda2 447 ret = -1;
kishino 12:0254eaccfda2 448 }
kishino 24:987e412ae879 449 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 450
kishino 12:0254eaccfda2 451 return ret;
kishino 12:0254eaccfda2 452 }
kishino 12:0254eaccfda2 453
kishino 12:0254eaccfda2 454 int C_SNIC_WifiInterface::getRssi( signed char *rssi_p )
kishino 12:0254eaccfda2 455 {
kishino 24:987e412ae879 456 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 457 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 12:0254eaccfda2 458 if( rssi_p == NULL )
kishino 12:0254eaccfda2 459 {
kishino 12:0254eaccfda2 460 printf("getRssi parameter error\r\n");
kishino 12:0254eaccfda2 461 return -1;
kishino 12:0254eaccfda2 462 }
kishino 12:0254eaccfda2 463
kishino 12:0254eaccfda2 464 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 465 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 466 if( payload_buf == NULL )
kishino 12:0254eaccfda2 467 {
kishino 12:0254eaccfda2 468 printf("getRssi payload_buf NULL\r\n");
kishino 12:0254eaccfda2 469 return -1;
kishino 12:0254eaccfda2 470 }
kishino 12:0254eaccfda2 471
kishino 22:a9ec0cad4f84 472 C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T req;
kishino 12:0254eaccfda2 473
kishino 12:0254eaccfda2 474 // Make request
kishino 12:0254eaccfda2 475 req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ;
kishino 12:0254eaccfda2 476 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 477
kishino 29:6a0ba999597d 478 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 479 unsigned int command_len;
kishino 24:987e412ae879 480 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 481 , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 482
kishino 12:0254eaccfda2 483 int ret;
kishino 12:0254eaccfda2 484 // Send uart command request
kishino 24:987e412ae879 485 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 486
kishino 12:0254eaccfda2 487 // Wait UART response
kishino 16:6100acfeb1f1 488 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 489 if( ret != 0 )
kishino 12:0254eaccfda2 490 {
kishino 12:0254eaccfda2 491 printf( "getRssi failed\r\n" );
kishino 24:987e412ae879 492 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 493 return -1;
kishino 12:0254eaccfda2 494 }
kishino 12:0254eaccfda2 495
kishino 12:0254eaccfda2 496 *rssi_p = (signed char)payload_buf->buf[2];
kishino 12:0254eaccfda2 497
kishino 24:987e412ae879 498 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 499 return 0;
kishino 12:0254eaccfda2 500 }
kishino 12:0254eaccfda2 501
kishino 12:0254eaccfda2 502 int C_SNIC_WifiInterface::getWifiStatus( tagWIFI_STATUS_T *status_p)
kishino 12:0254eaccfda2 503 {
kishino 24:987e412ae879 504 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 24:987e412ae879 505 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 16:6100acfeb1f1 506
kishino 12:0254eaccfda2 507 if( status_p == NULL )
kishino 12:0254eaccfda2 508 {
kishino 12:0254eaccfda2 509 printf("getWifiStatus parameter error\r\n");
kishino 12:0254eaccfda2 510 return -1;
kishino 12:0254eaccfda2 511 }
kishino 12:0254eaccfda2 512
kishino 12:0254eaccfda2 513 // Get buffer for response payload from MemoryPool
kishino 29:6a0ba999597d 514 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 12:0254eaccfda2 515 if( payload_buf == NULL )
kishino 12:0254eaccfda2 516 {
kishino 12:0254eaccfda2 517 printf("getWifiStatus payload_buf NULL\r\n");
kishino 12:0254eaccfda2 518 return -1;
kishino 12:0254eaccfda2 519 }
kishino 12:0254eaccfda2 520
kishino 22:a9ec0cad4f84 521 C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T req;
kishino 12:0254eaccfda2 522 // Make request
kishino 12:0254eaccfda2 523 req.cmd_sid = UART_CMD_SID_WIFI_GET_STATUS_REQ;
kishino 12:0254eaccfda2 524 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 525 req.interface = 0;
kishino 12:0254eaccfda2 526
kishino 29:6a0ba999597d 527 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 12:0254eaccfda2 528 unsigned int command_len;
kishino 24:987e412ae879 529 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req
kishino 22:a9ec0cad4f84 530 , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf->buf, command_array );
kishino 12:0254eaccfda2 531
kishino 12:0254eaccfda2 532 // Send uart command request
kishino 24:987e412ae879 533 snic_core_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 534
kishino 12:0254eaccfda2 535 int ret;
kishino 12:0254eaccfda2 536 // Wait UART response
kishino 16:6100acfeb1f1 537 ret = uartCmdMgr_p->wait();
kishino 12:0254eaccfda2 538 if( ret != 0 )
kishino 12:0254eaccfda2 539 {
kishino 12:0254eaccfda2 540 printf( "getWifiStatus failed\r\n" );
kishino 24:987e412ae879 541 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 542 return -1;
kishino 12:0254eaccfda2 543 }
kishino 12:0254eaccfda2 544
kishino 12:0254eaccfda2 545 // set status
kishino 12:0254eaccfda2 546 status_p->status = (E_WIFI_STATUS)payload_buf->buf[2];
kishino 12:0254eaccfda2 547
kishino 12:0254eaccfda2 548 // set Mac address
kishino 12:0254eaccfda2 549 if( status_p->status != e_STATUS_OFF )
kishino 12:0254eaccfda2 550 {
kishino 12:0254eaccfda2 551 memcpy( status_p->mac_address, &payload_buf->buf[3], BSSID_MAC_LENTH );
kishino 12:0254eaccfda2 552 }
kishino 12:0254eaccfda2 553
kishino 12:0254eaccfda2 554 // set SSID
kishino 37:f3a2053627c2 555 if( ( status_p->status == e_STA_JOINED ) || ( status_p->status == e_AP_STARTED ) )
kishino 12:0254eaccfda2 556 {
kishino 12:0254eaccfda2 557 memcpy( status_p->ssid, &payload_buf->buf[9], strlen( (char *)&payload_buf->buf[9]) );
kishino 12:0254eaccfda2 558 }
kishino 12:0254eaccfda2 559
kishino 24:987e412ae879 560 snic_core_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 561 return 0;
kishino 12:0254eaccfda2 562 }
kishino 31:15c22824cc46 563
kishino 31:15c22824cc46 564 int C_SNIC_WifiInterface::setIPConfig( bool is_DHCP
kishino 31:15c22824cc46 565 , const char *ip_p, const char *mask_p, const char *gateway_p )
kishino 31:15c22824cc46 566 {
kishino 31:15c22824cc46 567 // Parameter check
kishino 31:15c22824cc46 568 if( is_DHCP == false )
kishino 31:15c22824cc46 569 {
kishino 31:15c22824cc46 570 if( (ip_p == NULL) || (mask_p == NULL) ||(gateway_p == NULL) )
kishino 31:15c22824cc46 571 {
kishino 31:15c22824cc46 572 printf("setIPConfig parameter error\r\n");
kishino 31:15c22824cc46 573 return -1;
kishino 31:15c22824cc46 574 }
kishino 31:15c22824cc46 575 }
kishino 31:15c22824cc46 576
kishino 31:15c22824cc46 577 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
kishino 31:15c22824cc46 578 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
kishino 31:15c22824cc46 579
kishino 31:15c22824cc46 580 // Get buffer for response payload from MemoryPool
kishino 31:15c22824cc46 581 tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
kishino 31:15c22824cc46 582 if( payload_buf == NULL )
kishino 31:15c22824cc46 583 {
kishino 31:15c22824cc46 584 printf("setIPConfig payload_buf NULL\r\n");
kishino 31:15c22824cc46 585 return -1;
kishino 31:15c22824cc46 586 }
kishino 31:15c22824cc46 587
kishino 31:15c22824cc46 588 unsigned char *command_array = snic_core_p->getCommandBuf();
kishino 31:15c22824cc46 589 unsigned int command_len;
kishino 31:15c22824cc46 590 if( is_DHCP == true )
kishino 31:15c22824cc46 591 {
kishino 31:15c22824cc46 592 C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T req;
kishino 31:15c22824cc46 593 // Make request
kishino 31:15c22824cc46 594 req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ;
kishino 31:15c22824cc46 595 req.seq = mUartRequestSeq++;
kishino 31:15c22824cc46 596 req.interface = 0;
kishino 31:15c22824cc46 597 req.dhcp = 1;
kishino 31:15c22824cc46 598
kishino 31:15c22824cc46 599 // Preparation of command
kishino 31:15c22824cc46 600 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 31:15c22824cc46 601 , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T), payload_buf->buf, command_array );
kishino 31:15c22824cc46 602 }
kishino 31:15c22824cc46 603 else
kishino 31:15c22824cc46 604 {
kishino 31:15c22824cc46 605 C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T req;
kishino 31:15c22824cc46 606 // Make request
kishino 31:15c22824cc46 607 req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ;
kishino 31:15c22824cc46 608 req.seq = mUartRequestSeq++;
kishino 31:15c22824cc46 609 req.interface = 0;
kishino 31:15c22824cc46 610 req.dhcp = 0;
kishino 31:15c22824cc46 611
kishino 31:15c22824cc46 612 // Set paramter of address
kishino 31:15c22824cc46 613 int addr_temp;
kishino 31:15c22824cc46 614 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( ip_p );
kishino 31:15c22824cc46 615 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.ip_addr );
kishino 31:15c22824cc46 616 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( mask_p );
kishino 31:15c22824cc46 617 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.netmask );
kishino 31:15c22824cc46 618 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( gateway_p );
kishino 31:15c22824cc46 619 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.gateway );
kishino 31:15c22824cc46 620
kishino 31:15c22824cc46 621 // Preparation of command
kishino 31:15c22824cc46 622 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 31:15c22824cc46 623 , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T), payload_buf->buf, command_array );
kishino 31:15c22824cc46 624 }
kishino 31:15c22824cc46 625 // Send uart command request
kishino 31:15c22824cc46 626 snic_core_p->sendUart( command_len, command_array );
kishino 31:15c22824cc46 627
kishino 31:15c22824cc46 628 int ret;
kishino 31:15c22824cc46 629 // Wait UART response
kishino 31:15c22824cc46 630 ret = uartCmdMgr_p->wait();
kishino 31:15c22824cc46 631 if( ret != 0 )
kishino 31:15c22824cc46 632 {
kishino 31:15c22824cc46 633 printf( "setIPConfig failed\r\n" );
kishino 31:15c22824cc46 634 snic_core_p->freeCmdBuf( payload_buf );
kishino 31:15c22824cc46 635 return -1;
kishino 31:15c22824cc46 636 }
kishino 31:15c22824cc46 637
kishino 31:15c22824cc46 638 if( uartCmdMgr_p->getCommandStatus() != 0 )
kishino 31:15c22824cc46 639 {
kishino 31:15c22824cc46 640 printf("setIPConfig status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
kishino 31:15c22824cc46 641 ret = -1;
kishino 31:15c22824cc46 642 }
kishino 31:15c22824cc46 643
kishino 31:15c22824cc46 644 snic_core_p->freeCmdBuf( payload_buf );
kishino 36:f33fcf5975ab 645 return ret;
kishino 31:15c22824cc46 646 }