From 91d8fad66321ae054fbfcafd04092270cfd2eb67 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 21 May 2024 15:06:15 +0100 Subject: [PATCH] locking: Simplify maintenance / debugging /documentation Update the Public API equivalent of the libcoap functions that need to be called when locked with a name suffix of _lkd. Move matching Public API functions out of coap_threadsafe.c to be coded just ahead of the newly renamed _lkd function. Properly define the _lkd function in the appropriate _internal.h file for documentation, removing the #defines from coap_threadsafe_internal.h. Tag all the Public API functions that have a _lkd function with COAP_API. COAP_API can then be defined as to what should be done for that function. Check libcoap library does not call the original Public API function when it should be calling the _lkd function by marking the Public API not _lkd functions as deprecated during the libcoap library build. [Done by the new COAP_API being defined as __attribute__((deprecated)).] Work in progress for all the functions that need to be multi-thread safe. --- include/coap3/coap_async.h | 12 +- include/coap3/coap_async_internal.h | 76 +++++++++ include/coap3/coap_block.h | 36 ++-- include/coap3/coap_block_internal.h | 119 +++++++++++++- include/coap3/coap_cache.h | 24 +-- include/coap3/coap_cache_internal.h | 81 +++++++++ include/coap3/coap_net.h | 8 +- include/coap3/coap_net_internal.h | 49 +++++- include/coap3/coap_resource.h | 12 +- include/coap3/coap_resource_internal.h | 41 +++++ include/coap3/coap_subscribe.h | 10 +- include/coap3/coap_subscribe_internal.h | 50 +++++- include/coap3/coap_threadsafe_internal.h | 63 ------- src/coap_async.c | 57 ++++++- src/coap_block.c | 80 +++++++-- src/coap_cache.c | 58 ++++++- src/coap_gnutls.c | 26 +-- src/coap_io.c | 12 +- src/coap_io_lwip.c | 2 +- src/coap_mbedtls.c | 26 +-- src/coap_net.c | 67 +++++--- src/coap_openssl.c | 26 +-- src/coap_oscore.c | 102 ++++++------ src/coap_resource.c | 85 ++++++++-- src/coap_session.c | 28 ++-- src/coap_subscribe.c | 8 +- src/coap_threadsafe.c | 199 ----------------------- src/coap_tinydtls.c | 6 +- src/coap_wolfssl.c | 26 +-- src/coap_ws.c | 8 +- 30 files changed, 874 insertions(+), 523 deletions(-) diff --git a/include/coap3/coap_async.h b/include/coap3/coap_async.h index 4550a798de..fcb9eb85bb 100644 --- a/include/coap3/coap_async.h +++ b/include/coap3/coap_async.h @@ -53,9 +53,9 @@ int coap_async_is_supported(void); * @return A pointer to the registered coap_async_t object or @c * NULL in case of an error. */ -coap_async_t *coap_register_async(coap_session_t *session, - const coap_pdu_t *request, - coap_tick_t delay); +COAP_API coap_async_t *coap_register_async(coap_session_t *session, + const coap_pdu_t *request, + coap_tick_t delay); /** * Update the delay timeout, so changing when the registered @p async triggers. @@ -67,7 +67,7 @@ coap_async_t *coap_register_async(coap_session_t *session, * @param delay The amount of time to delay before sending response, 0 means * wait forever. */ -void coap_async_set_delay(coap_async_t *async, coap_tick_t delay); +COAP_API void coap_async_set_delay(coap_async_t *async, coap_tick_t delay); /** * Trigger the registered @p async. @@ -77,7 +77,7 @@ void coap_async_set_delay(coap_async_t *async, coap_tick_t delay); * * @param async The async object to trigger. */ -void coap_async_trigger(coap_async_t *async); +COAP_API void coap_async_trigger(coap_async_t *async); /** * Releases the memory that was allocated by coap_register_async() for the @@ -99,7 +99,7 @@ void coap_free_async(coap_session_t *session, coap_async_t *async); * @return A pointer to the object identified by @p token or @c NULL if * not found. */ -coap_async_t *coap_find_async(coap_session_t *session, coap_bin_const_t token); +COAP_API coap_async_t *coap_find_async(coap_session_t *session, coap_bin_const_t token); /** * Set the application data pointer held in @p async. This overwrites any diff --git a/include/coap3/coap_async_internal.h b/include/coap3/coap_async_internal.h index 0591a3311e..160882d746 100644 --- a/include/coap3/coap_async_internal.h +++ b/include/coap3/coap_async_internal.h @@ -41,6 +41,30 @@ struct coap_async_t { void *appdata; /**< User definable data pointer */ }; +/** + * Allocates a new coap_async_t object and fills its fields according to + * the given @p request. This function returns a pointer to the registered + * coap_async_t object or @c NULL on error. Note that this function will + * return @c NULL in case that an object with the same identifier is already + * registered. + * + * When the delay expires, a copy of the @p request will get sent to the + * appropriate request handler. + * + * Note: This function must be called in the locked state. + * + * @param session The session that is used for asynchronous transmissions. + * @param request The request that is handled asynchronously. + * @param delay The amount of time to delay before sending response, 0 means + * wait forever. + * + * @return A pointer to the registered coap_async_t object or @c + * NULL in case of an error. + */ +coap_async_t *coap_register_async_lkd(coap_session_t *session, + const coap_pdu_t *request, + coap_tick_t delay); + /** * Checks if there are any pending Async requests - if so, send them off. * Otherewise return the time remaining for the next Async to be triggered @@ -54,6 +78,58 @@ struct coap_async_t { */ coap_tick_t coap_check_async(coap_context_t *context, coap_tick_t now); +/** + * Retrieves the object identified by @p token from the list of asynchronous + * transactions that are registered with @p context. This function returns a + * pointer to that object or @c NULL if not found. + * + * Note: This function must be called in the locked state. + * + * @param session The session that is used for asynchronous transmissions. + * @param token The PDU's token of the object to retrieve. + * + * @return A pointer to the object identified by @p token or @c NULL if + * not found. + */ +coap_async_t *coap_find_async_lkd(coap_session_t *session, coap_bin_const_t token); + +/** + * Trigger the registered @p async. + * + * A copy of the original request will get sent to the appropriate request + * handler. + * + * Note: This function must be called in the locked state. + * + * @param async The async object to trigger. + */ +void coap_async_trigger_lkd(coap_async_t *async); + +/** + * Update the delay timeout, so changing when the registered @p async triggers. + * + * When the new delay expires, a copy of the original request will get sent to + * the appropriate request handler. + * + * Note: This function must be called in the locked state. + * + * @param async The object to update. + * @param delay The amount of time to delay before sending response, 0 means + * wait forever. + */ +void coap_async_set_delay_lkd(coap_async_t *async, coap_tick_t delay); + +/** + * Releases the memory that was allocated by coap_register_async() for the + * object @p async. + * + * Note: This function must be called in the locked state. + * + * @param session The session to use. + * @param async The object to delete. + */ +void coap_free_async_lkd(coap_session_t *session, coap_async_t *async); + /** * Removes and frees off all of the async entries for the given context. * diff --git a/include/coap3/coap_block.h b/include/coap3/coap_block.h index 154d687383..1c1fb1197d 100644 --- a/include/coap3/coap_block.h +++ b/include/coap3/coap_block.h @@ -335,12 +335,12 @@ typedef void (*coap_release_large_data_t)(coap_session_t *session, * * @return @c 1 if addition is successful, else @c 0. */ -int coap_add_data_large_request(coap_session_t *session, - coap_pdu_t *pdu, - size_t length, - const uint8_t *data, - coap_release_large_data_t release_func, - void *app_ptr); +COAP_API int coap_add_data_large_request(coap_session_t *session, + coap_pdu_t *pdu, + size_t length, + const uint8_t *data, + coap_release_large_data_t release_func, + void *app_ptr); /** * Associates given data with the @p response pdu that is passed as fourth @@ -392,18 +392,18 @@ int coap_add_data_large_request(coap_session_t *session, * * @return @c 1 if addition is successful, else @c 0. */ -int coap_add_data_large_response(coap_resource_t *resource, - coap_session_t *session, - const coap_pdu_t *request, - coap_pdu_t *response, - const coap_string_t *query, - uint16_t media_type, - int maxage, - uint64_t etag, - size_t length, - const uint8_t *data, - coap_release_large_data_t release_func, - void *app_ptr); +COAP_API int coap_add_data_large_response(coap_resource_t *resource, + coap_session_t *session, + const coap_pdu_t *request, + coap_pdu_t *response, + const coap_string_t *query, + uint16_t media_type, + int maxage, + uint64_t etag, + size_t length, + const uint8_t *data, + coap_release_large_data_t release_func, + void *app_ptr); /** * Set the context level CoAP block handling bits for handling RFC7959. diff --git a/include/coap3/coap_block_internal.h b/include/coap3/coap_block_internal.h index 5c27956ad9..c51efdcbbe 100644 --- a/include/coap3/coap_block_internal.h +++ b/include/coap3/coap_block_internal.h @@ -305,6 +305,72 @@ coap_lg_xmit_t *coap_find_lg_xmit_response(const coap_session_t *session, const coap_pdu_t *request, const coap_resource_t *resource, const coap_string_t *query); + +/** + * Associates given data with the @p response pdu that is passed as fourth + * parameter. + * + * This function will fail if data has already been added to the @p pdu. + * + * If all the data can be transmitted in a single PDU, this is functionally + * the same as coap_add_data() except @p release_func (if not NULL) will get + * invoked after data transmission. The Content-Format, Max-Age and ETag + * options may be added in as appropriate. + * + * Used by a server request handler to create the response. + * + * If the data spans multiple PDUs, then the data will get transmitted using + * (Q-)Block2 (response) option with the addition of the Size2 and ETag + * options. The underlying library will handle the transmission of the + * individual blocks. Once the body of data has been transmitted (or a + * failure occurred), then @p release_func (if not NULL) will get called so the + * application can de-allocate the @p data based on @p app_data. It is the + * responsibility of the application not to change the contents of @p data + * until the data transfer has completed. + * + * There is no need for the application to include the (Q-)Block2 option in the + * @p pdu. + * + * coap_add_data_large_response_lkd() (or the alternative coap_add_data_large_*() + * functions) must be called only once per PDU and must be the last PDU update + * before returning from the request handler function. + * + * Note: COAP_BLOCK_USE_LIBCOAP must be set by coap_context_set_block_mode() + * for libcoap to work correctly when using this function. + * + * Note: This function must be called in the locked state. + * + * @param resource The resource the data is associated with. + * @param session The coap session. + * @param request The requesting pdu. + * @param response The response pdu. + * @param query The query taken from the (original) requesting pdu. + * @param media_type The content format of the data. + * @param maxage The maxmimum life of the data. If @c -1, then there + * is no maxage. + * @param etag ETag to use if not 0. + * @param length The total length of the data. + * @param data The entire data block to transmit. + * @param release_func The function to call to de-allocate @p data or NULL if + * the function is not required. + * @param app_ptr A Pointer that the application can provide for when + * release_func() is called. + * + * @return @c 1 if addition is successful, else @c 0. + */ +int coap_add_data_large_response_lkd(coap_resource_t *resource, + coap_session_t *session, + const coap_pdu_t *request, + coap_pdu_t *response, + const coap_string_t *query, + uint16_t media_type, + int maxage, + uint64_t etag, + size_t length, + const uint8_t *data, + coap_release_large_data_t release_func, + void *app_ptr); + #endif /* COAP_SERVER_SUPPORT */ #if COAP_CLIENT_SUPPORT @@ -339,7 +405,7 @@ int coap_block_drop_resp_q_block2_crcv(coap_session_t *session, /** * The function checks that the code in a newly formed lg_xmit created by - * coap_add_data_large_response() is updated. + * coap_add_data_large_response_lkd() is updated. * * @param session The session. * @param request The request PDU to to check. @@ -354,6 +420,57 @@ void coap_check_code_lg_xmit(const coap_session_t *session, const coap_string_t *query); #if COAP_CLIENT_SUPPORT +/** + * Associates given data with the @p pdu that is passed as second parameter. + * + * This function will fail if data has already been added to the @p pdu. + * + * If all the data can be transmitted in a single PDU, this is functionally + * the same as coap_add_data_lkd() except @p release_func (if not NULL) will get + * invoked after data transmission. + * + * Used for a client request. + * + * If the data spans multiple PDUs, then the data will get transmitted using + * (Q-)Block1 option with the addition of the Size1 and Request-Tag options. + * The underlying library will handle the transmission of the individual blocks. + * Once the body of data has been transmitted (or a failure occurred), then + * @p release_func (if not NULL) will get called so the application can + * de-allocate the @p data based on @p app_data. It is the responsibility of + * the application not to change the contents of @p data until the data + * transfer has completed. + * + * There is no need for the application to include the (Q-)Block1 option in the + * @p pdu. + * + * coap_add_data_large_request_lkd() (or the alternative coap_add_data_large_*() + * functions) must be called only once per PDU and must be the last PDU update + * before the PDU is transmitted. The (potentially) initial data will get + * transmitted when coap_send() is invoked. + * + * Note: COAP_BLOCK_USE_LIBCOAP must be set by coap_context_set_block_mode() + * for libcoap to work correctly when using this function. + * + * Note: This function must be called in the locked state. + * + * @param session The session to associate the data with. + * @param pdu The PDU to associate the data with. + * @param length The length of data to transmit. + * @param data The data to transmit. + * @param release_func The function to call to de-allocate @p data or @c NULL + * if the function is not required. + * @param app_ptr A Pointer that the application can provide for when + * release_func() is called. + * + * @return @c 1 if addition is successful, else @c 0. + */ +int coap_add_data_large_request_lkd(coap_session_t *session, + coap_pdu_t *pdu, + size_t length, + const uint8_t *data, + coap_release_large_data_t release_func, + void *app_ptr); + /** * The function checks if the token needs to be updated before PDU is * presented to the application (only relevant to clients). diff --git a/include/coap3/coap_cache.h b/include/coap3/coap_cache.h index 5f0f558f34..60c3b70dea 100644 --- a/include/coap3/coap_cache.h +++ b/include/coap3/coap_cache.h @@ -123,8 +123,8 @@ void coap_delete_cache_key(coap_cache_key_t *cache_key); * * @return @return @c 1 if successful, else @c 0. */ -int coap_cache_ignore_options(coap_context_t *context, - const uint16_t *options, size_t count); +COAP_API int coap_cache_ignore_options(coap_context_t *context, + const uint16_t *options, size_t count); /** * Create a new cache-entry hash keyed by cache-key derived from the PDU. @@ -149,11 +149,11 @@ int coap_cache_ignore_options(coap_context_t *context, * * @return The returned cache-key or @c NULL if failure. */ -coap_cache_entry_t *coap_new_cache_entry(coap_session_t *session, - const coap_pdu_t *pdu, - coap_cache_record_pdu_t record_pdu, - coap_cache_session_based_t session_based, - unsigned int idle_time); +COAP_API coap_cache_entry_t *coap_new_cache_entry(coap_session_t *session, + const coap_pdu_t *pdu, + coap_cache_record_pdu_t record_pdu, + coap_cache_session_based_t session_based, + unsigned int idle_time); /** * Remove a cache-entry from the hash list and free off all the appropriate @@ -175,8 +175,8 @@ void coap_delete_cache_entry(coap_context_t *context, * * @return The cache-entry for @p cache_key or @c NULL if not found. */ -coap_cache_entry_t *coap_cache_get_by_key(coap_context_t *context, - const coap_cache_key_t *cache_key); +COAP_API coap_cache_entry_t *coap_cache_get_by_key(coap_context_t *context, + const coap_cache_key_t *cache_key); /** * Searches for a cache-entry corresponding to @p pdu. This @@ -190,9 +190,9 @@ coap_cache_entry_t *coap_cache_get_by_key(coap_context_t *context, * * @return The cache-entry for @p request or @c NULL if not found. */ -coap_cache_entry_t *coap_cache_get_by_pdu(coap_session_t *session, - const coap_pdu_t *pdu, - coap_cache_session_based_t session_based); +COAP_API coap_cache_entry_t *coap_cache_get_by_pdu(coap_session_t *session, + const coap_pdu_t *pdu, + coap_cache_session_based_t session_based); /** * Returns the PDU information stored in the @p coap_cache entry. diff --git a/include/coap3/coap_cache_internal.h b/include/coap3/coap_cache_internal.h index cdb3e79728..33a82c9a3e 100644 --- a/include/coap3/coap_cache_internal.h +++ b/include/coap3/coap_cache_internal.h @@ -58,6 +58,87 @@ struct coap_cache_entry_t { */ void coap_expire_cache_entries(coap_context_t *context); +/** + * Searches for a cache-entry identified by @p cache_key. This + * function returns the corresponding cache-entry or @c NULL + * if not found. + * + * Note: This function must be called in the locked state. + * + * @param context The context to use. + * @param cache_key The cache-key to get the hashed coap-entry. + * + * @return The cache-entry for @p cache_key or @c NULL if not found. + */ +coap_cache_entry_t *coap_cache_get_by_key_lkd(coap_context_t *context, + const coap_cache_key_t *cache_key); + +/** + * Searches for a cache-entry corresponding to @p pdu. This + * function returns the corresponding cache-entry or @c NULL if not + * found. + * + * Note: This function must be called in the locked state. + * + * @param session The session to use. + * @param pdu The CoAP request to search for. + * @param session_based COAP_CACHE_IS_SESSION_BASED if session based + * cache-key to be used, else COAP_CACHE_NOT_SESSION_BASED. + * + * @return The cache-entry for @p request or @c NULL if not found. + */ +coap_cache_entry_t *coap_cache_get_by_pdu_lkd(coap_session_t *session, + const coap_pdu_t *pdu, + coap_cache_session_based_t session_based); + +/** + * Define the CoAP options that are not to be included when calculating + * the cache-key. Options that are defined as Non-Cache and the Observe + * option are always ignored. + * + * Note: This function must be called in the locked state. + * + * @param context The context to save the ignored options information in. + * @param options The array of options to ignore. + * @param count The number of options to ignore. Use 0 to reset the + * options matching. + * + * @return @return @c 1 if successful, else @c 0. + */ +int coap_cache_ignore_options_lkd(coap_context_t *context, + const uint16_t *options, size_t count); + +/** + * Create a new cache-entry hash keyed by cache-key derived from the PDU. + * + * If @p session_based is set, then this cache-entry will get deleted when + * the session is freed off. + * If @p record_pdu is set, then the copied PDU will get freed off when + * this cache-entry is deleted. + * + * The cache-entry is maintained on a context hash list. + * + * Note: This function must be called in the locked state. + * + * @param session The session to use to derive the context from. + * @param pdu The pdu to use to generate the cache-key. + * @param record_pdu COAP_CACHE_RECORD_PDU if to take a copy of the PDU for + * later use, else COAP_CACHE_NOT_RECORD_PDU. + * @param session_based COAP_CACHE_IS_SESSION_BASED if to associate this + * cache-entry with the the session (which is embedded + * in the cache-entry), else COAP_CACHE_NOT_SESSION_BASED. + * @param idle_time Idle time in seconds before cache-entry is expired. + * If set to 0, it does not expire (but will get + * deleted if the session is deleted and it is session_based). + * + * @return The returned cache-key or @c NULL if failure. + */ +coap_cache_entry_t *coap_new_cache_entry_lkd(coap_session_t *session, + const coap_pdu_t *pdu, + coap_cache_record_pdu_t record_pdu, + coap_cache_session_based_t session_based, + unsigned int idle_time); + typedef void coap_digest_ctx_t; /** diff --git a/include/coap3/coap_net.h b/include/coap3/coap_net.h index 73d3919ee1..f98692867f 100644 --- a/include/coap3/coap_net.h +++ b/include/coap3/coap_net.h @@ -528,9 +528,9 @@ coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu); * @return The result from the associated event handler or 0 if none was * registered. */ -int coap_handle_event(coap_context_t *context, - coap_event_t event, - coap_session_t *session); +COAP_API int coap_handle_event(coap_context_t *context, + coap_event_t event, + coap_session_t *session); /** * Returns 1 if there are no messages to send or to dispatch in the context's * queues. @@ -541,7 +541,7 @@ int coap_handle_event(coap_context_t *context, * queued for transmission. Note that @c 0 does not mean there has * been a response to a transmitted request. */ -int coap_can_exit(coap_context_t *context); +COAP_API int coap_can_exit(coap_context_t *context); /** * Returns the current value of an internal tick counter. The counter counts \c diff --git a/include/coap3/coap_net_internal.h b/include/coap3/coap_net_internal.h index df064e8381..aea317c95a 100644 --- a/include/coap3/coap_net_internal.h +++ b/include/coap3/coap_net_internal.h @@ -180,9 +180,9 @@ struct coap_context_t { #else /* ! COAP_EPOLL_SUPPORT */ #if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI) fd_set readfds, writefds, exceptfds; /**< Used for select call - in coap_io_process_with_fds() */ + in coap_io_process_with_fds_lkd() */ coap_socket_t *sockets[64]; /**< Track different socket information - in coap_io_process_with_fds */ + in coap_io_process_with_fds_lkd() */ unsigned int num_sockets; /**< Number of sockets being tracked */ #endif /* ! RIOT_VERSION && ! WITH_CONTIKI */ #endif /* ! COAP_EPOLL_SUPPORT */ @@ -458,6 +458,36 @@ int coap_client_delay_first(coap_session_t *session); */ void coap_free_context_lkd(coap_context_t *context); +/** + * Invokes the event handler of @p context for the given @p event and + * @p data. + * + * Note: This function must be called in the locked state. + * + * @param context The CoAP context whose event handler is to be called. + * @param event The event to deliver. + * @param session The session related to @p event. + * @return The result from the associated event handler or 0 if none was + * registered. + */ +int coap_handle_event_lkd(coap_context_t *context, + coap_event_t event, + coap_session_t *session); + +/** + * Returns 1 if there are no messages to send or to dispatch in the context's + * queues. + * + * Note: This function must be called in the locked state. + * + * @param context The CoAP context to check. + * + * @return @c 0 if there are still pending transmits else @c 1 if nothing + * queued for transmission. Note that @c 0 does not mean there has + * been a response to a transmitted request. + */ +int coap_can_exit_lkd(coap_context_t *context); + /** @} */ /** @@ -505,7 +535,7 @@ void coap_io_do_epoll_lkd(coap_context_t *ctx, struct epoll_event *events, * * Note: This function must be called in the locked state. * - * coap_io_process() is called internally to try to send outstanding + * coap_io_process_lkd() is called internally to try to send outstanding * data as well as process any packets just received. * * @param context The CoAP context. @@ -579,14 +609,15 @@ unsigned int coap_io_prepare_io_lkd(coap_context_t *ctx, * The main I/O processing function. All pending network I/O is completed, * and then optionally waits for the next input packet. * - * This internally calls coap_io_prepare_io(), then select() for the appropriate - * sockets, updates COAP_SOCKET_CAN_xxx where appropriate and then calls - * coap_io_do_io() before returning with the time spent in the function. + * This internally calls coap_io_prepare_io_lkd(), then select() for the + * appropriate sockets, updates COAP_SOCKET_CAN_xxx where appropriate and then + * calls coap_io_do_io_lkd() before returning with the time spent in the + * function. * * Alternatively, if libcoap is compiled with epoll support, this internally - * calls coap_io_prepare_epoll(), then epoll_wait() for waiting for any file + * calls coap_io_prepare_epoll_lkd(), then epoll_wait() for waiting for any file * descriptors that have (internally) been set up with epoll_ctl() and - * finally coap_io_do_epoll() before returning with the time spent in the + * finally coap_io_do_epoll_lkd() before returning with the time spent in the * function. * * Note: This function must be called in the locked state. @@ -633,7 +664,7 @@ int coap_io_process_lkd(coap_context_t *ctx, uint32_t timeout_ms); * or NULL if not required. * * - * @return Number of milliseconds spent in coap_io_process_with_fds, or @c -1 + * @return Number of milliseconds spent in coap_io_process_with_fds_lkd, or @c -1 * if there was an error. If defined, readfds, writefds, exceptfds * are updated as returned by the internal select() call. */ diff --git a/include/coap3/coap_resource.h b/include/coap3/coap_resource.h index d49f5cc172..395fb9ef48 100644 --- a/include/coap3/coap_resource.h +++ b/include/coap3/coap_resource.h @@ -288,8 +288,8 @@ coap_resource_t *coap_resource_proxy_uri_init2(coap_method_handler_t handler, * * @return A pointer to the resource or @c NULL if not found. */ -coap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context, - coap_str_const_t *uri_path); +COAP_API coap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context, + coap_str_const_t *uri_path); /** * Get the uri_path from a @p resource. @@ -354,7 +354,7 @@ void coap_resource_release_userdata_handler(coap_context_t *context, * @param context The context to use. * @param resource The resource to store. */ -void coap_add_resource(coap_context_t *context, coap_resource_t *resource); +COAP_API void coap_add_resource(coap_context_t *context, coap_resource_t *resource); /** * Deletes a resource identified by @p resource. The storage allocated for that @@ -367,7 +367,7 @@ void coap_add_resource(coap_context_t *context, coap_resource_t *resource); * @return @c 1 if the resource was found (and destroyed), * @c 0 otherwise. */ -int coap_delete_resource(coap_context_t *context, coap_resource_t *resource); +COAP_API int coap_delete_resource(coap_context_t *context, coap_resource_t *resource); /** * Registers the specified @p handler as message handler for the request type @@ -499,7 +499,7 @@ coap_print_status_t coap_print_link(const coap_resource_t *resource, /** * @deprecated use coap_resource_notify_observers() instead. */ -COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, - const coap_string_t *query); +COAP_DEPRECATED COAP_API int coap_resource_set_dirty(coap_resource_t *r, + const coap_string_t *query); #endif /* COAP_RESOURCE_H_ */ diff --git a/include/coap3/coap_resource_internal.h b/include/coap3/coap_resource_internal.h index 2d69e36cf3..40811504b9 100644 --- a/include/coap3/coap_resource_internal.h +++ b/include/coap3/coap_resource_internal.h @@ -111,6 +111,33 @@ struct coap_resource_t { }; +/** + * Registers the given @p resource for @p context. The resource must have been + * created by coap_resource_init() or coap_resource_unknown_init(), the + * storage allocated for the resource will be released by coap_delete_resource_lkd(). + * + * Note: This function must be called in the locked state. + * + * @param context The context to use. + * @param resource The resource to store. + */ +void coap_add_resource_lkd(coap_context_t *context, coap_resource_t *resource); + +/** + * Deletes a resource identified by @p resource. The storage allocated for that + * resource is freed, and removed from the context. + * + * Note: This function must be called in the locked state. + * + * @param context This parameter is ignored, but kept for backward + * compatibility. + * @param resource The resource to delete. + * + * @return @c 1 if the resource was found (and destroyed), + * @c 0 otherwise. + */ +int coap_delete_resource_lkd(coap_context_t *context, coap_resource_t *resource); + /** * Deletes all resources from given @p context and frees their storage. * @@ -132,6 +159,20 @@ void coap_delete_all_resources(coap_context_t *context); HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \ } +/** + * Returns the resource identified by the unique string @p uri_path. If no + * resource was found, this function returns @c NULL. + * + * Note: This function must be called in the locked state. + * + * @param context The context to look for this resource. + * @param uri_path The unique string uri of the resource. + * + * @return A pointer to the resource or @c NULL if not found. + */ +coap_resource_t *coap_get_resource_from_uri_path_lkd(coap_context_t *context, + coap_str_const_t *uri_path); + /** * Deletes an attribute. * Note: This is for internal use only, as it is not deleted from its chain. diff --git a/include/coap3/coap_subscribe.h b/include/coap3/coap_subscribe.h index 9047a46e10..220665e486 100644 --- a/include/coap3/coap_subscribe.h +++ b/include/coap3/coap_subscribe.h @@ -60,8 +60,8 @@ void coap_resource_set_get_observable(coap_resource_t *resource, int mode); * * @return @c 1 if the Observe has been triggered, @c 0 otherwise. */ -int coap_resource_notify_observers(coap_resource_t *resource, - const coap_string_t *query); +COAP_API int coap_resource_notify_observers(coap_resource_t *resource, + const coap_string_t *query); /** * Checks all known resources to see if they are dirty and then notifies @@ -69,7 +69,7 @@ int coap_resource_notify_observers(coap_resource_t *resource, * * @param context The context to check for dirty resources. */ -void coap_check_notify(coap_context_t *context); +COAP_API void coap_check_notify(coap_context_t *context); /** * Callback handler definition called when a new observe has been set up, @@ -279,8 +279,8 @@ void coap_persist_set_observe_num(coap_resource_t *resource, * @return @c 1 if observe cancel transmission initiation is successful, * else @c 0. */ -int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, - coap_pdu_type_t message_type); +COAP_API int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, + coap_pdu_type_t message_type); /** @} */ diff --git a/include/coap3/coap_subscribe_internal.h b/include/coap3/coap_subscribe_internal.h index c3720b411a..59d0984b0c 100644 --- a/include/coap3/coap_subscribe_internal.h +++ b/include/coap3/coap_subscribe_internal.h @@ -147,6 +147,30 @@ int coap_delete_observer(coap_resource_t *resource, */ void coap_delete_observers(coap_context_t *context, coap_session_t *session); +/** + * Initiate the sending of an Observe packet for all observers of @p resource, + * optionally matching @p query if not NULL + * + * Note: This function must be called in the locked state. + * + * @param resource The CoAP resource to use. + * @param query The Query to match against or NULL + * + * @return @c 1 if the Observe has been triggered, @c 0 otherwise. + */ +int coap_resource_notify_observers_lkd(coap_resource_t *resource, + const coap_string_t *query); + +/** + * Checks all known resources to see if they are dirty and then notifies + * subscribed observers. + * + * Note: This function must be called in the locked state. + * + * @param context The context to check for dirty resources. + */ +void coap_check_notify_lkd(coap_context_t *context); + /** * Close down persist tracking, releasing any memory used. * @@ -154,7 +178,29 @@ void coap_delete_observers(coap_context_t *context, coap_session_t *session); */ void coap_persist_cleanup(coap_context_t *context); -/** @} */ - #endif /* COAP_SERVER_SUPPORT */ + +#if COAP_CLIENT_SUPPORT + +/** + * Cancel an observe that is being tracked by the client large receive logic. + * (coap_context_set_block_mode() has to be called) + * This will trigger the sending of an observe cancel pdu to the server. + * + * Note: This function must be called in the locked state. + * + * @param session The session that is being used for the observe. + * @param token The original token used to initiate the observation. + * @param message_type The COAP_MESSAGE_ type (NON or CON) to send the observe + * cancel pdu as. + * + * @return @c 1 if observe cancel transmission initiation is successful, + * else @c 0. + */ +int coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token, + coap_pdu_type_t message_type); + +#endif /* COAP_CLIENT_SUPPORT */ + +/** @} */ #endif /* COAP_SUBSCRIBE_INTERNAL_H_ */ diff --git a/include/coap3/coap_threadsafe_internal.h b/include/coap3/coap_threadsafe_internal.h index 92b880a0ff..e478ca8346 100644 --- a/include/coap3/coap_threadsafe_internal.h +++ b/include/coap3/coap_threadsafe_internal.h @@ -22,17 +22,6 @@ /* *INDENT-OFF* */ #ifndef COAP_THREAD_IGNORE_LOCKED_MAPPING -#define coap_add_data_large_request(a,b,c,d,e,f) coap_add_data_large_request_locked(a,b,c,d,e,f) -#define coap_add_data_large_response(a,b,c,d,e,f,g,h,i,j,k,l) coap_add_data_large_response_locked(a,b,c,d,e,f,g,h,i,j,k,l) -#define coap_add_resource(c,r) coap_add_resource_locked(c,r) -#define coap_async_trigger(a) coap_async_trigger_locked(a) -#define coap_async_set_delay(a,d) coap_async_set_delay_locked(a,d) -#define coap_cache_get_by_key(s,c) coap_cache_get_by_key_locked(s,c) -#define coap_cache_get_by_pdu(s,r,b) coap_cache_get_by_pdu_locked(s,r,b) -#define coap_cache_ignore_options(c,o,n) coap_cache_ignore_options_locked(c,o,n) -#define coap_can_exit(c) coap_can_exit_locked(c) -#define coap_cancel_observe(s,t,v) coap_cancel_observe_locked(s,t,v) -#define coap_check_notify(s) coap_check_notify_locked(s) #define coap_context_oscore_server(c,o) coap_context_oscore_server_locked(c,o) #define coap_context_set_block_mode(c,b) coap_context_set_block_mode_locked(c,b) #define coap_context_set_max_block_size(c,m) coap_context_set_max_block_size_locked(c,m) @@ -40,13 +29,9 @@ #define coap_context_set_pki_root_cas(c,f,d) coap_context_set_pki_root_cas_locked(c,f,d) #define coap_context_set_psk(c,h,k,l) coap_context_set_psk_locked(c,h,k,l) #define coap_context_set_psk2(c,s) coap_context_set_psk2_locked(c,s) -#define coap_find_async(s,t) coap_find_async_locked(s,t) #define coap_delete_oscore_recipient(s,r) coap_delete_oscore_recipient_locked(s,r) -#define coap_delete_resource(c,r) coap_delete_resource_locked(c,r) #define coap_free_endpoint(e) coap_free_endpoint_locked(e) -#define coap_get_resource_from_uri_path(c,u) coap_get_resource_from_uri_path_locked(c,u) #define coap_join_mcast_group_intf(c,g,i) coap_join_mcast_group_intf_locked(c,g,i) -#define coap_new_cache_entry(s,p,r,b,i) coap_new_cache_entry_locked(s,p,r,b,i) #define coap_new_client_session(c,l,s,p) coap_new_client_session_locked(c,l,s,p) #define coap_new_client_session_oscore(c,l,s,p,o) coap_new_client_session_oscore_locked(c,l,s,p,o) #define coap_new_client_session_oscore_pki(c,l,s,p,d,o) coap_new_client_session_oscore_pki_locked(c,l,s,p,d,o) @@ -62,10 +47,7 @@ #define coap_persist_startup(c,d,o,m,s) coap_persist_startup_locked(c,d,o,m,s) #define coap_persist_stop(c) coap_persist_stop_locked(c) #define coap_pdu_duplicate(o,s,l,t,d) coap_pdu_duplicate_locked(o,s,l,t,d) -#define coap_register_async(s,r,d) coap_register_async_locked(s,r,d) #define coap_register_option(c,t) coap_register_option_locked(c,t) -#define coap_resource_notify_observers(r,q) coap_resource_notify_observers_locked(r,q) -#define coap_resource_set_dirty(r,q) coap_resource_set_dirty_locked(r,q) #define coap_send(s,p) coap_send_locked(s,p) #define coap_send_ack(s,r) coap_send_ack_locked(s,r) #define coap_send_error(s,r,c,o) coap_send_error_locked(s,r,c,o) @@ -81,39 +63,6 @@ /* Locked equivalend functions */ -int coap_add_data_large_request_locked(coap_session_t *session, - coap_pdu_t *pdu, - size_t length, - const uint8_t *data, - coap_release_large_data_t release_func, - void *app_ptr); -int coap_add_data_large_response_locked(coap_resource_t *resource, - coap_session_t *session, - const coap_pdu_t *request, - coap_pdu_t *response, - const coap_string_t *query, - uint16_t media_type, - int maxage, - uint64_t etag, - size_t length, - const uint8_t *data, - coap_release_large_data_t release_func, - void *app_ptr); -void coap_add_resource_locked(coap_context_t *context, coap_resource_t *resource); -void coap_async_trigger_locked(coap_async_t *async); -void coap_async_set_delay_locked(coap_async_t *async, coap_tick_t delay); -coap_cache_entry_t *coap_cache_get_by_key_locked(coap_context_t *context, - const coap_cache_key_t *cache_key); -coap_cache_entry_t *coap_cache_get_by_pdu_locked(coap_session_t *session, - const coap_pdu_t *request, - coap_cache_session_based_t session_based); -int coap_cache_ignore_options_locked(coap_context_t *ctx, - const uint16_t *options, - size_t count); -int coap_can_exit_locked(coap_context_t *context); -int coap_cancel_observe_locked(coap_session_t *session, coap_binary_t *token, - coap_pdu_type_t type); -void coap_check_notify_locked(coap_context_t *context); int coap_context_oscore_server_locked(coap_context_t *context, coap_oscore_conf_t *oscore_conf); void coap_context_set_block_mode_locked(coap_context_t *context, @@ -130,11 +79,7 @@ int coap_context_set_psk2_locked(coap_context_t *ctx, coap_dtls_spsk_t *setup_data); int coap_delete_oscore_recipient_locked(coap_context_t *context, coap_bin_const_t *recipient_id); -int coap_delete_resource_locked(coap_context_t *context, coap_resource_t *resource); -coap_async_t *coap_find_async_locked(coap_session_t *session, coap_bin_const_t token); void coap_free_endpoint_locked(coap_endpoint_t *ep); -coap_resource_t *coap_get_resource_from_uri_path_locked(coap_context_t *context, - coap_str_const_t *uri_path); int coap_join_mcast_group_intf_locked(coap_context_t *ctx, const char *group_name, const char *ifname); coap_subscription_t *coap_persist_observe_add_locked(coap_context_t *context, @@ -149,13 +94,7 @@ int coap_persist_startup_locked(coap_context_t *context, const char *obs_cnt_save_file, uint32_t save_freq); void coap_persist_stop_locked(coap_context_t *context); -coap_async_t *coap_register_async_locked(coap_session_t *session, const coap_pdu_t *request, - coap_tick_t delay); size_t coap_session_max_pdu_size_locked(const coap_session_t *session); -coap_cache_entry_t *coap_new_cache_entry_locked(coap_session_t *session, const coap_pdu_t *pdu, - coap_cache_record_pdu_t record_pdu, - coap_cache_session_based_t session_based, - unsigned int idle_timeout); coap_session_t *coap_new_client_session_locked(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, @@ -206,8 +145,6 @@ coap_pdu_t *coap_pdu_duplicate_locked(const coap_pdu_t *old_pdu, const uint8_t *token, coap_opt_filter_t *drop_options); void coap_register_option_locked(coap_context_t *ctx, uint16_t type); -int coap_resource_notify_observers_locked(coap_resource_t *r, - const coap_string_t *query); int coap_resource_set_dirty_locked(coap_resource_t *r, const coap_string_t *query); coap_mid_t coap_send_locked(coap_session_t *session, coap_pdu_t *pdu); diff --git a/src/coap_async.c b/src/coap_async.c index ba39206bea..75de18ae4a 100644 --- a/src/coap_async.c +++ b/src/coap_async.c @@ -35,9 +35,20 @@ coap_async_is_supported(void) { return 1; } -coap_async_t * +COAP_API coap_async_t * coap_register_async(coap_session_t *session, const coap_pdu_t *request, coap_tick_t delay) { + coap_async_t *async; + + coap_lock_lock(session->context, return NULL); + async = coap_register_async_lkd(session, request, delay); + coap_lock_unlock(session->context); + return async; +} + +coap_async_t * +coap_register_async_lkd(coap_session_t *session, + const coap_pdu_t *request, coap_tick_t delay) { coap_async_t *s; size_t len; const uint8_t *data; @@ -81,7 +92,7 @@ coap_register_async(coap_session_t *session, s->pdu = coap_pdu_duplicate(request, session, request->actual_token.length, request->actual_token.s, NULL); if (s->pdu == NULL) { - coap_free_async(session, s); + coap_free_async_lkd(session, s); coap_log_crit("coap_register_async: insufficient memory\n"); return NULL; } @@ -92,13 +103,20 @@ coap_register_async(coap_session_t *session, s->session = coap_session_reference(session); - coap_async_set_delay(s, delay); + coap_async_set_delay_lkd(s, delay); return s; } -void +COAP_API void coap_async_trigger(coap_async_t *async) { + coap_lock_lock(async->session->context, return); + coap_async_trigger_lkd(async); + coap_lock_unlock(async->session->context); +} + +void +coap_async_trigger_lkd(coap_async_t *async) { assert(async != NULL); coap_lock_check_locked(async->session->context); coap_ticks(&async->delay); @@ -108,9 +126,15 @@ coap_async_trigger(coap_async_t *async) { coap_update_io_timer(async->session->context, 0); } +COAP_API void +coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { + coap_lock_lock(async->session->context, return); + coap_async_set_delay_lkd(async, delay); + coap_lock_unlock(async->session->context); +} void -coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { +coap_async_set_delay_lkd(coap_async_t *async, coap_tick_t delay) { coap_tick_t now; coap_lock_check_locked(async->session->context); @@ -132,10 +156,20 @@ coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { } } -coap_async_t * +COAP_API coap_async_t * coap_find_async(coap_session_t *session, coap_bin_const_t token) { coap_async_t *tmp; + coap_lock_lock(session->context, return NULL); + tmp = coap_find_async_lkd(session, token); + coap_lock_unlock(session->context); + return tmp; +} + +coap_async_t * +coap_find_async_lkd(coap_session_t *session, coap_bin_const_t token) { + coap_async_t *tmp; + coap_lock_check_locked(session->context); SEARCH_PAIR(session->context->async_state, tmp, session, session, @@ -160,8 +194,15 @@ coap_free_async_sub(coap_context_t *context, coap_async_t *s) { } void -coap_free_async(coap_session_t *session, coap_async_t *s) { - coap_free_async_sub(session->context, s); +coap_free_async(coap_session_t *session, coap_async_t *async) { + coap_lock_lock(session->context, return); + coap_free_async_lkd(session, async); + coap_lock_unlock(session->context); +} + +void +coap_free_async_lkd(coap_session_t *session, coap_async_t *async) { + coap_free_async_sub(session->context, async); } void diff --git a/src/coap_block.c b/src/coap_block.c index fa20114d46..032a5a1cac 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -421,9 +421,20 @@ full_match(const uint8_t *a, size_t alen, #if COAP_CLIENT_SUPPORT -int +COAP_API int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t type) { + int ret; + + coap_lock_lock(session->context, return 0); + ret = coap_cancel_observe_lkd(session, token, type); + coap_lock_unlock(session->context); + return ret; +} + +int +coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token, + coap_pdu_type_t type) { coap_lg_crcv_t *lg_crcv, *q; assert(session); @@ -488,7 +499,7 @@ coap_cancel_observe(coap_session_t *session, coap_binary_t *token, buf); } if (coap_get_data(&lg_crcv->pdu, &size, &data)) - coap_add_data_large_request(session, pdu, size, data, NULL, NULL); + coap_add_data_large_request_lkd(session, pdu, size, data, NULL, NULL); /* * Need to fix lg_xmit stateless token as using tokens from @@ -706,7 +717,7 @@ coap_add_data_large_internal(coap_session_t *session, LL_DELETE(session->lg_xmit, lg_xmit); coap_block_delete_lg_xmit(session, lg_xmit); lg_xmit = NULL; - coap_handle_event(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); + coap_handle_event_lkd(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); break; } } @@ -738,7 +749,7 @@ coap_add_data_large_internal(coap_session_t *session, LL_DELETE(session->lg_xmit, lg_xmit); coap_block_delete_lg_xmit(session, lg_xmit); lg_xmit = NULL; - coap_handle_event(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); + coap_handle_event_lkd(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); } #endif /* COAP_SERVER_SUPPORT */ } @@ -1064,13 +1075,30 @@ coap_add_data_large_internal(coap_session_t *session, } #if COAP_CLIENT_SUPPORT -int +COAP_API int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, - void *app_ptr) { + void *app_ptr + ) { + int ret; + + coap_lock_lock(session->context, return 0); + ret = coap_add_data_large_request_lkd(session, pdu, length, data, + release_func, app_ptr); + coap_lock_unlock(session->context); + return ret; +} + +int +coap_add_data_large_request_lkd(coap_session_t *session, + coap_pdu_t *pdu, + size_t length, + const uint8_t *data, + coap_release_large_data_t release_func, + void *app_ptr) { /* * Delay if session->doing_first is set. * E.g. Reliable and CSM not in yet for checking block support @@ -1086,7 +1114,7 @@ coap_add_data_large_request(coap_session_t *session, #endif /* ! COAP_CLIENT_SUPPORT */ #if COAP_SERVER_SUPPORT -int +COAP_API int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, @@ -1100,6 +1128,30 @@ coap_add_data_large_response(coap_resource_t *resource, coap_release_large_data_t release_func, void *app_ptr ) { + int ret; + + coap_lock_lock(session->context, return 0); + ret = coap_add_data_large_response_lkd(resource, session, request, + response, query, media_type, maxage, etag, + length, data, release_func, app_ptr); + coap_lock_unlock(session->context); + return ret; +} + +int +coap_add_data_large_response_lkd(coap_resource_t *resource, + coap_session_t *session, + const coap_pdu_t *request, + coap_pdu_t *response, + const coap_string_t *query, + uint16_t media_type, + int maxage, + uint64_t etag, + size_t length, + const uint8_t *data, + coap_release_large_data_t release_func, + void *app_ptr + ) { unsigned char buf[4]; coap_block_b_t block; int block_requested = 0; @@ -1241,7 +1293,7 @@ coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, /* Expire this entry */ LL_DELETE(session->lg_xmit, p); coap_block_delete_lg_xmit(session, p); - coap_handle_event(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); + coap_handle_event_lkd(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); } else { /* Delay until the lg_xmit needs to expire */ if (*tim_rem > p->last_sent + partial_timeout - now) { @@ -2330,7 +2382,7 @@ add_block_send(uint32_t num, int is_continue, send_track *out_blocks, * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2 * request. * - * This is set up using coap_add_data_large_response() + * This is set up using coap_add_data_large_response_lkd() * * Server is sending a large data response to GET / observe (Block2) * @@ -2933,7 +2985,7 @@ coap_handle_request_put_block(coap_context_t *context, if (!check_if_received_block(&p->rec_blocks, block.num)) { /* Update list of blocks received */ if (!update_received_blocks(&p->rec_blocks, block.num)) { - coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session); + coap_handle_event_lkd(context, COAP_EVENT_PARTIAL_BLOCK, session); coap_add_data(response, sizeof("Too many missing blocks")-1, (const uint8_t *)"Too many missing blocks"); response->code = COAP_RESPONSE_CODE(408); @@ -3232,7 +3284,7 @@ track_echo(coap_session_t *session, coap_pdu_t *rcvd) { * * Client receives large data acknowledgement from server (Block1) * - * This is set up using coap_add_data_large_request() + * This is set up using coap_add_data_large_request_lkd() * * Client is using GET etc. * @@ -3473,7 +3525,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, return 0; fail_body: - coap_handle_event(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); + coap_handle_event_lkd(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); /* There has been an internal error of some sort */ rcvd->code = COAP_RESPONSE_CODE(500); lg_xmit_finished: @@ -3718,7 +3770,7 @@ coap_handle_response_get_block(coap_context_t *context, coap_log_warn("Data body updated during receipt - new request started\n"); if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY)) - coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session); + coap_handle_event_lkd(context, COAP_EVENT_PARTIAL_BLOCK, session); p->initial = 1; coap_free_type(COAP_STRING, p->body_data); @@ -3788,7 +3840,7 @@ coap_handle_response_get_block(coap_context_t *context, #endif /* COAP_Q_BLOCK_SUPPORT */ /* Update list of blocks received */ if (!update_received_blocks(&p->rec_blocks, block.num)) { - coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session); + coap_handle_event_lkd(context, COAP_EVENT_PARTIAL_BLOCK, session); goto fail_resp; } updated_block = 1; diff --git a/src/coap_cache.c b/src/coap_cache.c index 7a9cedf182..3bcb862a9a 100644 --- a/src/coap_cache.c +++ b/src/coap_cache.c @@ -44,10 +44,22 @@ is_cache_key(uint16_t option_type, size_t cache_ignore_count, return 1; } -int +COAP_API int coap_cache_ignore_options(coap_context_t *ctx, const uint16_t *options, size_t count) { + int ret; + + coap_lock_lock(ctx, return 0); + ret = coap_cache_ignore_options_lkd(ctx, options, count); + coap_lock_unlock(ctx); + return ret; +} + +int +coap_cache_ignore_options_lkd(coap_context_t *ctx, + const uint16_t *options, + size_t count) { coap_lock_check_locked(ctx); if (ctx->cache_ignore_options) { coap_free_type(COAP_STRING, ctx->cache_ignore_options); @@ -149,11 +161,25 @@ coap_delete_cache_key(coap_cache_key_t *cache_key) { coap_free_type(COAP_CACHE_KEY, cache_key); } -coap_cache_entry_t * +COAP_API coap_cache_entry_t * coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, coap_cache_record_pdu_t record_pdu, coap_cache_session_based_t session_based, unsigned int idle_timeout) { + coap_cache_entry_t *cache; + + coap_lock_lock(session->context, return NULL); + cache = coap_new_cache_entry_lkd(session, pdu, record_pdu, session_based, + idle_timeout); + coap_lock_unlock(session->context); + return cache; +} + +coap_cache_entry_t * +coap_new_cache_entry_lkd(coap_session_t *session, const coap_pdu_t *pdu, + coap_cache_record_pdu_t record_pdu, + coap_cache_session_based_t session_based, + unsigned int idle_timeout) { coap_cache_entry_t *entry; coap_lock_check_locked(session->context); @@ -195,8 +221,18 @@ coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, return entry; } -coap_cache_entry_t * +COAP_API coap_cache_entry_t * coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) { + coap_cache_entry_t *cache; + + coap_lock_lock(ctx, return NULL); + cache = coap_cache_get_by_key_lkd(ctx, cache_key); + coap_lock_unlock(ctx); + return cache; +} + +coap_cache_entry_t * +coap_cache_get_by_key_lkd(coap_context_t *ctx, const coap_cache_key_t *cache_key) { coap_cache_entry_t *cache_entry = NULL; coap_lock_check_locked(ctx); @@ -211,10 +247,22 @@ coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) { return cache_entry; } -coap_cache_entry_t * +COAP_API coap_cache_entry_t * coap_cache_get_by_pdu(coap_session_t *session, const coap_pdu_t *request, coap_cache_session_based_t session_based) { + coap_cache_entry_t *entry; + + coap_lock_lock(session->context, return NULL); + entry = coap_cache_get_by_pdu_lkd(session, request, session_based); + coap_lock_unlock(session->context); + return entry; +} + +coap_cache_entry_t * +coap_cache_get_by_pdu_lkd(coap_session_t *session, + const coap_pdu_t *request, + coap_cache_session_based_t session_based) { coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based); coap_cache_entry_t *cache_entry; @@ -222,7 +270,7 @@ coap_cache_get_by_pdu(coap_session_t *session, return NULL; coap_lock_check_locked(session->context); - cache_entry = coap_cache_get_by_key(session->context, cache_key); + cache_entry = coap_cache_get_by_key_lkd(session->context, cache_key); coap_delete_cache_key(cache_key); if (cache_entry && cache_entry->idle_timeout > 0) { coap_ticks(&cache_entry->expire_ticks); diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index b144e4761e..75a8860222 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -2271,7 +2271,7 @@ coap_dtls_free_session(coap_session_t *c_session) { COAP_PROTO_NOT_RELIABLE(c_session->proto) ? COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP); c_session->tls = NULL; - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CLOSED, c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CLOSED, c_session); } } @@ -2338,7 +2338,7 @@ coap_dtls_send(coap_session_t *c_session, } if (c_session->dtls_event >= 0) { - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); @@ -2440,8 +2440,8 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, c_session->dtls_event = -1; if (g_env->established) { if (c_session->state == COAP_SESSION_STATE_HANDSHAKE) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, - c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, + c_session); gnutls_transport_set_ptr(g_env->g_session, c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } @@ -2490,7 +2490,7 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); @@ -2696,7 +2696,7 @@ coap_tls_new_client_session(coap_session_t *c_session) { c_session->tls = g_env; ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } return g_env; @@ -2742,7 +2742,7 @@ coap_tls_new_server_session(coap_session_t *c_session) { c_session->tls = g_env; ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } return g_env; @@ -2805,8 +2805,8 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, } else { ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, - c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, + c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); ret = 0; } else { @@ -2817,7 +2817,7 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); @@ -2855,8 +2855,8 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { if (!g_env->established && !g_env->sent_alert) { ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, - c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, + c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); ret = 0; } @@ -2898,7 +2898,7 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); diff --git a/src/coap_io.c b/src/coap_io.c index 191a4d0060..7a3e1d9642 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -513,7 +513,7 @@ coap_update_io_timer(coap_context_t *context, coap_tick_t delay) { ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL); if (ret == -1) { coap_log_err("%s: timerfd_settime failed: %s (%d)\n", - "coap_resource_notify_observers", + "coap_update_io_timer", coap_socket_strerror(), errno); } #ifdef COAP_DEBUG_WAKEUP_TIMES @@ -1194,7 +1194,7 @@ coap_io_prepare_epoll_lkd(coap_context_t *ctx, coap_tick_t now) { #ifndef COAP_EPOLL_SUPPORT (void)ctx; (void)now; - coap_log_emerg("coap_io_prepare_epoll_lkd() requires libcoap compiled for using epoll\n"); + coap_log_emerg("coap_io_prepare_epoll() requires libcoap compiled for using epoll\n"); return 0; #else /* COAP_EPOLL_SUPPORT */ coap_socket_t *sockets[1]; @@ -1228,7 +1228,7 @@ coap_io_prepare_epoll_lkd(coap_context_t *ctx, coap_tick_t now) { ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL); if (ret == -1) { coap_log_err("%s: timerfd_settime failed: %s (%d)\n", - "coap_io_prepare_epoll_lkd", + "coap_io_prepare_epoll", coap_socket_strerror(), errno); } } @@ -1281,7 +1281,7 @@ coap_io_prepare_io_lkd(coap_context_t *ctx, #if COAP_SERVER_SUPPORT /* Check to see if we need to send off any Observe requests */ - coap_check_notify(ctx); + coap_check_notify_lkd(ctx); #if COAP_ASYNC_SUPPORT /* Check to see if we need to send off any Async requests */ @@ -1340,7 +1340,7 @@ coap_io_prepare_io_lkd(coap_context_t *ctx, s->delayqueue == NULL && (s->last_rx_tx + session_timeout <= now || s->state == COAP_SESSION_STATE_NONE)) { - coap_handle_event(ctx, COAP_EVENT_SERVER_SESSION_DEL, s); + coap_handle_event_lkd(ctx, COAP_EVENT_SERVER_SESSION_DEL, s); coap_session_free(s); } else { if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 && @@ -1419,7 +1419,7 @@ coap_io_prepare_io_lkd(coap_context_t *ctx, /* Some issue - not safe to continue processing */ continue; if (s->last_ping > 0 && s->last_pong < s->last_ping) { - coap_handle_event(s->context, COAP_EVENT_KEEPALIVE_FAILURE, s); + coap_handle_event_lkd(s->context, COAP_EVENT_KEEPALIVE_FAILURE, s); } s->last_rx_tx = now; s->last_ping = now; diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index 01944fedae..9f09f15407 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -446,7 +446,7 @@ do_tcp_err(void *arg, err_t err) { (void)err; - coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_TCP_FAILED, session); /* * as per tcp_err() documentation, the corresponding pcb is already freed * when this callback is called. So, stop a double free when diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 8c71581186..6c70f4f875 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -1974,7 +1974,7 @@ coap_dtls_free_session(coap_session_t *c_session) { if (c_session && c_session->context && c_session->tls) { coap_dtls_free_mbedtls_env(c_session->tls); c_session->tls = NULL; - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CLOSED, c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CLOSED, c_session); } return; } @@ -2041,7 +2041,7 @@ coap_dtls_send(coap_session_t *c_session, if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); @@ -2165,8 +2165,8 @@ coap_dtls_receive(coap_session_t *c_session, #endif /* COAP_CONSTRAINED_STACK */ if (c_session->state == COAP_SESSION_STATE_HANDSHAKE) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, - c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, + c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } @@ -2216,7 +2216,7 @@ coap_dtls_receive(coap_session_t *c_session, if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); @@ -2349,7 +2349,7 @@ coap_tls_new_client_session(coap_session_t *c_session) { c_session->tls = m_env; ret = do_mbedtls_handshake(c_session, m_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } return m_env; @@ -2380,7 +2380,7 @@ coap_tls_new_server_session(coap_session_t *c_session) { c_session->tls = m_env; ret = do_mbedtls_handshake(c_session, m_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } return m_env; @@ -2448,8 +2448,8 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, } else { ret = do_mbedtls_handshake(c_session, m_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, - c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, + c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } else { ret = -1; @@ -2459,7 +2459,7 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); @@ -2498,8 +2498,8 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { if (!m_env->established && !m_env->sent_alert) { ret = do_mbedtls_handshake(c_session, m_env); if (ret == 1) { - coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, - c_session); + coap_handle_event_lkd(c_session->context, COAP_EVENT_DTLS_CONNECTED, + c_session); c_session->sock.lfunc[COAP_LAYER_TLS].l_establish(c_session); } } @@ -2537,7 +2537,7 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { if (c_session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (c_session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(c_session->context, c_session->dtls_event, c_session); + coap_handle_event_lkd(c_session->context, c_session->dtls_event, c_session); if (c_session->dtls_event == COAP_EVENT_DTLS_ERROR || c_session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); diff --git a/src/coap_net.c b/src/coap_net.c index df2fef58bd..282fd3da24 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -1291,7 +1291,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { /* Unfortunately need to change the ptr type to be r/w */ memcpy(&tmp.s, &pdu->actual_token.s, sizeof(tmp.s)); tmp.length = pdu->actual_token.length; - ret = coap_cancel_observe(session, &tmp, pdu->type); + ret = coap_cancel_observe_lkd(session, &tmp, pdu->type); if (ret == 1) { /* Observe Cancel successfully sent */ coap_delete_pdu(pdu); @@ -1739,7 +1739,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { coap_tick_t next_delay; node->retransmit_cnt++; - coap_handle_event(context, COAP_EVENT_MSG_RETRANSMITTED, node->session); + coap_handle_event_lkd(context, COAP_EVENT_MSG_RETRANSMITTED, node->session); next_delay = (coap_tick_t)node->timeout << node->retransmit_cnt; if (context->ping_timeout && @@ -1861,10 +1861,10 @@ coap_connect_session(coap_session_t *session, coap_tick_t now) { #else /* !COAP_DISABLE_TCP */ if (coap_netif_strm_connect2(session)) { session->last_rx_tx = now; - coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_TCP_CONNECTED, session); session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session); } else { - coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_TCP_FAILED, session); coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE); } #endif /* !COAP_DISABLE_TCP */ @@ -1959,7 +1959,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) } if (!coap_pdu_parse(session->proto, packet->payload, bytes_read, pdu)) { - coap_handle_event(session->context, COAP_EVENT_BAD_PACKET, session); + coap_handle_event_lkd(session->context, COAP_EVENT_BAD_PACKET, session); coap_log_warn("discard malformed PDU\n"); coap_delete_pdu(pdu); #if COAP_CONSTRAINED_STACK @@ -2172,7 +2172,7 @@ coap_io_do_io_lkd(coap_context_t *ctx, coap_tick_t now) { #ifdef COAP_EPOLL_SUPPORT (void)ctx; (void)now; - coap_log_emerg("coap_io_do_io_lkd() requires libcoap not compiled for using epoll\n"); + coap_log_emerg("coap_io_do_io() requires libcoap not compiled for using epoll\n"); #else /* ! COAP_EPOLL_SUPPORT */ coap_session_t *s, *rtmp; @@ -2238,7 +2238,7 @@ coap_io_do_epoll_lkd(coap_context_t *ctx, struct epoll_event *events, size_t nev (void)ctx; (void)events; (void)nevents; - coap_log_emerg("coap_io_do_epoll_lkd() requires libcoap compiled for using epoll\n"); + coap_log_emerg("coap_io_do_epoll() requires libcoap compiled for using epoll\n"); #else /* COAP_EPOLL_SUPPORT */ coap_tick_t now; size_t j; @@ -2363,7 +2363,7 @@ coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, goto error; if (!coap_pdu_parse(session->proto, msg, msg_len, pdu)) { - coap_handle_event(session->context, COAP_EVENT_BAD_PACKET, session); + coap_handle_event_lkd(session->context, COAP_EVENT_BAD_PACKET, session); coap_log_warn("discard malformed PDU\n"); goto error; } @@ -2687,13 +2687,13 @@ hnd_get_wellknown(coap_resource_t *resource, goto error; } free_wellknown_response(session, data_string); - } else if (!coap_add_data_large_response(resource, session, request, - response, query, - COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, - -1, 0, data_string->length, - data_string->s, - free_wellknown_response, - data_string)) { + } else if (!coap_add_data_large_response_lkd(resource, session, request, + response, query, + COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, + -1, 0, data_string->length, + data_string->s, + free_wellknown_response, + data_string)) { goto error_released; } } @@ -2933,7 +2933,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu } } #if COAP_ASYNC_SUPPORT - async = coap_find_async(session, pdu->actual_token); + async = coap_find_async_lkd(session, pdu->actual_token); if (async) { coap_tick_t now; @@ -3068,7 +3068,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu if (!is_proxy_uri && !is_proxy_scheme) { /* try to find the resource from the request URI */ coap_str_const_t uri_path_c = { uri_path->length, uri_path->s }; - resource = coap_get_resource_from_uri_path(context, &uri_path_c); + resource = coap_get_resource_from_uri_path_lkd(context, &uri_path_c); } if ((resource == NULL) || (resource->is_unknown == 1) || @@ -4069,7 +4069,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, COAP_NACK_BAD_RESPONSE, sent->id)); } } else { - coap_handle_event(context, COAP_EVENT_BAD_PACKET, session); + coap_handle_event_lkd(context, COAP_EVENT_BAD_PACKET, session); } } coap_delete_node_lkd(sent); @@ -4141,7 +4141,19 @@ coap_event_name(coap_event_t event) { #endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */ int -coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session) { +coap_handle_event(coap_context_t *context, coap_event_t event, + coap_session_t *session) { + int ret; + + coap_lock_lock(context, return 0); + ret = coap_handle_event_lkd(context, event, session); + coap_lock_unlock(context); + return ret; +} + +int +coap_handle_event_lkd(coap_context_t *context, coap_event_t event, + coap_session_t *session) { coap_log_debug("***EVENT: %s\n", coap_event_name(event)); if (context->handle_event) { @@ -4149,13 +4161,22 @@ coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *s coap_lock_callback_ret(ret, context, context->handle_event(session, event)); return ret; - } else { - return 0; } + return 0; } -int +COAP_API int coap_can_exit(coap_context_t *context) { + int ret; + + coap_lock_lock(context, return 0); + ret = coap_can_exit_lkd(context); + coap_lock_unlock(context); + return ret; +} + +int +coap_can_exit_lkd(coap_context_t *context) { coap_session_t *s, *rtmp; if (!context) return 1; @@ -4197,7 +4218,7 @@ coap_check_async(coap_context_t *context, coap_tick_t now) { handle_request(context, async->session, async->pdu); /* Remove this async entry as it has now fired */ - coap_free_async(async->session, async); + coap_free_async_lkd(async->session, async); } else { if (next_due == 0 || next_due > async->delay - now) next_due = async->delay - now; diff --git a/src/coap_openssl.c b/src/coap_openssl.c index cfbb851579..d795faebc9 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -3466,7 +3466,7 @@ coap_dtls_free_session(coap_session_t *session) { SSL_free(ssl); session->tls = NULL; if (session->context) - coap_handle_event(session->context, COAP_EVENT_DTLS_CLOSED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CLOSED, session); } } @@ -3498,7 +3498,7 @@ coap_dtls_send(coap_session_t *session, if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); @@ -3626,7 +3626,7 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) if (in_init && SSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } r = 0; @@ -3640,7 +3640,7 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); @@ -3768,7 +3768,7 @@ coap_tls_new_client_session(coap_session_t *session) { session->tls = ssl; if (SSL_is_init_finished(ssl)) { - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } @@ -3838,7 +3838,7 @@ coap_tls_new_server_session(coap_session_t *session) { session->tls = ssl; if (SSL_is_init_finished(ssl)) { - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } @@ -3871,7 +3871,7 @@ coap_tls_free_session(coap_session_t *session) { SSL_free(ssl); session->tls = NULL; if (session->context) - coap_handle_event(session->context, COAP_EVENT_DTLS_CLOSED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CLOSED, session); } } @@ -3898,7 +3898,7 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { if (in_init && SSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (err == SSL_ERROR_WANT_READ) @@ -3926,14 +3926,14 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { } else if (in_init && SSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); @@ -3976,7 +3976,7 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { if (in_init && SSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (err == SSL_ERROR_WANT_READ) @@ -4002,14 +4002,14 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { } else if (in_init && SSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); diff --git a/src/coap_oscore.c b/src/coap_oscore.c index c86c5ab2a5..a85fb45b41 100644 --- a/src/coap_oscore.c +++ b/src/coap_oscore.c @@ -828,18 +828,18 @@ coap_oscore_decrypt_pdu(coap_session_t *session, if (session->context->p_osc_ctx == NULL) { coap_log_warn("OSCORE: Not enabled\n"); if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_NOT_ENABLED, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_NOT_ENABLED, + session); return NULL; } if (pdu->data == NULL) { coap_log_warn("OSCORE: No protected payload\n"); if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD, + session); return NULL; } @@ -853,9 +853,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, coap_pdu_init(pdu->type, 0, pdu->mid, pdu->used_size); if (decrypt_pdu == NULL) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } @@ -898,9 +898,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, coap_opt_length(opt), coap_opt_value(opt))) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } break; @@ -1035,9 +1035,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, */ if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) { coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n"); - coap_handle_event(session->context, - COAP_EVENT_OSCORE_DECODE_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_DECODE_ERROR, + session); goto error; } association = oscore_find_association(session, &pdu_token); @@ -1066,9 +1066,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, osc_ctx->id_context->length); if (kc == NULL) { - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } @@ -1088,9 +1088,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, #endif /* COAP_CLIENT_SUPPORT */ } else { coap_log_crit("OSCORE: Security Context association not found\n"); - coap_handle_event(session->context, - COAP_EVENT_OSCORE_NO_SECURITY, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_NO_SECURITY, + session); goto error; } } @@ -1299,9 +1299,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, if (encrypt_len <= 0) { coap_log_warn("OSCORE: No protected payload\n"); if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD, + session); goto error; } cose_encrypt0_set_key(cose, rcp_ctx->recipient_key); @@ -1312,18 +1312,18 @@ coap_oscore_decrypt_pdu(coap_session_t *session, plain_pdu = coap_pdu_init(0, 0, 0, encrypt_len /* - tag_len */); if (plain_pdu == NULL) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } /* need the tag_len on the end for TinyDTLS to do its work - yuk */ if (!coap_pdu_resize(plain_pdu, encrypt_len /* - tag_len */)) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } @@ -1350,9 +1350,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, oscore_roll_back_seq(rcp_ctx); goto error_no_ack; } else { - coap_handle_event(session->context, - COAP_EVENT_OSCORE_DECRYPTION_FAILURE, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_DECRYPTION_FAILURE, + session); } goto error; } @@ -1368,9 +1368,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, if (kc == NULL) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } @@ -1422,9 +1422,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, if (kc == NULL) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } memcpy(kc->s, cose->kid_context.s, cose->kid_context.length); @@ -1519,9 +1519,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, opt_iter.number, len, cose->partial_iv.s ? &cose->partial_iv.s[bias] : NULL)) { - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } break; @@ -1538,9 +1538,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, coap_opt_length(opt), coap_opt_value(opt))) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } break; @@ -1555,9 +1555,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, (plain_pdu->data - plain_pdu->token), plain_pdu->data)) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } } @@ -1567,9 +1567,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session, /* Make sure headers are correctly set up */ if (!coap_pdu_encode_header(decrypt_pdu, session->proto)) { if (!coap_request) - coap_handle_event(session->context, - COAP_EVENT_OSCORE_INTERNAL_ERROR, - session); + coap_handle_event_lkd(session->context, + COAP_EVENT_OSCORE_INTERNAL_ERROR, + session); goto error; } diff --git a/src/coap_resource.c b/src/coap_resource.c index a879a9049e..ac0bd9797a 100644 --- a/src/coap_resource.c +++ b/src/coap_resource.c @@ -467,7 +467,7 @@ coap_free_resource(coap_resource_t *resource) { assert(resource); if (!resource->context->observe_no_clear) { - coap_resource_notify_observers(resource, NULL); + coap_resource_notify_observers_lkd(resource, NULL); coap_notify_observers(resource->context, resource, COAP_DELETING_RESOURCE); } @@ -506,8 +506,15 @@ coap_free_resource(coap_resource_t *resource) { coap_free_type(COAP_RESOURCE, resource); } -void +COAP_API void coap_add_resource(coap_context_t *context, coap_resource_t *resource) { + coap_lock_lock(context, return); + coap_add_resource_lkd(context, resource); + coap_lock_unlock(context); +} + +void +coap_add_resource_lkd(coap_context_t *context, coap_resource_t *resource) { coap_lock_check_locked(context); if (resource->is_unknown) { if (context->unknown_resource) @@ -518,14 +525,14 @@ coap_add_resource(coap_context_t *context, coap_resource_t *resource) { coap_free_resource(context->proxy_uri_resource); context->proxy_uri_resource = resource; } else { - coap_resource_t *r = coap_get_resource_from_uri_path(context, - resource->uri_path); + coap_resource_t *r = coap_get_resource_from_uri_path_lkd(context, + resource->uri_path); if (r) { coap_log_warn("coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n", (int)resource->uri_path->length, (int)resource->uri_path->length, resource->uri_path->s); - coap_delete_resource(context, r); + coap_delete_resource_lkd(context, r); } RESOURCES_ADD(context->resources, resource); #if COAP_WITH_OBSERVE_PERSIST @@ -546,11 +553,29 @@ coap_add_resource(coap_context_t *context, coap_resource_t *resource) { resource->context = context; } +COAP_API int +coap_delete_resource(coap_context_t *context, coap_resource_t *resource) { + int ret; + + if (!resource) + return 0; + + context = resource->context; + if (context) { + coap_lock_lock(context, return 0); + ret = coap_delete_resource_lkd(context, resource); + coap_lock_unlock(context); + } else { + ret = coap_delete_resource_lkd(context, resource); + } + return ret; +} + /* * Input context is ignored, but param left there to keep API consistent */ int -coap_delete_resource(coap_context_t *context, coap_resource_t *resource) { +coap_delete_resource_lkd(coap_context_t *context, coap_resource_t *resource) { if (!resource) return 0; @@ -603,9 +628,20 @@ coap_delete_all_resources(coap_context_t *context) { } } +COAP_API coap_resource_t * +coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path) { + coap_resource_t *result; + + coap_lock_lock(context, return NULL); + result = coap_get_resource_from_uri_path_lkd(context, uri_path); + coap_lock_unlock(context); + + return result; +} + coap_resource_t * -coap_get_resource_from_uri_path(coap_context_t *context, - coap_str_const_t *uri_path) { +coap_get_resource_from_uri_path_lkd(coap_context_t *context, + coap_str_const_t *uri_path) { coap_resource_t *result; coap_lock_check_locked(context); @@ -1183,14 +1219,30 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, r->dirty = 0; } -int +COAP_API int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query) { - return coap_resource_notify_observers(r, query); + int ret; + + coap_lock_lock(r->context, return 0); + ret = coap_resource_notify_observers_lkd(r, query); + coap_lock_unlock(r->context); + return ret; } -int +COAP_API int coap_resource_notify_observers(coap_resource_t *r, - const coap_string_t *query COAP_UNUSED) { + const coap_string_t *query) { + int ret; + + coap_lock_lock(r->context, return 0); + ret = coap_resource_notify_observers_lkd(r, query); + coap_lock_unlock(r->context); + return ret; +} + +int +coap_resource_notify_observers_lkd(coap_resource_t *r, + const coap_string_t *query COAP_UNUSED) { coap_lock_check_locked(r->context); if (!r->observable) return 0; @@ -1251,8 +1303,15 @@ coap_resource_get_uri_path(coap_resource_t *resource) { return NULL; } -void +COAP_API void coap_check_notify(coap_context_t *context) { + coap_lock_lock(context, return); + coap_check_notify_lkd(context); + coap_lock_unlock(context); +} + +void +coap_check_notify_lkd(coap_context_t *context) { coap_lock_check_locked(context); if (context->observe_pending) { diff --git a/src/coap_session.c b/src/coap_session.c index b78e23f405..dfa244ad29 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -475,7 +475,7 @@ coap_session_mfree(coap_session_t *session) { LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) { if (lg_crcv->observe_set && session->no_observe_cancel == 0) { /* Need to close down observe */ - if (coap_cancel_observe(session, lg_crcv->app_token, COAP_MESSAGE_NON)) { + if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) { /* Need to delete node we set up for NON */ coap_queue_t *queue = session->context->sendqueue; @@ -769,7 +769,7 @@ coap_session_connected(coap_session_t *session) { coap_log_debug("***%s: session connected\n", coap_session_str(session)); if (session->state == COAP_SESSION_STATE_CSM) { - coap_handle_event(session->context, COAP_EVENT_SESSION_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_SESSION_CONNECTED, session); if (session->doing_first) session->doing_first = 0; } @@ -972,14 +972,14 @@ coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) { #if !COAP_DISABLE_TCP if (COAP_PROTO_RELIABLE(session->proto)) { if (coap_netif_available(session)) { - coap_handle_event(session->context, - state == COAP_SESSION_STATE_CONNECTING ? - COAP_EVENT_TCP_FAILED : COAP_EVENT_TCP_CLOSED, session); + coap_handle_event_lkd(session->context, + state == COAP_SESSION_STATE_CONNECTING ? + COAP_EVENT_TCP_FAILED : COAP_EVENT_TCP_CLOSED, session); } if (state != COAP_SESSION_STATE_NONE) { - coap_handle_event(session->context, - state == COAP_SESSION_STATE_ESTABLISHED ? - COAP_EVENT_SESSION_CLOSED : COAP_EVENT_SESSION_FAILED, session); + coap_handle_event_lkd(session->context, + state == COAP_SESSION_STATE_ESTABLISHED ? + COAP_EVENT_SESSION_CLOSED : COAP_EVENT_SESSION_FAILED, session); } if (session->doing_first) session->doing_first = 0; @@ -1051,12 +1051,12 @@ coap_endpoint_get_session(coap_endpoint_t *endpoint, if (endpoint->context->max_idle_sessions > 0 && num_idle >= endpoint->context->max_idle_sessions) { - coap_handle_event(oldest->context, COAP_EVENT_SERVER_SESSION_DEL, oldest); + coap_handle_event_lkd(oldest->context, COAP_EVENT_SERVER_SESSION_DEL, oldest); coap_session_free(oldest); } else if (oldest_hs) { coap_log_warn("***%s: Incomplete session timed out\n", coap_session_str(oldest_hs)); - coap_handle_event(oldest_hs->context, COAP_EVENT_SERVER_SESSION_DEL, oldest_hs); + coap_handle_event_lkd(oldest_hs->context, COAP_EVENT_SERVER_SESSION_DEL, oldest_hs); coap_session_free(oldest_hs); } @@ -1126,7 +1126,7 @@ coap_endpoint_get_session(coap_endpoint_t *endpoint, SESSIONS_ADD(endpoint->sessions, session); coap_log_debug("***%s: session %p: new incoming session\n", coap_session_str(session), (void *)session); - coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); + coap_handle_event_lkd(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); } return session; } @@ -1584,8 +1584,8 @@ coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep, void *extra) { if (session) { coap_log_debug("***%s: session %p: new incoming session\n", coap_session_str(session), (void *)session); - coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session); - coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); + coap_handle_event_lkd(session->context, COAP_EVENT_TCP_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); session->state = COAP_SESSION_STATE_CONNECTING; session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session); } @@ -1859,7 +1859,7 @@ coap_free_endpoint(coap_endpoint_t *ep) { SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) { assert(session->ref == 0); if (session->ref == 0) { - coap_handle_event(ep->context, COAP_EVENT_SERVER_SESSION_DEL, session); + coap_handle_event_lkd(ep->context, COAP_EVENT_SERVER_SESSION_DEL, session); coap_session_free(session); } } diff --git a/src/coap_subscribe.c b/src/coap_subscribe.c index 2d4beb4c07..87216cad6b 100644 --- a/src/coap_subscribe.c +++ b/src/coap_subscribe.c @@ -139,8 +139,8 @@ coap_persist_observe_add(coap_context_t *context, if (!uri_path) goto malformed; - r = coap_get_resource_from_uri_path(session->context, - (coap_str_const_t *)uri_path); + r = coap_get_resource_from_uri_path_lkd(session->context, + (coap_str_const_t *)uri_path); if (r == NULL) { coap_log_warn("coap_persist_observe_add: resource '%s' not defined\n", uri_path->s); @@ -677,7 +677,7 @@ coap_op_obs_cnt_load_disk(coap_context_t *context) { context->observe_save_freq - 1; resource_key.s = (uint8_t *)buf; resource_key.length = strlen(buf); - r = coap_get_resource_from_uri_path(context, &resource_key); + r = coap_get_resource_from_uri_path_lkd(context, &resource_key); if (r) { coap_log_debug("persist: Initial observe number being updated\n"); coap_persist_set_observe_num(r, observe_num); @@ -915,7 +915,7 @@ coap_op_dyn_resource_load_disk(coap_context_t *ctx) { while (1) { if (!coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet)) break; - r = coap_get_resource_from_uri_path(ctx, (coap_str_const_t *)name); + r = coap_get_resource_from_uri_path_lkd(ctx, (coap_str_const_t *)name); if (!r) { /* Create the new resource using the application logic */ diff --git a/src/coap_threadsafe.c b/src/coap_threadsafe.c index a0e354253b..ecf1c88fb9 100644 --- a/src/coap_threadsafe.c +++ b/src/coap_threadsafe.c @@ -34,34 +34,6 @@ /* Client only wrapper functions */ -int -coap_add_data_large_request(coap_session_t *session, - coap_pdu_t *pdu, - size_t length, - const uint8_t *data, - coap_release_large_data_t release_func, - void *app_ptr - ) { - int ret; - - coap_lock_lock(session->context, return 0); - ret = coap_add_data_large_request_locked(session, pdu, length, data, - release_func, app_ptr); - coap_lock_unlock(session->context); - return ret; -} - -int -coap_cancel_observe(coap_session_t *session, coap_binary_t *token, - coap_pdu_type_t type) { - int ret; - - coap_lock_lock(session->context, return 0); - ret = coap_cancel_observe_locked(session, token, type); - coap_lock_unlock(session->context); - return ret; -} - coap_session_t * coap_new_client_session(coap_context_t *ctx, const coap_address_t *local_if, @@ -169,92 +141,6 @@ coap_new_client_session_psk2(coap_context_t *ctx, /* Server only wrapper functions */ -int -coap_add_data_large_response(coap_resource_t *resource, - coap_session_t *session, - const coap_pdu_t *request, - coap_pdu_t *response, - const coap_string_t *query, - uint16_t media_type, - int maxage, - uint64_t etag, - size_t length, - const uint8_t *data, - coap_release_large_data_t release_func, - void *app_ptr - ) { - int ret; - - coap_lock_lock(session->context, return 0); - ret = coap_add_data_large_response_locked(resource, session, request, - response, query, media_type, maxage, etag, - length, data, release_func, app_ptr); - coap_lock_unlock(session->context); - return ret; -} - -void -coap_add_resource(coap_context_t *context, coap_resource_t *resource) { - coap_lock_lock(context, return); - coap_add_resource_locked(context, resource); - coap_lock_unlock(context); -} - -void -coap_async_trigger(coap_async_t *async) { - coap_lock_lock(async->session->context, return); - coap_async_trigger_locked(async); - coap_lock_unlock(async->session->context); -} - -void -coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { - coap_lock_lock(async->session->context, return); - coap_async_set_delay_locked(async, delay); - coap_lock_unlock(async->session->context); -} - -coap_cache_entry_t * -coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) { - coap_cache_entry_t *cache; - - coap_lock_lock(ctx, return NULL); - cache = coap_cache_get_by_key_locked(ctx, cache_key); - coap_lock_unlock(ctx); - return cache; -} - -coap_cache_entry_t * -coap_cache_get_by_pdu(coap_session_t *session, - const coap_pdu_t *request, - coap_cache_session_based_t session_based) { - coap_cache_entry_t *entry; - - coap_lock_lock(session->context, return NULL); - entry = coap_cache_get_by_pdu_locked(session, request, session_based); - coap_lock_unlock(session->context); - return entry; -} - -int -coap_cache_ignore_options(coap_context_t *ctx, - const uint16_t *options, - size_t count) { - int ret; - - coap_lock_lock(ctx, return 0); - ret = coap_cache_ignore_options_locked(ctx, options, count); - coap_lock_unlock(ctx); - return ret; -} - -void -coap_check_notify(coap_context_t *context) { - coap_lock_lock(context, return); - coap_check_notify_locked(context); - coap_lock_unlock(context); -} - int coap_context_oscore_server(coap_context_t *context, coap_oscore_conf_t *oscore_conf) { @@ -300,34 +186,6 @@ coap_context_set_psk2(coap_context_t *ctx, coap_dtls_spsk_t *setup_data) { return ret; } -int -coap_delete_resource(coap_context_t *context, coap_resource_t *resource) { - int ret; - - if (!resource) - return 0; - - context = resource->context; - if (context) { - coap_lock_lock(context, return 0); - ret = coap_delete_resource_locked(context, resource); - coap_lock_unlock(context); - } else { - ret = coap_delete_resource_locked(context, resource); - } - return ret; -} - -coap_async_t * -coap_find_async(coap_session_t *session, coap_bin_const_t token) { - coap_async_t *tmp; - - coap_lock_lock(session->context, return NULL); - tmp = coap_find_async_locked(session, token); - coap_lock_unlock(session->context); - return tmp; -} - void coap_free_endpoint(coap_endpoint_t *ep) { if (ep) { @@ -340,17 +198,6 @@ coap_free_endpoint(coap_endpoint_t *ep) { } } -coap_resource_t * -coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path) { - coap_resource_t *result; - - coap_lock_lock(context, return NULL); - result = coap_get_resource_from_uri_path_locked(context, uri_path); - coap_lock_unlock(context); - - return result; -} - int coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, const char *ifname) { @@ -362,20 +209,6 @@ coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, return ret; } -coap_cache_entry_t * -coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, - coap_cache_record_pdu_t record_pdu, - coap_cache_session_based_t session_based, - unsigned int idle_timeout) { - coap_cache_entry_t *cache; - - coap_lock_lock(session->context, return NULL); - cache = coap_new_cache_entry_locked(session, pdu, record_pdu, session_based, - idle_timeout); - coap_lock_unlock(session->context); - return cache; -} - coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) { coap_endpoint_t *endpoint; @@ -433,42 +266,10 @@ coap_persist_stop(coap_context_t *context) { coap_lock_unlock(context); } -coap_async_t * -coap_register_async(coap_session_t *session, - const coap_pdu_t *request, coap_tick_t delay) { - coap_async_t *async; - - coap_lock_lock(session->context, return NULL); - async = coap_register_async_locked(session, request, delay); - coap_lock_unlock(session->context); - return async; -} - -int -coap_resource_notify_observers(coap_resource_t *r, - const coap_string_t *query) { - int ret; - - coap_lock_lock(r->context, return 0); - ret = coap_resource_notify_observers_locked(r, query); - coap_lock_unlock(r->context); - return ret; -} - #endif /* COAP_SERVER_SUPPORT */ /* Both Client and Server wrapper functions */ -int -coap_can_exit(coap_context_t *context) { - int ret; - - coap_lock_lock(context, return 0); - ret = coap_can_exit_locked(context); - coap_lock_unlock(context); - return ret; -} - void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode) { diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 1a8c299294..1dd57cab1a 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -759,7 +759,7 @@ coap_dtls_free_session(coap_session_t *coap_session) { coap_log_debug("***removed session %p\n", coap_session->tls); coap_free_type(COAP_DTLS_SESSION, coap_session->tls); coap_session->tls = NULL; - coap_handle_event(coap_session->context, COAP_EVENT_DTLS_CLOSED, coap_session); + coap_handle_event_lkd(coap_session->context, COAP_EVENT_DTLS_CLOSED, coap_session); } } @@ -788,7 +788,7 @@ coap_dtls_send(coap_session_t *session, if (coap_event_dtls >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (coap_event_dtls != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, coap_event_dtls, session); + coap_handle_event_lkd(session->context, coap_event_dtls, session); if (coap_event_dtls == COAP_EVENT_DTLS_CONNECTED) coap_session_connected(session); else if (coap_event_dtls == COAP_EVENT_DTLS_CLOSED || coap_event_dtls == COAP_EVENT_DTLS_ERROR) @@ -857,7 +857,7 @@ coap_dtls_receive(coap_session_t *session, if (coap_event_dtls >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (coap_event_dtls != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, coap_event_dtls, session); + coap_handle_event_lkd(session->context, coap_event_dtls, session); if (coap_event_dtls == COAP_EVENT_DTLS_CONNECTED) coap_session_connected(session); else if (coap_event_dtls == COAP_EVENT_DTLS_CLOSED || coap_event_dtls == COAP_EVENT_DTLS_ERROR) { diff --git a/src/coap_wolfssl.c b/src/coap_wolfssl.c index ff6df7cd52..4960fd181d 100644 --- a/src/coap_wolfssl.c +++ b/src/coap_wolfssl.c @@ -2088,7 +2088,7 @@ coap_dtls_free_session(coap_session_t *session) { w_env->ssl = NULL; wolfSSL_free(ssl); if (session->context) - coap_handle_event(session->context, COAP_EVENT_DTLS_CLOSED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CLOSED, session); } coap_dtls_free_wolfssl_env(w_env); } @@ -2124,7 +2124,7 @@ coap_dtls_send(coap_session_t *session, if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); @@ -2252,7 +2252,7 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) if (in_init && wolfSSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), wolfSSL_get_cipher((ssl))); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } r = 0; @@ -2287,7 +2287,7 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); @@ -2439,7 +2439,7 @@ coap_tls_new_client_session(coap_session_t *session) { coap_ticks(&now); w_env->last_timeout = now; if (wolfSSL_is_init_finished(ssl)) { - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } @@ -2540,7 +2540,7 @@ coap_tls_new_server_session(coap_session_t *session) { session->tls = w_env; if (wolfSSL_is_init_finished(ssl)) { - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } @@ -2568,7 +2568,7 @@ coap_tls_free_session(coap_session_t *session) { wolfSSL_free(ssl); w_env->ssl = NULL; if (session->context) - coap_handle_event(session->context, COAP_EVENT_DTLS_CLOSED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CLOSED, session); } coap_dtls_free_wolfssl_env(w_env); } @@ -2597,7 +2597,7 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { if (in_init && wolfSSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), wolfSSL_get_cipher((ssl))); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (err == WOLFSSL_ERROR_WANT_READ) @@ -2625,14 +2625,14 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) { } else if (in_init && wolfSSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), wolfSSL_get_cipher((ssl))); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); @@ -2676,7 +2676,7 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { if (in_init && wolfSSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), wolfSSL_get_cipher((ssl))); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (err == WOLFSSL_ERROR_WANT_READ) @@ -2715,14 +2715,14 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) { } else if (in_init && wolfSSL_is_init_finished(ssl)) { coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), wolfSSL_get_cipher((ssl))); - coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_DTLS_CONNECTED, session); session->sock.lfunc[COAP_LAYER_TLS].l_establish(session); } if (session->dtls_event >= 0) { /* COAP_EVENT_DTLS_CLOSED event reported in coap_session_disconnected() */ if (session->dtls_event != COAP_EVENT_DTLS_CLOSED) - coap_handle_event(session->context, session->dtls_event, session); + coap_handle_event_lkd(session->context, session->dtls_event, session); if (session->dtls_event == COAP_EVENT_DTLS_ERROR || session->dtls_event == COAP_EVENT_DTLS_CLOSED) { coap_session_disconnected(session, COAP_NACK_TLS_FAILED); diff --git a/src/coap_ws.c b/src/coap_ws.c index e65cc1dd4b..7e08939d2b 100644 --- a/src/coap_ws.c +++ b/src/coap_ws.c @@ -613,12 +613,12 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { session->sock.lfunc[COAP_LAYER_WS].l_write(session, (uint8_t *)buf, strlen(buf)); - coap_handle_event(session->context, COAP_EVENT_WS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_WS_CONNECTED, session); coap_log_debug("WS: established\n"); } else { /* TODO Process the GET response - error on failure */ - coap_handle_event(session->context, COAP_EVENT_WS_CONNECTED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_WS_CONNECTED, session); } session->sock.lfunc[COAP_LAYER_WS].l_establish(session); if (session->ws->hdr_ofs == 0) @@ -695,7 +695,7 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { if ((size_t)bytes_size > datalen) { coap_log_err("coap_ws_read: packet size bigger than provided data space" " (%zu > %zu)\n", bytes_size, datalen); - coap_handle_event(session->context, COAP_EVENT_WS_PACKET_SIZE, session); + coap_handle_event_lkd(session->context, COAP_EVENT_WS_PACKET_SIZE, session); session->ws->close_reason = 1009; coap_ws_close(session); return 0; @@ -910,7 +910,7 @@ coap_ws_close(coap_session_t *session) { count --; } #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ - coap_handle_event(session->context, COAP_EVENT_WS_CLOSED, session); + coap_handle_event_lkd(session->context, COAP_EVENT_WS_CLOSED, session); } session->sock.lfunc[COAP_LAYER_WS].l_close(session); }