Skip to content

Consolidate the ESP8266Interface and ESP8266Stack #5

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
Jul 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
49 changes: 17 additions & 32 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

// ESP8266Interface implementation
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
: _esp(tx, rx, debug), _stack(_esp)
: _esp(tx, rx, debug)
{
// Do nothing
memset(_ids, 0, sizeof(_ids));
memset(_cbs, 0, sizeof(_cbs));

_esp.attach(this, &ESP8266Interface::event);
}

int ESP8266Interface::connect(
Expand Down Expand Up @@ -78,31 +81,13 @@ 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;
};

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)
int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
{
// Look for an unused socket
int id = -1;
Expand Down Expand Up @@ -131,7 +116,7 @@ int ESP8266Stack::socket_open(void **handle, nsapi_protocol_t proto)
return 0;
}

int ESP8266Stack::socket_close(void *handle)
int ESP8266Interface::socket_close(void *handle)
{
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
int err = 0;
Expand All @@ -146,17 +131,17 @@ int ESP8266Stack::socket_close(void *handle)
return err;
}

int ESP8266Stack::socket_bind(void *handle, const SocketAddress &address)
int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
{
return NSAPI_ERROR_UNSUPPORTED;
}

int ESP8266Stack::socket_listen(void *handle, int backlog)
int ESP8266Interface::socket_listen(void *handle, int backlog)
{
return NSAPI_ERROR_UNSUPPORTED;
}

int ESP8266Stack::socket_connect(void *handle, const SocketAddress &addr)
int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
{
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
Expand All @@ -170,12 +155,12 @@ int ESP8266Stack::socket_connect(void *handle, const SocketAddress &addr)
return 0;
}

int ESP8266Stack::socket_accept(void **handle, void *server)
int ESP8266Interface::socket_accept(void **handle, void *server)
{
return NSAPI_ERROR_UNSUPPORTED;
}

int ESP8266Stack::socket_send(void *handle, const void *data, unsigned size)
int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
{
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
_esp.setTimeout(ESP8266_SEND_TIMEOUT);
Expand All @@ -187,7 +172,7 @@ int ESP8266Stack::socket_send(void *handle, const void *data, unsigned size)
return size;
}

int ESP8266Stack::socket_recv(void *handle, void *data, unsigned size)
int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
{
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
_esp.setTimeout(ESP8266_RECV_TIMEOUT);
Expand All @@ -200,7 +185,7 @@ int ESP8266Stack::socket_recv(void *handle, void *data, unsigned size)
return recv;
}

int ESP8266Stack::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
int ESP8266Interface::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 @@ -213,20 +198,20 @@ int ESP8266Stack::socket_sendto(void *handle, const SocketAddress &addr, const v
return socket_send(socket, data, size);
}

int ESP8266Stack::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
int ESP8266Interface::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 ESP8266Stack::socket_attach(void *handle, void (*callback)(void *), void *data)
void ESP8266Interface::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 ESP8266Stack::event() {
void ESP8266Interface::event() {
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
if (_cbs[i].callback) {
_cbs[i].callback(_cbs[i].data);
Expand Down
108 changes: 47 additions & 61 deletions ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,56 @@
#ifndef ESP8266_INTERFACE_H
#define ESP8266_INTERFACE_H

#include "WiFiInterface.h"
#include "NetworkStack.h"
#include "network-socket/NetworkStack.h"
#include "network-socket/WiFiInterface.h"
#include "ESP8266.h"


#define ESP8266_SOCKET_COUNT 5

/** ESP8266Stack class
/** ESP8266Interface class
* Implementation of the NetworkStack for the ESP8266
*/
class ESP8266Stack : public NetworkStack
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);

/** 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 @@ -132,73 +165,26 @@ class ESP8266Stack : public NetworkStack
* @note Callback may be called in an interrupt context.
*/
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);

/** Provide access to the NetworkStack object
*
* @return The underlying NetworkStack object
*/
virtual NetworkStack *get_stack()
{
return this;
}

private:
friend class ESP8266Interface;
ESP8266 &_esp;
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