Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

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