Skip to content

Commit 450cc12

Browse files
authored
Merge pull request #5 from geky/consolidate-stack
Consolidate the ESP8266Interface and ESP8266Stack
2 parents bb8182f + 6077440 commit 450cc12

File tree

2 files changed

+64
-93
lines changed

2 files changed

+64
-93
lines changed

ESP8266Interface.cpp

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626

2727
// ESP8266Interface implementation
2828
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
29-
: _esp(tx, rx, debug), _stack(_esp)
29+
: _esp(tx, rx, debug)
3030
{
31-
// Do nothing
31+
memset(_ids, 0, sizeof(_ids));
32+
memset(_cbs, 0, sizeof(_cbs));
33+
34+
_esp.attach(this, &ESP8266Interface::event);
3235
}
3336

3437
int ESP8266Interface::connect(
@@ -78,31 +81,13 @@ const char* ESP8266Interface::get_mac_address()
7881
return _esp.getMACAddress();
7982
}
8083

81-
NetworkStack* ESP8266Interface::get_stack(void)
82-
{
83-
return &_stack;
84-
}
85-
8684
struct esp8266_socket {
8785
int id;
8886
nsapi_protocol_t proto;
8987
bool connected;
9088
};
9189

92-
ESP8266Stack::ESP8266Stack(ESP8266 &esp): _esp(esp)
93-
{
94-
memset(_ids, 0, sizeof(_ids));
95-
memset(_cbs, 0, sizeof(_cbs));
96-
97-
_esp.attach(this, &ESP8266Stack::event);
98-
};
99-
100-
const char *ESP8266Stack::get_ip_address()
101-
{
102-
return _esp.getIPAddress();
103-
}
104-
105-
int ESP8266Stack::socket_open(void **handle, nsapi_protocol_t proto)
90+
int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
10691
{
10792
// Look for an unused socket
10893
int id = -1;
@@ -131,7 +116,7 @@ int ESP8266Stack::socket_open(void **handle, nsapi_protocol_t proto)
131116
return 0;
132117
}
133118

134-
int ESP8266Stack::socket_close(void *handle)
119+
int ESP8266Interface::socket_close(void *handle)
135120
{
136121
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
137122
int err = 0;
@@ -146,17 +131,17 @@ int ESP8266Stack::socket_close(void *handle)
146131
return err;
147132
}
148133

149-
int ESP8266Stack::socket_bind(void *handle, const SocketAddress &address)
134+
int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
150135
{
151136
return NSAPI_ERROR_UNSUPPORTED;
152137
}
153138

154-
int ESP8266Stack::socket_listen(void *handle, int backlog)
139+
int ESP8266Interface::socket_listen(void *handle, int backlog)
155140
{
156141
return NSAPI_ERROR_UNSUPPORTED;
157142
}
158143

159-
int ESP8266Stack::socket_connect(void *handle, const SocketAddress &addr)
144+
int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
160145
{
161146
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
162147
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
@@ -170,12 +155,12 @@ int ESP8266Stack::socket_connect(void *handle, const SocketAddress &addr)
170155
return 0;
171156
}
172157

173-
int ESP8266Stack::socket_accept(void **handle, void *server)
158+
int ESP8266Interface::socket_accept(void **handle, void *server)
174159
{
175160
return NSAPI_ERROR_UNSUPPORTED;
176161
}
177162

178-
int ESP8266Stack::socket_send(void *handle, const void *data, unsigned size)
163+
int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
179164
{
180165
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
181166
_esp.setTimeout(ESP8266_SEND_TIMEOUT);
@@ -187,7 +172,7 @@ int ESP8266Stack::socket_send(void *handle, const void *data, unsigned size)
187172
return size;
188173
}
189174

190-
int ESP8266Stack::socket_recv(void *handle, void *data, unsigned size)
175+
int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
191176
{
192177
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
193178
_esp.setTimeout(ESP8266_RECV_TIMEOUT);
@@ -200,7 +185,7 @@ int ESP8266Stack::socket_recv(void *handle, void *data, unsigned size)
200185
return recv;
201186
}
202187

