Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
coap.h File Reference

CoAP implementation for Zephyr. More...

#include <zephyr/types.h>
#include <stddef.h>
#include <stdbool.h>
#include <net/net_ip.h>
#include <sys/slist.h>

Go to the source code of this file.

Data Structures

struct  coap_resource
 Description of CoAP resource. More...
 
struct  coap_observer
 Represents a remote device that is observing a local resource. More...
 
struct  coap_packet
 Representation of a CoAP Packet. More...
 
struct  coap_option
 
struct  coap_pending
 Represents a request awaiting for an acknowledgment (ACK). More...
 
struct  coap_reply
 Represents the handler for the reply of a request, it is also used when observing resources. More...
 
struct  coap_block_context
 Represents the current state of a block-wise transaction. More...
 

Macros

#define COAP_REQUEST_MASK   0x07
 
#define COAP_VERSION_1   1U
 
#define coap_make_response_code(class, det)   ((class << 5) | (det))
 
#define COAP_CODE_EMPTY   (0)
 
#define COAP_TOKEN_MAX_LEN   8UL
 
#define GET_BLOCK_NUM(v)   ((v) >> 4)
 
#define GET_BLOCK_SIZE(v)   (((v) & 0x7))
 
#define GET_MORE(v)   (!!((v) & 0x08))
 
#define COAP_DEFAULT_MAX_RETRANSMIT   4
 
#define COAP_DEFAULT_ACK_RANDOM_FACTOR   1.5
 

Typedefs

typedef int(* coap_method_t) (struct coap_resource *resource, struct coap_packet *request, struct sockaddr *addr, socklen_t addr_len)
 Type of the callback being called when a resource's method is invoked by the remote entity. More...
 
typedef void(* coap_notify_t) (struct coap_resource *resource, struct coap_observer *observer)
 Type of the callback being called when a resource's has observers to be informed when an update happens. More...
 
typedef int(* coap_reply_t) (const struct coap_packet *response, struct coap_reply *reply, const struct sockaddr *from)
 Helper function to be called when a response matches the a pending request. More...
 

Enumerations

enum  coap_option_num {
  COAP_OPTION_IF_MATCH = 1 , COAP_OPTION_URI_HOST = 3 , COAP_OPTION_ETAG = 4 , COAP_OPTION_IF_NONE_MATCH = 5 ,
  COAP_OPTION_OBSERVE = 6 , COAP_OPTION_URI_PORT = 7 , COAP_OPTION_LOCATION_PATH = 8 , COAP_OPTION_URI_PATH = 11 ,
  COAP_OPTION_CONTENT_FORMAT = 12 , COAP_OPTION_MAX_AGE = 14 , COAP_OPTION_URI_QUERY = 15 , COAP_OPTION_ACCEPT = 17 ,
  COAP_OPTION_LOCATION_QUERY = 20 , COAP_OPTION_BLOCK2 = 23 , COAP_OPTION_BLOCK1 = 27 , COAP_OPTION_SIZE2 = 28 ,
  COAP_OPTION_PROXY_URI = 35 , COAP_OPTION_PROXY_SCHEME = 39 , COAP_OPTION_SIZE1 = 60
}
 Set of CoAP packet options we are aware of. More...
 
enum  coap_method { COAP_METHOD_GET = 1 , COAP_METHOD_POST = 2 , COAP_METHOD_PUT = 3 , COAP_METHOD_DELETE = 4 }
 Available request methods. More...
 
enum  coap_msgtype { COAP_TYPE_CON = 0 , COAP_TYPE_NON_CON = 1 , COAP_TYPE_ACK = 2 , COAP_TYPE_RESET = 3 }
 CoAP packets may be of one of these types. More...
 
