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