wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/wizfi310-driver/WizFi310/WizFi310.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* |
JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 2015 ARM Limited |
JMF | 0:24d3eb812fd4 | 3 | * |
JMF | 0:24d3eb812fd4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 0:24d3eb812fd4 | 5 | * you may not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 6 | * You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 7 | * |
JMF | 0:24d3eb812fd4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 9 | * |
JMF | 0:24d3eb812fd4 | 10 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:24d3eb812fd4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
JMF | 0:24d3eb812fd4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:24d3eb812fd4 | 13 | * See the License for the specific language governing permissions and |
JMF | 0:24d3eb812fd4 | 14 | * limitations under the License. |
JMF | 0:24d3eb812fd4 | 15 | */ |
JMF | 0:24d3eb812fd4 | 16 | |
JMF | 0:24d3eb812fd4 | 17 | /** |
JMF | 0:24d3eb812fd4 | 18 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 19 | * @file WizFi310.cpp |
JMF | 0:24d3eb812fd4 | 20 | * @author Gateway Team |
JMF | 0:24d3eb812fd4 | 21 | * @brief Implementation file of the WizFi310 WiFi Device |
JMF | 0:24d3eb812fd4 | 22 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 23 | * @attention |
JMF | 0:24d3eb812fd4 | 24 | * |
JMF | 0:24d3eb812fd4 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
JMF | 0:24d3eb812fd4 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
JMF | 0:24d3eb812fd4 | 27 | * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY |
JMF | 0:24d3eb812fd4 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
JMF | 0:24d3eb812fd4 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
JMF | 0:24d3eb812fd4 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
JMF | 0:24d3eb812fd4 | 31 | * |
JMF | 0:24d3eb812fd4 | 32 | * <h2><center>© COPYRIGHT 2017 WIZnet Co.,Ltd.</center></h2> |
JMF | 0:24d3eb812fd4 | 33 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 34 | */ |
JMF | 0:24d3eb812fd4 | 35 | |
JMF | 0:24d3eb812fd4 | 36 | #include "WizFi310.h" |
JMF | 0:24d3eb812fd4 | 37 | #define WIZFI310_DEFAULT_BAUD_RATE 115200 |
JMF | 0:24d3eb812fd4 | 38 | |
JMF | 0:24d3eb812fd4 | 39 | #define AT_CMD_PARSER_DEFAULT_TIMEOUT 500 |
JMF | 0:24d3eb812fd4 | 40 | #define AT_CMD_PARSER_INIT_TIMEOUT 1000 |
JMF | 0:24d3eb812fd4 | 41 | #define AT_CMD_PARSER_RECV_TIMEOUT 20000 |
JMF | 0:24d3eb812fd4 | 42 | |
JMF | 0:24d3eb812fd4 | 43 | using namespace mbed; |
JMF | 0:24d3eb812fd4 | 44 | WizFi310::WizFi310(PinName tx, PinName rx, bool debug) |
JMF | 0:24d3eb812fd4 | 45 | : _serial(tx, rx, WIZFI310_DEFAULT_BAUD_RATE), |
JMF | 0:24d3eb812fd4 | 46 | _parser(&_serial), |
JMF | 0:24d3eb812fd4 | 47 | _packets(0), |
JMF | 0:24d3eb812fd4 | 48 | _packets_end(&_packets) |
JMF | 0:24d3eb812fd4 | 49 | { |
JMF | 0:24d3eb812fd4 | 50 | _serial.set_baud( WIZFI310_DEFAULT_BAUD_RATE ); |
JMF | 0:24d3eb812fd4 | 51 | _parser.debug_on(debug); |
JMF | 0:24d3eb812fd4 | 52 | _parser.set_delimiter("\r\n"); |
JMF | 0:24d3eb812fd4 | 53 | |
JMF | 0:24d3eb812fd4 | 54 | setTimeout(AT_CMD_PARSER_INIT_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 55 | for(int i=0; i<10; i++) |
JMF | 0:24d3eb812fd4 | 56 | { |
JMF | 0:24d3eb812fd4 | 57 | if( _parser.send("AT") && _parser.recv("[OK]") ) |
JMF | 0:24d3eb812fd4 | 58 | { |
JMF | 0:24d3eb812fd4 | 59 | _parser.send("AT+MECHO=0"); |
JMF | 0:24d3eb812fd4 | 60 | _parser.recv("[OK]"); |
JMF | 0:24d3eb812fd4 | 61 | _parser.send("AT+MPROF=S"); |
JMF | 0:24d3eb812fd4 | 62 | _parser.recv("[OK]"); |
JMF | 0:24d3eb812fd4 | 63 | _parser.send("AT+MRESET"); |
JMF | 0:24d3eb812fd4 | 64 | _parser.recv("[OK]"); |
JMF | 0:24d3eb812fd4 | 65 | break; |
JMF | 0:24d3eb812fd4 | 66 | } |
JMF | 0:24d3eb812fd4 | 67 | } |
JMF | 0:24d3eb812fd4 | 68 | |
JMF | 0:24d3eb812fd4 | 69 | _parser.recv("WizFi310 Version %s (WIZnet Co.Ltd)", _firmware_version); |
JMF | 0:24d3eb812fd4 | 70 | } |
JMF | 0:24d3eb812fd4 | 71 | |
JMF | 0:24d3eb812fd4 | 72 | const char* WizFi310::get_firmware_version() |
JMF | 0:24d3eb812fd4 | 73 | { |
JMF | 0:24d3eb812fd4 | 74 | if( strlen(_firmware_version) != 0 ) |
JMF | 0:24d3eb812fd4 | 75 | { |
JMF | 0:24d3eb812fd4 | 76 | return _firmware_version; |
JMF | 0:24d3eb812fd4 | 77 | } |
JMF | 0:24d3eb812fd4 | 78 | |
JMF | 0:24d3eb812fd4 | 79 | _parser.send("AT+MINFO"); |
JMF | 0:24d3eb812fd4 | 80 | if( _parser.recv("%s/WizFi310 Rev", _firmware_version) ) |
JMF | 0:24d3eb812fd4 | 81 | { |
JMF | 0:24d3eb812fd4 | 82 | return _firmware_version; |
JMF | 0:24d3eb812fd4 | 83 | } |
JMF | 0:24d3eb812fd4 | 84 | |
JMF | 0:24d3eb812fd4 | 85 | return 0; |
JMF | 0:24d3eb812fd4 | 86 | } |
JMF | 0:24d3eb812fd4 | 87 | |
JMF | 0:24d3eb812fd4 | 88 | bool WizFi310::startup(int mode) |
JMF | 0:24d3eb812fd4 | 89 | { |
JMF | 0:24d3eb812fd4 | 90 | if( mode != 0 && mode != 1 ) |
JMF | 0:24d3eb812fd4 | 91 | { |
JMF | 0:24d3eb812fd4 | 92 | return false; |
JMF | 0:24d3eb812fd4 | 93 | } |
JMF | 0:24d3eb812fd4 | 94 | _op_mode = mode; |
JMF | 0:24d3eb812fd4 | 95 | |
JMF | 0:24d3eb812fd4 | 96 | _parser.oob("{", callback(this, &WizFi310::_packet_handler)); |
JMF | 0:24d3eb812fd4 | 97 | //_parser.oob("\n{", callback(this, &WizFi310::_packet_handler)); |
JMF | 0:24d3eb812fd4 | 98 | return true; |
JMF | 0:24d3eb812fd4 | 99 | } |
JMF | 0:24d3eb812fd4 | 100 | |
JMF | 0:24d3eb812fd4 | 101 | bool WizFi310::reset(void) |
JMF | 0:24d3eb812fd4 | 102 | { |
JMF | 0:24d3eb812fd4 | 103 | for (int i=0; i<2; i++) |
JMF | 0:24d3eb812fd4 | 104 | { |
JMF | 0:24d3eb812fd4 | 105 | if(_parser.send("AT+MRESET") |
JMF | 0:24d3eb812fd4 | 106 | && _parser.recv("[OK]")) |
JMF | 0:24d3eb812fd4 | 107 | { |
JMF | 0:24d3eb812fd4 | 108 | return true; |
JMF | 0:24d3eb812fd4 | 109 | } |
JMF | 0:24d3eb812fd4 | 110 | } |
JMF | 0:24d3eb812fd4 | 111 | |
JMF | 0:24d3eb812fd4 | 112 | return false; |
JMF | 0:24d3eb812fd4 | 113 | } |
JMF | 0:24d3eb812fd4 | 114 | |
JMF | 0:24d3eb812fd4 | 115 | bool WizFi310::dhcp(bool enabled) |
JMF | 0:24d3eb812fd4 | 116 | { |
JMF | 0:24d3eb812fd4 | 117 | _dhcp = enabled; |
JMF | 0:24d3eb812fd4 | 118 | return _dhcp; |
JMF | 0:24d3eb812fd4 | 119 | } |
JMF | 0:24d3eb812fd4 | 120 | |
JMF | 0:24d3eb812fd4 | 121 | bool WizFi310::connect(const char *ap, const char *passPhrase, const char *sec) |
JMF | 0:24d3eb812fd4 | 122 | { |
JMF | 0:24d3eb812fd4 | 123 | if ( !(_parser.send("AT+WSET=0,%s", ap) && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 124 | { |
JMF | 0:24d3eb812fd4 | 125 | return false; |
JMF | 0:24d3eb812fd4 | 126 | } |
JMF | 0:24d3eb812fd4 | 127 | |
JMF | 0:24d3eb812fd4 | 128 | //if ( !(_parser.send("AT+WSEC=0,%s,%s", sec, passPhrase) && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 129 | if ( !(_parser.send("AT+WSEC=0,,%s", passPhrase) && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 130 | { |
JMF | 0:24d3eb812fd4 | 131 | return false; |
JMF | 0:24d3eb812fd4 | 132 | } |
JMF | 0:24d3eb812fd4 | 133 | |
JMF | 0:24d3eb812fd4 | 134 | if (_dhcp) |
JMF | 0:24d3eb812fd4 | 135 | { |
JMF | 0:24d3eb812fd4 | 136 | if ( !(_parser.send("AT+WNET=1") && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 137 | { |
JMF | 0:24d3eb812fd4 | 138 | return false; |
JMF | 0:24d3eb812fd4 | 139 | } |
JMF | 0:24d3eb812fd4 | 140 | } |
JMF | 0:24d3eb812fd4 | 141 | else |
JMF | 0:24d3eb812fd4 | 142 | { |
JMF | 0:24d3eb812fd4 | 143 | if ( !(_parser.send("AT+WNET=0,%s,%s,%s",_ip_buffer,_netmask_buffer,_gateway_buffer) |
JMF | 0:24d3eb812fd4 | 144 | && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 145 | { |
JMF | 0:24d3eb812fd4 | 146 | return false; |
JMF | 0:24d3eb812fd4 | 147 | } |
JMF | 0:24d3eb812fd4 | 148 | } |
JMF | 0:24d3eb812fd4 | 149 | |
JMF | 0:24d3eb812fd4 | 150 | if ( !(_parser.send("AT+WJOIN") && _parser.recv("[Link-Up Event]") |
JMF | 0:24d3eb812fd4 | 151 | && _parser.recv(" IP Addr : %[^\n]\r\n",_ip_buffer) |
JMF | 0:24d3eb812fd4 | 152 | && _parser.recv(" Gateway : %[^\n]\r\n",_gateway_buffer) |
JMF | 0:24d3eb812fd4 | 153 | && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 154 | { |
JMF | 0:24d3eb812fd4 | 155 | return false; |
JMF | 0:24d3eb812fd4 | 156 | } |
JMF | 0:24d3eb812fd4 | 157 | |
JMF | 0:24d3eb812fd4 | 158 | return true; |
JMF | 0:24d3eb812fd4 | 159 | } |
JMF | 0:24d3eb812fd4 | 160 | |
JMF | 0:24d3eb812fd4 | 161 | bool WizFi310::disconnect(void) |
JMF | 0:24d3eb812fd4 | 162 | { |
JMF | 0:24d3eb812fd4 | 163 | return _parser.send("AT+WLEAVE") && _parser.recv("[OK]"); |
JMF | 0:24d3eb812fd4 | 164 | } |
JMF | 0:24d3eb812fd4 | 165 | |
JMF | 0:24d3eb812fd4 | 166 | const char *WizFi310::getIPAddress(void) |
JMF | 0:24d3eb812fd4 | 167 | { |
JMF | 0:24d3eb812fd4 | 168 | if (!(_parser.send("AT+WSTATUS") && _parser.recv("IF/SSID/IP-Addr/Gateway/MAC/TxPower(dBm)/RSSI(-dBm)") |
JMF | 0:24d3eb812fd4 | 169 | && _parser.recv("%*[^/]/%*[^/]/%15[^/]/",_ip_buffer) |
JMF | 0:24d3eb812fd4 | 170 | && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 171 | { |
JMF | 0:24d3eb812fd4 | 172 | return 0; |
JMF | 0:24d3eb812fd4 | 173 | } |
JMF | 0:24d3eb812fd4 | 174 | |
JMF | 0:24d3eb812fd4 | 175 | return _ip_buffer; |
JMF | 0:24d3eb812fd4 | 176 | } |
JMF | 0:24d3eb812fd4 | 177 | |
JMF | 0:24d3eb812fd4 | 178 | const char *WizFi310::getMACAddress(void) |
JMF | 0:24d3eb812fd4 | 179 | { |
JMF | 0:24d3eb812fd4 | 180 | if (!(_parser.send("AT+MMAC=?") |
JMF | 0:24d3eb812fd4 | 181 | && _parser.recv("%[^\n]\r\n",_mac_buffer) |
JMF | 0:24d3eb812fd4 | 182 | && _parser.recv("[OK]"))) { |
JMF | 0:24d3eb812fd4 | 183 | return 0; |
JMF | 0:24d3eb812fd4 | 184 | } |
JMF | 0:24d3eb812fd4 | 185 | |
JMF | 0:24d3eb812fd4 | 186 | return _mac_buffer; |
JMF | 0:24d3eb812fd4 | 187 | } |
JMF | 0:24d3eb812fd4 | 188 | |
JMF | 0:24d3eb812fd4 | 189 | const char *WizFi310::getGateway() |
JMF | 0:24d3eb812fd4 | 190 | { |
JMF | 0:24d3eb812fd4 | 191 | return _gateway_buffer; |
JMF | 0:24d3eb812fd4 | 192 | } |
JMF | 0:24d3eb812fd4 | 193 | |
JMF | 0:24d3eb812fd4 | 194 | const char *WizFi310::getNetmask() |
JMF | 0:24d3eb812fd4 | 195 | { |
JMF | 0:24d3eb812fd4 | 196 | return _netmask_buffer; |
JMF | 0:24d3eb812fd4 | 197 | } |
JMF | 0:24d3eb812fd4 | 198 | |
JMF | 0:24d3eb812fd4 | 199 | int8_t WizFi310::getRSSI() |
JMF | 0:24d3eb812fd4 | 200 | { |
JMF | 0:24d3eb812fd4 | 201 | char rssi[3]; |
JMF | 0:24d3eb812fd4 | 202 | |
JMF | 0:24d3eb812fd4 | 203 | if (!(_parser.send("AT+WSTATUS") && _parser.recv("IF/SSID/IP-Addr/Gateway/MAC/TxPower(dBm)/RSSI(-dBm)") |
JMF | 0:24d3eb812fd4 | 204 | //&& _parser.recv("%*[^/]/%*[^/]/%*[^/]/%*[^/]/%*[^/]/%*[^/]/%[^\n]\r\n",&rssi) |
JMF | 0:24d3eb812fd4 | 205 | && _parser.recv("%*[^/]/%*[^/]/%*[^/]/%*[^/]/%*[^/]//%[^\n]\r\n",rssi) |
JMF | 0:24d3eb812fd4 | 206 | && _parser.recv("[OK]")) ) |
JMF | 0:24d3eb812fd4 | 207 | { |
JMF | 0:24d3eb812fd4 | 208 | return 0; |
JMF | 0:24d3eb812fd4 | 209 | } |
JMF | 0:24d3eb812fd4 | 210 | |
JMF | 0:24d3eb812fd4 | 211 | return atoi(rssi); |
JMF | 0:24d3eb812fd4 | 212 | } |
JMF | 0:24d3eb812fd4 | 213 | |
JMF | 0:24d3eb812fd4 | 214 | bool WizFi310::isConnected(void) |
JMF | 0:24d3eb812fd4 | 215 | { |
JMF | 0:24d3eb812fd4 | 216 | return getIPAddress() != 0; |
JMF | 0:24d3eb812fd4 | 217 | } |
JMF | 0:24d3eb812fd4 | 218 | |
JMF | 0:24d3eb812fd4 | 219 | int WizFi310::scan(WiFiAccessPoint *res, unsigned limit) |
JMF | 0:24d3eb812fd4 | 220 | { |
JMF | 0:24d3eb812fd4 | 221 | unsigned int cnt = 0; |
JMF | 0:24d3eb812fd4 | 222 | nsapi_wifi_ap_t ap; |
JMF | 0:24d3eb812fd4 | 223 | |
JMF | 0:24d3eb812fd4 | 224 | // Scan Time out : 50ms |
JMF | 0:24d3eb812fd4 | 225 | if (!(_parser.send("AT+WSCAN=,,,50") |
JMF | 0:24d3eb812fd4 | 226 | && _parser.recv("Index/SSID/BSSID/RSSI(-dBm)/MaxDataRate(Mbps)/Security/RadioBand(GHz)/Channel"))) |
JMF | 0:24d3eb812fd4 | 227 | { |
JMF | 0:24d3eb812fd4 | 228 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 229 | } |
JMF | 0:24d3eb812fd4 | 230 | |
JMF | 0:24d3eb812fd4 | 231 | while (recv_ap(&ap)) { |
JMF | 0:24d3eb812fd4 | 232 | if (cnt < limit) |
JMF | 0:24d3eb812fd4 | 233 | { |
JMF | 0:24d3eb812fd4 | 234 | res[cnt] = WiFiAccessPoint(ap); |
JMF | 0:24d3eb812fd4 | 235 | } |
JMF | 0:24d3eb812fd4 | 236 | cnt++; |
JMF | 0:24d3eb812fd4 | 237 | if (limit != 0 && cnt >= limit) |
JMF | 0:24d3eb812fd4 | 238 | { |
JMF | 0:24d3eb812fd4 | 239 | break; |
JMF | 0:24d3eb812fd4 | 240 | } |
JMF | 0:24d3eb812fd4 | 241 | } |
JMF | 0:24d3eb812fd4 | 242 | |
JMF | 0:24d3eb812fd4 | 243 | return cnt; |
JMF | 0:24d3eb812fd4 | 244 | } |
JMF | 0:24d3eb812fd4 | 245 | |
JMF | 0:24d3eb812fd4 | 246 | bool WizFi310::open(const char *type, int id, const char* addr, int port) |
JMF | 0:24d3eb812fd4 | 247 | { |
JMF | 0:24d3eb812fd4 | 248 | int created_sock_id; |
JMF | 0:24d3eb812fd4 | 249 | |
JMF | 0:24d3eb812fd4 | 250 | //IDs only 0-7 |
JMF | 0:24d3eb812fd4 | 251 | if(id > 7) { |
JMF | 0:24d3eb812fd4 | 252 | return false; |
JMF | 0:24d3eb812fd4 | 253 | } |
JMF | 0:24d3eb812fd4 | 254 | |
JMF | 0:24d3eb812fd4 | 255 | if( !(_parser.send("AT+SCON=O,%s,%s,%d,,0",type,addr,port) && _parser.recv("[OK]") |
JMF | 0:24d3eb812fd4 | 256 | && _parser.recv("[CONNECT %d]",&created_sock_id))) { |
JMF | 0:24d3eb812fd4 | 257 | return false; |
JMF | 0:24d3eb812fd4 | 258 | } |
JMF | 0:24d3eb812fd4 | 259 | |
JMF | 0:24d3eb812fd4 | 260 | if( created_sock_id != id ) { |
JMF | 0:24d3eb812fd4 | 261 | close(created_sock_id); |
JMF | 0:24d3eb812fd4 | 262 | return false; |
JMF | 0:24d3eb812fd4 | 263 | } |
JMF | 0:24d3eb812fd4 | 264 | |
JMF | 0:24d3eb812fd4 | 265 | return true; |
JMF | 0:24d3eb812fd4 | 266 | } |
JMF | 0:24d3eb812fd4 | 267 | |
JMF | 0:24d3eb812fd4 | 268 | bool WizFi310::dns_lookup(const char* name, char* ip) |
JMF | 0:24d3eb812fd4 | 269 | { |
JMF | 0:24d3eb812fd4 | 270 | return (_parser.send("AT+FDNS=%s,5000", name) && _parser.recv("%[^\n]\r\n",ip) && _parser.recv("[OK]")); |
JMF | 0:24d3eb812fd4 | 271 | } |
JMF | 0:24d3eb812fd4 | 272 | |
JMF | 0:24d3eb812fd4 | 273 | bool WizFi310::send(int id, const void *data, uint32_t amount) |
JMF | 0:24d3eb812fd4 | 274 | { |
JMF | 0:24d3eb812fd4 | 275 | char str_result[20]; |
JMF | 0:24d3eb812fd4 | 276 | |
JMF | 0:24d3eb812fd4 | 277 | if(id > 8) { |
JMF | 0:24d3eb812fd4 | 278 | return false; |
JMF | 0:24d3eb812fd4 | 279 | } |
JMF | 0:24d3eb812fd4 | 280 | |
JMF | 0:24d3eb812fd4 | 281 | sprintf(str_result,"[%d,,,%d]",id,(int)amount); |
JMF | 0:24d3eb812fd4 | 282 | |
JMF | 0:24d3eb812fd4 | 283 | // Using _parser.printf because MCU can't send CR LF |
JMF | 0:24d3eb812fd4 | 284 | if( _parser.printf("AT+SSEND=%d,,,%d\r",id, (int)amount) |
JMF | 0:24d3eb812fd4 | 285 | && _parser.recv(str_result) |
JMF | 0:24d3eb812fd4 | 286 | && _parser.write((char*)data, (int)amount) >= 0 |
JMF | 0:24d3eb812fd4 | 287 | && _parser.recv("[OK]") ){ |
JMF | 0:24d3eb812fd4 | 288 | return true; |
JMF | 0:24d3eb812fd4 | 289 | } |
JMF | 0:24d3eb812fd4 | 290 | |
JMF | 0:24d3eb812fd4 | 291 | return false; |
JMF | 0:24d3eb812fd4 | 292 | } |
JMF | 0:24d3eb812fd4 | 293 | |
JMF | 0:24d3eb812fd4 | 294 | void WizFi310::_packet_handler() |
JMF | 0:24d3eb812fd4 | 295 | { |
JMF | 0:24d3eb812fd4 | 296 | int id; |
JMF | 0:24d3eb812fd4 | 297 | char ip_addr[16]; |
JMF | 0:24d3eb812fd4 | 298 | int port; |
JMF | 0:24d3eb812fd4 | 299 | uint32_t amount; |
JMF | 0:24d3eb812fd4 | 300 | |
JMF | 0:24d3eb812fd4 | 301 | // parse out the packet |
JMF | 0:24d3eb812fd4 | 302 | _parser.set_timeout(AT_CMD_PARSER_RECV_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 303 | if (!_parser.recv("%d,%[^,],%d,%d}",&id, ip_addr,&port, &amount) ) { |
JMF | 0:24d3eb812fd4 | 304 | setTimeout(_timeout_ms); |
JMF | 0:24d3eb812fd4 | 305 | return; |
JMF | 0:24d3eb812fd4 | 306 | } |
JMF | 0:24d3eb812fd4 | 307 | |
JMF | 0:24d3eb812fd4 | 308 | struct packet *packet = new struct packet; |
JMF | 0:24d3eb812fd4 | 309 | if (!packet) { |
JMF | 0:24d3eb812fd4 | 310 | return; |
JMF | 0:24d3eb812fd4 | 311 | } |
JMF | 0:24d3eb812fd4 | 312 | |
JMF | 0:24d3eb812fd4 | 313 | packet->id = id; |
JMF | 0:24d3eb812fd4 | 314 | packet->len = amount; |
JMF | 0:24d3eb812fd4 | 315 | packet->next = 0; |
JMF | 0:24d3eb812fd4 | 316 | packet->data = (char*)malloc(amount); |
JMF | 0:24d3eb812fd4 | 317 | |
JMF | 0:24d3eb812fd4 | 318 | |
JMF | 0:24d3eb812fd4 | 319 | if (!(_parser.read((char*)packet->data, amount))) { |
JMF | 0:24d3eb812fd4 | 320 | free(packet); |
JMF | 0:24d3eb812fd4 | 321 | setTimeout(_timeout_ms); |
JMF | 0:24d3eb812fd4 | 322 | return; |
JMF | 0:24d3eb812fd4 | 323 | } |
JMF | 0:24d3eb812fd4 | 324 | setTimeout(_timeout_ms); |
JMF | 0:24d3eb812fd4 | 325 | |
JMF | 0:24d3eb812fd4 | 326 | *_packets_end = packet; |
JMF | 0:24d3eb812fd4 | 327 | _packets_end = &packet->next; |
JMF | 0:24d3eb812fd4 | 328 | } |
JMF | 0:24d3eb812fd4 | 329 | |
JMF | 0:24d3eb812fd4 | 330 | int32_t WizFi310::recv(int id, void *data, uint32_t amount) |
JMF | 0:24d3eb812fd4 | 331 | { |
JMF | 0:24d3eb812fd4 | 332 | while (true) { |
JMF | 0:24d3eb812fd4 | 333 | // check if any packets are ready for us |
JMF | 0:24d3eb812fd4 | 334 | for (struct packet **p = &_packets; *p; p = &(*p)->next) { |
JMF | 0:24d3eb812fd4 | 335 | if ((*p)->id == id) { |
JMF | 0:24d3eb812fd4 | 336 | struct packet *q = *p; |
JMF | 0:24d3eb812fd4 | 337 | |
JMF | 0:24d3eb812fd4 | 338 | if (q->len <= amount) { |
JMF | 0:24d3eb812fd4 | 339 | memcpy(data,q->data, q->len); |
JMF | 0:24d3eb812fd4 | 340 | |
JMF | 0:24d3eb812fd4 | 341 | if (_packets_end == &(*p)->next) { |
JMF | 0:24d3eb812fd4 | 342 | _packets_end = p; |
JMF | 0:24d3eb812fd4 | 343 | } |
JMF | 0:24d3eb812fd4 | 344 | *p = (*p)->next; |
JMF | 0:24d3eb812fd4 | 345 | |
JMF | 0:24d3eb812fd4 | 346 | uint32_t len = q->len; |
JMF | 0:24d3eb812fd4 | 347 | free(q); |
JMF | 0:24d3eb812fd4 | 348 | return len; |
JMF | 0:24d3eb812fd4 | 349 | } else { // return only partial packet |
JMF | 0:24d3eb812fd4 | 350 | memcpy(data, q->data, amount); |
JMF | 0:24d3eb812fd4 | 351 | |
JMF | 0:24d3eb812fd4 | 352 | q->len -= amount; |
JMF | 0:24d3eb812fd4 | 353 | memmove(q->data, (uint8_t*)(q->data) + amount, q->len); |
JMF | 0:24d3eb812fd4 | 354 | return amount; |
JMF | 0:24d3eb812fd4 | 355 | } |
JMF | 0:24d3eb812fd4 | 356 | } |
JMF | 0:24d3eb812fd4 | 357 | } |
JMF | 0:24d3eb812fd4 | 358 | |
JMF | 0:24d3eb812fd4 | 359 | // check for inbound packets |
JMF | 0:24d3eb812fd4 | 360 | if (!_parser.process_oob()) { |
JMF | 0:24d3eb812fd4 | 361 | return -1; |
JMF | 0:24d3eb812fd4 | 362 | } |
JMF | 0:24d3eb812fd4 | 363 | } |
JMF | 0:24d3eb812fd4 | 364 | } |
JMF | 0:24d3eb812fd4 | 365 | |
JMF | 0:24d3eb812fd4 | 366 | bool WizFi310::close(int id) |
JMF | 0:24d3eb812fd4 | 367 | { |
JMF | 0:24d3eb812fd4 | 368 | char sock_event_msg[15]; |
JMF | 0:24d3eb812fd4 | 369 | |
JMF | 0:24d3eb812fd4 | 370 | if(id > 7) { |
JMF | 0:24d3eb812fd4 | 371 | return false; |
JMF | 0:24d3eb812fd4 | 372 | } |
JMF | 0:24d3eb812fd4 | 373 | |
JMF | 0:24d3eb812fd4 | 374 | if (_parser.send("AT+SMGMT=%d", id) && _parser.recv(sock_event_msg) && _parser.recv("[OK]") ) |
JMF | 0:24d3eb812fd4 | 375 | { |
JMF | 0:24d3eb812fd4 | 376 | return true; |
JMF | 0:24d3eb812fd4 | 377 | } |
JMF | 0:24d3eb812fd4 | 378 | |
JMF | 0:24d3eb812fd4 | 379 | return false; |
JMF | 0:24d3eb812fd4 | 380 | } |
JMF | 0:24d3eb812fd4 | 381 | |
JMF | 0:24d3eb812fd4 | 382 | void WizFi310::setTimeout(uint32_t timeout_ms) |
JMF | 0:24d3eb812fd4 | 383 | { |
JMF | 0:24d3eb812fd4 | 384 | _parser.set_timeout(timeout_ms); |
JMF | 0:24d3eb812fd4 | 385 | _timeout_ms = timeout_ms; |
JMF | 0:24d3eb812fd4 | 386 | } |
JMF | 0:24d3eb812fd4 | 387 | |
JMF | 0:24d3eb812fd4 | 388 | bool WizFi310::readable() |
JMF | 0:24d3eb812fd4 | 389 | { |
JMF | 0:24d3eb812fd4 | 390 | return _serial.FileHandle::readable(); |
JMF | 0:24d3eb812fd4 | 391 | } |
JMF | 0:24d3eb812fd4 | 392 | |
JMF | 0:24d3eb812fd4 | 393 | bool WizFi310::writeable() |
JMF | 0:24d3eb812fd4 | 394 | { |
JMF | 0:24d3eb812fd4 | 395 | return _serial.FileHandle::writable(); |
JMF | 0:24d3eb812fd4 | 396 | } |
JMF | 0:24d3eb812fd4 | 397 | |
JMF | 0:24d3eb812fd4 | 398 | void WizFi310::attach(Callback<void()> func) |
JMF | 0:24d3eb812fd4 | 399 | { |
JMF | 0:24d3eb812fd4 | 400 | _serial.sigio(func); |
JMF | 0:24d3eb812fd4 | 401 | } |
JMF | 0:24d3eb812fd4 | 402 | |
JMF | 0:24d3eb812fd4 | 403 | bool WizFi310::recv_ap(nsapi_wifi_ap_t *ap) |
JMF | 0:24d3eb812fd4 | 404 | { |
JMF | 0:24d3eb812fd4 | 405 | char scan_result[100]; |
JMF | 0:24d3eb812fd4 | 406 | char sec[10]; |
JMF | 0:24d3eb812fd4 | 407 | char bssid[32]; |
JMF | 0:24d3eb812fd4 | 408 | char* idx_ptr; |
JMF | 0:24d3eb812fd4 | 409 | char* bssid_ptr; |
JMF | 0:24d3eb812fd4 | 410 | |
JMF | 0:24d3eb812fd4 | 411 | _parser.recv("%s\r\n",scan_result); |
JMF | 0:24d3eb812fd4 | 412 | if( strcmp(scan_result,"[OK]") == 0 ) |
JMF | 0:24d3eb812fd4 | 413 | { |
JMF | 0:24d3eb812fd4 | 414 | return false; |
JMF | 0:24d3eb812fd4 | 415 | } |
JMF | 0:24d3eb812fd4 | 416 | |
JMF | 0:24d3eb812fd4 | 417 | idx_ptr = strtok((char*)scan_result, "/"); // index |
JMF | 0:24d3eb812fd4 | 418 | |
JMF | 0:24d3eb812fd4 | 419 | idx_ptr = strtok( NULL, "/" ); // ssid |
JMF | 0:24d3eb812fd4 | 420 | strncpy(ap->ssid,idx_ptr,strlen(idx_ptr)); |
JMF | 0:24d3eb812fd4 | 421 | ap->ssid[strlen(idx_ptr)] = '\0'; |
JMF | 0:24d3eb812fd4 | 422 | |
JMF | 0:24d3eb812fd4 | 423 | idx_ptr = strtok( NULL, "/" ); // bssid |
JMF | 0:24d3eb812fd4 | 424 | strncpy(bssid,idx_ptr,strlen(idx_ptr)); |
JMF | 0:24d3eb812fd4 | 425 | bssid[strlen(idx_ptr)] = '\0'; |
JMF | 0:24d3eb812fd4 | 426 | |
JMF | 0:24d3eb812fd4 | 427 | idx_ptr = strtok( NULL, "/" ); // RSSI |
JMF | 0:24d3eb812fd4 | 428 | ap->rssi = atoi(idx_ptr); |
JMF | 0:24d3eb812fd4 | 429 | |
JMF | 0:24d3eb812fd4 | 430 | //idx_ptr = strtok( NULL, "/" ); // DataRate |
JMF | 0:24d3eb812fd4 | 431 | |
JMF | 0:24d3eb812fd4 | 432 | idx_ptr = strtok( NULL, "/" ); // Security |
JMF | 0:24d3eb812fd4 | 433 | strncpy(sec,idx_ptr,strlen(idx_ptr)); |
JMF | 0:24d3eb812fd4 | 434 | sec[strlen(idx_ptr)] = '\0'; |
JMF | 0:24d3eb812fd4 | 435 | ap->security = str2sec(sec); |
JMF | 0:24d3eb812fd4 | 436 | |
JMF | 0:24d3eb812fd4 | 437 | idx_ptr = strtok( NULL, "/" ); // RadioBand |
JMF | 0:24d3eb812fd4 | 438 | |
JMF | 0:24d3eb812fd4 | 439 | idx_ptr = strtok( NULL, "/" ); // Channel |
JMF | 0:24d3eb812fd4 | 440 | ap->channel = atoi(idx_ptr); |
JMF | 0:24d3eb812fd4 | 441 | |
JMF | 0:24d3eb812fd4 | 442 | // Set BSSID |
JMF | 0:24d3eb812fd4 | 443 | bssid_ptr = strtok( (char*)bssid, ":"); |
JMF | 0:24d3eb812fd4 | 444 | ap->bssid[0] = hex_str_to_int(bssid_ptr); |
JMF | 0:24d3eb812fd4 | 445 | |
JMF | 0:24d3eb812fd4 | 446 | for(int i=1; i<6; i++) |
JMF | 0:24d3eb812fd4 | 447 | { |
JMF | 0:24d3eb812fd4 | 448 | bssid_ptr = strtok( NULL, ":"); |
JMF | 0:24d3eb812fd4 | 449 | ap->bssid[i] = hex_str_to_int(bssid_ptr); |
JMF | 0:24d3eb812fd4 | 450 | } |
JMF | 0:24d3eb812fd4 | 451 | |
JMF | 0:24d3eb812fd4 | 452 | return true; |
JMF | 0:24d3eb812fd4 | 453 | } |
JMF | 0:24d3eb812fd4 | 454 | |
JMF | 0:24d3eb812fd4 | 455 | nsapi_security_t WizFi310::str2sec(const char *str_sec) |
JMF | 0:24d3eb812fd4 | 456 | { |
JMF | 0:24d3eb812fd4 | 457 | if( strcmp(str_sec,"Open") == 0 ) |
JMF | 0:24d3eb812fd4 | 458 | { |
JMF | 0:24d3eb812fd4 | 459 | return NSAPI_SECURITY_NONE; |
JMF | 0:24d3eb812fd4 | 460 | } |
JMF | 0:24d3eb812fd4 | 461 | else if( strcmp(str_sec,"WEP") == 0 ) |
JMF | 0:24d3eb812fd4 | 462 | { |
JMF | 0:24d3eb812fd4 | 463 | return NSAPI_SECURITY_WEP; |
JMF | 0:24d3eb812fd4 | 464 | } |
JMF | 0:24d3eb812fd4 | 465 | else if( strcmp(str_sec,"WPA") == 0 ) |
JMF | 0:24d3eb812fd4 | 466 | { |
JMF | 0:24d3eb812fd4 | 467 | return NSAPI_SECURITY_WPA; |
JMF | 0:24d3eb812fd4 | 468 | } |
JMF | 0:24d3eb812fd4 | 469 | else if( strcmp(str_sec,"WPA2") == 0 ) |
JMF | 0:24d3eb812fd4 | 470 | { |
JMF | 0:24d3eb812fd4 | 471 | return NSAPI_SECURITY_WPA2; |
JMF | 0:24d3eb812fd4 | 472 | } |
JMF | 0:24d3eb812fd4 | 473 | else if( strcmp(str_sec,"WPAWPA2") == 0 ) |
JMF | 0:24d3eb812fd4 | 474 | { |
JMF | 0:24d3eb812fd4 | 475 | return NSAPI_SECURITY_WPA_WPA2; |
JMF | 0:24d3eb812fd4 | 476 | } |
JMF | 0:24d3eb812fd4 | 477 | |
JMF | 0:24d3eb812fd4 | 478 | return NSAPI_SECURITY_UNKNOWN; |
JMF | 0:24d3eb812fd4 | 479 | } |
JMF | 0:24d3eb812fd4 | 480 | |
JMF | 0:24d3eb812fd4 | 481 | int WizFi310::hex_str_to_int(const char* hex_str) |
JMF | 0:24d3eb812fd4 | 482 | { |
JMF | 0:24d3eb812fd4 | 483 | int n = 0; |
JMF | 0:24d3eb812fd4 | 484 | uint32_t value = 0; |
JMF | 0:24d3eb812fd4 | 485 | int shift = 7; |
JMF | 0:24d3eb812fd4 | 486 | while (hex_str[n] != '\0' && n < 8) |
JMF | 0:24d3eb812fd4 | 487 | { |
JMF | 0:24d3eb812fd4 | 488 | if ( hex_str[n] > 0x21 && hex_str[n] < 0x40 ) |
JMF | 0:24d3eb812fd4 | 489 | { |
JMF | 0:24d3eb812fd4 | 490 | value |= (hex_str[n] & 0x0f) << (shift << 2); |
JMF | 0:24d3eb812fd4 | 491 | } |
JMF | 0:24d3eb812fd4 | 492 | else if ( (hex_str[n] >= 'a' && hex_str[n] <= 'f') || (hex_str[n] >= 'A' && hex_str[n] <= 'F') ) |
JMF | 0:24d3eb812fd4 | 493 | { |
JMF | 0:24d3eb812fd4 | 494 | value |= ((hex_str[n] & 0x0f) + 9) << (shift << 2); |
JMF | 0:24d3eb812fd4 | 495 | } |
JMF | 0:24d3eb812fd4 | 496 | else |
JMF | 0:24d3eb812fd4 | 497 | { |
JMF | 0:24d3eb812fd4 | 498 | break; |
JMF | 0:24d3eb812fd4 | 499 | } |
JMF | 0:24d3eb812fd4 | 500 | n++; |
JMF | 0:24d3eb812fd4 | 501 | shift--; |
JMF | 0:24d3eb812fd4 | 502 | } |
JMF | 0:24d3eb812fd4 | 503 | |
JMF | 0:24d3eb812fd4 | 504 | return (value >> ((shift + 1) << 2)); |
JMF | 0:24d3eb812fd4 | 505 | } |
JMF | 0:24d3eb812fd4 | 506 |