Mistake on this page?
Report an issue in GitHub or email us
LWIPStack.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2017 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef LWIPSTACK_H_
19 #define LWIPSTACK_H_
20 
21 #include "lwip/tcpip.h"
22 #include "lwip/tcp.h"
23 #include "lwip/ip.h"
24 #include "lwip/api.h"
25 #include "netif/etharp.h"
26 #include "lwip/ethip6.h"
27 #include "netsocket/nsapi_types.h"
28 #include "netsocket/EMAC.h"
29 #include "netsocket/L3IP.h"
30 #include "netsocket/PPP.h"
31 #include "netsocket/OnboardNetworkStack.h"
32 #include "LWIPMemoryManager.h"
33 
34 #if LWIP_IPV6
35 #include "lwip/ip6_addr.h"
36 #define IP_ADDR_VALID IP6_ADDR_VALID
37 #else
38 #define IP_ADDR_VALID 0
39 #endif
40 
41 class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
42 public:
44  static LWIP &get_instance();
45 
47  public:
48  /** Set IP address to LWIP stack
49  *
50  * This function can set both IPv4 or IPv6 address to LWIP stack.
51  *
52  * bringup() can only take one IP address and in dual stack case
53  * another IP address can be set using this function.
54  *
55  * IPv4 or IPv6 address should be in format of https://tools.ietf.org/html/draft-main-ipaddr-text-rep-00.
56  *
57  * @param ip IP address to be used for the interface as IPv4 or IPv6 address string.
58  * This parameter should not be NULL.
59  * @param netmask Net mask to be used for the interface as IPv4 address string or NULL.
60  * NULL value will set this value to 255.255.255.255.
61  * This parameter will be ignored for IPv6 ip argument.
62  * @param gw Gateway address to be used for the interface as IPv4 address string or NULL
63  * NULL value will set this value to 0.0.0.0
64  * This parameter will be ignored for IPv6 ip argument.
65  * @param ipv6_flag Provide this flag for IPv6 state flag override. For example, you can set IP6_ADDR_PREFERRED.
66  * Default value is IP6_ADDR_VALID. For IPv4, this value will be ignored.
67  * @return NSAPI_ERROR_OK on success, or error code
68  */
69  nsapi_error_t set_ip_address(const char *ip,
70  const char *netmask,
71  const char *gw,
72  uint8_t ipv6_flag = IP_ADDR_VALID
73  ) override;
74 
75  /** Connect the interface to the network
76  *
77  * Sets up a connection on specified network interface, using DHCP or provided network details. If the @a dhcp is set to
78  * true all the remaining parameters are ignored.
79  *
80  * @param dhcp true if the network details should be acquired using DHCP
81  * @param ip IP address to be used for the interface as "W:X:Y:Z" or NULL
82  * @param netmask Net mask to be used for the interface as "W:X:Y:Z" or NULL
83  * @param gw Gateway address to be used for the interface as "W:X:Y:Z" or NULL
84  * @param stack Allow manual selection of IPv4 and/or IPv6.
85  * @param blocking Specify whether bringup blocks for connection completion.
86  * @return NSAPI_ERROR_OK on success, or error code
87  */
88  nsapi_error_t bringup(bool dhcp, const char *ip,
89  const char *netmask, const char *gw,
90  nsapi_ip_stack_t stack = DEFAULT_STACK,
91  bool blocking = true
92  ) override;
93 
94  /** Disconnect interface from the network
95  *
96  * After this call the network interface is inactive, to use it again user needs to call @a mbed_ipstack_bringup again.
97  *
98  * @return NSAPI_ERROR_OK on success, or error code
99  */
100  nsapi_error_t bringdown() override;
101 
102  /** Register callback for status reporting
103  *
104  * The specified status callback function will be called on status changes
105  * on the network. The parameters on the callback are the event type and
106  * event-type dependent reason parameter.
107  *
108  * @param status_cb The callback for status changes
109  */
110  void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
111 
112  /** Get the connection status
113  *
114  * @return The connection status according to ConnectionStatusType
115  */
116  nsapi_connection_status_t get_connection_status() const override;
117 
118  /** Return netif interface name
119  *
120  * @return netif name eg "en0"
121  */
122  char *get_interface_name(char *buf) override;
123 
124  /** Return MAC address of the network interface
125  *
126  * @return MAC address as "V:W:X:Y:Z"
127  */
128  char *get_mac_address(char *buf, nsapi_size_t buflen) override;
129 
130  /** @copydoc OnboardNetworkStack::Interface::get_ip_address */
131  nsapi_error_t get_ip_address(SocketAddress *address) override;
132 
133  /** Get the IPv6 link local address in SocketAddress representation
134  *
135  * @address SocketAddress representation of the link local IPv6 address
136  * @return NSAPI_ERROR_OK on success, or error code
137  */
139 
140  /** Copies netmask of the network interface to user supplied buffer
141  *
142  * @param buf buffer to which netmask will be copied as "W:X:Y:Z"
143  * @param buflen size of supplied buffer
144  * @return Pointer to a buffer, or NULL if the buffer is too small
145  */
146  nsapi_error_t get_netmask(SocketAddress *address) override;
147 
148  /** Copies gateway address of the network interface to user supplied buffer
149  *
150  * @param buf buffer to which gateway address will be copied as "W:X:Y:Z"
151  * @param buflen size of supplied buffer
152  * @return Pointer to a buffer, or NULL if the buffer is too small
153  */
154  nsapi_error_t get_gateway(SocketAddress *address) override;
155 
156  private:
157  friend class LWIP;
158 
159  Interface();
160 
161  nsapi_error_t set_dhcp();
162  static void netif_link_irq(struct netif *netif);
163  static void netif_status_irq(struct netif *netif);
164  static Interface *our_if_from_netif(struct netif *netif);
165  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 
169 
170 #if LWIP_ETHERNET
171  static err_t emac_low_level_output(struct netif *netif, struct pbuf *p);
172  void emac_input(net_stack_mem_buf_t *buf);
173  void emac_state_change(bool up);
174 #if LWIP_IGMP
175  static err_t emac_igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action);
176 #endif
177 #if LWIP_IPV6_MLD
178  static err_t emac_mld_mac_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action);
179 #endif
180 
181  static err_t emac_if_init(struct netif *netif);
182 #endif
183 
184 #if LWIP_L3IP
185 #if LWIP_IPV4
186  static err_t l3ip4_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr);
187 #endif
188 #if LWIP_IPV6
189  static err_t l3ip6_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
190 #endif
191  void l3ip_input(net_stack_mem_buf_t *buf);
192  void l3ip_state_change(bool up);
193 #if LWIP_IGMP
194  static err_t l3ip_multicast_ipv4_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action);
195 #endif
196 #if LWIP_IPV6_MLD
197  static err_t l3ip_multicast_ipv6_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action);
198 #endif
199 
200  static err_t l3ip_if_init(struct netif *netif);
201 #endif
202 
203 #if PPP_SUPPORT
204 #if PPP_IPV4_SUPPORT && LWIP_IPV4
205  static err_t ppp4_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr);
206 #endif
207 #if PPP_IPV6_SUPPORT && LWIP_IPV6
208  static err_t ppp6_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
209 #endif
210  void ppp_input(net_stack_mem_buf_t *buf);
211  void ppp_state_change(bool up);
212  static err_t ppp_if_init(struct netif *netif);
213 #endif
214 
215  union {
216 #if LWIP_ETHERNET
217  EMAC *emac; /**< HW specific emac implementation */
218 #endif
219 #if LWIP_L3IP
220  L3IP *l3ip; /**< L3IP implementation */
221 #endif
222 #if PPP_SUPPORT
223  PPP *ppp; /**< PPP implementation */
224 #endif
225  void *hw; /**< alternative implementation pointer - used for PPP */
226  };
227 
228  NetworkInterface *user_network_interface;
229 
230  mbed_rtos_storage_semaphore_t remove_interface_sem;
231  osSemaphoreId_t remove_interface;
232  mbed_rtos_storage_semaphore_t linked_sem;
233  osSemaphoreId_t linked;
234  mbed_rtos_storage_semaphore_t unlinked_sem;
235  osSemaphoreId_t unlinked;
236  mbed_rtos_storage_semaphore_t has_any_addr_sem;
237  osSemaphoreId_t has_any_addr;
238 #define HAS_ANY_ADDR 1
239 #if PREF_ADDR_TIMEOUT
240  mbed_rtos_storage_semaphore_t has_pref_addr_sem;
241  osSemaphoreId_t has_pref_addr;
242 #define HAS_PREF_ADDR 2
243 #endif
244 #if BOTH_ADDR_TIMEOUT
245  mbed_rtos_storage_semaphore_t has_both_addr_sem;
246  osSemaphoreId_t has_both_addr;
247 #define HAS_BOTH_ADDR 4
248 #endif
249  char _interface_name[NSAPI_INTERFACE_NAME_MAX_SIZE];
250  char has_addr_state;
251  nsapi_connection_status_t connected;
252  bool dhcp_started;
253  bool dhcp_has_to_be_set;
254  bool blocking;
255  bool ppp_enabled;
257  struct netif netif;
258  static Interface *list;
259  Interface *next;
261  };
262 
263  /** Register a network interface with the IP stack
264  *
265  * Connects EMAC layer with the IP stack and initializes all the required infrastructure.
266  * This function should be called only once for each available interface.
267  *
268  * @param emac EMAC HAL implementation for this network interface
269  * @param default_if true if the interface should be treated as the default one
270  * @param[out] interface_out pointer to stack interface object controlling the EMAC
271  * @return NSAPI_ERROR_OK on success, or error code
272  */
273  nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out, NetworkInterface *user_network_interface = NULL) override;
274 
275  /** Register a network interface with the IP stack
276  *
277  * Connects L3IP layer with the IP stack and initializes all the required infrastructure.
278  * This function should be called only once for each available interface.
279  *
280  * @param l3ip L3IP HAL implementation for this network interface
281  * @param default_if true if the interface should be treated as the default one
282  * @param[out] interface_out pointer to stack interface object controlling the L3IP
283  * @return NSAPI_ERROR_OK on success, or error code
284  */
285  nsapi_error_t add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetworkStack::Interface **interface_out) override;
286 
287  /** Register a PPP interface with the IP stack
288  *
289  * Connects PPP layer with the IP stack and initializes all the required infrastructure.
290  * This function should be called only once for each available interface.
291  *
292  * This is an internal function that links ppp_lwip.cpp to mbed_ipstack_lwip.cpp,
293  * once a driver starts it via the nsapi_ppp.h API.
294  *
295  * Ultimately the nsapi_ppp.h API will be deprecated, and there will be a
296  * mbed_ipstack_add_ppp_interface() replacing nsapi_ppp_connect().
297  *
298  * @param pcb PPP implementation specific user data; will be passed to PPP callbacks
299  * @param default_if true if the interface should be treated as the default one
300  * @param stack Allow manual selection of IPv4 and/or IPv6
301  * @param[out] interface_out set to interface handle that must be passed to subsequent mbed_stack calls
302  * @return NSAPI_ERROR_OK on success, or error code
303  */
304  nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out) override;
305 
306  /** Remove a network interface from IP stack
307  *
308  * Removes layer 3 IP objects,network interface from stack list .
309  * @param[out] interface_out pointer to stack interface object controlling the EMAC
310  * @return NSAPI_ERROR_OK on success, or error code
311  */
313 
314  /** Remove a network interface from IP stack
315  *
316  * Removes PPP objects,network interface from stack list, and shutdown device driver.
317  * @param[out] interface_out pointer to stack interface object controlling the PPP
318  * @return NSAPI_ERROR_OK on success, or error code
319  */
321 
322  /** Remove a network interface from IP stack
323  *
324  * Removes PPP objects,network interface from stack list, and shutdown device driver.
325  * @param[out] interface_out pointer to stack interface object controlling the PPP
326  * @return NSAPI_ERROR_OK on success, or error code
327  */
329 
330  /** Get a domain name server from a list of servers to query
331  *
332  * Returns a DNS server address for a index. If returns error no more
333  * DNS servers to read.
334  *
335  * @param index Index of the DNS server, starts from zero
336  * @param address Destination for the host address
337  * @return 0 on success, negative error code on failure
338  */
339  nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name) override;
340 
341  /** Add a domain name server to list of servers to query
342  *
343  * @param address Destination for the host address
344  * @param interface_name Network interface name
345  * @return NSAPI_ERROR_OK on success, negative error code on failure
346  */
347  nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name) override;
348 
349  /** @copydoc NetworkStack::get_ip_address */
350  nsapi_error_t get_ip_address(SocketAddress *address) override;
351 
352  /** @copydoc NetworkStack::get_ip_address_if */
353  nsapi_error_t get_ip_address_if(SocketAddress *address, const char *interface_name) override;
354 
355  /** Set the network interface as default one
356  */
357  void set_default_interface(OnboardNetworkStack::Interface *interface) override;
358 
359 protected:
360  LWIP();
361 
362  /** Opens a socket
363  *
364  * Creates a network socket and stores it in the specified handle.
365  * The handle must be passed to following calls on the socket.
366  *
367  * A stack may have a finite number of sockets, in this case
368  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
369  *
370  * @param handle Destination for the handle to a newly created socket
371  * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
372  * @return 0 on success, negative error code on failure
373  */
374  nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) override;
375 
376  /** Close the socket
377  *
378  * Closes any open connection and deallocates any memory associated
379  * with the socket.
380  *
381  * @param handle Socket handle
382  * @return 0 on success, negative error code on failure
383  */
384  nsapi_error_t socket_close(nsapi_socket_t handle) override;
385 
386  /** Bind a specific address to a socket
387  *
388  * Binding a socket specifies the address and port on which to recieve
389  * data. If the IP address is zeroed, only the port is bound.
390  *
391  * @param handle Socket handle
392  * @param address Local address to bind
393  * @return 0 on success, negative error code on failure.
394  */
395  nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) override;
396 
397  /** Listen for connections on a TCP socket
398  *
399  * Marks the socket as a passive socket that can be used to accept
400  * incoming connections.
401  *
402  * @param handle Socket handle
403  * @param backlog Number of pending connections that can be queued
404  * simultaneously
405  * @return 0 on success, negative error code on failure
406  */
407  nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) override;
408 
409  /** Connects TCP socket to a remote host
410  *
411  * Initiates a connection to a remote server specified by the
412  * indicated address.
413  *
414  * @param handle Socket handle
415  * @param address The SocketAddress of the remote host
416  * @return 0 on success, negative error code on failure
417  */
418  nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) override;
419 
420  /** Accepts a connection on a TCP socket
421  *
422  * The server socket must be bound and set to listen for connections.
423  * On a new connection, creates a network socket and stores it in the
424  * specified handle. The handle must be passed to following calls on
425  * the socket.
426  *
427  * A stack may have a finite number of sockets, in this case
428  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
429  *
430  * This call is non-blocking. If accept would block,
431  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
432  *
433  * @param server Socket handle to server to accept from
434  * @param handle Destination for a handle to the newly created socket
435  * @param address Destination for the remote address or NULL
436  * @return 0 on success, negative error code on failure
437  */
439  nsapi_socket_t *handle, SocketAddress *address = 0) override;
440 
441  /** Send data over a TCP socket
442  *
443  * The socket must be connected to a remote host. Returns the number of
444  * bytes sent from the buffer.
445  *
446  * This call is non-blocking. If send would block,
447  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
448  *
449  * @param handle Socket handle
450  * @param data Buffer of data to send to the host
451  * @param size Size of the buffer in bytes
452  * @return Number of sent bytes on success, negative error
453  * code on failure
454  */
456  const void *data, nsapi_size_t size) override;
457 
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  */
476  const void *data, nsapi_size_t size,
477  nsapi_msghdr_t *control, nsapi_size_t control_size) override;
478 
479  /** Receive data over a TCP socket
480  *
481  * The socket must be connected to a remote host. Returns the number of
482  * bytes received into the buffer.
483  *
484  * This call is non-blocking. If recv would block,
485  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
486  *
487  * @param handle Socket handle
488  * @param data Destination buffer for data received from the host
489  * @param size Size of the buffer in bytes
490  * @return Number of received bytes on success, negative error
491  * code on failure
492  */
494  void *data, nsapi_size_t size) override;
495 
496  /** Send a packet over a UDP socket
497  *
498  * Sends data to the specified address. Returns the number of bytes
499  * sent from the buffer.
500  *
501  * This call is non-blocking. If sendto would block,
502  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
503  *
504  * @param handle Socket handle
505  * @param address The SocketAddress of the remote host
506  * @param data Buffer of data to send to the host
507  * @param size Size of the buffer in bytes
508  * @return Number of sent bytes on success, negative error
509  * code on failure
510  */
512  const void *data, nsapi_size_t size) override;
513 
514  /** Receive a packet over a UDP socket
515  *
516  * Receives data and stores the source address in address if address
517  * is not NULL. Returns the number of bytes received into the buffer.
518  *
519  * This call is non-blocking. If recvfrom would block,
520  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
521  *
522  * It uses socket_recvfrom_control with zero ancillary data.
523  * @param handle Socket handle
524  * @param address Destination for the source address or NULL
525  * @param buffer Destination buffer for data received from the host
526  * @param size Size of the buffer in bytes
527  * @return Number of received bytes on success, negative error
528  * code on failure
529  */
531  void *buffer, nsapi_size_t size) override;
532 
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  */
551  void *data, nsapi_size_t size,
552  nsapi_msghdr_t *control, nsapi_size_t control_size) override;
553 
554  /** Register a callback on state change of the socket
555  *
556  * The specified callback will be called on state changes such as when
557  * the socket can recv/send/accept successfully and on when an error
558  * occurs. The callback may also be called spuriously without reason.
559  *
560  * The callback may be called in an interrupt context and should not
561  * perform expensive operations such as recv/send calls.
562  *
563  * @param handle Socket handle
564  * @param callback Function to call on state change
565  * @param data Argument to pass to callback
566  */
567  void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) override;
568 
569  /* Set stack-specific socket options
570  *
571  * The setsockopt allow an application to pass stack-specific hints
572  * to the underlying stack. For unsupported options,
573  * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
574  *
575  * @param handle Socket handle
576  * @param level Stack-specific protocol level
577  * @param optname Stack-specific option identifier
578  * @param optval Option value
579  * @param optlen Length of the option value
580  * @return 0 on success, negative error code on failure
581  */
582  nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
583  int optname, const void *optval, unsigned optlen) override;
584 
585  /* Get stack-specific socket options
586  *
587  * The getstackopt allow an application to retrieve stack-specific hints
588  * from the underlying stack. For unsupported options,
589  * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
590  *
591  * @param handle Socket handle
592  * @param level Stack-specific protocol level
593  * @param optname Stack-specific option identifier
594  * @param optval Destination for option value
595  * @param optlen Length of the option value
596  * @return 0 on success, negative error code on failure
597  */
598  nsapi_error_t getsockopt(nsapi_socket_t handle, int level,
599  int optname, void *optval, unsigned *optlen) override;
600 private:
601 
602  /** Call in callback
603  *
604  * Callback is used to call the call in method of the network stack.
605  */
607 
608  /** Get a call in callback
609  *
610  * Get a call in callback from the network stack context.
611  *
612  * Callback should not take more than 10ms to execute, otherwise it might
613  * prevent underlying thread processing. A portable user of the callback
614  * should not make calls to network operations due to stack size limitations.
615  * The callback should not perform expensive operations such as socket recv/send
616  * calls or blocking operations.
617  *
618  * @return Call in callback
619  */
620  call_in_callback_cb_t get_call_in_callback() override;
621 
622  /** Call a callback after a delay
623  *
624  * Call a callback from the network stack context after a delay. If function
625  * returns error callback will not be called.
626  *
627  * @param delay Delay in milliseconds
628  * @param func Callback to be called
629  * @return 0 on success, negative error code on failure
630  */
631  nsapi_error_t call_in(int delay, mbed::Callback<void()> func) override;
632 
633  struct mbed_lwip_socket {
634  bool in_use;
635 
636  struct netconn *conn;
637  struct pbuf *buf;
638  u16_t offset;
639 
640  void (*cb)(void *);
641  void *data;
642 
643  // Track multicast addresses subscribed to by this socket
644  nsapi_ip_mreq_t *multicast_memberships;
645  uint32_t multicast_memberships_count;
646  uint32_t multicast_memberships_registry;
647  };
648 
649  struct lwip_callback {
650  unsigned int delay;
652  };
653 
654  static nsapi_error_t err_remap(err_t err);
655  static bool is_local_addr(const ip_addr_t *ip_addr);
656  static const ip_addr_t *get_ip_addr(bool any_addr, const struct netif *netif);
657  static const ip_addr_t *get_ipv4_addr(const struct netif *netif);
658  static const ip_addr_t *get_ipv6_addr(const struct netif *netif);
659  static const ip_addr_t *get_ipv6_link_local_addr(const struct netif *netif);
660 
661  static void add_dns_addr(struct netif *lwip_netif, const char *interface_name);
662 
663  /* Static arena of sockets */
664  struct mbed_lwip_socket arena[MEMP_NUM_NETCONN];
665  void arena_init(void);
666  struct mbed_lwip_socket *arena_alloc();
667  void arena_dealloc(struct mbed_lwip_socket *s);
668 
669  static uint32_t next_registered_multicast_member(const struct mbed_lwip_socket *s, uint32_t index)
670  {
671  while (!(s->multicast_memberships_registry & (0x0001 << index))) {
672  index++;
673  }
674  return index;
675  }
676 
677  static uint32_t next_free_multicast_member(const struct mbed_lwip_socket *s, uint32_t index)
678  {
679  while ((s->multicast_memberships_registry & (0x0001 << index))) {
680  index++;
681  }
682  return index;
683  }
684 
685  static void set_multicast_member_registry_bit(struct mbed_lwip_socket *s, uint32_t index)
686  {
687  s->multicast_memberships_registry |= (0x0001 << index);
688  }
689 
690  static void clear_multicast_member_registry_bit(struct mbed_lwip_socket *s, uint32_t index)
691  {
692  s->multicast_memberships_registry &= ~(0x0001 << index);
693  }
694  static int32_t find_multicast_member(const struct mbed_lwip_socket *s, const nsapi_ip_mreq_t *imr);
695 
696  static void socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t len);
697 
698  static void tcpip_init_irq(void *handle);
699  static void tcpip_thread_callback(void *ptr);
700 
701  char ip_address[40];
702  Interface *default_interface;
703  LWIPMemoryManager memory_manager;
704  osThreadId tcpip_thread_id;
705  rtos::Mutex adaptation;
706  rtos::EventFlags _event_flag;
707  static const int TCP_CLOSED_FLAG = 0x4u;
708 };
709 
710 #endif /* LWIPSTACK_H_ */
nsapi_msghdr
Definition: nsapi_types.h:414
char * get_interface_name(char *buf) override
Return netif interface name.
Functions to sync with TCPIP thread.
void socket_attach(nsapi_socket_t handle, void(*callback)(void *), void *data) override
Register a callback on state change of the socket.
netconn API (to be used from non-TCPIP threads)
nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name) override
Get a domain name server from a list of servers to query.
nsapi_size_or_error_t socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Send a packet with ancillary data over a UDP socket.
nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen) override
Get stack-specific socket options.
The EventFlags class is used to control event flags or wait for event flags other threads control...
Definition: EventFlags.h:53
void * memory_manager
Pointer to memory manager.
Representation of a stack&#39;s view of an interface.
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:254
void set_default_interface(OnboardNetworkStack::Interface *interface) override
Set the network interface as default one.
struct netif * next
pointer to next in linked list
Definition: netif.h:264
nsapi_error_t get_gateway(SocketAddress *address) override
Copies gateway address of the network interface to user supplied buffer.
IP API.
nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out) override
Register a PPP interface with the IP stack.
nsapi_error_t get_ipv6_link_local_address(SocketAddress *address) override
Get the IPv6 link local address in SocketAddress representation.
nsapi_error_t get_ip_address_if(SocketAddress *address, const char *interface_name) override
Get the local IP address on interface name.
virtual nsapi_error_t get_ip_address(SocketAddress *address)
Get the local IP address.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
IPv6 addresses.
#define NSAPI_INTERFACE_NAME_MAX_SIZE
Maximum size of network interface name.
Definition: nsapi_types.h:189
nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name) override
Add a domain name server to list of servers to query.
nsapi_error_t bringup(bool dhcp, const char *ip, const char *netmask, const char *gw, nsapi_ip_stack_t stack=DEFAULT_STACK, bool blocking=true) override
Connect the interface to the network.
nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) override
Opens a socket.
Ethernet output for IPv6.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
void * hw
alternative implementation pointer - used for PPP
Definition: LWIPStack.h:225
nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address=0) override
Accepts a connection on a TCP socket.
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition: Callback.h:678
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:153
nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *buffer, nsapi_size_t size) override
Receive a packet over a UDP socket.
mbed OS API for onboard IP stack abstraction
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) override
Register callback for status reporting.
nsapi_error_t bringdown() override
Disconnect interface from the network.
Definition: PPP.h:26
Main packet buffer struct.
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: L3IP.h:31
Generic data structure used for all lwIP network interfaces.
SocketAddress class.
Definition: SocketAddress.h:37
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
Common interface that is shared between network devices.
nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, void *data, nsapi_size_t size) override
Receive data over a TCP socket.
#define MEMP_NUM_NETCONN
MEMP_NUM_NETCONN: the number of struct netconns.
Definition: opt.h:532
nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) override
Listen for connections on a TCP socket.
nsapi_error_t remove_ppp_interface(OnboardNetworkStack::Interface **interface_out) override
Remove a network interface from IP stack.
nsapi_connection_status_t get_connection_status() const override
Get the connection status.
nsapi_error_t get_netmask(SocketAddress *address) override
Copies netmask of the network interface to user supplied buffer.
nsapi_error_t remove_l3ip_interface(OnboardNetworkStack::Interface **interface_out) override
Remove a network interface from IP stack.
nsapi_error_t get_ip_address(SocketAddress *address) override
Get the local IP address.
char * get_mac_address(char *buf, nsapi_size_t buflen) override
Return MAC address of the network interface.
Definition: LWIPStack.h:41
Connected isochronous stream linked list.
Definition: lctr_int_cis.h:306
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:33
nsapi_error_t setsockopt(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen) override
Set stack-specific socket options.
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:237
nsapi_size_or_error_t socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Receive a packet with ancillary data over a UDP socket.
netif_mac_filter_action
MAC Filter Actions, these are passed to a netif&#39;s igmp_mac_filter or mld_mac_filter callback function...
Definition: netif.h:157
nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) override
Connects TCP socket to a remote host.
nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size) override
Send a packet over a UDP socket.
Callback class based on template specialization.
Definition: Callback.h:53
TCP API (to be used from TCPIP thread) See also tcp_raw.
nsapi_error_t socket_close(nsapi_socket_t handle) override
Close the socket.
nsapi_ip_mreq structure
Definition: nsapi_types.h:390
nsapi_error_t add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetworkStack::Interface **interface_out) override
Register a network interface with the IP stack.
nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out, NetworkInterface *user_network_interface=NULL) override
Register a network interface with the IP stack.
nsapi_error_t remove_ethernet_interface(OnboardNetworkStack::Interface **interface_out) override
Remove a network interface from IP stack.
nsapi_error_t set_ip_address(const char *ip, const char *netmask, const char *gw, uint8_t ipv6_flag=0) override
Set IP address to LWIP stack.
nsapi_size_or_error_t socket_send(nsapi_socket_t handle, const void *data, nsapi_size_t size) override
Send data over a TCP socket.
nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) override
Bind a specific address to a socket.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.