Mistake on this page?
Report an issue in GitHub or email us
Nanostack.h
1 /*
2  * Copyright (c) 2016-2017, Arm Limited and affiliates.
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 NANOSTACK_H_
19 #define NANOSTACK_H_
20 
21 #include "OnboardNetworkStack.h"
22 #include "NanostackMemoryManager.h"
23 #include "MeshInterface.h"
24 #include "mesh_interface_types.h"
25 #include "eventOS_event.h"
26 
27 struct ns_address;
28 
29 class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostack> {
30 public:
31  static Nanostack &get_instance();
32 
33  // Our Nanostack::Interface etc are defined by mbed_mesh_api
34  class Interface;
35  class EthernetInterface;
36  class MeshInterface;
37  class LoWPANNDInterface;
38  class ThreadInterface;
39  class WisunInterface;
40  class PPPInterface;
41 
42  /* Implement OnboardNetworkStack method */
43  nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out) override;
44 
45  /* Local variant with stronger typing and manual address specification */
46  nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, Nanostack::EthernetInterface **interface_out, const uint8_t *mac_addr = NULL);
47 
48  nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out) override;
49 
50  /* Local variant with stronger typing and manual address specification */
51  nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, Nanostack::PPPInterface **interface_out);
52 
53  nsapi_error_t remove_ppp_interface(OnboardNetworkStack::Interface **interface_out) override;
54 
55 protected:
56 
57  Nanostack();
58 
59  /** @copydoc NetworkStack::get_ip_address */
60  nsapi_error_t get_ip_address(SocketAddress *sockAddr) override;
61 
62  /** Translate a hostname to an IP address with specific version using network interface name.
63  *
64  * The hostname may be either a domain name or an IP address. If the
65  * hostname is an IP address, no network transactions will be performed.
66  *
67  * Method first checks Nanostack DNS query result cache. If match is found, then the result is returned immediately.
68  * Otherwise method calls DNS resolver to find a match.
69  *
70  * @param host Hostname to resolve.
71  * @param address Pointer to a SocketAddress to store the result.
72  * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
73  * version is chosen by the stack (defaults to NSAPI_UNSPEC).
74  * @param interface_name Network interface name
75  * @return NSAPI_ERROR_OK on success, negative error code on failure.
76  */
77  virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name);
78 
79  /** Translate a hostname to an IP address (asynchronous) using network interface name.
80  *
81  * The hostname may be either a domain name or a dotted IP address. If the
82  * hostname is an IP address, no network transactions will be performed.
83  *
84  * Method first checks Nanostack DNS query result cache. If match is found, then the result is returned immediately.
85  *
86  * Call is non-blocking. Result of the DNS operation is returned by the callback.
87  * If this function returns failure, callback will not be called. In case result
88  * is success (IP address was found from DNS cache), callback will be called
89  * before function returns.
90  *
91  * @param host Hostname to resolve.
92  * @param callback Callback that is called for result.
93  * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
94  * version is chosen by the stack (defaults to NSAPI_UNSPEC).
95  * @param interface_name Network interface name
96  * @return 0 on immediate success,
97  * negative error code on immediate failure or
98  * a positive unique id that represents the hostname translation operation
99  * and can be passed to cancel.
100  */
101  virtual nsapi_value_or_error_t gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name);
102 
103  /** Get a domain name server from a list of servers to query
104  *
105  * Returns a DNS server address for a index. DNS servers are queried from Nanostack DNS cache.
106  * If returns error no more DNS servers to read.
107  *
108  * @param index Index of the DNS server, starts from zero
109  * @param address Destination for the host address
110  * @return 0 on success, negative error code on failure
111  */
112  virtual nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name);
113 
114  /** Opens a socket
115  *
116  * Creates a network socket and stores it in the specified handle.
117  * The handle must be passed to following calls on the socket.
118  *
119  * A stack may have a finite number of sockets, in this case
120  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
121  *
122  * @param handle Destination for the handle to a newly created socket
123  * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
124  * @return 0 on success, negative error code on failure
125  */
126  nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto) override;
127 
128  /** Close the socket
129  *
130  * Closes any open connection and deallocates any memory associated
131  * with the socket.
132  *
133  * @param handle Socket handle
134  * @return 0 on success, negative error code on failure
135  */
136  nsapi_error_t socket_close(void *handle) override;
137 
138  /** Bind a specific address to a socket
139  *
140  * Binding a socket specifies the address and port on which to recieve
141  * data. If the IP address is zeroed, only the port is bound.
142  *
143  * @param handle Socket handle
144  * @param address Local address to bind
145  * @return 0 on success, negative error code on failure.
146  */
147  nsapi_error_t socket_bind(void *handle, const SocketAddress &address) override;
148 
149  /** Listen for connections on a TCP socket
150  *
151  * Marks the socket as a passive socket that can be used to accept
152  * incoming connections.
153  *
154  * @param handle Socket handle
155  * @param backlog Number of pending connections that can be queued
156  * simultaneously
157  * @return 0 on success, negative error code on failure
158  */
159  nsapi_error_t socket_listen(void *handle, int backlog) override;
160 
161  /** Connects TCP socket to a remote host
162  *
163  * Initiates a connection to a remote server specified by the
164  * indicated address.
165  *
166  * @param handle Socket handle
167  * @param address The SocketAddress of the remote host
168  * @return 0 on success, negative error code on failure
169  */
170  nsapi_error_t socket_connect(void *handle, const SocketAddress &address) override;
171 
172  /** Accepts a connection on a TCP socket
173  *
174  * The server socket must be bound and set to listen for connections.
175  * On a new connection, creates a network socket and stores it in the
176  * specified handle. The handle must be passed to following calls on
177  * the socket.
178  *
179  * A stack may have a finite number of sockets, in this case
180  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
181  *
182  * This call is non-blocking. If accept would block,
183  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
184  *
185  * @param server Socket handle to server to accept from
186  * @param handle Destination for a handle to the newly created socket
187  * @param address Destination for the remote address or NULL
188  * @return 0 on success, negative error code on failure
189  */
190  nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address) override;
191 
192  /** Send data over a TCP socket
193  *
194  * The socket must be connected to a remote host. Returns the number of
195  * bytes sent from the buffer.
196  *
197  * This call is non-blocking. If send would block,
198  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
199  *
200  * @param handle Socket handle
201  * @param data Buffer of data to send to the host
202  * @param size Size of the buffer in bytes
203  * @return Number of sent bytes on success, negative error
204  * code on failure
205  */
206  nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size) override;
207 
208  /** Receive data over a TCP socket
209  *
210  * The socket must be connected to a remote host. Returns the number of
211  * bytes received into the buffer.
212  *
213  * This call is non-blocking. If recv would block,
214  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
215  *
216  * @param handle Socket handle
217  * @param data Destination buffer for data received from the host
218  * @param size Size of the buffer in bytes
219  * @return Number of received bytes on success, negative error
220  * code on failure
221  */
222  nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size) override;
223 
224  /** Send a packet over a UDP socket
225  *
226  * Sends data to the specified address. Returns the number of bytes
227  * sent from the buffer.
228  *
229  * This call is non-blocking. If sendto would block,
230  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
231  *
232  * @param handle Socket handle
233  * @param address The SocketAddress of the remote host
234  * @param data Buffer of data to send to the host
235  * @param size Size of the buffer in bytes
236  * @return Number of sent bytes on success, negative error
237  * code on failure
238  */
239  nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size) override;
240 
241  /** Receive a packet over a UDP socket
242  *
243  * Receives data and stores the source address in address if address
244  * is not NULL. Returns the number of bytes received into the buffer.
245  *
246  * This call is non-blocking. If recvfrom would block,
247  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
248  *
249  * @param handle Socket handle
250  * @param address Destination for the source address or NULL
251  * @param buffer Destination buffer for data received from the host
252  * @param size Size of the buffer in bytes
253  * @return Number of received bytes on success, negative error
254  * code on failure
255  */
256  nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size) override;
257 
258  /** Register a callback on state change of the socket
259  *
260  * The specified callback will be called on state changes such as when
261  * the socket can recv/send/accept successfully and on when an error
262  * occurs. The callback may also be called spuriously without reason.
263  *
264  * The callback may be called in an interrupt context and should not
265  * perform expensive operations such as recv/send calls.
266  *
267  * @param handle Socket handle
268  * @param callback Function to call on state change
269  * @param data Argument to pass to callback
270  */
271  void socket_attach(void *handle, void (*callback)(void *), void *data) override;
272 
273  /* Set stack-specific socket options
274  *
275  * The setsockopt allow an application to pass stack-specific hints
276  * to the underlying stack. For unsupported options,
277  * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
278  *
279  * @param handle Socket handle
280  * @param level Stack-specific protocol level
281  * @param optname Stack-specific option identifier
282  * @param optval Option value
283  * @param optlen Length of the option value
284  * @return 0 on success, negative error code on failure
285  */
286  nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) override;
287 
288  /* Get stack-specific socket options
289  *
290  * The getstackopt allow an application to retrieve stack-specific hints
291  * from the underlying stack. For unsupported options,
292  * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
293  *
294  * @param handle Socket handle
295  * @param level Stack-specific protocol level
296  * @param optname Stack-specific option identifier
297  * @param optval Destination for option value
298  * @param optlen Length of the option value
299  * @return 0 on success, negative error code on failure
300  */
301  nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) override;
302 
303 private:
304 
305  /** Call in callback
306  *
307  * Callback is used to call the call in method of the network stack.
308  */
310 
311  /** Get a call in callback
312  *
313  * Get a call in callback from the network stack context.
314  *
315  * Callback should not take more than 10ms to execute, otherwise it might
316  * prevent underlying thread processing. A portable user of the callback
317  * should not make calls to network operations due to stack size limitations.
318  * The callback should not perform expensive operations such as socket recv/send
319  * calls or blocking operations.
320  *
321  * @return Call in callback
322  */
323  call_in_callback_cb_t get_call_in_callback() override;
324 
325  /** Call a callback after a delay
326  *
327  * Call a callback from the network stack context after a delay. If function
328  * returns error callback will not be called.
329  *
330  * @param delay Delay in milliseconds
331  * @param func Callback to be called
332  * @return 0 on success, negative error code on failure
333  */
334  nsapi_error_t call_in(int delay, mbed::Callback<void()> func) override;
335 
336  struct nanostack_callback {
338  };
339 
340  nsapi_size_or_error_t do_sendto(void *handle, const struct ns_address *address, const void *data, nsapi_size_t size);
341  static void call_event_tasklet_main(arm_event_s *event);
342  char text_ip_address[40];
343  NanostackMemoryManager memory_manager;
344  int8_t call_event_tasklet;
345 };
346 
347 nsapi_error_t map_mesh_error(mesh_error_t err);
348 
349 #endif /* NANOSTACK_H_ */
nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size) override
Receive a packet over a UDP socket.
Wi-SUN mesh network interface class.
6LoWPAN-ND mesh network interface class
nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) override
Set stack-specific socket options.
nsapi_error_t socket_bind(void *handle, const SocketAddress &address) override
Bind a specific address to a socket.
Representation of a stack&#39;s view of an interface.
virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name)
Translate a hostname to an IP address with specific version using network interface name...
virtual nsapi_value_or_error_t gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
Translate a hostname to an IP address (asynchronous) using network interface name.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:140
nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) override
Get stack-specific socket options.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
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:151
nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size) override
Receive data over a TCP socket.
nsapi_error_t socket_listen(void *handle, int backlog) override
Listen for connections on a TCP socket.
Thread mesh network interface class.
mbed OS API for onboard IP stack abstraction
signed int nsapi_value_or_error_t
Type used to represent either a value or error.
Definition: nsapi_types.h:158
Definition: PPP.h:26
nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto) override
Opens a socket.
SocketAddress class.
Definition: SocketAddress.h:37
nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size) override
Send data over a TCP socket.
nsapi_error_t get_ip_address(SocketAddress *sockAddr) override
Get the local IP address.
nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size) override
Send a packet over a UDP socket.
nsapi_error_t socket_close(void *handle) override
Close the socket.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:144
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:33
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name)
Get a domain name server from a list of servers to query.
void socket_attach(void *handle, void(*callback)(void *), void *data) override
Register a callback on state change of the socket.
nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address) override
Accepts a connection on a TCP socket.
Callback class based on template specialization.
Definition: Callback.h:53
nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out) override
Register a network interface with the IP stack.
nsapi_error_t socket_connect(void *handle, const SocketAddress &address) override
Connects TCP socket to a remote host.
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.