Kev Mann / mbed-dev-OS5_10_4
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     /**
00033      * @brief ESP8266Interface default constructor
00034      *        Will use values defined in mbed_lib.json
00035      */
00036     ESP8266Interface();
00037 
00038     /** ESP8266Interface lifetime
00039      * @param tx        TX pin
00040      * @param rx        RX pin
00041      * @param debug     Enable debugging
00042      */
00043     ESP8266Interface(PinName tx, PinName rx, bool debug = false);
00044 
00045     /** Start the interface
00046      *
00047      *  Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
00048      *  If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
00049      *
00050      *  @return         0 on success, negative error code on failure
00051      */
00052     virtual int connect();
00053 
00054     /** Start the interface
00055      *
00056      *  Attempts to connect to a WiFi network.
00057      *
00058      *  @param ssid      Name of the network to connect to
00059      *  @param pass      Security passphrase to connect to the network
00060      *  @param security  Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
00061      *  @param channel   This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
00062      *  @return          0 on success, or error code on failure
00063      */
00064     virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE ,
00065                                   uint8_t channel = 0);
00066 
00067     /** Set the WiFi network credentials
00068      *
00069      *  @param ssid      Name of the network to connect to
00070      *  @param pass      Security passphrase to connect to the network
00071      *  @param security  Type of encryption for connection
00072      *                   (defaults to NSAPI_SECURITY_NONE)
00073      *  @return          0 on success, or error code on failure
00074      */
00075     virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE );
00076 
00077     /** Set the WiFi network channel - NOT SUPPORTED
00078      *
00079      * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
00080      *
00081      *  @param channel   Channel on which the connection is to be made, or 0 for any (Default: 0)
00082      *  @return          Not supported, returns NSAPI_ERROR_UNSUPPORTED
00083      */
00084     virtual int set_channel(uint8_t channel);
00085 
00086     /** Stop the interface
00087      *  @return             0 on success, negative on failure
00088      */
00089     virtual int disconnect();
00090 
00091     /** Get the internally stored IP address
00092      *  @return             IP address of the interface or null if not yet connected
00093      */
00094     virtual const char *get_ip_address();
00095 
00096     /** Get the internally stored MAC address
00097      *  @return             MAC address of the interface
00098      */
00099     virtual const char *get_mac_address();
00100 
00101      /** Get the local gateway
00102      *
00103      *  @return         Null-terminated representation of the local gateway
00104      *                  or null if no network mask has been recieved
00105      */
00106     virtual const char *get_gateway();
00107 
00108     /** Get the local network mask
00109      *
00110      *  @return         Null-terminated representation of the local network mask
00111      *                  or null if no network mask has been recieved
00112      */
00113     virtual const char *get_netmask();
00114 
00115     /** Gets the current radio signal strength for active connection
00116      *
00117      * @return          Connection strength in dBm (negative value)
00118      */
00119     virtual int8_t get_rssi();
00120 
00121     /** Scan for available networks
00122      *
00123      * This function will block.
00124      *
00125      * @param  ap       Pointer to allocated array to store discovered AP
00126      * @param  count    Size of allocated @a res array, or 0 to only count available AP
00127      * @param  timeout  Timeout in milliseconds; 0 for no timeout (Default: 0)
00128      * @return          Number of entries in @a, or if @a count was 0 number of available networks, negative on error
00129      *                  see @a nsapi_error
00130      */
00131     virtual int scan(WiFiAccessPoint *res, unsigned count);
00132 
00133     /** Translates a hostname to an IP address with specific version
00134      *
00135      *  The hostname may be either a domain name or an IP address. If the
00136      *  hostname is an IP address, no network transactions will be performed.
00137      *
00138      *  If no stack-specific DNS resolution is provided, the hostname
00139      *  will be resolve using a UDP socket on the stack.
00140      *
00141      *  @param address  Destination for the host SocketAddress
00142      *  @param host     Hostname to resolve
00143      *  @param version  IP version of address to resolve, NSAPI_UNSPEC indicates
00144      *                  version is chosen by the stack (defaults to NSAPI_UNSPEC)
00145      *  @return         0 on success, negative error code on failure
00146      */
00147     using NetworkInterface::gethostbyname;
00148 
00149     /** Add a domain name server to list of servers to query
00150      *
00151      *  @param addr     Destination for the host address
00152      *  @return         0 on success, negative error code on failure
00153      */
00154     using NetworkInterface::add_dns_server;
00155 
00156     /** @copydoc NetworkStack::setsockopt
00157      */
00158     virtual nsapi_error_t setsockopt (nsapi_socket_t handle, int level,
00159             int optname, const void *optval, unsigned optlen);
00160 
00161     /** @copydoc NetworkStack::getsockopt
00162      */
00163     virtual nsapi_error_t getsockopt (nsapi_socket_t handle, int level, int optname,
00164             void *optval, unsigned *optlen);
00165 
00166     /** Register callback for status reporting
00167      *
00168      *  The specified status callback function will be called on status changes
00169      *  on the network. The parameters on the callback are the event type and
00170      *  event-type dependent reason parameter.
00171      *
00172      *  In ESP8266 the callback will be called when processing OOB-messages via
00173      *  AT-parser. Do NOT call any ESP8266Interface -functions or do extensive
00174      *  processing in the callback.
00175      *
00176      *  @param status_cb The callback for status changes
00177      */
00178     virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
00179 
00180     /** Get the connection status
00181      *
00182      *  @return         The connection status according to ConnectionStatusType
00183      */
00184     virtual nsapi_connection_status_t get_connection_status() const;
00185 
00186 protected:
00187     /** Open a socket
00188      *  @param handle       Handle in which to store new socket
00189      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00190      *  @return             0 on success, negative on failure
00191      */
00192     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00193 
00194     /** Close the socket
00195      *  @param handle       Socket handle
00196      *  @return             0 on success, negative on failure
00197      *  @note On failure, any memory associated with the socket must still
00198      *        be cleaned up
00199      */
00200     virtual int socket_close(void *handle);
00201 
00202     /** Bind a server socket to a specific port
00203      *  @param handle       Socket handle
00204      *  @param address      Local address to listen for incoming connections on
00205      *  @return             0 on success, negative on failure.
00206      */
00207     virtual int socket_bind(void *handle, const SocketAddress &address);
00208 
00209     /** Start listening for incoming connections
00210      *  @param handle       Socket handle
00211      *  @param backlog      Number of pending connections that can be queued up at any
00212      *                      one time [Default: 1]
00213      *  @return             0 on success, negative on failure
00214      */
00215     virtual int socket_listen(void *handle, int backlog);
00216 
00217     /** Connects this TCP socket to the server
00218      *  @param handle       Socket handle
00219      *  @param address      SocketAddress to connect to
00220      *  @return             0 on success, negative on failure
00221      */
00222     virtual int socket_connect(void *handle, const SocketAddress &address);
00223 
00224     /** Accept a new connection.
00225      *  @param handle       Handle in which to store new socket
00226      *  @param server       Socket handle to server to accept from
00227      *  @return             0 on success, negative on failure
00228      *  @note This call is not-blocking, if this call would block, must
00229      *        immediately return NSAPI_ERROR_WOULD_WAIT
00230      */
00231     virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
00232 
00233     /** Send data to the remote host
00234      *  @param handle       Socket handle
00235      *  @param data         The buffer to send to the host
00236      *  @param size         The length of the buffer to send
00237      *  @return             Number of written bytes on success, negative on failure
00238      *  @note This call is not-blocking, if this call would block, must
00239      *        immediately return NSAPI_ERROR_WOULD_WAIT
00240      */
00241     virtual int socket_send(void *handle, const void *data, unsigned size);
00242 
00243     /** Receive data from the remote host
00244      *  @param handle       Socket handle
00245      *  @param data         The buffer in which to store the data received from the host
00246      *  @param size         The maximum length of the buffer
00247      *  @return             Number of received bytes on success, negative on failure
00248      *  @note This call is not-blocking, if this call would block, must
00249      *        immediately return NSAPI_ERROR_WOULD_WAIT
00250      */
00251     virtual int socket_recv(void *handle, void *data, unsigned size);
00252 
00253     /** Send a packet to a remote endpoint
00254      *  @param handle       Socket handle
00255      *  @param address      The remote SocketAddress
00256      *  @param data         The packet to be sent
00257      *  @param size         The length of the packet to be sent
00258      *  @return             The number of written bytes on success, negative on failure
00259      *  @note This call is not-blocking, if this call would block, must
00260      *        immediately return NSAPI_ERROR_WOULD_WAIT
00261      */
00262     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00263 
00264     /** Receive a packet from a remote endpoint
00265      *  @param handle       Socket handle
00266      *  @param address      Destination for the remote SocketAddress or null
00267      *  @param buffer       The buffer for storing the incoming packet data
00268      *                      If a packet is too long to fit in the supplied buffer,
00269      *                      excess bytes are discarded
00270      *  @param size         The length of the buffer
00271      *  @return             The number of received bytes on success, negative on failure
00272      *  @note This call is not-blocking, if this call would block, must
00273      *        immediately return NSAPI_ERROR_WOULD_WAIT
00274      */
00275     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00276 
00277     /** Register a callback on state change of the socket
00278      *  @param handle       Socket handle
00279      *  @param callback     Function to call on state change
00280      *  @param data         Argument to pass to callback
00281      *  @note Callback may be called in an interrupt context.
00282      */
00283     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00284 
00285     /** Provide access to the NetworkStack object
00286      *
00287      *  @return The underlying NetworkStack object
00288      */
00289     virtual NetworkStack *get_stack()
00290     {
00291         return this;
00292     }
00293 
00294 private:
00295     static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */
00296     static const int ESP8266_PASSPHRASE_MAX_LENGTH = 63; /* The longest allowed passphrase */
00297     static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */
00298 
00299     ESP8266 _esp;
00300     bool _ids[ESP8266_SOCKET_COUNT];
00301     int _initialized;
00302     int _started;
00303 
00304     char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
00305     nsapi_security_t ap_sec;
00306     uint8_t ap_ch;
00307     char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1];
00308     uint16_t _local_ports[ESP8266_SOCKET_COUNT];
00309 
00310     bool _disable_default_softap();
00311     void event();
00312     bool _get_firmware_ok();
00313     nsapi_error_t _init(void);
00314     nsapi_error_t _startup(const int8_t wifi_mode);
00315 
00316     struct {
00317         void (*callback)(void *);
00318         void *data;
00319     } _cbs[ESP8266_SOCKET_COUNT];
00320 };
00321 
00322 #endif