123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- /**
- * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form, except as embedded into a Nordic
- * Semiconductor ASA integrated circuit in a product or a software update for
- * such product, must reproduce the above copyright notice, this list of
- * conditions and the following disclaimer in the documentation and/or other
- * materials provided with the distribution.
- *
- * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * 4. This software, with or without modification, must only be used with a
- * Nordic Semiconductor ASA integrated circuit.
- *
- * 5. Any software provided in binary form under this license must not be reverse
- * engineered, decompiled, modified and/or disassembled.
- *
- * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #include <string.h>
- #include "coap_observe_api.h"
- #include "coap_observe.h"
- #include "nrf_error.h"
- #include "iot_common.h"
- #include "sdk_common.h"
- #include "sdk_config.h"
- #include "coap.h"
- #if IOT_COAP_CONFIG_LOG_ENABLED
- #define NRF_LOG_MODULE_NAME coapobs
- #define NRF_LOG_LEVEL IOT_COAP_CONFIG_LOG_LEVEL
- #define NRF_LOG_INFO_COLOR IOT_COAP_CONFIG_INFO_COLOR
- #define NRF_LOG_DEBUG_COLOR IOT_COAP_CONFIG_DEBUG_COLOR
- #include "nrf_log.h"
- NRF_LOG_MODULE_REGISTER();
- #define COAP_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
- #define COAP_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
- #define COAP_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
- #define COAP_ENTRY() COAP_TRC(">> %s", __func__)
- #define COAP_EXIT() COAP_TRC("<< %s", __func__)
- #else // IOT_COAP_CONFIG_LOG_ENABLED
- #define COAP_TRC(...) /**< Disables traces. */
- #define COAP_DUMP(...) /**< Disables dumping of octet streams. */
- #define COAP_ERR(...) /**< Disables error logs. */
- #define COAP_ENTRY(...)
- #define COAP_EXIT(...)
- #endif // IOT_COAP_CONFIG_LOG_ENABLED
- #if (COAP_ENABLE_OBSERVE_SERVER == 1)
- static coap_observer_t m_observers[COAP_OBSERVE_MAX_NUM_OBSERVERS];
- static void observe_server_init(void)
- {
- COAP_ENTRY();
- // Loop through the observer array and clear the memory.
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVERS; i++)
- {
- memset(&m_observers[i], 0, sizeof(coap_observer_t));
- }
- COAP_EXIT();
- }
- uint32_t internal_coap_observe_server_register(uint32_t * p_handle, coap_observer_t * p_observer)
- {
- COAP_ENTRY();
- NULL_PARAM_CHECK(p_handle);
- NULL_PARAM_CHECK(p_observer);
- NULL_PARAM_MEMBER_CHECK(p_observer->p_resource_of_interest);
- // Check if there is already a registered observer in the list to be reused.
- uint32_t handle;
- uint32_t err_code = coap_observe_server_search(&handle,
- &p_observer->remote,
- p_observer->p_resource_of_interest);
- if (err_code == NRF_SUCCESS)
- {
- memcpy(&m_observers[handle], p_observer, sizeof(coap_observer_t));
- *p_handle = handle;
- return NRF_SUCCESS;
- }
- // Check if there is an available spot in the observer list.
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVERS; i++)
- {
- if (m_observers[i].p_resource_of_interest == NULL)
- {
- memcpy(&m_observers[i], p_observer, sizeof(coap_observer_t));
- *p_handle = i;
- return NRF_SUCCESS;
- }
- }
- COAP_EXIT();
- return (NRF_ERROR_NO_MEM | IOT_COAP_ERR_BASE);
- }
- uint32_t internal_coap_observe_server_unregister(uint32_t handle)
- {
- COAP_ENTRY();
- if (handle >= COAP_OBSERVE_MAX_NUM_OBSERVERS)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- if (m_observers[handle].p_resource_of_interest == NULL)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- m_observers[handle].p_resource_of_interest = NULL;
- COAP_EXIT();
- return NRF_SUCCESS;
- }
- uint32_t internal_coap_observe_server_search(uint32_t * p_handle,
- coap_remote_t * p_observer_addr,
- coap_resource_t * p_resource)
- {
- NULL_PARAM_CHECK(p_handle);
- NULL_PARAM_CHECK(p_observer_addr);
- NULL_PARAM_CHECK(p_resource);
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVERS; i++)
- {
- if (m_observers[i].p_resource_of_interest == p_resource)
- {
- if (m_observers[i].remote.port_number == p_observer_addr->port_number)
- {
- if (memcmp(p_observer_addr->addr, m_observers[i].remote.addr, sizeof(p_observer_addr->addr)) == 0)
- {
- *p_handle = i;
- return NRF_SUCCESS;
- }
- }
- }
- }
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- uint32_t internal_coap_observe_server_next_get(coap_observer_t ** pp_observer,
- coap_observer_t * p_observer,
- coap_resource_t * p_resource)
- {
- NULL_PARAM_CHECK(p_resource);
- NULL_PARAM_CHECK(pp_observer);
- if (p_observer == NULL)
- {
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVERS; i++)
- {
- if (m_observers[i].p_resource_of_interest == p_resource)
- {
- (*pp_observer) = &m_observers[i];
- return NRF_SUCCESS;
- }
- }
- }
- else
- {
- uint32_t index_to_previous = (uint8_t)(((uint32_t)p_observer - (uint32_t)m_observers) / (uint32_t)sizeof(coap_observer_t));
- for (uint32_t i = index_to_previous + 1; i < COAP_OBSERVE_MAX_NUM_OBSERVERS; i++)
- {
- if (m_observers[i].p_resource_of_interest == p_resource)
- {
- (*pp_observer) = &m_observers[i];
- return NRF_SUCCESS;
- }
- }
- }
- (*pp_observer) = NULL;
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- uint32_t internal_coap_observe_server_get(uint32_t handle, coap_observer_t ** pp_observer)
- {
- NULL_PARAM_CHECK(pp_observer);
- if (handle >= COAP_OBSERVE_MAX_NUM_OBSERVERS)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- if (m_observers[handle].p_resource_of_interest == NULL)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- *pp_observer = &m_observers[handle];
- return NRF_SUCCESS;
- }
- #else
- #define observe_server_init(...)
- #endif
- #if (COAP_ENABLE_OBSERVE_CLIENT == 1)
- static coap_observable_t m_observables[COAP_OBSERVE_MAX_NUM_OBSERVABLES];
- static void observe_client_init(void)
- {
- // Loop through the observable array and clear the memory.
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVABLES; i++)
- {
- memset(&m_observables[i], 0, sizeof(coap_observable_t));
- }
- }
- uint32_t internal_coap_observe_client_register(uint32_t * p_handle,
- coap_observable_t * p_observable)
- {
- COAP_ENTRY();
- NULL_PARAM_CHECK(p_handle);
- NULL_PARAM_CHECK(p_observable);
- NULL_PARAM_MEMBER_CHECK(p_observable->response_callback);
- // Check if there is an available spot in the observer list.
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVABLES; i++)
- {
- if (m_observables[i].response_callback == NULL)
- {
- memcpy(&m_observables[i], p_observable, sizeof(coap_observable_t));
- *p_handle = i;
- return NRF_SUCCESS;
- }
- }
- COAP_EXIT();
- return (NRF_ERROR_NO_MEM | IOT_COAP_ERR_BASE);
- }
- uint32_t internal_coap_observe_client_unregister(uint32_t handle)
- {
- COAP_ENTRY();
- if (handle >= COAP_OBSERVE_MAX_NUM_OBSERVABLES)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- if (m_observables[handle].response_callback == NULL)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- m_observables[handle].response_callback = NULL;
- COAP_EXIT();
- return NRF_SUCCESS;
- }
- uint32_t internal_coap_observe_client_search(uint32_t * p_handle, uint8_t * p_token, uint16_t token_len)
- {
- NULL_PARAM_CHECK(p_handle);
- NULL_PARAM_CHECK(p_token);
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVABLES; i++)
- {
- if ((m_observables[i].response_callback != NULL) &&
- (0 != m_observables[i].token_len) &&
- (memcmp(m_observables[i].token, p_token, m_observables[i].token_len) == 0))
- {
- *p_handle = i;
- return NRF_SUCCESS;
- }
- }
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- uint32_t internal_coap_observe_client_get(uint32_t handle, coap_observable_t ** pp_observable)
- {
- NULL_PARAM_CHECK(pp_observable);
- if (handle >= COAP_OBSERVE_MAX_NUM_OBSERVABLES)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- if (m_observables[handle].response_callback == NULL)
- {
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- *pp_observable = &m_observables[handle];
- return NRF_SUCCESS;
- }
- uint32_t internal_coap_observe_client_next_get(coap_observable_t ** pp_observable,
- uint32_t * p_handle,
- coap_observable_t * p_observable)
- {
- NULL_PARAM_CHECK(pp_observable);
- if (p_observable == NULL)
- {
- for (uint32_t i = 0; i < COAP_OBSERVE_MAX_NUM_OBSERVABLES; i++)
- {
- if (m_observables[i].response_callback != NULL)
- {
- (*pp_observable) = &m_observables[i];
- (*p_handle) = i;
- return NRF_SUCCESS;
- }
- }
- }
- else
- {
- uint32_t index_to_previous = (uint8_t)(((uint32_t)p_observable - (uint32_t)m_observables) / (uint32_t)sizeof(coap_observable_t));
- for (uint32_t i = index_to_previous + 1; i < COAP_OBSERVE_MAX_NUM_OBSERVABLES; i++)
- {
- if (m_observables[i].response_callback != NULL)
- {
- (*pp_observable) = &m_observables[i];
- (*p_handle) = i;
- return NRF_SUCCESS;
- }
- }
- }
- (*pp_observable) = NULL;
- COAP_MUTEX_UNLOCK();
- return (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE);
- }
- static uint32_t observe_opt_present(coap_message_t * p_message)
- {
- uint8_t index;
- for (index = 0; index < p_message->options_count; index++)
- {
- if (p_message->options[index].number == COAP_OPT_OBSERVE)
- {
- return NRF_SUCCESS;
- }
- }
- return NRF_ERROR_NOT_FOUND;
- }
- static void set_max_age(coap_observable_t * observable, coap_message_t * p_response)
- {
- uint8_t index;
- for (index = 0; index < p_response->options_count; index++)
- {
- if (p_response->options[index].number == COAP_OPT_MAX_AGE)
- {
- uint32_t max_age;
- observable->max_age = coap_opt_uint_decode(&max_age,
- p_response->options[index].length,
- p_response->options[index].p_data);
- observable->max_age = max_age;
- return;
- }
- }
- // Max-Age option is not present, set default value to 60.
- observable->max_age = 60;
- }
- void coap_observe_client_send_handle(coap_message_t * p_request)
- {
- COAP_ENTRY();
- if (p_request->header.code == COAP_CODE_GET)
- {
- uint32_t observe_option = 0;
- if (observe_opt_present(p_request) == NRF_SUCCESS)
- {
- // Locate option and check value.
- uint8_t index;
- for (index = 0; index < p_request->options_count; index++)
- {
- if (p_request->options[index].number == COAP_OPT_OBSERVE)
- {
- uint32_t err_code = coap_opt_uint_decode(&observe_option,
- p_request->options[index].length,
- p_request->options[index].p_data);
- if (err_code != NRF_SUCCESS)
- {
- return;
- }
- break;
- }
- }
- }
- if (observe_option == 1)
- {
- // Un-register observable instance.
- uint32_t handle;
- uint32_t err_code = internal_coap_observe_client_search(&handle,
- p_request->token,
- p_request->header.token_len);
- if (err_code == NRF_SUCCESS)
- {
- (void)internal_coap_observe_client_unregister(handle);
- COAP_TRC("OBSERVE=1 in request, client_unregister handle: %i", handle);
- }
- }
- }
- COAP_EXIT();
- }
- void coap_observe_client_response_handle(coap_message_t * p_response, coap_queue_item_t * p_item)
- {
- COAP_ENTRY();
- if (observe_opt_present(p_response) == NRF_SUCCESS)
- {
- if (p_item == NULL)
- {
- // Search for the token in the observable list.
- uint32_t handle;
- uint32_t err_code = internal_coap_observe_client_search(&handle, p_response->token, p_response->header.token_len);
- if (err_code == NRF_SUCCESS)
- {
- // Fetch the observable.
- coap_observable_t * p_observable;
- err_code = internal_coap_observe_client_get(handle, &p_observable);
- if (err_code == NRF_SUCCESS)
- {
- // Update max-age to the newly recieved message.
- set_max_age(p_observable, p_response);
- COAP_MUTEX_UNLOCK();
- // Callback to the application.
- p_observable->response_callback(NRF_SUCCESS, NULL, p_response);
- COAP_MUTEX_LOCK();
- COAP_TRC("Notification received on handle: %i", handle);
- #ifdef COAP_AUTOMODE
- if (p_response->header.type == COAP_TYPE_CON)
- {
- // Reply an ACK upon CON message.
- }
- else if (p_response->header.type == COAP_TYPE_RST)
- {
- // Remove observable from list.
- }
- #endif
- }
- else
- {
- #ifdef COAP_AUTOMODE
- if (p_response->header.type == COAP_TYPE_CON)
- {
- // Reply reset upon CON message when observer is not located.
- }
- #endif
- }
- }
- else
- {
- // Send RST message back to server to indicate there is no one listening.
- }
- }
- else // p_item set.
- {
- // If there is no observable instance created yet for thit token, add it.
- uint32_t handle;
- uint32_t err_code = internal_coap_observe_client_search(&handle, p_response->token, p_response->header.token_len);
- if (err_code == (NRF_ERROR_NOT_FOUND | IOT_COAP_ERR_BASE))
- {
- // If the response is a valid response, add the observable resource.
- if (p_response->header.code == COAP_CODE_205_CONTENT)
- {
- coap_observable_t observable;
- // Token Length.
- observable.token_len = p_response->header.token_len;
- // Remote.
- memcpy(&observable.remote, &p_response->remote, sizeof(coap_remote_t));
- // Token.
- memcpy(observable.token, p_response->token, observable.token_len);
- // Callback to be called upon notification.
- observable.response_callback = p_item->callback;
- // Update max-age to the newly recieved message.
- set_max_age(&observable, p_response);
- // Register the observable.
- uint32_t observable_resource_handle;
- (void)internal_coap_observe_client_register(&observable_resource_handle, &observable);
- // TODO: error check
- COAP_TRC("Subscription response received, client_register handle: %i", observable_resource_handle);
- }
- }
- }
- }
- else // COAP_OPT_OBSERVE not present
- {
- uint32_t handle;
- uint32_t err_code = internal_coap_observe_client_search(&handle, p_response->token, p_response->header.token_len);
- if (err_code == NRF_SUCCESS)
- {
- (void)internal_coap_observe_client_unregister(handle);
- COAP_TRC("OBSERVE not present in notification, client_unregister handle: %i", handle);
- }
- }
- COAP_EXIT();
- }
- #else
- #define observe_client_init(...)
- #endif
- void internal_coap_observe_init(void)
- {
- observe_server_init();
- observe_client_init();
- }
- #if (COAP_ENABLE_OBSERVE_SERVER == 1)
- uint32_t coap_observe_server_register(uint32_t * p_handle, coap_observer_t * p_observer)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_server_register(p_handle, p_observer);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_server_unregister(uint32_t handle)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_server_unregister(handle);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_server_search(uint32_t * p_handle, coap_remote_t * p_observer_addr, coap_resource_t * p_resource)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_server_search(p_handle, p_observer_addr, p_resource);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_server_next_get(coap_observer_t ** pp_observer, coap_observer_t * p_observer, coap_resource_t * p_resource)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_server_next_get(pp_observer, p_observer, p_resource);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_server_get(uint32_t handle, coap_observer_t ** pp_observer)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_server_get(handle, pp_observer);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- #endif // COAP_ENABLE_OBSERVE_SERVER = 1
- #if (COAP_ENABLE_OBSERVE_CLIENT == 1)
- uint32_t coap_observe_client_register(uint32_t * p_handle, coap_observable_t * p_observable)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_client_register(p_handle, p_observable);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_client_unregister(uint32_t handle)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_client_unregister(handle);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_client_search(uint32_t * p_handle, uint8_t * p_token, uint16_t token_len)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_client_search(p_handle, p_token, token_len);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_client_get(uint32_t handle, coap_observable_t ** pp_observable)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_client_get(handle, pp_observable);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- uint32_t coap_observe_client_next_get(coap_observable_t ** pp_observable, uint32_t * p_handle, coap_observable_t * p_observable)
- {
- COAP_MUTEX_UNLOCK();
- uint32_t err_code = internal_coap_observe_client_next_get(pp_observable, p_handle, p_observable);
- COAP_MUTEX_UNLOCK();
- return err_code;
- }
- #endif // COAP_ENABLE_OBSERVE_CLIENT == 1
|