Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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