SNIC UART Interface library: Serial to Wi-Fi library for Murata TypeYD Wi-Fi module. For more information about TypeYD: http://www.murata.co.jp/products/microwave/module/lbwb1zzydz/index.html

Dependents:   SNIC-xively-jumpstart-demo SNIC-FluentLogger-example TCPEchoServer murataDemo ... more

Fork of YDwifiInterface by Takao Kishino

Committer:
kishino
Date:
Mon May 26 05:17:28 2014 +0000
Revision:
29:6a0ba999597d
Parent:
28:b796031f6519
Child:
31:15c22824cc46
Changed the receive logic of UART.; Changed the use area of the global buffer.

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