203-
int ESP8266Stack::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
188+
int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
204189
{
205190
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
206191
if (!socket->connected) {
@@ -213,20 +198,20 @@ int ESP8266Stack::socket_sendto(void *handle, const SocketAddress &addr, const v
213198
return socket_send(socket, data, size);
214199
}
215200

216-
int ESP8266Stack::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
201+
int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
217202
{
218203
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
219204
return socket_recv(socket, data, size);
220205
}
221206

222-
void ESP8266Stack::socket_attach(void *handle, void (*callback)(void *), void *data)
207+
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
223208
{
224209
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
225210
_cbs[socket->id].callback = callback;
226211
_cbs[socket->id].data = data;
227212
}
228213

229-
void ESP8266Stack::event() {
214+
void ESP8266Interface::event() {
230215
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
231216
if (_cbs[i].callback) {
232217
_cbs[i].callback(_cbs[i].data);

ESP8266Interface.h

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,56 @@
1717
#ifndef ESP8266_INTERFACE_H
1818
#define ESP8266_INTERFACE_H
1919

20-
#include "WiFiInterface.h"
21-
#include "NetworkStack.h"
20+
#include "network-socket/NetworkStack.h"
21+
#include "network-socket/WiFiInterface.h"
2222
#include "ESP8266.h"
2323

24+
2425
#define ESP8266_SOCKET_COUNT 5
2526

26-
/** ESP8266Stack class
27+
/** ESP8266Interface class
2728
* Implementation of the NetworkStack for the ESP8266
2829
*/
29-
class ESP8266Stack : public NetworkStack
30+
class ESP8266Interface : public NetworkStack, public WiFiInterface
3031
{
3132
public:
33+
/** ESP8266Interface lifetime
34+
* @param tx TX pin
35+
* @param rx RX pin
36+
* @param debug Enable debugging
37+
*/
38+
ESP8266Interface(PinName tx, PinName rx, bool debug = false);
39+
40+
/** Start the interface
41+
*
42+
* Attempts to connect to a WiFi network. If passphrase is invalid,
43+
* NSAPI_ERROR_AUTH_ERROR is returned.
44+
*
45+
* @param ssid Name of the network to connect to
46+
* @param pass Security passphrase to connect to the network
47+
* @param security Type of encryption for connection
48+
* @return 0 on success, negative error code on failure
49+
*/
50+
virtual int connect(
51+
const char *ssid,
52+
const char *pass,
53+
nsapi_security_t security = NSAPI_SECURITY_NONE);
54+
55+
/** Stop the interface
56+
* @return 0 on success, negative on failure
57+
*/
58+
virtual int disconnect();
59+
3260
/** Get the internally stored IP address
3361
* @return IP address of the interface or null if not yet connected
3462
*/
3563
virtual const char *get_ip_address();
3664

65+
/** Get the internally stored MAC address
66+
* @return MAC address of the interface
67+
*/
68+
virtual const char *get_mac_address();
69+
3770
protected:
3871
/** Open a socket
3972
* @param handle Handle in which to store new socket
@@ -132,73 +165,26 @@ class ESP8266Stack : public NetworkStack
132165
* @note Callback may be called in an interrupt context.
133166
*/
134167
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
168+
169+
/** Provide access to the NetworkStack object
170+
*
171+
* @return The underlying NetworkStack object
172+
*/
173+
virtual NetworkStack *get_stack()
174+
{
175+
return this;
176+
}
135177

136178
private:
137-
friend class ESP8266Interface;
138-
ESP8266 &_esp;
179+
ESP8266 _esp;
139180
bool _ids[ESP8266_SOCKET_COUNT];
140181

141182
void event();
142183
struct {
143184
void (*callback)(void *);
144185
void *data;
145186
} _cbs[ESP8266_SOCKET_COUNT];
146-
147-
ESP8266Stack(ESP8266 &esp);
148187
};
149188

150189

151-
/** ESP8266Stack class
152-
* Implementation of the NetworkInterface for the ESP8266
153-
*/
154-
class ESP8266Interface : public WiFiInterface
155-
{
156-
public:
157-
/** ESP8266Interface lifetime
158-
* @param tx TX pin
159-
* @param rx RX pin
160-
* @param debug Enable debugging
161-
*/
162-
ESP8266Interface(PinName tx, PinName rx, bool debug = false);
163-
164-
/** Start the interface
165-
*
166-
* Attempts to connect to a WiFi network. If passphrase is invalid,
167-
* NSAPI_ERROR_AUTH_ERROR is returned.
168-
*
169-
* @param ssid Name of the network to connect to
170-
* @param pass Security passphrase to connect to the network
171-
* @param security Type of encryption for connection
172-
* @return 0 on success, negative error code on failure
173-
*/
174-
virtual int connect(
175-
const char *ssid,
176-
const char *pass,
177-
nsapi_security_t security = NSAPI_SECURITY_NONE);
178-
179-
/** Stop the interface
180-
* @return 0 on success, negative on failure
181-
*/
182-
virtual int disconnect();
183-
184-
/** Get the internally stored MAC address
185-
* @return MAC address of the interface
186-
*/
187-
virtual const char* get_mac_address();
188-
189-
190-
/** Get the internally stored IP address
191-
* @return IP address of the interface or null if not yet connected
192-
*/
193-
virtual const char* get_ip_address();
194-
195-
protected:
196-
virtual NetworkStack* get_stack(void);
197-
198-
private:
199-
ESP8266 _esp;
200-
ESP8266Stack _stack;
201-
202-
};
203-
204190
#endif

0 commit comments

Comments
 (0)