SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Fri Mar 28 00:45:15 2014 +0000
Revision:
24:987e412ae879
Parent:
22:a9ec0cad4f84
Child:
26:f2e1030964e4
Changed to that any classes do not have instance of C_SNIC_Core class.

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