Skip to content

Commit 8b4d3f1

Browse files
New feature: send/recv message implementation added to network stack
1 parent 81aaef4 commit 8b4d3f1

File tree

24 files changed

+644
-49
lines changed

24 files changed

+644
-49
lines changed

UNITTESTS/stubs/NetworkStack_stub.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class NetworkStackstub : public NetworkStack {
4040
{
4141
}
4242

43-
virtual nsapi_error_t get_ip_address(SocketAddress* address)
43+
virtual nsapi_error_t get_ip_address(SocketAddress *address)
4444
{
4545
address->set_ip_address("127.0.0.1");
4646
return NSAPI_ERROR_OK;
@@ -136,6 +136,12 @@ class NetworkStackstub : public NetworkStack {
136136
{
137137
return return_value;
138138
};
139+
virtual nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
140+
const void *data, nsapi_size_t size,
141+
nsapi_msghdr_t *control, nsapi_size_t control_size)
142+
{
143+
return return_value;
144+
};
139145
virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
140146
void *buffer, nsapi_size_t size)
141147
{
@@ -149,6 +155,12 @@ class NetworkStackstub : public NetworkStack {
149155
}
150156
return return_value;
151157
};
158+
virtual nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
159+
void *data, nsapi_size_t size,
160+
nsapi_msghdr_t *control, nsapi_size_t control_size)
161+
{
162+
return return_value;
163+
};
152164
virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) {};
153165

154166
private:

connectivity/cellular/include/cellular/framework/AT/AT_CellularStack.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ class AT_CellularStack : public NetworkStack {
8888

8989
virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
9090

91+
92+
nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
93+
const void *data, nsapi_size_t size,
94+
nsapi_msghdr_t *control, nsapi_size_t control_size) override
95+
{
96+
return NSAPI_ERROR_UNSUPPORTED;
97+
}
98+
99+
nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
100+
void *data, nsapi_size_t size,
101+
nsapi_msghdr_t *control, nsapi_size_t control_size) override
102+
{
103+
return NSAPI_ERROR_UNSUPPORTED;
104+
}
105+
91106
protected:
92107
class CellularSocket {
93108
public:

connectivity/lwipstack/include/lwipstack/LWIPStack.h

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
163163
static void netif_status_irq(struct netif *netif);
164164
static Interface *our_if_from_netif(struct netif *netif);
165165
static void delete_interface(OnboardNetworkStack::Interface **interface_out);
166+
NetworkInterface *network_if_from_netif_id(int id);
167+
int netif_id_from_network_if(NetworkInterface *userInterface);
168+
166169

167170
#if LWIP_ETHERNET
168171
static err_t emac_low_level_output(struct netif *netif, struct pbuf *p);
@@ -222,6 +225,8 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
222225
void *hw; /**< alternative implementation pointer - used for PPP */
223226
};
224227

228+
NetworkInterface *user_network_interface;
229+
225230
mbed_rtos_storage_semaphore_t remove_interface_sem;
226231
osSemaphoreId_t remove_interface;
227232
mbed_rtos_storage_semaphore_t linked_sem;
@@ -265,7 +270,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
265270
* @param[out] interface_out pointer to stack interface object controlling the EMAC
266271
* @return NSAPI_ERROR_OK on success, or error code
267272
*/
268-
nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out) override;
273+
nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out, NetworkInterface *user_network_interface = NULL) override;
269274

