Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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