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. | |
Private Messaging API | |
| enum | unicoap_messaging_flags_t { UNICOAP_MESSAGING_FLAG_RELIABLE = 0x01 } |
| Messaging layer result after initial treatment of an inbound message. More... | |
| enum | unicoap_preprocessing_result_t { UNICOAP_PREPROCESSING_ERROR_INVALID_CODE_CLASS = -0x1001 , UNICOAP_PREPROCESSING_ERROR_UNSUPPORTED = -0x1002 , UNICOAP_PREPROCESSING_ERROR_RESPONSE_UNEXPECTED = -0x1004 , UNICOAP_PREPROCESSING_ERROR_NOTIFICATION_UNEXPECTED = -0x100c , UNICOAP_PREPROCESSING_ERROR_TRUNCATED = -0x1010 , UNICOAP_PREPROCESSING_ERROR_REQUEST = -0x1020 , UNICOAP_PREPROCESSING_SUCCESS_RESPONSE = 0x1001 , UNICOAP_PREPROCESSING_SUCCESS_REQUEST = 0x1002 } |
| Result of unicoap_exchange_preprocess. More... | |
| 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. | |
| int | unicoap_exchange_process (unicoap_packet_t *packet, unicoap_exchange_arg_t arg) |
| Processes message. | |
| int | unicoap_exchange_release_endpoint_state (const unicoap_endpoint_t *endpoint) |
| Releases all buffers and state allocated in connection with the given endpoint. | |
| int | unicoap_messaging_process_rfc7252 (const uint8_t *pdu, size_t size, bool truncated, unicoap_packet_t *packet) |
| Internal RFC 7252 messaging inbound processor. | |
| int | unicoap_messaging_send (unicoap_packet_t *packet, unicoap_messaging_flags_t flags) |
| Forwards packet to messaging layer and the corresponding driver for further message processing, encoding, and transport I/O. | |
| int | unicoap_messaging_send_rfc7252 (unicoap_packet_t *packet, unicoap_messaging_flags_t flags) |
| Sends CoAP over UDP or DTLS packet, see unicoap_messaging_send. | |
| void | unicoap_generate_token (uint8_t *token) |
| Generates new token. | |
| static unicoap_messaging_flags_t | _messaging_flags_resource (unicoap_resource_flags_t resource_flags) |
| Retrieves the part of a resource flags bitfield relevant for the messaging driver. | |
| #define | UNICOAP_MESSAGING_FLAGS_DEFAULT (0) |
| The messaging flags the stack uses when no source for messaging flags is available. | |
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. | |
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. | |
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_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. | |
| #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 110 of file messaging.h.
| #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 (TODO: ref) unicoap_client_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.
Result of unicoap_exchange_preprocess.
Definition at line 124 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 286 of file messaging.h.
|
inlinestatic |
|
inlinestatic |
| 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 |
| int unicoap_exchange_release_endpoint_state | ( | const unicoap_endpoint_t * | endpoint | ) |
Releases all buffers and state allocated in connection with the given endpoint.
Call this function from your driver if you encounter a severe error with a given endpoint
| [in] | endpoint | Remote endpoint to release buffers for |
| `-ENOENT` | if no state is currently associated with the given endpoint |
| 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, | ||
| bool | truncated, | ||
| unicoap_packet_t * | packet ) |
Internal RFC 7252 messaging inbound processor.
| [in] | pdu | Buffer containing PDU |
| size | Size of PDU in bytes | |
| truncated | A boolean value indicating whether the message has been truncated by the transport layer | |
| [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 ) |
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 |
|
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) |