Skip to content

Commit 3b20e4b

Browse files
committed
Moved to static declaration of LWIPStack and refactored a bit
- Renamed LWIPInterface to LWIPStack - Moved LWIPStack to static declaration - Removed hidden comments - Reduced lwip includes
1 parent 004750b commit 3b20e4b

File tree

2 files changed

+27
-150
lines changed

2 files changed

+27
-150
lines changed

EthernetInterface.cpp

Lines changed: 27 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -18,148 +18,33 @@
1818
#include "EthernetInterface.h"
1919
#include "NetworkStack.h"
2020

21+
#include "eth_arch.h"
2122
#include "lwip/opt.h"
23+
#include "lwip/api.h"
2224
#include "lwip/inet.h"
2325
#include "lwip/netif.h"
2426
#include "lwip/dhcp.h"
2527
#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"
3228
#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"
3829

3930

40-
/* Predeclared LWIPInterface class */
41-
class LWIPInterface : public NetworkStack
31+
/* Predeclared LWIPStack class */
32+
static class LWIPStack : public NetworkStack
4233
{
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-
*/
4834
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-
*/
5535
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-
*/
6336
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-
*/
7037
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-
*/
7838
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-
*/
8539
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-
*/
9440
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-
*/
10441
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-
*/
11442
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-
*/
12543
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-
*/
13844
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-
*/
15345
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-
*/
16146
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
162-
};
47+
} lwip_stack;
16348

16449

