Skip to content

Separate interface from stack in ESP8266 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -71,23 +68,41 @@ 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)
{
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
96 changes: 61 additions & 35 deletions ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -165,14 +134,71 @@ 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();
struct {
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