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