SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Fri Mar 28 03:03:40 2014 +0000
Revision:
26:f2e1030964e4
Parent:
24:987e412ae879
Child:
28:b796031f6519
Created a virtual destructor

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