![]() |
![]()
| ![]() |
![]()
NAMEcoap_observe, coap_resource_set_get_observable, coap_resource_notify_observers, coap_cancel_observe, coap_session_set_no_observe_cancel - Work with CoAP observe SYNOPSIS#include <coap3/coap.h> void coap_resource_set_get_observable(coap_resource_t *resource, int mode); int coap_resource_notify_observers(coap_resource_t *resource, const coap_string_t *query); int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type); void coap_session_set_no_observe_cancel(coap_session_t *session); For specific (D)TLS library support, link with -lcoap-3-notls, -lcoap-3-gnutls, -lcoap-3-openssl, -lcoap-3-mbedtls, -lcoap-3-wolfssl or -lcoap-3-tinydtls. Otherwise, link with -lcoap-3 to get the default (D)TLS library support. DESCRIPTIONRFC7641 extends the CoAP protocol to be able to monitor the state of a resource over time. This enables clients to "observe" resources with a defined query, i.e., to retrieve a representation of a resource and keep this representation updated by the server over a period of time. The server has to flag a resource as "observable", and then the client has to request in a GET request that it wants to observe this resource by the use of the COAP_OPTION_OBSERVE Option with a value of COAP_OBSERVE_ESTABLISH. Optionally, the client can specify query options for the resource, or by using a FETCH request instead of a GET to define a query (RFC8132). To remove the "observe" subscription, the client has to issue a GET (or FETCH) request with the COAP_OPTION_OBSERVE Option with a value of COAP_OBSERVE_CANCEL using the same token and other options used for making the initial "observe" request. Alternatively, "observe" can be cancelled using coap_cancel_observe() instead. The underlying library adds in and removes "subscribers" to "observe" the resource as appropriate in the server side logic. NOTE: COAP_RESOURCE_MAX_SUBSCRIBER may have been defined to limit the number of subscribers to a resource when libcoap was built. Within the server application, it needs to determine that there is a change of state of the resource under observation, and then cause the CoAP library layer to initiate a "fake GET/FETCH request" so that an observe GET/FETCH response gets sent back to all the clients that are observing the resource. The appropriate GET/FETCH handler within the server application is called to fill in the response packet with the appropriate information. This "fake GET/FETCH request" is triggered by a call to coap_resource_notify_observers(). The call to coap_io_process() in the main server application i/o loop will do all the necessary processing of sending any outstanding "fake GET/FETCH requests". Whenever the server sends a copy of the state of the "observed" resource to the client, it will use the same token used by the client when the client requested the "observe" (or the last token used for a FETCH that spans multiple blocks). The client will receive this observe response in the handler defined by coap_register_response_handler(3) (with the token updated to the initial token used by the client application for a large FETCH). It is the responsibility of the client application to match the supplied token and update the appropriate internal information. FUNCTIONSFunction: coap_resource_set_get_observable() The coap_resource_set_get_observable() function enables or disables the observable status of the resource by the setting of mode. If mode is 1, then the resource is observable. If mode is 0, then the resource is no longer observable. NOTE: It is not possible for the Unknown Resource, created by coap_resource_unknown_init(3), to be observable as the Uri-Path is not known when libcoap creates a "fake GET/FETCH request". The Unknown Resource PUT handler must create a new resource and mark the resource as "observable" if a specific resource needs to be observable. The application must then manage the deletion of the resource at the appropriate time. NOTE: The type (confirmable or non-confirmable) of the triggered observe GET response is determined not by the initial GET/FETCH request, but independently by the server as per "RFC7641 3.5. Transmission". This is controlled by the flags (one of COAP_RESOURCE_FLAGS_NOTIFY_NON, COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS or COAP_RESOURCE_FLAGS_NOTIFY_CON) used when creating the resource using coap_resource_init(3). NOTE: Furthermore, the server must send at least one "observe" response as confirmable, when generally sending non-confirmable, at least every 24 hours as per "RFC7641 4.5. Transmission". Libcoap automatically handles this by sending every fifth (COAP_OBS_MAX_NON) response as a confirmable response for detection that the client is still responding unless if COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS is set, which is a "RFC7641 4.5. Transmission" violation, where non-confirmable "observe" responses are always sent as required by some higher layer protocols. Function: coap_resource_notify_observers() The coap_resource_notify_observers() function needs to be called whenever the server application determines that there has been a change to the state of resource. The query parameter is obsolete and ignored. Function: coap_cancel_observe() The coap_cancel_observe() function can be used by the client to cancel an observe request that is being tracked. This will cause the appropriate PDU to be sent to the server to cancel the observation, based on the session and token used to set up the observe and the PDU is of type message_type (use COAP_MESSAGE_NON or COAP_MESSAGE_CON). Function: coap_session_set_no_observe_cancel() The coap_session_set_no_observe_cancel() function can be called by the client to disable calling coap_cancel_observe() when the session is being closed down / freed off. coap_cancel_observe() can still be called directly by the client application. RETURN VALUEScoap_resource_notify_observers() returns 0 if not observable or no observers, 1 on success. coap_cancel_observe() returns 0 on failure, 1 on success. EXAMPLESSimple Time Server #include <coap3/coap.h> #include <stdio.h> coap_resource_t *time_resource = NULL; /* specific GET "time" handler, called from hnd_get_generic() */ static void hnd_get_time(coap_resource_t *resource, coap_session_t *session, Client Observe Request Setup #include <coap3/coap.h> /* Usually, requests are sent confirmable */ static unsigned char msgtype = COAP_MESSAGE_CON; static unsigned int token = 0; static coap_pdu_t * coap_new_request(coap_context_t *context, coap_session_t *session, SEE ALSOcoap_block(3), coap_context(3), coap_handler(3), coap_init(3), coap_pdu_setup(3), coap_resource(3) and coap_session(3) FURTHER INFORMATIONSee "RFC7252: The Constrained Application Protocol (CoAP)" "RFC7641: Observing Resources in the Constrained Application Protocol (CoAP)" "RFC8132: PATCH and FETCH Methods for the Constrained Application Protocol (CoAP)" for further information. BUGSPlease raise an issue on GitHub at https://github.com/obgm/libcoap/issues to report any bugs. Please raise a Pull Request at https://github.com/obgm/libcoap/pulls for any fixes. AUTHORSThe libcoap project <libcoap-developers@lists.sourceforge.net>
|