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