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