wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Committer:
JMF
Date:
Wed Sep 05 14:28:24 2018 +0000
Revision:
0:24d3eb812fd4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:24d3eb812fd4 1 /* ESP8266Interface Example
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 #ifndef ESP8266_H
JMF 0:24d3eb812fd4 18 #define ESP8266_H
JMF 0:24d3eb812fd4 19
JMF 0:24d3eb812fd4 20 #include "ATCmdParser.h"
JMF 0:24d3eb812fd4 21 #include "nsapi_types.h"
JMF 0:24d3eb812fd4 22 #include "rtos.h"
JMF 0:24d3eb812fd4 23
JMF 0:24d3eb812fd4 24 // Various timeouts for different ESP8266 operations
JMF 0:24d3eb812fd4 25 #ifndef ESP8266_CONNECT_TIMEOUT
JMF 0:24d3eb812fd4 26 #define ESP8266_CONNECT_TIMEOUT 15000
JMF 0:24d3eb812fd4 27 #endif
JMF 0:24d3eb812fd4 28 #ifndef ESP8266_SEND_TIMEOUT
JMF 0:24d3eb812fd4 29 #define ESP8266_SEND_TIMEOUT 2000
JMF 0:24d3eb812fd4 30 #endif
JMF 0:24d3eb812fd4 31 #ifndef ESP8266_RECV_TIMEOUT
JMF 0:24d3eb812fd4 32 #define ESP8266_RECV_TIMEOUT 2000
JMF 0:24d3eb812fd4 33 #endif
JMF 0:24d3eb812fd4 34 #ifndef ESP8266_MISC_TIMEOUT
JMF 0:24d3eb812fd4 35 #define ESP8266_MISC_TIMEOUT 2000
JMF 0:24d3eb812fd4 36 #endif
JMF 0:24d3eb812fd4 37
JMF 0:24d3eb812fd4 38 /** ESP8266Interface class.
JMF 0:24d3eb812fd4 39 This is an interface to a ESP8266 radio.
JMF 0:24d3eb812fd4 40 */
JMF 0:24d3eb812fd4 41 class ESP8266
JMF 0:24d3eb812fd4 42 {
JMF 0:24d3eb812fd4 43 public:
JMF 0:24d3eb812fd4 44 ESP8266(PinName tx, PinName rx, bool debug=false);
JMF 0:24d3eb812fd4 45
JMF 0:24d3eb812fd4 46 /**
JMF 0:24d3eb812fd4 47 * Check firmware version of ESP8266
JMF 0:24d3eb812fd4 48 *
JMF 0:24d3eb812fd4 49 * @return integer firmware version or -1 if firmware query command gives outdated response
JMF 0:24d3eb812fd4 50 */
JMF 0:24d3eb812fd4 51 int get_firmware_version(void);
JMF 0:24d3eb812fd4 52
JMF 0:24d3eb812fd4 53 /**
JMF 0:24d3eb812fd4 54 * Startup the ESP8266
JMF 0:24d3eb812fd4 55 *
JMF 0:24d3eb812fd4 56 * @param mode mode of WIFI 1-client, 2-host, 3-both
JMF 0:24d3eb812fd4 57 * @return true only if ESP8266 was setup correctly
JMF 0:24d3eb812fd4 58 */
JMF 0:24d3eb812fd4 59 bool startup(int mode);
JMF 0:24d3eb812fd4 60
JMF 0:24d3eb812fd4 61 /**
JMF 0:24d3eb812fd4 62 * Reset ESP8266
JMF 0:24d3eb812fd4 63 *
JMF 0:24d3eb812fd4 64 * @return true only if ESP8266 resets successfully
JMF 0:24d3eb812fd4 65 */
JMF 0:24d3eb812fd4 66 bool reset(void);
JMF 0:24d3eb812fd4 67
JMF 0:24d3eb812fd4 68 /**
JMF 0:24d3eb812fd4 69 * Enable/Disable DHCP
JMF 0:24d3eb812fd4 70 *
JMF 0:24d3eb812fd4 71 * @param enabled DHCP enabled when true
JMF 0:24d3eb812fd4 72 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
JMF 0:24d3eb812fd4 73 * @return true only if ESP8266 enables/disables DHCP successfully
JMF 0:24d3eb812fd4 74 */
JMF 0:24d3eb812fd4 75 bool dhcp(bool enabled, int mode);
JMF 0:24d3eb812fd4 76
JMF 0:24d3eb812fd4 77 /**
JMF 0:24d3eb812fd4 78 * Connect ESP8266 to AP
JMF 0:24d3eb812fd4 79 *
JMF 0:24d3eb812fd4 80 * @param ap the name of the AP
JMF 0:24d3eb812fd4 81 * @param passPhrase the password of AP
JMF 0:24d3eb812fd4 82 * @return NSAPI_ERROR_OK only if ESP8266 is connected successfully
JMF 0:24d3eb812fd4 83 */
JMF 0:24d3eb812fd4 84 nsapi_error_t connect(const char *ap, const char *passPhrase);
JMF 0:24d3eb812fd4 85
JMF 0:24d3eb812fd4 86 /**
JMF 0:24d3eb812fd4 87 * Disconnect ESP8266 from AP
JMF 0:24d3eb812fd4 88 *
JMF 0:24d3eb812fd4 89 * @return true only if ESP8266 is disconnected successfully
JMF 0:24d3eb812fd4 90 */
JMF 0:24d3eb812fd4 91 bool disconnect(void);
JMF 0:24d3eb812fd4 92
JMF 0:24d3eb812fd4 93 /**
JMF 0:24d3eb812fd4 94 * Get the IP address of ESP8266
JMF 0:24d3eb812fd4 95 *
JMF 0:24d3eb812fd4 96 * @return null-teriminated IP address or null if no IP address is assigned
JMF 0:24d3eb812fd4 97 */
JMF 0:24d3eb812fd4 98 const char *getIPAddress(void);
JMF 0:24d3eb812fd4 99
JMF 0:24d3eb812fd4 100 /**
JMF 0:24d3eb812fd4 101 * Get the MAC address of ESP8266
JMF 0:24d3eb812fd4 102 *
JMF 0:24d3eb812fd4 103 * @return null-terminated MAC address or null if no MAC address is assigned
JMF 0:24d3eb812fd4 104 */
JMF 0:24d3eb812fd4 105 const char *getMACAddress(void);
JMF 0:24d3eb812fd4 106
JMF 0:24d3eb812fd4 107 /** Get the local gateway
JMF 0:24d3eb812fd4 108 *
JMF 0:24d3eb812fd4 109 * @return Null-terminated representation of the local gateway
JMF 0:24d3eb812fd4 110 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 111 */
JMF 0:24d3eb812fd4 112 const char *getGateway();
JMF 0:24d3eb812fd4 113
JMF 0:24d3eb812fd4 114 /** Get the local network mask
JMF 0:24d3eb812fd4 115 *
JMF 0:24d3eb812fd4 116 * @return Null-terminated representation of the local network mask
JMF 0:24d3eb812fd4 117 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 118 */
JMF 0:24d3eb812fd4 119 const char *getNetmask();
JMF 0:24d3eb812fd4 120
JMF 0:24d3eb812fd4 121 /* Return RSSI for active connection
JMF 0:24d3eb812fd4 122 *
JMF 0:24d3eb812fd4 123 * @return Measured RSSI
JMF 0:24d3eb812fd4 124 */
JMF 0:24d3eb812fd4 125 int8_t getRSSI();
JMF 0:24d3eb812fd4 126
JMF 0:24d3eb812fd4 127 /** Scan for available networks
JMF 0:24d3eb812fd4 128 *
JMF 0:24d3eb812fd4 129 * @param ap Pointer to allocated array to store discovered AP
JMF 0:24d3eb812fd4 130 * @param limit Size of allocated @a res array, or 0 to only count available AP
JMF 0:24d3eb812fd4 131 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
JMF 0:24d3eb812fd4 132 * see @a nsapi_error
JMF 0:24d3eb812fd4 133 */
JMF 0:24d3eb812fd4 134 int scan(WiFiAccessPoint *res, unsigned limit);
JMF 0:24d3eb812fd4 135
JMF 0:24d3eb812fd4 136 /**Perform a dns query
JMF 0:24d3eb812fd4 137 *
JMF 0:24d3eb812fd4 138 * @param name Hostname to resolve
JMF 0:24d3eb812fd4 139 * @param ip Buffer to store IP address
JMF 0:24d3eb812fd4 140 * @return 0 true on success, false on failure
JMF 0:24d3eb812fd4 141 */
JMF 0:24d3eb812fd4 142 bool dns_lookup(const char *name, char *ip);
JMF 0:24d3eb812fd4 143
JMF 0:24d3eb812fd4 144 /**
JMF 0:24d3eb812fd4 145 * Open a socketed connection
JMF 0:24d3eb812fd4 146 *
JMF 0:24d3eb812fd4 147 * @param type the type of socket to open "UDP" or "TCP"
JMF 0:24d3eb812fd4 148 * @param id id to give the new socket, valid 0-4
JMF 0:24d3eb812fd4 149 * @param port port to open connection with
JMF 0:24d3eb812fd4 150 * @param addr the IP address of the destination
JMF 0:24d3eb812fd4 151 * @param port the port on the destination
JMF 0:24d3eb812fd4 152 * @param local_port UDP socket's local port, zero means any
JMF 0:24d3eb812fd4 153 * @return true only if socket opened successfully
JMF 0:24d3eb812fd4 154 */
JMF 0:24d3eb812fd4 155 nsapi_error_t open_udp(int id, const char* addr, int port, int local_port = 0);
JMF 0:24d3eb812fd4 156
JMF 0:24d3eb812fd4 157 /**
JMF 0:24d3eb812fd4 158 * Open a socketed connection
JMF 0:24d3eb812fd4 159 *
JMF 0:24d3eb812fd4 160 * @param type the type of socket to open "UDP" or "TCP"
JMF 0:24d3eb812fd4 161 * @param id id to give the new socket, valid 0-4
JMF 0:24d3eb812fd4 162 * @param port port to open connection with
JMF 0:24d3eb812fd4 163 * @param addr the IP address of the destination
JMF 0:24d3eb812fd4 164 * @param port the port on the destination
JMF 0:24d3eb812fd4 165 * @param tcp_keepalive TCP connection's keep alive time, zero means disabled
JMF 0:24d3eb812fd4 166 * @return true only if socket opened successfully
JMF 0:24d3eb812fd4 167 */
JMF 0:24d3eb812fd4 168 bool open_tcp(int id, const char* addr, int port, int keepalive = 0);
JMF 0:24d3eb812fd4 169
JMF 0:24d3eb812fd4 170 /**
JMF 0:24d3eb812fd4 171 * Sends data to an open socket
JMF 0:24d3eb812fd4 172 *
JMF 0:24d3eb812fd4 173 * @param id id of socket to send to
JMF 0:24d3eb812fd4 174 * @param data data to be sent
JMF 0:24d3eb812fd4 175 * @param amount amount of data to be sent - max 1024
JMF 0:24d3eb812fd4 176 * @return NSAPI_ERROR_OK in success, negative error code in failure
JMF 0:24d3eb812fd4 177 */
JMF 0:24d3eb812fd4 178 nsapi_error_t send(int id, const void *data, uint32_t amount);
JMF 0:24d3eb812fd4 179
JMF 0:24d3eb812fd4 180 /**
JMF 0:24d3eb812fd4 181 * Receives datagram from an open UDP socket
JMF 0:24d3eb812fd4 182 *
JMF 0:24d3eb812fd4 183 * @param id id to receive from
JMF 0:24d3eb812fd4 184 * @param data placeholder for returned information
JMF 0:24d3eb812fd4 185 * @param amount number of bytes to be received
JMF 0:24d3eb812fd4 186 * @return the number of bytes received
JMF 0:24d3eb812fd4 187 */
JMF 0:24d3eb812fd4 188 int32_t recv_udp(int id, void *data, uint32_t amount, uint32_t timeout=ESP8266_RECV_TIMEOUT);
JMF 0:24d3eb812fd4 189
JMF 0:24d3eb812fd4 190 /**
JMF 0:24d3eb812fd4 191 * Receives stream data from an open TCP socket
JMF 0:24d3eb812fd4 192 *
JMF 0:24d3eb812fd4 193 * @param id id to receive from
JMF 0:24d3eb812fd4 194 * @param data placeholder for returned information
JMF 0:24d3eb812fd4 195 * @param amount number of bytes to be received
JMF 0:24d3eb812fd4 196 * @return the number of bytes received
JMF 0:24d3eb812fd4 197 */
JMF 0:24d3eb812fd4 198 int32_t recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout=ESP8266_RECV_TIMEOUT);
JMF 0:24d3eb812fd4 199
JMF 0:24d3eb812fd4 200 /**
JMF 0:24d3eb812fd4 201 * Closes a socket
JMF 0:24d3eb812fd4 202 *
JMF 0:24d3eb812fd4 203 * @param id id of socket to close, valid only 0-4
JMF 0:24d3eb812fd4 204 * @return true only if socket is closed successfully
JMF 0:24d3eb812fd4 205 */
JMF 0:24d3eb812fd4 206 bool close(int id);
JMF 0:24d3eb812fd4 207
JMF 0:24d3eb812fd4 208 /**
JMF 0:24d3eb812fd4 209 * Allows timeout to be changed between commands
JMF 0:24d3eb812fd4 210 *
JMF 0:24d3eb812fd4 211 * @param timeout_ms timeout of the connection
JMF 0:24d3eb812fd4 212 */
JMF 0:24d3eb812fd4 213 void setTimeout(uint32_t timeout_ms=ESP8266_MISC_TIMEOUT);
JMF 0:24d3eb812fd4 214
JMF 0:24d3eb812fd4 215 /**
JMF 0:24d3eb812fd4 216 * Checks if data is available
JMF 0:24d3eb812fd4 217 */
JMF 0:24d3eb812fd4 218 bool readable();
JMF 0:24d3eb812fd4 219
JMF 0:24d3eb812fd4 220 /**
JMF 0:24d3eb812fd4 221 * Checks if data can be written
JMF 0:24d3eb812fd4 222 */
JMF 0:24d3eb812fd4 223 bool writeable();
JMF 0:24d3eb812fd4 224
JMF 0:24d3eb812fd4 225 /**
JMF 0:24d3eb812fd4 226 * Attach a function to call whenever sigio happens in the serial
JMF 0:24d3eb812fd4 227 *
JMF 0:24d3eb812fd4 228 * @param func A pointer to a void function, or 0 to set as none
JMF 0:24d3eb812fd4 229 */
JMF 0:24d3eb812fd4 230 void sigio(Callback<void()> func);
JMF 0:24d3eb812fd4 231
JMF 0:24d3eb812fd4 232 /**
JMF 0:24d3eb812fd4 233 * Attach a function to call whenever sigio happens in the serial
JMF 0:24d3eb812fd4 234 *
JMF 0:24d3eb812fd4 235 * @param obj pointer to the object to call the member function on
JMF 0:24d3eb812fd4 236 * @param method pointer to the member function to call
JMF 0:24d3eb812fd4 237 */
JMF 0:24d3eb812fd4 238 template <typename T, typename M>
JMF 0:24d3eb812fd4 239 void sigio(T *obj, M method) {
JMF 0:24d3eb812fd4 240 sigio(Callback<void()>(obj, method));
JMF 0:24d3eb812fd4 241 }
JMF 0:24d3eb812fd4 242
JMF 0:24d3eb812fd4 243 /**
JMF 0:24d3eb812fd4 244 * Attach a function to call whenever network state has changed
JMF 0:24d3eb812fd4 245 *
JMF 0:24d3eb812fd4 246 * @param func A pointer to a void function, or 0 to set as none
JMF 0:24d3eb812fd4 247 */
JMF 0:24d3eb812fd4 248 void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
JMF 0:24d3eb812fd4 249
JMF 0:24d3eb812fd4 250 /**
JMF 0:24d3eb812fd4 251 * Read default Wifi mode from flash
JMF 0:24d3eb812fd4 252 *
JMF 0:24d3eb812fd4 253 * return Station, SoftAP or SoftAP+Station - 0 on failure
JMF 0:24d3eb812fd4 254 */
JMF 0:24d3eb812fd4 255 int8_t get_default_wifi_mode();
JMF 0:24d3eb812fd4 256
JMF 0:24d3eb812fd4 257 /**
JMF 0:24d3eb812fd4 258 * Write default Wifi mode to flash
JMF 0:24d3eb812fd4 259 */
JMF 0:24d3eb812fd4 260 bool set_default_wifi_mode(const int8_t mode);
JMF 0:24d3eb812fd4 261
JMF 0:24d3eb812fd4 262 /** Get the connection status
JMF 0:24d3eb812fd4 263 *
JMF 0:24d3eb812fd4 264 * @return The connection status according to ConnectionStatusType
JMF 0:24d3eb812fd4 265 */
JMF 0:24d3eb812fd4 266 nsapi_connection_status_t get_connection_status() const;
JMF 0:24d3eb812fd4 267
JMF 0:24d3eb812fd4 268 static const int8_t WIFIMODE_STATION = 1;
JMF 0:24d3eb812fd4 269 static const int8_t WIFIMODE_SOFTAP = 2;
JMF 0:24d3eb812fd4 270 static const int8_t WIFIMODE_STATION_SOFTAP = 3;
JMF 0:24d3eb812fd4 271 static const int8_t SOCKET_COUNT = 5;
JMF 0:24d3eb812fd4 272
JMF 0:24d3eb812fd4 273 private:
JMF 0:24d3eb812fd4 274 UARTSerial _serial;
JMF 0:24d3eb812fd4 275 ATCmdParser _parser;
JMF 0:24d3eb812fd4 276 Mutex _smutex; // Protect serial port access
JMF 0:24d3eb812fd4 277
JMF 0:24d3eb812fd4 278 struct packet {
JMF 0:24d3eb812fd4 279 struct packet *next;
JMF 0:24d3eb812fd4 280 int id;
JMF 0:24d3eb812fd4 281 uint32_t len;
JMF 0:24d3eb812fd4 282 // data follows
JMF 0:24d3eb812fd4 283 } *_packets, **_packets_end;
JMF 0:24d3eb812fd4 284 void _packet_handler();
JMF 0:24d3eb812fd4 285 void _connect_error_handler();
JMF 0:24d3eb812fd4 286 bool recv_ap(nsapi_wifi_ap_t *ap);
JMF 0:24d3eb812fd4 287 void _oob_socket0_closed_handler();
JMF 0:24d3eb812fd4 288 void _oob_socket1_closed_handler();
JMF 0:24d3eb812fd4 289 void _oob_socket2_closed_handler();
JMF 0:24d3eb812fd4 290 void _oob_socket3_closed_handler();
JMF 0:24d3eb812fd4 291 void _oob_socket4_closed_handler();
JMF 0:24d3eb812fd4 292 void _connection_status_handler();
JMF 0:24d3eb812fd4 293 void _oob_socket_close_error();
JMF 0:24d3eb812fd4 294 void _clear_socket_packets(int id);
JMF 0:24d3eb812fd4 295
JMF 0:24d3eb812fd4 296 char _ip_buffer[16];
JMF 0:24d3eb812fd4 297 char _gateway_buffer[16];
JMF 0:24d3eb812fd4 298 char _netmask_buffer[16];
JMF 0:24d3eb812fd4 299 char _mac_buffer[18];
JMF 0:24d3eb812fd4 300
JMF 0:24d3eb812fd4 301 int _connect_error;
JMF 0:24d3eb812fd4 302 bool _fail;
JMF 0:24d3eb812fd4 303 bool _closed;
JMF 0:24d3eb812fd4 304 int _socket_open[SOCKET_COUNT];
JMF 0:24d3eb812fd4 305 nsapi_connection_status_t _connection_status;
JMF 0:24d3eb812fd4 306 Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
JMF 0:24d3eb812fd4 307 };
JMF 0:24d3eb812fd4 308
JMF 0:24d3eb812fd4 309 #endif