mayuresh bharmoria / Mbed OS mbed-os-example-wifi
Committer:
mayur098
Date:
Thu Jun 21 17:50:21 2018 +0000
Revision:
0:8f8e8f3cbd1c
first commit;

Who changed what in which revision?

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