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:
Wed Mar 26 04:38:12 2014 +0000
Revision:
14:54378c96d285
Parent:
12:0254eaccfda2
Child:
15:5eb637414df2
Added a comment of copyright.

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