Socket interface for ESP8266. Implements the NetworkSocketAPI. Requires device to use the Espressif Firmware.

Dependencies:   ESP8266

Dependents:   ESP8266InterfaceTests HelloESP8266Interface

Fork of ESP8266Interface by NetworkSocketAPI

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 "WiFiInterface.h"
00021 #include "ESP8266.h"
00022 
00023 #define ESP8266_SOCKET_COUNT 5
00024 
00025 /** ESP8266Interface class
00026  *  Implementation of the NetworkStack for the ESP8266
00027  */
00028 class ESP8266Interface : public NetworkStack, public WiFiInterface
00029 {
00030 public:
00031     /** ESP8266Interface lifetime
00032      * @param tx        TX pin
00033      * @param rx        RX pin
00034      * @param debug     Enable debugging
00035      */
00036     ESP8266Interface(PinName tx, PinName rx, bool debug = false);
00037 
00038     /** Start the interface
00039      *
00040      *  Attempts to connect to a WiFi network. If passphrase is invalid,
00041      *  NSAPI_ERROR_AUTH_ERROR is returned.
00042      *
00043      *  @param ssid      Name of the network to connect to
00044      *  @param pass      Security passphrase to connect to the network
00045      *  @param security  Type of encryption for connection
00046      *  @return          0 on success, negative error code on failure
00047      */
00048     virtual int connect(
00049         const char *ssid,
00050         const char *pass,
00051         nsapi_security_t security = NSAPI_SECURITY_NONE);
00052 
00053     /** Stop the interface
00054      *  @return             0 on success, negative on failure
00055      */
00056     virtual int disconnect();
00057 
00058     /** Get the internally stored IP address
00059      *  @return             IP address of the interface or null if not yet connected
00060      */
00061     virtual const char *get_ip_address();
00062 
00063     /** Get the internally stored MAC address
00064      *  @return             MAC address of the interface
00065      */
00066     virtual const char *get_mac_address();
00067 
00068 protected:
00069     /** Open a socket
00070      *  @param handle       Handle in which to store new socket
00071      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00072      *  @return             0 on success, negative on failure
00073      */
00074     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00075 
00076     /** Close the socket
00077      *  @param handle       Socket handle
00078      *  @return             0 on success, negative on failure
00079      *  @note On failure, any memory associated with the socket must still 
00080      *        be cleaned up
00081      */
00082     virtual int socket_close(void *handle);
00083 
00084     /** Bind a server socket to a specific port
00085      *  @param handle       Socket handle
00086      *  @param address      Local address to listen for incoming connections on 
00087      *  @return             0 on success, negative on failure.
00088      */
00089     virtual int socket_bind(void *handle, const SocketAddress &address);
00090 
00091     /** Start listening for incoming connections
00092      *  @param handle       Socket handle
00093      *  @param backlog      Number of pending connections that can be queued up at any
00094      *                      one time [Default: 1]
00095      *  @return             0 on success, negative on failure
00096      */
00097     virtual int socket_listen(void *handle, int backlog);
00098 
00099     /** Connects this TCP socket to the server
00100      *  @param handle       Socket handle
00101      *  @param address      SocketAddress to connect to
00102      *  @return             0 on success, negative on failure
00103      */
00104     virtual int socket_connect(void *handle, const SocketAddress &address);
00105 
00106     /** Accept a new connection.
00107      *  @param handle       Handle in which to store new socket
00108      *  @param server       Socket handle to server to accept from
00109      *  @return             0 on success, negative on failure
00110      *  @note This call is not-blocking, if this call would block, must
00111      *        immediately return NSAPI_ERROR_WOULD_WAIT
00112      */
00113     virtual int socket_accept(void **handle, void *server);
00114 
00115     /** Send data to the remote host
00116      *  @param handle       Socket handle
00117      *  @param data         The buffer to send to the host
00118      *  @param size         The length of the buffer to send
00119      *  @return             Number of written bytes on success, negative on failure
00120      *  @note This call is not-blocking, if this call would block, must
00121      *        immediately return NSAPI_ERROR_WOULD_WAIT
00122      */
00123     virtual int socket_send(void *handle, const void *data, unsigned size);
00124 
00125     /** Receive data from the remote host
00126      *  @param handle       Socket handle
00127      *  @param data         The buffer in which to store the data received from the host
00128      *  @param size         The maximum length of the buffer
00129      *  @return             Number of received bytes on success, negative on failure
00130      *  @note This call is not-blocking, if this call would block, must
00131      *        immediately return NSAPI_ERROR_WOULD_WAIT
00132      */
00133     virtual int socket_recv(void *handle, void *data, unsigned size);
00134 
00135     /** Send a packet to a remote endpoint
00136      *  @param handle       Socket handle
00137      *  @param address      The remote SocketAddress
00138      *  @param data         The packet to be sent
00139      *  @param size         The length of the packet to be sent
00140      *  @return             The number of written bytes on success, negative on failure
00141      *  @note This call is not-blocking, if this call would block, must
00142      *        immediately return NSAPI_ERROR_WOULD_WAIT
00143      */
00144     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00145 
00146     /** Receive a packet from a remote endpoint
00147      *  @param handle       Socket handle
00148      *  @param address      Destination for the remote SocketAddress or null
00149      *  @param buffer       The buffer for storing the incoming packet data
00150      *                      If a packet is too long to fit in the supplied buffer,
00151      *                      excess bytes are discarded
00152      *  @param size         The length of the buffer
00153      *  @return             The number of received bytes on success, negative on failure
00154      *  @note This call is not-blocking, if this call would block, must
00155      *        immediately return NSAPI_ERROR_WOULD_WAIT
00156      */
00157     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00158 
00159     /** Register a callback on state change of the socket
00160      *  @param handle       Socket handle
00161      *  @param callback     Function to call on state change
00162      *  @param data         Argument to pass to callback
00163      *  @note Callback may be called in an interrupt context.
00164      */
00165     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00166     
00167 private:
00168     ESP8266 _esp;
00169     bool _ids[ESP8266_SOCKET_COUNT];
00170 };
00171 
00172 #endif