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 * @brief Convert an nsapi_error value into a string corresponding to its name.
110 *
111 * For example, \c nsapi_strerror(NSAPI_ERROR_TIMEOUT) will return a char* pointing to the string
112 * constant "TIMEOUT"
113 *
114 * @param error Error code to operate on
115 *
116 * @return String corresponding to the error code, or nullptr if an invalid enum value is passed.
117 */
119
120/** Enum of connection status types
121 *
122 * Valid error codes have negative values.
123 *
124 * @enum nsapi_connection_status
125 */
127 NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */
128 NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */
129 NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */
130 NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */
131 NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED
132} nsapi_connection_status_t;
133
134
135/** Enum of event types
136 *
137 * Event callbacks are accompanied with an event-dependent parameter passed as an intptr_t.
138 *
139 * @enum nsapi_event
140 */
141typedef enum nsapi_event {
142 NSAPI_EVENT_CONNECTION_STATUS_CHANGE = 0, /*!< network connection status has changed, the parameter = new status (nsapi_connection_status_t) */
143 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 */
144 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 */
145} nsapi_event_t;
146
147
148/** Type used to represent error codes
149 *
150 * This is a separate type from enum nsapi_error to avoid breaking
151 * compatibility in type-sensitive overloads
152 */
153typedef signed int nsapi_error_t;
154
155/** Type used to represent the size of data passed through sockets
156 */
157typedef unsigned int nsapi_size_t;
158
159/** Type used to represent either a size or error passed through sockets
160 *
161 * A valid nsapi_size_or_error_t is either a non-negative size or a
162 * negative error code from the nsapi_error_t
163 */
164typedef signed int nsapi_size_or_error_t;
165
166/** Type used to represent either a value or error
167 *
168 * A valid nsapi_value_or_error_t is either a non-negative value or a
169 * negative error code from the nsapi_error_t
170 */
171typedef signed int nsapi_value_or_error_t;
172
173/** Enum of encryption types
174 *
175 * The security type specifies a particular security to use when
176 * connected to a WiFi network
177 */
178typedef enum nsapi_security {
179 NSAPI_SECURITY_NONE = 0x0, /*!< open access point */
180 NSAPI_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */
181 NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */
182 NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */
183 NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */
184 NSAPI_SECURITY_PAP = 0x5, /*!< phrase conforms to PPP authentication context */
185 NSAPI_SECURITY_CHAP = 0x6, /*!< phrase conforms to PPP authentication context */
186 NSAPI_SECURITY_EAP_TLS = 0x7, /*!< phrase conforms to EAP-TLS */
187 NSAPI_SECURITY_PEAP = 0x8, /*!< phrase conforms to PEAP */
188 NSAPI_SECURITY_WPA2_ENT = 0x9, /*!< phrase conforms to WPA2-AES and WPA-TKIP with enterprise security */
189 NSAPI_SECURITY_WPA3 = 0xA, /*!< phrase conforms to WPA3 */
190 NSAPI_SECURITY_WPA3_WPA2 = 0xB, /*!< phrase conforms to WPA3_WPA2 */
191 NSAPI_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */
193
194/**
195 * @brief Convert an \c nsapi_security_t to its (short) string name
196 *
197 * For example, \c nsapi_security_to_string(NSAPI_SECURITY_WPA3) returns a string constant
198 * pointing to the string "WPA3".
199 *
200 * @param sec Security type to operate on
201 *
202 * @return String value of the security type, or NULL if an unknown enum value is passed.
203 */
205
206/**
207 * @brief Convert the (short) string name of a wifi security type to \c the corresponding nsapi_security_t value.
208 *
209 * For example, \c nsapi_security_to_string("WPA3") returns \c NSAPI_SECURITY_WPA3
210 *
211 * @param str String to operate on
212 *
213 * @return Security enum value, or NSAPI_SECURITY_UNKNOWN if none match
214 */
216
217/** Size of 2 char network interface name from driver
218 */
219#define NSAPI_INTERFACE_PREFIX_SIZE 2
220
221/** Maximum size of network interface name
222 */
223#define NSAPI_INTERFACE_NAME_MAX_SIZE 6
224
225/** Maximum size of IP address representation
226 */
227#define NSAPI_IP_SIZE NSAPI_IPv6_SIZE
228
229/** Maximum number of bytes for IP address
230 */
231#define NSAPI_IP_BYTES NSAPI_IPv6_BYTES
232
233/** Maximum size of hostname
234 *
235 * According to RFC 1034 [1], Section 3.1 "Name space specifications and
236 * terminology", 63 is the maximum size of a hostname. +1 for the string
237 * terminator.
238 *
239 * [1] https://www.rfc-editor.org/rfc/rfc1034
240 */
241#define NSAPI_HOSTNAME_SIZE 64
242
243/** Maximum size of MAC address representation
244 */
245#define NSAPI_MAC_SIZE 18
246
247/** Maximum number of bytes for MAC address
248 */
249#define NSAPI_MAC_BYTES 6
250
251/** Size of IPv4 representation
252 */
253#define NSAPI_IPv4_SIZE 16
254
255/** Number of bytes in IPv4 address
256 */
257#define NSAPI_IPv4_BYTES 4
258
259/** Size of IPv6 representation
260 */
261#define NSAPI_IPv6_SIZE 40
262
263/** Number of bytes in IPv6 address
264 */
265#define NSAPI_IPv6_BYTES 16
266
267/** Enum of IP address versions
268 *
269 * The IP version specifies the type of an IP address.
270 *
271 * @enum nsapi_version
272 */
273typedef enum nsapi_version {
274 NSAPI_UNSPEC, /*!< Address is unspecified */
275 NSAPI_IPv4, /*!< Address is IPv4 */
276 NSAPI_IPv6, /*!< Address is IPv6 */
277} nsapi_version_t;
278
279/** IP address structure for passing IP addresses by value
280 */
281typedef struct nsapi_addr {
282 /** IP version
283 * - NSAPI_IPv4
284 * - NSAPI_IPv6
285 * - NSAPI_UNSPEC
286 */
287 nsapi_version_t version;
288
289 /** IP address
290 * The raw bytes of the IP address stored in big-endian format
291 */
294
295
296/** Opaque handle for network sockets
297 */
298typedef void *nsapi_socket_t;
299
300
301/** Enum of socket protocols
302 *
303 * The socket protocol specifies a particular protocol to
304 * be used with a newly created socket.
305 *
306 * @enum nsapi_protocol
307 */
308typedef enum nsapi_protocol {
309 NSAPI_TCP, /*!< Socket is of TCP type */
310 NSAPI_UDP, /*!< Socket is of UDP type */
311 NSAPI_ICMP, /*!< Socket is of ICMP type */
312} nsapi_protocol_t;
313
314/** Enum of standardized stack option levels
315 * for use with NetworkStack::setstackopt and getstackopt.
316 *
317 * @enum nsapi_stack_level
318 */
319typedef enum nsapi_stack_level {
320 NSAPI_STACK = 5000, /*!< Stack option level - see nsapi_stack_option_t for options */
321} nsapi_stack_level_t;
322
323/** Enum of standardized stack option names for level NSAPI_STACK
324 * of NetworkStack::setstackopt and getstackopt.
325 *
326 * These options may not be supported on all stacks, in which
327 * case NSAPI_ERROR_UNSUPPORTED may be returned.
328 *
329 * @enum nsapi_stack_option
330 */
331typedef enum nsapi_stack_option {
332 NSAPI_IPV4_MRU, /*!< Sets/gets size of largest IPv4 fragmented datagram to reassemble */
333 NSAPI_IPV6_MRU, /*!< Sets/gets size of largest IPv6 fragmented datagram to reassemble */
334} nsapi_stack_option_t;
335
336/** Enum of standardized socket option levels
337 * for use with Socket::setsockopt and getsockopt.
338 *
339 * @enum nsapi_socket_level
340 */
341typedef enum nsapi_socket_level {
342 NSAPI_SOCKET = 7000, /*!< Socket option level - see nsapi_socket_option_t for options */
343} nsapi_socket_level_t;
344
345/** Enum of standardized socket option names for level NSAPI_SOCKET
346 * of Socket::setsockopt and getsockopt.
347 *
348 * These options may not be supported on all stacks, in which
349 * case NSAPI_ERROR_UNSUPPORTED may be returned.
350 *
351 * @enum nsapi_socket_option
352 */
354 NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */
355 NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */
356 NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */
357 NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */
358 NSAPI_LINGER, /*!< Keeps close from returning until queues empty */
359 NSAPI_SNDBUF, /*!< Sets send buffer size */
360 NSAPI_RCVBUF, /*!< Sets recv buffer size */
361 NSAPI_ADD_MEMBERSHIP, /*!< Add membership to multicast address */
362 NSAPI_DROP_MEMBERSHIP, /*!< Drop membership to multicast address */
363 NSAPI_BIND_TO_DEVICE, /*!< Bind socket network interface name*/
364 NSAPI_LATENCY, /*!< Read estimated latency to destination */
365 NSAPI_STAGGER, /*!< Read estimated stagger value to destination */
366 NSAPI_IPTOS, /*!< Set IP type of service to set specific precedence */
367 NSAPI_BROADCAST, /*!< Set broadcast flag for UDP socket */
368 NSAPI_PKTINFO /*!< Get additional information when using sendto_control/recvfrom_control */
369} nsapi_socket_option_t;
370
372 NSAPI_TLSSOCKET_LEVEL = 7099, /*!< TLSSocket option level - see nsapi_tlssocket_option_t for options*/
373} nsapi_tlssocket_level_t;
374
376 NSAPI_TLSSOCKET_SET_HOSTNAME, /*!< Set host name */
377 NSAPI_TLSSOCKET_SET_CACERT, /*!< Set server CA certificate */
378 NSAPI_TLSSOCKET_SET_CLCERT, /*!< Set client certificate */
379 NSAPI_TLSSOCKET_SET_CLKEY, /*!< Set client key */
380 NSAPI_TLSSOCKET_ENABLE /*!< Enable TLSSocket */
381} nsapi_tlssocket_option_t;
382
383/** Supported IP protocol versions of IP stack
384 *
385 * @enum nsapi_ip_stack
386 */
387typedef enum nsapi_ip_stack {
388 DEFAULT_STACK = 0,
389 IPV4_STACK,
390 IPV6_STACK,
391 IPV4V6_STACK
392} nsapi_ip_stack_t;
393
394/* Backwards compatibility - previously didn't distinguish stack and socket options */
395typedef nsapi_socket_level_t nsapi_level_t;
396typedef nsapi_socket_option_t nsapi_option_t;
397
398/** nsapi_wifi_ap structure
399 *
400 * Structure representing a WiFi Access Point
401 */
402typedef struct nsapi_wifi_ap {
403 char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
404 uint8_t bssid[6];
405 nsapi_security_t security;
406 int8_t rssi;
407 uint8_t channel;
409
410
411/** nsapi_stack structure
412 *
413 * Stack structure representing a specific instance of a stack.
414 */
415typedef struct nsapi_stack {
416 /** Network stack operation table
417 *
418 * Provides access to the underlying api of the stack. This is not
419 * flattened into the nsapi_stack to allow allocation in read-only
420 * memory.
421 */
423
424 /** Opaque handle for network stacks
425 */
426 void *stack;
427
428 // Internal nsapi buffer
429 unsigned _stack_buffer[16];
431
432/** nsapi_ip_mreq structure
433 */
434typedef struct nsapi_ip_mreq {
435 nsapi_addr_t imr_multiaddr; /* IP multicast address of group */
436 nsapi_addr_t imr_interface; /* local IP address of interface */
438
439/** nsapi_latency_req structure
440 */
441typedef struct nsapi_latency_req {
442 uint8_t addr[16]; /* [IN] Destination address to estimate latency */
443 uint32_t latency; /* [OUT] Latency value */
445
446/** nsapi_stagger_req structure
447 */
448typedef struct nsapi_stagger_req {
449 uint8_t addr[16]; /* [IN] Destination address to estimate stagger */
450 uint16_t data_amount; /* [IN] Amount of data to be sent in kilobytes */
451 uint16_t stagger_min; /* [OUT] Minimum stagger value in seconds */
452 uint16_t stagger_max; /* [OUT] Maximum stagger value in seconds */
453 uint16_t stagger_rand; /* [OUT] Randomized stagger value in seconds */
455
456/**
457 * Header structure for control info.
458 * See #MsgHeaderIterator.
459 */
460typedef struct nsapi_msghdr {
461 /* In C alignment can't be specified in the type declaration.
462 * Specifying it in the first member definition will affect whole structure.
463 */
464 MBED_ALIGN(max_align_t) nsapi_size_t len; /* Data byte count, including header */
465 int level; /* Originating protocol */
466 int type; /* Protocol-specific type */
468
469/** nsapi_pktinfo structure
470 */
471typedef struct nsapi_pktinfo {
472 nsapi_msghdr_t hdr; /* Header identifying the message control structure */
473 nsapi_addr_t ipi_addr; /* Address associated with the packet */
474 int ipi_ifindex; /* Interface associated with the packet */
475 void *network_interface; /* Network interface pointer*/
477
478/** nsapi_stack_api structure
479 *
480 * Common api structure for network stack operations. A network stack
481 * can provide a nsapi_stack_api structure filled out with the
482 * appropriate implementation.
483 *
484 * Unsupported operations can be left as null pointers.
485 */
486typedef struct nsapi_stack_api {
487 /** Get the local IP address
488 *
489 * <br> \c stack : Stack handle
490 *
491 * @return Local IP Address or null address if not connected
492 */
494
495 /** Translates a hostname to an IP address
496 *
497 * The hostname may be either a domain name or an IP address. If the
498 * hostname is an IP address, no network transactions will be performed.
499 *
500 * If no stack-specific DNS resolution is provided, the hostname
501 * will be resolve using a UDP socket on the stack.
502 *
503 * <br> \c stack : Stack handle
504 * <br> \c addr : Destination for the host IP address
505 * <br> \c host : Hostname to resolve
506 * <br> \c version : Address family
507 *
508 * @return 0 on success, negative error code on failure
509 */
510 nsapi_error_t (*gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version);
511
512 /** Add a domain name server to list of servers to query
513 *
514 * <br> \c addr : Destination for the host address
515 *
516 * @return 0 on success, negative error code on failure
517 */
519
520 /** Set stack-specific stack options
521 *
522 * The setstackopt allow an application to pass stack-specific hints
523 * to the underlying stack. For unsupported options,
524 * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified.
525 *
526 * <br> \c stack : Stack handle
527 * <br> \c level : Stack-specific protocol level
528 * <br> \c optname : Stack-specific option identifier
529 * <br> \c optval : Option value
530 * <br> \c optlen : Length of the option value
531 *
532 * @return 0 on success, negative error code on failure
533 */
535 int optname, const void *optval, unsigned optlen);
536
537 /** Get stack-specific stack options
538 *
539 * The getstackopt allow an application to retrieve stack-specific hints
540 * from the underlying stack. For unsupported options,
541 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
542 *
543 * <br> \c stack : Stack handle
544 * <br> \c level : Stack-specific protocol level
545 * <br> \c optname : Stack-specific option identifier
546 * <br> \c optval : Destination for option value
547 * <br> \c optlen : Length of the option value
548 *
549 * @return 0 on success, negative error code on failure
550 */
552 int optname, void *optval, unsigned *optlen);
553
554 /** Opens a socket
555 *
556 * Creates a network socket and stores it in the specified handle.
557 * The handle must be passed to following calls on the socket.
558 *
559 * A stack may have a finite number of sockets, in this case
560 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
561 *
562 * <br> \c stack : Stack context
563 * <br> \c socket : Destination for the handle to a newly created socket
564 * <br> \c proto : Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
565 *
566 * @return 0 on success, negative error code on failure
567 */
569 nsapi_protocol_t proto);
570
571 /** Close the socket
572 *
573 * Closes any open connection and deallocates any memory associated
574 * with the socket.
575 *
576 * <br> \c stack : Stack handle
577 * <br> \c socket : Socket handle
578 *
579 * @return 0 on success, negative error code on failure
580 */
582
583 /** Bind a specific address to a socket
584 *
585 * Binding a socket specifies the address and port on which to receive
586 * data. If the IP address is zeroed, only the port is bound.
587 *
588 * <br> \c stack : Stack handle
589 * <br> \c socket : Socket handle
590 * <br> \c addr : Local address to bind, may be null
591 * <br> \c port : Local port to bind
592 *
593 * @return 0 on success, negative error code on failure.
594 */
596 nsapi_addr_t addr, uint16_t port);
597
598 /** Listen for connections on a TCP socket
599 *
600 * Marks the socket as a passive socket that can be used to accept
601 * incoming connections.
602 *
603 * <br> \c stack : Stack handle
604 * <br> \c socket : Socket handle
605 * <br> \c backlog : Number of pending connections that can be queued simultaneously
606 *
607 * @return 0 on success, negative error code on failure
608 */
610
611 /** Connects TCP socket to a remote host
612 *
613 * Initiates a connection to a remote server specified by the
614 * indicated address.
615 *
616 * <br> \c stack : Stack handle
617 * <br> \c socket : Socket handle
618 * <br> \c addr : The address of the remote host
619 * <br> \c port : The port of the remote host
620 *
621 * @return 0 on success, negative error code on failure
622 */
624 nsapi_addr_t addr, uint16_t port);
625
626 /** Accepts a connection on a TCP socket
627 *
628 * The server socket must be bound and set to listen for connections.
629 * On a new connection, creates a network socket and stores it in the
630 * specified handle. The handle must be passed to following calls on
631 * the socket.
632 *
633 * A stack may have a finite number of sockets, in this case
634 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
635 *
636 * This call is non-blocking. If accept would block,
637 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
638 *
639 * <br> \c stack : Stack handle
640 * <br> \c server : Socket handle to server to accept from
641 * <br> \c socket : Destination for a handle to the newly created socket
642 * <br> \c addr : Destination for the address of the remote host
643 * <br> \c port : Destination for the port of the remote host
644 *
645 * @return 0 on success, negative error code on failure
646 */
648 nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port);
649
650 /** Send data over a TCP socket
651 *
652 * The socket must be connected to a remote host. Returns the number of
653 * bytes sent from the buffer.
654 *
655 * This call is non-blocking. If send would block,
656 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
657 *
658 * <br> \c stack : Stack handle
659 * <br> \c socket : Socket handle
660 * <br> \c data : Buffer of data to send to the host
661 * <br> \c size : Size of the buffer in bytes
662 *
663 * @return Number of sent bytes on success, negative error code on failure
664 */
666 const void *data, nsapi_size_t size);
667
668 /** Receive data over a TCP socket
669 *
670 * The socket must be connected to a remote host. Returns the number of
671 * bytes received into the buffer.
672 *
673 * This call is non-blocking. If recv would block,
674 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
675 *
676 * <br> \c stack : Stack handle
677 * <br> \c socket : Socket handle
678 * <br> \c data : Destination buffer for data received from the host
679 * <br> \c size : Size of the buffer in bytes
680 *
681 * @return Number of received bytes on success, negative error code on failure
682 */
684 void *data, nsapi_size_t size);
685
686 /** Send a packet over a UDP socket
687 *
688 * Sends data to the specified address. Returns the number of bytes
689 * sent from the buffer.
690 *
691 * This call is non-blocking. If sendto would block,
692 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
693 *
694 * <br> \c stack : Stack handle
695 * <br> \c socket : Socket handle
696 * <br> \c addr : The address of the remote host
697 * <br> \c port : The port of the remote host
698 * <br> \c data : Buffer of data to send to the host
699 * <br> \c size : Size of the buffer in bytes
700 *
701 * @return Number of sent bytes on success, negative error code on failure
702 */
704 nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size);
705
706 /** Receive a packet over a UDP socket
707 *
708 * Receives data and stores the source address in address if address
709 * is not NULL. Returns the number of bytes received into the buffer.
710 *
711 * This call is non-blocking. If recvfrom would block,
712 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
713 *
714 * <br> \c stack : Stack handle
715 * <br> \c socket : Socket handle
716 * <br> \c addr : Destination for the address of the remote host
717 * <br> \c port : Destination for the port of the remote host
718 * <br> \c data : Destination buffer for data received from the host
719 * <br> \c size : Size of the buffer in bytes
720 *
721 * @return Number of received bytes on success, negative error code on failure
722 */
724 nsapi_addr_t *addr, uint16_t *port, void *buffer, nsapi_size_t size);
725
726 // TODO: Documentation
727 nsapi_size_or_error_t (*socket_sendto_control)(nsapi_stack_t *stack, nsapi_socket_t socket,
728 nsapi_addr_t addr, uint16_t port,
729 const void *data, nsapi_size_t size,
730 const nsapi_msghdr_t *control, nsapi_size_t control_size);
731
732 nsapi_size_or_error_t (*socket_recvfrom_control)(nsapi_stack_t *stack, nsapi_socket_t socket,
733 nsapi_addr_t *addr, uint16_t *port,
734 void *data, nsapi_size_t size,
735 nsapi_msghdr_t *control, nsapi_size_t control_size);
736
737 /** Register a callback on state change of the socket
738 *
739 * The specified callback will be called on state changes such as when
740 * the socket can recv/send/accept successfully and on when an error
741 * occurs. The callback may also be called spuriously without reason.
742 *
743 * The callback may be called in an interrupt context and should not
744 * perform expensive operations such as recv/send calls.
745 *
746 * <br> \c stack : Stack handle
747 * <br> \c socket : Socket handle
748 * <br> \c callback : Function to call on state change
749 * <br> \c data : Argument to pass to callback
750 */
752 void (*callback)(void *), void *data);
753
754 /** Set stack-specific socket options
755 *
756 * The setsockopt allow an application to pass stack-specific hints
757 * to the underlying stack. For unsupported options,
758 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
759 *
760 * <br> \c stack : Stack handle
761 * <br> \c socket : Socket handle
762 * <br> \c level : Stack-specific protocol level
763 * <br> \c optname : Stack-specific option identifier
764 * <br> \c optval : Option value
765 * <br> \c optlen : Length of the option value
766 *
767 * @return 0 on success, negative error code on failure
768 */
770 int optname, const void *optval, unsigned optlen);
771
772 /** Get stack-specific socket options
773 *
774 * The getstackopt allow an application to retrieve stack-specific hints
775 * from the underlying stack. For unsupported options,
776 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
777 *
778 * <br> \c stack : Stack handle
779 * <br> \c socket : Socket handle
780 * <br> \c level : Stack-specific protocol level
781 * <br> \c optname : Stack-specific option identifier
782 * <br> \c optval : Destination for option value
783 * <br> \c optlen : Length of the option value
784 *
785 * @return 0 on success, negative error code on failure
786 */
788 int optname, void *optval, unsigned *optlen);
790
791
792#ifdef __cplusplus
793}
794#endif
795
796#endif
797
798/** @}*/
enum nsapi_security nsapi_security_t
Enum of encryption types.
nsapi_security
Enum of encryption types.
nsapi_socket_option
Enum of standardized socket option names for level NSAPI_SOCKET of Socket::setsockopt and getsockopt.
nsapi_ip_stack
Supported IP protocol versions of IP stack.
nsapi_event
Enum of event types.
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
nsapi_stack_level
Enum of standardized stack option levels for use with NetworkStack::setstackopt and getstackopt.
nsapi_stack_option
Enum of standardized stack option names for level NSAPI_STACK of NetworkStack::setstackopt and getsta...
struct nsapi_pktinfo nsapi_pktinfo_t
nsapi_pktinfo structure
#define NSAPI_IP_BYTES
Maximum number of bytes for IP address.
char const * nsapi_strerror(enum nsapi_error error)
Convert an nsapi_error value into a string corresponding to its name.
char const * nsapi_security_to_string(nsapi_security_t sec)
Convert an nsapi_security_t to its (short) string name.
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.
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.
signed int nsapi_error_t
Type used to represent error codes.
nsapi_socket_level
Enum of standardized socket option levels for use with Socket::setsockopt and getsockopt.
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
struct nsapi_wifi_ap nsapi_wifi_ap_t
nsapi_wifi_ap structure
nsapi_security_t nsapi_string_to_security(char const *str)
Convert the (short) string name of a wifi security type to the corresponding nsapi_security_t value.
nsapi_error
Enum of standardized error codes.
Definition nsapi_types.h:84
nsapi_version
Enum of IP address versions.
nsapi_protocol
Enum of socket protocols.
void * nsapi_socket_t
Opaque handle for network sockets.
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.
nsapi_tlssocket_option
@ NSAPI_SECURITY_UNKNOWN
@ NSAPI_SECURITY_EAP_TLS
@ NSAPI_SECURITY_WPA2
@ NSAPI_SECURITY_NONE
@ NSAPI_SECURITY_WEP
@ NSAPI_SECURITY_WPA_WPA2
@ NSAPI_SECURITY_WPA3
@ NSAPI_SECURITY_WPA
@ NSAPI_SECURITY_WPA2_ENT
@ NSAPI_SECURITY_PAP
@ NSAPI_SECURITY_CHAP
@ NSAPI_SECURITY_PEAP
@ NSAPI_SECURITY_WPA3_WPA2
@ NSAPI_KEEPALIVE
@ NSAPI_REUSEADDR
@ NSAPI_RCVBUF
@ NSAPI_LINGER
@ NSAPI_SNDBUF
@ NSAPI_ADD_MEMBERSHIP
@ NSAPI_STAGGER
@ NSAPI_KEEPIDLE
@ NSAPI_KEEPINTVL
@ NSAPI_IPTOS
@ NSAPI_LATENCY
@ NSAPI_PKTINFO
@ NSAPI_BIND_TO_DEVICE
@ NSAPI_BROADCAST
@ NSAPI_DROP_MEMBERSHIP
@ NSAPI_EVENT_CELLULAR_STATUS_END
@ NSAPI_EVENT_CELLULAR_STATUS_BASE
@ NSAPI_EVENT_CONNECTION_STATUS_CHANGE
@ NSAPI_STACK
@ NSAPI_IPV6_MRU
@ NSAPI_IPV4_MRU
@ NSAPI_STATUS_CONNECTING
@ NSAPI_STATUS_DISCONNECTED
@ NSAPI_STATUS_LOCAL_UP
@ NSAPI_STATUS_GLOBAL_UP
@ NSAPI_SOCKET
@ NSAPI_TLSSOCKET_LEVEL
@ 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
@ NSAPI_ERROR_NO_SOCKET
Definition nsapi_types.h:90
@ NSAPI_ERROR_DNS_FAILURE
Definition nsapi_types.h:94
@ NSAPI_ERROR_IS_CONNECTED
@ NSAPI_ERROR_NO_ADDRESS
Definition nsapi_types.h:91
@ NSAPI_ERROR_DHCP_FAILURE
Definition nsapi_types.h:95
@ NSAPI_ERROR_ADDRESS_IN_USE
@ 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
@ NSAPI_ERROR_NO_SSID
Definition nsapi_types.h:93
@ NSAPI_ERROR_TIMEOUT
@ NSAPI_ERROR_ALREADY
Definition nsapi_types.h:99
@ NSAPI_ERROR_CONNECTION_TIMEOUT
@ NSAPI_ERROR_OK
Definition nsapi_types.h:85
@ NSAPI_ERROR_UNSUPPORTED
Definition nsapi_types.h:87
@ NSAPI_UNSPEC
@ NSAPI_IPv4
@ NSAPI_IPv6
@ NSAPI_TCP
@ NSAPI_ICMP
@ NSAPI_UDP
@ NSAPI_TLSSOCKET_SET_HOSTNAME
@ NSAPI_TLSSOCKET_SET_CLKEY
@ NSAPI_TLSSOCKET_SET_CACERT
@ NSAPI_TLSSOCKET_SET_CLCERT
@ NSAPI_TLSSOCKET_ENABLE
MBED_NORETURN void error(const char *format,...) MBED_PRINTF(1
To generate a fatal compile-time error, you can use the pre-processor error directive.
#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.
nsapi_version_t version
IP version.
uint8_t bytes[16]
IP address The raw bytes of the IP address stored in big-endian format.
nsapi_ip_mreq structure
nsapi_latency_req structure
Header structure for control info.
nsapi_pktinfo structure
nsapi_stack_api structure
nsapi_error_t(* socket_listen)(nsapi_stack_t *stack, nsapi_socket_t socket, int backlog)
Listen for connections on a TCP socket.
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.
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.
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.
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.
nsapi_error_t(* socket_close)(nsapi_stack_t *stack, nsapi_socket_t socket)
Close the socket.
nsapi_error_t(* socket_open)(nsapi_stack_t *stack, nsapi_socket_t *socket, nsapi_protocol_t proto)
Opens a socket.
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.
nsapi_addr_t(* get_ip_address)(nsapi_stack_t *stack)
Get the local IP address.
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.
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.
nsapi_error_t(* setstackopt)(nsapi_stack_t *stack, int level, int optname, const void *optval, unsigned optlen)
Set stack-specific stack options.
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.
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.
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.
nsapi_error_t(* getstackopt)(nsapi_stack_t *stack, int level, int optname, void *optval, unsigned *optlen)
Get stack-specific stack options.
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.
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.
nsapi_stack structure
const struct nsapi_stack_api * stack_api
Network stack operation table.
void * stack
Opaque handle for network stacks.
nsapi_stagger_req structure
nsapi_wifi_ap structure