Skip to content

Commit 636af25

Browse files
authored
Merge pull request #2 from c1728p9/nsapi_update
Separate interface from stack in ESP8266
2 parents cc460d8 + a3b8397 commit 636af25

File tree

2 files changed

+95
-54
lines changed

2 files changed

+95
-54
lines changed

ESP8266Interface.cpp

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

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

3734
int ESP8266Interface::connect(
@@ -71,23 +68,41 @@ int ESP8266Interface::disconnect()
7168
return 0;
7269
}
7370

74-
const char *ESP8266Interface::get_ip_address()
71+
const char* ESP8266Interface::get_ip_address()
7572
{
7673
return _esp.getIPAddress();
7774
}
7875

79-
const char *ESP8266Interface::get_mac_address()
76+
const char* ESP8266Interface::get_mac_address()
8077
{
8178
return _esp.getMACAddress();
8279
}
8380

81+
NetworkStack* ESP8266Interface::get_stack(void)
82+
{
83+
return &_stack;
84+
}
85+
8486
struct esp8266_socket {
8587
int id;
8688
nsapi_protocol_t proto;
8789
bool connected;
8890
};
8991

90-
int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
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)
91106
{
92107
// Look for an unused socket
93108
int id = -1;
@@ -116,7 +131,7 @@ int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
116131
return 0;
117132
}
118133

119-
int ESP8266Interface::socket_close(void *handle)
134+
int ESP8266Stack::socket_close(void *handle)
120135
{
121136
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
122137
int err = 0;
@@ -131,17 +146,17 @@ int ESP8266Interface::socket_close(void *handle)
131146
return err;
132147
}
133148

134-
int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
149+
int ESP8266Stack::socket_bind(void *handle, const SocketAddress &address)
135150
{
136151
return NSAPI_ERROR_UNSUPPORTED;
137152
}
138153

139-
int ESP8266Interface::socket_listen(void *handle, int backlog)
154+
int ESP8266Stack::socket_listen(void *handle, int backlog)
140155
{
141156
return NSAPI_ERROR_UNSUPPORTED;
142157
}
143158

144-
int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
159+
int ESP8266Stack::socket_connect(void *handle, const SocketAddress &addr)
145160
{
146161
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
147162
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
@@ -155,12 +170,12 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
155170
return 0;
156171
}
157172

158-
int ESP8266Interface::socket_accept(void **handle, void *server)
173+
int ESP8266Stack::socket_accept(void **handle, void *server)
159174
{
160175
return NSAPI_ERROR_UNSUPPORTED;
161176
}
162177

163-
int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
178+
int ESP8266Stack::socket_send(void *handle, const void *data, unsigned size)
164179
{
165180
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
166181
_esp.setTimeout(ESP8266_SEND_TIMEOUT);
@@ -172,7 +187,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
172187
return size;
173188
}
174189

175-
int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
190+
int ESP8266Stack::socket_recv(void *handle, void *data, unsigned size)
176191
{
177192
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
178193
_esp.setTimeout(ESP8266_RECV_TIMEOUT);
@@ -185,7 +200,7 @@ int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
185200
return recv;
186201
}
187202

188-
int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
203+
int ESP8266Stack::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
189204
{
190205
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
191206
if (!socket->connected) {
@@ -198,20 +213,20 @@ int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, con
198213
return socket_send(socket, data, size);
199214
}
200215

201-
int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
216+
int ESP8266Stack::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
202217
{
203218
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
204219
return socket_recv(socket, data, size);
205220
}
206221

207-
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
222+
void ESP8266Stack::socket_attach(void *handle, void (*callback)(void *), void *data)
208223
{
209224
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
210225
_cbs[socket->id].callback = callback;
211226
_cbs[socket->id].data = data;
212227
}
213228

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

ESP8266Interface.h

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,22 @@
1818
#define ESP8266_INTERFACE_H
1919

2020
#include "WiFiInterface.h"
21+
#include "NetworkStack.h"
2122
#include "ESP8266.h"
2223

2324
#define ESP8266_SOCKET_COUNT 5
2425

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

63-
/** Get the internally stored MAC address
64-
* @return MAC address of the interface
65-
*/
66-
virtual const char *get_mac_address();
67-
6837
protected:
6938
/** Open a socket
7039
* @param handle Handle in which to store new socket
@@ -165,14 +134,71 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
165134
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
166135

167136
private:
168-
ESP8266 _esp;
137+
friend class ESP8266Interface;
138+
ESP8266 &_esp;
169139
bool _ids[ESP8266_SOCKET_COUNT];
170140

171141
void event();
172142
struct {
173143
void (*callback)(void *);
174144
void *data;
175145
} _cbs[ESP8266_SOCKET_COUNT];
146+
147+
ESP8266Stack(ESP8266 &esp);
148+
};
149+
150+
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+
176202
};
177203

178204
#endif

0 commit comments

Comments
 (0)