enum  coap_response_code {
  COAP_RESPONSE_CODE_OK = (( 2 << 5) | ( 0 )) , COAP_RESPONSE_CODE_CREATED = (( 2 << 5) | ( 1 )) , COAP_RESPONSE_CODE_DELETED = (( 2 << 5) | ( 2 )) , COAP_RESPONSE_CODE_VALID = (( 2 << 5) | ( 3 )) ,
  COAP_RESPONSE_CODE_CHANGED = (( 2 << 5) | ( 4 )) , COAP_RESPONSE_CODE_CONTENT = (( 2 << 5) | ( 5 )) , COAP_RESPONSE_CODE_CONTINUE = (( 2 << 5) | ( 31 )) , COAP_RESPONSE_CODE_BAD_REQUEST = (( 4 << 5) | ( 0 )) ,
  COAP_RESPONSE_CODE_UNAUTHORIZED = (( 4 << 5) | ( 1 )) , COAP_RESPONSE_CODE_BAD_OPTION = (( 4 << 5) | ( 2 )) , COAP_RESPONSE_CODE_FORBIDDEN = (( 4 << 5) | ( 3 )) , COAP_RESPONSE_CODE_NOT_FOUND = (( 4 << 5) | ( 4 )) ,
  COAP_RESPONSE_CODE_NOT_ALLOWED = (( 4 << 5) | ( 5 )) , COAP_RESPONSE_CODE_NOT_ACCEPTABLE = (( 4 << 5) | ( 6 )) , COAP_RESPONSE_CODE_INCOMPLETE = (( 4 << 5) | ( 8 )) , COAP_RESPONSE_CODE_PRECONDITION_FAILED = (( 4 << 5) | ( 12 )) ,
  COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = (( 4 << 5) | ( 13 )) , COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT , COAP_RESPONSE_CODE_INTERNAL_ERROR = (( 5 << 5) | ( 0 )) , COAP_RESPONSE_CODE_NOT_IMPLEMENTED = (( 5 << 5) | ( 1 )) ,
  COAP_RESPONSE_CODE_BAD_GATEWAY = (( 5 << 5) | ( 2 )) , COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = (( 5 << 5) | ( 3 )) , COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = (( 5 << 5) | ( 4 )) , COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED
}
 Set of response codes available for a response packet. More...
 
enum  coap_content_format {
  COAP_CONTENT_FORMAT_TEXT_PLAIN = 0 , COAP_CONTENT_FORMAT_APP_LINK_FORMAT = 40 , COAP_CONTENT_FORMAT_APP_XML = 41 , COAP_CONTENT_FORMAT_APP_OCTET_STREAM = 42 ,
  COAP_CONTENT_FORMAT_APP_EXI = 47 , COAP_CONTENT_FORMAT_APP_JSON = 50 , COAP_CONTENT_FORMAT_APP_CBOR = 60
}
 Set of Content-Format option values for CoAP. More...
 
enum  coap_block_size {
  COAP_BLOCK_16 , COAP_BLOCK_32 , COAP_BLOCK_64 , COAP_BLOCK_128 ,
  COAP_BLOCK_256 , COAP_BLOCK_512 , COAP_BLOCK_1024
}
 

Functions

uint8_t coap_header_get_version (const struct coap_packet *cpkt)
 Returns the version present in a CoAP packet. More...
 
uint8_t coap_header_get_type (const struct coap_packet *cpkt)
 Returns the type of the CoAP packet. More...
 
uint8_t coap_header_get_token (const struct coap_packet *cpkt, uint8_t *token)
 Returns the token (if any) in the CoAP packet. More...
 
uint8_t coap_header_get_code (const struct coap_packet *cpkt)
 Returns the code of the CoAP packet. More...
 
uint16_t coap_header_get_id (const struct coap_packet *cpkt)
 Returns the message id associated with the CoAP packet. More...
 
const uint8_tcoap_packet_get_payload (const struct coap_packet *cpkt, uint16_t *len)
 Returns the data pointer and length of the CoAP packet. More...
 
int coap_packet_parse (struct coap_packet *cpkt, uint8_t *data, uint16_t len, struct coap_option *options, uint8_t opt_num)
 Parses the CoAP packet in data, validating it and initializing cpkt. data must remain valid while cpkt is used. More...
 
int coap_packet_init (struct coap_packet *cpkt, uint8_t *data, uint16_t max_len, uint8_t ver, uint8_t type, uint8_t token_len, const uint8_t *token, uint8_t code, uint16_t id)
 Creates a new CoAP Packet from input data. More...
 
int coap_ack_init (struct coap_packet *cpkt, const struct coap_packet *req, uint8_t *data, uint16_t max_len, uint8_t code)
 Create a new CoAP Acknowledgment message for given request. More...
 
uint8_tcoap_next_token (void)
 Returns a randomly generated array of 8 bytes, that can be used as a message's token. More...
 
uint16_t coap_next_id (void)
 Helper to generate message ids. More...
 
int coap_find_options (const struct coap_packet *cpkt, uint16_t code, struct coap_option *options, uint16_t veclen)
 Return the values associated with the option of value code. More...
 
int coap_packet_append_option (struct coap_packet *cpkt, uint16_t code, const uint8_t *value, uint16_t len)
 Appends an option to the packet. More...
 
unsigned int coap_option_value_to_int (const struct coap_option *option)
 Converts an option to its integer representation. More...
 
int coap_append_option_int (struct coap_packet *cpkt, uint16_t code, unsigned int val)
 Appends an integer value option to the packet. More...
 
int coap_packet_append_payload_marker (struct coap_packet *cpkt)
 Append payload marker to CoAP packet. More...
 
int coap_packet_append_payload (struct coap_packet *cpkt, const uint8_t *payload, uint16_t payload_len)
 Append payload to CoAP packet. More...
 
