Mistake on this page?
Report an issue in GitHub or email us
nsapi_types.h
1 
2 /** \addtogroup netsocket */
3 /** @{*/
4 /* nsapi.h - The network socket API
5  * Copyright (c) 2015 ARM Limited
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef NSAPI_TYPES_H
22 #define NSAPI_TYPES_H
23 
24 #include <stdint.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 
31 /*
32  * The Type of Service provides an indication of the abstract
33  * parameters of the quality of service desired. These parameters are
34  * to be used to guide the selection of the actual service parameters
35  * when transmitting a datagram through a particular network. Several
36  * networks offer service precedence, which somehow treats high
37  * precedence traffic as more important than other traffic (generally
38  * by accepting only traffic above a certain precedence at time of high
39  * load). The major choice is a three way tradeoff between low-delay,
40  * high-reliability, and high-throughput.
41  * The use of the Delay, Throughput, and Reliability indications may
42  * increase the cost (in some sense) of the service. In many networks
43  * better performance for one of these parameters is coupled with worse
44  * performance on another. Except for very unusual cases at most two
45  * of these three indications should be set.
46  */
47 #define NSAPI_IPTOS_TOS_MASK 0x1E
48 #define NSAPI_IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
49 #define NSAPI_IPTOS_LOWDELAY 0x10
50 #define NSAPI_IPTOS_THROUGHPUT 0x08
51 #define NSAPI_IPTOS_RELIABILITY 0x04
52 #define NSAPI_IPTOS_LOWCOST 0x02
53 #define NSAPI_IPTOS_MINCOST IPTOS_LOWCOST
54 
55 /*
56  * The Network Control precedence designation is intended to be used
57  * within a network only. The actual use and control of that
58  * designation is up to each network. The Internetwork Control
59  * designation is intended for use by gateway control originators only.
60  * If the actual use of these precedence designations is of concern to
61  * a particular network, it is the responsibility of that network to
62  * control the access to, and use of, those precedence designations.
63  */
64 #define NSAPI_IPTOS_PREC_MASK 0xe0
65 #define NSAPI_IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
66 #define NSAPI_IPTOS_PREC_NETCONTROL 0xe0
67 #define NSAPI_IPTOS_PREC_INTERNETCONTROL 0xc0
68 #define NSAPI_IPTOS_PREC_CRITIC_ECP 0xa0
69 #define NSAPI_IPTOS_PREC_FLASHOVERRIDE 0x80
70 #define NSAPI_IPTOS_PREC_FLASH 0x60
71 #define NSAPI_IPTOS_PREC_IMMEDIATE 0x40
72 #define NSAPI_IPTOS_PREC_PRIORITY 0x20
73 #define NSAPI_IPTOS_PREC_ROUTINE 0x00
74 
75 /** Enum of standardized error codes
76  *
77  * Valid error codes have negative values and may
78  * be returned by any network operation.
79  *
80  * @enum nsapi_error
81  */
83  NSAPI_ERROR_OK = 0, /*!< no error */
84  NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
85  NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
86  NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
87  NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
88  NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
89  NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
90  NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
91  NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */
92  NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */
93  NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
94  NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
95  NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
96  NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */
97  NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */
98  NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */
99  NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */
100  NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */
101  NSAPI_ERROR_ADDRESS_IN_USE = -3018, /*!< Address already in use */
102  NSAPI_ERROR_TIMEOUT = -3019, /*!< operation timed out */
103  NSAPI_ERROR_BUSY = -3020, /*!< device is busy and cannot accept new operation */
104 };
105 
106 
107 /** Enum of connection status types
108  *
109  * Valid error codes have negative values.
110  *
111  * @enum nsapi_connection_status
112  */
114  NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */
115  NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */
116  NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */
117  NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */
118  NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED
119 } nsapi_connection_status_t;
120 
121 
122 /** Enum of event types
123  *
124  * Event callbacks are accompanied with an event-dependent parameter passed as an intptr_t.
125  *
126  * @enum nsapi_event
127  */
128 typedef enum nsapi_event {
129  NSAPI_EVENT_CONNECTION_STATUS_CHANGE = 0, /*!< network connection status has changed, the parameter = new status (nsapi_connection_status_t) */
130  NSAPI_EVENT_CELLULAR_STATUS_BASE = 0x1000, /*!< Cellular modem status has changed, See the enum values from enum cellular_connection_status_t in /connectivity/cellular/framework/common/CellularCommon.h */
131  NSAPI_EVENT_CELLULAR_STATUS_END = 0x1FFF /*!< cellular modem status has changed, See the enum values from enum cellular_connection_status_t in /connectivity/cellular/framework/common/CellularCommon.h */
132 } nsapi_event_t;
133 
134 
135 /** Type used to represent error codes
136  *
137  * This is a separate type from enum nsapi_error to avoid breaking
138  * compatibility in type-sensitive overloads
139  */
140 typedef signed int nsapi_error_t;
141 
142 /** Type used to represent the size of data passed through sockets
143  */
144 typedef unsigned int nsapi_size_t;
145 
146 /** Type used to represent either a size or error passed through sockets
147  *
148  * A valid nsapi_size_or_error_t is either a non-negative size or a
149  * negative error code from the nsapi_error_t
150  */
151 typedef signed int nsapi_size_or_error_t;
152 
153 /** Type used to represent either a value or error
154  *
155  * A valid nsapi_value_or_error_t is either a non-negative value or a
156  * negative error code from the nsapi_error_t
157  */
158 typedef signed int nsapi_value_or_error_t;
159 
160 /** Enum of encryption types
161  *
162  * The security type specifies a particular security to use when
163  * connected to a WiFi network
164  */
165 typedef enum nsapi_security {
166  NSAPI_SECURITY_NONE = 0x0, /*!< open access point */
167  NSAPI_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */
168  NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */
169  NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */
170  NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */
171  NSAPI_SECURITY_PAP = 0x5, /*!< phrase conforms to PPP authentication context */
172  NSAPI_SECURITY_CHAP = 0x6, /*!< phrase conforms to PPP authentication context */
173  NSAPI_SECURITY_EAP_TLS = 0x7, /*!< phrase conforms to EAP-TLS */
174  NSAPI_SECURITY_PEAP = 0x8, /*!< phrase conforms to PEAP */
175  NSAPI_SECURITY_WPA2_ENT = 0x9, /*!< phrase conforms to WPA2-AES and WPA-TKIP with enterprise security */
176  NSAPI_SECURITY_WPA3 = 0xA, /*!< phrase conforms to WPA3 */
177  NSAPI_SECURITY_WPA3_WPA2 = 0xB, /*!< phrase conforms to WPA3_WPA2 */
178  NSAPI_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */
180 
181 /** Size of 2 char network interface name from driver
182  */
183 #define NSAPI_INTERFACE_PREFIX_SIZE 2
184 
185 /** Maximum size of network interface name
186  */
187 #define NSAPI_INTERFACE_NAME_MAX_SIZE 6
188 
189 /** Maximum size of IP address representation
190  */
191 #define NSAPI_IP_SIZE NSAPI_IPv6_SIZE
192 
193 /** Maximum number of bytes for IP address
194  */
195 #define NSAPI_IP_BYTES NSAPI_IPv6_BYTES
196 
197 /** Maximum size of MAC address representation
198  */
199 #define NSAPI_MAC_SIZE 18
200 
201 /** Maximum number of bytes for MAC address
202  */
203 #define NSAPI_MAC_BYTES 6
204 
205 /** Size of IPv4 representation
206  */
207 #define NSAPI_IPv4_SIZE 16
208 
209 /** Number of bytes in IPv4 address
210  */
211 #define NSAPI_IPv4_BYTES 4
212 
213 /** Size of IPv6 representation
214  */
215 #define NSAPI_IPv6_SIZE 40
216 
217 /** Number of bytes in IPv6 address
218  */
219 #define NSAPI_IPv6_BYTES 16
220 
221 /** Enum of IP address versions
222  *
223  * The IP version specifies the type of an IP address.
224  *
225  * @enum nsapi_version
226  */
227 typedef enum nsapi_version {
228  NSAPI_UNSPEC, /*!< Address is unspecified */
229  NSAPI_IPv4, /*!< Address is IPv4 */
230  NSAPI_IPv6, /*!< Address is IPv6 */
231 } nsapi_version_t;
232 
233 /** IP address structure for passing IP addresses by value
234  */
235 typedef struct nsapi_addr {
236  /** IP version
237  * - NSAPI_IPv4
238  * - NSAPI_IPv6
239  * - NSAPI_UNSPEC
240  */
241  nsapi_version_t version;
242 
243  /** IP address
244  * The raw bytes of the IP address stored in big-endian format
245  */
247 } nsapi_addr_t;
248 
249 
250 /** Opaque handle for network sockets
251  */
252 typedef void *nsapi_socket_t;
253 
254 
255 /** Enum of socket protocols
256  *
257  * The socket protocol specifies a particular protocol to
258  * be used with a newly created socket.
259  *
260  * @enum nsapi_protocol
261  */
262 typedef enum nsapi_protocol {
263  NSAPI_TCP, /*!< Socket is of TCP type */
264  NSAPI_UDP, /*!< Socket is of UDP type */
265  NSAPI_ICMP, /*!< Socket is of ICMP type */
266 } nsapi_protocol_t;
267 
268 /** Enum of standardized stack option levels
269  * for use with NetworkStack::setstackopt and getstackopt.
270  *
271  * @enum nsapi_stack_level
272  */
273 typedef enum nsapi_stack_level {
274  NSAPI_STACK = 5000, /*!< Stack option level - see nsapi_stack_option_t for options */
275 } nsapi_stack_level_t;
276 
277 /** Enum of standardized stack option names for level NSAPI_STACK
278  * of NetworkStack::setstackopt and getstackopt.
279  *
280  * These options may not be supported on all stacks, in which
281  * case NSAPI_ERROR_UNSUPPORTED may be returned.
282  *
283  * @enum nsapi_stack_option
284  */
285 typedef enum nsapi_stack_option {
286  NSAPI_IPV4_MRU, /*!< Sets/gets size of largest IPv4 fragmented datagram to reassemble */
287  NSAPI_IPV6_MRU, /*!< Sets/gets size of largest IPv6 fragmented datagram to reassemble */
288 } nsapi_stack_option_t;
289 
290 /** Enum of standardized socket option levels
291  * for use with Socket::setsockopt and getsockopt.
292  *
293  * @enum nsapi_socket_level
294  */
295 typedef enum nsapi_socket_level {
296  NSAPI_SOCKET = 7000, /*!< Socket option level - see nsapi_socket_option_t for options */
297 } nsapi_socket_level_t;
298 
299 /** Enum of standardized socket option names for level NSAPI_SOCKET
300  * of Socket::setsockopt and getsockopt.
301  *
302  * These options may not be supported on all stacks, in which
303  * case NSAPI_ERROR_UNSUPPORTED may be returned.
304  *
305  * @enum nsapi_socket_option
306  */
307 typedef enum nsapi_socket_option {
308  NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */
309  NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */
310  NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */
311  NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */
312  NSAPI_LINGER, /*!< Keeps close from returning until queues empty */
313  NSAPI_SNDBUF, /*!< Sets send buffer size */
314  NSAPI_RCVBUF, /*!< Sets recv buffer size */
315  NSAPI_ADD_MEMBERSHIP, /*!< Add membership to multicast address */
316  NSAPI_DROP_MEMBERSHIP, /*!< Drop membership to multicast address */
317  NSAPI_BIND_TO_DEVICE, /*!< Bind socket network interface name*/
318  NSAPI_LATENCY, /*!< Read estimated latency to destination */
319  NSAPI_STAGGER, /*!< Read estimated stagger value to destination */
320  NSAPI_IPTOS, /*!< Set IP type of service to set specific precedence */
321  NSAPI_BROADCAST /*!< Set broadcast flag for UDP socket */
322 } nsapi_socket_option_t;
323 
324 typedef enum nsapi_tlssocket_level {
325  NSAPI_TLSSOCKET_LEVEL = 7099, /*!< TLSSocket option level - see nsapi_tlssocket_option_t for options*/
326 } nsapi_tlssocket_level_t;
327 
329  NSAPI_TLSSOCKET_SET_HOSTNAME, /*!< Set host name */
330  NSAPI_TLSSOCKET_SET_CACERT, /*!< Set server CA certificate */
331  NSAPI_TLSSOCKET_SET_CLCERT, /*!< Set client certificate */
332  NSAPI_TLSSOCKET_SET_CLKEY, /*!< Set client key */
333  NSAPI_TLSSOCKET_ENABLE /*!< Enable TLSSocket */
334 } nsapi_tlssocket_option_t;
335 
336 /** Supported IP protocol versions of IP stack
337  *
338  * @enum nsapi_ip_stack
339  */
340 typedef enum nsapi_ip_stack {
341  DEFAULT_STACK = 0,
342  IPV4_STACK,
343  IPV6_STACK,
344  IPV4V6_STACK
345 } nsapi_ip_stack_t;
346 
347 /* Backwards compatibility - previously didn't distinguish stack and socket options */
348 typedef nsapi_socket_level_t nsapi_level_t;
349 typedef nsapi_socket_option_t nsapi_option_t;
350 
351 /** nsapi_wifi_ap structure
352  *
353  * Structure representing a WiFi Access Point
354  */
355 typedef struct nsapi_wifi_ap {
356  char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
357  uint8_t bssid[6];
358  nsapi_security_t security;
359  int8_t rssi;
360  uint8_t channel;
362 
363 
364 /** nsapi_stack structure
365  *
366  * Stack structure representing a specific instance of a stack.
367  */
368 typedef struct nsapi_stack {
369  /** Network stack operation table
370  *
371  * Provides access to the underlying api of the stack. This is not
372  * flattened into the nsapi_stack to allow allocation in read-only
373  * memory.
374  */
375  const struct nsapi_stack_api *stack_api;
376 
377  /** Opaque handle for network stacks
378  */
379  void *stack;
380 
381  // Internal nsapi buffer
382  unsigned _stack_buffer[16];
383 } nsapi_stack_t;
384 
385 /** nsapi_ip_mreq structure
386  */
387 typedef struct nsapi_ip_mreq {
388  nsapi_addr_t imr_multiaddr; /* IP multicast address of group */
389  nsapi_addr_t imr_interface; /* local IP address of interface */
391 
392 /** nsapi_latency_req structure
393  */
394 typedef struct nsapi_latency_req {
395  uint8_t addr[16]; /* [IN] Destination address to estimate latency */
396  uint32_t latency; /* [OUT] Latency value */
398 
399 /** nsapi_stagger_req structure
400  */
401 typedef struct nsapi_stagger_req {
402  uint8_t addr[16]; /* [IN] Destination address to estimate stagger */
403  uint16_t data_amount; /* [IN] Amount of data to be sent in kilobytes */
404  uint16_t stagger_min; /* [OUT] Minimum stagger value in seconds */
405  uint16_t stagger_max; /* [OUT] Maximum stagger value in seconds */
406  uint16_t stagger_rand; /* [OUT] Randomized stagger value in seconds */
408 
409 /** nsapi_stack_api structure
410  *
411  * Common api structure for network stack operations. A network stack
412  * can provide a nsapi_stack_api structure filled out with the
413  * appropriate implementation.
414  *
415  * Unsupported operations can be left as null pointers.
416  */
417 typedef struct nsapi_stack_api {
418  /** Get the local IP address
419  *
420  * @param stack Stack handle
421  * @return Local IP Address or null address if not connected
422  */
423  nsapi_addr_t (*get_ip_address)(nsapi_stack_t *stack);
424 
425  /** Translates a hostname to an IP address
426  *
427  * The hostname may be either a domain name or an IP address. If the
428  * hostname is an IP address, no network transactions will be performed.
429  *
430  * If no stack-specific DNS resolution is provided, the hostname
431  * will be resolve using a UDP socket on the stack.
432  *
433  * @param stack Stack handle
434  * @param addr Destination for the host IP address
435  * @param host Hostname to resolve
436  * @param version Address family
437  * @return 0 on success, negative error code on failure
438  */
439  nsapi_error_t (*gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version);
440 
441  /** Add a domain name server to list of servers to query
442  *
443  * @param addr Destination for the host address
444  * @return 0 on success, negative error code on failure
445  */
446  nsapi_error_t (*add_dns_server)(nsapi_stack_t *stack, nsapi_addr_t addr);
447 
448  /** Set stack-specific stack options
449  *
450  * The setstackopt allow an application to pass stack-specific hints
451  * to the underlying stack. For unsupported options,
452  * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified.
453  *
454  * @param stack Stack handle
455  * @param level Stack-specific protocol level
456  * @param optname Stack-specific option identifier
457  * @param optval Option value
458  * @param optlen Length of the option value
459  * @return 0 on success, negative error code on failure
460  */
461  nsapi_error_t (*setstackopt)(nsapi_stack_t *stack, int level,
462  int optname, const void *optval, unsigned optlen);
463 
464  /** Get stack-specific stack options
465  *
466  * The getstackopt allow an application to retrieve stack-specific hints
467  * from the underlying stack. For unsupported options,
468  * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
469  *
470  * @param stack Stack handle
471  * @param level Stack-specific protocol level
472  * @param optname Stack-specific option identifier
473  * @param optval Destination for option value
474  * @param optlen Length of the option value
475  * @return 0 on success, negative error code on failure
476  */
477  nsapi_error_t (*getstackopt)(nsapi_stack_t *stack, int level,
478  int optname, void *optval, unsigned *optlen);
479 
480  /** Opens a socket
481  *
482  * Creates a network socket and stores it in the specified handle.
483  * The handle must be passed to following calls on the socket.
484  *
485  * A stack may have a finite number of sockets, in this case
486  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
487  *
488  * @param stack Stack context
489  * @param socket Destination for the handle to a newly created socket
490  * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
491  * @return 0 on success, negative error code on failure
492  */
493  nsapi_error_t (*socket_open)(nsapi_stack_t *stack, nsapi_socket_t *socket,
494  nsapi_protocol_t proto);
495 
496  /** Close the socket
497  *
498  * Closes any open connection and deallocates any memory associated
499  * with the socket.
500  *
501  * @param stack Stack handle
502  * @param socket Socket handle
503  * @return 0 on success, negative error code on failure
504  */
505  nsapi_error_t (*socket_close)(nsapi_stack_t *stack, nsapi_socket_t socket);
506 
507  /** Bind a specific address to a socket
508  *
509  * Binding a socket specifies the address and port on which to receive
510  * data. If the IP address is zeroed, only the port is bound.
511  *
512  * @param stack Stack handle
513  * @param socket Socket handle
514  * @param addr Local address to bind, may be null
515  * @param port Local port to bind
516  * @return 0 on success, negative error code on failure.
517  */
518  nsapi_error_t (*socket_bind)(nsapi_stack_t *stack, nsapi_socket_t socket,
519  nsapi_addr_t addr, uint16_t port);
520 
521  /** Listen for connections on a TCP socket
522  *
523  * Marks the socket as a passive socket that can be used to accept
524  * incoming connections.
525  *
526  * @param stack Stack handle
527  * @param socket Socket handle
528  * @param backlog Number of pending connections that can be queued
529  * simultaneously
530  * @return 0 on success, negative error code on failure
531  */
532  nsapi_error_t (*socket_listen)(nsapi_stack_t *stack, nsapi_socket_t socket, int backlog);
533 
534  /** Connects TCP socket to a remote host
535  *
536  * Initiates a connection to a remote server specified by the
537  * indicated address.
538  *
539  * @param stack Stack handle
540  * @param socket Socket handle
541  * @param addr The address of the remote host
542  * @param port The port of the remote host
543  * @return 0 on success, negative error code on failure
544  */
545  nsapi_error_t (*socket_connect)(nsapi_stack_t *stack, nsapi_socket_t socket,
546  nsapi_addr_t addr, uint16_t port);
547 
548  /** Accepts a connection on a TCP socket
549  *
550  * The server socket must be bound and set to listen for connections.
551  * On a new connection, creates a network socket and stores it in the
552  * specified handle. The handle must be passed to following calls on
553  * the socket.
554  *
555  * A stack may have a finite number of sockets, in this case
556  * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
557  *
558  * This call is non-blocking. If accept would block,
559  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
560  *
561  * @param stack Stack handle
562  * @param server Socket handle to server to accept from
563  * @param socket Destination for a handle to the newly created socket
564  * @param addr Destination for the address of the remote host
565  * @param port Destination for the port of the remote host
566  * @return 0 on success, negative error code on failure
567  */
568  nsapi_error_t (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t server,
569  nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port);
570 
571  /** Send data over a TCP socket
572  *
573  * The socket must be connected to a remote host. Returns the number of
574  * bytes sent from the buffer.
575  *
576  * This call is non-blocking. If send would block,
577  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
578  *
579  * @param stack Stack handle
580  * @param socket Socket handle
581  * @param data Buffer of data to send to the host
582  * @param size Size of the buffer in bytes
583  * @return Number of sent bytes on success, negative error
584  * code on failure
585  */
586  nsapi_size_or_error_t (*socket_send)(nsapi_stack_t *stack, nsapi_socket_t socket,
587  const void *data, nsapi_size_t size);
588 
589  /** Receive data over a TCP socket
590  *
591  * The socket must be connected to a remote host. Returns the number of
592  * bytes received into the buffer.
593  *
594  * This call is non-blocking. If recv would block,
595  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
596  *
597  * @param stack Stack handle
598  * @param socket Socket handle
599  * @param data Destination buffer for data received from the host
600  * @param size Size of the buffer in bytes
601  * @return Number of received bytes on success, negative error
602  * code on failure
603  */
604  nsapi_size_or_error_t (*socket_recv)(nsapi_stack_t *stack, nsapi_socket_t socket,
605  void *data, nsapi_size_t size);
606 
607  /** Send a packet over a UDP socket
608  *
609  * Sends data to the specified address. Returns the number of bytes
610  * sent from the buffer.
611  *
612  * This call is non-blocking. If sendto would block,
613  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
614  *
615  * @param stack Stack handle
616  * @param socket Socket handle
617  * @param addr The address of the remote host
618  * @param port The port of the remote host
619  * @param data Buffer of data to send to the host
620  * @param size Size of the buffer in bytes
621  * @return Number of sent bytes on success, negative error
622  * code on failure
623  */
624  nsapi_size_or_error_t (*socket_sendto)(nsapi_stack_t *stack, nsapi_socket_t socket,
625  nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size);
626 
627  /** Receive a packet over a UDP socket
628  *
629  * Receives data and stores the source address in address if address
630  * is not NULL. Returns the number of bytes received into the buffer.
631  *
632  * This call is non-blocking. If recvfrom would block,
633  * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
634  *
635  * @param stack Stack handle
636  * @param socket Socket handle
637  * @param addr Destination for the address of the remote host
638  * @param port Destination for the port of the remote host
639  * @param data Destination buffer for data received from the host
640  * @param size Size of the buffer in bytes
641  * @return Number of received bytes on success, negative error
642  * code on failure
643  */
644  nsapi_size_or_error_t (*socket_recvfrom)(nsapi_stack_t *stack, nsapi_socket_t socket,
645  nsapi_addr_t *addr, uint16_t *port, void *buffer, nsapi_size_t size);
646 
647  /** Register a callback on state change of the socket
648  *
649  * The specified callback will be called on state changes such as when
650  * the socket can recv/send/accept successfully and on when an error
651  * occurs. The callback may also be called spuriously without reason.
652  *
653  * The callback may be called in an interrupt context and should not
654  * perform expensive operations such as recv/send calls.
655  *
656  * @param stack Stack handle
657  * @param socket Socket handle
658  * @param callback Function to call on state change
659  * @param data Argument to pass to callback
660  */
661  void (*socket_attach)(nsapi_stack_t *stack, nsapi_socket_t socket,
662  void (*callback)(void *), void *data);
663 
664  /** Set stack-specific socket options
665  *
666  * The setsockopt allow an application to pass stack-specific hints
667  * to the underlying stack. For unsupported options,
668  * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
669  *
670  * @param stack Stack handle
671  * @param socket Socket handle
672  * @param level Stack-specific protocol level
673  * @param optname Stack-specific option identifier
674  * @param optval Option value
675  * @param optlen Length of the option value
676  * @return 0 on success, negative error code on failure
677  */
678  nsapi_error_t (*setsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level,
679  int optname, const void *optval, unsigned optlen);
680 
681  /** Get stack-specific socket options
682  *
683  * The getstackopt allow an application to retrieve stack-specific hints
684  * from the underlying stack. For unsupported options,
685  * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
686  *
687  * @param stack Stack handle
688  * @param socket Socket handle
689  * @param level Stack-specific protocol level
690  * @param optname Stack-specific option identifier
691  * @param optval Destination for option value
692  * @param optlen Length of the option value
693  * @return 0 on success, negative error code on failure
694  */
695  nsapi_error_t (*getsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level,
696  int optname, void *optval, unsigned *optlen);
698 
699 
700 #ifdef __cplusplus
701 }
702 #endif
703 
704 #endif
705 
706 /** @}*/
nsapi_stack structure
Definition: nsapi_types.h:368
nsapi_stack_option
Enum of standardized stack option names for level NSAPI_STACK of NetworkStack::setstackopt and getsta...
Definition: nsapi_types.h:285
nsapi_latency_req structure
Definition: nsapi_types.h:394
const struct nsapi_stack_api * stack_api
Network stack operation table.
Definition: nsapi_types.h:375
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:252
void * stack
Opaque handle for network stacks.
Definition: nsapi_types.h:379
nsapi_security
Enum of encryption types.
Definition: nsapi_types.h:165
nsapi_stack_api structure
Definition: nsapi_types.h:417
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:140
struct nsapi_stack_api nsapi_stack_api_t
nsapi_stack_api structure
nsapi_stack_level
Enum of standardized stack option levels for use with NetworkStack::setstackopt and getstackopt...
Definition: nsapi_types.h:273
nsapi_ip_stack
Supported IP protocol versions of IP stack.
Definition: nsapi_types.h:340
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
#define NSAPI_IP_BYTES
Maximum number of bytes for IP address.
Definition: nsapi_types.h:195
nsapi_socket_level
Enum of standardized socket option levels for use with Socket::setsockopt and getsockopt.
Definition: nsapi_types.h:295
struct nsapi_stagger_req nsapi_stagger_req_t
nsapi_stagger_req structure
signed int nsapi_value_or_error_t
Type used to represent either a value or error.
Definition: nsapi_types.h:158
struct nsapi_addr nsapi_addr_t
IP address structure for passing IP addresses by value.
nsapi_wifi_ap structure
Definition: nsapi_types.h:355
nsapi_event
Enum of event types.
Definition: nsapi_types.h:128
struct nsapi_wifi_ap nsapi_wifi_ap_t
nsapi_wifi_ap structure
uint8_t bytes[16]
IP address The raw bytes of the IP address stored in big-endian format.
Definition: nsapi_types.h:246
nsapi_version_t version
IP version.
Definition: nsapi_types.h:241
nsapi_connection_status
Enum of connection status types.
Definition: nsapi_types.h:113
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:144
nsapi_stagger_req structure
Definition: nsapi_types.h:401
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:235
struct nsapi_latency_req nsapi_latency_req_t
nsapi_latency_req structure
enum nsapi_security nsapi_security_t
Enum of encryption types.
nsapi_tlssocket_level
Definition: nsapi_types.h:324
struct nsapi_ip_mreq nsapi_ip_mreq_t
nsapi_ip_mreq structure
nsapi_error
Enum of standardized error codes.
Definition: nsapi_types.h:82
nsapi_tlssocket_option
Definition: nsapi_types.h:328
struct nsapi_stack nsapi_stack_t
nsapi_stack structure
nsapi_ip_mreq structure
Definition: nsapi_types.h:387
nsapi_protocol
Enum of socket protocols.
Definition: nsapi_types.h:262
nsapi_socket_option
Enum of standardized socket option names for level NSAPI_SOCKET of Socket::setsockopt and getsockopt...
Definition: nsapi_types.h:307
nsapi_version
Enum of IP address versions.
Definition: nsapi_types.h:227
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.