18
18
#include " EthernetInterface.h"
19
19
#include " NetworkStack.h"
20
20
21
+ #include " eth_arch.h"
21
22
#include " lwip/opt.h"
23
+ #include " lwip/api.h"
22
24
#include " lwip/inet.h"
23
25
#include " lwip/netif.h"
24
26
#include " lwip/dhcp.h"
25
27
#include " lwip/tcpip.h"
26
- #include " lwip/sockets.h"
27
- #include " lwip/netdb.h"
28
- #include " netif/etharp.h"
29
- #include " eth_arch.h"
30
- #include " lwip/netif.h"
31
- #include " lwip/udp.h"
32
28
#include " lwip/tcp.h"
33
- #include " lwip/tcp_impl.h"
34
- #include " lwip/timers.h"
35
- #include " lwip/dns.h"
36
- #include " lwip/def.h"
37
- #include " lwip/ip_addr.h"
38
29
39
30
40
- /* Predeclared LWIPInterface class */
41
- class LWIPInterface : public NetworkStack
31
+ /* Predeclared LWIPStack class */
32
+ static class LWIPStack : public NetworkStack
42
33
{
43
- /* * Get the local IP address
44
- *
45
- * @return Null-terminated representation of the local IP address
46
- * or null if not yet connected
47
- */
48
34
virtual const char *get_ip_address ();
49
-
50
- /* * Open a socket
51
- * @param handle Handle in which to store new socket
52
- * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
53
- * @return 0 on success, negative on failure
54
- */
55
35
virtual int socket_open (void **handle, nsapi_protocol_t proto);
56
-
57
- /* * Close the socket
58
- * @param handle Socket handle
59
- * @return 0 on success, negative on failure
60
- * @note On failure, any memory associated with the socket must still
61
- * be cleaned up
62
- */
63
36
virtual int socket_close (void *handle);
64
-
65
- /* * Bind a server socket to a specific port
66
- * @param handle Socket handle
67
- * @param address Local address to listen for incoming connections on
68
- * @return 0 on success, negative on failure.
69
- */
70
37
virtual int socket_bind (void *handle, const SocketAddress &address);
71
-
72
- /* * Start listening for incoming connections
73
- * @param handle Socket handle
74
- * @param backlog Number of pending connections that can be queued up at any
75
- * one time [Default: 1]
76
- * @return 0 on success, negative on failure
77
- */
78
38
virtual int socket_listen (void *handle, int backlog);
79
-
80
- /* * Connects this TCP socket to the server
81
- * @param handle Socket handle
82
- * @param address SocketAddress to connect to
83
- * @return 0 on success, negative on failure
84
- */
85
39
virtual int socket_connect (void *handle, const SocketAddress &address);
86
-
87
- /* * Accept a new connection.
88
- * @param handle Handle in which to store new socket
89
- * @param server Socket handle to server to accept from
90
- * @return 0 on success, negative on failure
91
- * @note This call is not-blocking, if this call would block, must
92
- * immediately return NSAPI_ERROR_WOULD_WAIT
93
- */
94
40
virtual int socket_accept (void **handle, void *server);
95
-
96
- /* * Send data to the remote host
97
- * @param handle Socket handle
98
- * @param data The buffer to send to the host
99
- * @param size The length of the buffer to send
100
- * @return Number of written bytes on success, negative on failure
101
- * @note This call is not-blocking, if this call would block, must
102
- * immediately return NSAPI_ERROR_WOULD_WAIT
103
- */
104
41
virtual int socket_send (void *handle, const void *data, unsigned size);
105
-
106
- /* * Receive data from the remote host
107
- * @param handle Socket handle
108
- * @param data The buffer in which to store the data received from the host
109
- * @param size The maximum length of the buffer
110
- * @return Number of received bytes on success, negative on failure
111
- * @note This call is not-blocking, if this call would block, must
112
- * immediately return NSAPI_ERROR_WOULD_WAIT
113
- */
114
42
virtual int socket_recv (void *handle, void *data, unsigned size);
115
-
116
- /* * Send a packet to a remote endpoint
117
- * @param handle Socket handle
118
- * @param address The remote SocketAddress
119
- * @param data The packet to be sent
120
- * @param size The length of the packet to be sent
121
- * @return the number of written bytes on success, negative on failure
122
- * @note This call is not-blocking, if this call would block, must
123
- * immediately return NSAPI_ERROR_WOULD_WAIT
124
- */
125
43
virtual int socket_sendto (void *handle, const SocketAddress &address, const void *data, unsigned size);
126
-
127
- /* * Receive a packet from a remote endpoint
128
- * @param handle Socket handle
129
- * @param address Destination for the remote SocketAddress or null
130
- * @param buffer The buffer for storing the incoming packet data
131
- * If a packet is too long to fit in the supplied buffer,
132
- * excess bytes are discarded
133
- * @param size The length of the buffer
134
- * @return the number of received bytes on success, negative on failure
135
- * @note This call is not-blocking, if this call would block, must
136
- * immediately return NSAPI_ERROR_WOULD_WAIT
137
- */
138
44
virtual int socket_recvfrom (void *handle, SocketAddress *address, void *buffer, unsigned size);
139
-
140
- /* Set stack-specific socket options
141
- *
142
- * The setsockopt allow an application to pass stack-specific hints
143
- * to the underlying stack. For unsupported options,
144
- * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
145
- *
146
- * @param handle Socket handle
147
- * @param level Stack-specific protocol level
148
- * @param optname Stack-specific option identifier
149
- * @param optval Option value
150
- * @param optlen Length of the option value
151
- * @return 0 on success, negative error code on failure
152
- */
153
45
virtual int setsockopt (void *handle, int level, int optname, const void *optval, unsigned optlen);
154
-
155
- /* * Register a callback on state change of the socket
156
- * @param handle Socket handle
157
- * @param callback Function to call on state change
158
- * @param data Argument to pass to callback
159
- * @note Callback may be called in an interrupt context.
160
- */
161
46
virtual void socket_attach (void *handle, void (*callback)(void *), void *data);
162
- };
47
+ } lwip_stack ;
163
48
164
49
165
50
/* Static arena of sockets */
@@ -202,12 +87,11 @@ static void lwip_arena_dealloc(struct lwip_socket *s)
202
87
s->in_use = false ;
203
88
}
204
89
205
- static void lwip_socket_callback (
206
- struct netconn *nc, enum netconn_evt, u16_t len) {
90
+ static void lwip_socket_callback (struct netconn *nc, enum netconn_evt, u16_t len) {
207
91
sys_prot_t prot = sys_arch_protect ();
208
92
209
93
for (int i = 0 ; i < MEMP_NUM_NETCONN; i++) {
210
- if (lwip_arena[i].in_use
94
+ if (lwip_arena[i].in_use
211
95
&& lwip_arena[i].conn == nc
212
96
&& lwip_arena[i].cb ) {
213
97
lwip_arena[i].cb (lwip_arena[i].data );
@@ -256,7 +140,7 @@ static void lwip_set_mac_address()
256
140
#else
257
141
char mac[6 ];
258
142
mbed_mac_address (mac);
259
- snprintf (lwip_mac_addr, 19 , " %02x:%02x:%02x:%02x:%02x:%02x" ,
143
+ snprintf (lwip_mac_addr, 19 , " %02x:%02x:%02x:%02x:%02x:%02x" ,
260
144
mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]);
261
145
#endif
262
146
}
@@ -307,7 +191,7 @@ static int lwip_init()
307
191
return 0 ;
308
192
}
309
193
310
- static void lwip_deinit ()
194
+ static void lwip_deinit ()
311
195
{
312
196
dhcp_release (&lwip_netif);
313
197
dhcp_stop (&lwip_netif);
@@ -321,7 +205,7 @@ static int lwip_err_remap(err_t err) {
321
205
switch (err) {
322
206
case ERR_OK:
323
207
return 0 ;
324
- case ERR_MEM:
208
+ case ERR_MEM:
325
209
return NSAPI_ERROR_NO_MEMORY;
326
210
case ERR_CONN:
327
211
case ERR_CLSD:
@@ -342,11 +226,11 @@ static int lwip_err_remap(err_t err) {
342
226
}
343
227
344
228
/* LWIP stack implementation */
345
- const char *LWIPInterface ::get_ip_address () {
229
+ const char *LWIPStack ::get_ip_address () {
346
230
return lwip_get_ip_address ();
347
231
}
348
232
349
- int LWIPInterface ::socket_open (void **handle, nsapi_protocol_t proto)
233
+ int LWIPStack ::socket_open (void **handle, nsapi_protocol_t proto)
350
234
{
351
235
struct lwip_socket *s = lwip_arena_alloc ();
352
236
if (!s) {
@@ -367,7 +251,7 @@ int LWIPInterface::socket_open(void **handle, nsapi_protocol_t proto)
367
251
return 0 ;
368
252
}
369
253
370
- int LWIPInterface ::socket_close (void *handle)
254
+ int LWIPStack ::socket_close (void *handle)
371
255
{
372
256
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
373
257
@@ -377,7 +261,7 @@ int LWIPInterface::socket_close(void *handle)
377
261
}
378
262
379
263
380
- int LWIPInterface ::socket_bind (void *handle, const SocketAddress &addr)
264
+ int LWIPStack ::socket_bind (void *handle, const SocketAddress &addr)
381
265
{
382
266
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
383
267
@@ -388,15 +272,15 @@ int LWIPInterface::socket_bind(void *handle, const SocketAddress &addr)
388
272
return lwip_err_remap (err);
389
273
}
390
274
391
- int LWIPInterface ::socket_listen (void *handle, int backlog)
275
+ int LWIPStack ::socket_listen (void *handle, int backlog)
392
276
{
393
277
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
394
278
395
279
err_t err = netconn_listen_with_backlog (s->conn , backlog);
396
280
return lwip_err_remap (err);
397
281
}
398
282
399
- int LWIPInterface ::socket_connect (void *handle, const SocketAddress &addr)
283
+ int LWIPStack ::socket_connect (void *handle, const SocketAddress &addr)
400
284
{
401
285
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
402
286
@@ -410,7 +294,7 @@ int LWIPInterface::socket_connect(void *handle, const SocketAddress &addr)
410
294
return lwip_err_remap (err);
411
295
}
412
296
413
- int LWIPInterface ::socket_accept (void **handle, void *server)
297
+ int LWIPStack ::socket_accept (void **handle, void *server)
414
298
{
415
299
struct lwip_socket *s = static_cast <struct lwip_socket *>(server);
416
300
struct lwip_socket *ns = lwip_arena_alloc ();
@@ -422,10 +306,10 @@ int LWIPInterface::socket_accept(void **handle, void *server)
422
306
}
423
307
424
308
*reinterpret_cast <struct lwip_socket **>(handle) = ns;
425
- return 0 ;
309
+ return 0 ;
426
310
}
427
311
428
- int LWIPInterface ::socket_send (void *handle, const void *data, unsigned size)
312
+ int LWIPStack ::socket_send (void *handle, const void *data, unsigned size)
429
313
{
430
314
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
431
315
@@ -437,7 +321,7 @@ int LWIPInterface::socket_send(void *handle, const void *data, unsigned size)
437
321
return size;
438
322
}
439
323
440
- int LWIPInterface ::socket_recv (void *handle, void *data, unsigned size)
324
+ int LWIPStack ::socket_recv (void *handle, void *data, unsigned size)
441
325
{
442
326
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
443
327
@@ -462,7 +346,7 @@ int LWIPInterface::socket_recv(void *handle, void *data, unsigned size)
462
346
return recv;
463
347
}
464
348
465
- int LWIPInterface ::socket_sendto (void *handle, const SocketAddress &addr, const void *data, unsigned size)
349
+ int LWIPStack ::socket_sendto (void *handle, const SocketAddress &addr, const void *data, unsigned size)
466
350
{
467
351
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
468
352
@@ -485,7 +369,7 @@ int LWIPInterface::socket_sendto(void *handle, const SocketAddress &addr, const
485
369
return size;
486
370
}
487
371
488
- int LWIPInterface ::socket_recvfrom (void *handle, SocketAddress *addr, void *data, unsigned size)
372
+ int LWIPStack ::socket_recvfrom (void *handle, SocketAddress *addr, void *data, unsigned size)
489
373
{
490
374
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
491
375
@@ -507,15 +391,15 @@ int LWIPInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data
507
391
return recv;
508
392
}
509
393
510
- int LWIPInterface ::setsockopt (void *handle, int level, int optname, const void *optval, unsigned optlen) {
394
+ int LWIPStack ::setsockopt (void *handle, int level, int optname, const void *optval, unsigned optlen) {
511
395
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
512
396
513
397
switch (optname) {
514
398
case NSAPI_KEEPALIVE:
515
399
if (optlen != sizeof (int ) || s->conn ->type != NETCONN_TCP) {
516
400
return NSAPI_ERROR_UNSUPPORTED;
517
401
}
518
-
402
+
519
403
s->conn ->pcb .tcp ->so_options |= SOF_KEEPALIVE;
520
404
return 0 ;
521
405
@@ -534,13 +418,13 @@ int LWIPInterface::setsockopt(void *handle, int level, int optname, const void *
534
418
535
419
s->conn ->pcb .tcp ->keep_intvl = *(int *)optval;
536
420
return 0 ;
537
-
421
+
538
422
default :
539
423
return NSAPI_ERROR_UNSUPPORTED;
540
424
}
541
425
}
542
426
543
- void LWIPInterface ::socket_attach (void *handle, void (*callback)(void *), void *data)
427
+ void LWIPStack ::socket_attach (void *handle, void (*callback)(void *), void *data)
544
428
{
545
429
struct lwip_socket *s = static_cast <struct lwip_socket *>(handle);
546
430
@@ -550,11 +434,6 @@ void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *
550
434
551
435
552
436
/* Interface implementation */
553
- EthernetInterface::EthernetInterface ()
554
- {
555
- _stack = new LWIPInterface ();
556
- }
557
-
558
437
int EthernetInterface::connect ()
559
438
{
560
439
return lwip_init ();
@@ -578,5 +457,5 @@ const char *EthernetInterface::get_mac_address()
578
457
579
458
NetworkStack *EthernetInterface::get_stack ()
580
459
{
581
- return _stack ;
460
+ return &lwip_stack ;
582
461
}
0 commit comments