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  virtual nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out);
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  virtual nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out);
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);
54 
55 protected:
56 
57  Nanostack();
58 
59  /** Get the local IP address
60  *
61  * @return Null-terminated representation of the local IP address
62  * or null if not yet connected
63  */
64  virtual const char *get_ip_address();
65 
66  /** Opens a socket
67  *
68  * Creates a network socket and stores it in the specified handle.
69  * The handle must be passed to following calls on the socket.
70  *
71  * A stack may have a finite number of sockets, in this case
72  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
73  *
74  * @param handle Destination for the handle to a newly created socket
75  * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
76  * @return 0 on success, negative error code on failure
77  */
78  virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto);
79 
80  /** Close the socket
81  *
82  * Closes any open connection and deallocates any memory associated
83  * with the socket.
84  *
85  * @param handle Socket handle
86  * @return 0 on success, negative error code on failure
87  */
88  virtual nsapi_error_t socket_close(void *handle);
89 
90  /** Bind a specific address to a socket
91  *
92  * Binding a socket specifies the address and port on which to recieve
93  * data. If the IP address is zeroed, only the port is bound.
94  *
95  * @param handle Socket handle
96  * @param address Local address to bind
97  * @return 0 on success, negative error code on failure.
98  */
99  virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address);
100 
101  /** Listen for connections on a TCP socket
102  *
103  * Marks the socket as a passive socket that can be used to accept
104  * incoming connections.
105  *
106  * @param handle Socket handle
107  * @param backlog Number of pending connections that can be queued
108  * simultaneously
109  * @return 0 on success, negative error code on failure
110  */
111  virtual nsapi_error_t socket_listen(void *handle, int backlog);
112 
113  /** Connects TCP socket to a remote host
114  *
115  * Initiates a connection to a remote server specified by the
116  * indicated address.
117  *
118  * @param handle Socket handle
119  * @param address The SocketAddress of the remote host
120  * @return 0 on success, negative error code on failure
121  */
122  virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address);
123 
124  /** Accepts a connection on a TCP socket
125  *
126  * The server socket must be bound and set to listen for connections.
127  * On a new connection, creates a network socket and stores it in the
128  * specified handle. The handle must be passed to following calls on
129  * the socket.
130  *
131  * A stack may have a finite number of sockets, in this case
132  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
133  *
134  * This call is non-blocking. If accept would block,
135  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
136  *
137  * @param server Socket handle to server to accept from
138  * @param handle Destination for a handle to the newly created socket
139  * @param address Destination for the remote address or NULL
140  * @return 0 on success, negative error code on failure
141  */
142  virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address);
143 
144  /** Send data over a TCP socket
145  *
146  * The socket must be connected to a remote host. Returns the number of
147  * bytes sent from the buffer.
148  *
149  * This call is non-blocking. If send would block,
150  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
151  *
152  * @param handle Socket handle
153  * @param data Buffer of data to send to the host
154  * @param size Size of the buffer in bytes
155  * @return Number of sent bytes on success, negative error
156  * code on failure
157  */
158  virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size);
159 
160  /** Receive data over a TCP socket
161  *
162  * The socket must be connected to a remote host. Returns the number of
163  * bytes received into the buffer.
164  *
165  * This call is non-blocking. If recv would block,
166  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
167  *
168  * @param handle Socket handle
169  * @param data Destination buffer for data received from the host
170  * @param size Size of the buffer in bytes
171  * @return Number of received bytes on success, negative error
172  * code on failure
173  */
174  virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size);
175 
176  /** Send a packet over a UDP socket
177  *
178  * Sends data to the specified address. Returns the number of bytes
179  * sent from the buffer.
180  *
181  * This call is non-blocking. If sendto would block,
182  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
183  *
184  * @param handle Socket handle
185  * @param address The SocketAddress of the remote host
186  * @param data Buffer of data to send to the host
187  * @param size Size of the buffer in bytes
188  * @return Number of sent bytes on success, negative error
189  * code on failure
190  */
191  virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size);
192 
193  /** Receive a packet over a UDP socket
194  *
195  * Receives data and stores the source address in address if address
196  * is not NULL. Returns the number of bytes received into the buffer.
197  *
198  * This call is non-blocking. If recvfrom would block,
199  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
200  *
201  * @param handle Socket handle
202  * @param address Destination for the source address or NULL
203  * @param buffer Destination buffer for data received from the host
204  * @param size Size of the buffer in bytes
205  * @return Number of received bytes on success, negative error
206  * code on failure
207  */
208  virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size);
209 
210  /** Register a callback on state change of the socket
211  *
212  * The specified callback will be called on state changes such as when
213  * the socket can recv/send/accept successfully and on when an error
214  * occurs. The callback may also be called spuriously without reason.
215  *
216  * The callback may be called in an interrupt context and should not
217  * perform expensive operations such as recv/send calls.
218  *
219  * @param handle Socket handle
220  * @param callback Function to call on state change
221  * @param data Argument to pass to callback
222  */
223  virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
224 
225  /* Set stack-specific socket options
226  *
227  * The setsockopt allow an application to pass stack-specific hints
228  * to the underlying stack. For unsupported options,
229  * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
230  *
231  * @param handle Socket handle
232  * @param level Stack-specific protocol level
233  * @param optname Stack-specific option identifier
234  * @param optval Option value
235  * @param optlen Length of the option value
236  * @return 0 on success, negative error code on failure
237  */
238  virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen);
239 
240  /* Get stack-specific socket options
241  *
242  * The getstackopt allow an application to retrieve stack-specific hints
243  * from the underlying stack. For unsupported options,
244  * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
245  *
246  * @param handle Socket handle
247  * @param level Stack-specific protocol level
248  * @param optname Stack-specific option identifier
249  * @param optval Destination for option value
250  * @param optlen Length of the option value
251  * @return 0 on success, negative error code on failure
252  */
253  virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen);
254 
255 private:
256 
257  /** Call in callback
258  *
259  * Callback is used to call the call in method of the network stack.
260  */
262 
263  /** Get a call in callback
264  *
265  * Get a call in callback from the network stack context.
266  *
267  * Callback should not take more than 10ms to execute, otherwise it might
268  * prevent underlying thread processing. A portable user of the callback
269  * should not make calls to network operations due to stack size limitations.
270  * The callback should not perform expensive operations such as socket recv/send
271  * calls or blocking operations.
272  *
273  * @return Call in callback
274  */
275  virtual call_in_callback_cb_t get_call_in_callback();
276 
277  /** Call a callback after a delay
278  *
279  * Call a callback from the network stack context after a delay. If function
280  * returns error callback will not be called.
281  *
282  * @param delay Delay in milliseconds
283  * @param func Callback to be called
284  * @return 0 on success, negative error code on failure
285  */
286  virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
287 
288  struct nanostack_callback {
290  };
291 
292  nsapi_size_or_error_t do_sendto(void *handle, const struct ns_address *address, const void *data, nsapi_size_t size);
293  static void call_event_tasklet_main(arm_event_s *event);
294  char text_ip_address[40];
295  NanostackMemoryManager memory_manager;
296  int8_t call_event_tasklet;
297 };
298 
299 nsapi_error_t map_mesh_error(mesh_error_t err);
300 
301 #endif /* NANOSTACK_H_ */
virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address)
Bind a specific address to a socket.
virtual void socket_attach(void *handle, void(*callback)(void *), void *data)
Register a callback on state change of the socket.
Wi-SUN mesh network interface class.
virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen)
Set stack-specific socket options.
6LoWPAN-ND mesh network interface class
virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen)
Get stack-specific socket options.
Representation of a stack&#39;s view of an interface.
virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size)
Receive data over a TCP socket.
virtual nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out)
Register a network interface with the IP stack.
virtual nsapi_error_t socket_listen(void *handle, int backlog)
Listen for connections on a TCP socket.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size)
Send a packet over a UDP socket.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:106
Thread mesh network interface class.
mbed OS API for onboard IP stack abstraction
virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address)
Accepts a connection on a TCP socket.
Definition: PPP.h:26
SocketAddress class.
Definition: SocketAddress.h:35
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:709
virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address)
Connects TCP socket to a remote host.
virtual nsapi_error_t socket_close(void *handle)
Close the socket.
virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto)
Opens a socket.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:99
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:32
virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size)
Send data over a TCP socket.
Callback class based on template specialization.
Definition: Callback.h:39
virtual const char * get_ip_address()
Get the local IP address.
virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size)
Receive a packet over a UDP 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.