The driver for the ESP8266 WiFi module

Dependents:   Robot Wi-fi

Fork of esp8266-driver by ESP8266

Committer:
williampeers
Date:
Thu Jul 27 03:25:51 2017 +0000
Revision:
2:77a1e2188ed9
Parent:
0:6946b0b9e323
Sender working with reconnect if AGV or control resets, but not wifi

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