270275
/** Register a network interface with the IP stack
271276
*
@@ -450,6 +455,27 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
450455
nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
451456
const void *data, nsapi_size_t size) override;
452457

458+
/** Send a packet with ancillary data over a UDP socket
459+
*
460+
* Sends data to the specified address. Returns the number of bytes
461+
* sent from the buffer.
462+
*
463+
* This call is non-blocking. If sendto would block,
464+
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
465+
*
466+
* @param handle Socket handle
467+
* @param address The SocketAddress of the remote host
468+
* @param data Buffer of data to send to the host
469+
* @param size Size of the buffer in bytes
470+
* @param control Ancillary data storage
471+
* @param control_size Size of the Ancillary data in bytes
472+
* @return Number of sent bytes on success, negative error
473+
* code on failure
474+
*/
475+
nsapi_size_or_error_t socket_sendmsg(nsapi_socket_t handle, const SocketAddress &address,
476+
const void *data, nsapi_size_t size,
477+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
478+
453479
/** Receive data over a TCP socket
454480
*
455481
* The socket must be connected to a remote host. Returns the number of
@@ -493,6 +519,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
493519
* This call is non-blocking. If recvfrom would block,
494520
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
495521
*
522+
* It uses socket_recvmsg with zero ancillary data.
496523
* @param handle Socket handle
497524
* @param address Destination for the source address or NULL
498525
* @param buffer Destination buffer for data received from the host
@@ -503,6 +530,27 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
503530
nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
504531
void *buffer, nsapi_size_t size) override;
505532

533+
/** Receive a packet with ancillary data over a UDP socket
534+
*
535+
* Receives data and stores the source address in address if address
536+
* is not NULL. Returns the number of bytes received into the buffer.
537+
*
538+
* This call is non-blocking. If recvfrom would block,
539+
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
540+
*
541+
* @param handle Socket handle
542+
* @param address Destination for the source address or NULL
543+
* @param buffer Destination buffer for data received from the host
544+
* @param size Size of the buffer in bytes
545+
* @param control Ancillary data storage
546+
* @param control_size Size of the Ancillary data in bytes
547+
* @return Number of received bytes on success, negative error
548+
* code on failure
549+
*/
550+
nsapi_size_or_error_t socket_recvmsg(nsapi_socket_t handle, SocketAddress *address,
551+
void *data, nsapi_size_t size,
552+
nsapi_msghdr_t *control, nsapi_size_t control_size) override;
553+
506554
/** Register a callback on state change of the socket
507555
*
508556
* The specified callback will be called on state changes such as when

connectivity/lwipstack/include/lwipstack/lwipopts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@
311311

312312
#endif
313313

314+
// FIXME: Add compile time configuration and define conditionaly
315+
#define LWIP_NETBUF_RECVINFO MBED_CONF_LWIP_NETBUF_RECVINFO_ENABLED
316+
314317
// Make sure we default these to off, so
315318
// LWIP doesn't default to on
316319
#ifndef LWIP_ARP

connectivity/lwipstack/lwip/test/unit/lwipopts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#define LWIP_NETCONN !NO_SYS
4747
#define LWIP_SOCKET !NO_SYS
4848
#define LWIP_NETCONN_FULLDUPLEX LWIP_SOCKET
49-
#define LWIP_NETBUF_RECVINFO 1
49+
#define LWIP_NETBUF_RECVINFO MBED_CONF_NETBUF_RECVINFO_ENABLED
5050
#define LWIP_HAVE_LOOPIF 1
5151
#define TCPIP_THREAD_TEST
5252

connectivity/lwipstack/source/LWIPInterface.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@
4242

4343
LWIP::Interface *LWIP::Interface::list;
4444

45+
NetworkInterface *LWIP::Interface::network_if_from_netif_id(int id)
46+
{
47+
for (Interface *interface = list; interface; interface = interface->next) {
48+
if (id == netif_get_index(&interface->netif)) {
49+
return interface->user_network_interface;
50+
}
51+
}
52+
return NULL;
53+
}
54+
55+
int LWIP::Interface::netif_id_from_network_if(NetworkInterface *userInterface)
56+
{
57+
if (userInterface != NULL) {
58+
for (Interface *interface = list; interface; interface = interface->next) {
59+
if (userInterface == interface->user_network_interface) {
60+
return netif_get_index(&interface->netif);
61+
}
62+
}
63+
}
64+
return 0;
65+
}
66+
4567
LWIP::Interface *LWIP::Interface::our_if_from_netif(struct netif *netif)
4668
{
4769
for (Interface *interface = list; interface; interface = interface->next) {
@@ -408,7 +430,7 @@ LWIP::Interface::Interface() :
408430
list = this;
409431
}
410432

411-
nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out)
433+
nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out, NetworkInterface *user_network_interface)
412434
{
413435
#if LWIP_ETHERNET
414436
Interface *interface = new (std::nothrow) Interface();
@@ -431,7 +453,7 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
431453
#endif
432454

433455
interface->netif.hwaddr_len = 6;
434-
456+
interface->user_network_interface = user_network_interface;
435457
if (!netif_add(&interface->netif,
436458
#if LWIP_IPV4
437459
0, 0, 0,

0 commit comments

Comments
 (0)