16550
/* Static arena of sockets */
@@ -202,12 +87,11 @@ static void lwip_arena_dealloc(struct lwip_socket *s)
20287
s->in_use = false;
20388
}
20489

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) {
20791
sys_prot_t prot = sys_arch_protect();
20892

20993
for (int i = 0; i < MEMP_NUM_NETCONN; i++) {
210-
if (lwip_arena[i].in_use
94+
if (lwip_arena[i].in_use
21195
&& lwip_arena[i].conn == nc
21296
&& lwip_arena[i].cb) {
21397
lwip_arena[i].cb(lwip_arena[i].data);
@@ -256,7 +140,7 @@ static void lwip_set_mac_address()
256140
#else
257141
char mac[6];
258142
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",
260144
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
261145
#endif
262146
}
@@ -307,7 +191,7 @@ static int lwip_init()
307191
return 0;
308192
}
309193

310-
static void lwip_deinit()
194+
static void lwip_deinit()
311195
{
312196
dhcp_release(&lwip_netif);
313197
dhcp_stop(&lwip_netif);
@@ -321,7 +205,7 @@ static int lwip_err_remap(err_t err) {
321205
switch (err) {
322206
case ERR_OK:
323207
return 0;
324-
case ERR_MEM:
208+
case ERR_MEM:
325209
return NSAPI_ERROR_NO_MEMORY;
326210
case ERR_CONN:
327211
case ERR_CLSD:
@@ -342,11 +226,11 @@ static int lwip_err_remap(err_t err) {
342226
}
343227

344228
/* LWIP stack implementation */
345-
const char *LWIPInterface::get_ip_address() {
229+
const char *LWIPStack::get_ip_address() {
346230
return lwip_get_ip_address();
347231
}
348232

349-
int LWIPInterface::socket_open(void **handle, nsapi_protocol_t proto)
233+
int LWIPStack::socket_open(void **handle, nsapi_protocol_t proto)
350234
{
351235
struct lwip_socket *s = lwip_arena_alloc();
352236
if (!s) {
@@ -367,7 +251,7 @@ int LWIPInterface::socket_open(void **handle, nsapi_protocol_t proto)
367251
return 0;
368252
}
369253

370-
int LWIPInterface::socket_close(void *handle)
254+
int LWIPStack::socket_close(void *handle)
371255
{
372256
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
373257

@@ -377,7 +261,7 @@ int LWIPInterface::socket_close(void *handle)
377261
}
378262

379263

380-
int LWIPInterface::socket_bind(void *handle, const SocketAddress &addr)
264+
int LWIPStack::socket_bind(void *handle, const SocketAddress &addr)
381265
{
382266
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
383267

@@ -388,15 +272,15 @@ int LWIPInterface::socket_bind(void *handle, const SocketAddress &addr)
388272
return lwip_err_remap(err);
389273
}
390274

391-
int LWIPInterface::socket_listen(void *handle, int backlog)
275+
int LWIPStack::socket_listen(void *handle, int backlog)
392276
{
393277
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
394278

395279
err_t err = netconn_listen_with_backlog(s->conn, backlog);
396280
return lwip_err_remap(err);
397281
}
398282

399-
int LWIPInterface::socket_connect(void *handle, const SocketAddress &addr)
283+
int LWIPStack::socket_connect(void *handle, const SocketAddress &addr)
400284
{
401285
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
402286

@@ -410,7 +294,7 @@ int LWIPInterface::socket_connect(void *handle, const SocketAddress &addr)
410294
return lwip_err_remap(err);
411295
}
412296

413-
int LWIPInterface::socket_accept(void **handle, void *server)
297+
int LWIPStack::socket_accept(void **handle, void *server)
414298
{
415299
struct lwip_socket *s = static_cast<struct lwip_socket*>(server);
416300
struct lwip_socket *ns = lwip_arena_alloc();
@@ -422,10 +306,10 @@ int LWIPInterface::socket_accept(void **handle, void *server)
422306
}
423307

424308
*reinterpret_cast<struct lwip_socket**>(handle) = ns;
425-
return 0;
309+
return 0;
426310
}
427311

428-
int LWIPInterface::socket_send(void *handle, const void *data, unsigned size)
312+
int LWIPStack::socket_send(void *handle, const void *data, unsigned size)
429313
{
430314
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
431315

@@ -437,7 +321,7 @@ int LWIPInterface::socket_send(void *handle, const void *data, unsigned size)
437321
return size;
438322
}
439323

440-
int LWIPInterface::socket_recv(void *handle, void *data, unsigned size)
324+
int LWIPStack::socket_recv(void *handle, void *data, unsigned size)
441325
{
442326
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
443327

@@ -462,7 +346,7 @@ int LWIPInterface::socket_recv(void *handle, void *data, unsigned size)
462346
return recv;
463347
}
464348

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)
466350
{
467351
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
468352

@@ -485,7 +369,7 @@ int LWIPInterface::socket_sendto(void *handle, const SocketAddress &addr, const
485369
return size;
486370
}
487371

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)
489373
{
490374
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
491375

@@ -507,15 +391,15 @@ int LWIPInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data
507391
return recv;
508392
}
509393

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) {
511395
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
512396

513397
switch (optname) {
514398
case NSAPI_KEEPALIVE:
515399
if (optlen != sizeof(int) || s->conn->type != NETCONN_TCP) {
516400
return NSAPI_ERROR_UNSUPPORTED;
517401
}
518-
402+
519403
s->conn->pcb.tcp->so_options |= SOF_KEEPALIVE;
520404
return 0;
521405

@@ -534,13 +418,13 @@ int LWIPInterface::setsockopt(void *handle, int level, int optname, const void *
534418

535419
s->conn->pcb.tcp->keep_intvl = *(int*)optval;
536420
return 0;
537-
421+
538422
default:
539423
return NSAPI_ERROR_UNSUPPORTED;
540424
}
541425
}
542426

543-
void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *data)
427+
void LWIPStack::socket_attach(void *handle, void (*callback)(void *), void *data)
544428
{
545429
struct lwip_socket *s = static_cast<struct lwip_socket*>(handle);
546430

@@ -550,11 +434,6 @@ void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *
550434

551435

552436
/* Interface implementation */
553-
EthernetInterface::EthernetInterface()
554-
{
555-
_stack = new LWIPInterface();
556-
}
557-
558437
int EthernetInterface::connect()
559438
{
560439
return lwip_init();
@@ -578,5 +457,5 @@ const char *EthernetInterface::get_mac_address()
578457

579458
NetworkStack *EthernetInterface::get_stack()
580459
{
581-
return _stack;
460+
return &lwip_stack;
582461
}

EthernetInterface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class NetworkStack;
3030
class EthernetInterface : public EthInterface
3131
{
3232
public:
33-
EthernetInterface();
34-
3533
/** Start the interface
3634
* @return 0 on success, negative on failure
3735
*/

0 commit comments

Comments
 (0)