The driver for the ESP8266 WiFi module - isConnected function added, working and tested with mbed os 5.7.6

Fork of esp8266-driver by ESP8266

Committer:
mareszym
Date:
Sun Nov 04 12:50:49 2018 +0000
Revision:
2:0bc0b654995c
Parent:
0:6946b0b9e323
isConnected function added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ESP8266 0:6946b0b9e323 1 /* ESP8266 implementation of NetworkInterfaceAPI
group-ESP8266 0:6946b0b9e323 2 * Copyright (c) 2015 ARM Limited
group-ESP8266 0:6946b0b9e323 3 *
group-ESP8266 0:6946b0b9e323 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-ESP8266 0:6946b0b9e323 5 * you may not use this file except in compliance with the License.
group-ESP8266 0:6946b0b9e323 6 * You may obtain a copy of the License at
group-ESP8266 0:6946b0b9e323 7 *
group-ESP8266 0:6946b0b9e323 8 * http://www.apache.org/licenses/LICENSE-2.0
group-ESP8266 0:6946b0b9e323 9 *
group-ESP8266 0:6946b0b9e323 10 * Unless required by applicable law or agreed to in writing, software
group-ESP8266 0:6946b0b9e323 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-ESP8266 0:6946b0b9e323 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-ESP8266 0:6946b0b9e323 13 * See the License for the specific language governing permissions and
group-ESP8266 0:6946b0b9e323 14 * limitations under the License.
group-ESP8266 0:6946b0b9e323 15 */
group-ESP8266 0:6946b0b9e323 16
group-ESP8266 0:6946b0b9e323 17 #ifndef ESP8266_INTERFACE_H
group-ESP8266 0:6946b0b9e323 18 #define ESP8266_INTERFACE_H
group-ESP8266 0:6946b0b9e323 19
group-ESP8266 0:6946b0b9e323 20 #include "mbed.h"
group-ESP8266 0:6946b0b9e323 21 #include "ESP8266.h"
group-ESP8266 0:6946b0b9e323 22
group-ESP8266 0:6946b0b9e323 23
group-ESP8266 0:6946b0b9e323 24 #define ESP8266_SOCKET_COUNT 5
group-ESP8266 0:6946b0b9e323 25
group-ESP8266 0:6946b0b9e323 26 /** ESP8266Interface class
group-ESP8266 0:6946b0b9e323 27 * Implementation of the NetworkStack for the ESP8266
group-ESP8266 0:6946b0b9e323 28 */
group-ESP8266 0:6946b0b9e323 29 class ESP8266Interface : public NetworkStack, public WiFiInterface
group-ESP8266 0:6946b0b9e323 30 {
group-ESP8266 0:6946b0b9e323 31 public:
group-ESP8266 0:6946b0b9e323 32 /** ESP8266Interface lifetime
group-ESP8266 0:6946b0b9e323 33 * @param tx TX pin
group-ESP8266 0:6946b0b9e323 34 * @param rx RX pin
group-ESP8266 0:6946b0b9e323 35 * @param debug Enable debugging
group-ESP8266 0:6946b0b9e323 36 */
group-ESP8266 0:6946b0b9e323 37 ESP8266Interface(PinName tx, PinName rx, bool debug = false);
group-ESP8266 0:6946b0b9e323 38
group-ESP8266 0:6946b0b9e323 39 /** Start the interface
group-ESP8266 0:6946b0b9e323 40 *
group-ESP8266 0:6946b0b9e323 41 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
group-ESP8266 0:6946b0b9e323 42 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
group-ESP8266 0:6946b0b9e323 43 *
group-ESP8266 0:6946b0b9e323 44 * @return 0 on success, negative error code on failure
group-ESP8266 0:6946b0b9e323 45 */
group-ESP8266 0:6946b0b9e323 46 virtual int connect();
mareszym 2:0bc0b654995c 47
mareszym 2:0bc0b654995c 48 /** Check interface connection status
mareszym 2:0bc0b654995c 49 *
mareszym 2:0bc0b654995c 50 * Attempts to check connection to a WiFi network.
mareszym 2:0bc0b654995c 51 *
mareszym 2:0bc0b654995c 52 * @return true only if the chip has an IP address
mareszym 2:0bc0b654995c 53 */
mareszym 2:0bc0b654995c 54 virtual bool isConnected();
group-ESP8266 0:6946b0b9e323 55
group-ESP8266 0:6946b0b9e323 56 /** Start the interface
group-ESP8266 0:6946b0b9e323 57 *
group-ESP8266 0:6946b0b9e323 58 * Attempts to connect to a WiFi network.
group-ESP8266 0:6946b0b9e323 59 *
group-ESP8266 0:6946b0b9e323 60 * @param ssid Name of the network to connect to
group-ESP8266 0:6946b0b9e323 61 * @param pass Security passphrase to connect to the network
group-ESP8266 0:6946b0b9e323 62 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
group-ESP8266 0:6946b0b9e323 63 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
group-ESP8266 0:6946b0b9e323 64 * @return 0 on success, or error code on failure
group-ESP8266 0:6946b0b9e323 65 */
group-ESP8266 0:6946b0b9e323 66 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
group-ESP8266 0:6946b0b9e323 67 uint8_t channel = 0);
group-ESP8266 0:6946b0b9e323 68
group-ESP8266 0:6946b0b9e323 69 /** Set the WiFi network credentials
group-ESP8266 0:6946b0b9e323 70 *
group-ESP8266 0:6946b0b9e323 71 * @param ssid Name of the network to connect to
group-ESP8266 0:6946b0b9e323 72 * @param pass Security passphrase to connect to the network
group-ESP8266 0:6946b0b9e323 73 * @param security Type of encryption for connection
group-ESP8266 0:6946b0b9e323 74 * (defaults to NSAPI_SECURITY_NONE)
group-ESP8266 0:6946b0b9e323 75 * @return 0 on success, or error code on failure
group-ESP8266 0:6946b0b9e323 76 */
group-ESP8266 0:6946b0b9e323 77 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
group-ESP8266 0:6946b0b9e323 78
group-ESP8266 0:6946b0b9e323 79 /** Set the WiFi network channel - NOT SUPPORTED
group-ESP8266 0:6946b0b9e323 80 *
group-ESP8266 0:6946b0b9e323 81 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
group-ESP8266 0:6946b0b9e323 82 *
group-ESP8266 0:6946b0b9e323 83 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
group-ESP8266 0:6946b0b9e323 84 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
group-ESP8266 0:6946b0b9e323 85 */
group-ESP8266 0:6946b0b9e323 86 virtual int set_channel(uint8_t channel);
group-ESP8266 0:6946b0b9e323 87
group-ESP8266 0:6946b0b9e323 88 /** Stop the interface
group-ESP8266 0:6946b0b9e323 89 * @return 0 on success, negative on failure
group-ESP8266 0:6946b0b9e323 90 */
group-ESP8266 0:6946b0b9e323 91 virtual int disconnect();
group-ESP8266 0:6946b0b9e323 92
group-ESP8266 0:6946b0b9e323 93 /** Get the internally stored IP address
group-ESP8266 0:6946b0b9e323 94 * @return IP address of the interface or null if not yet connected
group-ESP8266 0:6946b0b9e323 95 */
group-ESP8266 0:6946b0b9e323 96 virtual const char *get_ip_address();
group-ESP8266 0:6946b0b9e323 97
group-ESP8266 0:6946b0b9e323 98 /** Get the internally stored MAC address
group-ESP8266 0:6946b0b9e323 99 * @return MAC address of the interface
group-ESP8266 0:6946b0b9e323 100 */
group-ESP8266 0:6946b0b9e323 101 virtual const char *get_mac_address();
group-ESP8266 0:6946b0b9e323 102
group-ESP8266 0:6946b0b9e323 103 /** Get the local gateway
group-ESP8266 0:6946b0b9e323 104 *
group-ESP8266 0:6946b0b9e323 105 * @return Null-terminated representation of the local gateway
group-ESP8266 0:6946b0b9e323 106 * or null if no network mask has been recieved
group-ESP8266 0:6946b0b9e323 107 */
group-ESP8266 0:6946b0b9e323 108 virtual const char *get_gateway();
group-ESP8266 0:6946b0b9e323 109
group-ESP8266 0:6946b0b9e323 110 /** Get the local network mask
group-ESP8266 0:6946b0b9e323 111 *
group-ESP8266 0:6946b0b9e323 112 * @return Null-terminated representation of the local network mask
group-ESP8266 0:6946b0b9e323 113 * or null if no network mask has been recieved
group-ESP8266 0:6946b0b9e323 114 */
group-ESP8266 0:6946b0b9e323 115 virtual const char *get_netmask();
group-ESP8266 0:6946b0b9e323 116
group-ESP8266 0:6946b0b9e323 117 /** Gets the current radio signal strength for active connection
group-ESP8266 0:6946b0b9e323 118 *
group-ESP8266 0:6946b0b9e323 119 * @return Connection strength in dBm (negative value)
group-ESP8266 0:6946b0b9e323 120 */
group-ESP8266 0:6946b0b9e323 121 virtual int8_t get_rssi();
group-ESP8266 0:6946b0b9e323 122
group-ESP8266 0:6946b0b9e323 123 /** Scan for available networks
group-ESP8266 0:6946b0b9e323 124 *
group-ESP8266 0:6946b0b9e323 125 * This function will block.
group-ESP8266 0:6946b0b9e323 126 *
group-ESP8266 0:6946b0b9e323 127 * @param ap Pointer to allocated array to store discovered AP
group-ESP8266 0:6946b0b9e323 128 * @param count Size of allocated @a res array, or 0 to only count available AP
group-ESP8266 0:6946b0b9e323 129 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
group-ESP8266 0:6946b0b9e323 130 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
group-ESP8266 0:6946b0b9e323 131 * see @a nsapi_error
group-ESP8266 0:6946b0b9e323 132 */
group-ESP8266 0:6946b0b9e323 133 virtual int scan(WiFiAccessPoint *res, unsigned count);
group-ESP8266 0:6946b0b9e323 134
group-ESP8266 0:6946b0b9e323 135 /** Translates a hostname to an IP address with specific version
group-ESP8266 0:6946b0b9e323 136 *
group-ESP8266 0:6946b0b9e323 137 * The hostname may be either a domain name or an IP address. If the
group-ESP8266 0:6946b0b9e323 138 * hostname is an IP address, no network transactions will be performed.
group-ESP8266 0:6946b0b9e323 139 *
group-ESP8266 0:6946b0b9e323 140 * If no stack-specific DNS resolution is provided, the hostname
group-ESP8266 0:6946b0b9e323 141 * will be resolve using a UDP socket on the stack.
group-ESP8266 0:6946b0b9e323 142 *
group-ESP8266 0:6946b0b9e323 143 * @param address Destination for the host SocketAddress
group-ESP8266 0:6946b0b9e323 144 * @param host Hostname to resolve
group-ESP8266 0:6946b0b9e323 145 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
group-ESP8266 0:6946b0b9e323 146 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
group-ESP8266 0:6946b0b9e323 147 * @return 0 on success, negative error code on failure
group-ESP8266 0:6946b0b9e323 148 */
group-ESP8266 0:6946b0b9e323 149 using NetworkInterface::gethostbyname;
group-ESP8266 0:6946b0b9e323 150
group-ESP8266 0:6946b0b9e323 151 /** Add a domain name server to list of servers to query
group-ESP8266 0:6946b0b9e323 152 *
group-ESP8266 0:6946b0b9e323 153 * @param addr Destination for the host address
group-ESP8266 0:6946b0b9e323 154 * @return 0 on success, negative error code on failure
group-ESP8266 0:6946b0b9e323 155 */
group-ESP8266 0:6946b0b9e323 156 using NetworkInterface::add_dns_server;
group-ESP8266 0:6946b0b9e323 157
group-ESP8266 0:6946b0b9e323 158 protected:
group-ESP8266 0:6946b0b9e323 159 /** Open a socket
group-ESP8266 0:6946b0b9e323 160 * @param handle Handle in which to store new socket
group-ESP8266 0:6946b0b9e323 161 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
group-ESP8266 0:6946b0b9e323 162 * @return 0 on success, negative on failure
group-ESP8266 0:6946b0b9e323 163 */
group-ESP8266 0:6946b0b9e323 164 virtual int socket_open(void **handle, nsapi_protocol_t proto);
group-ESP8266 0:6946b0b9e323 165
group-ESP8266 0:6946b0b9e323 166 /** Close the socket
group-ESP8266 0:6946b0b9e323 167 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 168 * @return 0 on success, negative on failure
group-ESP8266 0:6946b0b9e323 169 * @note On failure, any memory associated with the socket must still
group-ESP8266 0:6946b0b9e323 170 * be cleaned up
group-ESP8266 0:6946b0b9e323 171 */
group-ESP8266 0:6946b0b9e323 172 virtual int socket_close(void *handle);
group-ESP8266 0:6946b0b9e323 173
group-ESP8266 0:6946b0b9e323 174 /** Bind a server socket to a specific port
group-ESP8266 0:6946b0b9e323 175 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 176 * @param address Local address to listen for incoming connections on
group-ESP8266 0:6946b0b9e323 177 * @return 0 on success, negative on failure.
group-ESP8266 0:6946b0b9e323 178 */
group-ESP8266 0:6946b0b9e323 179 virtual int socket_bind(void *handle, const SocketAddress &address);
group-ESP8266 0:6946b0b9e323 180
group-ESP8266 0:6946b0b9e323 181 /** Start listening for incoming connections
group-ESP8266 0:6946b0b9e323 182 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 183 * @param backlog Number of pending connections that can be queued up at any
group-ESP8266 0:6946b0b9e323 184 * one time [Default: 1]
group-ESP8266 0:6946b0b9e323 185 * @return 0 on success, negative on failure
group-ESP8266 0:6946b0b9e323 186 */
group-ESP8266 0:6946b0b9e323 187 virtual int socket_listen(void *handle, int backlog);
group-ESP8266 0:6946b0b9e323 188
group-ESP8266 0:6946b0b9e323 189 /** Connects this TCP socket to the server
group-ESP8266 0:6946b0b9e323 190 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 191 * @param address SocketAddress to connect to
group-ESP8266 0:6946b0b9e323 192 * @return 0 on success, negative on failure
group-ESP8266 0:6946b0b9e323 193 */
group-ESP8266 0:6946b0b9e323 194 virtual int socket_connect(void *handle, const SocketAddress &address);
group-ESP8266 0:6946b0b9e323 195
group-ESP8266 0:6946b0b9e323 196 /** Accept a new connection.
group-ESP8266 0:6946b0b9e323 197 * @param handle Handle in which to store new socket
group-ESP8266 0:6946b0b9e323 198 * @param server Socket handle to server to accept from
group-ESP8266 0:6946b0b9e323 199 * @return 0 on success, negative on failure
group-ESP8266 0:6946b0b9e323 200 * @note This call is not-blocking, if this call would block, must
group-ESP8266 0:6946b0b9e323 201 * immediately return NSAPI_ERROR_WOULD_WAIT
group-ESP8266 0:6946b0b9e323 202 */
group-ESP8266 0:6946b0b9e323 203 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
group-ESP8266 0:6946b0b9e323 204
group-ESP8266 0:6946b0b9e323 205 /** Send data to the remote host
group-ESP8266 0:6946b0b9e323 206 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 207 * @param data The buffer to send to the host
group-ESP8266 0:6946b0b9e323 208 * @param size The length of the buffer to send
group-ESP8266 0:6946b0b9e323 209 * @return Number of written bytes on success, negative on failure
group-ESP8266 0:6946b0b9e323 210 * @note This call is not-blocking, if this call would block, must
group-ESP8266 0:6946b0b9e323 211 * immediately return NSAPI_ERROR_WOULD_WAIT
group-ESP8266 0:6946b0b9e323 212 */
group-ESP8266 0:6946b0b9e323 213 virtual int socket_send(void *handle, const void *data, unsigned size);
group-ESP8266 0:6946b0b9e323 214
group-ESP8266 0:6946b0b9e323 215 /** Receive data from the remote host
group-ESP8266 0:6946b0b9e323 216 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 217 * @param data The buffer in which to store the data received from the host
group-ESP8266 0:6946b0b9e323 218 * @param size The maximum length of the buffer
group-ESP8266 0:6946b0b9e323 219 * @return Number of received bytes on success, negative on failure
group-ESP8266 0:6946b0b9e323 220 * @note This call is not-blocking, if this call would block, must
group-ESP8266 0:6946b0b9e323 221 * immediately return NSAPI_ERROR_WOULD_WAIT
group-ESP8266 0:6946b0b9e323 222 */
group-ESP8266 0:6946b0b9e323 223 virtual int socket_recv(void *handle, void *data, unsigned size);
group-ESP8266 0:6946b0b9e323 224
group-ESP8266 0:6946b0b9e323 225 /** Send a packet to a remote endpoint
group-ESP8266 0:6946b0b9e323 226 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 227 * @param address The remote SocketAddress
group-ESP8266 0:6946b0b9e323 228 * @param data The packet to be sent
group-ESP8266 0:6946b0b9e323 229 * @param size The length of the packet to be sent
group-ESP8266 0:6946b0b9e323 230 * @return The number of written bytes on success, negative on failure
group-ESP8266 0:6946b0b9e323 231 * @note This call is not-blocking, if this call would block, must
group-ESP8266 0:6946b0b9e323 232 * immediately return NSAPI_ERROR_WOULD_WAIT
group-ESP8266 0:6946b0b9e323 233 */
group-ESP8266 0:6946b0b9e323 234 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
group-ESP8266 0:6946b0b9e323 235
group-ESP8266 0:6946b0b9e323 236 /** Receive a packet from a remote endpoint
group-ESP8266 0:6946b0b9e323 237 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 238 * @param address Destination for the remote SocketAddress or null
group-ESP8266 0:6946b0b9e323 239 * @param buffer The buffer for storing the incoming packet data
group-ESP8266 0:6946b0b9e323 240 * If a packet is too long to fit in the supplied buffer,
group-ESP8266 0:6946b0b9e323 241 * excess bytes are discarded
group-ESP8266 0:6946b0b9e323 242 * @param size The length of the buffer
group-ESP8266 0:6946b0b9e323 243 * @return The number of received bytes on success, negative on failure
group-ESP8266 0:6946b0b9e323 244 * @note This call is not-blocking, if this call would block, must
group-ESP8266 0:6946b0b9e323 245 * immediately return NSAPI_ERROR_WOULD_WAIT
group-ESP8266 0:6946b0b9e323 246 */
group-ESP8266 0:6946b0b9e323 247 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
group-ESP8266 0:6946b0b9e323 248
group-ESP8266 0:6946b0b9e323 249 /** Register a callback on state change of the socket
group-ESP8266 0:6946b0b9e323 250 * @param handle Socket handle
group-ESP8266 0:6946b0b9e323 251 * @param callback Function to call on state change
group-ESP8266 0:6946b0b9e323 252 * @param data Argument to pass to callback
group-ESP8266 0:6946b0b9e323 253 * @note Callback may be called in an interrupt context.
group-ESP8266 0:6946b0b9e323 254 */
group-ESP8266 0:6946b0b9e323 255 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
group-ESP8266 0:6946b0b9e323 256
group-ESP8266 0:6946b0b9e323 257 /** Provide access to the NetworkStack object
group-ESP8266 0:6946b0b9e323 258 *
group-ESP8266 0:6946b0b9e323 259 * @return The underlying NetworkStack object
group-ESP8266 0:6946b0b9e323 260 */
group-ESP8266 0:6946b0b9e323 261 virtual NetworkStack *get_stack()
group-ESP8266 0:6946b0b9e323 262 {
group-ESP8266 0:6946b0b9e323 263 return this;
group-ESP8266 0:6946b0b9e323 264 }
group-ESP8266 0:6946b0b9e323 265
group-ESP8266 0:6946b0b9e323 266 private:
group-ESP8266 0:6946b0b9e323 267 ESP8266 _esp;
group-ESP8266 0:6946b0b9e323 268 bool _ids[ESP8266_SOCKET_COUNT];
group-ESP8266 0:6946b0b9e323 269
group-ESP8266 0:6946b0b9e323 270 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
group-ESP8266 0:6946b0b9e323 271 nsapi_security_t ap_sec;
group-ESP8266 0:6946b0b9e323 272 uint8_t ap_ch;
group-ESP8266 0:6946b0b9e323 273 char ap_pass[64]; /* The longest allowed passphrase */
group-ESP8266 0:6946b0b9e323 274
group-ESP8266 0:6946b0b9e323 275 void event();
group-ESP8266 0:6946b0b9e323 276
group-ESP8266 0:6946b0b9e323 277 struct {
group-ESP8266 0:6946b0b9e323 278 void (*callback)(void *);
group-ESP8266 0:6946b0b9e323 279 void *data;
group-ESP8266 0:6946b0b9e323 280 } _cbs[ESP8266_SOCKET_COUNT];
group-ESP8266 0:6946b0b9e323 281 };
group-ESP8266 0:6946b0b9e323 282
group-ESP8266 0:6946b0b9e323 283 #endif