Fork of my original MQTTGateway

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266Interface.h Source File

ESP8266Interface.h

00001 /* ESP8266 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ESP8266_INTERFACE_H
00018 #define ESP8266_INTERFACE_H
00019 
00020 #include "mbed.h"
00021 #include "ESP8266.h"
00022 
00023 
00024 #define ESP8266_SOCKET_COUNT 5
00025 
00026 /** ESP8266Interface class
00027  *  Implementation of the NetworkStack for the ESP8266
00028  */
00029 class ESP8266Interface : public NetworkStack, public WiFiInterface
00030 {
00031 public:
00032     /** ESP8266Interface lifetime
00033      * @param tx        TX pin
00034      * @param rx        RX pin
00035      * @param debug     Enable debugging
00036      */
00037     ESP8266Interface(PinName tx, PinName rx, bool debug = false);
00038 
00039     /** Start the interface
00040      *
00041      *  Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
00042      *  If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
00043      *
00044      *  @return         0 on success, negative error code on failure
00045      */
00046     virtual int connect();
00047 
00048     /** Start the interface
00049      *
00050      *  Attempts to connect to a WiFi network.
00051      *
00052      *  @param ssid      Name of the network to connect to
00053      *  @param pass      Security passphrase to connect to the network
00054      *  @param security  Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
00055      *  @param channel   This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
00056      *  @return          0 on success, or error code on failure
00057      */
00058     virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
00059                                   uint8_t channel = 0);
00060 
00061     /** Set the WiFi network credentials
00062      *
00063      *  @param ssid      Name of the network to connect to
00064      *  @param pass      Security passphrase to connect to the network
00065      *  @param security  Type of encryption for connection
00066      *                   (defaults to NSAPI_SECURITY_NONE)
00067      *  @return          0 on success, or error code on failure
00068      */
00069     virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
00070 
00071     /** Set the WiFi network channel - NOT SUPPORTED
00072      *
00073      * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
00074      *
00075      *  @param channel   Channel on which the connection is to be made, or 0 for any (Default: 0)
00076      *  @return          Not supported, returns NSAPI_ERROR_UNSUPPORTED
00077      */
00078     virtual int set_channel(uint8_t channel);
00079 
00080     /** Stop the interface
00081      *  @return             0 on success, negative on failure
00082      */
00083     virtual int disconnect();
00084 
00085     /** Get the internally stored IP address
00086      *  @return             IP address of the interface or null if not yet connected
00087      */
00088     virtual const char *get_ip_address();
00089 
00090     /** Get the internally stored MAC address
00091      *  @return             MAC address of the interface
00092      */
00093     virtual const char *get_mac_address();
00094 
00095      /** Get the local gateway
00096      *
00097      *  @return         Null-terminated representation of the local gateway
00098      *                  or null if no network mask has been recieved
00099      */
00100     virtual const char *get_gateway();
00101 
00102     /** Get the local network mask
00103      *
00104      *  @return         Null-terminated representation of the local network mask
00105      *                  or null if no network mask has been recieved
00106      */
00107     virtual const char *get_netmask();
00108 
00109     /** Gets the current radio signal strength for active connection
00110      *
00111      * @return          Connection strength in dBm (negative value)
00112      */
00113     virtual int8_t get_rssi();
00114 
00115     /** Scan for available networks
00116      *
00117      * This function will block.
00118      *
00119      * @param  ap       Pointer to allocated array to store discovered AP
00120      * @param  count    Size of allocated @a res array, or 0 to only count available AP
00121      * @param  timeout  Timeout in milliseconds; 0 for no timeout (Default: 0)
00122      * @return          Number of entries in @a, or if @a count was 0 number of available networks, negative on error
00123      *                  see @a nsapi_error
00124      */
00125     virtual int scan(WiFiAccessPoint *res, unsigned count);
00126 
00127     /** Translates a hostname to an IP address with specific version
00128      *
00129      *  The hostname may be either a domain name or an IP address. If the
00130      *  hostname is an IP address, no network transactions will be performed.
00131      *
00132      *  If no stack-specific DNS resolution is provided, the hostname
00133      *  will be resolve using a UDP socket on the stack.
00134      *
00135      *  @param address  Destination for the host SocketAddress
00136      *  @param host     Hostname to resolve
00137      *  @param version  IP version of address to resolve, NSAPI_UNSPEC indicates
00138      *                  version is chosen by the stack (defaults to NSAPI_UNSPEC)
00139      *  @return         0 on success, negative error code on failure
00140      */
00141     using NetworkInterface::gethostbyname;
00142 
00143     /** Add a domain name server to list of servers to query
00144      *
00145      *  @param addr     Destination for the host address
00146      *  @return         0 on success, negative error code on failure
00147      */
00148     using NetworkInterface::add_dns_server;
00149 
00150 protected:
00151     /** Open a socket
00152      *  @param handle       Handle in which to store new socket
00153      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00154      *  @return             0 on success, negative on failure
00155      */
00156     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00157 
00158     /** Close the socket
00159      *  @param handle       Socket handle
00160      *  @return             0 on success, negative on failure
00161      *  @note On failure, any memory associated with the socket must still
00162      *        be cleaned up
00163      */
00164     virtual int socket_close(void *handle);
00165 
00166     /** Bind a server socket to a specific port
00167      *  @param handle       Socket handle
00168      *  @param address      Local address to listen for incoming connections on
00169      *  @return             0 on success, negative on failure.
00170      */
00171     virtual int socket_bind(void *handle, const SocketAddress &address);
00172 
00173     /** Start listening for incoming connections
00174      *  @param handle       Socket handle
00175      *  @param backlog      Number of pending connections that can be queued up at any
00176      *                      one time [Default: 1]
00177      *  @return             0 on success, negative on failure
00178      */
00179     virtual int socket_listen(void *handle, int backlog);
00180 
00181     /** Connects this TCP socket to the server
00182      *  @param handle       Socket handle
00183      *  @param address      SocketAddress to connect to
00184      *  @return             0 on success, negative on failure
00185      */
00186     virtual int socket_connect(void *handle, const SocketAddress &address);
00187 
00188     /** Accept a new connection.
00189      *  @param handle       Handle in which to store new socket
00190      *  @param server       Socket handle to server to accept from
00191      *  @return             0 on success, negative on failure
00192      *  @note This call is not-blocking, if this call would block, must
00193      *        immediately return NSAPI_ERROR_WOULD_WAIT
00194      */
00195     virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
00196 
00197     /** Send data to the remote host
00198      *  @param handle       Socket handle
00199      *  @param data         The buffer to send to the host
00200      *  @param size         The length of the buffer to send
00201      *  @return             Number of written bytes on success, negative on failure
00202      *  @note This call is not-blocking, if this call would block, must
00203      *        immediately return NSAPI_ERROR_WOULD_WAIT
00204      */
00205     virtual int socket_send(void *handle, const void *data, unsigned size);
00206 
00207     /** Receive data from the remote host
00208      *  @param handle       Socket handle
00209      *  @param data         The buffer in which to store the data received from the host
00210      *  @param size         The maximum length of the buffer
00211      *  @return             Number of received bytes on success, negative on failure
00212      *  @note This call is not-blocking, if this call would block, must
00213      *        immediately return NSAPI_ERROR_WOULD_WAIT
00214      */
00215     virtual int socket_recv(void *handle, void *data, unsigned size);
00216 
00217     /** Send a packet to a remote endpoint
00218      *  @param handle       Socket handle
00219      *  @param address      The remote SocketAddress
00220      *  @param data         The packet to be sent
00221      *  @param size         The length of the packet to be sent
00222      *  @return             The number of written bytes on success, negative on failure
00223      *  @note This call is not-blocking, if this call would block, must
00224      *        immediately return NSAPI_ERROR_WOULD_WAIT
00225      */
00226     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00227 
00228     /** Receive a packet from a remote endpoint
00229      *  @param handle       Socket handle
00230      *  @param address      Destination for the remote SocketAddress or null
00231      *  @param buffer       The buffer for storing the incoming packet data
00232      *                      If a packet is too long to fit in the supplied buffer,
00233      *                      excess bytes are discarded
00234      *  @param size         The length of the buffer
00235      *  @return             The number of received bytes on success, negative on failure
00236      *  @note This call is not-blocking, if this call would block, must
00237      *        immediately return NSAPI_ERROR_WOULD_WAIT
00238      */
00239     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00240 
00241     /** Register a callback on state change of the socket
00242      *  @param handle       Socket handle
00243      *  @param callback     Function to call on state change
00244      *  @param data         Argument to pass to callback
00245      *  @note Callback may be called in an interrupt context.
00246      */
00247     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00248 
00249     /** Provide access to the NetworkStack object
00250      *
00251      *  @return The underlying NetworkStack object
00252      */
00253     virtual NetworkStack *get_stack()
00254     {
00255         return this;
00256     }
00257 
00258 private:
00259     ESP8266 _esp;
00260     bool _ids[ESP8266_SOCKET_COUNT];
00261 
00262     char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
00263     nsapi_security_t ap_sec;
00264     uint8_t ap_ch;
00265     char ap_pass[64]; /* The longest allowed passphrase */
00266 
00267     void event();
00268 
00269     struct {
00270         void (*callback)(void *);
00271         void *data;
00272     } _cbs[ESP8266_SOCKET_COUNT];
00273 };
00274 
00275 #endif