Implementation details of unicoap
More...
Implementation details of unicoap
unicoap's internal state. Topics | |
| State Management | |
Files | |
| file | private.h |
| Private API. | |
| file | messaging.h |
| Messaging API. | |
| file | packet.h |
| Packet. | |
Data Structures | |
| struct | unicoap_state_t |
| Container the unicoap stacks keeps state in. More... | |
| union | unicoap_exchange_arg_t |
| Argument passed to exchange-layer processing function. More... | |
| struct | unicoap_packet_t |
| A type acting as an envelope for a message and data connected, like endpoints. More... | |
Functions | |
| static unicoap_proto_t | unicoap_packet_proto (const unicoap_packet_t *packet) |
| Retrieves protocol number from packet. | |
| static const void * | _packet_get_dtls_session (unicoap_packet_t *packet) |
| Retrieves transport session from packet. | |
| static void | _packet_set_dtls_session (unicoap_packet_t *packet, const unicoap_sock_dtls_session_t *dtls_session) |
| Sets DTLS transport session of packet. | |
Thread | |
| kernel_pid_t | _unicoap_pid |
PID of unicoap thread. | |
| void | unicoap_state_lock (void) |
| Locks internal state lock. | |
| void | unicoap_state_unlock (void) |
| Unlocks internal state lock. | |
| #define | UNICOAP_THREAD_IDENTIFIER "unicoap" |
| Name of background thread spawned upon calls to unicoap_init. | |
Debugging | |
| void | unicoap_assist_emit_diagnostic_missing_driver (unicoap_proto_t proto) |
| Emit a diagnostic stating that a driver is missing. | |
| #define | UNICOAP_DEBUG_PREFIX "coap" |
| Debug log prefix. | |
| #define | DEBUG_ENDPOINT(endpoint) |
| Debug print endpoint. | |
| #define | DEBUG_FLAGS(flags, role) |
| Debug print flags. | |
State and initialization | |
| static void | unicoap_set_listeners (unicoap_state_t *state, unicoap_listener_t *listeners) |
Sets listener array on state object to listeners. | |
| static unicoap_listener_t * | unicoap_get_listeners (unicoap_state_t *state) |
Gets listener array from state object. | |
| int | unicoap_init_udp (event_queue_t *queue) |
Initializes the CoAP over UDP driver on the given queue. | |
| int | unicoap_deinit_udp (event_queue_t *queue) |
Deinitializes the CoAP over UDP driver on the given queue. | |
| int | unicoap_init_dtls (event_queue_t *queue) |
Initializes the CoAP over DTLS over UDP driver on the given queue. | |
| int | unicoap_deinit_dtls (event_queue_t *queue) |
Deinitializes the CoAP over DTLS over UDP driver on the given queue. | |
| int | unicoap_init_slipmux (event_queue_t *queue) |
Initializes the CoAP over Slipmux driver on the given queue. | |
| int | unicoap_deinit_slipmux (event_queue_t *queue) |
Deinitializes the CoAP over Slipmux driver on the given queue. | |
| int | unicoap_init_rfc7252_common (event_queue_t *queue) |
Initializes the common RFC 7252 driver on the given queue. | |
| int | unicoap_deinit_rfc7252_common (event_queue_t *queue) |
DeiInitializes the common RFC 7252 driver on the given queue. | |
Resource-request matching | |
| int | unicoap_resource_find (const unicoap_packet_t *packet, const unicoap_resource_t **resource_ptr, const unicoap_listener_t **listener_ptr) |
| Tries to find a resource for the given packet. | |
| int | unicoap_resource_match_request_default (const unicoap_listener_t *listener, const unicoap_resource_t **resource_ptr, const unicoap_message_t *request, const unicoap_endpoint_t *endpoint) |
| Default request-resource matcher for listeners. | |
| int | unicoap_resource_handle_well_known_core (unicoap_message_t *message, const unicoap_aux_t *aux, unicoap_request_context_t *ctx, void *arg) |
Default resource handler for /.well-known/core | |
Other Utils | |
| static bool | unicoap_transport_truncate_received (size_t *chunk_size, size_t received) |
| Determines whether the chunk of the given size must be truncated to fit into a buffer of size CONFIG_UNICOAP_PDU_SIZE_MAX. | |
| #define | UNICOAP_DECL_RECEIVER_STORAGE uint8_t unicoap_receiver_buffer[CONFIG_UNICOAP_PDU_SIZE_MAX] |
| Declares internal receiver storage buffer. | |
| #define | UNICOAP_DECL_RECEIVER_STORAGE_EXTERN extern UNICOAP_DECL_RECEIVER_STORAGE |
extern declaration of UNICOAP_DECL_RECEIVER_STORAGE | |
Server | |
| int | unicoap_server_process_request (unicoap_packet_t *packet, const unicoap_resource_t *resource) |
| Informs the unicoap server a new packet has been preprocessed and can be handled. | |
| int | unicoap_server_send_response_body (unicoap_packet_t *packet, const unicoap_resource_t *resource) |
| Sends entire response body, may be split into parts and then sent. | |
Client | |
| int | unicoap_client_process_response (unicoap_packet_t *packet, unicoap_client_memo_t *memo) |
| Informs the unicoap client a new packet has been preprocessed and can be handled. | |
| int | unicoap_client_send_request_part (unicoap_packet_t *packet, unicoap_client_memo_t *memo, unicoap_request_flags_t request_flags) |
| Sends part of request body, such as single request in a block-wise transfer. | |
| int | unicoap_client_send_request_body (unicoap_message_t *request, unicoap_endpoint_t *endpoint, unicoap_callback_t callback, unicoap_request_parameters_t *parameters, unicoap_request_flags_t flags) |
| Sends entire request body, may be split into parts and then sent. | |
Private message and option helpers | |
| #define | UNICOAP_OPTIONS_ALLOCA(name, capacity) |
| Dynamically allocates options with buffer capacity on stack at runtime. | |
| #define DEBUG_ENDPOINT | ( | endpoint | ) |
Debug print endpoint.
| #define DEBUG_FLAGS | ( | flags, | |
| role ) |
| #define UNICOAP_DECL_RECEIVER_STORAGE uint8_t unicoap_receiver_buffer[CONFIG_UNICOAP_PDU_SIZE_MAX] |
| #define UNICOAP_DECL_RECEIVER_STORAGE_EXTERN extern UNICOAP_DECL_RECEIVER_STORAGE |
extern declaration of UNICOAP_DECL_RECEIVER_STORAGE
| #define UNICOAP_MESSAGING_FLAGS_DEFAULT (0) |
The messaging flags the stack uses when no source for messaging flags is available.
These flags are used if the server needs to send a 4.04 Path Not Found response, or the message is truncated.
Definition at line 141 of file messaging.h.
| #define UNICOAP_OPTIONS_ALLOCA | ( | name, | |
| capacity ) |
Dynamically allocates options with buffer capacity on stack at runtime.
| name | Name of the variable storing the options structure as identifier token |
| capacity | Storage buffer capacity in bytes |
Allocates a new unicoap_options_t container and a storage buffer with the given capacity, and initializes it. No need to call unicoap_options_t::unicoap_options_init afterwards.
See UNICOAP_OPTIONS_ALLOC for stack allocation at compile-time See UNICOAP_OPTIONS_ALLOC_STATIC for static allocation
| #define UNICOAP_THREAD_IDENTIFIER "unicoap" |
Name of background thread spawned upon calls to unicoap_init.
Messaging layer result after initial treatment of an inbound message.
Flags for sending a message communicated by the unicoap stack to the messaging driver
Each time the stack wants to send a message, unicoap_messaging_send will be called, which, in turn, invokes the messaging driver implementation.
These flags are intended for messaging and transmission options. Messaging options may change the CoAP message flow (e.g., waiting for an acknowledgement) or transmission behavior (e.g., using an optional reliability layer of the underlying transport protocol or implementation).
Messaging flags are provided as part of client and resource flags. The unicoap_request_flags_t and unicoap_resource_flags_t are separated into
The stack communicates only the messaging flags to the driver, in the form of this bitfield.
Definition at line 83 of file messaging.h.
RFC 7252 event used to notify messaging layer of a change.
| Enumerator | |
|---|---|
| UNICOAP_MESSAGING_RFC7252_EVENT_RX | 'Received' event The attached packet contains a message that has been received. |
| UNICOAP_MESSAGING_RFC7252_EVENT_TRUNCATED | 'Truncated' event The attached packet contains a message that has been truncated. This event will always occur in conjunction with UNICOAP_MESSAGING_RFC7252_EVENT_RX. |
| UNICOAP_MESSAGING_RFC7252_EVENT_SESSION_ESTABLISHED | 'Session established' event A DTLS session with the remote endpoint has been establishment. The messaging layer may resume sending any delayed transmissions now. |
Definition at line 253 of file messaging.h.
Result of unicoap_exchange_preprocess.
Definition at line 156 of file messaging.h.
|
inlinestatic |
Retrieves the part of a request flags bitfield relevant for the messaging driver.
| request_flags | Request flags |
Definition at line 354 of file messaging.h.
|
inlinestatic |
Retrieves the part of a resource flags bitfield relevant for the messaging driver.
| resource_flags | Resource flags |
Definition at line 341 of file messaging.h.
|
inlinestatic |
|
inlinestatic |
| int unicoap_client_process_response | ( | unicoap_packet_t * | packet, |
| unicoap_client_memo_t * | memo ) |
Informs the unicoap client a new packet has been preprocessed and can be handled.
| [in,out] | packet | Packet that will be processed by the client |
| [in,out] | memo | Mandatory pointer to memo variable, memo itself can be NULL |
0 on success | int unicoap_client_send_request_body | ( | unicoap_message_t * | request, |
| unicoap_endpoint_t * | endpoint, | ||
| unicoap_callback_t | callback, | ||
| unicoap_request_parameters_t * | parameters, | ||
| unicoap_request_flags_t | flags ) |
Sends entire request body, may be split into parts and then sent.
| [in] | request | Request body to send |
| [in] | endpoint | Remote endpoint (server) |
| [in] | callback | Optional application callback |
| [in] | parameters | Optional parameters (nullable) |
| flags | Request flags |
| int unicoap_client_send_request_part | ( | unicoap_packet_t * | packet, |
| unicoap_client_memo_t * | memo, | ||
| unicoap_request_flags_t | request_flags ) |
Sends part of request body, such as single request in a block-wise transfer.
| packet | Packet to send |
| memo | Optional memo |
| request_flags | Request flags |
| unicoap_preprocessing_result_t unicoap_exchange_preprocess | ( | unicoap_packet_t * | packet, |
| unicoap_messaging_flags_t * | flags, | ||
| unicoap_exchange_arg_t * | arg, | ||
| bool | truncated ) |
Performs initial processing of a CoAP packet after it has been parsed.
The processing of a CoAP message is divided into two parts: preprocessing and the actual processing. This design was chosen to allow the messaging driver to implement networking logic in between.
Example: The RFC 7252 messaging driver sends an ACK for every confirmable response received at the client. Waiting for the application to handle the response could delay the ACK, possibly lead to retransmissions or even open a timing-based side channel.
| [in,out] | packet | Packet received |
| [out] | flags | Messaging flags associated with resource or client exchange |
| [out] | arg | Argument to be passed to the processor |
| truncated | A boolean value indicating whether the given PDU was truncated, but could still be parsed (i.e., a valid message) |
| int unicoap_exchange_process | ( | unicoap_packet_t * | packet, |
| unicoap_exchange_arg_t | arg ) |
Processes message.
Call this after you have preprocessed the message using unicoap_exchange_preprocess.
| [in,out] | packet | CoAP packet |
| [in] | arg | Argument returned (via out parameter) by unicoap_exchange_preprocess |
| Zero | if processing succeeds |
| Negative | error number if processing failed |
| void unicoap_generate_token | ( | uint8_t * | token | ) |
Generates new token.
Token will be CONFIG_UNICOAP_GENERATED_TOKEN_LENGTH bytes long.
| [in,out] | token | Buffer for generated token, must have a minimum capacity of CONFIG_UNICOAP_GENERATED_TOKEN_LENGTH |
|
inlinestatic |
| int unicoap_messaging_process_rfc7252 | ( | const uint8_t * | pdu, |
| size_t | size, | ||
| unicoap_messaging_rfc7252_event_type_t | event, | ||
| unicoap_packet_t * | packet ) |
Internal RFC 7252 messaging inbound processor.
| [in] | pdu | Buffer containing PDU |
| size | Size of PDU in bytes | |
| event | Event the messaging layer must process | |
| [in] | packet | Packet to process |
This function forwards the truncated characteristic to the exchange layer, which can handle that scenario appropriately, such as by setting a Size option.
sock is not an option. | int unicoap_messaging_send | ( | unicoap_packet_t * | packet, |
| unicoap_messaging_flags_t | flags, | ||
| void * | exchange ) |
Forwards packet to messaging layer and the corresponding driver for further message processing, encoding, and transport I/O.
The driver needs to encode the given message into a serialized PDU and use its own means of transmitting the packet to the packet's remote endpoint.
This function calls the individual driver messaging implementation.
| [in,out] | packet | Packet to send |
| flags | Messaging flags | |
| [in,out] | exchange | Exchange-layer state object associated with this attempted . messaging-layer transmission |
|
inlinestatic |
| int unicoap_resource_find | ( | const unicoap_packet_t * | packet, |
| const unicoap_resource_t ** | resource_ptr, | ||
| const unicoap_listener_t ** | listener_ptr ) |
Tries to find a resource for the given packet.
The resource and listener variables passed by reference will be set to the respective resource and encompassing listener, if found.
This method calls each listeners unicoap_listener_t::request_matcher to check whether the given resource can be matched to the given packet.
| [in] | packet | Packet to find resource for |
| [in] | resource_ptr | Pointer to a resource variable |
| [in] | listener_ptr | Pointer to a listener variable |
| `0` | if found |
| or | unicoap_status_t if not found, method mismatch, or server error |
| int unicoap_resource_handle_well_known_core | ( | unicoap_message_t * | message, |
| const unicoap_aux_t * | aux, | ||
| unicoap_request_context_t * | ctx, | ||
| void * | arg ) |
Default resource handler for /.well-known/core
| int unicoap_resource_match_request_default | ( | const unicoap_listener_t * | listener, |
| const unicoap_resource_t ** | resource_ptr, | ||
| const unicoap_message_t * | request, | ||
| const unicoap_endpoint_t * | endpoint ) |
Default request-resource matcher for listeners.
The resource variable passed by reference will be set to the respective resource, if found.
| [in] | listener | Listener |
| [out] | resource_ptr | Pointer to a resource variable |
| [in] | request | Request message |
| [in] | endpoint | Remote endpoint the request originates from |
| `0` | if found |
| An | unicoap_status_t if not found, method mismatch, or server error |
| int unicoap_server_process_request | ( | unicoap_packet_t * | packet, |
| const unicoap_resource_t * | resource ) |
Informs the unicoap server a new packet has been preprocessed and can be handled.
This function handles block-wise transfers.
| [in,out] | packet | Packet that will be processed by the server |
| [in,out] | resource | Resource |
| `0` | on success |
| Negative | errno on failure |
| int unicoap_server_send_response_body | ( | unicoap_packet_t * | packet, |
| const unicoap_resource_t * | resource ) |
Sends entire response body, may be split into parts and then sent.
| [in,out] | packet | Response packet to send |
| [in] | resource | mandatory resource which is sending this response |
|
inlinestatic |
|
inlinestatic |
Determines whether the chunk of the given size must be truncated to fit into a buffer of size CONFIG_UNICOAP_PDU_SIZE_MAX.
Use this method when receiving fragmented/scattered data, e.g., when reading from a socket.
This method returns false if a chunk of the given chunk_size can still be copied into a buffer of size CONFIG_UNICOAP_PDU_SIZE_MAX when received bytes are already stored in that buffer.
If said buffer cannot store a chunk of the given size while also holding received bytes already saved, the chunk needs to be truncated. In this case, true is returned and the chunk size is truncated to the largest chunk size that is still acceptable. Stop your receiver loop as further calls to this function will, consequently, also return true and yield a chunk_size of zero (buffer is full).
| [in,out] | chunk_size | Number of bytes in the currently received chunk. Written to if chunk does not fit into buffer. |
| received | Number of bytes already stored in the buffer (received byte count) |