Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of NySNICInterface by
SNIC_WifiInterface.cpp
00001 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License 00002 * muRata, SWITCH SCIENCE Wi-FI module TypeYD-SNIC UART. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 #include "SNIC_WifiInterface.h" 00020 #include "SNIC_UartMsgUtil.h" 00021 00022 #define UART_CONNECT_BUF_SIZE 512 00023 unsigned char gCONNECT_BUF[UART_CONNECT_BUF_SIZE]; 00024 00025 C_SNIC_WifiInterface::C_SNIC_WifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) 00026 { 00027 mUART_tx = tx; 00028 mUART_rx = rx; 00029 mUART_cts = cts; 00030 mUART_rts = rts;; 00031 mUART_baud = baud; 00032 mModuleReset = reset; 00033 } 00034 00035 C_SNIC_WifiInterface::~C_SNIC_WifiInterface() 00036 { 00037 } 00038 00039 int C_SNIC_WifiInterface::init() 00040 { 00041 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00042 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00043 00044 /* Initialize UART */ 00045 snic_core_p->initUart( mUART_tx, mUART_rx, mUART_baud ); 00046 00047 /* Module reset */ 00048 snic_core_p->resetModule( mModuleReset ); 00049 00050 wait(1); 00051 /* Initialize SNIC API */ 00052 // Get buffer for response payload from MemoryPool 00053 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00054 if( payload_buf_p == NULL ) 00055 { 00056 DEBUG_PRINT("snic_init payload_buf_p NULL\r\n"); 00057 printf("initial failed1\n"); 00058 return -1; 00059 } 00060 00061 C_SNIC_Core::tagSNIC_INIT_REQ_T req; 00062 // Make request 00063 req.cmd_sid = UART_CMD_SID_SNIC_INIT_REQ; 00064 req.seq = mUartRequestSeq++; 00065 req.buf_size[0] = 0x08; 00066 req.buf_size[1] = 0x00; 00067 00068 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00069 unsigned int command_len; 00070 // Preparation of command 00071 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req 00072 , sizeof(C_SNIC_Core::tagSNIC_INIT_REQ_T), payload_buf_p->buf, command_array_p ); 00073 00074 // Send uart command request 00075 snic_core_p->sendUart( command_len, command_array_p ); 00076 00077 00078 int ret; 00079 00080 // Wait UART response 00081 ret = uartCmdMgr_p->wait(); 00082 if( ret != 0 ) 00083 { 00084 DEBUG_PRINT( "snic_init failed\r\n" ); 00085 snic_core_p->freeCmdBuf( payload_buf_p ); 00086 printf("initial failed2\n"); 00087 return -1; 00088 } 00089 00090 00091 if( uartCmdMgr_p->getCommandStatus() != 0 ) 00092 { 00093 DEBUG_PRINT("snic_init status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00094 snic_core_p->freeCmdBuf( payload_buf_p ); 00095 printf("initial failed3\n"); 00096 return -1; 00097 } 00098 snic_core_p->freeCmdBuf( payload_buf_p ); 00099 00100 return ret; 00101 } 00102 00103 int C_SNIC_WifiInterface::getFWVersion( unsigned char *version_p ) 00104 { 00105 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00106 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00107 00108 // Get buffer for response payload from MemoryPool 00109 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00110 if( payload_buf_p == NULL ) 00111 { 00112 DEBUG_PRINT("getFWVersion payload_buf_p NULL\r\n"); 00113 return -1; 00114 } 00115 00116 C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T req; 00117 // Make request 00118 req.cmd_sid = UART_CMD_SID_GEN_FW_VER_GET_REQ; 00119 req.seq = mUartRequestSeq++; 00120 00121 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00122 unsigned int command_len; 00123 // Preparation of command 00124 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_GEN, req.cmd_sid, (unsigned char *)&req 00125 , sizeof(C_SNIC_Core::tagGEN_FW_VER_GET_REQ_T), payload_buf_p->buf, command_array_p ); 00126 00127 int ret; 00128 00129 // Send uart command request 00130 snic_core_p->sendUart( command_len, command_array_p ); 00131 00132 // Wait UART response 00133 ret = uartCmdMgr_p->wait(); 00134 if( ret != 0 ) 00135 { 00136 DEBUG_PRINT( "getFWversion failed\r\n" ); 00137 snic_core_p->freeCmdBuf( payload_buf_p ); 00138 return -1; 00139 } 00140 00141 if( uartCmdMgr_p->getCommandStatus() == 0 ) 00142 { 00143 unsigned char version_len = payload_buf_p->buf[3]; 00144 memcpy( version_p, &payload_buf_p->buf[4], version_len ); 00145 } 00146 snic_core_p->freeCmdBuf( payload_buf_p ); 00147 return 0; 00148 } 00149 00150 int C_SNIC_WifiInterface::connect(const char *ssid_p, unsigned char ssid_len, E_SECURITY sec_type 00151 , const char *sec_key_p, unsigned char sec_key_len) 00152 { 00153 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00154 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00155 00156 // Parameter check(SSID) 00157 if( (ssid_p == NULL) || (ssid_len == 0) ) 00158 { 00159 DEBUG_PRINT( "connect failed [ parameter NG:SSID ]\r\n" ); 00160 return -1; 00161 } 00162 00163 // Parameter check(Security key) 00164 if( (sec_type != e_SEC_OPEN) && ( (sec_key_len == 0) || (sec_key_p == NULL) ) ) 00165 { 00166 DEBUG_PRINT( "connect failed [ parameter NG:Security key ]\r\n" ); 00167 return -1; 00168 } 00169 00170 // Get buffer for response payload from MemoryPool 00171 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00172 if( payload_buf_p == NULL ) 00173 { 00174 DEBUG_PRINT("connect payload_buf_p NULL\r\n"); 00175 return -1; 00176 } 00177 00178 unsigned char *buf = &gCONNECT_BUF[0]; 00179 unsigned int buf_len = 0; 00180 unsigned int command_len; 00181 00182 memset( buf, 0, UART_CONNECT_BUF_SIZE ); 00183 // Make request 00184 buf[0] = UART_CMD_SID_WIFI_JOIN_REQ; 00185 buf_len++; 00186 buf[1] = mUartRequestSeq++; 00187 buf_len++; 00188 // SSID 00189 memcpy( &buf[2], ssid_p, ssid_len ); 00190 buf_len += ssid_len; 00191 buf_len++; 00192 00193 // Security mode 00194 buf[ buf_len ] = (unsigned char)sec_type; 00195 buf_len++; 00196 00197 // Security key 00198 if( sec_type != e_SEC_OPEN ) 00199 { 00200 buf[ buf_len ] = sec_key_len; 00201 buf_len++; 00202 if( sec_key_len > 0 ) 00203 { 00204 memcpy( &buf[buf_len], sec_key_p, sec_key_len ); 00205 buf_len += sec_key_len; 00206 } 00207 } 00208 00209 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00210 // Preparation of command 00211 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, UART_CMD_SID_WIFI_JOIN_REQ, buf 00212 , buf_len, payload_buf_p->buf, command_array_p ); 00213 00214 // Send uart command request 00215 snic_core_p->sendUart( command_len, command_array_p ); 00216 00217 int ret; 00218 // Wait UART response 00219 ret = uartCmdMgr_p->wait(); 00220 if(uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_WIFI_ERR_ALREADY_JOINED) 00221 { 00222 DEBUG_PRINT( "Already connected\r\n" ); 00223 } 00224 else 00225 { 00226 if( ret != 0 ) 00227 { 00228 DEBUG_PRINT( "join failed\r\n" ); 00229 snic_core_p->freeCmdBuf( payload_buf_p ); 00230 return -1; 00231 } 00232 } 00233 00234 if(uartCmdMgr_p->getCommandStatus() != 0) 00235 { 00236 DEBUG_PRINT("join status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00237 snic_core_p->freeCmdBuf( payload_buf_p ); 00238 return -1; 00239 } 00240 snic_core_p->freeCmdBuf( payload_buf_p ); 00241 00242 return ret; 00243 } 00244 00245 int C_SNIC_WifiInterface::disconnect() 00246 { 00247 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00248 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00249 00250 // Get buffer for response payload from MemoryPool 00251 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00252 if( payload_buf_p == NULL ) 00253 { 00254 DEBUG_PRINT("disconnect payload_buf_p NULL\r\n"); 00255 return -1; 00256 } 00257 00258 C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T req; 00259 // Make request 00260 req.cmd_sid = UART_CMD_SID_WIFI_DISCONNECT_REQ; 00261 req.seq = mUartRequestSeq++; 00262 00263 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00264 unsigned int command_len; 00265 // Preparation of command 00266 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req 00267 , sizeof(C_SNIC_Core::tagWIFI_DISCONNECT_REQ_T), payload_buf_p->buf, command_array_p ); 00268 00269 // Send uart command request 00270 snic_core_p->sendUart( command_len, command_array_p ); 00271 00272 int ret; 00273 // Wait UART response 00274 ret = uartCmdMgr_p->wait(); 00275 if( ret != 0 ) 00276 { 00277 DEBUG_PRINT( "disconnect failed\r\n" ); 00278 snic_core_p->freeCmdBuf( payload_buf_p ); 00279 return -1; 00280 } 00281 00282 if( uartCmdMgr_p->getCommandStatus() != 0 ) 00283 { 00284 DEBUG_PRINT("disconnect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00285 ret = -1; 00286 } 00287 snic_core_p->freeCmdBuf( payload_buf_p ); 00288 return ret; 00289 } 00290 00291 int C_SNIC_WifiInterface::scan( const char *ssid_p, unsigned char *bssid_p 00292 , void (*result_handler_p)(tagSCAN_RESULT_T *scan_result) ) 00293 { 00294 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00295 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00296 00297 // Get buffer for response payload from MemoryPool 00298 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00299 if( payload_buf_p == NULL ) 00300 { 00301 DEBUG_PRINT("scan payload_buf_p NULL\r\n"); 00302 return -1; 00303 } 00304 00305 C_SNIC_Core::tagWIFI_SCAN_REQ_T req; 00306 unsigned int buf_len = 0; 00307 00308 memset( &req, 0, sizeof(C_SNIC_Core::tagWIFI_SCAN_REQ_T) ); 00309 // Make request 00310 req.cmd_sid = UART_CMD_SID_WIFI_SCAN_REQ; 00311 buf_len++; 00312 req.seq = mUartRequestSeq++; 00313 buf_len++; 00314 00315 // Set scan type(Active scan) 00316 req.scan_type = 0; 00317 buf_len++; 00318 // Set bss type(any) 00319 req.bss_type = 2; 00320 buf_len++; 00321 // Set BSSID 00322 if( bssid_p != NULL ) 00323 { 00324 memcpy( req.bssid, bssid_p, BSSID_MAC_LENTH ); 00325 } 00326 buf_len += BSSID_MAC_LENTH; 00327 // Set channel list(0) 00328 req.chan_list = 0; 00329 buf_len++; 00330 //Set SSID 00331 if( ssid_p != NULL ) 00332 { 00333 strcpy( (char *)req.ssid, ssid_p ); 00334 buf_len += strlen(ssid_p); 00335 } 00336 buf_len++; 00337 00338 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00339 unsigned int command_len; 00340 // Preparation of command 00341 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req 00342 , buf_len, payload_buf_p->buf, command_array_p ); 00343 00344 // Set scan result callback 00345 uartCmdMgr_p->setScanResultHandler( result_handler_p ); 00346 00347 // Send uart command request 00348 snic_core_p->sendUart( command_len, command_array_p ); 00349 00350 int ret; 00351 // Wait UART response 00352 ret = uartCmdMgr_p->wait(); 00353 DEBUG_PRINT( "scan wait:%d\r\n", ret ); 00354 if( ret != 0 ) 00355 { 00356 DEBUG_PRINT( "scan failed\r\n" ); 00357 snic_core_p->freeCmdBuf( payload_buf_p ); 00358 return -1; 00359 } 00360 00361 if( uartCmdMgr_p->getCommandStatus() != 0 ) 00362 { 00363 DEBUG_PRINT("scan status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00364 snic_core_p->freeCmdBuf( payload_buf_p ); 00365 return -1; 00366 } 00367 00368 snic_core_p->freeCmdBuf( payload_buf_p ); 00369 00370 return ret; 00371 } 00372 00373 int C_SNIC_WifiInterface::wifi_on( const char *country_p ) 00374 { 00375 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00376 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00377 00378 // Parameter check 00379 if( country_p == NULL ) 00380 { 00381 DEBUG_PRINT("wifi_on parameter error\r\n"); 00382 return -1; 00383 } 00384 00385 // Get buffer for response payload from MemoryPool 00386 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00387 if( payload_buf_p == NULL ) 00388 { 00389 DEBUG_PRINT("wifi_on payload_buf_p NULL\r\n"); 00390 return -1; 00391 } 00392 00393 C_SNIC_Core::tagWIFI_ON_REQ_T req; 00394 // Make request 00395 req.cmd_sid = UART_CMD_SID_WIFI_ON_REQ; 00396 req.seq = mUartRequestSeq++; 00397 memcpy( req.country, country_p, COUNTRYC_CODE_LENTH ); 00398 00399 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00400 unsigned int command_len; 00401 // Preparation of command 00402 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req 00403 , sizeof(C_SNIC_Core::tagWIFI_ON_REQ_T), payload_buf_p->buf, command_array_p ); 00404 00405 // Send uart command request 00406 snic_core_p->sendUart( command_len, command_array_p ); 00407 00408 int ret; 00409 // Wait UART response 00410 ret = uartCmdMgr_p->wait(); 00411 if( ret != 0 ) 00412 { 00413 DEBUG_PRINT( "wifi_on failed\r\n" ); 00414 snic_core_p->freeCmdBuf( payload_buf_p ); 00415 return -1; 00416 } 00417 00418 if( uartCmdMgr_p->getCommandStatus() != 0 ) 00419 { 00420 DEBUG_PRINT("wifi_on status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00421 snic_core_p->freeCmdBuf( payload_buf_p ); 00422 return -1; 00423 } 00424 snic_core_p->freeCmdBuf( payload_buf_p ); 00425 00426 return ret; 00427 } 00428 00429 int C_SNIC_WifiInterface::wifi_off() 00430 { 00431 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00432 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00433 00434 // Get buffer for response payload from MemoryPool 00435 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00436 if( payload_buf_p == NULL ) 00437 { 00438 DEBUG_PRINT("wifi_off payload_buf_p NULL\r\n"); 00439 return -1; 00440 } 00441 00442 C_SNIC_Core::tagWIFI_OFF_REQ_T req; 00443 // Make request 00444 req.cmd_sid = UART_CMD_SID_WIFI_OFF_REQ; 00445 req.seq = mUartRequestSeq++; 00446 00447 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00448 unsigned int command_len; 00449 // Preparation of command 00450 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req 00451 , sizeof(C_SNIC_Core::tagWIFI_OFF_REQ_T), payload_buf_p->buf, command_array_p ); 00452 00453 // Send uart command request 00454 snic_core_p->sendUart( command_len, command_array_p ); 00455 00456 int ret; 00457 // Wait UART response 00458 ret = uartCmdMgr_p->wait(); 00459 if( ret != 0 ) 00460 { 00461 DEBUG_PRINT( "wifi_off failed\r\n" ); 00462 snic_core_p->freeCmdBuf( payload_buf_p ); 00463 return -1; 00464 } 00465 00466 if( uartCmdMgr_p->getCommandStatus() != 0 ) 00467 { 00468 DEBUG_PRINT("wifi_off status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00469 snic_core_p->freeCmdBuf( payload_buf_p ); 00470 return -1; 00471 } 00472 snic_core_p->freeCmdBuf( payload_buf_p ); 00473 00474 return ret; 00475 } 00476 00477 int C_SNIC_WifiInterface::getRssi( signed char *rssi_p ) 00478 { 00479 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00480 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00481 if( rssi_p == NULL ) 00482 { 00483 DEBUG_PRINT("getRssi parameter error\r\n"); 00484 return -1; 00485 } 00486 00487 // Get buffer for response payload from MemoryPool 00488 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00489 if( payload_buf_p == NULL ) 00490 { 00491 DEBUG_PRINT("getRssi payload_buf_p NULL\r\n"); 00492 return -1; 00493 } 00494 00495 C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T req; 00496 00497 // Make request 00498 req.cmd_sid = UART_CMD_SID_WIFI_GET_STA_RSSI_REQ; 00499 req.seq = mUartRequestSeq++; 00500 00501 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00502 unsigned int command_len; 00503 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req 00504 , sizeof(C_SNIC_Core::tagWIFI_GET_STA_RSSI_REQ_T), payload_buf_p->buf, command_array_p ); 00505 00506 int ret; 00507 // Send uart command request 00508 snic_core_p->sendUart( command_len, command_array_p ); 00509 00510 // Wait UART response 00511 ret = uartCmdMgr_p->wait(); 00512 if( ret != 0 ) 00513 { 00514 DEBUG_PRINT( "getRssi failed\r\n" ); 00515 snic_core_p->freeCmdBuf( payload_buf_p ); 00516 return -1; 00517 } 00518 00519 *rssi_p = (signed char)payload_buf_p->buf[2]; 00520 00521 snic_core_p->freeCmdBuf( payload_buf_p ); 00522 return 0; 00523 } 00524 00525 int C_SNIC_WifiInterface::getWifiStatus( tagWIFI_STATUS_T *status_p) 00526 { 00527 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00528 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00529 00530 if( status_p == NULL ) 00531 { 00532 DEBUG_PRINT("getWifiStatus parameter error\r\n"); 00533 return -1; 00534 } 00535 00536 // Get buffer for response payload from MemoryPool 00537 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00538 if( payload_buf_p == NULL ) 00539 { 00540 DEBUG_PRINT("getWifiStatus payload_buf_p NULL\r\n"); 00541 return -1; 00542 } 00543 00544 C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T req; 00545 // Make request 00546 req.cmd_sid = UART_CMD_SID_WIFI_GET_STATUS_REQ; 00547 req.seq = mUartRequestSeq++; 00548 req.interface = 0; 00549 00550 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00551 unsigned int command_len; 00552 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_WIFI, req.cmd_sid, (unsigned char *)&req 00553 , sizeof(C_SNIC_Core::tagWIFI_GET_STATUS_REQ_T), payload_buf_p->buf, command_array_p ); 00554 00555 // Send uart command request 00556 snic_core_p->sendUart( command_len, command_array_p ); 00557 00558 int ret; 00559 // Wait UART response 00560 ret = uartCmdMgr_p->wait(); 00561 if( ret != 0 ) 00562 { 00563 DEBUG_PRINT( "getWifiStatus failed\r\n" ); 00564 snic_core_p->freeCmdBuf( payload_buf_p ); 00565 return -1; 00566 } 00567 00568 // set status 00569 status_p->status = (E_WIFI_STATUS)payload_buf_p->buf[2]; 00570 00571 // set Mac address 00572 if( status_p->status != e_STATUS_OFF ) 00573 { 00574 memcpy( status_p->mac_address, &payload_buf_p->buf[3], BSSID_MAC_LENTH ); 00575 } 00576 00577 // set SSID 00578 if( ( status_p->status == e_STA_JOINED ) || ( status_p->status == e_AP_STARTED ) ) 00579 { 00580 memcpy( status_p->ssid, &payload_buf_p->buf[9], strlen( (char *)&payload_buf_p->buf[9]) ); 00581 } 00582 00583 snic_core_p->freeCmdBuf( payload_buf_p ); 00584 return 0; 00585 } 00586 00587 int C_SNIC_WifiInterface::setIPConfig( bool is_DHCP 00588 , const char *ip_p, const char *mask_p, const char *gateway_p ) 00589 { 00590 // Parameter check 00591 if( is_DHCP == false ) 00592 { 00593 if( (ip_p == NULL) || (mask_p == NULL) ||(gateway_p == NULL) ) 00594 { 00595 DEBUG_PRINT("setIPConfig parameter error\r\n"); 00596 return -1; 00597 } 00598 } 00599 00600 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00601 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00602 00603 // Get buffer for response payload from MemoryPool 00604 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00605 if( payload_buf_p == NULL ) 00606 { 00607 DEBUG_PRINT("setIPConfig payload_buf_p NULL\r\n"); 00608 return -1; 00609 } 00610 00611 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00612 unsigned int command_len; 00613 if( is_DHCP == true ) 00614 { 00615 C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T req; 00616 // Make request 00617 req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; 00618 req.seq = mUartRequestSeq++; 00619 req.interface = 0; 00620 req.dhcp = 1; 00621 00622 // Preparation of command 00623 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req 00624 , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_DHCP_T), payload_buf_p->buf, command_array_p ); 00625 } 00626 else 00627 { 00628 C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T req; 00629 // Make request 00630 req.cmd_sid = UART_CMD_SID_SNIC_IP_CONFIG_REQ; 00631 req.seq = mUartRequestSeq++; 00632 req.interface = 0; 00633 req.dhcp = 0; 00634 00635 // Set paramter of address 00636 int addr_temp; 00637 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( ip_p ); 00638 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.ip_addr ); 00639 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( mask_p ); 00640 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.netmask ); 00641 addr_temp = C_SNIC_UartMsgUtil::addrToInteger( gateway_p ); 00642 C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.gateway ); 00643 00644 // Preparation of command 00645 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req 00646 , sizeof(C_SNIC_Core::tagSNIC_IP_CONFIG_REQ_STATIC_T), payload_buf_p->buf, command_array_p ); 00647 } 00648 // Send uart command request 00649 snic_core_p->sendUart( command_len, command_array_p ); 00650 00651 int ret; 00652 // Wait UART response 00653 ret = uartCmdMgr_p->wait(); 00654 if( ret != 0 ) 00655 { 00656 DEBUG_PRINT( "setIPConfig failed\r\n" ); 00657 snic_core_p->freeCmdBuf( payload_buf_p ); 00658 return -1; 00659 } 00660 00661 if( uartCmdMgr_p->getCommandStatus() != 0 ) 00662 { 00663 DEBUG_PRINT("setIPConfig status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00664 snic_core_p->freeCmdBuf( payload_buf_p ); 00665 return -1; 00666 } 00667 00668 snic_core_p->freeCmdBuf( payload_buf_p ); 00669 return ret; 00670 }
Generated on Tue Jul 12 2022 17:46:21 by
1.7.2