int coap_handle_request (struct coap_packet *cpkt, struct coap_resource *resources, struct coap_option *options, uint8_t opt_num, struct sockaddr *addr, socklen_t addr_len)
 When a request is received, call the appropriate methods of the matching resources. More...
 
static uint16_t coap_block_size_to_bytes (enum coap_block_size block_size)
 Helper for converting the enumeration to the size expressed in bytes. More...
 
int coap_block_transfer_init (struct coap_block_context *ctx, enum coap_block_size block_size, size_t total_size)
 Initializes the context of a block-wise transfer. More...
 
int coap_append_block1_option (struct coap_packet *cpkt, struct coap_block_context *ctx)
 Append BLOCK1 option to the packet. More...
 
int coap_append_block2_option (struct coap_packet *cpkt, struct coap_block_context *ctx)
 Append BLOCK2 option to the packet. More...
 
int coap_append_size1_option (struct coap_packet *cpkt, struct coap_block_context *ctx)
 Append SIZE1 option to the packet. More...
 
int coap_append_size2_option (struct coap_packet *cpkt, struct coap_block_context *ctx)
 Append SIZE2 option to the packet. More...
 
int coap_get_option_int (const struct coap_packet *cpkt, uint16_t code)
 Get the integer representation of a CoAP option. More...
 
int coap_update_from_block (const struct coap_packet *cpkt, struct coap_block_context *ctx)
 Retrieves BLOCK{1,2} and SIZE{1,2} from cpkt and updates ctx accordingly. More...
 
size_t coap_next_block (const struct coap_packet *cpkt, struct coap_block_context *ctx)
 Updates ctx so after this is called the current entry indicates the correct offset in the body of data being transferred. More...
 
void coap_observer_init (struct coap_observer *observer, const struct coap_packet *request, const struct sockaddr *addr)
 Indicates that the remote device referenced by addr, with request, wants to observe a resource. More...
 
bool coap_register_observer (struct coap_resource *resource, struct coap_observer *observer)
 After the observer is initialized, associate the observer with an resource. More...
 
void coap_remove_observer (struct coap_resource *resource, struct coap_observer *observer)
 Remove this observer from the list of registered observers of that resource. More...
 
struct coap_observercoap_find_observer_by_addr (struct coap_observer *observers, size_t len, const struct sockaddr *addr)
 Returns the observer that matches address addr. More...
 
struct coap_observercoap_observer_next_unused (struct coap_observer *observers, size_t len)
 Returns the next available observer representation. More...
 
void coap_reply_init (struct coap_reply *reply, const struct coap_packet *request)
 Indicates that a reply is expected for request. More...
 
int coap_pending_init (struct coap_pending *pending, const struct coap_packet *request, const struct sockaddr *addr, uint8_t retries)
 Initialize a pending request with a request. More...
 
struct coap_pendingcoap_pending_next_unused (struct coap_pending *pendings, size_t len)
 Returns the next available pending struct, that can be used to track the retransmission status of a request. More...
 
struct coap_replycoap_reply_next_unused (struct coap_reply *replies, size_t len)
 Returns the next available reply struct, so it can be used to track replies and notifications received. More...
 
struct coap_pendingcoap_pending_received (const struct coap_packet *response, struct coap_pending *pendings, size_t len)
 After a response is received, returns if there is any matching pending request exits. User has to clear all pending retransmissions related to that response by calling coap_pending_clear(). More...
 
struct coap_replycoap_response_received (const struct coap_packet *response, const struct sockaddr *from, struct coap_reply *replies, size_t len)
 After a response is received, call coap_reply_t handler registered in coap_reply structure. More...
 
struct coap_pendingcoap_pending_next_to_expire (struct coap_pending *pendings, size_t len)
 Returns the next pending about to expire, pending->timeout informs how many ms to next expiration. More...
 
bool coap_pending_cycle (struct coap_pending *pending)
 After a request is sent, user may want to cycle the pending retransmission so the timeout is updated. More...
 
void coap_pending_clear (struct coap_pending *pending)
 Cancels the pending retransmission, so it again becomes available. More...
 
void coap_pendings_clear (struct coap_pending *pendings, size_t len)
 Cancels all pending retransmissions, so they become available again. More...
 
void coap_reply_clear (struct coap_reply *reply)
 Cancels awaiting for this reply, so it becomes available again. User responsibility to free the memory associated with data. More...
 
void coap_replies_clear (struct coap_reply *replies, size_t len)
 Cancels all replies, so they become available again. More...
 
int coap_resource_notify (struct coap_resource *resource)
 Indicates that this resource was updated and that the notify callback should be called for every registered observer. More...
 
bool coap_request_is_observe (const struct coap_packet *request)
 Returns if this request is enabling observing a resource. More...
 

Detailed Description

CoAP implementation for Zephyr.