LCOV - code coverage report
Current view: top level - zephyr/net - coap.h Coverage Total Hit
Test: new.info Lines: 98.4 % 127 125
Test Date: 2025-10-20 12:20:01

            Line data    Source code
       1            1 : /*
       2              :  * Copyright (c) 2018 Intel Corporation
       3              :  * Copyright (c) 2021 Nordic Semiconductor
       4              :  *
       5              :  * SPDX-License-Identifier: Apache-2.0
       6              :  */
       7              : 
       8              : /**
       9              :  * @file
      10              :  *
      11              :  * @brief CoAP implementation for Zephyr.
      12              :  */
      13              : 
      14              : #ifndef ZEPHYR_INCLUDE_NET_COAP_H_
      15              : #define ZEPHYR_INCLUDE_NET_COAP_H_
      16              : 
      17              : /**
      18              :  * @brief COAP library
      19              :  * @defgroup coap COAP Library
      20              :  * @since 1.10
      21              :  * @version 0.8.0
      22              :  * @ingroup networking
      23              :  * @{
      24              :  */
      25              : 
      26              : #include <zephyr/types.h>
      27              : #include <stddef.h>
      28              : #include <stdbool.h>
      29              : #include <zephyr/net/net_ip.h>
      30              : #include <zephyr/sys/math_extras.h>
      31              : #include <zephyr/sys/slist.h>
      32              : 
      33              : #ifdef __cplusplus
      34              : extern "C" {
      35              : #endif
      36              : 
      37              : /**
      38              :  * @brief Set of CoAP packet options we are aware of.
      39              :  *
      40              :  * Users may add options other than these to their packets, provided
      41              :  * they know how to format them correctly. The only restriction is
      42              :  * that all options must be added to a packet in numeric order.
      43              :  *
      44              :  * Refer to RFC 7252, section 12.2 for more information.
      45              :  */
      46            1 : enum coap_option_num {
      47              :         COAP_OPTION_IF_MATCH = 1,        /**< If-Match */
      48              :         COAP_OPTION_URI_HOST = 3,        /**< Uri-Host */
      49              :         COAP_OPTION_ETAG = 4,            /**< ETag */
      50              :         COAP_OPTION_IF_NONE_MATCH = 5,   /**< If-None-Match */
      51              :         COAP_OPTION_OBSERVE = 6,         /**< Observe (RFC 7641) */
      52              :         COAP_OPTION_URI_PORT = 7,        /**< Uri-Port */
      53              :         COAP_OPTION_LOCATION_PATH = 8,   /**< Location-Path */
      54              :         COAP_OPTION_URI_PATH = 11,       /**< Uri-Path */
      55              :         COAP_OPTION_CONTENT_FORMAT = 12, /**< Content-Format */
      56              :         COAP_OPTION_MAX_AGE = 14,        /**< Max-Age */
      57              :         COAP_OPTION_URI_QUERY = 15,      /**< Uri-Query */
      58              :         COAP_OPTION_ACCEPT = 17,         /**< Accept */
      59              :         COAP_OPTION_LOCATION_QUERY = 20, /**< Location-Query */
      60              :         COAP_OPTION_BLOCK2 = 23,         /**< Block2 (RFC 7959) */
      61              :         COAP_OPTION_BLOCK1 = 27,         /**< Block1 (RFC 7959) */
      62              :         COAP_OPTION_SIZE2 = 28,          /**< Size2 (RFC 7959) */
      63              :         COAP_OPTION_PROXY_URI = 35,      /**< Proxy-Uri */
      64              :         COAP_OPTION_PROXY_SCHEME = 39,   /**< Proxy-Scheme */
      65              :         COAP_OPTION_SIZE1 = 60,          /**< Size1 */
      66              :         COAP_OPTION_ECHO = 252,          /**< Echo (RFC 9175) */
      67              :         COAP_OPTION_NO_RESPONSE = 258,   /**< No-Response (RFC 7967) */
      68              :         COAP_OPTION_REQUEST_TAG = 292    /**< Request-Tag (RFC 9175) */
      69              : };
      70              : 
      71              : /**
      72              :  * @brief Available request methods.
      73              :  *
      74              :  * To be used when creating a request or a response.
      75              :  */
      76            1 : enum coap_method {
      77              :         COAP_METHOD_GET = 1,     /**< GET */
      78              :         COAP_METHOD_POST = 2,    /**< POST */
      79              :         COAP_METHOD_PUT = 3,     /**< PUT */
      80              :         COAP_METHOD_DELETE = 4,  /**< DELETE */
      81              :         COAP_METHOD_FETCH = 5,   /**< FETCH */
      82              :         COAP_METHOD_PATCH = 6,   /**< PATCH */
      83              :         COAP_METHOD_IPATCH = 7,  /**< IPATCH */
      84              : };
      85              : 
      86              : /** @cond INTERNAL_HIDDEN */
      87              : 
      88              : #define COAP_REQUEST_MASK 0x07
      89              : 
      90              : #define COAP_VERSION_1 1U
      91              : 
      92              : #define COAP_OBSERVE_MAX_AGE 0xFFFFFF
      93              : 
      94              : /** @endcond */
      95              : 
      96              : /**
      97              :  * @brief CoAP packets may be of one of these types.
      98              :  */
      99            1 : enum coap_msgtype {
     100              :         /**
     101              :          * Confirmable message.
     102              :          *
     103              :          * The packet is a request or response the destination end-point must
     104              :          * acknowledge.
     105              :          */
     106              :         COAP_TYPE_CON = 0,
     107              :         /**
     108              :          * Non-confirmable message.
     109              :          *
     110              :          * The packet is a request or response that doesn't
     111              :          * require acknowledgements.
     112              :          */
     113              :         COAP_TYPE_NON_CON = 1,
     114              :         /**
     115              :          * Acknowledge.
     116              :          *
     117              :          * Response to a confirmable message.
     118              :          */
     119              :         COAP_TYPE_ACK = 2,
     120              :         /**
     121              :          * Reset.
     122              :          *
     123              :          * Rejecting a packet for any reason is done by sending a message
     124              :          * of this type.
     125              :          */
     126              :         COAP_TYPE_RESET = 3
     127              : };
     128              : 
     129              : /**
     130              :  * Utility macro to create a CoAP response code.
     131              :  * @param class Class of the response code (ex. 2, 4, 5, ...)
     132              :  * @param det Detail of the response code
     133              :  * @return Response code literal
     134              :  */
     135            1 : #define COAP_MAKE_RESPONSE_CODE(class, det) ((class << 5) | (det))
     136              : 
     137              : /**
     138              :  * @brief Set of response codes available for a response packet.
     139              :  *
     140              :  * To be used when creating a response.
     141              :  */
     142            1 : enum coap_response_code {
     143              :         /** 2.00 - OK */
     144              :         COAP_RESPONSE_CODE_OK = COAP_MAKE_RESPONSE_CODE(2, 0),
     145              :         /** 2.01 - Created */
     146              :         COAP_RESPONSE_CODE_CREATED = COAP_MAKE_RESPONSE_CODE(2, 1),
     147              :         /** 2.02 - Deleted */
     148              :         COAP_RESPONSE_CODE_DELETED = COAP_MAKE_RESPONSE_CODE(2, 2),
     149              :         /** 2.03 - Valid */
     150              :         COAP_RESPONSE_CODE_VALID = COAP_MAKE_RESPONSE_CODE(2, 3),
     151              :         /** 2.04 - Changed */
     152              :         COAP_RESPONSE_CODE_CHANGED = COAP_MAKE_RESPONSE_CODE(2, 4),
     153              :         /** 2.05 - Content */
     154              :         COAP_RESPONSE_CODE_CONTENT = COAP_MAKE_RESPONSE_CODE(2, 5),
     155              :         /** 2.31 - Continue */
     156              :         COAP_RESPONSE_CODE_CONTINUE = COAP_MAKE_RESPONSE_CODE(2, 31),
     157              :         /** 4.00 - Bad Request */
     158              :         COAP_RESPONSE_CODE_BAD_REQUEST = COAP_MAKE_RESPONSE_CODE(4, 0),
     159              :         /** 4.01 - Unauthorized */
     160              :         COAP_RESPONSE_CODE_UNAUTHORIZED = COAP_MAKE_RESPONSE_CODE(4, 1),
     161              :         /** 4.02 - Bad Option */
     162              :         COAP_RESPONSE_CODE_BAD_OPTION = COAP_MAKE_RESPONSE_CODE(4, 2),
     163              :         /** 4.03 - Forbidden */
     164              :         COAP_RESPONSE_CODE_FORBIDDEN = COAP_MAKE_RESPONSE_CODE(4, 3),
     165              :         /** 4.04 - Not Found */
     166              :         COAP_RESPONSE_CODE_NOT_FOUND = COAP_MAKE_RESPONSE_CODE(4, 4),
     167              :         /** 4.05 - Method Not Allowed */
     168              :         COAP_RESPONSE_CODE_NOT_ALLOWED = COAP_MAKE_RESPONSE_CODE(4, 5),
     169              :         /** 4.06 - Not Acceptable */
     170              :         COAP_RESPONSE_CODE_NOT_ACCEPTABLE = COAP_MAKE_RESPONSE_CODE(4, 6),
     171              :         /** 4.08 - Request Entity Incomplete */
     172              :         COAP_RESPONSE_CODE_INCOMPLETE = COAP_MAKE_RESPONSE_CODE(4, 8),
     173              :         /** 4.12 - Precondition Failed */
     174              :         COAP_RESPONSE_CODE_CONFLICT = COAP_MAKE_RESPONSE_CODE(4, 9),
     175              :         /** 4.12 - Precondition Failed */
     176              :         COAP_RESPONSE_CODE_PRECONDITION_FAILED = COAP_MAKE_RESPONSE_CODE(4, 12),
     177              :         /** 4.13 - Request Entity Too Large */
     178              :         COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = COAP_MAKE_RESPONSE_CODE(4, 13),
     179              :         /** 4.15 - Unsupported Content-Format */
     180              :         COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT =
     181              :                                                 COAP_MAKE_RESPONSE_CODE(4, 15),
     182              :         /** 4.22 - Unprocessable Entity */
     183              :         COAP_RESPONSE_CODE_UNPROCESSABLE_ENTITY = COAP_MAKE_RESPONSE_CODE(4, 22),
     184              :         /** 4.29 - Too Many Requests */
     185              :         COAP_RESPONSE_CODE_TOO_MANY_REQUESTS = COAP_MAKE_RESPONSE_CODE(4, 29),
     186              :         /** 5.00 - Internal Server Error */
     187              :         COAP_RESPONSE_CODE_INTERNAL_ERROR = COAP_MAKE_RESPONSE_CODE(5, 0),
     188              :         /** 5.01 - Not Implemented */
     189              :         COAP_RESPONSE_CODE_NOT_IMPLEMENTED = COAP_MAKE_RESPONSE_CODE(5, 1),
     190              :         /** 5.02 - Bad Gateway */
     191              :         COAP_RESPONSE_CODE_BAD_GATEWAY = COAP_MAKE_RESPONSE_CODE(5, 2),
     192              :         /** 5.03 - Service Unavailable */
     193              :         COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = COAP_MAKE_RESPONSE_CODE(5, 3),
     194              :         /** 5.04 - Gateway Timeout */
     195              :         COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = COAP_MAKE_RESPONSE_CODE(5, 4),
     196              :         /** 5.05 - Proxying Not Supported */
     197              :         COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED =
     198              :                                                 COAP_MAKE_RESPONSE_CODE(5, 5)
     199              : };
     200              : 
     201              : /** @cond INTERNAL_HIDDEN */
     202              : 
     203              : #define COAP_CODE_EMPTY (0)
     204              : 
     205              : #define COAP_TOKEN_MAX_LEN 8UL
     206              : #define COAP_FIXED_HEADER_SIZE 4UL
     207              : 
     208              : /** @endcond */
     209              : 
     210              : /**
     211              :  * @brief Set of Content-Format option values for CoAP.
     212              :  *
     213              :  * To be used when encoding or decoding a Content-Format option.
     214              :  */
     215            1 : enum coap_content_format {
     216              :         COAP_CONTENT_FORMAT_TEXT_PLAIN = 0,             /**< text/plain;charset=utf-8 */
     217              :         COAP_CONTENT_FORMAT_APP_LINK_FORMAT = 40,       /**< application/link-format */
     218              :         COAP_CONTENT_FORMAT_APP_XML = 41,               /**< application/xml */
     219              :         COAP_CONTENT_FORMAT_APP_OCTET_STREAM = 42,      /**< application/octet-stream */
     220              :         COAP_CONTENT_FORMAT_APP_EXI = 47,               /**< application/exi */
     221              :         COAP_CONTENT_FORMAT_APP_JSON = 50,              /**< application/json */
     222              :         COAP_CONTENT_FORMAT_APP_JSON_PATCH_JSON = 51,   /**< application/json-patch+json */
     223              :         COAP_CONTENT_FORMAT_APP_MERGE_PATCH_JSON = 52,  /**< application/merge-patch+json */
     224              :         COAP_CONTENT_FORMAT_APP_CBOR = 60               /**< application/cbor */
     225              : };
     226              : 
     227              : /**
     228              :  * @brief Set of No-Response option values for CoAP.
     229              :  *
     230              :  * To be used when encoding or decoding a No-Response option defined
     231              :  * in RFC 7967.
     232              :  */
     233            0 : enum coap_no_response {
     234              :         COAP_NO_RESPONSE_SUPPRESS_2_XX = 0x02,
     235              :         COAP_NO_RESPONSE_SUPPRESS_4_XX = 0x08,
     236              :         COAP_NO_RESPONSE_SUPPRESS_5_XX = 0x10,
     237              : 
     238              :         COAP_NO_RESPONSE_SUPPRESS_ALL = COAP_NO_RESPONSE_SUPPRESS_2_XX |
     239              :                                         COAP_NO_RESPONSE_SUPPRESS_4_XX |
     240              :                                         COAP_NO_RESPONSE_SUPPRESS_5_XX,
     241              : };
     242              : 
     243              : /** @cond INTERNAL_HIDDEN */
     244              : 
     245              : /* block option helper */
     246              : #define GET_BLOCK_NUM(v)        ((v) >> 4)
     247              : #define GET_BLOCK_SIZE(v)       (((v) & 0x7))
     248              : #define GET_MORE(v)             (!!((v) & 0x08))
     249              : 
     250              : /** @endcond */
     251              : 
     252              : struct coap_observer;
     253              : struct coap_packet;
     254              : struct coap_pending;
     255              : struct coap_reply;
     256              : struct coap_resource;
     257              : 
     258              : /**
     259              :  * @typedef coap_method_t
     260              :  * @brief Type of the callback being called when a resource's method is
     261              :  * invoked by the remote entity.
     262              :  */
     263            1 : typedef int (*coap_method_t)(struct coap_resource *resource,
     264              :                              struct coap_packet *request,
     265              :                              struct sockaddr *addr, socklen_t addr_len);
     266              : 
     267              : /**
     268              :  * @typedef coap_notify_t
     269              :  * @brief Type of the callback being called when a resource's has observers
     270              :  * to be informed when an update happens.
     271              :  */
     272            1 : typedef void (*coap_notify_t)(struct coap_resource *resource,
     273              :                               struct coap_observer *observer);
     274              : 
     275              : /**
     276              :  * @brief Description of CoAP resource.
     277              :  *
     278              :  * CoAP servers often want to register resources, so that clients can act on
     279              :  * them, by fetching their state or requesting updates to them.
     280              :  */
     281            1 : struct coap_resource {
     282              :         /** Which function to be called for each CoAP method */
     283            0 :         coap_method_t get, post, put, del, fetch, patch, ipatch;
     284              :         /** Notify function to call */
     285            1 :         coap_notify_t notify;
     286              :         /** Resource path */
     287            1 :         const char * const *path;
     288              :         /** User specific opaque data */
     289            1 :         void *user_data;
     290              :         /** List of resource observers */
     291            1 :         sys_slist_t observers;
     292              :         /** Resource age */
     293            1 :         int age;
     294              : };
     295              : 
     296              : /**
     297              :  * @brief Represents a remote device that is observing a local resource.
     298              :  */
     299            1 : struct coap_observer {
     300              :         /** Observer list node */
     301            1 :         sys_snode_t list;
     302              :         /** Observer connection end point information */
     303            1 :         struct sockaddr addr;
     304              :         /** Observer token */
     305            1 :         uint8_t token[8];
     306              :         /** Extended token length */
     307            1 :         uint8_t tkl;
     308              : };
     309              : 
     310              : /**
     311              :  * @brief Representation of a CoAP Packet.
     312              :  */
     313            1 : struct coap_packet {
     314            1 :         uint8_t *data;    /**< User allocated buffer */
     315            1 :         uint16_t offset;  /**< CoAP lib maintains offset while adding data */
     316            1 :         uint16_t max_len; /**< Max CoAP packet data length */
     317            1 :         uint8_t hdr_len;  /**< CoAP header length */
     318            1 :         uint16_t opt_len; /**< Total options length (delta + len + value) */
     319            1 :         uint16_t delta;   /**< Used for delta calculation in CoAP packet */
     320              : #if defined(CONFIG_COAP_KEEP_USER_DATA) || defined(__DOXYGEN__)
     321              :         /**
     322              :          * Application specific user data.
     323              :          * @kconfig_dep{CONFIG_COAP_KEEP_USER_DATA}
     324              :          */
     325            1 :         void *user_data;
     326              : #endif
     327              : };
     328              : 
     329              : /**
     330              :  * @brief Representation of a CoAP option.
     331              :  */
     332            1 : struct coap_option {
     333            1 :         uint16_t delta;     /**< Option delta */
     334              : #if defined(CONFIG_COAP_EXTENDED_OPTIONS_LEN)
     335              :         uint16_t len;
     336              :         uint8_t value[CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE];
     337              : #else
     338            1 :         uint8_t len;        /**< Option length */
     339            1 :         uint8_t value[12];  /**< Option value */
     340              : #endif
     341              : };
     342              : 
     343              : /**
     344              :  * @typedef coap_reply_t
     345              :  * @brief Helper function to be called when a response matches the
     346              :  * a pending request.
     347              :  * When sending blocks, the callback is only executed when the
     348              :  * reply of the last block is received.
     349              :  * i.e. it is not called when the code of the reply is 'continue' (2.31).
     350              :  */
     351            1 : typedef int (*coap_reply_t)(const struct coap_packet *response,
     352              :                             struct coap_reply *reply,
     353              :                             const struct sockaddr *from);
     354              : 
     355              : /**
     356              :  * @brief CoAP transmission parameters.
     357              :  */
     358            1 : struct coap_transmission_parameters {
     359              :         /** Initial ACK timeout. Value is used as a base value to retry pending CoAP packets. */
     360            1 :         uint32_t ack_timeout;
     361              : #if defined(CONFIG_COAP_RANDOMIZE_ACK_TIMEOUT) || defined(__DOXYGEN__)
     362              :         /**
     363              :          * Set CoAP ack random factor. A value of 150 means a factor of 1.5. A value of 0 defaults
     364              :          * to @kconfig{CONFIG_COAP_ACK_RANDOM_PERCENT}. The value must be >= 100.
     365              :          */
     366            1 :         uint16_t ack_random_percent;
     367              : #endif /* defined(CONFIG_COAP_RANDOMIZE_ACK_TIMEOUT) */
     368              :         /** Set CoAP retry backoff factor. A value of 200 means a factor of 2.0. */
     369            1 :         uint16_t coap_backoff_percent;
     370              :         /** Maximum number of retransmissions. */
     371            1 :         uint8_t max_retransmission;
     372              : };
     373              : 
     374              : /**
     375              :  * @brief Represents a request awaiting for an acknowledgment (ACK).
     376              :  */
     377            1 : struct coap_pending {
     378            1 :         struct sockaddr addr; /**< Remote address */
     379            1 :         int64_t t0;           /**< Time when the request was sent */
     380            1 :         uint32_t timeout;     /**< Timeout in ms */
     381            1 :         uint16_t id;          /**< Message id */
     382            1 :         uint8_t *data;        /**< User allocated buffer */
     383            1 :         uint16_t len;         /**< Length of the CoAP packet */
     384            1 :         uint8_t retries;      /**< Number of times the request has been sent */
     385            1 :         struct coap_transmission_parameters params; /**< Transmission parameters */
     386              : };
     387              : 
     388              : /**
     389              :  * @brief Represents the handler for the reply of a request, it is
     390              :  * also used when observing resources.
     391              :  */
     392            1 : struct coap_reply {
     393              :         /** CoAP reply callback */
     394            1 :         coap_reply_t reply;
     395              :         /** User specific opaque data */
     396            1 :         void *user_data;
     397              :         /** Reply age */
     398            1 :         int age;
     399              :         /** Reply id */
     400            1 :         uint16_t id;
     401              :         /** Reply token */
     402            1 :         uint8_t token[8];
     403              :         /** Extended token length */
     404            1 :         uint8_t tkl;
     405              : };
     406              : 
     407              : /**
     408              :  * @brief Returns the version present in a CoAP packet.
     409              :  *
     410              :  * @param cpkt CoAP packet representation
     411              :  *
     412              :  * @return the CoAP version in packet
     413              :  */
     414            1 : uint8_t coap_header_get_version(const struct coap_packet *cpkt);
     415              : 
     416              : /**
     417              :  * @brief Returns the type of the CoAP packet.
     418              :  *
     419              :  * @param cpkt CoAP packet representation
     420              :  *
     421              :  * @return the type of the packet
     422              :  */
     423            1 : uint8_t coap_header_get_type(const struct coap_packet *cpkt);
     424              : 
     425              : /**
     426              :  * @brief Returns the token (if any) in the CoAP packet.
     427              :  *
     428              :  * @param cpkt CoAP packet representation
     429              :  * @param token Where to store the token, must point to a buffer containing
     430              :  *              at least COAP_TOKEN_MAX_LEN bytes
     431              :  *
     432              :  * @return Token length in the CoAP packet (0 - COAP_TOKEN_MAX_LEN).
     433              :  */
     434            1 : uint8_t coap_header_get_token(const struct coap_packet *cpkt, uint8_t *token);
     435              : 
     436              : /**
     437              :  * @brief Returns the code of the CoAP packet.
     438              :  *
     439              :  * @param cpkt CoAP packet representation
     440              :  *
     441              :  * @return the code present in the packet
     442              :  */
     443            1 : uint8_t coap_header_get_code(const struct coap_packet *cpkt);
     444              : 
     445              : /**
     446              :  * @brief Modifies the code of the CoAP packet.
     447              :  *
     448              :  * @param cpkt CoAP packet representation
     449              :  * @param code CoAP code
     450              :  * @return 0 on success, -EINVAL on failure
     451              :  */
     452            1 : int coap_header_set_code(const struct coap_packet *cpkt, uint8_t code);
     453              : 
     454              : /**
     455              :  * @brief Returns the message id associated with the CoAP packet.
     456              :  *
     457              :  * @param cpkt CoAP packet representation
     458              :  *
     459              :  * @return the message id present in the packet
     460              :  */
     461            1 : uint16_t coap_header_get_id(const struct coap_packet *cpkt);
     462              : 
     463              : /**
     464              :  * @brief Returns the data pointer and length of the CoAP packet.
     465              :  *
     466              :  * @param cpkt CoAP packet representation
     467              :  * @param len Total length of CoAP payload
     468              :  *
     469              :  * @return data pointer and length if payload exists
     470              :  *         NULL pointer and length set to 0 in case there is no payload
     471              :  */
     472            1 : const uint8_t *coap_packet_get_payload(const struct coap_packet *cpkt,
     473              :                                        uint16_t *len);
     474              : 
     475              : /**
     476              :  * @brief Verify if CoAP URI path matches with provided options.
     477              :  *
     478              :  * @param path Null-terminated array of strings.
     479              :  * @param options Parsed options from coap_packet_parse()
     480              :  * @param opt_num Number of options
     481              :  *
     482              :  * @return true if the CoAP URI path matches,
     483              :  *        false otherwise.
     484              :  */
     485            1 : bool coap_uri_path_match(const char * const *path,
     486              :                          struct coap_option *options,
     487              :                          uint8_t opt_num);
     488              : 
     489              : /**
     490              :  * @brief Parses the CoAP packet in data, validating it and
     491              :  * initializing @a cpkt. @a data must remain valid while @a cpkt is used.
     492              :  *
     493              :  * @param cpkt Packet to be initialized from received @a data.
     494              :  * @param data Data containing a CoAP packet, its @a data pointer is
     495              :  * positioned on the start of the CoAP packet.
     496              :  * @param len Length of the data
     497              :  * @param options Parse options and cache its details.
     498              :  * @param opt_num Number of options
     499              :  *
     500              :  * @retval 0 in case of success.
     501              :  * @retval -EINVAL in case of invalid input args.
     502              :  * @retval -EBADMSG in case of malformed coap packet header.
     503              :  * @retval -EILSEQ in case of malformed coap options.
     504              :  */
     505            1 : int coap_packet_parse(struct coap_packet *cpkt, uint8_t *data, uint16_t len,
     506              :                       struct coap_option *options, uint8_t opt_num);
     507              : 
     508              : /**
     509              :  * @brief Parses provided coap path (with/without query) or query and appends
     510              :  * that as options to the @a cpkt.
     511              :  *
     512              :  * @param cpkt Packet to append path and query options for.
     513              :  * @param path Null-terminated string of coap path, query or both.
     514              :  *
     515              :  * @retval 0 in case of success or negative in case of error.
     516              :  */
     517            1 : int coap_packet_set_path(struct coap_packet *cpkt, const char *path);
     518              : 
     519              : /**
     520              :  * @brief Creates a new CoAP Packet from input data.
     521              :  *
     522              :  * @param cpkt New packet to be initialized using the storage from @a data.
     523              :  * @param data Data that will contain a CoAP packet information
     524              :  * @param max_len Maximum allowable length of data
     525              :  * @param ver CoAP header version
     526              :  * @param type CoAP header type
     527              :  * @param token_len CoAP header token length
     528              :  * @param token CoAP header token
     529              :  * @param code CoAP header code
     530              :  * @param id CoAP header message id
     531              :  *
     532              :  * @return 0 in case of success or negative in case of error.
     533              :  */
     534            1 : int coap_packet_init(struct coap_packet *cpkt, uint8_t *data, uint16_t max_len,
     535              :                      uint8_t ver, uint8_t type, uint8_t token_len,
     536              :                      const uint8_t *token, uint8_t code, uint16_t id);
     537              : 
     538              : /**
     539              :  * @brief Create a new CoAP Acknowledgment message for given request.
     540              :  *
     541              :  * This function works like @ref coap_packet_init, filling CoAP header type,
     542              :  * CoAP header token, and CoAP header message id fields according to
     543              :  * acknowledgment rules.
     544              :  *
     545              :  * @param cpkt New packet to be initialized using the storage from @a data.
     546              :  * @param req CoAP request packet that is being acknowledged
     547              :  * @param data Data that will contain a CoAP packet information
     548              :  * @param max_len Maximum allowable length of data
     549              :  * @param code CoAP header code
     550              :  *
     551              :  * @return 0 in case of success or negative in case of error.
     552              :  */
     553            1 : int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req,
     554              :                   uint8_t *data, uint16_t max_len, uint8_t code);
     555              : 
     556              : /**
     557              :  * @brief Create a new CoAP Reset message for given request.
     558              :  *
     559              :  * This function works like @ref coap_packet_init, filling CoAP header type,
     560              :  * and CoAP header message id fields.
     561              :  *
     562              :  * @param cpkt New packet to be initialized using the storage from @a data.
     563              :  * @param req CoAP request packet that is being acknowledged
     564              :  * @param data Data that will contain a CoAP packet information
     565              :  * @param max_len Maximum allowable length of data
     566              :  *
     567              :  * @return 0 in case of success or negative in case of error.
     568              :  */
     569            1 : int coap_rst_init(struct coap_packet *cpkt, const struct coap_packet *req,
     570              :                   uint8_t *data, uint16_t max_len);
     571              : /**
     572              :  * @brief Returns a randomly generated array of 8 bytes, that can be
     573              :  * used as a message's token.
     574              :  *
     575              :  * @return a 8-byte pseudo-random token.
     576              :  */
     577            1 : uint8_t *coap_next_token(void);
     578              : 
     579              : /**
     580              :  * @brief Helper to generate message ids
     581              :  *
     582              :  * @return a new message id
     583              :  */
     584            1 : uint16_t coap_next_id(void);
     585              : 
     586              : /**
     587              :  * @brief Return the values associated with the option of value @a
     588              :  * code.
     589              :  *
     590              :  * @param cpkt CoAP packet representation
     591              :  * @param code Option number to look for
     592              :  * @param options Array of #coap_option where to store the value
     593              :  * of the options found
     594              :  * @param veclen Number of elements in the options array
     595              :  *
     596              :  * @return The number of options found in packet matching code,
     597              :  * negative on error.
     598              :  */
     599            1 : int coap_find_options(const struct coap_packet *cpkt, uint16_t code,
     600              :                       struct coap_option *options, uint16_t veclen);
     601              : 
     602              : /**
     603              :  * @brief Appends an option to the packet.
     604              :  *
     605              :  * Note: options can be added out of numeric order of their codes. But
     606              :  * it's more efficient to add them in order.
     607              :  *
     608              :  * @param cpkt Packet to be updated
     609              :  * @param code Option code to add to the packet, see #coap_option_num
     610              :  * @param value Pointer to the value of the option, will be copied to the
     611              :  * packet
     612              :  * @param len Size of the data to be added
     613              :  *
     614              :  * @return 0 in case of success or negative in case of error.
     615              :  */
     616            1 : int coap_packet_append_option(struct coap_packet *cpkt, uint16_t code,
     617              :                               const uint8_t *value, uint16_t len);
     618              : 
     619              : /**
     620              :  * @brief Remove an option from the packet.
     621              :  *
     622              :  * @param cpkt Packet to be updated
     623              :  * @param code Option code to remove from the packet, see #coap_option_num
     624              :  *
     625              :  * @return 0 in case of success or negative in case of error.
     626              :  */
     627            1 : int coap_packet_remove_option(struct coap_packet *cpkt, uint16_t code);
     628              : 
     629              : /**
     630              :  * @brief Converts an option to its integer representation.
     631              :  *
     632              :  * Assumes that the number is encoded in the network byte order in the
     633              :  * option.
     634              :  *
     635              :  * @param option Pointer to the option value, retrieved by
     636              :  * coap_find_options()
     637              :  *
     638              :  * @return The integer representation of the option
     639              :  */
     640            1 : unsigned int coap_option_value_to_int(const struct coap_option *option);
     641              : 
     642              : /**
     643              :  * @brief Appends an integer value option to the packet.
     644              :  *
     645              :  * The option must be added in numeric order of their codes, and the
     646              :  * least amount of bytes will be used to encode the value.
     647              :  *
     648              :  * @param cpkt Packet to be updated
     649              :  * @param code Option code to add to the packet, see #coap_option_num
     650              :  * @param val Integer value to be added
     651              :  *
     652              :  * @return 0 in case of success or negative in case of error.
     653              :  */
     654            1 : int coap_append_option_int(struct coap_packet *cpkt, uint16_t code,
     655              :                            unsigned int val);
     656              : 
     657              : /**
     658              :  * @brief Append payload marker to CoAP packet
     659              :  *
     660              :  * @param cpkt Packet to append the payload marker (0xFF)
     661              :  *
     662              :  * @return 0 in case of success or negative in case of error.
     663              :  */
     664            1 : int coap_packet_append_payload_marker(struct coap_packet *cpkt);
     665              : 
     666              : /**
     667              :  * @brief Append payload to CoAP packet
     668              :  *
     669              :  * @param cpkt Packet to append the payload
     670              :  * @param payload CoAP packet payload
     671              :  * @param payload_len CoAP packet payload len
     672              :  *
     673              :  * @return 0 in case of success or negative in case of error.
     674              :  */
     675            1 : int coap_packet_append_payload(struct coap_packet *cpkt, const uint8_t *payload,
     676              :                                uint16_t payload_len);
     677              : 
     678              : /**
     679              :  * @brief Check if a CoAP packet is a CoAP request.
     680              :  *
     681              :  * @param cpkt Packet to be checked.
     682              :  *
     683              :  * @return true if the packet is a request,
     684              :  *        false otherwise.
     685              :  */
     686            1 : bool coap_packet_is_request(const struct coap_packet *cpkt);
     687              : 
     688              : /**
     689              :  * @brief When a request is received, call the appropriate methods of
     690              :  * the matching resources.
     691              :  *
     692              :  * @param cpkt Packet received
     693              :  * @param resources Array of known resources
     694              :  * @param resources_len Number of resources in the array
     695              :  * @param options Parsed options from coap_packet_parse()
     696              :  * @param opt_num Number of options
     697              :  * @param addr Peer address
     698              :  * @param addr_len Peer address length
     699              :  *
     700              :  * @retval >= 0 in case of success.
     701              :  * @retval -ENOTSUP in case of invalid request code.
     702              :  * @retval -EPERM in case resource handler is not implemented.
     703              :  * @retval -ENOENT in case the resource is not found.
     704              :  */
     705            1 : int coap_handle_request_len(struct coap_packet *cpkt,
     706              :                             struct coap_resource *resources,
     707              :                             size_t resources_len,
     708              :                             struct coap_option *options,
     709              :                             uint8_t opt_num,
     710              :                             struct sockaddr *addr, socklen_t addr_len);
     711              : 
     712              : /**
     713              :  * @brief When a request is received, call the appropriate methods of
     714              :  * the matching resources.
     715              :  *
     716              :  * @param cpkt Packet received
     717              :  * @param resources Array of known resources (terminated with empty resource)
     718              :  * @param options Parsed options from coap_packet_parse()
     719              :  * @param opt_num Number of options
     720              :  * @param addr Peer address
     721              :  * @param addr_len Peer address length
     722              :  *
     723              :  * @retval >= 0 in case of success.
     724              :  * @retval -ENOTSUP in case of invalid request code.
     725              :  * @retval -EPERM in case resource handler is not implemented.
     726              :  * @retval -ENOENT in case the resource is not found.
     727              :  */
     728            1 : int coap_handle_request(struct coap_packet *cpkt,
     729              :                         struct coap_resource *resources,
     730              :                         struct coap_option *options,
     731              :                         uint8_t opt_num,
     732              :                         struct sockaddr *addr, socklen_t addr_len);
     733              : 
     734              : /**
     735              :  * Represents the size of each block that will be transferred using
     736              :  * block-wise transfers [RFC7959]:
     737              :  *
     738              :  * Each entry maps directly to the value that is used in the wire.
     739              :  *
     740              :  * https://tools.ietf.org/html/rfc7959
     741              :  */
     742            1 : enum coap_block_size {
     743              :         COAP_BLOCK_16,   /**< 16-byte block size */
     744              :         COAP_BLOCK_32,   /**< 32-byte block size */
     745              :         COAP_BLOCK_64,   /**< 64-byte block size */
     746              :         COAP_BLOCK_128,  /**< 128-byte block size */
     747              :         COAP_BLOCK_256,  /**< 256-byte block size */
     748              :         COAP_BLOCK_512,  /**< 512-byte block size */
     749              :         COAP_BLOCK_1024, /**< 1024-byte block size */
     750              : };
     751              : 
     752              : /**
     753              :  * @brief Helper for converting the enumeration to the size expressed
     754              :  * in bytes.
     755              :  *
     756              :  * @param block_size The block size to be converted
     757              :  *
     758              :  * @return The size in bytes that the block_size represents
     759              :  */
     760            1 : static inline uint16_t coap_block_size_to_bytes(
     761              :         enum coap_block_size block_size)
     762              : {
     763              :         return (1 << (block_size + 4));
     764              : }
     765              : 
     766              : /**
     767              :  * @brief Helper for converting block size in bytes to enumeration.
     768              :  *
     769              :  * NOTE: Only valid CoAP block sizes map correctly.
     770              :  *
     771              :  * @param bytes CoAP block size in bytes.
     772              :  * @return enum coap_block_size
     773              :  */
     774            1 : static inline enum coap_block_size coap_bytes_to_block_size(uint16_t bytes)
     775              : {
     776              :         int sz = u32_count_trailing_zeros(bytes) - 4;
     777              : 
     778              :         if (sz < COAP_BLOCK_16) {
     779              :                 return COAP_BLOCK_16;
     780              :         }
     781              :         if (sz > COAP_BLOCK_1024) {
     782              :                 return COAP_BLOCK_1024;
     783              :         }
     784              :         return (enum coap_block_size)sz;
     785              : }
     786              : 
     787              : /**
     788              :  * @brief Represents the current state of a block-wise transaction.
     789              :  */
     790            1 : struct coap_block_context {
     791              :         /** Total size of the block-wise transaction */
     792            1 :         size_t total_size;
     793              :         /** Current size of the block-wise transaction */
     794            1 :         size_t current;
     795              :         /** Block size */
     796            1 :         enum coap_block_size block_size;
     797              : };
     798              : 
     799              : /**
     800              :  * @brief Initializes the context of a block-wise transfer.
     801              :  *
     802              :  * @param ctx The context to be initialized
     803              :  * @param block_size The size of the block
     804              :  * @param total_size The total size of the transfer, if known
     805              :  *
     806              :  * @return 0 in case of success or negative in case of error.
     807              :  */
     808            1 : int coap_block_transfer_init(struct coap_block_context *ctx,
     809              :                              enum coap_block_size block_size,
     810              :                              size_t total_size);
     811              : 
     812              : /**
     813              :  * @brief Append BLOCK1 or BLOCK2 option to the packet.
     814              :  *
     815              :  * If the CoAP packet is a request then BLOCK1 is appended
     816              :  * otherwise BLOCK2 is appended.
     817              :  *
     818              :  * @param cpkt Packet to be updated
     819              :  * @param ctx Block context from which to retrieve the
     820              :  * information for the block option
     821              :  *
     822              :  * @return 0 in case of success or negative in case of error.
     823              :  */
     824            1 : int coap_append_descriptive_block_option(struct coap_packet *cpkt, struct coap_block_context *ctx);
     825              : 
     826              : /**
     827              :  * @brief Check if a descriptive block option is set in the packet.
     828              :  *
     829              :  * If the CoAP packet is a request then an available BLOCK1 option
     830              :  * would be checked otherwise a BLOCK2 option would be checked.
     831              :  *
     832              :  * @param cpkt Packet to be checked.
     833              :  *
     834              :  * @return true if the corresponding block option is set,
     835              :  *        false otherwise.
     836              :  */
     837            1 : bool coap_has_descriptive_block_option(struct coap_packet *cpkt);
     838              : 
     839              : /**
     840              :  * @brief Remove BLOCK1 or BLOCK2 option from the packet.
     841              :  *
     842              :  * If the CoAP packet is a request then BLOCK1 is removed
     843              :  * otherwise BLOCK2 is removed.
     844              :  *
     845              :  * @param cpkt Packet to be updated.
     846              :  *
     847              :  * @return 0 in case of success or negative in case of error.
     848              :  */
     849            1 : int coap_remove_descriptive_block_option(struct coap_packet *cpkt);
     850              : 
     851              : /**
     852              :  * @brief Check if BLOCK1 or BLOCK2 option has more flag set
     853              :  *
     854              :  * @param cpkt Packet to be checked.
     855              :  * @return true If more flag is set in BLOCK1 or BLOCK2
     856              :  * @return false If MORE flag is not set or BLOCK header not found.
     857              :  */
     858            1 : bool coap_block_has_more(struct coap_packet *cpkt);
     859              : 
     860              : /**
     861              :  * @brief Append BLOCK1 option to the packet.
     862              :  *
     863              :  * @param cpkt Packet to be updated
     864              :  * @param ctx Block context from which to retrieve the
     865              :  * information for the Block1 option
     866              :  *
     867              :  * @return 0 in case of success or negative in case of error.
     868              :  */
     869            1 : int coap_append_block1_option(struct coap_packet *cpkt,
     870              :                               struct coap_block_context *ctx);
     871              : 
     872              : /**
     873              :  * @brief Append BLOCK2 option to the packet.
     874              :  *
     875              :  * @param cpkt Packet to be updated
     876              :  * @param ctx Block context from which to retrieve the
     877              :  * information for the Block2 option
     878              :  *
     879              :  * @return 0 in case of success or negative in case of error.
     880              :  */
     881            1 : int coap_append_block2_option(struct coap_packet *cpkt,
     882              :                               struct coap_block_context *ctx);
     883              : 
     884              : /**
     885              :  * @brief Append SIZE1 option to the packet.
     886              :  *
     887              :  * @param cpkt Packet to be updated
     888              :  * @param ctx Block context from which to retrieve the
     889              :  * information for the Size1 option
     890              :  *
     891              :  * @return 0 in case of success or negative in case of error.
     892              :  */
     893            1 : int coap_append_size1_option(struct coap_packet *cpkt,
     894              :                              struct coap_block_context *ctx);
     895              : 
     896              : /**
     897              :  * @brief Append SIZE2 option to the packet.
     898              :  *
     899              :  * @param cpkt Packet to be updated
     900              :  * @param ctx Block context from which to retrieve the
     901              :  * information for the Size2 option
     902              :  *
     903              :  * @return 0 in case of success or negative in case of error.
     904              :  */
     905            1 : int coap_append_size2_option(struct coap_packet *cpkt,
     906              :                              struct coap_block_context *ctx);
     907              : 
     908              : /**
     909              :  * @brief Get the integer representation of a CoAP option.
     910              :  *
     911              :  * @param cpkt Packet to be inspected
     912              :  * @param code CoAP option code
     913              :  *
     914              :  * @return Integer value >= 0 in case of success or negative in case
     915              :  * of error.
     916              :  */
     917            1 : int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code);
     918              : 
     919              : /**
     920              :  * @brief Get the block size, more flag and block number from the
     921              :  * CoAP block1 option.
     922              :  *
     923              :  * @param cpkt Packet to be inspected
     924              :  * @param has_more Is set to the value of the more flag
     925              :  * @param block_number Is set to the number of the block
     926              :  *
     927              :  * @return Integer value of the block size in case of success
     928              :  * or negative in case of error.
     929              :  */
     930            1 : int coap_get_block1_option(const struct coap_packet *cpkt, bool *has_more, uint32_t *block_number);
     931              : 
     932              : /**
     933              :  * @brief Get values from CoAP block2 option.
     934              :  *
     935              :  * Decode block number, more flag and block size from option.
     936              :  *
     937              :  * @param cpkt Packet to be inspected
     938              :  * @param has_more Is set to the value of the more flag
     939              :  * @param block_number Is set to the number of the block
     940              :  *
     941              :  * @return Integer value of the block size in case of success
     942              :  * or negative in case of error.
     943              :  */
     944            1 : int coap_get_block2_option(const struct coap_packet *cpkt, bool *has_more,
     945              :                            uint32_t *block_number);
     946              : 
     947              : /**
     948              :  * @brief Retrieves BLOCK{1,2} and SIZE{1,2} from @a cpkt and updates
     949              :  * @a ctx accordingly.
     950              :  *
     951              :  * @param cpkt Packet in which to look for block-wise transfers options
     952              :  * @param ctx Block context to be updated
     953              :  *
     954              :  * @return 0 in case of success or negative in case of error.
     955              :  */
     956            1 : int coap_update_from_block(const struct coap_packet *cpkt,
     957              :                            struct coap_block_context *ctx);
     958              : 
     959              : /**
     960              :  * @brief Updates @a ctx according to @a option set in @a cpkt
     961              :  * so after this is called the current entry indicates the correct
     962              :  * offset in the body of data being transferred.
     963              :  *
     964              :  * @param cpkt Packet in which to look for block-wise transfers options
     965              :  * @param ctx Block context to be updated
     966              :  * @param option Either COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2
     967              :  *
     968              :  * @return The offset in the block-wise transfer, 0 if the transfer
     969              :  * has finished or a negative value in case of an error.
     970              :  */
     971            1 : int coap_next_block_for_option(const struct coap_packet *cpkt,
     972              :                                struct coap_block_context *ctx,
     973              :                                enum coap_option_num option);
     974              : 
     975              : /**
     976              :  * @brief Updates @a ctx so after this is called the current entry
     977              :  * indicates the correct offset in the body of data being
     978              :  * transferred.
     979              :  *
     980              :  * @param cpkt Packet in which to look for block-wise transfers options
     981              :  * @param ctx Block context to be updated
     982              :  *
     983              :  * @return The offset in the block-wise transfer, 0 if the transfer
     984              :  * has finished.
     985              :  */
     986            1 : size_t coap_next_block(const struct coap_packet *cpkt,
     987              :                        struct coap_block_context *ctx);
     988              : 
     989              : /**
     990              :  * @brief Indicates that the remote device referenced by @a addr, with
     991              :  * @a request, wants to observe a resource.
     992              :  *
     993              :  * @param observer Observer to be initialized
     994              :  * @param request Request on which the observer will be based
     995              :  * @param addr Address of the remote device
     996              :  */
     997            1 : void coap_observer_init(struct coap_observer *observer,
     998              :                         const struct coap_packet *request,
     999              :                         const struct sockaddr *addr);
    1000              : 
    1001              : /**
    1002              :  * @brief After the observer is initialized, associate the observer
    1003              :  * with an resource.
    1004              :  *
    1005              :  * @param resource Resource to add an observer
    1006              :  * @param observer Observer to be added
    1007              :  *
    1008              :  * @return true if this is the first observer added to this resource.
    1009              :  */
    1010            1 : bool coap_register_observer(struct coap_resource *resource,
    1011              :                             struct coap_observer *observer);
    1012              : 
    1013              : /**
    1014              :  * @brief Remove this observer from the list of registered observers
    1015              :  * of that resource.
    1016              :  *
    1017              :  * @param resource Resource in which to remove the observer
    1018              :  * @param observer Observer to be removed
    1019              :  *
    1020              :  * @return true if the observer was found and removed.
    1021              :  */
    1022            1 : bool coap_remove_observer(struct coap_resource *resource,
    1023              :                           struct coap_observer *observer);
    1024              : 
    1025              : /**
    1026              :  * @brief Returns the observer that matches address @a addr
    1027              :  * and has token @a token.
    1028              :  *
    1029              :  * @param observers Pointer to the array of observers
    1030              :  * @param len Size of the array of observers
    1031              :  * @param addr Address of the endpoint observing a resource
    1032              :  * @param token Pointer to the token
    1033              :  * @param token_len Length of valid bytes in the token
    1034              :  *
    1035              :  * @return A pointer to a observer if a match is found, NULL
    1036              :  * otherwise.
    1037              :  */
    1038            1 : struct coap_observer *coap_find_observer(
    1039              :         struct coap_observer *observers, size_t len,
    1040              :         const struct sockaddr *addr,
    1041              :         const uint8_t *token, uint8_t token_len);
    1042              : 
    1043              : /**
    1044              :  * @brief Returns the observer that matches address @a addr.
    1045              :  *
    1046              :  * @param observers Pointer to the array of observers
    1047              :  * @param len Size of the array of observers
    1048              :  * @param addr Address of the endpoint observing a resource
    1049              :  *
    1050              :  * @note The function coap_find_observer() should be preferred
    1051              :  * if both the observer's address and token are known.
    1052              :  *
    1053              :  * @return A pointer to a observer if a match is found, NULL
    1054              :  * otherwise.
    1055              :  */
    1056            1 : struct coap_observer *coap_find_observer_by_addr(
    1057              :         struct coap_observer *observers, size_t len,
    1058              :         const struct sockaddr *addr);
    1059              : 
    1060              : /**
    1061              :  * @brief Returns the observer that has token @a token.
    1062              :  *
    1063              :  * @param observers Pointer to the array of observers
    1064              :  * @param len Size of the array of observers
    1065              :  * @param token Pointer to the token
    1066              :  * @param token_len Length of valid bytes in the token
    1067              :  *
    1068              :  * @note The function coap_find_observer() should be preferred
    1069              :  * if both the observer's address and token are known.
    1070              :  *
    1071              :  * @return A pointer to a observer if a match is found, NULL
    1072              :  * otherwise.
    1073              :  */
    1074            1 : struct coap_observer *coap_find_observer_by_token(
    1075              :         struct coap_observer *observers, size_t len,
    1076              :         const uint8_t *token, uint8_t token_len);
    1077              : 
    1078              : /**
    1079              :  * @brief Returns the next available observer representation.
    1080              :  *
    1081              :  * @param observers Pointer to the array of observers
    1082              :  * @param len Size of the array of observers
    1083              :  *
    1084              :  * @return A pointer to a observer if there's an available observer,
    1085              :  * NULL otherwise.
    1086              :  */
    1087            1 : struct coap_observer *coap_observer_next_unused(
    1088              :         struct coap_observer *observers, size_t len);
    1089              : 
    1090              : /**
    1091              :  * @brief Indicates that a reply is expected for @a request.
    1092              :  *
    1093              :  * @param reply Reply structure to be initialized
    1094              :  * @param request Request from which @a reply will be based
    1095              :  */
    1096            1 : void coap_reply_init(struct coap_reply *reply,
    1097              :                      const struct coap_packet *request);
    1098              : 
    1099              : /**
    1100              :  * @brief Initialize a pending request with a request.
    1101              :  *
    1102              :  * The request's fields are copied into the pending struct, so @a
    1103              :  * request doesn't have to live for as long as the pending struct
    1104              :  * lives, but "data" that needs to live for at least that long.
    1105              :  *
    1106              :  * @param pending Structure representing the waiting for a
    1107              :  * confirmation message, initialized with data from @a request
    1108              :  * @param request Message waiting for confirmation
    1109              :  * @param addr Address to send the retransmission
    1110              :  * @param params Pointer to the CoAP transmission parameters struct,
    1111              :  * or NULL to use default values
    1112              :  *
    1113              :  * @return 0 in case of success or negative in case of error.
    1114              :  */
    1115            1 : int coap_pending_init(struct coap_pending *pending,
    1116              :                       const struct coap_packet *request,
    1117              :                       const struct sockaddr *addr,
    1118              :                       const struct coap_transmission_parameters *params);
    1119              : 
    1120              : /**
    1121              :  * @brief Returns the next available pending struct, that can be used
    1122              :  * to track the retransmission status of a request.
    1123              :  *
    1124              :  * @param pendings Pointer to the array of #coap_pending structures
    1125              :  * @param len Size of the array of #coap_pending structures
    1126              :  *
    1127              :  * @return pointer to a free #coap_pending structure, NULL in case
    1128              :  * none could be found.
    1129              :  */
    1130            1 : struct coap_pending *coap_pending_next_unused(
    1131              :         struct coap_pending *pendings, size_t len);
    1132              : 
    1133              : /**
    1134              :  * @brief Returns the next available reply struct, so it can be used
    1135              :  * to track replies and notifications received.
    1136              :  *
    1137              :  * @param replies Pointer to the array of #coap_reply structures
    1138              :  * @param len Size of the array of #coap_reply structures
    1139              :  *
    1140              :  * @return pointer to a free #coap_reply structure, NULL in case
    1141              :  * none could be found.
    1142              :  */
    1143            1 : struct coap_reply *coap_reply_next_unused(
    1144              :         struct coap_reply *replies, size_t len);
    1145              : 
    1146              : /**
    1147              :  * @brief After a response is received, returns if there is any
    1148              :  * matching pending request exits. User has to clear all pending
    1149              :  * retransmissions related to that response by calling
    1150              :  * coap_pending_clear().
    1151              :  *
    1152              :  * @param response The received response
    1153              :  * @param pendings Pointer to the array of #coap_reply structures
    1154              :  * @param len Size of the array of #coap_reply structures
    1155              :  *
    1156              :  * @return pointer to the associated #coap_pending structure, NULL in
    1157              :  * case none could be found.
    1158              :  */
    1159            1 : struct coap_pending *coap_pending_received(
    1160              :         const struct coap_packet *response,
    1161              :         struct coap_pending *pendings, size_t len);
    1162              : 
    1163              : /**
    1164              :  * @brief After a response is received, call coap_reply_t handler
    1165              :  * registered in #coap_reply structure
    1166              :  *
    1167              :  * @param response A response received
    1168              :  * @param from Address from which the response was received
    1169              :  * @param replies Pointer to the array of #coap_reply structures
    1170              :  * @param len Size of the array of #coap_reply structures
    1171              :  *
    1172              :  * @return Pointer to the reply matching the packet received, NULL if
    1173              :  * none could be found.
    1174              :  */
    1175            1 : struct coap_reply *coap_response_received(
    1176              :         const struct coap_packet *response,
    1177              :         const struct sockaddr *from,
    1178              :         struct coap_reply *replies, size_t len);
    1179              : 
    1180              : /**
    1181              :  * @brief Returns the next pending about to expire, pending->timeout
    1182              :  * informs how many ms to next expiration.
    1183              :  *
    1184              :  * @param pendings Pointer to the array of #coap_pending structures
    1185              :  * @param len Size of the array of #coap_pending structures
    1186              :  *
    1187              :  * @return The next #coap_pending to expire, NULL if none is about to
    1188              :  * expire.
    1189              :  */
    1190            1 : struct coap_pending *coap_pending_next_to_expire(
    1191              :         struct coap_pending *pendings, size_t len);
    1192              : 
    1193              : /**
    1194              :  * @brief After a request is sent, user may want to cycle the pending
    1195              :  * retransmission so the timeout is updated.
    1196              :  *
    1197              :  * @param pending Pending representation to have its timeout updated
    1198              :  *
    1199              :  * @return false if this is the last retransmission.
    1200              :  */
    1201            1 : bool coap_pending_cycle(struct coap_pending *pending);
    1202              : 
    1203              : /**
    1204              :  * @brief Cancels the pending retransmission, so it again becomes
    1205              :  * available.
    1206              :  *
    1207              :  * @param pending Pending representation to be canceled
    1208              :  */
    1209            1 : void coap_pending_clear(struct coap_pending *pending);
    1210              : 
    1211              : /**
    1212              :  * @brief Cancels all pending retransmissions, so they become
    1213              :  * available again.
    1214              :  *
    1215              :  * @param pendings Pointer to the array of #coap_pending structures
    1216              :  * @param len Size of the array of #coap_pending structures
    1217              :  */
    1218            1 : void coap_pendings_clear(struct coap_pending *pendings, size_t len);
    1219              : 
    1220              : /**
    1221              :  * @brief Count number of pending requests.
    1222              :  *
    1223              :  * @param len Number of elements in array.
    1224              :  * @param pendings Array of pending requests.
    1225              :  * @return count of elements where timeout is not zero.
    1226              :  */
    1227            1 : size_t coap_pendings_count(struct coap_pending *pendings, size_t len);
    1228              : 
    1229              : /**
    1230              :  * @brief Cancels awaiting for this reply, so it becomes available
    1231              :  * again. User responsibility to free the memory associated with data.
    1232              :  *
    1233              :  * @param reply The reply to be canceled
    1234              :  */
    1235            1 : void coap_reply_clear(struct coap_reply *reply);
    1236              : 
    1237              : /**
    1238              :  * @brief Cancels all replies, so they become available again.
    1239              :  *
    1240              :  * @param replies Pointer to the array of #coap_reply structures
    1241              :  * @param len Size of the array of #coap_reply structures
    1242              :  */
    1243            1 : void coap_replies_clear(struct coap_reply *replies, size_t len);
    1244              : 
    1245              : /**
    1246              :  * @brief Indicates that this resource was updated and that the @a
    1247              :  * notify callback should be called for every registered observer.
    1248              :  *
    1249              :  * @param resource Resource that was updated
    1250              :  *
    1251              :  * @return 0 in case of success or negative in case of error.
    1252              :  */
    1253            1 : int coap_resource_notify(struct coap_resource *resource);
    1254              : 
    1255              : /**
    1256              :  * @brief Returns if this request is enabling observing a resource.
    1257              :  *
    1258              :  * @param request Request to be checked
    1259              :  *
    1260              :  * @return True if the request is enabling observing a resource, False
    1261              :  * otherwise
    1262              :  */
    1263            1 : bool coap_request_is_observe(const struct coap_packet *request);
    1264              : 
    1265              : /**
    1266              :  * @brief Get currently active CoAP transmission parameters.
    1267              :  *
    1268              :  * @return CoAP transmission parameters structure.
    1269              :  */
    1270            1 : struct coap_transmission_parameters coap_get_transmission_parameters(void);
    1271              : 
    1272              : /**
    1273              :  * @brief Set CoAP transmission parameters.
    1274              :  *
    1275              :  * @param params Pointer to the transmission parameters structure.
    1276              :  */
    1277            1 : void coap_set_transmission_parameters(const struct coap_transmission_parameters *params);
    1278              : 
    1279              : #ifdef __cplusplus
    1280              : }
    1281              : #endif
    1282              : 
    1283              : /**
    1284              :  * @}
    1285              :  */
    1286              : 
    1287              : #endif /* ZEPHYR_INCLUDE_NET_COAP_H_ */
        

Generated by: LCOV version 2.0-1