Socket interface for ESP8266. Implements the NetworkSocketAPI. Requires device to use the Espressif Firmware.
Dependencies: ESP8266
Dependents: ESP8266InterfaceTests HelloESP8266Interface
Fork of ESP8266Interface by
Note
This library assumes your ESP8266 is running the Espressif Firmware. For instructions on how to update your ESP8266 to use the correct firmware see the Firmware Update Wiki Page.
Currently the ESP8266Interface LIbrary has the following Abilities:
Working
- TCP Client
- UDP Client
- Transparent mode (single connection of 1 type at a time)
- Station Mode (connects to AP)
To be implimented
- TCP Server
- UDP Server
- Multi Connection Mode (able to have up to 5 sockets at a time)
- AP Mode (Make ESP Chip act like access point)
- DNS Support (currently websites must be looked up by IP)
- Error Recovery
Nice but not necessary
- colorized text for ESP AT Commands in Command line (easier to differentiate from other text)
Diff: ESP8266Interface.h
- Revision:
- 56:34829ec3a3da
- Parent:
- 53:8ded612adb96
- Child:
- 57:2ad35ade7a83
--- a/ESP8266Interface.h Fri Apr 15 23:58:10 2016 +0000 +++ b/ESP8266Interface.h Thu Apr 21 06:11:08 2016 -0500 @@ -23,55 +23,150 @@ #define ESP8266_SOCKET_COUNT 5 /** ESP8266Interface class - * Implementation of the NetworkInterface for the ESP8266 + * Implementation of the NetworkStack for LWIP */ -class ESP8266Interface : public WiFiInterface +class ESP8266Interface : public NetworkStack, public WiFiInterface { public: - + /** ESP8266Interface lifetime + * @param tx TX pin + * @param rx RX pin + * @param debug Enable debugging + */ ESP8266Interface(PinName tx, PinName rx, bool debug = false); - virtual ~ESP8266Interface(); - // Implementation of WiFiInterface - virtual int32_t connect( + /** Start the interface + * + * Attempts to connect to a WiFi network. If passphrase is invalid, + * NSAPI_ERROR_AUTH_ERROR is returned. + * + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * @return 0 on success, negative error code on failure + */ + virtual int connect( const char *ssid, const char *pass, - ns_security_t security = NS_SECURITY_NONE); + nsapi_security_t security = NSAPI_SECURITY_NONE); + + /** Stop the interface + * @return 0 on success, negative on failure + */ + virtual int disconnect(); + + /** Get the internally stored IP address + * @return IP address of the interface or null if not yet connected + */ + virtual const char *get_ip_address(); + + /** Get the internally stored MAC address + * @return MAC address of the interface + */ + virtual const char *get_mac_address(); + +protected: + /** Open a socket + * @param handle Handle in which to store new socket + * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP + * @return 0 on success, negative on failure + */ + virtual int socket_open(void **handle, nsapi_protocol_t proto); - virtual int32_t disconnect(); + /** Close the socket + * @param handle Socket handle + * @return 0 on success, negative on failure + * @note On failure, any memory associated with the socket must still + * be cleaned up + */ + virtual int socket_close(void *handle); + + /** Bind a server socket to a specific port + * @param handle Socket handle + * @param address Local address to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + virtual int socket_bind(void *handle, const SocketAddress &address); + + /** Start listening for incoming connections + * @param handle Socket handle + * @param backlog Number of pending connections that can be queued up at any + * one time [Default: 1] + * @return 0 on success, negative on failure + */ + virtual int socket_listen(void *handle, int backlog); + + /** Connects this TCP socket to the server + * @param handle Socket handle + * @param address SocketAddress to connect to + * @return 0 on success, negative on failure + */ + virtual int socket_connect(void *handle, const SocketAddress &address); - // Implementation of NetworkInterface - virtual const char *getIPAddress(); - virtual const char *getMACAddress(); + /** Accept a new connection. + * @param handle Handle in which to store new socket + * @param server Socket handle to server to accept from + * @return 0 on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_accept(void **handle, void *server); + + /** Send data to the remote host + * @param handle Socket handle + * @param data The buffer to send to the host + * @param size The length of the buffer to send + * @return Number of written bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_send(void *handle, const void *data, unsigned size); + + /** Receive data from the remote host + * @param handle Socket handle + * @param data The buffer in which to store the data received from the host + * @param size The maximum length of the buffer + * @return Number of received bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_recv(void *handle, void *data, unsigned size); - virtual SocketInterface *createSocket(ns_protocol_t proto); - virtual void destroySocket(SocketInterface *socket); + /** Send a packet to a remote endpoint + * @param handle Socket handle + * @param address The remote SocketAddress + * @param data The packet to be sent + * @param size The length of the packet to be sent + * @return the number of written bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); + + /** Receive a packet from a remote endpoint + * @param handle Socket handle + * @param address Destination for the remote SocketAddress or null + * @param buffer The buffer for storing the incoming packet data + * If a packet is too long to fit in the supplied buffer, + * excess bytes are discarded + * @param size The length of the buffer + * @return the number of received bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); + + /** Register a callback on state change of the socket + * @param handle Socket handle + * @param callback Function to call on state change + * @param data Argument to pass to callback + * @note Callback may be called in an interrupt context. + */ + virtual void socket_attach(void *handle, void (*callback)(void *), void *data); private: - ESP8266 _esp; bool _ids[ESP8266_SOCKET_COUNT]; - - // Implementation of the SocketInterface for the ESP8266 - class ESP8266Socket : public SocketInterface - { - public: - - // ESP8266 specific details - ESP8266 *_esp; - ns_protocol_t _proto; - int _id; - - ESP8266Socket(ESP8266 *esp, ns_protocol_t proto, int id) - : _esp(esp), _proto(proto), _id(id) {} - - // Implementation of SocketInterface - virtual int32_t open(const char *ip, uint16_t port); - virtual int32_t close(); - - virtual int32_t send(const void *data, uint32_t size); - virtual int32_t recv(void *data, uint32_t size); - }; }; #endif