From 8e0c9a889667fd3a837894a1cace2d8196a691a6 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Fri, 24 Jun 2016 14:32:58 -0500 Subject: [PATCH 1/2] Separate interface from stack in ESP8266 Create separate objects for the interface and stack of the ESP8266. --- ESP8266Interface.cpp | 49 ++++++++++++++-------- ESP8266Interface.h | 96 ++++++++++++++++++++++++++++---------------- 2 files changed, 93 insertions(+), 52 deletions(-) diff --git a/ESP8266Interface.cpp b/ESP8266Interface.cpp index 71cc595..85fbaf1 100644 --- a/ESP8266Interface.cpp +++ b/ESP8266Interface.cpp @@ -26,12 +26,9 @@ // ESP8266Interface implementation ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) - : _esp(tx, rx, debug) + : _esp(tx, rx, debug), _stack(_esp) { - memset(_ids, 0, sizeof(_ids)); - memset(_cbs, 0, sizeof(_cbs)); - - _esp.attach(this, &ESP8266Interface::event); + // Do nothing } int ESP8266Interface::connect( @@ -81,13 +78,31 @@ const char *ESP8266Interface::get_mac_address() return _esp.getMACAddress(); } +NetworkStack * ESP8266Interface::get_stack(void) +{ + return &_stack; +} + struct esp8266_socket { int id; nsapi_protocol_t proto; bool connected; }; -int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) +ESP8266Stack::ESP8266Stack(ESP8266 &esp): _esp(esp) +{ + memset(_ids, 0, sizeof(_ids)); + memset(_cbs, 0, sizeof(_cbs)); + + _esp.attach(this, &ESP8266Stack::event); +}; + +const char *ESP8266Stack::get_ip_address() +{ + return _esp.getIPAddress(); +} + +int ESP8266Stack::socket_open(void **handle, nsapi_protocol_t proto) { // Look for an unused socket int id = -1; @@ -116,7 +131,7 @@ int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) return 0; } -int ESP8266Interface::socket_close(void *handle) +int ESP8266Stack::socket_close(void *handle) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; int err = 0; @@ -131,17 +146,17 @@ int ESP8266Interface::socket_close(void *handle) return err; } -int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) +int ESP8266Stack::socket_bind(void *handle, const SocketAddress &address) { return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_listen(void *handle, int backlog) +int ESP8266Stack::socket_listen(void *handle, int backlog) { return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) +int ESP8266Stack::socket_connect(void *handle, const SocketAddress &addr) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; _esp.setTimeout(ESP8266_MISC_TIMEOUT); @@ -155,12 +170,12 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) return 0; } -int ESP8266Interface::socket_accept(void **handle, void *server) +int ESP8266Stack::socket_accept(void **handle, void *server) { return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) +int ESP8266Stack::socket_send(void *handle, const void *data, unsigned size) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; _esp.setTimeout(ESP8266_SEND_TIMEOUT); @@ -172,7 +187,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) return size; } -int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) +int ESP8266Stack::socket_recv(void *handle, void *data, unsigned size) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; _esp.setTimeout(ESP8266_RECV_TIMEOUT); @@ -185,7 +200,7 @@ int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) return recv; } -int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) +int ESP8266Stack::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; if (!socket->connected) { @@ -198,20 +213,20 @@ int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, con return socket_send(socket, data, size); } -int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) +int ESP8266Stack::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; return socket_recv(socket, data, size); } -void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) +void ESP8266Stack::socket_attach(void *handle, void (*callback)(void *), void *data) { struct esp8266_socket *socket = (struct esp8266_socket *)handle; _cbs[socket->id].callback = callback; _cbs[socket->id].data = data; } -void ESP8266Interface::event() { +void ESP8266Stack::event() { for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { if (_cbs[i].callback) { _cbs[i].callback(_cbs[i].data); diff --git a/ESP8266Interface.h b/ESP8266Interface.h index 831e027..187bb09 100644 --- a/ESP8266Interface.h +++ b/ESP8266Interface.h @@ -18,53 +18,22 @@ #define ESP8266_INTERFACE_H #include "WiFiInterface.h" +#include "NetworkStack.h" #include "ESP8266.h" #define ESP8266_SOCKET_COUNT 5 -/** ESP8266Interface class +/** ESP8266Stack class * Implementation of the NetworkStack for the ESP8266 */ -class ESP8266Interface : public NetworkStack, public WiFiInterface +class ESP8266Stack : public NetworkStack { public: - /** ESP8266Interface lifetime - * @param tx TX pin - * @param rx RX pin - * @param debug Enable debugging - */ - ESP8266Interface(PinName tx, PinName rx, bool debug = false); - - /** 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, - 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 @@ -165,7 +134,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface virtual void socket_attach(void *handle, void (*callback)(void *), void *data); private: - ESP8266 _esp; + friend class ESP8266Interface; + ESP8266 &_esp; bool _ids[ESP8266_SOCKET_COUNT]; void event(); @@ -173,6 +143,62 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface void (*callback)(void *); void *data; } _cbs[ESP8266_SOCKET_COUNT]; + + ESP8266Stack(ESP8266 &esp); +}; + + +/** ESP8266Stack class + * Implementation of the NetworkInterface for the ESP8266 + */ +class ESP8266Interface : 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); + + /** 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, + nsapi_security_t security = NSAPI_SECURITY_NONE); + + /** Stop the interface + * @return 0 on success, negative on failure + */ + virtual int disconnect(); + + /** Get the internally stored MAC address + * @return MAC address of the interface + */ + virtual const char *get_mac_address(); + + + /** Get the internally stored IP address + * @return IP address of the interface or null if not yet connected + */ + virtual const char *get_ip_address(); + +protected: + virtual NetworkStack * get_stack(void); + +private: + ESP8266 _esp; + ESP8266Stack _stack; + }; #endif From a3b83979ec8272d283fb1a2ad25c9b9e74d9e0cf Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Mon, 27 Jun 2016 11:06:21 -0500 Subject: [PATCH 2/2] Update asterisk position Move the asterisk so it is closer to the return type on several functions. --- ESP8266Interface.cpp | 6 +++--- ESP8266Interface.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ESP8266Interface.cpp b/ESP8266Interface.cpp index 85fbaf1..c30ea21 100644 --- a/ESP8266Interface.cpp +++ b/ESP8266Interface.cpp @@ -68,17 +68,17 @@ int ESP8266Interface::disconnect() return 0; } -const char *ESP8266Interface::get_ip_address() +const char* ESP8266Interface::get_ip_address() { return _esp.getIPAddress(); } -const char *ESP8266Interface::get_mac_address() +const char* ESP8266Interface::get_mac_address() { return _esp.getMACAddress(); } -NetworkStack * ESP8266Interface::get_stack(void) +NetworkStack* ESP8266Interface::get_stack(void) { return &_stack; } diff --git a/ESP8266Interface.h b/ESP8266Interface.h index 187bb09..8f2bc9c 100644 --- a/ESP8266Interface.h +++ b/ESP8266Interface.h @@ -184,16 +184,16 @@ class ESP8266Interface : public WiFiInterface /** Get the internally stored MAC address * @return MAC address of the interface */ - virtual const char *get_mac_address(); + virtual const char* get_mac_address(); /** Get the internally stored IP address * @return IP address of the interface or null if not yet connected */ - virtual const char *get_ip_address(); + virtual const char* get_ip_address(); protected: - virtual NetworkStack * get_stack(void); + virtual NetworkStack* get_stack(void); private: ESP8266 _esp;