1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186 |
- /**
- * Copyright (c) 2015 - 2020, 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 "sdk_common.h"
- #if NRF_MODULE_ENABLED(PEER_MANAGER)
- #include "ble_err.h"
- #include "peer_manager.h"
- #include <string.h>
- #include "security_manager.h"
- #include "security_dispatcher.h"
- #include "gatt_cache_manager.h"
- #include "gatts_cache_manager.h"
- #include "peer_database.h"
- #include "peer_data_storage.h"
- #include "id_manager.h"
- #include "ble_conn_state.h"
- #include "peer_manager_internal.h"
- #include "nrf_sdh_ble.h"
- #define NRF_LOG_MODULE_NAME peer_manager
- #if PM_LOG_ENABLED
- #define NRF_LOG_LEVEL PM_LOG_LEVEL
- #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
- #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
- #else
- #define NRF_LOG_LEVEL 0
- #endif // PM_LOG_ENABLED
- #include "nrf_log.h"
- #include "nrf_log_ctrl.h"
- NRF_LOG_MODULE_REGISTER();
- #include "nrf_strerror.h"
- #ifndef PM_PEER_RANKS_ENABLED
- #define PM_PEER_RANKS_ENABLED 1
- #endif
- #define MODULE_INITIALIZED (m_module_initialized) /**< Macro indicating whether the module has been initialized properly. */
- static bool m_module_initialized; /**< Whether or not @ref pm_init has been called successfully. */
- static bool m_peer_rank_initialized; /**< Whether or not @ref rank_init has been called successfully. */
- static bool m_deleting_all; /**< True from when @ref pm_peers_delete is called until all peers have been deleted. */
- static pm_store_token_t m_peer_rank_token; /**< The store token of an ongoing peer rank update via a call to @ref pm_peer_rank_highest. If @ref PM_STORE_TOKEN_INVALID, there is no ongoing update. */
- static uint32_t m_current_highest_peer_rank; /**< The current highest peer rank. Used by @ref pm_peer_rank_highest. */
- static pm_peer_id_t m_highest_ranked_peer; /**< The peer with the highest peer rank. Used by @ref pm_peer_rank_highest. */
- static pm_evt_handler_t m_evt_handlers[PM_MAX_REGISTRANTS];/**< The subscribers to Peer Manager events, as registered through @ref pm_register. */
- static uint8_t m_n_registrants; /**< The number of event handlers registered through @ref pm_register. */
- /**@brief Function for sending a Peer Manager event to all subscribers.
- *
- * @param[in] p_pm_evt The event to send.
- */
- static void evt_send(pm_evt_t const * p_pm_evt)
- {
- for (int i = 0; i < m_n_registrants; i++)
- {
- m_evt_handlers[i](p_pm_evt);
- }
- }
- #if PM_PEER_RANKS_ENABLED == 1
- /**@brief Function for initializing peer rank static variables.
- */
- static void rank_vars_update(void)
- {
- ret_code_t err_code = pm_peer_ranks_get(&m_highest_ranked_peer,
- &m_current_highest_peer_rank,
- NULL,
- NULL);
- if (err_code == NRF_ERROR_NOT_FOUND)
- {
- m_highest_ranked_peer = PM_PEER_ID_INVALID;
- m_current_highest_peer_rank = 0;
- }
- m_peer_rank_initialized = ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND));
- }
- #endif
- /**@brief Event handler for events from the Peer Database module.
- * This handler is extern in the Peer Database module.
- *
- * @param[in] p_pdb_evt The incoming Peer Database event.
- */
- void pm_pdb_evt_handler(pm_evt_t * p_pdb_evt)
- {
- bool send_evt = true;
- p_pdb_evt->conn_handle = im_conn_handle_get(p_pdb_evt->peer_id);
- switch (p_pdb_evt->evt_id)
- {
- #if PM_PEER_RANKS_ENABLED == 1
- case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
- if (p_pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE)
- {
- if ( (m_peer_rank_token != PM_STORE_TOKEN_INVALID)
- && (m_peer_rank_token == p_pdb_evt->params.peer_data_update_succeeded.token))
- {
- m_peer_rank_token = PM_STORE_TOKEN_INVALID;
- m_highest_ranked_peer = p_pdb_evt->peer_id;
- p_pdb_evt->params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID;
- }
- else if ( m_peer_rank_initialized
- && (p_pdb_evt->peer_id == m_highest_ranked_peer)
- && (p_pdb_evt->params.peer_data_update_succeeded.data_id
- == PM_PEER_DATA_ID_PEER_RANK))
- {
- // Update peer rank variable if highest ranked peer has changed its rank.
- rank_vars_update();
- }
- }
- else if (p_pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_DELETE)
- {
- if ( m_peer_rank_initialized
- && (p_pdb_evt->peer_id == m_highest_ranked_peer)
- && (p_pdb_evt->params.peer_data_update_succeeded.data_id == PM_PEER_DATA_ID_PEER_RANK))
- {
- // Update peer rank variable if highest ranked peer has deleted its rank.
- rank_vars_update();
- }
- }
- break;
- case PM_EVT_PEER_DATA_UPDATE_FAILED:
- if (p_pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE)
- {
- if ( (m_peer_rank_token != PM_STORE_TOKEN_INVALID)
- && (m_peer_rank_token == p_pdb_evt->params.peer_data_update_failed.token))
- {
- m_peer_rank_token = PM_STORE_TOKEN_INVALID;
- m_current_highest_peer_rank -= 1;
- p_pdb_evt->params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID;
- }
- }
- break;
- #endif
- case PM_EVT_PEER_DELETE_SUCCEEDED:
- // Check that no peers marked for deletion are left.
- if (m_deleting_all
- && (pds_next_peer_id_get(PM_PEER_ID_INVALID) == PM_PEER_ID_INVALID)
- && (pds_next_deleted_peer_id_get(PM_PEER_ID_INVALID) == PM_PEER_ID_INVALID))
- {
- // pm_peers_delete() has been called and this is the last peer to be deleted.
- m_deleting_all = false;
- pm_evt_t pm_delete_all_evt;
- memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t));
- pm_delete_all_evt.evt_id = PM_EVT_PEERS_DELETE_SUCCEEDED;
- pm_delete_all_evt.peer_id = PM_PEER_ID_INVALID;
- pm_delete_all_evt.conn_handle = BLE_CONN_HANDLE_INVALID;
- send_evt = false;
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_pdb_evt); // Ensure that PEER_DELETE_SUCCEEDED arrives before PEERS_DELETE_SUCCEEDED.
- evt_send(&pm_delete_all_evt);
- }
- #if PM_PEER_RANKS_ENABLED == 1
- if (m_peer_rank_initialized && (p_pdb_evt->peer_id == m_highest_ranked_peer))
- {
- // Update peer rank variable if highest ranked peer has been deleted.
- rank_vars_update();
- }
- #endif
- break;
- case PM_EVT_PEER_DELETE_FAILED:
- if (m_deleting_all)
- {
- // pm_peers_delete() was called and has thus failed.
- m_deleting_all = false;
- pm_evt_t pm_delete_all_evt;
- memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t));
- pm_delete_all_evt.evt_id = PM_EVT_PEERS_DELETE_FAILED;
- pm_delete_all_evt.peer_id = PM_PEER_ID_INVALID;
- pm_delete_all_evt.conn_handle = BLE_CONN_HANDLE_INVALID;
- pm_delete_all_evt.params.peers_delete_failed_evt.error
- = p_pdb_evt->params.peer_delete_failed.error;
- send_evt = false;
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_pdb_evt); // Ensure that PEER_DELETE_FAILED arrives before PEERS_DELETE_FAILED.
- evt_send(&pm_delete_all_evt);
- }
- break;
- default:
- // Do nothing.
- break;
- }
- if (send_evt)
- {
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_pdb_evt);
- }
- }
- /**@brief Event handler for events from the Security Manager module.
- * This handler is extern in the Security Manager module.
- *
- * @param[in] p_sm_evt The incoming Security Manager event.
- */
- void pm_sm_evt_handler(pm_evt_t * p_sm_evt)
- {
- VERIFY_PARAM_NOT_NULL_VOID(p_sm_evt);
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_sm_evt);
- }
- /**@brief Event handler for events from the GATT Cache Manager module.
- * This handler is extern in GATT Cache Manager.
- *
- * @param[in] p_gcm_evt The incoming GATT Cache Manager event.
- */
- void pm_gcm_evt_handler(pm_evt_t * p_gcm_evt)
- {
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_gcm_evt);
- }
- /**@brief Event handler for events from the GATTS Cache Manager module.
- * This handler is extern in GATTS Cache Manager.
- *
- * @param[in] p_gscm_evt The incoming GATTS Cache Manager event.
- */
- void pm_gscm_evt_handler(pm_evt_t * p_gscm_evt)
- {
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_gscm_evt);
- }
- /**@brief Event handler for events from the ID Manager module.
- * This function is registered in the ID Manager.
- *
- * @param[in] p_im_evt The incoming ID Manager event.
- */
- void pm_im_evt_handler(pm_evt_t * p_im_evt)
- {
- // Forward the event to all registered Peer Manager event handlers.
- evt_send(p_im_evt);
- }
- /**
- * @brief Function for handling BLE events.
- *
- * @param[in] p_ble_evt Event received from the BLE stack.
- * @param[in] p_context Context.
- */
- static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
- {
- VERIFY_MODULE_INITIALIZED_VOID();
- im_ble_evt_handler(p_ble_evt);
- sm_ble_evt_handler(p_ble_evt);
- gcm_ble_evt_handler(p_ble_evt);
- }
- NRF_SDH_BLE_OBSERVER(m_ble_evt_observer, PM_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
- /**@brief Function for resetting the internal state of this module.
- */
- static void internal_state_reset()
- {
- m_highest_ranked_peer = PM_PEER_ID_INVALID;
- m_peer_rank_token = PM_STORE_TOKEN_INVALID;
- }
- ret_code_t pm_init(void)
- {
- ret_code_t err_code;
- err_code = pds_init();
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s failed because pds_init() returned %s.", __func__, nrf_strerror_get(err_code));
- return NRF_ERROR_INTERNAL;
- }
- err_code = pdb_init();
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s failed because pdb_init() returned %s.", __func__, nrf_strerror_get(err_code));
- return NRF_ERROR_INTERNAL;
- }
- err_code = sm_init();
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s failed because sm_init() returned %s.", __func__, nrf_strerror_get(err_code));
- return NRF_ERROR_INTERNAL;
- }
- err_code = smd_init();
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s failed because smd_init() returned %s.", __func__, nrf_strerror_get(err_code));
- return NRF_ERROR_INTERNAL;
- }
- err_code = gcm_init();
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s failed because gcm_init() returned %s.", __func__, nrf_strerror_get(err_code));
- return NRF_ERROR_INTERNAL;
- }
- err_code = gscm_init();
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s failed because gscm_init() returned %s.", __func__, nrf_strerror_get(err_code));
- return NRF_ERROR_INTERNAL;
- }
- internal_state_reset();
- m_peer_rank_initialized = false;
- m_module_initialized = true;
- // If PM_PEER_RANKS_ENABLED is 0, these variables are unused.
- UNUSED_VARIABLE(m_peer_rank_initialized);
- UNUSED_VARIABLE(m_peer_rank_token);
- UNUSED_VARIABLE(m_current_highest_peer_rank);
- UNUSED_VARIABLE(m_highest_ranked_peer);
- return NRF_SUCCESS;
- }
- ret_code_t pm_register(pm_evt_handler_t event_handler)
- {
- VERIFY_MODULE_INITIALIZED();
- if (m_n_registrants >= PM_MAX_REGISTRANTS)
- {
- return NRF_ERROR_NO_MEM;
- }
- m_evt_handlers[m_n_registrants] = event_handler;
- m_n_registrants += 1;
- return NRF_SUCCESS;
- }
- ret_code_t pm_sec_params_set(ble_gap_sec_params_t * p_sec_params)
- {
- VERIFY_MODULE_INITIALIZED();
- ret_code_t err_code;
- err_code = sm_sec_params_set(p_sec_params);
- // NRF_ERROR_INVALID_PARAM if parameters are invalid,
- // NRF_SUCCESS otherwise.
- return err_code;
- }
- ret_code_t pm_conn_secure(uint16_t conn_handle, bool force_repairing)
- {
- VERIFY_MODULE_INITIALIZED();
- ret_code_t err_code;
- err_code = sm_link_secure(conn_handle, force_repairing);
- if (err_code == NRF_ERROR_INVALID_STATE)
- {
- err_code = NRF_ERROR_BUSY;
- }
- return err_code;
- }
- void pm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config)
- {
- if (p_conn_sec_config != NULL)
- {
- sm_conn_sec_config_reply(conn_handle, p_conn_sec_config);
- }
- }
- ret_code_t pm_conn_sec_params_reply(uint16_t conn_handle,
- ble_gap_sec_params_t * p_sec_params,
- void const * p_context)
- {
- VERIFY_MODULE_INITIALIZED();
- return sm_sec_params_reply(conn_handle, p_sec_params, p_context);
- }
- void pm_local_database_has_changed(void)
- {
- #if !defined(PM_SERVICE_CHANGED_ENABLED) || (PM_SERVICE_CHANGED_ENABLED == 1)
- VERIFY_MODULE_INITIALIZED_VOID();
- gcm_local_database_has_changed();
- #endif
- }
- ret_code_t pm_id_addr_set(ble_gap_addr_t const * p_addr)
- {
- VERIFY_MODULE_INITIALIZED();
- return im_id_addr_set(p_addr);
- }
- ret_code_t pm_id_addr_get(ble_gap_addr_t * p_addr)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_addr);
- return im_id_addr_get(p_addr);
- }
- ret_code_t pm_privacy_set(pm_privacy_params_t const * p_privacy_params)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_privacy_params);
- return im_privacy_set(p_privacy_params);
- }
- ret_code_t pm_privacy_get(pm_privacy_params_t * p_privacy_params)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_privacy_params);
- VERIFY_PARAM_NOT_NULL(p_privacy_params->p_device_irk);
- return im_privacy_get(p_privacy_params);
- }
- bool pm_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk)
- {
- VERIFY_MODULE_INITIALIZED();
- if ((p_addr == NULL) || (p_irk == NULL))
- {
- return false;
- }
- else
- {
- return im_address_resolve(p_addr, p_irk);
- }
- }
- ret_code_t pm_whitelist_set(pm_peer_id_t const * p_peers,
- uint32_t peer_cnt)
- {
- VERIFY_MODULE_INITIALIZED();
- return im_whitelist_set(p_peers, peer_cnt);
- }
- ret_code_t pm_whitelist_get(ble_gap_addr_t * p_addrs,
- uint32_t * p_addr_cnt,
- ble_gap_irk_t * p_irks,
- uint32_t * p_irk_cnt)
- {
- VERIFY_MODULE_INITIALIZED();
- if (((p_addrs == NULL) && (p_irks == NULL)) ||
- ((p_addrs != NULL) && (p_addr_cnt == NULL)) ||
- ((p_irks != NULL) && (p_irk_cnt == NULL)))
- {
- // The buffers can't be both NULL, and if a buffer is provided its size must be specified.
- return NRF_ERROR_NULL;
- }
- return im_whitelist_get(p_addrs, p_addr_cnt, p_irks, p_irk_cnt);
- }
- ret_code_t pm_device_identities_list_set(pm_peer_id_t const * p_peers,
- uint32_t peer_cnt)
- {
- VERIFY_MODULE_INITIALIZED();
- return im_device_identities_list_set(p_peers, peer_cnt);
- }
- ret_code_t pm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t * p_conn_sec_status)
- {
- VERIFY_MODULE_INITIALIZED();
- return sm_conn_sec_status_get(conn_handle, p_conn_sec_status);
- }
- bool pm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t * p_sec_status_req)
- {
- VERIFY_MODULE_INITIALIZED_BOOL();
- return sm_sec_is_sufficient(conn_handle, p_sec_status_req);
- }
- ret_code_t pm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key)
- {
- VERIFY_MODULE_INITIALIZED();
- return sm_lesc_public_key_set(p_public_key);
- }
- ret_code_t pm_conn_handle_get(pm_peer_id_t peer_id, uint16_t * p_conn_handle)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_conn_handle);
- *p_conn_handle = im_conn_handle_get(peer_id);
- return NRF_SUCCESS;
- }
- ret_code_t pm_peer_id_get(uint16_t conn_handle, pm_peer_id_t * p_peer_id)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_peer_id);
- *p_peer_id = im_peer_id_get_by_conn_handle(conn_handle);
- return NRF_SUCCESS;
- }
- uint32_t pm_peer_count(void)
- {
- if (!MODULE_INITIALIZED)
- {
- return 0;
- }
- return pds_peer_count_get();
- }
- pm_peer_id_t pm_next_peer_id_get(pm_peer_id_t prev_peer_id)
- {
- pm_peer_id_t next_peer_id = prev_peer_id;
- if (!MODULE_INITIALIZED)
- {
- return PM_PEER_ID_INVALID;
- }
- do
- {
- next_peer_id = pds_next_peer_id_get(next_peer_id);
- } while (pds_peer_id_is_deleted(next_peer_id));
- return next_peer_id;
- }
- /**@brief Function for checking if the peer has a valid Identity Resolving Key.
- *
- * @param[in] p_irk Pointer to the Identity Resolving Key.
- */
- static bool peer_is_irk(ble_gap_irk_t const * const p_irk)
- {
- for (uint32_t i = 0; i < ARRAY_SIZE(p_irk->irk); i++)
- {
- if (p_irk->irk[i] != 0)
- {
- return true;
- }
- }
- return false;
- }
- ret_code_t pm_peer_id_list(pm_peer_id_t * p_peer_list,
- uint32_t * const p_list_size,
- pm_peer_id_t first_peer_id,
- pm_peer_id_list_skip_t skip_id)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_list_size);
- VERIFY_PARAM_NOT_NULL(p_peer_list);
- ret_code_t err_code;
- uint32_t size = *p_list_size;
- uint32_t current_size = 0;
- pm_peer_data_t pm_car_data;
- pm_peer_data_t pm_bond_data;
- pm_peer_id_t current_peer_id = first_peer_id;
- ble_gap_addr_t const * p_gap_addr;
- bool skip_no_addr = skip_id & PM_PEER_ID_LIST_SKIP_NO_ID_ADDR;
- bool skip_no_irk = skip_id & PM_PEER_ID_LIST_SKIP_NO_IRK;
- bool skip_no_car = skip_id & PM_PEER_ID_LIST_SKIP_NO_CAR;
- //lint -save -e685
- if ((*p_list_size < 1) ||
- (skip_id > (PM_PEER_ID_LIST_SKIP_NO_ID_ADDR | PM_PEER_ID_LIST_SKIP_ALL)))
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- //lint -restore
- *p_list_size = 0;
- if (current_peer_id == PM_PEER_ID_INVALID)
- {
- current_peer_id = pm_next_peer_id_get(current_peer_id);
- if (current_peer_id == PM_PEER_ID_INVALID)
- {
- return NRF_SUCCESS;
- }
- }
- memset(&pm_car_data, 0, sizeof(pm_peer_data_t));
- memset(&pm_bond_data, 0, sizeof(pm_peer_data_t));
- while (current_peer_id != PM_PEER_ID_INVALID)
- {
- bool skip = false;
- if (skip_no_addr || skip_no_irk)
- {
- // Get data
- pm_bond_data.p_bonding_data = NULL;
- err_code = pds_peer_data_read(current_peer_id,
- PM_PEER_DATA_ID_BONDING,
- &pm_bond_data,
- NULL);
- if (err_code == NRF_ERROR_NOT_FOUND)
- {
- skip = true;
- }
- else
- {
- VERIFY_SUCCESS(err_code);
- }
- // Check data
- if (skip_no_addr)
- {
- p_gap_addr = &pm_bond_data.p_bonding_data->peer_ble_id.id_addr_info;
- if ((p_gap_addr->addr_type != BLE_GAP_ADDR_TYPE_PUBLIC) &&
- (p_gap_addr->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_STATIC))
- {
- skip = true;
- }
- }
- if (skip_no_irk)
- {
- if (!peer_is_irk(&pm_bond_data.p_bonding_data->peer_ble_id.id_info))
- {
- skip = true;
- }
- }
- }
- if (skip_no_car)
- {
- // Get data
- pm_car_data.p_central_addr_res = NULL;
- err_code = pds_peer_data_read(current_peer_id,
- PM_PEER_DATA_ID_CENTRAL_ADDR_RES,
- &pm_car_data,
- NULL);
- if (err_code == NRF_ERROR_NOT_FOUND)
- {
- skip = true;
- }
- else
- {
- VERIFY_SUCCESS(err_code);
- }
- // Check data
- if (*pm_car_data.p_central_addr_res == 0)
- {
- skip = true;
- }
- }
- if (!skip)
- {
- p_peer_list[current_size++] = current_peer_id;
- if (current_size >= size)
- {
- break;
- }
- }
- current_peer_id = pm_next_peer_id_get(current_peer_id);
- }
- *p_list_size = current_size;
- return NRF_SUCCESS;
- }
- ret_code_t pm_peer_data_load(pm_peer_id_t peer_id,
- pm_peer_data_id_t data_id,
- void * p_data,
- uint32_t * p_length)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_data);
- VERIFY_PARAM_NOT_NULL(p_length);
- pm_peer_data_t peer_data;
- memset(&peer_data, 0, sizeof(peer_data));
- peer_data.p_all_data = p_data;
- return pds_peer_data_read(peer_id, data_id, &peer_data, p_length);
- }
- ret_code_t pm_peer_data_bonding_load(pm_peer_id_t peer_id,
- pm_peer_data_bonding_t * p_data)
- {
- uint32_t length = sizeof(pm_peer_data_bonding_t);
- return pm_peer_data_load(peer_id,
- PM_PEER_DATA_ID_BONDING,
- p_data,
- &length);
- }
- ret_code_t pm_peer_data_remote_db_load(pm_peer_id_t peer_id,
- ble_gatt_db_srv_t * p_data,
- uint32_t * p_length)
- {
- return pm_peer_data_load(peer_id,
- PM_PEER_DATA_ID_GATT_REMOTE,
- p_data,
- p_length);
- }
- ret_code_t pm_peer_data_app_data_load(pm_peer_id_t peer_id,
- void * p_data,
- uint32_t * p_length)
- {
- return pm_peer_data_load(peer_id,
- PM_PEER_DATA_ID_APPLICATION,
- p_data,
- p_length);
- }
- ret_code_t pm_peer_data_store(pm_peer_id_t peer_id,
- pm_peer_data_id_t data_id,
- void const * p_data,
- uint32_t length,
- pm_store_token_t * p_token)
- {
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_data);
- if (ALIGN_NUM(4, length) != length)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- if (data_id == PM_PEER_DATA_ID_BONDING)
- {
- pm_peer_id_t dupl_peer_id;
- dupl_peer_id = im_find_duplicate_bonding_data((pm_peer_data_bonding_t *) p_data, peer_id);
- if (dupl_peer_id != PM_PEER_ID_INVALID)
- {
- return NRF_ERROR_FORBIDDEN;
- }
- }
- pm_peer_data_flash_t peer_data;
- memset(&peer_data, 0, sizeof(peer_data));
- peer_data.length_words = BYTES_TO_WORDS(length);
- peer_data.data_id = data_id;
- peer_data.p_all_data = p_data;
- return pds_peer_data_store(peer_id, &peer_data, p_token);
- }
- ret_code_t pm_peer_data_bonding_store(pm_peer_id_t peer_id,
- pm_peer_data_bonding_t const * p_data,
- pm_store_token_t * p_token)
- {
- return pm_peer_data_store(peer_id,
- PM_PEER_DATA_ID_BONDING,
- p_data,
- ALIGN_NUM(4, sizeof(pm_peer_data_bonding_t)),
- p_token);
- }
- ret_code_t pm_peer_data_remote_db_store(pm_peer_id_t peer_id,
- ble_gatt_db_srv_t const * p_data,
- uint32_t length,
- pm_store_token_t * p_token)
- {
- return pm_peer_data_store(peer_id,
- PM_PEER_DATA_ID_GATT_REMOTE,
- p_data,
- length,
- p_token);
- }
- ret_code_t pm_peer_data_app_data_store(pm_peer_id_t peer_id,
- void const * p_data,
- uint32_t length,
- pm_store_token_t * p_token)
- {
- return pm_peer_data_store(peer_id,
- PM_PEER_DATA_ID_APPLICATION,
- p_data,
- length,
- p_token);
- }
- ret_code_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id)
- {
- VERIFY_MODULE_INITIALIZED();
- if (data_id == PM_PEER_DATA_ID_BONDING)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- return pds_peer_data_delete(peer_id, data_id);
- }
- ret_code_t pm_peer_new(pm_peer_id_t * p_new_peer_id,
- pm_peer_data_bonding_t * p_bonding_data,
- pm_store_token_t * p_token)
- {
- ret_code_t err_code;
- pm_peer_id_t peer_id;
- pm_peer_data_flash_t peer_data;
- VERIFY_MODULE_INITIALIZED();
- VERIFY_PARAM_NOT_NULL(p_bonding_data);
- VERIFY_PARAM_NOT_NULL(p_new_peer_id);
- memset(&peer_data, 0, sizeof(pm_peer_data_flash_t));
- // Search through existing bonds to look for a duplicate.
- pds_peer_data_iterate_prepare();
- // @note This check is not thread safe since data is not copied while iterating.
- while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data))
- {
- if (im_is_duplicate_bonding_data(p_bonding_data, peer_data.p_bonding_data))
- {
- *p_new_peer_id = peer_id;
- return NRF_SUCCESS;
- }
- }
- // If no duplicate data is found, prepare to write a new bond to flash.
- *p_new_peer_id = pds_peer_id_allocate();
- if (*p_new_peer_id == PM_PEER_ID_INVALID)
- {
- return NRF_ERROR_NO_MEM;
- }
- memset(&peer_data, 0, sizeof(pm_peer_data_flash_t));
- peer_data.data_id = PM_PEER_DATA_ID_BONDING;
- peer_data.p_bonding_data = p_bonding_data;
- peer_data.length_words = BYTES_TO_WORDS(sizeof(pm_peer_data_bonding_t));
- err_code = pds_peer_data_store(*p_new_peer_id, &peer_data, p_token);
- if (err_code != NRF_SUCCESS)
- {
- ret_code_t err_code_free = im_peer_free(*p_new_peer_id);
- if (err_code_free != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("Fatal error during cleanup of a failed call to %s. im_peer_free() "\
- "returned %s. peer_id: %d",
- __func__,
- nrf_strerror_get(err_code_free),
- *p_new_peer_id);
- return NRF_ERROR_INTERNAL;
- }
- // NRF_ERROR_STORAGE_FULL, if no space in flash.
- // NRF_ERROR_BUSY, if flash filesystem was busy.
- // NRF_ERROR_INVALID_ADDR, if bonding data is unaligned.
- // NRF_ERROR_INTENRAL, on internal error.
- return err_code;
- }
- return NRF_SUCCESS;
- }
- ret_code_t pm_peer_delete(pm_peer_id_t peer_id)
- {
- VERIFY_MODULE_INITIALIZED();
- return im_peer_free(peer_id);
- }
- ret_code_t pm_peers_delete(void)
- {
- VERIFY_MODULE_INITIALIZED();
- m_deleting_all = true;
- pm_peer_id_t current_peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID);
- if (current_peer_id == PM_PEER_ID_INVALID)
- {
- // No peers bonded.
- m_deleting_all = false;
- pm_evt_t pm_delete_all_evt;
- memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t));
- pm_delete_all_evt.evt_id = PM_EVT_PEERS_DELETE_SUCCEEDED;
- pm_delete_all_evt.peer_id = PM_PEER_ID_INVALID;
- pm_delete_all_evt.conn_handle = BLE_CONN_HANDLE_INVALID;
- evt_send(&pm_delete_all_evt);
- }
- while (current_peer_id != PM_PEER_ID_INVALID)
- {
- ret_code_t err_code = pm_peer_delete(current_peer_id);
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("%s() failed because a call to pm_peer_delete() returned %s. peer_id: %d",
- __func__,
- nrf_strerror_get(err_code),
- current_peer_id);
- return NRF_ERROR_INTERNAL;
- }
- current_peer_id = pds_next_peer_id_get(current_peer_id);
- }
- return NRF_SUCCESS;
- }
- ret_code_t pm_peer_ranks_get(pm_peer_id_t * p_highest_ranked_peer,
- uint32_t * p_highest_rank,
- pm_peer_id_t * p_lowest_ranked_peer,
- uint32_t * p_lowest_rank)
- {
- #if PM_PEER_RANKS_ENABLED == 0
- return NRF_ERROR_NOT_SUPPORTED;
- #else
- VERIFY_MODULE_INITIALIZED();
- pm_peer_id_t peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID);
- uint32_t peer_rank = 0;
- //lint -save -e65 -e64
- uint32_t length = sizeof(peer_rank);
- pm_peer_data_t peer_data = {.p_peer_rank = &peer_rank};
- //lint -restore
- ret_code_t err_code = pds_peer_data_read(peer_id,
- PM_PEER_DATA_ID_PEER_RANK,
- &peer_data,
- &length);
- uint32_t highest_rank = 0;
- uint32_t lowest_rank = 0xFFFFFFFF;
- pm_peer_id_t highest_ranked_peer = PM_PEER_ID_INVALID;
- pm_peer_id_t lowest_ranked_peer = PM_PEER_ID_INVALID;
- if (err_code == NRF_ERROR_INVALID_PARAM)
- {
- // No peer IDs exist.
- return NRF_ERROR_NOT_FOUND;
- }
- while ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND))
- {
- if (err_code == NRF_SUCCESS)
- {
- if (peer_rank >= highest_rank)
- {
- highest_rank = peer_rank;
- highest_ranked_peer = peer_id;
- }
- if (peer_rank < lowest_rank)
- {
- lowest_rank = peer_rank;
- lowest_ranked_peer = peer_id;
- }
- }
- peer_id = pds_next_peer_id_get(peer_id);
- err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_PEER_RANK, &peer_data, &length);
- }
- if (peer_id == PM_PEER_ID_INVALID)
- {
- if ((highest_ranked_peer == PM_PEER_ID_INVALID) || (lowest_ranked_peer == PM_PEER_ID_INVALID))
- {
- err_code = NRF_ERROR_NOT_FOUND;
- }
- else
- {
- err_code = NRF_SUCCESS;
- }
- if (p_highest_ranked_peer != NULL)
- {
- *p_highest_ranked_peer = highest_ranked_peer;
- }
- if (p_highest_rank != NULL)
- {
- *p_highest_rank = highest_rank;
- }
- if (p_lowest_ranked_peer != NULL)
- {
- *p_lowest_ranked_peer = lowest_ranked_peer;
- }
- if (p_lowest_rank != NULL)
- {
- *p_lowest_rank = lowest_rank;
- }
- }
- else
- {
- NRF_LOG_ERROR("Could not retreive ranks. pdb_peer_data_load() returned %s. peer_id: %d",
- nrf_strerror_get(err_code),
- peer_id);
- err_code = NRF_ERROR_INTERNAL;
- }
- return err_code;
- #endif
- }
- #if PM_PEER_RANKS_ENABLED == 1
- /**@brief Function for initializing peer rank functionality.
- */
- static void rank_init(void)
- {
- rank_vars_update();
- }
- #endif
- ret_code_t pm_peer_rank_highest(pm_peer_id_t peer_id)
- {
- #if PM_PEER_RANKS_ENABLED == 0
- return NRF_ERROR_NOT_SUPPORTED;
- #else
- VERIFY_MODULE_INITIALIZED();
- ret_code_t err_code;
- //lint -save -e65 -e64
- pm_peer_data_flash_t peer_data = {.length_words = BYTES_TO_WORDS(sizeof(m_current_highest_peer_rank)),
- .data_id = PM_PEER_DATA_ID_PEER_RANK,
- .p_peer_rank = &m_current_highest_peer_rank};
- //lint -restore
- if (!m_peer_rank_initialized)
- {
- rank_init();
- }
- if (!m_peer_rank_initialized || (m_peer_rank_token != PM_STORE_TOKEN_INVALID))
- {
- err_code = NRF_ERROR_BUSY;
- }
- else
- {
- if ((peer_id == m_highest_ranked_peer) && (m_current_highest_peer_rank > 0))
- {
- pm_evt_t pm_evt;
- // The reported peer is already regarded as highest (provided it has an index at all)
- err_code = NRF_SUCCESS;
- memset(&pm_evt, 0, sizeof(pm_evt));
- pm_evt.evt_id = PM_EVT_PEER_DATA_UPDATE_SUCCEEDED;
- pm_evt.conn_handle = im_conn_handle_get(peer_id);
- pm_evt.peer_id = peer_id;
- pm_evt.params.peer_data_update_succeeded.data_id = PM_PEER_DATA_ID_PEER_RANK;
- pm_evt.params.peer_data_update_succeeded.action = PM_PEER_DATA_OP_UPDATE;
- pm_evt.params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID;
- pm_evt.params.peer_data_update_succeeded.flash_changed = false;
- evt_send(&pm_evt);
- }
- else
- {
- if (m_current_highest_peer_rank == UINT32_MAX)
- {
- err_code = NRF_ERROR_RESOURCES;
- }
- else
- {
- m_current_highest_peer_rank += 1;
- err_code = pds_peer_data_store(peer_id, &peer_data, &m_peer_rank_token);
- if (err_code != NRF_SUCCESS)
- {
- m_peer_rank_token = PM_STORE_TOKEN_INVALID;
- m_current_highest_peer_rank -= 1;
- if ((err_code != NRF_ERROR_BUSY) &&
- (err_code != NRF_ERROR_STORAGE_FULL) &&
- (err_code != NRF_ERROR_INVALID_PARAM)) // Assume INVALID_PARAM refers to peer_id, not data_id.
- {
- NRF_LOG_ERROR("Could not update rank. pdb_raw_store() returned %s. "\
- "peer_id: %d",
- nrf_strerror_get(err_code),
- peer_id);
- err_code = NRF_ERROR_INTERNAL;
- }
- }
- }
- }
- }
- return err_code;
- #endif
- }
- #endif // NRF_MODULE_ENABLED(PEER_MANAGER)
|