Mbed OS Reference
Loading...
Searching...
No Matches
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
29extern "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 */
130typedef 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 */
142typedef signed int nsapi_error_t;
143
144/** Type used to represent the size of data passed through sockets
145 */
146typedef 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 */
153typedef 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 */
160typedef 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 */
167typedef 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 */
229typedef 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 */
237typedef 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 */
250
251
252/** Opaque handle for network sockets
253 */
254typedef 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 */
264typedef 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 */
275typedef 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 */
287typedef 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 */
297typedef 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 */
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
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 */
343typedef 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 */
351typedef nsapi_socket_level_t nsapi_level_t;
352typedef nsapi_socket_option_t nsapi_option_t;
353
354/** nsapi_wifi_ap structure
355 *
356 * Structure representing a WiFi Access Point
357 */
358typedef 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 */
371typedef 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 */
379
380 /** Opaque handle for network stacks
381 */
382 void *stack;
383
384 // Internal nsapi buffer
385 unsigned _stack_buffer[16];
387
388/** nsapi_ip_mreq structure
389 */
390typedef 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 */
397typedef 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 */
404typedef 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/**
413 * Header structure for control info.
414 * See #MsgHeaderIterator.
415 */
416typedef struct nsapi_msghdr {
417 /* In C alignment can't be specified in the type declaration.
418 * Specifying it in the first member definition will affect whole structure.
419 */
420 MBED_ALIGN(max_align_t) nsapi_size_t len; /* Data byte count, including header */
421 int level; /* Originating protocol */
422 int type; /* Protocol-specific type */
424
425/** nsapi_pktinfo structure
426 */
427typedef struct nsapi_pktinfo {
428 nsapi_msghdr_t hdr; /* Header identifying the message control structure */
429 nsapi_addr_t ipi_addr; /* Address associated with the packet */
430 int ipi_ifindex; /* Interface associated with the packet */
431 void *network_interface; /* Network interface pointer*/
433
434/** nsapi_stack_api structure
435 *
436 * Common api structure for network stack operations. A network stack
437 * can provide a nsapi_stack_api structure filled out with the
438 * appropriate implementation.
439 *
440 * Unsupported operations can be left as null pointers.
441 */
442typedef struct nsapi_stack_api {
443 /** Get the local IP address
444 *
445 * <br> \c stack : Stack handle
446 *
447 * @return Local IP Address or null address if not connected
448 */
450
451 /** Translates a hostname to an IP address
452 *
453 * The hostname may be either a domain name or an IP address. If the
454 * hostname is an IP address, no network transactions will be performed.
455 *
456 * If no stack-specific DNS resolution is provided, the hostname
457 * will be resolve using a UDP socket on the stack.
458 *
459 * <br> \c stack : Stack handle
460 * <br> \c addr : Destination for the host IP address
461 * <br> \c host : Hostname to resolve
462 * <br> \c version : Address family
463 *
464 * @return 0 on success, negative error code on failure
465 */
466 nsapi_error_t (*gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version);
467
468 /** Add a domain name server to list of servers to query
469 *
470 * <br> \c addr : Destination for the host address
471 *
472 * @return 0 on success, negative error code on failure
473 */
475
476 /** Set stack-specific stack options
477 *
478 * The setstackopt allow an application to pass stack-specific hints
479 * to the underlying stack. For unsupported options,
480 * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified.
481 *
482 * <br> \c stack : Stack handle
483 * <br> \c level : Stack-specific protocol level
484 * <br> \c optname : Stack-specific option identifier
485 * <br> \c optval : Option value
486 * <br> \c optlen : Length of the option value
487 *
488 * @return 0 on success, negative error code on failure
489 */
491 int optname, const void *optval, unsigned optlen);
492
493 /** Get stack-specific stack options
494 *
495 * The getstackopt allow an application to retrieve stack-specific hints
496 * from the underlying stack. For unsupported options,
497 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
498 *
499 * <br> \c stack : Stack handle
500 * <br> \c level : Stack-specific protocol level
501 * <br> \c optname : Stack-specific option identifier
502 * <br> \c optval : Destination for option value
503 * <br> \c optlen : Length of the option value
504 *
505 * @return 0 on success, negative error code on failure
506 */
508 int optname, void *optval, unsigned *optlen);
509
510 /** Opens a socket
511 *
512 * Creates a network socket and stores it in the specified handle.
513 * The handle must be passed to following calls on the socket.
514 *
515 * A stack may have a finite number of sockets, in this case
516 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
517 *
518 * <br> \c stack : Stack context
519 * <br> \c socket : Destination for the handle to a newly created socket
520 * <br> \c proto : Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
521 *
522 * @return 0 on success, negative error code on failure
523 */
525 nsapi_protocol_t proto);
526
527 /** Close the socket
528 *
529 * Closes any open connection and deallocates any memory associated
530 * with the socket.
531 *
532 * <br> \c stack : Stack handle
533 * <br> \c socket : Socket handle
534 *
535 * @return 0 on success, negative error code on failure
536 */
538
539 /** Bind a specific address to a socket
540 *
541 * Binding a socket specifies the address and port on which to receive
542 * data. If the IP address is zeroed, only the port is bound.
543 *
544 * <br> \c stack : Stack handle
545 * <br> \c socket : Socket handle
546 * <br> \c addr : Local address to bind, may be null
547 * <br> \c port : Local port to bind
548 *
549 * @return 0 on success, negative error code on failure.
550 */
552 nsapi_addr_t addr, uint16_t port);
553
554 /** Listen for connections on a TCP socket
555 *
556 * Marks the socket as a passive socket that can be used to accept
557 * incoming connections.
558 *
559 * <br> \c stack : Stack handle
560 * <br> \c socket : Socket handle
561 * <br> \c backlog : Number of pending connections that can be queued simultaneously
562 *
563 * @return 0 on success, negative error code on failure
564 */
566
567 /** Connects TCP socket to a remote host
568 *
569 * Initiates a connection to a remote server specified by the
570 * indicated address.
571 *
572 * <br> \c stack : Stack handle
573 * <br> \c socket : Socket handle
574 * <br> \c addr : The address of the remote host
575 * <br> \c port : The port of the remote host
576 *
577 * @return 0 on success, negative error code on failure
578 */
580 nsapi_addr_t addr, uint16_t port);
581
582 /** Accepts a connection on a TCP socket
583 *
584 * The server socket must be bound and set to listen for connections.
585 * On a new connection, creates a network socket and stores it in the
586 * specified handle. The handle must be passed to following calls on
587 * the socket.
588 *
589 * A stack may have a finite number of sockets, in this case
590 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
591 *
592 * This call is non-blocking. If accept would block,
593 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
594 *
595 * <br> \c stack : Stack handle
596 * <br> \c server : Socket handle to server to accept from
597 * <br> \c socket : Destination for a handle to the newly created socket
598 * <br> \c addr : Destination for the address of the remote host
599 * <br> \c port : Destination for the port of the remote host
600 *
601 * @return 0 on success, negative error code on failure
602 */
604 nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port);
605
606 /** Send data over a TCP socket
607 *
608 * The socket must be connected to a remote host. Returns the number of
609 * bytes sent from the buffer.
610 *
611 * This call is non-blocking. If send would block,
612 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
613 *
614 * <br> \c stack : Stack handle
615 * <br> \c socket : Socket handle
616 * <br> \c data : Buffer of data to send to the host
617 * <br> \c size : Size of the buffer in bytes
618 *
619 * @return Number of sent bytes on success, negative error code on failure
620 */
622 const void *data, nsapi_size_t size);
623
624 /** Receive data over a TCP socket
625 *
626 * The socket must be connected to a remote host. Returns the number of
627 * bytes received into the buffer.
628 *
629 * This call is non-blocking. If recv would block,
630 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
631 *
632 * <br> \c stack : Stack handle
633 * <br> \c socket : Socket handle
634 * <br> \c data : Destination buffer for data received from the host
635 * <br> \c size : Size of the buffer in bytes
636 *
637 * @return Number of received bytes on success, negative error code on failure
638 */
640 void *data, nsapi_size_t size);
641
642 /** Send a packet over a UDP socket
643 *
644 * Sends data to the specified address. Returns the number of bytes
645 * sent from the buffer.
646 *
647 * This call is non-blocking. If sendto would block,
648 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
649 *
650 * <br> \c stack : Stack handle
651 * <br> \c socket : Socket handle
652 * <br> \c addr : The address of the remote host
653 * <br> \c port : The port of the remote host
654 * <br> \c data : Buffer of data to send to the host
655 * <br> \c size : Size of the buffer in bytes
656 *
657 * @return Number of sent bytes on success, negative error code on failure
658 */
660 nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size);
661
662 /** Receive a packet over a UDP socket
663 *
664 * Receives data and stores the source address in address if address
665 * is not NULL. Returns the number of bytes received into the buffer.
666 *
667 * This call is non-blocking. If recvfrom would block,
668 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
669 *
670 * <br> \c stack : Stack handle
671 * <br> \c socket : Socket handle
672 * <br> \c addr : Destination for the address of the remote host
673 * <br> \c port : Destination for the port of the remote host
674 * <br> \c data : Destination buffer for data received from the host
675 * <br> \c size : Size of the buffer in bytes
676 *
677 * @return Number of received bytes on success, negative error code on failure
678 */
680 nsapi_addr_t *addr, uint16_t *port, void *buffer, nsapi_size_t size);
681
682 // TODO: Documentation
683 nsapi_size_or_error_t (*socket_sendto_control)(nsapi_stack_t *stack, nsapi_socket_t socket,
684 nsapi_addr_t addr, uint16_t port,
685 const void *data, nsapi_size_t size,
686 const nsapi_msghdr_t *control, nsapi_size_t control_size);
687
688 nsapi_size_or_error_t (*socket_recvfrom_control)(nsapi_stack_t *stack, nsapi_socket_t socket,
689 nsapi_addr_t *addr, uint16_t *port,
690 void *data, nsapi_size_t size,
691 nsapi_msghdr_t *control, nsapi_size_t control_size);
692
693 /** Register a callback on state change of the socket
694 *
695 * The specified callback will be called on state changes such as when
696 * the socket can recv/send/accept successfully and on when an error
697 * occurs. The callback may also be called spuriously without reason.
698 *
699 * The callback may be called in an interrupt context and should not
700 * perform expensive operations such as recv/send calls.
701 *
702 * <br> \c stack : Stack handle
703 * <br> \c socket : Socket handle
704 * <br> \c callback : Function to call on state change
705 * <br> \c data : Argument to pass to callback
706 */
708 void (*callback)(void *), void *data);
709
710 /** Set stack-specific socket options
711 *
712 * The setsockopt allow an application to pass stack-specific hints
713 * to the underlying stack. For unsupported options,
714 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
715 *
716 * <br> \c stack : Stack handle
717 * <br> \c socket : Socket handle
718 * <br> \c level : Stack-specific protocol level
719 * <br> \c optname : Stack-specific option identifier
720 * <br> \c optval : Option value
721 * <br> \c optlen : Length of the option value
722 *
723 * @return 0 on success, negative error code on failure
724 */
726 int optname, const void *optval, unsigned optlen);
727
728 /** Get stack-specific socket options
729 *
730 * The getstackopt allow an application to retrieve stack-specific hints
731 * from the underlying stack. For unsupported options,
732 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
733 *
734 * <br> \c stack : Stack handle
735 * <br> \c socket : Socket handle
736 * <br> \c level : Stack-specific protocol level
737 * <br> \c optname : Stack-specific option identifier
738 * <br> \c optval : Destination for option value
739 * <br> \c optlen : Length of the option value
740 *
741 * @return 0 on success, negative error code on failure
742 */
744 int optname, void *optval, unsigned *optlen);
746
747
748#ifdef __cplusplus
749}
750#endif
751
752#endif
753
754/** @}*/
enum nsapi_security nsapi_security_t
Enum of encryption types.
nsapi_security
Enum of encryption types.
Definition: nsapi_types.h:167
nsapi_socket_option
Enum of standardized socket option names for level NSAPI_SOCKET of Socket::setsockopt and getsockopt.
Definition: nsapi_types.h:309
nsapi_ip_stack
Supported IP protocol versions of IP stack.
Definition: nsapi_types.h:343
nsapi_event
Enum of event types.
Definition: nsapi_types.h:130
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:153
nsapi_stack_level
Enum of standardized stack option levels for use with NetworkStack::setstackopt and getstackopt.
Definition: nsapi_types.h:275
nsapi_stack_option
Enum of standardized stack option names for level NSAPI_STACK of NetworkStack::setstackopt and getsta...
Definition: nsapi_types.h:287
struct nsapi_pktinfo nsapi_pktinfo_t
nsapi_pktinfo structure
#define NSAPI_IP_BYTES
Maximum number of bytes for IP address.
Definition: nsapi_types.h:197
struct nsapi_addr nsapi_addr_t
IP address structure for passing IP addresses by value.
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
struct nsapi_latency_req nsapi_latency_req_t
nsapi_latency_req structure
struct nsapi_stack nsapi_stack_t
nsapi_stack structure
nsapi_connection_status
Enum of connection status types.
Definition: nsapi_types.h:115
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
nsapi_socket_level
Enum of standardized socket option levels for use with Socket::setsockopt and getsockopt.
Definition: nsapi_types.h:297
struct nsapi_stack_api nsapi_stack_api_t
nsapi_stack_api structure
struct nsapi_ip_mreq nsapi_ip_mreq_t
nsapi_ip_mreq structure
nsapi_tlssocket_level
Definition: nsapi_types.h:327
struct nsapi_wifi_ap nsapi_wifi_ap_t
nsapi_wifi_ap structure
nsapi_error
Enum of standardized error codes.
Definition: nsapi_types.h:84
nsapi_version
Enum of IP address versions.
Definition: nsapi_types.h:229
nsapi_protocol
Enum of socket protocols.
Definition: nsapi_types.h:264
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:254
struct nsapi_msghdr nsapi_msghdr_t
Header structure for control info.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
nsapi_tlssocket_option
Definition: nsapi_types.h:331
@ NSAPI_SECURITY_UNKNOWN
Definition: nsapi_types.h:180
@ NSAPI_SECURITY_EAP_TLS
Definition: nsapi_types.h:175
@ NSAPI_SECURITY_WPA2
Definition: nsapi_types.h:171
@ NSAPI_SECURITY_NONE
Definition: nsapi_types.h:168
@ NSAPI_SECURITY_WEP
Definition: nsapi_types.h:169
@ NSAPI_SECURITY_WPA_WPA2
Definition: nsapi_types.h:172
@ NSAPI_SECURITY_WPA3
Definition: nsapi_types.h:178
@ NSAPI_SECURITY_WPA
Definition: nsapi_types.h:170
@ NSAPI_SECURITY_WPA2_ENT
Definition: nsapi_types.h:177
@ NSAPI_SECURITY_PAP
Definition: nsapi_types.h:173
@ NSAPI_SECURITY_CHAP
Definition: nsapi_types.h:174
@ NSAPI_SECURITY_PEAP
Definition: nsapi_types.h:176
@ NSAPI_SECURITY_WPA3_WPA2
Definition: nsapi_types.h:179
@ NSAPI_KEEPALIVE
Definition: nsapi_types.h:311
@ NSAPI_REUSEADDR
Definition: nsapi_types.h:310
@ NSAPI_RCVBUF
Definition: nsapi_types.h:316
@ NSAPI_LINGER
Definition: nsapi_types.h:314
@ NSAPI_SNDBUF
Definition: nsapi_types.h:315
@ NSAPI_ADD_MEMBERSHIP
Definition: nsapi_types.h:317
@ NSAPI_STAGGER
Definition: nsapi_types.h:321
@ NSAPI_KEEPIDLE
Definition: nsapi_types.h:312
@ NSAPI_KEEPINTVL
Definition: nsapi_types.h:313
@ NSAPI_IPTOS
Definition: nsapi_types.h:322
@ NSAPI_LATENCY
Definition: nsapi_types.h:320
@ NSAPI_PKTINFO
Definition: nsapi_types.h:324
@ NSAPI_BIND_TO_DEVICE
Definition: nsapi_types.h:319
@ NSAPI_BROADCAST
Definition: nsapi_types.h:323
@ NSAPI_DROP_MEMBERSHIP
Definition: nsapi_types.h:318
@ NSAPI_EVENT_CELLULAR_STATUS_END
Definition: nsapi_types.h:133
@ NSAPI_EVENT_CELLULAR_STATUS_BASE
Definition: nsapi_types.h:132
@ NSAPI_EVENT_CONNECTION_STATUS_CHANGE
Definition: nsapi_types.h:131
@ NSAPI_STACK
Definition: nsapi_types.h:276
@ NSAPI_IPV6_MRU
Definition: nsapi_types.h:289
@ NSAPI_IPV4_MRU
Definition: nsapi_types.h:288
@ NSAPI_STATUS_CONNECTING
Definition: nsapi_types.h:119
@ NSAPI_STATUS_DISCONNECTED
Definition: nsapi_types.h:118
@ NSAPI_STATUS_LOCAL_UP
Definition: nsapi_types.h:116
@ NSAPI_STATUS_GLOBAL_UP
Definition: nsapi_types.h:117
@ NSAPI_SOCKET
Definition: nsapi_types.h:298
@ NSAPI_TLSSOCKET_LEVEL
Definition: nsapi_types.h:328
@ NSAPI_ERROR_IN_PROGRESS
Definition: nsapi_types.h:98
@ NSAPI_ERROR_PARAMETER
Definition: nsapi_types.h:88
@ NSAPI_ERROR_DEVICE_ERROR
Definition: nsapi_types.h:97
@ NSAPI_ERROR_NO_MEMORY
Definition: nsapi_types.h:92
@ NSAPI_ERROR_BUSY
Definition: nsapi_types.h:105
@ NSAPI_ERROR_NO_SOCKET
Definition: nsapi_types.h:90
@ NSAPI_ERROR_DNS_FAILURE
Definition: nsapi_types.h:94
@ NSAPI_ERROR_IS_CONNECTED
Definition: nsapi_types.h:100
@ NSAPI_ERROR_NO_ADDRESS
Definition: nsapi_types.h:91
@ NSAPI_ERROR_DHCP_FAILURE
Definition: nsapi_types.h:95
@ NSAPI_ERROR_ADDRESS_IN_USE
Definition: nsapi_types.h:103
@ NSAPI_ERROR_WOULD_BLOCK
Definition: nsapi_types.h:86
@ NSAPI_ERROR_AUTH_FAILURE
Definition: nsapi_types.h:96
@ NSAPI_ERROR_NO_CONNECTION
Definition: nsapi_types.h:89
@ NSAPI_ERROR_CONNECTION_LOST
Definition: nsapi_types.h:101
@ NSAPI_ERROR_NO_SSID
Definition: nsapi_types.h:93
@ NSAPI_ERROR_TIMEOUT
Definition: nsapi_types.h:104
@ NSAPI_ERROR_ALREADY
Definition: nsapi_types.h:99
@ NSAPI_ERROR_CONNECTION_TIMEOUT
Definition: nsapi_types.h:102
@ NSAPI_ERROR_OK
Definition: nsapi_types.h:85
@ NSAPI_ERROR_UNSUPPORTED
Definition: nsapi_types.h:87
@ NSAPI_UNSPEC
Definition: nsapi_types.h:230
@ NSAPI_IPv4
Definition: nsapi_types.h:231
@ NSAPI_IPv6
Definition: nsapi_types.h:232
@ NSAPI_TCP
Definition: nsapi_types.h:265
@ NSAPI_ICMP
Definition: nsapi_types.h:267
@ NSAPI_UDP
Definition: nsapi_types.h:266
@ NSAPI_TLSSOCKET_SET_HOSTNAME
Definition: nsapi_types.h:332
@ NSAPI_TLSSOCKET_SET_CLKEY
Definition: nsapi_types.h:335
@ NSAPI_TLSSOCKET_SET_CACERT
Definition: nsapi_types.h:333
@ NSAPI_TLSSOCKET_SET_CLCERT
Definition: nsapi_types.h:334
@ NSAPI_TLSSOCKET_ENABLE
Definition: nsapi_types.h:336
#define MBED_ALIGN(N)
MBED_ALIGN(N) Declare a variable to be aligned on an N-byte boundary.
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:237
nsapi_version_t version
IP version.
Definition: nsapi_types.h:243
uint8_t bytes[16]
IP address The raw bytes of the IP address stored in big-endian format.
Definition: nsapi_types.h:248
nsapi_ip_mreq structure
Definition: nsapi_types.h:390
nsapi_latency_req structure
Definition: nsapi_types.h:397
Header structure for control info.
Definition: nsapi_types.h:416
nsapi_pktinfo structure
Definition: nsapi_types.h:427
nsapi_stack_api structure
Definition: nsapi_types.h:442
nsapi_error_t(* socket_listen)(nsapi_stack_t *stack, nsapi_socket_t socket, int backlog)
Listen for connections on a TCP socket.
Definition: nsapi_types.h:565
nsapi_error_t(* gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version)
Translates a hostname to an IP address.
Definition: nsapi_types.h:466
nsapi_size_or_error_t(* socket_sendto)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size)
Send a packet over a UDP socket.
Definition: nsapi_types.h:659
nsapi_size_or_error_t(* socket_recv)(nsapi_stack_t *stack, nsapi_socket_t socket, void *data, nsapi_size_t size)
Receive data over a TCP socket.
Definition: nsapi_types.h:639
nsapi_error_t(* socket_accept)(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port)
Accepts a connection on a TCP socket.
Definition: nsapi_types.h:603
nsapi_error_t(* socket_close)(nsapi_stack_t *stack, nsapi_socket_t socket)
Close the socket.
Definition: nsapi_types.h:537
nsapi_error_t(* socket_open)(nsapi_stack_t *stack, nsapi_socket_t *socket, nsapi_protocol_t proto)
Opens a socket.
Definition: nsapi_types.h:524
nsapi_error_t(* socket_connect)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t addr, uint16_t port)
Connects TCP socket to a remote host.
Definition: nsapi_types.h:579
nsapi_addr_t(* get_ip_address)(nsapi_stack_t *stack)
Get the local IP address.
Definition: nsapi_types.h:449
nsapi_error_t(* add_dns_server)(nsapi_stack_t *stack, nsapi_addr_t addr)
Add a domain name server to list of servers to query.
Definition: nsapi_types.h:474
nsapi_size_or_error_t(* socket_recvfrom)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t *addr, uint16_t *port, void *buffer, nsapi_size_t size)
Receive a packet over a UDP socket.
Definition: nsapi_types.h:679
nsapi_error_t(* setstackopt)(nsapi_stack_t *stack, int level, int optname, const void *optval, unsigned optlen)
Set stack-specific stack options.
Definition: nsapi_types.h:490
nsapi_error_t(* socket_bind)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t addr, uint16_t port)
Bind a specific address to a socket.
Definition: nsapi_types.h:551
nsapi_error_t(* setsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen)
Set stack-specific socket options.
Definition: nsapi_types.h:725
nsapi_error_t(* getsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen)
Get stack-specific socket options.
Definition: nsapi_types.h:743
nsapi_error_t(* getstackopt)(nsapi_stack_t *stack, int level, int optname, void *optval, unsigned *optlen)
Get stack-specific stack options.
Definition: nsapi_types.h:507
void(* socket_attach)(nsapi_stack_t *stack, nsapi_socket_t socket, void(*callback)(void *), void *data)
Register a callback on state change of the socket.
Definition: nsapi_types.h:707
nsapi_size_or_error_t(* socket_send)(nsapi_stack_t *stack, nsapi_socket_t socket, const void *data, nsapi_size_t size)
Send data over a TCP socket.
Definition: nsapi_types.h:621
nsapi_stack structure
Definition: nsapi_types.h:371
const struct nsapi_stack_api * stack_api
Network stack operation table.
Definition: nsapi_types.h:378
void * stack
Opaque handle for network stacks.
Definition: nsapi_types.h:382
nsapi_stagger_req structure
Definition: nsapi_types.h:404
nsapi_wifi_ap structure
Definition: nsapi_types.h:358