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 hostname
200 *
201 * According to RFC 1034 [1], Section 3.1 "Name space specifications and
202 * terminology", 63 is the maximum size of a hostname. +1 for the string
203 * terminator.
204 *
205 * [1] https://www.rfc-editor.org/rfc/rfc1034
206 */
207#define NSAPI_HOSTNAME_SIZE 64
208
209/** Maximum size of MAC address representation
210 */
211#define NSAPI_MAC_SIZE 18
212
213/** Maximum number of bytes for MAC address
214 */
215#define NSAPI_MAC_BYTES 6
216
217/** Size of IPv4 representation
218 */
219#define NSAPI_IPv4_SIZE 16
220
221/** Number of bytes in IPv4 address
222 */
223#define NSAPI_IPv4_BYTES 4
224
225/** Size of IPv6 representation
226 */
227#define NSAPI_IPv6_SIZE 40
228
229/** Number of bytes in IPv6 address
230 */
231#define NSAPI_IPv6_BYTES 16
232
233/** Enum of IP address versions
234 *
235 * The IP version specifies the type of an IP address.
236 *
237 * @enum nsapi_version
238 */
239typedef enum nsapi_version {
240 NSAPI_UNSPEC, /*!< Address is unspecified */
241 NSAPI_IPv4, /*!< Address is IPv4 */
242 NSAPI_IPv6, /*!< Address is IPv6 */
243} nsapi_version_t;
244
245/** IP address structure for passing IP addresses by value
246 */
247typedef struct nsapi_addr {
248 /** IP version
249 * - NSAPI_IPv4
250 * - NSAPI_IPv6
251 * - NSAPI_UNSPEC
252 */
253 nsapi_version_t version;
254
255 /** IP address
256 * The raw bytes of the IP address stored in big-endian format
257 */
260
261
262/** Opaque handle for network sockets
263 */
264typedef void *nsapi_socket_t;
265
266
267/** Enum of socket protocols
268 *
269 * The socket protocol specifies a particular protocol to
270 * be used with a newly created socket.
271 *
272 * @enum nsapi_protocol
273 */
274typedef enum nsapi_protocol {
275 NSAPI_TCP, /*!< Socket is of TCP type */
276 NSAPI_UDP, /*!< Socket is of UDP type */
277 NSAPI_ICMP, /*!< Socket is of ICMP type */
278} nsapi_protocol_t;
279
280/** Enum of standardized stack option levels
281 * for use with NetworkStack::setstackopt and getstackopt.
282 *
283 * @enum nsapi_stack_level
284 */
285typedef enum nsapi_stack_level {
286 NSAPI_STACK = 5000, /*!< Stack option level - see nsapi_stack_option_t for options */
287} nsapi_stack_level_t;
288
289/** Enum of standardized stack option names for level NSAPI_STACK
290 * of NetworkStack::setstackopt and getstackopt.
291 *
292 * These options may not be supported on all stacks, in which
293 * case NSAPI_ERROR_UNSUPPORTED may be returned.
294 *
295 * @enum nsapi_stack_option
296 */
297typedef enum nsapi_stack_option {
298 NSAPI_IPV4_MRU, /*!< Sets/gets size of largest IPv4 fragmented datagram to reassemble */
299 NSAPI_IPV6_MRU, /*!< Sets/gets size of largest IPv6 fragmented datagram to reassemble */
300} nsapi_stack_option_t;
301
302/** Enum of standardized socket option levels
303 * for use with Socket::setsockopt and getsockopt.
304 *
305 * @enum nsapi_socket_level
306 */
307typedef enum nsapi_socket_level {
308 NSAPI_SOCKET = 7000, /*!< Socket option level - see nsapi_socket_option_t for options */
309} nsapi_socket_level_t;
310
311/** Enum of standardized socket option names for level NSAPI_SOCKET
312 * of Socket::setsockopt and getsockopt.
313 *
314 * These options may not be supported on all stacks, in which
315 * case NSAPI_ERROR_UNSUPPORTED may be returned.
316 *
317 * @enum nsapi_socket_option
318 */
320 NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */
321 NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */
322 NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */
323 NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */
324 NSAPI_LINGER, /*!< Keeps close from returning until queues empty */
325 NSAPI_SNDBUF, /*!< Sets send buffer size */
326 NSAPI_RCVBUF, /*!< Sets recv buffer size */
327 NSAPI_ADD_MEMBERSHIP, /*!< Add membership to multicast address */
328 NSAPI_DROP_MEMBERSHIP, /*!< Drop membership to multicast address */
329 NSAPI_BIND_TO_DEVICE, /*!< Bind socket network interface name*/
330 NSAPI_LATENCY, /*!< Read estimated latency to destination */
331 NSAPI_STAGGER, /*!< Read estimated stagger value to destination */
332 NSAPI_IPTOS, /*!< Set IP type of service to set specific precedence */
333 NSAPI_BROADCAST, /*!< Set broadcast flag for UDP socket */
334 NSAPI_PKTINFO /*!< Get additional information when using sendto_control/recvfrom_control */
335} nsapi_socket_option_t;
336
338 NSAPI_TLSSOCKET_LEVEL = 7099, /*!< TLSSocket option level - see nsapi_tlssocket_option_t for options*/
339} nsapi_tlssocket_level_t;
340
342 NSAPI_TLSSOCKET_SET_HOSTNAME, /*!< Set host name */
343 NSAPI_TLSSOCKET_SET_CACERT, /*!< Set server CA certificate */
344 NSAPI_TLSSOCKET_SET_CLCERT, /*!< Set client certificate */
345 NSAPI_TLSSOCKET_SET_CLKEY, /*!< Set client key */
346 NSAPI_TLSSOCKET_ENABLE /*!< Enable TLSSocket */
347} nsapi_tlssocket_option_t;
348
349/** Supported IP protocol versions of IP stack
350 *
351 * @enum nsapi_ip_stack
352 */
353typedef enum nsapi_ip_stack {
354 DEFAULT_STACK = 0,
355 IPV4_STACK,
356 IPV6_STACK,
357 IPV4V6_STACK
358} nsapi_ip_stack_t;
359
360/* Backwards compatibility - previously didn't distinguish stack and socket options */
361typedef nsapi_socket_level_t nsapi_level_t;
362typedef nsapi_socket_option_t nsapi_option_t;
363
364/** nsapi_wifi_ap structure
365 *
366 * Structure representing a WiFi Access Point
367 */
368typedef struct nsapi_wifi_ap {
369 char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
370 uint8_t bssid[6];
371 nsapi_security_t security;
372 int8_t rssi;
373 uint8_t channel;
375
376
377/** nsapi_stack structure
378 *
379 * Stack structure representing a specific instance of a stack.
380 */
381typedef struct nsapi_stack {
382 /** Network stack operation table
383 *
384 * Provides access to the underlying api of the stack. This is not
385 * flattened into the nsapi_stack to allow allocation in read-only
386 * memory.
387 */
389
390 /** Opaque handle for network stacks
391 */
392 void *stack;
393
394 // Internal nsapi buffer
395 unsigned _stack_buffer[16];
397
398/** nsapi_ip_mreq structure
399 */
400typedef struct nsapi_ip_mreq {
401 nsapi_addr_t imr_multiaddr; /* IP multicast address of group */
402 nsapi_addr_t imr_interface; /* local IP address of interface */
404
405/** nsapi_latency_req structure
406 */
407typedef struct nsapi_latency_req {
408 uint8_t addr[16]; /* [IN] Destination address to estimate latency */
409 uint32_t latency; /* [OUT] Latency value */
411
412/** nsapi_stagger_req structure
413 */
414typedef struct nsapi_stagger_req {
415 uint8_t addr[16]; /* [IN] Destination address to estimate stagger */
416 uint16_t data_amount; /* [IN] Amount of data to be sent in kilobytes */
417 uint16_t stagger_min; /* [OUT] Minimum stagger value in seconds */
418 uint16_t stagger_max; /* [OUT] Maximum stagger value in seconds */
419 uint16_t stagger_rand; /* [OUT] Randomized stagger value in seconds */
421
422/**
423 * Header structure for control info.
424 * See #MsgHeaderIterator.
425 */
426typedef struct nsapi_msghdr {
427 /* In C alignment can't be specified in the type declaration.
428 * Specifying it in the first member definition will affect whole structure.
429 */
430 MBED_ALIGN(max_align_t) nsapi_size_t len; /* Data byte count, including header */
431 int level; /* Originating protocol */
432 int type; /* Protocol-specific type */
434
435/** nsapi_pktinfo structure
436 */
437typedef struct nsapi_pktinfo {
438 nsapi_msghdr_t hdr; /* Header identifying the message control structure */
439 nsapi_addr_t ipi_addr; /* Address associated with the packet */
440 int ipi_ifindex; /* Interface associated with the packet */
441 void *network_interface; /* Network interface pointer*/
443
444/** nsapi_stack_api structure
445 *
446 * Common api structure for network stack operations. A network stack
447 * can provide a nsapi_stack_api structure filled out with the
448 * appropriate implementation.
449 *
450 * Unsupported operations can be left as null pointers.
451 */
452typedef struct nsapi_stack_api {
453 /** Get the local IP address
454 *
455 * <br> \c stack : Stack handle
456 *
457 * @return Local IP Address or null address if not connected
458 */
460
461 /** Translates a hostname to an IP address
462 *
463 * The hostname may be either a domain name or an IP address. If the
464 * hostname is an IP address, no network transactions will be performed.
465 *
466 * If no stack-specific DNS resolution is provided, the hostname
467 * will be resolve using a UDP socket on the stack.
468 *
469 * <br> \c stack : Stack handle
470 * <br> \c addr : Destination for the host IP address
471 * <br> \c host : Hostname to resolve
472 * <br> \c version : Address family
473 *
474 * @return 0 on success, negative error code on failure
475 */
476 nsapi_error_t (*gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version);
477
478 /** Add a domain name server to list of servers to query
479 *
480 * <br> \c addr : Destination for the host address
481 *
482 * @return 0 on success, negative error code on failure
483 */
485
486 /** Set stack-specific stack options
487 *
488 * The setstackopt allow an application to pass stack-specific hints
489 * to the underlying stack. For unsupported options,
490 * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified.
491 *
492 * <br> \c stack : Stack handle
493 * <br> \c level : Stack-specific protocol level
494 * <br> \c optname : Stack-specific option identifier
495 * <br> \c optval : Option value
496 * <br> \c optlen : Length of the option value
497 *
498 * @return 0 on success, negative error code on failure
499 */
501 int optname, const void *optval, unsigned optlen);
502
503 /** Get stack-specific stack options
504 *
505 * The getstackopt allow an application to retrieve stack-specific hints
506 * from the underlying stack. For unsupported options,
507 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
508 *
509 * <br> \c stack : Stack handle
510 * <br> \c level : Stack-specific protocol level
511 * <br> \c optname : Stack-specific option identifier
512 * <br> \c optval : Destination for option value
513 * <br> \c optlen : Length of the option value
514 *
515 * @return 0 on success, negative error code on failure
516 */
518 int optname, void *optval, unsigned *optlen);
519
520 /** Opens a socket
521 *
522 * Creates a network socket and stores it in the specified handle.
523 * The handle must be passed to following calls on the socket.
524 *
525 * A stack may have a finite number of sockets, in this case
526 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
527 *
528 * <br> \c stack : Stack context
529 * <br> \c socket : Destination for the handle to a newly created socket
530 * <br> \c proto : Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
531 *
532 * @return 0 on success, negative error code on failure
533 */
535 nsapi_protocol_t proto);
536
537 /** Close the socket
538 *
539 * Closes any open connection and deallocates any memory associated
540 * with the socket.
541 *
542 * <br> \c stack : Stack handle
543 * <br> \c socket : Socket handle
544 *
545 * @return 0 on success, negative error code on failure
546 */
548
549 /** Bind a specific address to a socket
550 *
551 * Binding a socket specifies the address and port on which to receive
552 * data. If the IP address is zeroed, only the port is bound.
553 *
554 * <br> \c stack : Stack handle
555 * <br> \c socket : Socket handle
556 * <br> \c addr : Local address to bind, may be null
557 * <br> \c port : Local port to bind
558 *
559 * @return 0 on success, negative error code on failure.
560 */
562 nsapi_addr_t addr, uint16_t port);
563
564 /** Listen for connections on a TCP socket
565 *
566 * Marks the socket as a passive socket that can be used to accept
567 * incoming connections.
568 *
569 * <br> \c stack : Stack handle
570 * <br> \c socket : Socket handle
571 * <br> \c backlog : Number of pending connections that can be queued simultaneously
572 *
573 * @return 0 on success, negative error code on failure
574 */
576
577 /** Connects TCP socket to a remote host
578 *
579 * Initiates a connection to a remote server specified by the
580 * indicated address.
581 *
582 * <br> \c stack : Stack handle
583 * <br> \c socket : Socket handle
584 * <br> \c addr : The address of the remote host
585 * <br> \c port : The port of the remote host
586 *
587 * @return 0 on success, negative error code on failure
588 */
590 nsapi_addr_t addr, uint16_t port);
591
592 /** Accepts a connection on a TCP socket
593 *
594 * The server socket must be bound and set to listen for connections.
595 * On a new connection, creates a network socket and stores it in the
596 * specified handle. The handle must be passed to following calls on
597 * the socket.
598 *
599 * A stack may have a finite number of sockets, in this case
600 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
601 *
602 * This call is non-blocking. If accept would block,
603 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
604 *
605 * <br> \c stack : Stack handle
606 * <br> \c server : Socket handle to server to accept from
607 * <br> \c socket : Destination for a handle to the newly created socket
608 * <br> \c addr : Destination for the address of the remote host
609 * <br> \c port : Destination for the port of the remote host
610 *
611 * @return 0 on success, negative error code on failure
612 */
614 nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port);
615
616 /** Send data over a TCP socket
617 *
618 * The socket must be connected to a remote host. Returns the number of
619 * bytes sent from the buffer.
620 *
621 * This call is non-blocking. If send would block,
622 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
623 *
624 * <br> \c stack : Stack handle
625 * <br> \c socket : Socket handle
626 * <br> \c data : Buffer of data to send to the host
627 * <br> \c size : Size of the buffer in bytes
628 *
629 * @return Number of sent bytes on success, negative error code on failure
630 */
632 const void *data, nsapi_size_t size);
633
634 /** Receive data over a TCP socket
635 *
636 * The socket must be connected to a remote host. Returns the number of
637 * bytes received into the buffer.
638 *
639 * This call is non-blocking. If recv would block,
640 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
641 *
642 * <br> \c stack : Stack handle
643 * <br> \c socket : Socket handle
644 * <br> \c data : Destination buffer for data received from the host
645 * <br> \c size : Size of the buffer in bytes
646 *
647 * @return Number of received bytes on success, negative error code on failure
648 */
650 void *data, nsapi_size_t size);
651
652 /** Send a packet over a UDP socket
653 *
654 * Sends data to the specified address. Returns the number of bytes
655 * sent from the buffer.
656 *
657 * This call is non-blocking. If sendto would block,
658 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
659 *
660 * <br> \c stack : Stack handle
661 * <br> \c socket : Socket handle
662 * <br> \c addr : The address of the remote host
663 * <br> \c port : The port of the remote host
664 * <br> \c data : Buffer of data to send to the host
665 * <br> \c size : Size of the buffer in bytes
666 *
667 * @return Number of sent bytes on success, negative error code on failure
668 */
670 nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size);
671
672 /** Receive a packet over a UDP socket
673 *
674 * Receives data and stores the source address in address if address
675 * is not NULL. Returns the number of bytes received into the buffer.
676 *
677 * This call is non-blocking. If recvfrom would block,
678 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
679 *
680 * <br> \c stack : Stack handle
681 * <br> \c socket : Socket handle
682 * <br> \c addr : Destination for the address of the remote host
683 * <br> \c port : Destination for the port of the remote host
684 * <br> \c data : Destination buffer for data received from the host
685 * <br> \c size : Size of the buffer in bytes
686 *
687 * @return Number of received bytes on success, negative error code on failure
688 */
690 nsapi_addr_t *addr, uint16_t *port, void *buffer, nsapi_size_t size);
691
692 // TODO: Documentation
693 nsapi_size_or_error_t (*socket_sendto_control)(nsapi_stack_t *stack, nsapi_socket_t socket,
694 nsapi_addr_t addr, uint16_t port,
695 const void *data, nsapi_size_t size,
696 const nsapi_msghdr_t *control, nsapi_size_t control_size);
697
698 nsapi_size_or_error_t (*socket_recvfrom_control)(nsapi_stack_t *stack, nsapi_socket_t socket,
699 nsapi_addr_t *addr, uint16_t *port,
700 void *data, nsapi_size_t size,
701 nsapi_msghdr_t *control, nsapi_size_t control_size);
702
703 /** Register a callback on state change of the socket
704 *
705 * The specified callback will be called on state changes such as when
706 * the socket can recv/send/accept successfully and on when an error
707 * occurs. The callback may also be called spuriously without reason.
708 *
709 * The callback may be called in an interrupt context and should not
710 * perform expensive operations such as recv/send calls.
711 *
712 * <br> \c stack : Stack handle
713 * <br> \c socket : Socket handle
714 * <br> \c callback : Function to call on state change
715 * <br> \c data : Argument to pass to callback
716 */
718 void (*callback)(void *), void *data);
719
720 /** Set stack-specific socket options
721 *
722 * The setsockopt allow an application to pass stack-specific hints
723 * to the underlying stack. For unsupported options,
724 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
725 *
726 * <br> \c stack : Stack handle
727 * <br> \c socket : Socket handle
728 * <br> \c level : Stack-specific protocol level
729 * <br> \c optname : Stack-specific option identifier
730 * <br> \c optval : Option value
731 * <br> \c optlen : Length of the option value
732 *
733 * @return 0 on success, negative error code on failure
734 */
736 int optname, const void *optval, unsigned optlen);
737
738 /** Get stack-specific socket options
739 *
740 * The getstackopt allow an application to retrieve stack-specific hints
741 * from the underlying stack. For unsupported options,
742 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
743 *
744 * <br> \c stack : Stack handle
745 * <br> \c socket : Socket handle
746 * <br> \c level : Stack-specific protocol level
747 * <br> \c optname : Stack-specific option identifier
748 * <br> \c optval : Destination for option value
749 * <br> \c optlen : Length of the option value
750 *
751 * @return 0 on success, negative error code on failure
752 */
754 int optname, void *optval, unsigned *optlen);
756
757
758#ifdef __cplusplus
759}
760#endif
761
762#endif
763
764/** @}*/
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:319
nsapi_ip_stack
Supported IP protocol versions of IP stack.
Definition: nsapi_types.h:353
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:285
nsapi_stack_option
Enum of standardized stack option names for level NSAPI_STACK of NetworkStack::setstackopt and getsta...
Definition: nsapi_types.h:297
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:307
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:337
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:239
nsapi_protocol
Enum of socket protocols.
Definition: nsapi_types.h:274
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:264
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:341
@ 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:321
@ NSAPI_REUSEADDR
Definition: nsapi_types.h:320
@ NSAPI_RCVBUF
Definition: nsapi_types.h:326
@ NSAPI_LINGER
Definition: nsapi_types.h:324
@ NSAPI_SNDBUF
Definition: nsapi_types.h:325
@ NSAPI_ADD_MEMBERSHIP
Definition: nsapi_types.h:327
@ NSAPI_STAGGER
Definition: nsapi_types.h:331
@ NSAPI_KEEPIDLE
Definition: nsapi_types.h:322
@ NSAPI_KEEPINTVL
Definition: nsapi_types.h:323
@ NSAPI_IPTOS
Definition: nsapi_types.h:332
@ NSAPI_LATENCY
Definition: nsapi_types.h:330
@ NSAPI_PKTINFO
Definition: nsapi_types.h:334
@ NSAPI_BIND_TO_DEVICE
Definition: nsapi_types.h:329
@ NSAPI_BROADCAST
Definition: nsapi_types.h:333
@ NSAPI_DROP_MEMBERSHIP
Definition: nsapi_types.h:328
@ 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:286
@ NSAPI_IPV6_MRU
Definition: nsapi_types.h:299
@ NSAPI_IPV4_MRU
Definition: nsapi_types.h:298
@ 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:308
@ NSAPI_TLSSOCKET_LEVEL
Definition: nsapi_types.h:338
@ 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:240
@ NSAPI_IPv4
Definition: nsapi_types.h:241
@ NSAPI_IPv6
Definition: nsapi_types.h:242
@ NSAPI_TCP
Definition: nsapi_types.h:275
@ NSAPI_ICMP
Definition: nsapi_types.h:277
@ NSAPI_UDP
Definition: nsapi_types.h:276
@ NSAPI_TLSSOCKET_SET_HOSTNAME
Definition: nsapi_types.h:342
@ NSAPI_TLSSOCKET_SET_CLKEY
Definition: nsapi_types.h:345
@ NSAPI_TLSSOCKET_SET_CACERT
Definition: nsapi_types.h:343
@ NSAPI_TLSSOCKET_SET_CLCERT
Definition: nsapi_types.h:344
@ NSAPI_TLSSOCKET_ENABLE
Definition: nsapi_types.h:346
#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:247
nsapi_version_t version
IP version.
Definition: nsapi_types.h:253
uint8_t bytes[16]
IP address The raw bytes of the IP address stored in big-endian format.
Definition: nsapi_types.h:258
nsapi_ip_mreq structure
Definition: nsapi_types.h:400
nsapi_latency_req structure
Definition: nsapi_types.h:407
Header structure for control info.
Definition: nsapi_types.h:426
nsapi_pktinfo structure
Definition: nsapi_types.h:437
nsapi_stack_api structure
Definition: nsapi_types.h:452
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:575
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:476
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:669
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:649
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:613
nsapi_error_t(* socket_close)(nsapi_stack_t *stack, nsapi_socket_t socket)
Close the socket.
Definition: nsapi_types.h:547
nsapi_error_t(* socket_open)(nsapi_stack_t *stack, nsapi_socket_t *socket, nsapi_protocol_t proto)
Opens a socket.
Definition: nsapi_types.h:534
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:589
nsapi_addr_t(* get_ip_address)(nsapi_stack_t *stack)
Get the local IP address.
Definition: nsapi_types.h:459
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:484
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:689
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:500
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:561
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:735
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:753
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:517
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:717
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:631
nsapi_stack structure
Definition: nsapi_types.h:381
const struct nsapi_stack_api * stack_api
Network stack operation table.
Definition: nsapi_types.h:388
void * stack
Opaque handle for network stacks.
Definition: nsapi_types.h:392
nsapi_stagger_req structure
Definition: nsapi_types.h:414
nsapi_wifi_ap structure
Definition: nsapi_types.h:368