diff --git a/doc/nrf-bm/libraries/bluetooth/ble_peer_manager.rst b/doc/nrf-bm/libraries/bluetooth/ble_peer_manager.rst index 9fb47086f3..c5f41cf37d 100644 --- a/doc/nrf-bm/libraries/bluetooth/ble_peer_manager.rst +++ b/doc/nrf-bm/libraries/bluetooth/ble_peer_manager.rst @@ -311,7 +311,7 @@ The handlers can be provided to the :c:func:`pm_register` function directly, or .. code-block:: c - static void pm_evt_handler(pm_evt_t const *p_evt) + static void pm_evt_handler(const struct pm_evt *p_evt) { pm_handler_on_pm_evt(p_evt); pm_handler_disconnect_on_sec_failure(p_evt); @@ -349,7 +349,7 @@ The store operation is finished when either the :c:enum:`PM_EVT_PEER_DATA_UPDATE .. code-block:: c uint32_t err; - pm_store_token_t store_token; + uint32_t store_token; err = pm_peer_data_remote_db_store(peer_id, array_of_services, number_of_services, &store_token); if (err != NRF_ERROR_BUSY) { @@ -362,7 +362,7 @@ The :c:func:`pm_peer_data_store` function can also be used directly, as in the f .. code-block:: c uint32_t err; - pm_store_token_t store_token; + uint32_t store_token; err = pm_peer_data_store(peer_id, PM_PEER_DATA_ID_GATT_REMOTE, array_of_services, number_of_services, &store_token); if (err != NRF_ERROR_BUSY) { @@ -382,9 +382,9 @@ The following example shows how to use the :c:func:`pm_whitelist_set` function t { /* Fetch a list of peer IDs from Peer Manager and whitelist them. */ - pm_peer_id_t peer_ids[8] = {PM_PEER_ID_INVALID}; + uint16_t peer_ids[8] = {PM_PEER_ID_INVALID}; uint32_t n_peer_ids = 0; - pm_peer_id_t peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID); + uint16_t peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID); while((peer_id != PM_PEER_ID_INVALID) && (n_peer_ids < 8)) { peer_ids[n_peer_ids++] = peer_id; diff --git a/doc/nrf-bm/release_notes/release_notes_changelog.rst b/doc/nrf-bm/release_notes/release_notes_changelog.rst index e45bfb731e..12aea214aa 100644 --- a/doc/nrf-bm/release_notes/release_notes_changelog.rst +++ b/doc/nrf-bm/release_notes/release_notes_changelog.rst @@ -81,6 +81,30 @@ Libraries * The ``CONFIG_PM_LESC_ENABLED`` Kconfig option is renamed to :kconfig:option:`CONFIG_PM_LESC`. * The ``CONFIG_PM_RA_PROTECTION_ENABLED`` Kconfig option is renamed to :kconfig:option:`CONFIG_PM_RA_PROTECTION`. * The :kconfig:option:`CONFIG_PM_SERVICE_CHANGED` Kconfig option to depend on the :kconfig:option:`CONFIG_NRF_SDH_BLE_SERVICE_CHANGED` Kconfig option. + * All instances of ``pm_peer_id_t`` to ``uint16_t`` and removed the ``pm_peer_id_t`` type. + * All instances of ``pm_store_token_t`` to ``uint32_t`` and removed the ``pm_store_token_t`` type. + * All instances of ``pm_sec_error_code_t`` to ``uint16_t`` and removed the ``pm_sec_error_code_t`` type. + * All instances of ``ble_gatt_db_src_t`` to :c:struct:`ble_gatt_db_srv` and removed the ``ble_gatt_db_src_t`` type. + * All instances of ``ble_gatt_db_char_t`` to :c:struct:`ble_gatt_db_char` and removed the ``ble_gatt_db_char_t`` type. + * All instances of ``pm_peer_id_list_skip_t`` to :c:enum:`pm_peer_id_list_skip` and removed the ``pm_peer_id_list_skip_t`` type. + * All instances of ``pm_peer_data_id_t`` to :c:enum:`pm_peer_data_id` and removed the ``pm_peer_data_id_t`` type. + * All instances of ``pm_conn_sec_procedure_t`` to :c:enum:`pm_conn_sec_procedure` and removed the ``pm_conn_sec_procedure_t`` type. + * All instances of ``pm_conn_sec_config_t`` to :c:struct:`pm_conn_sec_config` and removed the ``pm_conn_sec_config_t`` type. + * All instances of ``pm_peer_data_bonding_t`` to :c:struct:`pm_peer_data_bonding` and removed the ``pm_peer_data_bonding_t`` type. + * All instances of ``pm_peer_data_local_gatt_db_t`` to :c:struct:`pm_peer_data_local_gatt_db` and removed the ``pm_peer_data_local_gatt_db_t`` type. + * All instances of ``pm_privacy_params_t`` to :c:type:`ble_gap_privacy_params_t` and removed the ``pm_privacy_params_t`` type. + * All instances of ``pm_conn_sec_status_t`` to :c:struct:`pm_conn_sec_status` and removed the ``pm_conn_sec_status_t`` type. + * All instances of ``pm_evt_id_t`` to :c:enum:`pm_evt_id` and removed the ``pm_evt_id_t`` type. + * All instances of ``pm_conn_config_req_evt_t`` to :c:struct:`pm_conn_config_req_evt` and removed the ``pm_conn_config_req_evt_t`` type. + * All instances of ``pm_conn_sec_start_evt_t`` to :c:struct:`pm_conn_sec_start_evt` and removed the ``pm_conn_sec_start_evt_t`` type. + * All instances of ``pm_conn_secured_evt_t`` to :c:struct:`pm_conn_secured_evt` and removed the ``pm_conn_secured_evt_t`` type. + * All instances of ``pm_conn_secure_failed_evt_t`` to :c:struct:`pm_conn_secure_failed_evt` and removed the ``pm_conn_secure_failed_evt_t`` type. + * All instances of ``pm_conn_sec_params_req_evt_t`` to :c:struct:`pm_conn_sec_params_req_evt` and removed the ``pm_conn_sec_params_req_evt_t`` type. + * All instances of ``pm_peer_data_op_t`` to :c:enum:`pm_peer_data_op` and removed the ``pm_peer_data_op_t`` type. + * All instances of ``pm_peer_data_update_succeeded_evt_t`` to :c:struct:`pm_peer_data_update_succeeded_evt` and removed the ``pm_peer_data_update_succeeded_evt_t`` type. + * All instances of ``pm_peer_data_update_failed_t`` to :c:struct:`pm_peer_data_update_failed` and removed the ``pm_peer_data_update_failed_t`` type. + * All instances of ``pm_failure_evt_t`` to :c:struct:`pm_failure_evt` and removed the ``pm_failure_evt_t`` type. + * All instances of ``pm_evt_t`` to :c:struct:`pm_evt` and removed the ``pm_evt_t`` type. Samples ======= diff --git a/include/bm/bluetooth/peer_manager/ble_gatt_db.h b/include/bm/bluetooth/peer_manager/ble_gatt_db.h index f966a161a1..a1a6ed4ef9 100644 --- a/include/bm/bluetooth/peer_manager/ble_gatt_db.h +++ b/include/bm/bluetooth/peer_manager/ble_gatt_db.h @@ -29,7 +29,7 @@ extern "C" { /** * @brief Structure for holding the characteristic and the handle of its CCCD present on a server. */ -typedef struct { +struct ble_gatt_db_char { /** @brief Structure containing information about the characteristic. */ ble_gattc_char_t characteristic; /** @@ -55,13 +55,13 @@ typedef struct { * descriptor is not present at the server. */ uint16_t report_ref_handle; -} ble_gatt_db_char_t; +}; /** * @brief Structure for holding information about the service and the characteristics present on a * server. */ -typedef struct { +struct ble_gatt_db_srv { /** @brief UUID of the service. */ ble_uuid_t srv_uuid; /** @brief Number of characteristics present in the service. */ @@ -72,8 +72,8 @@ typedef struct { * @brief Array of information related to the characteristics present in the service. * This list can extend further than one. */ - ble_gatt_db_char_t charateristics[BLE_GATT_DB_MAX_CHARS]; -} ble_gatt_db_srv_t; + struct ble_gatt_db_char charateristics[BLE_GATT_DB_MAX_CHARS]; +}; #ifdef __cplusplus } diff --git a/include/bm/bluetooth/peer_manager/peer_manager.h b/include/bm/bluetooth/peer_manager/peer_manager.h index 2fda805a9a..4f22f01be2 100644 --- a/include/bm/bluetooth/peer_manager/peer_manager.h +++ b/include/bm/bluetooth/peer_manager/peer_manager.h @@ -34,7 +34,7 @@ extern "C" { #endif /** @brief Peer list filtrations. They determine which peer ID will be added to list. */ -typedef enum { +enum pm_peer_id_list_skip { /** @brief Add all peers. */ PM_PEER_ID_LIST_ALL_ID, /** @brief Add only peers with an ID address (static address). */ @@ -49,7 +49,7 @@ typedef enum { PM_PEER_ID_LIST_SKIP_NO_CAR = 1 << 2, /** @brief All above filters applied. */ PM_PEER_ID_LIST_SKIP_ALL = PM_PEER_ID_LIST_SKIP_NO_IRK | PM_PEER_ID_LIST_SKIP_NO_CAR -} pm_peer_id_list_skip_t; +}; /** * @brief Initialize the Peer Manager. @@ -154,20 +154,20 @@ uint32_t pm_conn_secure(uint16_t conn_handle, bool force_repairing); * @retval NRF_SUCCESS If the reply was successful. * @retval NRF_ERROR_NULL If @p context was NULL. */ -uint32_t pm_conn_exclude(uint16_t conn_handle, void const *context); +uint32_t pm_conn_exclude(uint16_t conn_handle, const void *context); /** * @brief Provide a security configuration for a link. * * @details This function is optional, and must be called in reply to a @ref * PM_EVT_CONN_SEC_CONFIG_REQ event, before the Peer Manager event handler returns. If it - * is not called in time, a default configuration is used. See @ref pm_conn_sec_config_t + * is not called in time, a default configuration is used. See @ref pm_conn_sec_config * for the value of the default. * * @param[in] conn_handle The connection to set the configuration for. * @param[in] conn_sec_config The configuration. */ -void pm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *conn_sec_config); +void pm_conn_sec_config_reply(uint16_t conn_handle, struct pm_conn_sec_config *conn_sec_config); /** * @brief Provide security parameters for a link. @@ -215,7 +215,7 @@ void pm_local_database_has_changed(void); * @retval NRF_ERROR_NULL If @p conn_sec_status was NULL. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t *conn_sec_status); +uint32_t pm_conn_sec_status_get(uint16_t conn_handle, struct pm_conn_sec_status *conn_sec_status); /** * @brief Compare the security status of a connection against a baseline. @@ -228,7 +228,7 @@ uint32_t pm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t *conn * @retval false If the security status of the connection does not fulfill the baseline, or could * not be retrieved. */ -bool pm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t *sec_status_req); +bool pm_sec_is_sufficient(uint16_t conn_handle, struct pm_conn_sec_status *sec_status_req); /** * @brief Specify the public key to use for LESC operations. @@ -276,7 +276,7 @@ uint32_t pm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *public_key); * @ref BLE_GAP_WHITELIST_ADDR_MAX_COUNT. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_whitelist_set(const pm_peer_id_t *peers, uint32_t peer_cnt); +uint32_t pm_whitelist_set(const uint16_t *peers, uint32_t peer_cnt); /** * @brief Retrieve the previously set whitelist. @@ -336,7 +336,7 @@ uint32_t pm_whitelist_get(ble_gap_addr_t *addrs, uint32_t *addr_cnt, ble_gap_irk * @retval NRF_ERROR_NOT_SUPPORTED If using a SoftDevice that does not support * device identities. */ -uint32_t pm_device_identities_list_set(const pm_peer_id_t *peers, uint32_t peer_cnt); +uint32_t pm_device_identities_list_set(const uint16_t *peers, uint32_t peer_cnt); /** * @brief Set the local Bluetooth identity address. @@ -406,7 +406,7 @@ uint32_t pm_id_addr_get(ble_gap_addr_t *addr); * privacy are enabled. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_privacy_set(const pm_privacy_params_t *privacy_params); +uint32_t pm_privacy_set(const ble_gap_privacy_params_t *privacy_params); /** * @brief Retrieve privacy settings. @@ -420,7 +420,7 @@ uint32_t pm_privacy_set(const pm_privacy_params_t *privacy_params); * NULL. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_privacy_get(pm_privacy_params_t *privacy_params); +uint32_t pm_privacy_get(ble_gap_privacy_params_t *privacy_params); /** * @brief Resolve a resolvable address with an identity resolution key (IRK). @@ -446,7 +446,7 @@ bool pm_address_resolve(const ble_gap_addr_t *addr, const ble_gap_irk_t *irk); * @retval NRF_ERROR_NULL If @p conn_handle was NULL. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_conn_handle_get(pm_peer_id_t peer_id, uint16_t *conn_handle); +uint32_t pm_conn_handle_get(uint16_t peer_id, uint16_t *conn_handle); /** * @brief Retrieve the ID of a peer, given its connection handle. @@ -459,7 +459,7 @@ uint32_t pm_conn_handle_get(pm_peer_id_t peer_id, uint16_t *conn_handle); * @retval NRF_ERROR_NULL If @p peer_id was NULL. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_peer_id_get(uint16_t conn_handle, pm_peer_id_t *peer_id); +uint32_t pm_peer_id_get(uint16_t conn_handle, uint16_t *peer_id); /** * @brief Retrieve a filtered list of peer IDs. @@ -484,8 +484,8 @@ uint32_t pm_peer_id_get(uint16_t conn_handle, pm_peer_id_t *peer_id); * @retval NRF_ERROR_NULL If @p peer_list or @p list_size were NULL. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_peer_id_list(pm_peer_id_t *peer_list, uint32_t *const list_size, - pm_peer_id_t first_peer_id, pm_peer_id_list_skip_t skip_id); +uint32_t pm_peer_id_list(uint16_t *peer_list, uint32_t *const list_size, + uint16_t first_peer_id, enum pm_peer_id_list_skip skip_id); /** * @brief Get the next peer ID in the sequence of all used peer IDs. @@ -502,7 +502,7 @@ uint32_t pm_peer_id_list(pm_peer_id_t *peer_list, uint32_t *const list_size, * next peer ID is the first used peer ID. If @p prev_peer_id was the last * used peer ID, the function returns @ref PM_PEER_ID_INVALID. */ -pm_peer_id_t pm_next_peer_id_get(pm_peer_id_t prev_peer_id); +uint16_t pm_next_peer_id_get(uint16_t prev_peer_id); /** * @brief Query the number of valid peer IDs that are available. @@ -533,7 +533,7 @@ uint32_t pm_peer_count(void); * @param[in] peer_id Peer ID to get data for. * @param[in] data_id Which type of data to read. * @param[out] data Where to put the retrieved data. The documentation for - * @ref pm_peer_data_id_t specifies what data type each data ID is stored as. + * @ref pm_peer_data_id specifies what data type each data ID is stored as. * @param[in,out] len In: The length in bytes of @p data. * Out: The length in bytes of the read data, if the read was successful. * @@ -549,27 +549,27 @@ uint32_t pm_peer_count(void); * bytes) when storing. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_peer_data_load(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, void *data, +uint32_t pm_peer_data_load(uint16_t peer_id, enum pm_peer_data_id data_id, void *data, uint32_t *len); /** * @brief Read a peer's bonding data (@ref PM_PEER_DATA_ID_BONDING). * @details See @ref pm_peer_data_load for parameters and return values. */ -uint32_t pm_peer_data_bonding_load(pm_peer_id_t peer_id, pm_peer_data_bonding_t *data); +uint32_t pm_peer_data_bonding_load(uint16_t peer_id, struct pm_peer_data_bonding *data); /** * @brief Read a peer's remote DB values. (@ref PM_PEER_DATA_ID_GATT_REMOTE). * @details See @ref pm_peer_data_load for parameters and return values. */ -uint32_t pm_peer_data_remote_db_load(pm_peer_id_t peer_id, ble_gatt_db_srv_t *data, +uint32_t pm_peer_data_remote_db_load(uint16_t peer_id, struct ble_gatt_db_srv *data, uint32_t *len); /** * @brief Read a peer's application data. (@ref PM_PEER_DATA_ID_APPLICATION). * @details See @ref pm_peer_data_load for parameters and return values. */ -uint32_t pm_peer_data_app_data_load(pm_peer_id_t peer_id, void *data, uint32_t *len); +uint32_t pm_peer_data_app_data_load(uint16_t peer_id, void *data, uint32_t *len); /** @} */ /** @@ -590,7 +590,7 @@ uint32_t pm_peer_data_app_data_load(pm_peer_id_t peer_id, void *data, uint32_t * * * @param[in] peer_id Peer ID to set data for. * @param[in] data_id The type of data to set. - * @param[in] data New value to set. The documentation for @ref pm_peer_data_id_t specifies + * @param[in] data New value to set. The documentation for @ref pm_peer_data_id specifies * what data type each data ID should be stored as. * @param[in] len The length in bytes of @p data. * @param[out] token A token that identifies this particular store operation. The token can be @@ -609,32 +609,32 @@ uint32_t pm_peer_data_app_data_load(pm_peer_id_t peer_id, void *data, uint32_t * * so duplicate entries are avoided. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_peer_data_store(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, const void *data, - uint32_t len, pm_store_token_t *token); +uint32_t pm_peer_data_store(uint16_t peer_id, enum pm_peer_data_id data_id, const void *data, + uint32_t len, uint32_t *token); /** * @brief Set or update a peer's bonding data (@ref PM_PEER_DATA_ID_BONDING). * * @details See @ref pm_peer_data_store for parameters and return values. */ -uint32_t pm_peer_data_bonding_store(pm_peer_id_t peer_id, const pm_peer_data_bonding_t *data, - pm_store_token_t *token); +uint32_t pm_peer_data_bonding_store(uint16_t peer_id, const struct pm_peer_data_bonding *data, + uint32_t *token); /** * @brief Set or update a peer's remote DB values. (@ref PM_PEER_DATA_ID_GATT_REMOTE). * * @details See @ref pm_peer_data_store for parameters and return values. */ -uint32_t pm_peer_data_remote_db_store(pm_peer_id_t peer_id, const ble_gatt_db_srv_t *data, - uint32_t len, pm_store_token_t *token); +uint32_t pm_peer_data_remote_db_store(uint16_t peer_id, const struct ble_gatt_db_srv *data, + uint32_t len, uint32_t *token); /** * @brief Set or update a peer's application data. (@ref PM_PEER_DATA_ID_APPLICATION). * * @details See @ref pm_peer_data_store for parameters and return values. */ -uint32_t pm_peer_data_app_data_store(pm_peer_id_t peer_id, const void *data, uint32_t len, - pm_store_token_t *token); +uint32_t pm_peer_data_app_data_store(uint16_t peer_id, const void *data, uint32_t len, + uint32_t *token); /** @} */ /** @@ -664,7 +664,7 @@ uint32_t pm_peer_data_app_data_store(pm_peer_id_t peer_id, const void *data, uin * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. * @retval NRF_ERROR_INTERNAL If an internal error occurred. */ -uint32_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id); +uint32_t pm_peer_data_delete(uint16_t peer_id, enum pm_peer_data_id data_id); /** * @brief Manually add a peer to the non-volatile storage. @@ -690,8 +690,8 @@ uint32_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id); * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. * @retval NRF_ERROR_INTERNAL If an internal error occurred. */ -uint32_t pm_peer_new(pm_peer_id_t *new_peer_id, pm_peer_data_bonding_t *bonding_data, - pm_store_token_t *token); +uint32_t pm_peer_new(uint16_t *new_peer_id, struct pm_peer_data_bonding *bonding_data, + uint32_t *token); /** * @brief Delete all data stored for a peer and free the peer ID. @@ -712,7 +712,7 @@ uint32_t pm_peer_new(pm_peer_id_t *new_peer_id, pm_peer_data_bonding_t *bonding_ * @retval NRF_ERROR_INVALID_PARAM If the peer ID was not valid. * @retval NRF_ERROR_INVALID_STATE If the Peer Manager is not initialized. */ -uint32_t pm_peer_delete(pm_peer_id_t peer_id); +uint32_t pm_peer_delete(uint16_t peer_id); /** * @brief Delete all data stored for all peers. @@ -767,8 +767,8 @@ uint32_t pm_peers_delete(void); * @retval NRF_ERROR_NOT_SUPPORTED If the peer rank functionality has been disabled via the * @c CONFIG_PM_PEER_RANKS Kconfig option. */ -uint32_t pm_peer_ranks_get(pm_peer_id_t *highest_ranked_peer, uint32_t *highest_rank, - pm_peer_id_t *lowest_ranked_peer, uint32_t *lowest_rank); +uint32_t pm_peer_ranks_get(uint16_t *highest_ranked_peer, uint32_t *highest_rank, + uint16_t *lowest_ranked_peer, uint32_t *lowest_rank); /** * @brief Update the rank of a peer to be the highest among all stored peers. @@ -782,8 +782,8 @@ uint32_t pm_peer_ranks_get(pm_peer_id_t *highest_ranked_peer, uint32_t *highest_ * @ref pm_peer_ranks_get. * * @note The @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED event can arrive before the function returns if - * the peer is already ranked highest. In this case, the @ref - * pm_peer_data_update_succeeded_evt_t.flash_changed flag in the event will be false. + * the peer is already ranked highest. In this case, the flash_changed flag in the + * @ref pm_peer_data_update_succeeded_evt event will be false. * * @param[in] peer_id The peer to rank highest. * @@ -802,7 +802,7 @@ uint32_t pm_peer_ranks_get(pm_peer_id_t *highest_ranked_peer, uint32_t *highest_ * @retval NRF_ERROR_NOT_SUPPORTED If peer rank functionality has been disabled via the * @c CONFIG_PM_PEER_RANKS Kconfig option. */ -uint32_t pm_peer_rank_highest(pm_peer_id_t peer_id); +uint32_t pm_peer_rank_highest(uint16_t peer_id); /** @} */ diff --git a/include/bm/bluetooth/peer_manager/peer_manager_handler.h b/include/bm/bluetooth/peer_manager/peer_manager_handler.h index 893e9f6392..f9e230f0f2 100644 --- a/include/bm/bluetooth/peer_manager/peer_manager_handler.h +++ b/include/bm/bluetooth/peer_manager/peer_manager_handler.h @@ -35,7 +35,7 @@ extern "C" { * * @param[in] pm_evt Peer Manager event to handle. */ -void pm_handler_on_pm_evt(const pm_evt_t *pm_evt); +void pm_handler_on_pm_evt(const struct pm_evt *pm_evt); /** * @brief Auxiliary standard function for logging Peer Manager events. @@ -46,7 +46,7 @@ void pm_handler_on_pm_evt(const pm_evt_t *pm_evt); * * @param[in] pm_evt Peer Manager event to log. */ -void pm_handler_pm_evt_log(const pm_evt_t *pm_evt); +void pm_handler_pm_evt_log(const struct pm_evt *pm_evt); /** * @brief Auxiliary standard function for maintaining room in non-volatile storage based on Peer @@ -63,7 +63,7 @@ void pm_handler_pm_evt_log(const pm_evt_t *pm_evt); * * @param[in] pm_evt Peer Manager event to handle. */ -void pm_handler_flash_clean(const pm_evt_t *pm_evt); +void pm_handler_flash_clean(const struct pm_evt *pm_evt); /** * @brief Function to call when a Peer Manager function returns @ref NRF_ERROR_RESOURCES. @@ -83,7 +83,7 @@ void pm_handler_flash_clean_on_return(void); * * @param[in] pm_evt Peer Manager event to handle. */ -void pm_handler_disconnect_on_sec_failure(const pm_evt_t *pm_evt); +void pm_handler_disconnect_on_sec_failure(const struct pm_evt *pm_evt); /** * @brief Auxiliary standard function for disconnecting on insufficient connection security. @@ -98,8 +98,8 @@ void pm_handler_disconnect_on_sec_failure(const pm_evt_t *pm_evt); * @param[in] pm_evt Peer Manager event to handle. * @param[in] min_conn_sec Minimum security status below which to disconnect the link. */ -void pm_handler_disconnect_on_insufficient_sec(const pm_evt_t *pm_evt, - pm_conn_sec_status_t *min_conn_sec); +void pm_handler_disconnect_on_insufficient_sec(const struct pm_evt *pm_evt, + struct pm_conn_sec_status *min_conn_sec); /** * @brief Secure a connection when it is established. diff --git a/include/bm/bluetooth/peer_manager/peer_manager_types.h b/include/bm/bluetooth/peer_manager/peer_manager_types.h index 58fa5a55af..a8e66cd77c 100644 --- a/include/bm/bluetooth/peer_manager/peer_manager_types.h +++ b/include/bm/bluetooth/peer_manager/peer_manager_types.h @@ -26,19 +26,6 @@ extern "C" { #endif -/** @brief Handle to uniquely identify a peer for which we have persistently stored data. */ -typedef uint16_t pm_peer_id_t; - -/** @brief Type that is used to hold a reference to a stored item in non-volatile storage. */ -typedef uint32_t pm_store_token_t; - -/** - * @brief Errors from security procedures in Peer Manager. - * - * @details Possible values are defined in @ref PM_SEC_ERRORS and @ref BLE_GAP_SEC_STATUS. - */ -typedef uint16_t pm_sec_error_code_t; - /** @brief Invalid value for Peer ID. */ #define PM_PEER_ID_INVALID 0xFFFF /** @brief Invalid value for store token. */ @@ -46,10 +33,9 @@ typedef uint16_t pm_sec_error_code_t; /** @brief The number of available peer IDs. */ #define PM_PEER_ID_N_AVAILABLE_IDS 256 /** @brief The static-length part of the local GATT data struct. */ -#define PM_LOCAL_DB_LEN_OVERHEAD_BYTES offsetof(pm_peer_data_local_gatt_db_t, data) +#define PM_LOCAL_DB_LEN_OVERHEAD_BYTES offsetof(struct pm_peer_data_local_gatt_db, data) /** - * @brief The base for Peer Manager defined errors. See @ref PM_SEC_ERRORS and - * @ref pm_sec_error_code_t. + * @brief The base for Peer Manager defined errors. See @ref PM_SEC_ERRORS. */ #define PM_CONN_SEC_ERROR_BASE 0x1000 @@ -128,16 +114,16 @@ typedef uint16_t pm_sec_error_code_t; /** * @brief The different types of data associated with a peer. */ -typedef enum { +enum pm_peer_data_id { /** @brief The smallest data ID. */ PM_PEER_DATA_ID_FIRST = PM_PEER_DATA_ID_FIRST_VX, - /** @brief The data ID for bonding data. Type: @ref pm_peer_data_bonding_t. */ + /** @brief The data ID for bonding data. Type: @ref pm_peer_data_bonding. */ PM_PEER_DATA_ID_BONDING = PM_PEER_DATA_ID_BONDING_V2, /** @brief The data ID for service changed state. Type: bool. */ PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING = PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING_V1, /** * @brief The data ID for local GATT data (sys attributes). - * Type: @ref pm_peer_data_local_gatt_db_t. + * Type: @ref pm_peer_data_local_gatt_db. */ PM_PEER_DATA_ID_GATT_LOCAL = PM_PEER_DATA_ID_GATT_LOCAL_V2, /** @brief The data ID for remote GATT data. Type: uint8_t array. */ @@ -155,10 +141,10 @@ typedef enum { PM_PEER_DATA_ID_LAST = PM_PEER_DATA_ID_LAST_VX, /** @brief A data ID guaranteed to be invalid. */ PM_PEER_DATA_ID_INVALID = PM_PEER_DATA_ID_INVALID_VX, -} pm_peer_data_id_t; +}; /** @brief Different procedures that can lead to an encrypted link. */ -typedef enum { +enum pm_conn_sec_procedure { /** * @brief Using an LTK that was shared during a previous bonding procedure to encrypt the * link. @@ -168,20 +154,20 @@ typedef enum { PM_CONN_SEC_PROCEDURE_BONDING, /** @brief A pairing procedure with no bonding. */ PM_CONN_SEC_PROCEDURE_PAIRING, -} pm_conn_sec_procedure_t; +}; /** @brief Configuration of a security procedure. */ -typedef struct { +struct pm_conn_sec_config { /** * @brief Whether to allow the peer to pair if it wants to, but is already bonded. * If this is false, the procedure is rejected, and no more events are sent. * Default: false. */ bool allow_repairing; -} pm_conn_sec_config_t; +}; /** @brief Data associated with a bond to a peer. */ -typedef struct { +struct pm_peer_data_bonding { /** @brief The BLE role of the local device during bonding. See @ref BLE_GAP_ROLES. */ uint8_t own_role; /** @brief The peer's Bluetooth address and identity resolution key (IRK). */ @@ -193,39 +179,20 @@ typedef struct { * the peer. */ ble_gap_enc_key_t own_ltk; -} pm_peer_data_bonding_t; +}; /** @brief Data on a local GATT database. */ -typedef struct { +struct pm_peer_data_local_gatt_db { /** @brief Flags that describe the database attributes. */ uint32_t flags; /** @brief Size of the attribute array. */ uint16_t len; /** @brief Array to hold the database attributes. */ uint8_t data[1]; -} pm_peer_data_local_gatt_db_t; - -/** - * @brief Device Privacy. - * - * The privacy feature provides a way for the device to avoid being tracked over a period of - * time. The privacy feature, when enabled, hides the local device identity and replaces it - * with a private address that is automatically refreshed at a specified interval. - * - * If a device still wants to be recognized by other peers, it needs to share it's Identity - * Resolving Key (IRK). With this key, a device can generate a random private address that - * can only be recognized by peers in possession of that key, and devices can establish - * connections without revealing their real identities. - * - * @note If the device IRK is updated, the new IRK becomes the one to be distributed in all - * bonding procedures performed after @ref sd_ble_gap_privacy_set returns. - * The IRK distributed during bonding procedure is the device IRK that is active when @ref - * sd_ble_gap_sec_params_reply is called. - */ -typedef ble_gap_privacy_params_t pm_privacy_params_t; +}; /** @brief Security status of a connection. */ -typedef struct { +struct pm_conn_sec_status { /** @brief The connection is active (not disconnected). */ uint8_t connected: 1; /** @brief The communication on this link is encrypted. */ @@ -240,10 +207,10 @@ typedef struct { uint8_t lesc: 1; /** @brief Reserved for future use. */ uint8_t reserved: 3; -} pm_conn_sec_status_t; +}; /** @brief Types of events that can come from the @ref peer_manager module. */ -typedef enum { +enum pm_evt_id { /** * @brief A connected peer has been identified as one with which we have a bond. * When performing bonding with a peer for the first time, this event will not be @@ -378,10 +345,10 @@ typedef enum { PM_EVT_FLASH_GARBAGE_COLLECTED, /** @brief Garbage collection was attempted but failed. */ PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED, -} pm_evt_id_t; +}; /** @brief Parameters specific to the @ref PM_EVT_CONN_CONFIG_REQ event. */ -typedef struct { +struct pm_conn_config_req_evt { /** @brief Connected Event parameters. */ const ble_gap_evt_connected_t *peer_params; /** @@ -389,18 +356,18 @@ typedef struct { * context argument. */ const void *context; -} pm_conn_config_req_evt_t; +}; /** @brief Events parameters specific to the @ref PM_EVT_CONN_SEC_START event. */ -typedef struct { +struct pm_conn_sec_start_evt { /** @brief The procedure that has started. */ - pm_conn_sec_procedure_t procedure; -} pm_conn_sec_start_evt_t; + enum pm_conn_sec_procedure procedure; +}; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_SUCCEEDED event. */ -typedef struct { +struct pm_conn_secured_evt { /** @brief The procedure that led to securing the link. */ - pm_conn_sec_procedure_t procedure; + enum pm_conn_sec_procedure procedure; /** * @brief Whether bonding data was successfully requested to be stored. * This is false if: No bonding happened, or an internal error occurred @@ -408,20 +375,20 @@ typedef struct { * @ref pm_conn_sec_config_reply. */ bool data_stored; -} pm_conn_secured_evt_t; +}; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_FAILED event. */ -typedef struct { +struct pm_conn_secure_failed_evt { /** @brief The procedure that failed. */ - pm_conn_sec_procedure_t procedure; - /** @brief An error code that describes the failure. */ - pm_sec_error_code_t error; + enum pm_conn_sec_procedure procedure; + /** @brief An error code that describes the failure. See @ref PM_SEC_ERRORS. */ + uint16_t error; /** @brief The party that raised the error, see @ref BLE_GAP_SEC_STATUS_SOURCES. */ uint8_t error_src; -} pm_conn_secure_failed_evt_t; +}; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_PARAMS_REQ event. */ -typedef struct { +struct pm_conn_sec_params_req_evt { /** @brief Peer security parameters, if role is peripheral. Otherwise, this is NULL. */ const ble_gap_sec_params_t *peer_params; /** @@ -429,22 +396,22 @@ typedef struct { * context argument. */ const void *context; -} pm_conn_sec_params_req_evt_t; +}; /** @brief Actions that can be performed to peer data in persistent storage. */ -typedef enum { +enum pm_peer_data_op { /** @brief Writing or overwriting the data. */ PM_PEER_DATA_OP_UPDATE, /** @brief Removing the data. */ PM_PEER_DATA_OP_DELETE, -} pm_peer_data_op_t; +}; /** @brief Parameters specific to the @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED event. */ -typedef struct { +struct pm_peer_data_update_succeeded_evt { /** @brief The type of the data that was changed. */ - pm_peer_data_id_t data_id; + enum pm_peer_data_id data_id; /** @brief What happened to the data. */ - pm_peer_data_op_t action; + enum pm_peer_data_op action; /** * @brief Token that identifies the operation. For @ref PM_PEER_DATA_OP_DELETE actions, * this token can be disregarded. For @ref PM_PEER_DATA_OP_UPDATE actions, @@ -458,14 +425,14 @@ typedef struct { * scenarios, this flag will be true even if the new value is the same as the old. */ uint8_t flash_changed: 1; -} pm_peer_data_update_succeeded_evt_t; +}; /** @brief Parameters specific to the @ref PM_EVT_PEER_DATA_UPDATE_FAILED event. */ -typedef struct { +struct pm_peer_data_update_failed { /** @brief The type of the data that was supposed to be changed. */ - pm_peer_data_id_t data_id; + enum pm_peer_data_id data_id; /** @brief The action that failed. */ - pm_peer_data_op_t action; + enum pm_peer_data_op action; /** * @brief Token that identifies the operation. For @ref PM_PEER_DATA_OP_DELETE actions, * this token can be disregarded. For @ref PM_PEER_DATA_OP_UPDATE actions, compare @@ -475,61 +442,61 @@ typedef struct { uint32_t token; /** @brief An error code that describes the failure. */ uint32_t error; -} pm_peer_data_update_failed_t; +}; /** @brief Standard parameters for failure events. */ -typedef struct { +struct pm_failure_evt { /** @brief The error that occurred. */ uint32_t error; -} pm_failure_evt_t; +}; /** * @brief An event from the @ref peer_manager module. * - * @details The structure contains both standard parameters and parameters that are specific to some - * events. + * @details The structure contains both standard parameters and parameters that are specific to + * some events. */ -typedef struct { +struct pm_evt { /** @brief The type of the event. */ - pm_evt_id_t evt_id; + enum pm_evt_id evt_id; /** @brief The connection that this event pertains to, or @ref BLE_CONN_HANDLE_INVALID. */ uint16_t conn_handle; /** @brief The bonded peer that this event pertains to, or @ref PM_PEER_ID_INVALID. */ uint16_t peer_id; union { /** @brief Parameters specific to the @ref PM_EVT_CONN_CONFIG_REQ event. */ - pm_conn_config_req_evt_t conn_config_req; + struct pm_conn_config_req_evt conn_config_req; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_START event. */ - pm_conn_sec_start_evt_t conn_sec_start; + struct pm_conn_sec_start_evt conn_sec_start; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_SUCCEEDED event. */ - pm_conn_secured_evt_t conn_sec_succeeded; + struct pm_conn_secured_evt conn_sec_succeeded; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_FAILED event. */ - pm_conn_secure_failed_evt_t conn_sec_failed; + struct pm_conn_secure_failed_evt conn_sec_failed; /** @brief Parameters specific to the @ref PM_EVT_CONN_SEC_PARAMS_REQ event. */ - pm_conn_sec_params_req_evt_t conn_sec_params_req; + struct pm_conn_sec_params_req_evt conn_sec_params_req; /** * @brief Parameters specific to the @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED event. */ - pm_peer_data_update_succeeded_evt_t peer_data_update_succeeded; + struct pm_peer_data_update_succeeded_evt peer_data_update_succeeded; /** @brief Parameters specific to the @ref PM_EVT_PEER_DATA_UPDATE_FAILED event. */ - pm_peer_data_update_failed_t peer_data_update_failed; + struct pm_peer_data_update_failed peer_data_update_failed; /** @brief Parameters specific to the @ref PM_EVT_PEER_DELETE_FAILED event. */ - pm_failure_evt_t peer_delete_failed; + struct pm_failure_evt peer_delete_failed; /** @brief Parameters specific to the @ref PM_EVT_PEERS_DELETE_FAILED event. */ - pm_failure_evt_t peers_delete_failed_evt; + struct pm_failure_evt peers_delete_failed_evt; /** @brief Parameters specific to the @ref PM_EVT_ERROR_UNEXPECTED event. */ - pm_failure_evt_t error_unexpected; + struct pm_failure_evt error_unexpected; #ifdef CONFIG_SOFTDEVICE_CENTRAL /** @brief Parameters specific to the @ref PM_EVT_SLAVE_SECURITY_REQ event. */ ble_gap_evt_sec_request_t slave_security_req; +#endif /** * @brief Parameters specific to the @ref PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED * event. */ -#endif - pm_failure_evt_t garbage_collection_failed; + struct pm_failure_evt garbage_collection_failed; } params; -} pm_evt_t; +}; /** * @brief Event handler for events from the @ref peer_manager module. @@ -538,7 +505,7 @@ typedef struct { * * @param[in] pm_evt The event that has occurred. */ -typedef void (*pm_evt_handler_t)(const pm_evt_t *pm_evt); +typedef void (*pm_evt_handler_t)(const struct pm_evt *pm_evt); #ifdef __cplusplus } diff --git a/lib/bluetooth/peer_manager/include/modules/gatt_cache_manager.h b/lib/bluetooth/peer_manager/include/modules/gatt_cache_manager.h index c470ab0552..538a4a33ee 100644 --- a/lib/bluetooth/peer_manager/include/modules/gatt_cache_manager.h +++ b/lib/bluetooth/peer_manager/include/modules/gatt_cache_manager.h @@ -35,9 +35,9 @@ uint32_t gcm_init(void); /** * @brief Function for dispatching SoftDevice events to the GATT Cache Manager module. * - * @param[in] p_ble_evt The SoftDevice event. + * @param[in] ble_evt The SoftDevice event. */ -void gcm_ble_evt_handler(ble_evt_t const *p_ble_evt); +void gcm_ble_evt_handler(const ble_evt_t *ble_evt); /** * @brief Function for triggering local GATT database data to be stored persistently. diff --git a/lib/bluetooth/peer_manager/include/modules/gatts_cache_manager.h b/lib/bluetooth/peer_manager/include/modules/gatts_cache_manager.h index 63b3cef088..c793e3f56f 100644 --- a/lib/bluetooth/peer_manager/include/modules/gatts_cache_manager.h +++ b/lib/bluetooth/peer_manager/include/modules/gatts_cache_manager.h @@ -115,7 +115,7 @@ uint32_t gscm_service_changed_ind_send(uint16_t conn_handle); * * @param[in] peer_id The connection to send the indication on. */ -void gscm_db_change_notification_done(pm_peer_id_t peer_id); +void gscm_db_change_notification_done(uint16_t peer_id); #ifdef __cplusplus } diff --git a/lib/bluetooth/peer_manager/include/modules/id_manager.h b/lib/bluetooth/peer_manager/include/modules/id_manager.h index 37da8b8c8e..e09637f406 100644 --- a/lib/bluetooth/peer_manager/include/modules/id_manager.h +++ b/lib/bluetooth/peer_manager/include/modules/id_manager.h @@ -19,7 +19,7 @@ #include #include #include -#include "peer_manager_internal.h" +#include #ifdef __cplusplus extern "C" { @@ -28,9 +28,9 @@ extern "C" { /** * @brief Function for dispatching SoftDevice events to the ID Manager module. * - * @param[in] p_ble_evt The SoftDevice event. + * @param[in] ble_evt The SoftDevice event. */ -void im_ble_evt_handler(ble_evt_t const *p_ble_evt); +void im_ble_evt_handler(const ble_evt_t *ble_evt); /** * @brief Function for getting the corresponding peer ID from a connection handle. @@ -39,16 +39,16 @@ void im_ble_evt_handler(ble_evt_t const *p_ble_evt); * * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved. */ -pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle); +uint16_t im_peer_id_get_by_conn_handle(uint16_t conn_handle); /** * @brief Function for getting the corresponding peer ID from a master ID (EDIV and rand). * - * @param[in] p_master_id The master ID. + * @param[in] master_id The master ID. * * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved. */ -pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t const *p_master_id); +uint16_t im_peer_id_get_by_master_id(const ble_gap_master_id_t *master_id); /** * @brief Function for getting the corresponding connection handle from a peer ID. @@ -58,64 +58,64 @@ pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t const *p_master_id) * @return The corresponding connection handle, or @ref BLE_CONN_HANDLE_INVALID if none could be * resolved. The conn_handle can refer to a recently disconnected connection. */ -uint16_t im_conn_handle_get(pm_peer_id_t peer_id); +uint16_t im_conn_handle_get(uint16_t peer_id); /** * @brief Function for comparing two master ids * @note Two invalid master IDs will not match. * - * @param[in] p_master_id1 First master id for comparison - * @param[in] p_master_id2 Second master id for comparison + * @param[in] master_id1 First master id for comparison + * @param[in] master_id2 Second master id for comparison * * @return True if the input matches, false if it does not. */ -bool im_master_ids_compare(ble_gap_master_id_t const *p_master_id1, - ble_gap_master_id_t const *p_master_id2); +bool im_master_ids_compare(const ble_gap_master_id_t *master_id1, + const ble_gap_master_id_t *master_id2); /** * @brief Function for getting the BLE address used by the peer when connecting. * * @param[in] conn_handle The connection handle. - * @param[out] p_ble_addr The BLE address used by the peer when the connection specified by + * @param[out] ble_addr The BLE address used by the peer when the connection specified by * conn_handle was established. Cannot be NULL. * * @retval NRF_SUCCESS The address was found and copied. * @retval BLE_ERROR_INVALID_CONN_HANDLE conn_handle does not refer to an active connection. */ -uint32_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t *p_ble_addr); +uint32_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t *ble_addr); /** * @brief Function for checking if a master ID is valid or invalid * - * @param[in] p_master_id The master ID. + * @param[in] master_id The master ID. * * @retval true The master id is valid. * @retval false The master id is invalid (i.e. all zeros). */ -bool im_master_id_is_valid(ble_gap_master_id_t const *p_master_id); +bool im_master_id_is_valid(const ble_gap_master_id_t *master_id); /** * @brief Function for checking if two pieces of bonding data correspond to the same peer. * - * @param[in] p_bonding_data1 The first piece of bonding data to check. - * @param[in] p_bonding_data2 The second piece of bonding data to check. + * @param[in] bonding_data1 The first piece of bonding data to check. + * @param[in] bonding_data2 The second piece of bonding data to check. * * @retval true The bonding data correspond to the same peer. * @retval false The bonding data do not correspond to the same peer. */ -bool im_is_duplicate_bonding_data(pm_peer_data_bonding_t const *p_bonding_data1, - pm_peer_data_bonding_t const *p_bonding_data2); +bool im_is_duplicate_bonding_data(const struct pm_peer_data_bonding *bonding_data1, + const struct pm_peer_data_bonding *bonding_data2); /** * @brief Function for finding if we are already bonded to a peer. * - * @param[in] p_bonding_data The bonding data to check. - * @param[in] peer_id_skip Optional peer to ignore when searching for duplicates. + * @param[in] bonding_data The bonding data to check. + * @param[in] peer_id_skip Optional peer to ignore when searching for duplicates. * * @return An existing peer ID for the peer, or PM_PEER_ID_INVALID if none was found. */ -pm_peer_id_t im_find_duplicate_bonding_data(pm_peer_data_bonding_t const *p_bonding_data, - pm_peer_id_t peer_id_skip); +uint16_t im_find_duplicate_bonding_data(const struct pm_peer_data_bonding *bonding_data, + uint16_t peer_id_skip); /** * @brief Function for reporting that a new peer ID has been allocated for a specified connection. @@ -123,7 +123,7 @@ pm_peer_id_t im_find_duplicate_bonding_data(pm_peer_data_bonding_t const *p_bond * @param[in] conn_handle The connection. * @param[in] peer_id The new peer ID. */ -void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id); +void im_new_peer_id(uint16_t conn_handle, uint16_t peer_id); /** * @brief Function for deleting all of a peer's data from flash and disassociating it from any @@ -133,7 +133,7 @@ void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id); * * @return Any error code returned by @ref pdb_peer_free. */ -uint32_t im_peer_free(pm_peer_id_t peer_id); +uint32_t im_peer_free(uint16_t peer_id); /** * @brief Function to set the local Bluetooth identity address. @@ -151,7 +151,7 @@ uint32_t im_peer_free(pm_peer_id_t peer_id); * upon being enabled. The address is a random number populated during the IC manufacturing * process and remains unchanged for the lifetime of each IC. * - * @param[in] p_addr Pointer to address structure. + * @param[in] addr Pointer to address structure. * * @retval NRF_SUCCESS Address successfully set. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the GAP address is invalid. @@ -161,7 +161,7 @@ uint32_t im_peer_free(pm_peer_id_t peer_id); * scanning, or while in a connection. * @retval NRF_ERROR_INTERNAL If an internal error occurred. */ -uint32_t im_id_addr_set(ble_gap_addr_t const *p_addr); +uint32_t im_id_addr_set(const ble_gap_addr_t *addr); /** * @brief Function to get the local Bluetooth identity address. @@ -170,41 +170,41 @@ uint32_t im_id_addr_set(ble_gap_addr_t const *p_addr); * i.e. the address type will always be either @ref BLE_GAP_ADDR_TYPE_PUBLIC or @ref * BLE_GAP_ADDR_TYPE_RANDOM_STATIC. * - * @param[out] p_addr Pointer to address structure to be filled in. + * @param[out] addr Pointer to address structure to be filled in. * * @retval NRF_SUCCESS If the address was successfully retrieved. */ -uint32_t im_id_addr_get(ble_gap_addr_t *p_addr); +uint32_t im_id_addr_get(ble_gap_addr_t *addr); /** * @brief Function to set privacy settings. * * @details Privacy settings cannot be set while advertising, scanning, or while in a connection. * - * @param[in] p_privacy_params Privacy settings. + * @param[in] privacy_params Privacy settings. * * @retval NRF_SUCCESS If privacy options were set successfully. - * @retval NRF_ERROR_NULL If @p p_privacy_params is NULL. + * @retval NRF_ERROR_NULL If @p privacy_params is NULL. * @retval NRF_ERROR_INVALID_PARAM If the address type is not valid. * @retval NRF_ERROR_BUSY If the request could not be processed at this time. * Process SoftDevice events and retry. * @retval NRF_ERROR_INVALID_STATE Privacy settings cannot be changed while BLE roles using * privacy are enabled. */ -uint32_t im_privacy_set(pm_privacy_params_t const *p_privacy_params); +uint32_t im_privacy_set(const ble_gap_privacy_params_t *privacy_params); /** * @brief Function to retrieve the current privacy settings. * * @details The privacy settings returned include the current device irk as well. * - * @param[in] p_privacy_params Privacy settings. + * @param[in] privacy_params Privacy settings. * * @retval NRF_SUCCESS Successfully retrieved privacy settings. - * @retval NRF_ERROR_NULL @c p_privacy_params is NULL. + * @retval NRF_ERROR_NULL @c privacy_params is NULL. * @retval NRF_ERROR_INTERNAL If an internal error occurred. */ -uint32_t im_privacy_get(pm_privacy_params_t *p_privacy_params); +uint32_t im_privacy_get(ble_gap_privacy_params_t *privacy_params); /** * @brief Function for resolving a resolvable address with an identity resolution key (IRK). @@ -214,19 +214,19 @@ uint32_t im_privacy_get(pm_privacy_params_t *p_privacy_params); * resolvable address based on any IRKs you have received earlier. If an address is * resolved by an IRK, the device distributing the address must also know the IRK. * - * @param[in] p_addr A random resolvable address. - * @param[in] p_irk An identity resolution key (IRK). + * @param[in] addr A random resolvable address. + * @param[in] irk An identity resolution key (IRK). * * @retval true The irk used matched the one used to create the address. * @retval false The irk used did not match the one used to create the address, or an argument was * NULL. */ -bool im_address_resolve(ble_gap_addr_t const *p_addr, ble_gap_irk_t const *p_irk); +bool im_address_resolve(const ble_gap_addr_t *addr, const ble_gap_irk_t *irk); /** * @brief Function for setting / clearing the whitelist. * - * @param p_peers The peers to whitelist. Pass NULL to clear the whitelist. + * @param peers The peers to whitelist. Pass NULL to clear the whitelist. * @param peer_cnt The number of peers to whitelist. Pass zero to clear the whitelist. * * @retval NRF_SUCCESS If the whitelist was successfully set or cleared. @@ -237,17 +237,17 @@ bool im_address_resolve(ble_gap_addr_t const *p_addr, ble_gap_irk_t const *p_irk * @retval NRF_ERROR_DATA_SIZE If @p peer_cnt is greater than * @ref BLE_GAP_WHITELIST_ADDR_MAX_COUNT. */ -uint32_t im_whitelist_set(pm_peer_id_t const *p_peers, uint32_t const peer_cnt); +uint32_t im_whitelist_set(const uint16_t *peers, const uint32_t peer_cnt); /** * @brief Retrieves the current whitelist, set by a previous call to @ref im_whitelist_set. * - * @param[out] A buffer where to copy the GAP addresses. - * @param[inout] In: the size of the @p p_addrs buffer. - * Out: the number of address copied into the buffer. - * @param[out] A buffer where to copy the IRKs. - * @param[inout] In: the size of the @p p_irks buffer. - * Out: the number of IRKs copied into the buffer. + * @param[out] addrs A buffer where to copy the GAP addresses. + * @param[inout] addr_cnt In: the size of the @p addrs buffer. + * Out: the number of addresses copied into the buffer. + * @param[out] irks A buffer where to copy the IRKs. + * @param[inout] irk_cnt In: the size of the @p irks buffer. + * Out: the number of IRKs copied into the buffer. * * @retval NRF_SUCCESS If the whitelist was successfully retrieved. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If any peer has an address which can not be used for @@ -257,11 +257,11 @@ uint32_t im_whitelist_set(pm_peer_id_t const *p_peers, uint32_t const peer_cnt); * the meanwhile. * @retval NRF_ERROR_NO_MEM If the provided buffers are too small. */ -uint32_t im_whitelist_get(ble_gap_addr_t *p_addrs, uint32_t *p_addr_cnt, ble_gap_irk_t *p_irks, - uint32_t *p_irk_cnt); +uint32_t im_whitelist_get(ble_gap_addr_t *addrs, uint32_t *addr_cnt, ble_gap_irk_t *irks, + uint32_t *irk_cnt); /** @brief Set the device identities list. */ -uint32_t im_device_identities_list_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt); +uint32_t im_device_identities_list_set(const uint16_t *peers, uint32_t peer_cnt); #ifdef __cplusplus } diff --git a/lib/bluetooth/peer_manager/include/modules/peer_data_storage.h b/lib/bluetooth/peer_manager/include/modules/peer_data_storage.h index 3aae1cd9ed..8b469fd982 100644 --- a/lib/bluetooth/peer_manager/include/modules/peer_data_storage.h +++ b/lib/bluetooth/peer_manager/include/modules/peer_data_storage.h @@ -20,7 +20,7 @@ #include #include #include -#include "peer_manager_internal.h" +#include #ifdef __cplusplus extern "C" { @@ -35,13 +35,13 @@ extern "C" { /** @brief The end of the range of record keys reserved for Peer Manager. */ #define PDS_LAST_RESERVED_RECORD_KEY (0xFFFE) -/** @brief Macro for converting a @ref pm_peer_id_t to an FDS file ID. */ +/** @brief Macro for converting a peer ID to an FDS file ID. */ #define PEER_ID_TO_FILE_ID (PDS_FIRST_RESERVED_FILE_ID) -/** @brief Macro for converting an FDS file ID to a @ref pm_peer_id_t. */ +/** @brief Macro for converting an FDS file ID to a peer ID. */ #define FILE_ID_TO_PEER_ID (-PDS_FIRST_RESERVED_FILE_ID) -/** @brief Macro for converting a @ref pm_peer_data_id_t to an FDS record ID. */ +/** @brief Macro for converting a @ref pm_peer_data_id to an FDS record ID. */ #define DATA_ID_TO_RECORD_KEY (PDS_FIRST_RESERVED_RECORD_KEY) -/** @brief Macro for converting an FDS record ID to a @ref pm_peer_data_id_t. */ +/** @brief Macro for converting an FDS record ID to a @ref pm_peer_data_id. */ #define RECORD_KEY_TO_DATA_ID (-PDS_FIRST_RESERVED_RECORD_KEY) /** @@ -56,12 +56,12 @@ uint32_t pds_init(void); /** * @brief Function for reading peer data in flash. * - * @param[in] peer_id The peer the data belongs to. - * @param[in] data_id The data to retrieve. - * @param[out] p_data The peer data. May not be @c NULL. p_data.length_words and p_data.data_id - * are ignored. p_data.p_all_data is ignored if @p p_buf_len is @c NULL. - * @param[in] p_buf_len Length of the provided buffer, in bytes. Pass @c NULL to only copy - * a pointer to the data in flash. + * @param[in] peer_id The peer the data belongs to. + * @param[in] data_id The data to retrieve. + * @param[out] data The peer data. May not be @c NULL. data.length_words and data.data_id + * are ignored. data.all_data is ignored if @p buf_len is @c NULL. + * @param[in] buf_len Length of the provided buffer, in bytes. Pass @c NULL to only copy + * a pointer to the data in flash. * * @retval NRF_SUCCESS If the operation was successful. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id or @p data_id are invalid. @@ -69,32 +69,32 @@ uint32_t pds_init(void); * @retval NRF_ERROR_DATA_SIZE If the provided buffer is too small. The data is still copied, * filling the provided buffer. */ -uint32_t pds_peer_data_read(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, - pm_peer_data_t *const p_data, uint32_t const *const p_buf_len); +uint32_t pds_peer_data_read(uint16_t peer_id, enum pm_peer_data_id data_id, + struct pm_peer_data *const data, const uint32_t *const buf_len); /** * @brief Function to prepare iterating over peer data in flash using @ref pds_peer_data_iterate. * Call this function once each time before iterating using @ref pds_peer_data_iterate. * - * @param[in] p_peer_id_iter The peer ID used for keeping track of the iteration. + * @param[in] peer_id_iter The peer ID used for keeping track of the iteration. */ -void pds_peer_data_iterate_prepare(pm_peer_id_t *p_peer_id_iter); +void pds_peer_data_iterate_prepare(uint16_t *peer_id_iter); /** * @brief Function for iterating peers' data in flash. * Always call @ref pds_peer_data_iterate_prepare before starting iterating. * - * @param[in] data_id The peer data to iterate over. - * @param[out] p_peer_id The peer the data belongs to. - * @param[out] p_data The peer data in flash. @ref p_data.p_all_data must point to a buffer - * of size @ref PM_PEER_DATA_MAX_SIZE. - * @param[in] p_peer_id_iter The peer ID used for keeping track of the iteration. + * @param[in] data_id The peer data to iterate over. + * @param[out] peer_id The peer the data belongs to. + * @param[out] data The peer data in flash. @ref data.all_data must point to a buffer + * of size @ref PM_PEER_DATA_MAX_SIZE. + * @param[in] peer_id_iter The peer ID used for keeping track of the iteration. * * @retval true If the operation was successful. * @retval false If the data was not found in flash, or another error occurred. */ -bool pds_peer_data_iterate(pm_peer_data_id_t data_id, pm_peer_id_t *const p_peer_id, - pm_peer_data_flash_t *const p_data, pm_peer_id_t *p_peer_id_iter); +bool pds_peer_data_iterate(enum pm_peer_data_id data_id, uint16_t *const peer_id, + struct pm_peer_data_const *const data, uint16_t *peer_id_iter); /** * @brief Function for storing peer data in flash. If the same piece of data already exists for the @@ -102,21 +102,21 @@ bool pds_peer_data_iterate(pm_peer_data_id_t data_id, pm_peer_id_t *const p_peer * Expect a @ref PM_EVT_PEER_DATA_UPDATE_SUCCEEDED or @ref PM_EVT_PEER_DATA_UPDATE_FAILED * event. * - * @param[in] peer_id The peer the data belongs to. - * @param[in] p_peer_data The peer data. May not be @c NULL. - * @param[out] p_store_token A token identifying this particular store operation. The token can be - * used to identify events pertaining to this operation. Pass @p NULL - * if not used. + * @param[in] peer_id The peer the data belongs to. + * @param[in] peer_data The peer data. May not be @c NULL. + * @param[out] store_token A token identifying this particular store operation. The token can be + * used to identify events pertaining to this operation. Pass @p NULL + * if not used. * * @retval NRF_SUCCESS If the operation was initiated successfully. - * @retval NRF_ERROR_INVALID_PARAM If @p peer_id or the data ID in @p_peer_data are invalid. - * @retval NRF_ERROR_INVALID_ADDR If @p p_peer_data is not word-aligned. + * @retval NRF_ERROR_INVALID_PARAM If @p peer_id or the data ID in @p peer_data are invalid. + * @retval NRF_ERROR_INVALID_ADDR If @p peer_data is not word-aligned. * @retval NRF_ERROR_RESOURCES If no space is available in flash. * @retval NRF_ERROR_BUSY If the flash filesystem was busy. * @retval NRF_ERROR_INTERNAL If an unexpected error occurred. */ -uint32_t pds_peer_data_store(pm_peer_id_t peer_id, pm_peer_data_const_t const *p_peer_data, - pm_store_token_t *p_store_token); +uint32_t pds_peer_data_store(uint16_t peer_id, const struct pm_peer_data_const *peer_data, + uint32_t *store_token); /** * @brief Function for deleting peer data in flash. This operation is asynchronous. @@ -132,14 +132,14 @@ uint32_t pds_peer_data_store(pm_peer_id_t peer_id, pm_peer_data_const_t const *p * @retval NRF_ERROR_BUSY If the flash filesystem was busy. * @retval NRF_ERROR_INTERNAL If an unexpected error occurred. */ -uint32_t pds_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id); +uint32_t pds_peer_data_delete(uint16_t peer_id, enum pm_peer_data_id data_id); /** * @brief Function for claiming an unused peer ID. * * @retval PM_PEER_ID_INVALID If no peer ID was available. */ -pm_peer_id_t pds_peer_id_allocate(void); +uint16_t pds_peer_id_allocate(void); /** * @brief Function for freeing a peer ID and deleting all data associated with it in flash. @@ -149,7 +149,7 @@ pm_peer_id_t pds_peer_id_allocate(void); * @retval NRF_SUCCESS The operation was initiated successfully. * @retval NRF_ERROR_INVALID_PARAM If @p peer_id is invalid. */ -uint32_t pds_peer_id_free(pm_peer_id_t peer_id); +uint32_t pds_peer_id_free(uint16_t peer_id); /** * @brief Function for finding out whether a peer ID is in use. @@ -159,7 +159,7 @@ uint32_t pds_peer_id_free(pm_peer_id_t peer_id); * @retval true @p peer_id is in use. * @retval false @p peer_id is free. */ -bool pds_peer_id_is_allocated(pm_peer_id_t peer_id); +bool pds_peer_id_is_allocated(uint16_t peer_id); /** * @brief Function for finding out whether a peer ID is marked for deletion. @@ -169,7 +169,7 @@ bool pds_peer_id_is_allocated(pm_peer_id_t peer_id); * @retval true @p peer_id is marked for deletion. * @retval false @p peer_id is not marked for deletion. */ -bool pds_peer_id_is_deleted(pm_peer_id_t peer_id); +bool pds_peer_id_is_deleted(uint16_t peer_id); /** * @brief Function for getting the next peer ID in the sequence of all used peer IDs. Can be @@ -184,7 +184,7 @@ bool pds_peer_id_is_deleted(pm_peer_id_t peer_id); * @retval PM_PEER_ID_INVALID If @p prev_peer_id is the last ordinary peer ID or the * module is not initialized. */ -pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id); +uint16_t pds_next_peer_id_get(uint16_t prev_peer_id); /** * @brief Function for getting the next peer ID in the sequence of all peer IDs pending deletion. @@ -200,7 +200,7 @@ pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id); * @retval PM_PEER_ID_INVALID if prev_peer_id was the last ordinary peer ID or the module * is not initialized. */ -pm_peer_id_t pds_next_deleted_peer_id_get(pm_peer_id_t prev_peer_id); +uint16_t pds_next_deleted_peer_id_get(uint16_t prev_peer_id); /** * @brief Function for querying the number of valid peer IDs available. I.E the number of peers diff --git a/lib/bluetooth/peer_manager/include/modules/peer_database.h b/lib/bluetooth/peer_manager/include/modules/peer_database.h index 89cf71ae16..bac52f8869 100644 --- a/lib/bluetooth/peer_manager/include/modules/peer_database.h +++ b/lib/bluetooth/peer_manager/include/modules/peer_database.h @@ -19,7 +19,7 @@ #include #include #include -#include "peer_manager_internal.h" +#include #ifdef __cplusplus extern "C" { @@ -29,7 +29,7 @@ extern "C" { * @brief The size (in bytes) of each block in the internal buffer accessible via * @ref pdb_write_buf_get. */ -#define PDB_WRITE_BUF_SIZE (sizeof(pm_peer_data_bonding_t)) +#define PDB_WRITE_BUF_SIZE (sizeof(struct pm_peer_data_bonding)) /** * @brief Function for creating a peer ID value from a connection handle. @@ -43,7 +43,7 @@ extern "C" { * @retval NRF_SUCCESS Temporary peer ID written to @p peer_id. * @retval NRF_ERROR_INVALID_PARAM Connection handle was invalid. */ -static inline uint32_t pdb_temp_peer_id_get(uint16_t conn_handle, pm_peer_id_t *peer_id) +static inline uint32_t pdb_temp_peer_id_get(uint16_t conn_handle, uint16_t *peer_id) { const int idx = nrf_sdh_ble_idx_get(conn_handle); @@ -74,7 +74,7 @@ uint32_t pdb_init(void); * successfully. * @retval NRF_ERROR_INVALID_PARAM Peer ID was invalid. */ -uint32_t pdb_peer_free(pm_peer_id_t peer_id); +uint32_t pdb_peer_free(uint16_t peer_id); /** * @brief Function for retrieving pointers to a write buffer for peer data. @@ -93,13 +93,14 @@ uint32_t pdb_peer_free(pm_peer_id_t peer_id); * will be copied. If n_bufs was increased since last time, this function might return @ref * NRF_ERROR_BUSY. In that case, the buffer is automatically released. * - * @param[in] peer_id ID of the peer to get a write buffer for. If @p peer_id is larger than - * @ref PM_PEER_ID_N_AVAILABLE_IDS, it is interpreted as pertaining to - * the connection that have been assigned idx (peer_id - - * PM_PEER_ID_N_AVAILABLE_IDS) using @ref nrf_sdh_ble_idx_get. See @ref pdb_temp_peer_id_get. - * @param[in] data_id The piece of data to get. - * @param[in] n_bufs Number of contiguous buffers needed. - * @param[out] p_peer_data Pointers to mutable peer data. + * @param[in] peer_id ID of the peer to get a write buffer for. If @p peer_id is larger than + * @ref PM_PEER_ID_N_AVAILABLE_IDS, it is interpreted as pertaining to + * the connection that have been assigned idx + * (peer_id - PM_PEER_ID_N_AVAILABLE_IDS) using @ref nrf_sdh_ble_idx_get. + * See @ref pdb_temp_peer_id_get. + * @param[in] data_id The piece of data to get. + * @param[in] n_bufs Number of contiguous buffers needed. + * @param[out] peer_data Pointers to mutable peer data. * * @retval NRF_SUCCESS Data retrieved successfully. * @retval NRF_ERROR_INVALID_PARAM @p data_id was invalid, or @p n_bufs was 0 or more than the @@ -107,12 +108,12 @@ uint32_t pdb_peer_free(pm_peer_id_t peer_id); * @retval NRF_ERROR_FORBIDDEN n_bufs was higher or lower than the existing buffer. If needed, * release the existing buffer with @ref pdb_write_buf_release, and * call this function again. - * @retval NRF_ERROR_NULL p_peer_data was NULL. + * @retval NRF_ERROR_NULL @p peer_data was NULL. * @retval NRF_ERROR_BUSY Not enough buffer(s) available. * @retval NRF_ERROR_INTERNAL Unexpected internal error. */ -uint32_t pdb_write_buf_get(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, uint32_t n_bufs, - pm_peer_data_t *p_peer_data); +uint32_t pdb_write_buf_get(uint16_t peer_id, enum pm_peer_data_id data_id, uint32_t n_bufs, + struct pm_peer_data *peer_data); /** * @brief Function for freeing a write buffer allocated with @ref pdb_write_buf_get. @@ -126,7 +127,7 @@ uint32_t pdb_write_buf_get(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, uint * @retval NRF_SUCCESS Successfully released buffer. * @retval NRF_ERROR_NOT_FOUND No buffer was allocated for this peer ID/data ID pair. */ -uint32_t pdb_write_buf_release(pm_peer_id_t peer_id, pm_peer_data_id_t data_id); +uint32_t pdb_write_buf_release(uint16_t peer_id, enum pm_peer_data_id data_id); /** * @brief Function for writing data into persistent storage. Writing happens asynchronously. @@ -147,8 +148,7 @@ uint32_t pdb_write_buf_release(pm_peer_id_t peer_id, pm_peer_data_id_t data_id); * @retval NRF_ERROR_NOT_FOUND No buffer has been allocated for this peer ID/data ID pair. * @retval NRF_ERROR_INTERNAL Unexpected internal error. */ -uint32_t pdb_write_buf_store(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, - pm_peer_id_t new_peer_id); +uint32_t pdb_write_buf_store(uint16_t peer_id, enum pm_peer_data_id data_id, uint16_t new_peer_id); #ifdef __cplusplus } diff --git a/lib/bluetooth/peer_manager/include/modules/peer_id.h b/lib/bluetooth/peer_manager/include/modules/peer_id.h index 9be571af57..109168429a 100644 --- a/lib/bluetooth/peer_manager/include/modules/peer_id.h +++ b/lib/bluetooth/peer_manager/include/modules/peer_id.h @@ -35,7 +35,7 @@ void peer_id_init(void); * @return The allocated peer ID. * @retval PM_PEER_ID_INVALID If no peer ID could be allocated or module is not initialized. */ -pm_peer_id_t peer_id_allocate(pm_peer_id_t peer_id); +uint16_t peer_id_allocate(uint16_t peer_id); /** * @brief Function for marking a peer ID for deletion. @@ -46,7 +46,7 @@ pm_peer_id_t peer_id_allocate(pm_peer_id_t peer_id); * @retval false Peer ID already marked for deletion, peer_id was PM_PEER_ID_INVALID, or module is * not initialized. */ -bool peer_id_delete(pm_peer_id_t peer_id); +bool peer_id_delete(uint16_t peer_id); /** * @brief Function for freeing a peer ID and clearing all data associated with it in persistent @@ -54,7 +54,7 @@ bool peer_id_delete(pm_peer_id_t peer_id); * * @param[in] peer_id Peer ID to free. */ -void peer_id_free(pm_peer_id_t peer_id); +void peer_id_free(uint16_t peer_id); /** * @brief Function for finding out whether a peer ID is marked for deletion. @@ -64,7 +64,7 @@ void peer_id_free(pm_peer_id_t peer_id); * @retval true peer_id is in marked for deletion. * @retval false peer_id is not marked for deletion, or the module is not initialized. */ -bool peer_id_is_deleted(pm_peer_id_t peer_id); +bool peer_id_is_deleted(uint16_t peer_id); /** * @brief Function for finding out whether a peer ID is in use. @@ -74,7 +74,7 @@ bool peer_id_is_deleted(pm_peer_id_t peer_id); * @retval true peer_id is in use. * @retval false peer_id is free, or the module is not initialized. */ -bool peer_id_is_allocated(pm_peer_id_t peer_id); +bool peer_id_is_allocated(uint16_t peer_id); /** * @brief Function for getting the next peer ID in the sequence of all used peer IDs. Can be @@ -90,7 +90,7 @@ bool peer_id_is_allocated(pm_peer_id_t peer_id); * @retval PM_PEER_ID_INVALID if prev_peer_id was the last ordinary peer ID or the module is * not initialized. */ -pm_peer_id_t peer_id_get_next_used(pm_peer_id_t prev_peer_id); +uint16_t peer_id_get_next_used(uint16_t prev_peer_id); /** * @brief Function for getting the next peer ID in the sequence of all peer IDs marked for deletion. @@ -106,7 +106,7 @@ pm_peer_id_t peer_id_get_next_used(pm_peer_id_t prev_peer_id); * @retval PM_PEER_ID_INVALID if prev_peer_id was the last ordinary peer ID or the module is * not initialized. */ -pm_peer_id_t peer_id_get_next_deleted(pm_peer_id_t prev_peer_id); +uint16_t peer_id_get_next_deleted(uint16_t prev_peer_id); /** * @brief Function for querying the number of valid peer IDs available. I.E the number of peers diff --git a/lib/bluetooth/peer_manager/include/modules/peer_manager_internal.h b/lib/bluetooth/peer_manager/include/modules/peer_manager_internal.h index e7dc4d7ea4..517285fa7c 100644 --- a/lib/bluetooth/peer_manager/include/modules/peer_manager_internal.h +++ b/lib/bluetooth/peer_manager/include/modules/peer_manager_internal.h @@ -25,7 +25,7 @@ extern "C" { #endif -/** @brief Max size for @ref pm_peer_data_local_gatt_db_t.data */ +/** @brief Max size of the data member in @ref pm_peer_data_local_gatt_db. */ #define PM_PEER_DATA_LOCAL_GATT_DB_MAX_SIZE 128 #define PM_PEER_DATA_MAX_SIZE PM_PEER_DATA_LOCAL_GATT_DB_MAX_SIZE @@ -34,18 +34,18 @@ extern "C" { * * @note This type is deprecated. */ -typedef struct { +struct pm_peer_data { /** @brief The length of the data in words. */ uint16_t length_words; /** * @brief ID that specifies the type of data (defines which member of the union is * used). */ - pm_peer_data_id_t data_id; + enum pm_peer_data_id data_id; /** @brief The data. */ union { /** @brief The exchanged bond information in addition to metadata of the bonding. */ - pm_peer_data_bonding_t *p_bonding_data; + struct pm_peer_data_bonding *bonding_data; /** * @brief A value locally assigned to this peer. Its * interpretation is up to the user. The rank is not set @@ -53,77 +53,70 @@ typedef struct { * the user using either @ref pm_peer_rank_highest or a @ref * PM_PEER_DATA_FUNCTIONS function. */ - uint32_t *p_peer_rank; + uint32_t *peer_rank; /** @brief Value of peer's Central Address Resolution characteristic. */ - uint32_t *p_central_addr_res; + uint32_t *central_addr_res; /** @brief Whether a service changed indication should be sent to the peer. */ - bool *p_service_changed_pending; + bool *service_changed_pending; /** @brief Persistent information pertaining to a peer GATT client. */ - pm_peer_data_local_gatt_db_t *p_local_gatt_db; + struct pm_peer_data_local_gatt_db *local_gatt_db; /** @brief Persistent information pertaining to a peer GATT server. */ - ble_gatt_db_srv_t *p_remote_gatt_db; + struct ble_gatt_db_srv *remote_gatt_db; /** * @brief Arbitrary data to associate with the peer. This data can be freely used * by the application. */ - uint8_t *p_application_data; + uint8_t *application_data; /** * @brief Generic access pointer to the data. It is used only to * handle the data without regard to type. */ - void *p_all_data; + void *all_data; }; -} pm_peer_data_t; +}; /** - * @brief Immutable version of @ref pm_peer_data_t. + * @brief Immutable version of @ref pm_peer_data. * * @note This type is deprecated. */ -typedef struct { +struct pm_peer_data_const { /** @brief The length of the data in words. */ uint16_t length_words; /** * @brief ID that specifies the type of data (defines which member of the union is * used). */ - pm_peer_data_id_t data_id; + enum pm_peer_data_id data_id; /** @brief The data. */ union { - /** @brief Immutable @ref pm_peer_data_t::p_bonding_data. */ - pm_peer_data_bonding_t const *p_bonding_data; - /** @brief Immutable @ref pm_peer_data_t::p_peer_rank. */ - uint32_t const *p_peer_rank; - /** @brief Immutable @ref pm_peer_data_t::p_central_addr_res. */ - uint32_t const *p_central_addr_res; - /** @brief Immutable @ref pm_peer_data_t::p_service_changed_pending. */ - bool const *p_service_changed_pending; - /** @brief Immutable @ref pm_peer_data_t::p_local_gatt_db. */ - pm_peer_data_local_gatt_db_t const *p_local_gatt_db; - /** @brief Immutable @ref pm_peer_data_t::p_remote_gatt_db. */ - ble_gatt_db_srv_t const *p_remote_gatt_db; - /** @brief Immutable @ref pm_peer_data_t::p_application_data. */ - uint8_t const *p_application_data; - /** @brief Immutable @ref pm_peer_data_t::p_all_data. */ - void const *p_all_data; + /** @brief Immutable @ref pm_peer_data::bonding_data. */ + const struct pm_peer_data_bonding *bonding_data; + /** @brief Immutable @ref pm_peer_data::peer_rank. */ + const uint32_t *peer_rank; + /** @brief Immutable @ref pm_peer_data::central_addr_res. */ + const uint32_t *central_addr_res; + /** @brief Immutable @ref pm_peer_data::service_changed_pending. */ + const bool *service_changed_pending; + /** @brief Immutable @ref pm_peer_data::local_gatt_db. */ + const struct pm_peer_data_local_gatt_db *local_gatt_db; + /** @brief Immutable @ref pm_peer_data::remote_gatt_db. */ + const struct ble_gatt_db_srv *remote_gatt_db; + /** @brief Immutable @ref pm_peer_data::application_data. */ + const uint8_t *application_data; + /** @brief Immutable @ref pm_peer_data::all_data. */ + const void *all_data; }; -} pm_peer_data_const_t; - -/** - * @brief Version of @ref pm_peer_data_t that reflects the structure of peer data in flash. - * - * @note This type is deprecated. - */ -typedef pm_peer_data_const_t pm_peer_data_flash_t; +}; /** * @brief Event handler for events from the @ref peer_manager module. * * @sa pm_register * - * @param[in] p_event The event that has occurred. + * @param[in] event The event that has occurred. */ -typedef void (*pm_evt_handler_internal_t)(pm_evt_t *p_event); +typedef void (*pm_evt_handler_internal_t)(struct pm_evt *event); /** @brief Macro for showing that a variable is unused. */ #define UNUSED_VARIABLE(X) ((void)(X)) @@ -145,7 +138,7 @@ typedef void (*pm_evt_handler_internal_t)(pm_evt_t *p_event); * * @return The number of words that the data takes in flash. */ -#define PM_BONDING_DATA_N_WORDS() BYTES_TO_WORDS(sizeof(pm_peer_data_bonding_t)) +#define PM_BONDING_DATA_N_WORDS() BYTES_TO_WORDS(sizeof(struct pm_peer_data_bonding)) /** * @brief Macro for calculating the flash size of service changed pending state. @@ -181,7 +174,7 @@ typedef void (*pm_evt_handler_internal_t)(pm_evt_t *p_event); * @return The number of words that the data takes in flash. */ #define PM_REMOTE_DB_N_WORDS(service_count) \ - BYTES_TO_WORDS(sizeof(ble_gatt_db_srv_t) * (service_count)) + BYTES_TO_WORDS(sizeof(struct ble_gatt_db_srv) * (service_count)) /** * @brief Macro for calculating the number of services that can be stored in a region of n words. @@ -190,7 +183,8 @@ typedef void (*pm_evt_handler_internal_t)(pm_evt_t *p_event); * * @return The number of services that can be stored in a region of n words. */ -#define PM_REMOTE_DB_N_SERVICES(n_words) (((n_words)*BYTES_PER_WORD) / sizeof(ble_gatt_db_srv_t)) +#define PM_REMOTE_DB_N_SERVICES(n_words) \ + (((n_words)*BYTES_PER_WORD) / sizeof(struct ble_gatt_db_srv)) /** * @brief Function for calculating the flash size of the usage index. @@ -199,20 +193,6 @@ typedef void (*pm_evt_handler_internal_t)(pm_evt_t *p_event); */ #define PM_USAGE_INDEX_N_WORDS() BYTES_TO_WORDS(sizeof(uint32_t)) -#ifdef NRF_PM_DEBUG - -#define NRF_PM_DEBUG_CHECK(condition) \ - if (!(condition)) { \ - __asm("bkpt #0"); \ - } - -#else - -/* Prevent "variable set but never used" compiler warnings. */ -#define NRF_PM_DEBUG_CHECK(condition) (void)(condition) - -#endif - #ifdef __cplusplus } #endif diff --git a/lib/bluetooth/peer_manager/include/modules/pm_buffer.h b/lib/bluetooth/peer_manager/include/modules/pm_buffer.h index 846c85c9a7..6571646c4a 100644 --- a/lib/bluetooth/peer_manager/include/modules/pm_buffer.h +++ b/lib/bluetooth/peer_manager/include/modules/pm_buffer.h @@ -26,41 +26,41 @@ extern "C" { /** * @brief Convenience macro for declaring memory and initializing a buffer instance. * - * @param[out] p_buffer The buffer instance to initialize. + * @param[out] buffer The buffer instance to initialize. * @param[in] n_blocks The desired number of blocks in the buffer. * @param[in] block_size The desired block size of the buffer. * @param[out] err_code The return code from @ref pm_buffer_init. */ -#define PM_BUFFER_INIT(p_buffer, n_blocks, block_size, err_code) \ +#define PM_BUFFER_INIT(buffer, n_blocks, block_size, err_code) \ do { \ __ALIGN(4) static uint8_t buffer_memory[(n_blocks) * (block_size)]; \ static atomic_t mutex_memory[(n_blocks - 1) / (sizeof(atomic_t) * 8) + 1]; \ - err_code = pm_buffer_init((p_buffer), buffer_memory, (n_blocks) * (block_size), \ + err_code = pm_buffer_init((buffer), buffer_memory, (n_blocks) * (block_size), \ mutex_memory, (n_blocks), (block_size)); \ } while (0) -typedef struct { +struct pm_buffer { /** * @brief The storage for all buffer entries. The size of the buffer must be * n_blocks*block_size. */ - uint8_t *p_memory; + uint8_t *memory; /** @brief A mutex group with one mutex for each buffer entry. */ - atomic_t *p_mutex; + atomic_t *mutex; /** @brief The number of allocatable blocks in the buffer. */ uint32_t n_blocks; /** @brief The size of each block in the buffer. */ uint32_t block_size; -} pm_buffer_t; +}; /** * @brief Function for initializing a buffer instance. * - * @param[out] p_buffer The buffer instance to initialize. - * @param[in] p_buffer_memory The memory this buffer will use. - * @param[in] buffer_memory_size The size of p_buffer_memory. This must be at least + * @param[out] buffer The buffer instance to initialize. + * @param[in] buffer_memory The memory this buffer will use. + * @param[in] buffer_memory_size The size of buffer_memory. This must be at least * n_blocks*block_size. - * @param[in] p_mutex_memory The memory for the mutexes. This must be at least + * @param[in] mutex_memory The memory for the mutexes. This must be at least * @ref NRF_ATFLAGS_ARRAY_LEN(n_blocks). * @param[in] n_blocks The number of blocks in the buffer. * @param[in] block_size The size of each block. @@ -68,39 +68,39 @@ typedef struct { * @retval NRF_SUCCESS Successfully initialized buffer instance. * @retval NRF_ERROR_INVALID_PARAM A parameter was 0 or NULL or a size was too small. */ -uint32_t pm_buffer_init(pm_buffer_t *p_buffer, uint8_t *p_buffer_memory, - uint32_t buffer_memory_size, atomic_t *p_mutex_memory, - uint32_t n_blocks, uint32_t block_size); +uint32_t pm_buffer_init(struct pm_buffer *buffer, uint8_t *buffer_memory, + uint32_t buffer_memory_size, atomic_t *mutex_memory, + uint32_t n_blocks, uint32_t block_size); /** * @brief Function for acquiring a buffer block in a buffer. * - * @param[in] p_buffer The buffer instance acquire from. + * @param[in] buffer The buffer instance acquire from. * @param[in] n_blocks The number of contiguous blocks to acquire. * * @return The id of the acquired block, if successful. * @retval PM_BUFFER_INVALID_ID If unsuccessful. */ -uint8_t pm_buffer_block_acquire(pm_buffer_t *p_buffer, uint32_t n_blocks); +uint8_t pm_buffer_block_acquire(struct pm_buffer *buffer, uint32_t n_blocks); /** * @brief Function for getting a pointer to a specific buffer block. * - * @param[in] p_buffer The buffer instance get from. - * @param[in] id The id of the buffer to get the pointer for. + * @param[in] buffer The buffer instance get from. + * @param[in] id The id of the buffer to get the pointer for. * * @return A pointer to the buffer for the specified id, if the id is valid. * @retval NULL If the id is invalid. */ -uint8_t *pm_buffer_ptr_get(pm_buffer_t *p_buffer, uint8_t id); +uint8_t *pm_buffer_ptr_get(struct pm_buffer *buffer, uint8_t id); /** * @brief Function for releasing a buffer block. * - * @param[in] p_buffer The buffer instance containing the block to release. - * @param[in] id The id of the block to release. + * @param[in] buffer The buffer instance containing the block to release. + * @param[in] id The id of the block to release. */ -void pm_buffer_release(pm_buffer_t *p_buffer, uint8_t id); +void pm_buffer_release(struct pm_buffer *buffer, uint8_t id); #ifdef __cplusplus } diff --git a/lib/bluetooth/peer_manager/include/modules/security_dispatcher.h b/lib/bluetooth/peer_manager/include/modules/security_dispatcher.h index 7b05d1ae95..bb0af4a5ea 100644 --- a/lib/bluetooth/peer_manager/include/modules/security_dispatcher.h +++ b/lib/bluetooth/peer_manager/include/modules/security_dispatcher.h @@ -38,20 +38,20 @@ uint32_t smd_init(void); * * @param[in] ble_evt The SoftDevice event. */ -void smd_ble_evt_handler(ble_evt_t const *ble_evt); +void smd_ble_evt_handler(const ble_evt_t *ble_evt); /** * @brief Function for providing security configuration for a link. * * @details This function is optional, and must be called in reply to a @ref * PM_EVT_CONN_SEC_CONFIG_REQ event, before the Peer Manager event handler returns. If it - * is not called in time, a default configuration is used. See @ref pm_conn_sec_config_t + * is not called in time, a default configuration is used. See @ref pm_conn_sec_config * for the value of the default. * - * @param[in] conn_handle The connection to set the configuration for. - * @param[in] p_conn_sec_config The configuration. + * @param[in] conn_handle The connection to set the configuration for. + * @param[in] conn_sec_config The configuration. */ -void smd_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_conn_sec_config); +void smd_conn_sec_config_reply(uint16_t conn_handle, struct pm_conn_sec_config *conn_sec_config); /** * @brief Function for providing pairing and bonding parameters to use for the current pairing @@ -61,11 +61,11 @@ void smd_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_con * BLE_ERROR_INVALID_CONN_HANDLE, or @ref NRF_ERROR_RESOURCES, this function can be called * again after corrective action. * - * @note To reject a request, call this function with NULL p_sec_params. + * @note To reject a request, call this function with NULL sec_params. * * @param[in] conn_handle The connection handle of the connection the pairing is happening on. - * @param[in] p_sec_params The security parameters to use for this link. - * @param[in] p_public_key A pointer to the public key to use if using LESC, or NULL. + * @param[in] sec_params The security parameters to use for this link. + * @param[in] public_key A pointer to the public key to use if using LESC, or NULL. * * @retval NRF_SUCCESS Success. * @retval NRF_ERROR_INVALID_STATE No parameters have been requested on that conn_handle, or @@ -78,8 +78,8 @@ void smd_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_con * @retval NRF_ERROR_BUSY No write buffer. Reattempt later. * @retval NRF_ERROR_INTERNAL A fatal error occurred. */ -uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, - ble_gap_lesc_p256_pk_t *p_public_key); +uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, + ble_gap_lesc_p256_pk_t *public_key); /** * @brief Function for initiating security on the link, with the specified parameters. @@ -91,14 +91,14 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para * available, pairing will be started. * * @param[in] conn_handle Handle of the connection to initiate pairing on. - * @param[in] p_sec_params The security parameters to use for this link. As a central, this can + * @param[in] sec_params The security parameters to use for this link. As a central, this can * be NULL to reject a slave security request. * @param[in] force_repairing Whether to force a pairing procedure to happen regardless of whether * an encryption key already exists. This argument is only relevant for * the central role. Recommended value: false * * @retval NRF_SUCCESS Success. - * @retval NRF_ERROR_NULL p_sec_params was NULL (peripheral only). + * @retval NRF_ERROR_NULL @p sec_params was NULL (peripheral only). * @retval NRF_ERROR_INVALID_STATE A security procedure is already in progress on the link, * or the link is disconnecting. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters (not including @@ -111,7 +111,7 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle. * @retval NRF_ERROR_INTERNAL No more available peer IDs. */ -uint32_t smd_link_secure(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, +uint32_t smd_link_secure(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, bool force_repairing); #ifdef __cplusplus diff --git a/lib/bluetooth/peer_manager/include/modules/security_manager.h b/lib/bluetooth/peer_manager/include/modules/security_manager.h index 8154b67729..6d8fbdb293 100644 --- a/lib/bluetooth/peer_manager/include/modules/security_manager.h +++ b/lib/bluetooth/peer_manager/include/modules/security_manager.h @@ -19,7 +19,6 @@ #include #include #include -#include "security_dispatcher.h" #ifdef __cplusplus extern "C" { @@ -38,35 +37,35 @@ uint32_t sm_init(void); * * @param[in] ble_evt The SoftDevice event. */ -void sm_ble_evt_handler(ble_evt_t const *ble_evt); +void sm_ble_evt_handler(const ble_evt_t *ble_evt); /** * @brief Function for providing pairing and bonding parameters to use for pairing procedures. * * @details Until this is called, all bonding procedures initiated by the peer will be rejected. - * This function can be called multiple times, even with NULL p_sec_params, in which case + * This function can be called multiple times, even with NULL sec_params, in which case * it will go back to rejecting all procedures. * - * @param[in] p_sec_params The security parameters to use for this link. Can be NULL to reject - * all pairing procedures. + * @param[in] sec_params The security parameters to use for this link. Can be NULL to reject + * all pairing procedures. * * @retval NRF_SUCCESS Success. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters. */ -uint32_t sm_sec_params_set(ble_gap_sec_params_t *p_sec_params); +uint32_t sm_sec_params_set(ble_gap_sec_params_t *sec_params); /** * @brief Function for providing security configuration for a link. * * @details This function is optional, and must be called in reply to a @ref * PM_EVT_CONN_SEC_CONFIG_REQ event, before the Peer Manager event handler returns. If it - * is not called in time, a default configuration is used. See @ref pm_conn_sec_config_t + * is not called in time, a default configuration is used. See @ref pm_conn_sec_config * for the value of the default. * - * @param[in] conn_handle The connection to set the configuration for. - * @param[in] p_conn_sec_config The configuration. + * @param[in] conn_handle The connection to set the configuration for. + * @param[in] conn_sec_config The configuration. */ -void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_conn_sec_config); +void sm_conn_sec_config_reply(uint16_t conn_handle, struct pm_conn_sec_config *conn_sec_config); /** * @brief Function for providing security parameters for a link. @@ -74,19 +73,19 @@ void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_conn * @details This function is optional, and must be called in reply to a @ref * PM_EVT_CONN_SEC_PARAMS_REQ event, before the Security Manager event handler returns. If * it is not called in time, the parameters given in @ref sm_sec_params_set are used. See - * @ref pm_conn_sec_config_t for the value of the default. + * @ref pm_conn_sec_config for the value of the default. * * @param[in] conn_handle The connection to set the parameters for. - * @param[in] p_sec_params The parameters. If NULL, the security procedure is rejected. - * @param[in] p_context The context found in the request event that this function replies to. + * @param[in] sec_params The parameters. If NULL, the security procedure is rejected. + * @param[in] context The context found in the request event that this function replies to. * * @retval NRF_SUCCESS Successful reply. - * @retval NRF_ERROR_NULL p_context was null. - * @retval NRF_ERROR_INVALID_PARAM Value of p_sec_params was invalid. + * @retval NRF_ERROR_NULL @p context was null. + * @retval NRF_ERROR_INVALID_PARAM Value of @p sec_params was invalid. * @retval NRF_ERROR_INVALID_STATE This module is not initialized. */ -uint32_t sm_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, - void const *p_context); +uint32_t sm_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, + const void *context); /** * @brief Experimental function for specifying the public key to use for LESC operations. @@ -97,37 +96,37 @@ uint32_t sm_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_p * * @note The key must continue to reside in application memory as it is not copied by Peer Manager. * - * @param[in] p_public_key The public key to use for all subsequent LESC operations. + * @param[in] public_key The public key to use for all subsequent LESC operations. * * @retval NRF_SUCCESS Pairing initiated successfully. * @retval NRF_ERROR_INVALID_STATE This module is not initialized. */ -uint32_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *p_public_key); +uint32_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *public_key); /** * @brief Function for getting the security status of a connection. * - * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice. - * @param[out] p_conn_sec_status Security status of the link. + * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice. + * @param[out] conn_sec_status Security status of the link. * * @retval NRF_SUCCESS If pairing was initiated successfully. * @retval BLE_ERROR_INVALID_CONN_HANDLE If the connection handle is invalid. - * @retval NRF_ERROR_NULL If @p p_conn_sec_status was NULL. + * @retval NRF_ERROR_NULL If @p conn_sec_status was NULL. */ -uint32_t sm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t *p_conn_sec_status); +uint32_t sm_conn_sec_status_get(uint16_t conn_handle, struct pm_conn_sec_status *conn_sec_status); /** * @brief Function for comparing the security status of a connection against a baseline. * - * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice. - * @param[out] p_sec_status_req Target baseline security status to compare against. + * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice. + * @param[out] sec_status_req Target baseline security status to compare against. * * @retval true If the security status of the connection matches or exceeds the baseline on all * points. * @retval false If the security status of the connection does not fulfill the baseline, or could * not be retrieved. */ -bool sm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t *p_sec_status_req); +bool sm_sec_is_sufficient(uint16_t conn_handle, struct pm_conn_sec_status *sec_status_req); /** * @brief Function for initiating security on the link, with the specified parameters. diff --git a/lib/bluetooth/peer_manager/include/nrf_strerror.h b/lib/bluetooth/peer_manager/include/nrf_strerror.h index de6ecaf797..ddd2395e72 100644 --- a/lib/bluetooth/peer_manager/include/nrf_strerror.h +++ b/lib/bluetooth/peer_manager/include/nrf_strerror.h @@ -32,7 +32,7 @@ extern "C" { * If the string is not found, * it returns a simple string that says that the error is unknown. */ -char const *nrf_strerror_get(uint32_t code); +const char *nrf_strerror_get(uint32_t code); /** * @brief Function for finding a printable error string. @@ -44,7 +44,7 @@ char const *nrf_strerror_get(uint32_t code); * @return Pointer to the printable string. * If the string is not found, NULL is returned. */ -char const *nrf_strerror_find(uint32_t code); +const char *nrf_strerror_find(uint32_t code); /** @} */ diff --git a/lib/bluetooth/peer_manager/include/sdk_macros.h b/lib/bluetooth/peer_manager/include/sdk_macros.h deleted file mode 100644 index 5631d3e6fc..0000000000 --- a/lib/bluetooth/peer_manager/include/sdk_macros.h +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2015-2025 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -/** - * @file - * - * @defgroup sdk_common_macros SDK Common Header - * @ingroup app_common - * @brief Macros for parameter checking and similar tasks - * @{ - */ - -#ifndef SDK_MACROS_H__ -#define SDK_MACROS_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Macro for parameter checking. - * - * If @p _cond evaluates to true, does nothing. Otherwise, - * if @p _module ## _PARAM_CHECK_DISABLED is @e not set (default), prints an error message - * if @p _printfn is provided, and returns from the calling function context with code @p _err. - * If @p _module ## _PARAM_CHECK_DISABLED is set, behaves like the ASSERT macro. - * - * Parameter checking implemented using this macro can be optionally turned off for release code. - * Only disable runtime parameter checks if size if a major concern. - * - * @param _module The module name. - * @param _cond The condition to be evaluated. - * @param _err The error to be returned. - * @param _printfn A printf-compatible function used to log the error. - * Leave empty if no logging is needed. - * - * @hideinitializer - */ -#define NRF_PARAM_CHECK(_module, _cond, _err, _printfn) \ - do { \ - if ((_cond)) { \ - /* Do nothing. */ \ - } else if (!(_module##_PARAM_CHECK_DISABLED)) { \ - _printfn("%s check failed in %s() with value 0x%x.", #_cond, __func__, \ - _err); \ - return _err; \ - } else { \ - __ASSERT((_cond)); \ - } \ - } while (0) - -/** - * @brief Macro for verifying statement to be true. It will cause the exterior function to return - * err_code if the statement is not true. - * - * @param[in] statement Statement to test. - * @param[in] err_code Error value to return if test was invalid. - * - * @retval nothing, but will cause the exterior function to return @p err_code if @p statement - * is false. - */ -#define VERIFY_TRUE(statement, err_code) \ - do { \ - if (!(statement)) { \ - return err_code; \ - } \ - } while (0) - -/** - * @brief Macro for verifying statement to be true. It will cause the exterior function to return - * if the statement is not true. - * - * @param[in] statement Statement to test. - */ -#define VERIFY_TRUE_VOID(statement) VERIFY_TRUE((statement),) - -/** - * @brief Macro for verifying statement to be false. It will cause the exterior function to return - * err_code if the statement is not false. - * - * @param[in] statement Statement to test. - * @param[in] err_code Error value to return if test was invalid. - * - * @retval nothing, but will cause the exterior function to return @p err_code if @p statement - * is true. - */ -#define VERIFY_FALSE(statement, err_code) \ - do { \ - if ((statement)) { \ - return err_code; \ - } \ - } while (0) - -/** - * @brief Macro for verifying statement to be false. It will cause the exterior function to return - * if the statement is not false. - * - * @param[in] statement Statement to test. - */ -#define VERIFY_FALSE_VOID(statement) VERIFY_FALSE((statement),) - -/** - * @brief Macro for verifying that a function returned NRF_SUCCESS. It will cause the exterior - * function to return error code of statement if it is not @ref NRF_SUCCESS. - * - * @param[in] statement Statement to check against NRF_SUCCESS. - */ -#define VERIFY_SUCCESS(statement) \ - do { \ - uint32_t _err_code = (uint32_t)(statement); \ - if (_err_code != NRF_SUCCESS) { \ - return _err_code; \ - } \ - } while (0) - -/** - * @brief Macro for verifying that a function returned NRF_SUCCESS. It will cause the exterior - * function to return if the err_code is not @ref NRF_SUCCESS. - * - * @param[in] err_code The error code to check. - */ -#define VERIFY_SUCCESS_VOID(err_code) VERIFY_TRUE_VOID((err_code) == NRF_SUCCESS) - -/** - * @brief Macro for verifying that the module is initialized. It will cause the exterior function to - * return @ref NRF_ERROR_INVALID_STATE if not. - * - * @note MODULE_INITIALIZED must be defined in each module using this macro. MODULE_INITIALIZED - * should be true if the module is initialized, false if not. - */ -#define VERIFY_MODULE_INITIALIZED() VERIFY_TRUE((MODULE_INITIALIZED), NRF_ERROR_INVALID_STATE) - -/** - * @brief Macro for verifying that the module is initialized. It will cause the exterior function to - * return if not. - * - * @note MODULE_INITIALIZED must be defined in each module using this macro. MODULE_INITIALIZED - * should be true if the module is initialized, false if not. - */ -#define VERIFY_MODULE_INITIALIZED_VOID() VERIFY_TRUE_VOID((MODULE_INITIALIZED)) - -/** - * @brief Macro for verifying that the module is initialized. It will cause the exterior function to - * return false if not. - * - * @note MODULE_INITIALIZED must be defined in each module using this macro. MODULE_INITIALIZED - * should be true if the module is initialized, false if not. - */ -#define VERIFY_MODULE_INITIALIZED_BOOL() VERIFY_TRUE((MODULE_INITIALIZED), false) - -/** - * @brief Macro for verifying that the module is initialized. It will cause the exterior function to - * return if not. - * - * @param[in] param The variable to check if is NULL. - */ -#define VERIFY_PARAM_NOT_NULL(param) VERIFY_FALSE(((param) == NULL), NRF_ERROR_NULL) - -/** - * @brief Macro for verifying that the module is initialized. It will cause the exterior function to - * return if not. - * - * @param[in] param The variable to check if is NULL. - */ -#define VERIFY_PARAM_NOT_NULL_VOID(param) VERIFY_FALSE_VOID(((param) == NULL)) - -/** @} */ - -#ifdef __cplusplus -} -#endif - -#endif /* SDK_MACROS_H__ */ diff --git a/lib/bluetooth/peer_manager/modules/auth_status_tracker.c b/lib/bluetooth/peer_manager/modules/auth_status_tracker.c index c0821179c5..f1fd1a0f70 100644 --- a/lib/bluetooth/peer_manager/modules/auth_status_tracker.c +++ b/lib/bluetooth/peer_manager/modules/auth_status_tracker.c @@ -25,7 +25,7 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); : (_lvl + 1) /** @brief Tracked peer state. */ -typedef struct { +struct blacklisted_peer { /** @brief BLE address, used to identify peer. */ ble_gap_addr_t peer_addr; /** @@ -50,11 +50,11 @@ typedef struct { bool is_active; /** @brief Flag indicating that this entry is valid in the peer blacklist. */ bool is_valid; -} blacklisted_peer_t; +}; -static struct bm_timer m_pairing_attempt_timer; -static blacklisted_peer_t m_blacklisted_peers[CONFIG_PM_RA_PROTECTION_TRACKED_PEERS_NUM]; -static uint64_t m_ticks_cnt; +static struct bm_timer pairing_attempt_timer; +static struct blacklisted_peer blacklisted_peers[CONFIG_PM_RA_PROTECTION_TRACKED_PEERS_NUM]; +static uint64_t ticks_cnt; /** * @brief Function for updating the state of blacklisted peers after timer has been stopped or @@ -66,55 +66,55 @@ static uint32_t blacklisted_peers_state_update(uint32_t ticks_passed) { uint32_t minimal_ticks = UINT32_MAX; - for (uint32_t id = 0; id < ARRAY_SIZE(m_blacklisted_peers); id++) { - blacklisted_peer_t *p_bl_peer = &m_blacklisted_peers[id]; + for (uint32_t id = 0; id < ARRAY_SIZE(blacklisted_peers); id++) { + struct blacklisted_peer *bl_peer = &blacklisted_peers[id]; - if (p_bl_peer->is_valid) { - if (p_bl_peer->is_active) { - if (p_bl_peer->penality_ticks > ticks_passed) { - p_bl_peer->penality_ticks -= ticks_passed; + if (bl_peer->is_valid) { + if (bl_peer->is_active) { + if (bl_peer->penality_ticks > ticks_passed) { + bl_peer->penality_ticks -= ticks_passed; minimal_ticks = - MIN(minimal_ticks, p_bl_peer->penality_ticks); + MIN(minimal_ticks, bl_peer->penality_ticks); } else { - p_bl_peer->is_active = false; + bl_peer->is_active = false; - if (p_bl_peer->penality_lvl == 0) { - p_bl_peer->is_valid = false; + if (bl_peer->penality_lvl == 0) { + bl_peer->is_valid = false; LOG_DBG("Peer has been removed from the " "blacklist, its address:"); - LOG_HEXDUMP_DBG(p_bl_peer->peer_addr.addr, - sizeof(p_bl_peer->peer_addr.addr), ""); + LOG_HEXDUMP_DBG(bl_peer->peer_addr.addr, + sizeof(bl_peer->peer_addr.addr), ""); } else { minimal_ticks = MIN(minimal_ticks, PAIR_REWARD_TICKS); } LOG_DBG("Pairing waiting interval has expired for:"); - LOG_HEXDUMP_DBG(p_bl_peer->peer_addr.addr, - sizeof(p_bl_peer->peer_addr.addr), ""); + LOG_HEXDUMP_DBG(bl_peer->peer_addr.addr, + sizeof(bl_peer->peer_addr.addr), ""); } } else { - if (p_bl_peer->penality_lvl == 0) { - p_bl_peer->is_valid = false; + if (bl_peer->penality_lvl == 0) { + bl_peer->is_valid = false; LOG_DBG("Peer has been removed from the blacklist, " "its address:"); - LOG_HEXDUMP_DBG(p_bl_peer->peer_addr.addr, - sizeof(p_bl_peer->peer_addr.addr), ""); + LOG_HEXDUMP_DBG(bl_peer->peer_addr.addr, + sizeof(bl_peer->peer_addr.addr), ""); } else { - p_bl_peer->reward_ticks += ticks_passed; - if (p_bl_peer->reward_ticks >= PAIR_REWARD_TICKS) { - p_bl_peer->penality_lvl--; - p_bl_peer->reward_ticks -= PAIR_REWARD_TICKS; + bl_peer->reward_ticks += ticks_passed; + if (bl_peer->reward_ticks >= PAIR_REWARD_TICKS) { + bl_peer->penality_lvl--; + bl_peer->reward_ticks -= PAIR_REWARD_TICKS; LOG_DBG("Peer penality level has decreased " "to %d for device:", - p_bl_peer->penality_lvl); - LOG_HEXDUMP_DBG(p_bl_peer->peer_addr.addr, - sizeof(p_bl_peer->peer_addr.addr), ""); + bl_peer->penality_lvl); + LOG_HEXDUMP_DBG(bl_peer->peer_addr.addr, + sizeof(bl_peer->peer_addr.addr), ""); } minimal_ticks = MIN(minimal_ticks, - (PAIR_REWARD_TICKS - p_bl_peer->reward_ticks)); + (PAIR_REWARD_TICKS - bl_peer->reward_ticks)); } } } @@ -135,10 +135,10 @@ static void blacklisted_peers_state_transition_handle(void *context) uint32_t ticks_passed = (uint32_t)context; minimal_ticks = blacklisted_peers_state_update(ticks_passed); - m_ticks_cnt = k_uptime_ticks(); + ticks_cnt = k_uptime_ticks(); if (minimal_ticks != UINT32_MAX) { - err = bm_timer_start(&m_pairing_attempt_timer, minimal_ticks, + err = bm_timer_start(&pairing_attempt_timer, minimal_ticks, (void *)minimal_ticks); if (err) { LOG_WRN("bm_timer_start() returned %d", err); @@ -149,7 +149,7 @@ static void blacklisted_peers_state_transition_handle(void *context) uint32_t ast_init(void) { - int err_code = bm_timer_init(&m_pairing_attempt_timer, BM_TIMER_MODE_SINGLE_SHOT, + int err_code = bm_timer_init(&pairing_attempt_timer, BM_TIMER_MODE_SINGLE_SHOT, blacklisted_peers_state_transition_handle); if (err_code) { return NRF_ERROR_INTERNAL; @@ -164,7 +164,7 @@ void ast_auth_error_notify(uint16_t conn_handle) uint32_t err_code; ble_gap_addr_t peer_addr; uint32_t new_timeout; - uint32_t free_id = ARRAY_SIZE(m_blacklisted_peers); + uint32_t free_id = ARRAY_SIZE(blacklisted_peers); bool new_bl_entry = true; /* Get the peer address associated with connection handle. */ @@ -177,39 +177,39 @@ void ast_auth_error_notify(uint16_t conn_handle) } /* Stop the timer and update the state of all blacklisted peers. */ - err = bm_timer_stop(&m_pairing_attempt_timer); + err = bm_timer_stop(&pairing_attempt_timer); if (err) { LOG_WRN("bm_timer_stop() returned %d", err); return; } - new_timeout = blacklisted_peers_state_update((uint32_t)(k_uptime_ticks() - m_ticks_cnt)); - m_ticks_cnt = k_uptime_ticks(); + new_timeout = blacklisted_peers_state_update((uint32_t)(k_uptime_ticks() - ticks_cnt)); + ticks_cnt = k_uptime_ticks(); /* Check if authorization has failed for already blacklisted peer. */ - for (uint32_t id = 0; id < ARRAY_SIZE(m_blacklisted_peers); id++) { - blacklisted_peer_t *p_bl_peer = &m_blacklisted_peers[id]; + for (uint32_t id = 0; id < ARRAY_SIZE(blacklisted_peers); id++) { + struct blacklisted_peer *bl_peer = &blacklisted_peers[id]; - if (p_bl_peer->is_valid) { - if (memcmp(peer_addr.addr, p_bl_peer->peer_addr.addr, BLE_GAP_ADDR_LEN) == + if (bl_peer->is_valid) { + if (memcmp(peer_addr.addr, bl_peer->peer_addr.addr, BLE_GAP_ADDR_LEN) == 0) { - uint8_t lvl = p_bl_peer->penality_lvl; + uint8_t lvl = bl_peer->penality_lvl; PENALITY_LVL_NEXT_SET(lvl); - p_bl_peer->penality_lvl = lvl; - p_bl_peer->reward_ticks = 0; - p_bl_peer->penality_ticks = PENALITY_LVL_TO_PENALITY_TICKS(lvl); + bl_peer->penality_lvl = lvl; + bl_peer->reward_ticks = 0; + bl_peer->penality_ticks = PENALITY_LVL_TO_PENALITY_TICKS(lvl); - new_timeout = MIN(new_timeout, p_bl_peer->penality_ticks); + new_timeout = MIN(new_timeout, bl_peer->penality_ticks); - p_bl_peer->is_active = true; + bl_peer->is_active = true; new_bl_entry = false; LOG_DBG("Pairing waiting interval has been renewed. " "Penality level: %d for device:", lvl); - LOG_HEXDUMP_DBG(p_bl_peer->peer_addr.addr, - sizeof(p_bl_peer->peer_addr.addr), ""); + LOG_HEXDUMP_DBG(bl_peer->peer_addr.addr, + sizeof(bl_peer->peer_addr.addr), ""); } } else { free_id = id; @@ -218,23 +218,23 @@ void ast_auth_error_notify(uint16_t conn_handle) /* Add a new peer to the blacklist. */ if (new_bl_entry) { - if (free_id < ARRAY_SIZE(m_blacklisted_peers)) { - blacklisted_peer_t *p_bl_peer = &m_blacklisted_peers[free_id]; + if (free_id < ARRAY_SIZE(blacklisted_peers)) { + struct blacklisted_peer *bl_peer = &blacklisted_peers[free_id]; - memcpy(&p_bl_peer->peer_addr, &peer_addr, sizeof(peer_addr)); + memcpy(&bl_peer->peer_addr, &peer_addr, sizeof(peer_addr)); - p_bl_peer->penality_lvl = 0; - p_bl_peer->reward_ticks = 0; - p_bl_peer->penality_ticks = - PENALITY_LVL_TO_PENALITY_TICKS(p_bl_peer->penality_lvl); + bl_peer->penality_lvl = 0; + bl_peer->reward_ticks = 0; + bl_peer->penality_ticks = + PENALITY_LVL_TO_PENALITY_TICKS(bl_peer->penality_lvl); - new_timeout = MIN(new_timeout, p_bl_peer->penality_ticks); + new_timeout = MIN(new_timeout, bl_peer->penality_ticks); - p_bl_peer->is_active = true; - p_bl_peer->is_valid = true; + bl_peer->is_active = true; + bl_peer->is_valid = true; LOG_DBG("New peer has been added to the blacklist:"); - LOG_HEXDUMP_DBG(p_bl_peer->peer_addr.addr, - sizeof(p_bl_peer->peer_addr.addr), ""); + LOG_HEXDUMP_DBG(bl_peer->peer_addr.addr, + sizeof(bl_peer->peer_addr.addr), ""); } else { LOG_WRN("No space to blacklist another peer ID"); } @@ -242,7 +242,7 @@ void ast_auth_error_notify(uint16_t conn_handle) /* Restart the timer. */ if (new_timeout != UINT32_MAX) { - err = bm_timer_start(&m_pairing_attempt_timer, new_timeout, (void *)new_timeout); + err = bm_timer_start(&pairing_attempt_timer, new_timeout, (void *)new_timeout); if (err) { LOG_WRN("bm_timer_start() returned %d", err); } @@ -262,13 +262,12 @@ bool ast_peer_blacklisted(uint16_t conn_handle) return true; } - for (uint32_t id = 0; id < ARRAY_SIZE(m_blacklisted_peers); id++) { - blacklisted_peer_t *p_bl_peer = &m_blacklisted_peers[id]; + for (uint32_t id = 0; id < ARRAY_SIZE(blacklisted_peers); id++) { + struct blacklisted_peer *bl_peer = &blacklisted_peers[id]; - if (p_bl_peer->is_valid) { - if ((memcmp(peer_addr.addr, p_bl_peer->peer_addr.addr, BLE_GAP_ADDR_LEN) == - 0) && - (p_bl_peer->is_active)) { + if (bl_peer->is_valid) { + if ((memcmp(peer_addr.addr, bl_peer->peer_addr.addr, BLE_GAP_ADDR_LEN) == 0) + && (bl_peer->is_active)) { return true; } } diff --git a/lib/bluetooth/peer_manager/modules/gatt_cache_manager.c b/lib/bluetooth/peer_manager/modules/gatt_cache_manager.c index 20f596a997..ba12d9acfe 100644 --- a/lib/bluetooth/peer_manager/modules/gatt_cache_manager.c +++ b/lib/bluetooth/peer_manager/modules/gatt_cache_manager.c @@ -26,108 +26,59 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); -#define NRF_MTX_LOCKED 1 -#define NRF_MTX_UNLOCKED 0 - -typedef atomic_t nrf_mtx_t; - -/** - * @brief Initialize mutex. - * - * This function _must_ be called before nrf_mtx_trylock() and nrf_mtx_unlock() functions. - * - * @param[in, out] p_mtx The mutex to be initialized. - */ -__STATIC_INLINE void nrf_mtx_init(nrf_mtx_t *p_mtx) -{ - __ASSERT(p_mtx != NULL, ""); - - atomic_set(p_mtx, NRF_MTX_UNLOCKED); -} - -/** - * @brief Try to lock a mutex. - * - * If the mutex is already held by another context, this function will return immediately. - * - * @param[in, out] p_mtx The mutex to be locked. - * @return true if lock was acquired, false if not - */ -__STATIC_INLINE bool nrf_mtx_trylock(nrf_mtx_t *p_mtx) -{ - __ASSERT(p_mtx != NULL, ""); - - return atomic_cas(p_mtx, NRF_MTX_UNLOCKED, NRF_MTX_LOCKED); -} - -/** - * @brief Unlock a mutex. - * - * This function _must_ only be called when holding the lock. Unlocking a mutex which you do not - * hold will give undefined behavior. - * - * @note Unlock must happen from the same context as the one used to lock the mutex. - * - * @param[in, out] p_mtx The mutex to be unlocked. - */ -__STATIC_INLINE void nrf_mtx_unlock(nrf_mtx_t *p_mtx) -{ - __ASSERT(p_mtx != NULL, ""); - __ASSERT(*p_mtx == NRF_MTX_LOCKED, ""); - - atomic_set(p_mtx, NRF_MTX_UNLOCKED); -} +#define MTX_LOCKED 1 +#define MTX_UNLOCKED 0 /* The number of registered event handlers. */ -#define GCM_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define GCM_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* GATT Cache Manager event handler in Peer Manager. */ -extern void pm_gcm_evt_handler(pm_evt_t *p_gcm_evt); +extern void pm_gcm_evt_handler(struct pm_evt *gcm_evt); /** * @brief GATT Cache Manager events' handlers. * The number of elements in this array is GCM_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t m_evt_handlers[] = {pm_gcm_evt_handler}; +static pm_evt_handler_internal_t evt_handlers[] = {pm_gcm_evt_handler}; -static bool m_module_initialized; +static bool module_initialized; /** @brief Mutex indicating whether a local DB write operation is ongoing. */ -static nrf_mtx_t m_db_update_in_progress_mutex; +static atomic_t db_update_in_progress_mutex; /** * @brief Flag ID for flag collection to keep track of which connections need a local DB update * procedure. */ -static int m_flag_local_db_update_pending; +static int flag_local_db_update_pending; /** * @brief Flag ID for flag collection to keep track of which connections need a local DB apply * procedure. */ -static int m_flag_local_db_apply_pending; +static int flag_local_db_apply_pending; /** * @brief Flag ID for flag collection to keep track of which connections need to be sent a service * changed indication. */ -static int m_flag_service_changed_pending; +static int flag_service_changed_pending; /** * @brief Flag ID for flag collection to keep track of which connections have been sent a service * changed indication and are waiting for a handle value confirmation. */ -static int m_flag_service_changed_sent; +static int flag_service_changed_sent; /** * @brief Flag ID for flag collection to keep track of which connections need to have their Central * Address Resolution value stored. */ -static int m_flag_car_update_pending; +static int flag_car_update_pending; /** * @brief Flag ID for flag collection to keep track of which connections are pending Central * Address Resolution handle reply. */ -static int m_flag_car_handle_queried; +static int flag_car_handle_queried; /** * @brief Flag ID for flag collection to keep track of which connections are pending Central * Address Resolution value reply. */ -static int m_flag_car_value_queried; +static int flag_car_value_queried; /** * @brief Function for resetting the module variable(s) of the GSCM module. @@ -136,15 +87,15 @@ static int m_flag_car_value_queried; */ static void internal_state_reset(void) { - m_module_initialized = false; + module_initialized = false; } -static void evt_send(pm_evt_t *p_gcm_evt) +static void evt_send(struct pm_evt *gcm_evt) { - p_gcm_evt->peer_id = im_peer_id_get_by_conn_handle(p_gcm_evt->conn_handle); + gcm_evt->peer_id = im_peer_id_get_by_conn_handle(gcm_evt->conn_handle); for (uint32_t i = 0; i < GCM_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_gcm_evt); + evt_handlers[i](gcm_evt); } } @@ -152,15 +103,15 @@ static void evt_send(pm_evt_t *p_gcm_evt) * @brief Function for checking a write event for whether a CCCD was written during the write * operation. * - * @param[in] p_write_evt The parameters of the write event. + * @param[in] write_evt The parameters of the write event. * * @return Whether the write was on a CCCD. */ -static bool cccd_written(ble_gatts_evt_write_t const *p_write_evt) +static bool cccd_written(const ble_gatts_evt_write_t *write_evt) { - return ((p_write_evt->op == BLE_GATTS_OP_WRITE_REQ) && - (p_write_evt->uuid.type == BLE_UUID_TYPE_BLE) && - (p_write_evt->uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG)); + return ((write_evt->op == BLE_GATTS_OP_WRITE_REQ) && + (write_evt->uuid.type == BLE_UUID_TYPE_BLE) && + (write_evt->uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG)); } /** @@ -171,9 +122,13 @@ static bool cccd_written(ble_gatts_evt_write_t const *p_write_evt) */ static void send_unexpected_error(uint16_t conn_handle, uint32_t err_code) { - pm_evt_t error_evt = {.evt_id = PM_EVT_ERROR_UNEXPECTED, - .conn_handle = conn_handle, - .params = {.error_unexpected = {.error = err_code}}}; + struct pm_evt error_evt = { + .evt_id = PM_EVT_ERROR_UNEXPECTED, + .conn_handle = conn_handle, + .params.error_unexpected = { + .error = err_code, + }, + }; evt_send(&error_evt); } @@ -191,7 +146,7 @@ static void local_db_apply_in_evt(uint16_t conn_handle) { bool set_procedure_as_pending = false; uint32_t err_code; - pm_evt_t event = { + struct pm_evt event = { .conn_handle = conn_handle, }; @@ -233,7 +188,7 @@ static void local_db_apply_in_evt(uint16_t conn_handle) break; } - ble_conn_state_user_flag_set(conn_handle, m_flag_local_db_apply_pending, + ble_conn_state_user_flag_set(conn_handle, flag_local_db_apply_pending, set_procedure_as_pending); } @@ -247,7 +202,7 @@ static void local_db_apply_in_evt(uint16_t conn_handle) */ static __INLINE void local_db_update(uint16_t conn_handle, bool update) { - ble_conn_state_user_flag_set(conn_handle, m_flag_local_db_update_pending, update); + ble_conn_state_user_flag_set(conn_handle, flag_local_db_update_pending, update); } /** @@ -281,7 +236,7 @@ static bool local_db_update_in_evt(uint16_t conn_handle) break; case NRF_ERROR_RESOURCES: { - pm_evt_t event = { + struct pm_evt event = { .evt_id = PM_EVT_STORAGE_FULL, .conn_handle = conn_handle, }; @@ -311,18 +266,18 @@ static bool local_db_update_in_evt(uint16_t conn_handle) * @details This function will search all system handles consecutively. * * @param[in] conn_handle The connection to check. - * @param[out] p_cccd The CCCD value of the service changed characteristic for this link. + * @param[out] cccd The CCCD value of the service changed characteristic for this link. * * @return Any error from @ref sd_ble_gatts_value_get or @ref sd_ble_gatts_attr_get. */ -static uint32_t service_changed_cccd(uint16_t conn_handle, uint16_t *p_cccd) +static uint32_t service_changed_cccd(uint16_t conn_handle, uint16_t *cccd) { bool sc_found = false; uint16_t end_handle; uint32_t err_code = sd_ble_gatts_initial_user_handle_get(&end_handle); - __ASSERT(err_code == NRF_SUCCESS, ""); + __ASSERT_NO_MSG(err_code == NRF_SUCCESS); for (uint16_t handle = 1; handle < end_handle; handle++) { ble_uuid_t uuid; @@ -335,7 +290,7 @@ static uint32_t service_changed_cccd(uint16_t conn_handle, uint16_t *p_cccd) (uuid.uuid == BLE_UUID_GATT_CHARACTERISTIC_SERVICE_CHANGED)) { sc_found = true; } else if (sc_found && (uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG)) { - value.p_value = (uint8_t *)p_cccd; + value.p_value = (uint8_t *)cccd; return sd_ble_gatts_value_get(conn_handle, handle, &value); } } @@ -359,7 +314,7 @@ static void service_changed_send_in_evt(uint16_t conn_handle) switch (err_code) { case NRF_SUCCESS: { - pm_evt_t event = { + struct pm_evt event = { .evt_id = PM_EVT_SERVICE_CHANGED_IND_SENT, .conn_handle = conn_handle, }; @@ -413,62 +368,64 @@ static void service_changed_send_in_evt(uint16_t conn_handle) break; } - ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, sc_pending_state); - ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_sent, sc_sent_state); + ble_conn_state_user_flag_set(conn_handle, flag_service_changed_pending, sc_pending_state); + ble_conn_state_user_flag_set(conn_handle, flag_service_changed_sent, sc_sent_state); } #endif -static void apply_pending_handle(uint16_t conn_handle, void *p_context) +static void apply_pending_handle(uint16_t conn_handle, void *context) { - ARG_UNUSED(p_context); + ARG_UNUSED(context); local_db_apply_in_evt(conn_handle); } static __INLINE void apply_pending_flags_check(void) { - (void)ble_conn_state_for_each_set_user_flag(m_flag_local_db_apply_pending, + (void)ble_conn_state_for_each_set_user_flag(flag_local_db_apply_pending, apply_pending_handle, NULL); } -static void db_update_pending_handle(uint16_t conn_handle, void *p_context) +static void db_update_pending_handle(uint16_t conn_handle, void *context) { - ARG_UNUSED(p_context); - if (nrf_mtx_trylock(&m_db_update_in_progress_mutex)) { + ARG_UNUSED(context); + if (atomic_cas(&db_update_in_progress_mutex, MTX_UNLOCKED, MTX_LOCKED)) { if (local_db_update_in_evt(conn_handle)) { /* Successfully started writing to flash. */ return; } - nrf_mtx_unlock(&m_db_update_in_progress_mutex); + bool ok = atomic_cas(&db_update_in_progress_mutex, MTX_LOCKED, MTX_UNLOCKED); + __ASSERT_NO_MSG(ok == true); + ARG_UNUSED(ok); } } #if defined(CONFIG_PM_SERVICE_CHANGED) -static void sc_send_pending_handle(uint16_t conn_handle, void *p_context) +static void sc_send_pending_handle(uint16_t conn_handle, void *context) { - ARG_UNUSED(p_context); - if (!ble_conn_state_user_flag_get(conn_handle, m_flag_service_changed_sent)) { + ARG_UNUSED(context); + if (!ble_conn_state_user_flag_get(conn_handle, flag_service_changed_sent)) { service_changed_send_in_evt(conn_handle); } } static __INLINE void service_changed_pending_flags_check(void) { - (void)(ble_conn_state_for_each_set_user_flag(m_flag_service_changed_pending, + (void)(ble_conn_state_for_each_set_user_flag(flag_service_changed_pending, sc_send_pending_handle, NULL)); } static void service_changed_needed(uint16_t conn_handle) { if (gscm_service_changed_ind_needed(conn_handle)) { - ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, true); + ble_conn_state_user_flag_set(conn_handle, flag_service_changed_pending, true); } } #endif -static void car_update_pending_handle(uint16_t conn_handle, void *p_context) +static void car_update_pending_handle(uint16_t conn_handle, void *context) { - ARG_UNUSED(p_context); + ARG_UNUSED(context); ble_uuid_t car_uuid; @@ -476,13 +433,13 @@ static void car_update_pending_handle(uint16_t conn_handle, void *p_context) car_uuid.uuid = BLE_UUID_GAP_CHARACTERISTIC_CAR; car_uuid.type = BLE_UUID_TYPE_BLE; - ble_gattc_handle_range_t const car_handle_range = {1, 0xFFFF}; + const ble_gattc_handle_range_t car_handle_range = {1, 0xFFFF}; uint32_t err_code = sd_ble_gattc_char_value_by_uuid_read(conn_handle, &car_uuid, &car_handle_range); if (err_code == NRF_SUCCESS) { - ble_conn_state_user_flag_set(conn_handle, m_flag_car_handle_queried, true); + ble_conn_state_user_flag_set(conn_handle, flag_car_handle_queried, true); } } @@ -490,23 +447,23 @@ static void car_update_needed(uint16_t conn_handle) { uint32_t central_addr_res = 0; uint32_t central_addr_res_size = sizeof(uint32_t); - pm_peer_data_t peer_data; + struct pm_peer_data peer_data; - peer_data.p_all_data = ¢ral_addr_res; + peer_data.all_data = ¢ral_addr_res; if (pds_peer_data_read(im_peer_id_get_by_conn_handle(conn_handle), PM_PEER_DATA_ID_CENTRAL_ADDR_RES, &peer_data, ¢ral_addr_res_size) == NRF_ERROR_NOT_FOUND) { - ble_conn_state_user_flag_set(conn_handle, m_flag_car_update_pending, true); + ble_conn_state_user_flag_set(conn_handle, flag_car_update_pending, true); } } static __INLINE void update_pending_flags_check(void) { - uint32_t count = ble_conn_state_for_each_set_user_flag(m_flag_local_db_update_pending, + uint32_t count = ble_conn_state_for_each_set_user_flag(flag_local_db_update_pending, db_update_pending_handle, NULL); if (count == 0) { - (void)ble_conn_state_for_each_set_user_flag(m_flag_car_update_pending, + (void)ble_conn_state_for_each_set_user_flag(flag_car_update_pending, car_update_pending_handle, NULL); } } @@ -515,17 +472,17 @@ static __INLINE void update_pending_flags_check(void) * @brief Callback function for events from the ID Manager module. * This function is registered in the ID Manager module. * - * @param[in] p_event The event from the ID Manager module. + * @param[in] event The event from the ID Manager module. */ -void gcm_im_evt_handler(pm_evt_t *p_event) +void gcm_im_evt_handler(struct pm_evt *event) { - switch (p_event->evt_id) { + switch (event->evt_id) { case PM_EVT_BONDED_PEER_CONNECTED: - local_db_apply_in_evt(p_event->conn_handle); + local_db_apply_in_evt(event->conn_handle); #if defined(CONFIG_PM_SERVICE_CHANGED) - service_changed_needed(p_event->conn_handle); + service_changed_needed(event->conn_handle); #endif - car_update_needed(p_event->conn_handle); + car_update_needed(event->conn_handle); update_pending_flags_check(); break; default: @@ -537,15 +494,15 @@ void gcm_im_evt_handler(pm_evt_t *p_event) * @brief Callback function for events from the Peer Database module. * This handler is extern in Peer Database. * - * @param[in] p_event The event from the Security Dispatcher module. + * @param[in] event The event from the Security Dispatcher module. */ -void gcm_pdb_evt_handler(pm_evt_t *p_event) +void gcm_pdb_evt_handler(struct pm_evt *event) { - if (p_event->evt_id == PM_EVT_PEER_DATA_UPDATE_SUCCEEDED && - p_event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) { - switch (p_event->params.peer_data_update_succeeded.data_id) { + if (event->evt_id == PM_EVT_PEER_DATA_UPDATE_SUCCEEDED && + event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) { + switch (event->params.peer_data_update_succeeded.data_id) { case PM_PEER_DATA_ID_BONDING: { - uint16_t conn_handle = im_conn_handle_get(p_event->peer_id); + uint16_t conn_handle = im_conn_handle_get(event->peer_id); if (conn_handle != BLE_CONN_HANDLE_INVALID) { local_db_update(conn_handle, true); @@ -559,21 +516,21 @@ void gcm_pdb_evt_handler(pm_evt_t *p_event) uint32_t err_code; bool service_changed_pending = false; uint32_t service_changed_pending_size = sizeof(bool); - pm_peer_data_t peer_data; + struct pm_peer_data peer_data; - peer_data.p_all_data = &service_changed_pending; + peer_data.all_data = &service_changed_pending; - err_code = pds_peer_data_read(p_event->peer_id, + err_code = pds_peer_data_read(event->peer_id, PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING, &peer_data, &service_changed_pending_size); if (err_code == NRF_SUCCESS) { if (service_changed_pending) { - uint16_t conn_handle = im_conn_handle_get(p_event->peer_id); + uint16_t conn_handle = im_conn_handle_get(event->peer_id); if (conn_handle != BLE_CONN_HANDLE_INVALID) { ble_conn_state_user_flag_set( - conn_handle, m_flag_service_changed_pending, + conn_handle, flag_service_changed_pending, true); service_changed_pending_flags_check(); } @@ -584,9 +541,7 @@ void gcm_pdb_evt_handler(pm_evt_t *p_event) #endif case PM_PEER_DATA_ID_GATT_LOCAL: - if (m_db_update_in_progress_mutex == NRF_MTX_LOCKED) { - nrf_mtx_unlock(&m_db_update_in_progress_mutex); - } + (void)atomic_cas(&db_update_in_progress_mutex, MTX_LOCKED, MTX_UNLOCKED); /* Expecting a call to update_pending_flags_check() immediately. */ break; @@ -602,33 +557,33 @@ void gcm_pdb_evt_handler(pm_evt_t *p_event) uint32_t gcm_init(void) { - NRF_PM_DEBUG_CHECK(!m_module_initialized); + __ASSERT_NO_MSG(!module_initialized); internal_state_reset(); - m_flag_local_db_update_pending = ble_conn_state_user_flag_acquire(); - m_flag_local_db_apply_pending = ble_conn_state_user_flag_acquire(); - m_flag_service_changed_pending = ble_conn_state_user_flag_acquire(); - m_flag_service_changed_sent = ble_conn_state_user_flag_acquire(); - m_flag_car_update_pending = ble_conn_state_user_flag_acquire(); - m_flag_car_handle_queried = ble_conn_state_user_flag_acquire(); - m_flag_car_value_queried = ble_conn_state_user_flag_acquire(); - - if ((m_flag_local_db_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_local_db_apply_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_service_changed_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_service_changed_sent == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_car_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_car_handle_queried == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_car_value_queried == BLE_CONN_STATE_USER_FLAG_INVALID)) { + flag_local_db_update_pending = ble_conn_state_user_flag_acquire(); + flag_local_db_apply_pending = ble_conn_state_user_flag_acquire(); + flag_service_changed_pending = ble_conn_state_user_flag_acquire(); + flag_service_changed_sent = ble_conn_state_user_flag_acquire(); + flag_car_update_pending = ble_conn_state_user_flag_acquire(); + flag_car_handle_queried = ble_conn_state_user_flag_acquire(); + flag_car_value_queried = ble_conn_state_user_flag_acquire(); + + if ((flag_local_db_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_local_db_apply_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_service_changed_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_service_changed_sent == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_car_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_car_handle_queried == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_car_value_queried == BLE_CONN_STATE_USER_FLAG_INVALID)) { LOG_ERR("Could not acquire conn_state user flags. Increase " "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module."); return NRF_ERROR_INTERNAL; } - nrf_mtx_init(&m_db_update_in_progress_mutex); + (void)atomic_set(&db_update_in_progress_mutex, MTX_UNLOCKED); - m_module_initialized = true; + module_initialized = true; return NRF_SUCCESS; } @@ -639,13 +594,13 @@ void store_car_value(uint16_t conn_handle, bool car_value) static const uint32_t car_value_true = true; static const uint32_t car_value_false; - pm_peer_data_const_t peer_data = { + struct pm_peer_data_const peer_data = { .data_id = PM_PEER_DATA_ID_CENTRAL_ADDR_RES, .length_words = 1, }; - ble_conn_state_user_flag_set(conn_handle, m_flag_car_update_pending, false); - peer_data.p_central_addr_res = car_value ? &car_value_true : &car_value_false; + ble_conn_state_user_flag_set(conn_handle, flag_car_update_pending, false); + peer_data.central_addr_res = car_value ? &car_value_true : &car_value_false; uint32_t err_code = pds_peer_data_store(im_peer_id_get_by_conn_handle(conn_handle), &peer_data, NULL); if (err_code != NRF_SUCCESS) { @@ -658,20 +613,20 @@ void store_car_value(uint16_t conn_handle, bool car_value) /** * @brief Callback function for BLE events from the SoftDevice. * - * @param[in] p_ble_evt The BLE event from the SoftDevice. + * @param[in] ble_evt The BLE event from the SoftDevice. */ -void gcm_ble_evt_handler(ble_evt_t const *p_ble_evt) +void gcm_ble_evt_handler(const ble_evt_t *ble_evt) { - uint16_t conn_handle = p_ble_evt->evt.gatts_evt.conn_handle; + uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle; - switch (p_ble_evt->header.evt_id) { + switch (ble_evt->header.evt_id) { case BLE_GATTS_EVT_SYS_ATTR_MISSING: local_db_apply_in_evt(conn_handle); break; #if defined(CONFIG_PM_SERVICE_CHANGED) case BLE_GATTS_EVT_SC_CONFIRM: { - pm_evt_t event = { + struct pm_evt event = { .evt_id = PM_EVT_SERVICE_CHANGED_IND_CONFIRMED, .peer_id = im_peer_id_get_by_conn_handle(conn_handle), .conn_handle = conn_handle, @@ -679,15 +634,15 @@ void gcm_ble_evt_handler(ble_evt_t const *p_ble_evt) gscm_db_change_notification_done(event.peer_id); - ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_sent, false); - ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, false); + ble_conn_state_user_flag_set(conn_handle, flag_service_changed_sent, false); + ble_conn_state_user_flag_set(conn_handle, flag_service_changed_pending, false); evt_send(&event); break; } #endif case BLE_GATTS_EVT_WRITE: - if (cccd_written(&p_ble_evt->evt.gatts_evt.params.write)) { + if (cccd_written(&ble_evt->evt.gatts_evt.params.write)) { local_db_update(conn_handle, true); update_pending_flags_check(); } @@ -696,41 +651,41 @@ void gcm_ble_evt_handler(ble_evt_t const *p_ble_evt) case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP: { bool handle_found = false; - conn_handle = p_ble_evt->evt.gattc_evt.conn_handle; - const ble_gattc_evt_char_val_by_uuid_read_rsp_t *p_val = - &p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp; + conn_handle = ble_evt->evt.gattc_evt.conn_handle; + const ble_gattc_evt_char_val_by_uuid_read_rsp_t *val = + &ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp; - if (!ble_conn_state_user_flag_get(conn_handle, m_flag_car_handle_queried)) { + if (!ble_conn_state_user_flag_get(conn_handle, flag_car_handle_queried)) { break; } - ble_conn_state_user_flag_set(conn_handle, m_flag_car_handle_queried, false); + ble_conn_state_user_flag_set(conn_handle, flag_car_handle_queried, false); - if (p_ble_evt->evt.gattc_evt.gatt_status == + if (ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND) { /* Store 0. */ - } else if (p_ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS) { + } else if (ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS) { LOG_WRN("Unexpected GATT status while getting CAR char value: 0x%x", - p_ble_evt->evt.gattc_evt.gatt_status); + ble_evt->evt.gattc_evt.gatt_status); /* Store 0. */ } else { - if (p_val->count != 1) { + if (val->count != 1) { LOG_WRN("Multiple (%d) CAR characteristics found, using the first.", - p_val->count); + val->count); } - if (p_val->value_len != 1) { + if (val->value_len != 1) { LOG_WRN("Unexpected CAR characteristic value length (%d), store 0.", - p_val->value_len); + val->value_len); /* Store 0. */ } else { uint32_t err_code = sd_ble_gattc_read( - conn_handle, *(uint16_t *)p_val->handle_value, 0); + conn_handle, *(uint16_t *)val->handle_value, 0); if (err_code == NRF_SUCCESS) { handle_found = true; ble_conn_state_user_flag_set( - conn_handle, m_flag_car_value_queried, true); + conn_handle, flag_car_value_queried, true); } } } @@ -744,26 +699,26 @@ void gcm_ble_evt_handler(ble_evt_t const *p_ble_evt) case BLE_GATTC_EVT_READ_RSP: { bool car_value = false; - conn_handle = p_ble_evt->evt.gattc_evt.conn_handle; - const ble_gattc_evt_read_rsp_t *p_val = &p_ble_evt->evt.gattc_evt.params.read_rsp; + conn_handle = ble_evt->evt.gattc_evt.conn_handle; + const ble_gattc_evt_read_rsp_t *val = &ble_evt->evt.gattc_evt.params.read_rsp; - if (!ble_conn_state_user_flag_get(conn_handle, m_flag_car_value_queried)) { + if (!ble_conn_state_user_flag_get(conn_handle, flag_car_value_queried)) { break; } - ble_conn_state_user_flag_set(conn_handle, m_flag_car_value_queried, false); + ble_conn_state_user_flag_set(conn_handle, flag_car_value_queried, false); - if (p_ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS) { + if (ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS) { LOG_WRN("Unexpected GATT status while getting CAR char value: 0x%x", - p_ble_evt->evt.gattc_evt.gatt_status); + ble_evt->evt.gattc_evt.gatt_status); /* Store 0. */ } else { - if (p_val->len != 1) { + if (val->len != 1) { LOG_WRN("Unexpected CAR characteristic value length (%d), store 0.", - p_val->len); + val->len); /* Store 0. */ } else { - car_value = *p_val->data; + car_value = *val->data; } } @@ -779,7 +734,7 @@ void gcm_ble_evt_handler(ble_evt_t const *p_ble_evt) uint32_t gcm_local_db_cache_update(uint16_t conn_handle) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); local_db_update(conn_handle, true); update_pending_flags_check(); @@ -798,7 +753,7 @@ void gcm_local_database_has_changed(void) if (im_peer_id_get_by_conn_handle(conn_handles.conn_handles[i]) == PM_PEER_ID_INVALID) { ble_conn_state_user_flag_set(conn_handles.conn_handles[i], - m_flag_service_changed_pending, true); + flag_service_changed_pending, true); } } diff --git a/lib/bluetooth/peer_manager/modules/gatts_cache_manager.c b/lib/bluetooth/peer_manager/modules/gatts_cache_manager.c index cc02ebf9b7..1abd16a5e1 100644 --- a/lib/bluetooth/peer_manager/modules/gatts_cache_manager.c +++ b/lib/bluetooth/peer_manager/modules/gatts_cache_manager.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -24,15 +25,15 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); #if defined(CONFIG_PM_SERVICE_CHANGED) /* The number of registered event handlers. */ -#define GSCM_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define GSCM_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* GATTS Cache Manager event handler in Peer Manager. */ -extern void pm_gscm_evt_handler(pm_evt_t *p_gcm_evt); +extern void pm_gscm_evt_handler(struct pm_evt *gcm_evt); /* GATTS Cache Manager events' handlers. * The number of elements in this array is GSCM_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t m_evt_handlers[] = {pm_gscm_evt_handler}; +static pm_evt_handler_internal_t evt_handlers[] = {pm_gscm_evt_handler}; #endif /* Syntactic sugar, two spoons. */ @@ -40,26 +41,26 @@ static pm_evt_handler_internal_t m_evt_handlers[] = {pm_gscm_evt_handler}; #define SYS_ATTR_USR (BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS) #define SYS_ATTR_BOTH (SYS_ATTR_SYS | SYS_ATTR_USR) -static bool m_module_initialized; -static pm_peer_id_t m_current_sc_store_peer_id; +static bool module_initialized; +static uint16_t current_sc_store_peer_id; /** @brief Function for resetting the module variable(s) of the GSCM module. */ static void internal_state_reset(void) { - m_module_initialized = false; - m_current_sc_store_peer_id = PM_PEER_ID_INVALID; + module_initialized = false; + current_sc_store_peer_id = PM_PEER_ID_INVALID; /* If CONFIG_PM_SERVICE_CHANGED is 0, this variable is unused. */ - UNUSED_VARIABLE(m_current_sc_store_peer_id); + UNUSED_VARIABLE(current_sc_store_peer_id); } #if defined(CONFIG_PM_SERVICE_CHANGED) -static void evt_send(pm_evt_t *p_gscm_evt) +static void evt_send(struct pm_evt *gscm_evt) { - p_gscm_evt->conn_handle = im_conn_handle_get(p_gscm_evt->peer_id); + gscm_evt->conn_handle = im_conn_handle_get(gscm_evt->peer_id); for (uint32_t i = 0; i < GSCM_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_gscm_evt); + evt_handlers[i](gscm_evt); } } @@ -71,22 +72,22 @@ static void evt_send(pm_evt_t *p_gscm_evt) */ static void service_changed_pending_set(void) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); uint32_t err_code; /* Use a uint32_t to enforce 4-byte alignment. */ static const uint32_t service_changed_pending = true; - pm_peer_data_const_t peer_data = { + struct pm_peer_data_const peer_data = { .data_id = PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING, .length_words = PM_SC_STATE_N_WORDS(), - .p_service_changed_pending = (bool *)&service_changed_pending, + .service_changed_pending = (bool *)&service_changed_pending, }; - while (m_current_sc_store_peer_id != PM_PEER_ID_INVALID) { - err_code = pds_peer_data_store(m_current_sc_store_peer_id, &peer_data, NULL); + while (current_sc_store_peer_id != PM_PEER_ID_INVALID) { + err_code = pds_peer_data_store(current_sc_store_peer_id, &peer_data, NULL); if (err_code != NRF_SUCCESS) { - pm_evt_t evt = {.peer_id = m_current_sc_store_peer_id}; + struct pm_evt evt = {.peer_id = current_sc_store_peer_id}; if (err_code == NRF_ERROR_BUSY) { /* Do nothing. */ @@ -98,7 +99,7 @@ static void service_changed_pending_set(void) "service changed " "state for peer id %d.", nrf_strerror_get(err_code), - m_current_sc_store_peer_id); + current_sc_store_peer_id); evt.evt_id = PM_EVT_ERROR_UNEXPECTED; evt.params.error_unexpected.error = err_code; evt_send(&evt); @@ -106,7 +107,7 @@ static void service_changed_pending_set(void) break; } - m_current_sc_store_peer_id = pds_next_peer_id_get(m_current_sc_store_peer_id); + current_sc_store_peer_id = pds_next_peer_id_get(current_sc_store_peer_id); } } @@ -114,11 +115,11 @@ static void service_changed_pending_set(void) * @brief Event handler for events from the Peer Database module. * This function is extern in Peer Database. * - * @param[in] p_event The event that has happened with peer id and flags. + * @param[in] event The event that has happened with peer id and flags. */ -void gscm_pdb_evt_handler(pm_evt_t *p_event) +void gscm_pdb_evt_handler(struct pm_evt *event) { - if (m_current_sc_store_peer_id != PM_PEER_ID_INVALID) { + if (current_sc_store_peer_id != PM_PEER_ID_INVALID) { service_changed_pending_set(); } } @@ -126,26 +127,26 @@ void gscm_pdb_evt_handler(pm_evt_t *p_event) uint32_t gscm_init(void) { - NRF_PM_DEBUG_CHECK(!m_module_initialized); + __ASSERT_NO_MSG(!module_initialized); internal_state_reset(); - m_module_initialized = true; + module_initialized = true; return NRF_SUCCESS; } uint32_t gscm_local_db_cache_update(uint16_t conn_handle) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); - pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); + uint16_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); uint32_t err_code; if (peer_id == PM_PEER_ID_INVALID) { return BLE_ERROR_INVALID_CONN_HANDLE; } - pm_peer_data_t peer_data; + struct pm_peer_data peer_data; uint16_t n_bufs = 1; bool retry_with_bigger_buffer = false; @@ -155,25 +156,25 @@ uint32_t gscm_local_db_cache_update(uint16_t conn_handle) err_code = pdb_write_buf_get(peer_id, PM_PEER_DATA_ID_GATT_LOCAL, n_bufs++, &peer_data); if (err_code == NRF_SUCCESS) { - pm_peer_data_local_gatt_db_t *p_local_gatt_db = - peer_data.p_local_gatt_db; + struct pm_peer_data_local_gatt_db *local_gatt_db = + peer_data.local_gatt_db; - p_local_gatt_db->flags = SYS_ATTR_BOTH; + local_gatt_db->flags = SYS_ATTR_BOTH; err_code = sd_ble_gatts_sys_attr_get( - conn_handle, &p_local_gatt_db->data[0], - &p_local_gatt_db->len, p_local_gatt_db->flags); + conn_handle, &local_gatt_db->data[0], + &local_gatt_db->len, local_gatt_db->flags); if (err_code == NRF_SUCCESS) { uint8_t local_gatt_db_buf[PM_PEER_DATA_LOCAL_GATT_DB_MAX_SIZE] = { 0 }; uint32_t local_gatt_db_size = PM_PEER_DATA_LOCAL_GATT_DB_MAX_SIZE; - pm_peer_data_local_gatt_db_t *curr_p_local_gatt_db = - (pm_peer_data_local_gatt_db_t *)local_gatt_db_buf; - pm_peer_data_t curr_peer_data; + struct pm_peer_data_local_gatt_db *curr_local_gatt_db = + (struct pm_peer_data_local_gatt_db *)local_gatt_db_buf; + struct pm_peer_data curr_peer_data; - curr_peer_data.p_all_data = local_gatt_db_buf; + curr_peer_data.all_data = local_gatt_db_buf; err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_GATT_LOCAL, @@ -189,13 +190,10 @@ uint32_t gscm_local_db_cache_update(uint16_t conn_handle) return NRF_ERROR_INTERNAL; } - if ((err_code == NRF_ERROR_NOT_FOUND) || - (p_local_gatt_db->len != - curr_p_local_gatt_db->len) || - (memcmp(p_local_gatt_db->data, - curr_p_local_gatt_db->data, - p_local_gatt_db->len) != 0)) { + (local_gatt_db->len != curr_local_gatt_db->len) || + (memcmp(local_gatt_db->data, curr_local_gatt_db->data, + local_gatt_db->len) != 0)) { err_code = pdb_write_buf_store( peer_id, PM_PEER_DATA_ID_GATT_LOCAL, peer_id); @@ -255,12 +253,12 @@ uint32_t gscm_local_db_cache_update(uint16_t conn_handle) uint32_t gscm_local_db_cache_apply(uint16_t conn_handle) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); - pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); + uint16_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); uint32_t err_code; - pm_peer_data_t peer_data; - uint8_t const *p_sys_attr_data = NULL; + struct pm_peer_data peer_data; + const uint8_t *sys_attr_data = NULL; uint16_t sys_attr_len = 0; uint32_t sys_attr_flags = (SYS_ATTR_BOTH); bool all_attributes_applied = true; @@ -268,23 +266,23 @@ uint32_t gscm_local_db_cache_apply(uint16_t conn_handle) if (peer_id != PM_PEER_ID_INVALID) { uint8_t local_gatt_db_buf[PM_PEER_DATA_LOCAL_GATT_DB_MAX_SIZE] = { 0 }; uint32_t local_gatt_db_size = PM_PEER_DATA_LOCAL_GATT_DB_MAX_SIZE; - pm_peer_data_local_gatt_db_t *curr_p_local_gatt_db = - (pm_peer_data_local_gatt_db_t *)local_gatt_db_buf; - peer_data.p_all_data = &local_gatt_db_buf; + struct pm_peer_data_local_gatt_db *curr_local_gatt_db = + (struct pm_peer_data_local_gatt_db *)local_gatt_db_buf; + peer_data.all_data = &local_gatt_db_buf; err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_GATT_LOCAL, &peer_data, &local_gatt_db_size); if (err_code == NRF_SUCCESS) { - pm_peer_data_local_gatt_db_t const *p_local_gatt_db; + const struct pm_peer_data_local_gatt_db *local_gatt_db; - p_local_gatt_db = curr_p_local_gatt_db; - p_sys_attr_data = p_local_gatt_db->data; - sys_attr_len = p_local_gatt_db->len; - sys_attr_flags = p_local_gatt_db->flags; + local_gatt_db = curr_local_gatt_db; + sys_attr_data = local_gatt_db->data; + sys_attr_len = local_gatt_db->len; + sys_attr_flags = local_gatt_db->flags; } } do { - err_code = sd_ble_gatts_sys_attr_set(conn_handle, p_sys_attr_data, sys_attr_len, + err_code = sd_ble_gatts_sys_attr_set(conn_handle, sys_attr_data, sys_attr_len, sys_attr_flags); if (err_code == NRF_ERROR_NO_MEM) { @@ -297,9 +295,9 @@ uint32_t gscm_local_db_cache_apply(uint16_t conn_handle) if (sys_attr_flags & SYS_ATTR_USR) { /* Try setting only system attributes. */ sys_attr_flags = SYS_ATTR_SYS; - } else if (p_sys_attr_data || sys_attr_len) { + } else if (sys_attr_data || sys_attr_len) { /* Try reporting that none exist. */ - p_sys_attr_data = NULL; + sys_attr_data = NULL; sys_attr_len = 0; sys_attr_flags = SYS_ATTR_BOTH; } else { @@ -322,8 +320,8 @@ uint32_t gscm_local_db_cache_apply(uint16_t conn_handle) #if defined(CONFIG_PM_SERVICE_CHANGED) void gscm_local_database_has_changed(void) { - NRF_PM_DEBUG_CHECK(m_module_initialized); - m_current_sc_store_peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID); + __ASSERT_NO_MSG(module_initialized); + current_sc_store_peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID); service_changed_pending_set(); } @@ -332,10 +330,10 @@ bool gscm_service_changed_ind_needed(uint16_t conn_handle) uint32_t err_code; bool service_changed_state; uint32_t service_changed_state_size = sizeof(bool); - pm_peer_data_t peer_data; + struct pm_peer_data peer_data; - peer_data.p_all_data = &service_changed_state; - pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); + peer_data.all_data = &service_changed_state; + uint16_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING, &peer_data, @@ -373,17 +371,17 @@ uint32_t gscm_service_changed_ind_send(uint16_t conn_handle) return err_code; } -void gscm_db_change_notification_done(pm_peer_id_t peer_id) +void gscm_db_change_notification_done(uint16_t peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); /* Use a uint32_t to enforce 4-byte alignment. */ static const uint32_t service_changed_pending; - pm_peer_data_const_t peer_data = { + struct pm_peer_data_const peer_data = { .data_id = PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING, .length_words = PM_SC_STATE_N_WORDS(), - .p_service_changed_pending = (bool *)&service_changed_pending, + .service_changed_pending = (bool *)&service_changed_pending, }; /* Don't need to check return code, because all error conditions can be ignored. */ diff --git a/lib/bluetooth/peer_manager/modules/id_manager.c b/lib/bluetooth/peer_manager/modules/id_manager.c index a1d51857d9..9cb18e062e 100644 --- a/lib/bluetooth/peer_manager/modules/id_manager.c +++ b/lib/bluetooth/peer_manager/modules/id_manager.c @@ -21,6 +21,7 @@ #include #include +#include LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); @@ -30,36 +31,36 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); #define IM_ADDR_CIPHERTEXT_LENGTH (3) /* The number of registered event handlers. */ -#define IM_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define IM_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* Identity Manager event handlers in Peer Manager and GATT Cache Manager. */ -extern void pm_im_evt_handler(pm_evt_t *p_event); -extern void gcm_im_evt_handler(pm_evt_t *p_event); +extern void pm_im_evt_handler(struct pm_evt *event); +extern void gcm_im_evt_handler(struct pm_evt *event); /* Identity Manager events' handlers. * The number of elements in this array is IM_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t const m_evt_handlers[] = {pm_im_evt_handler, gcm_im_evt_handler}; +static const pm_evt_handler_internal_t evt_handlers[] = {pm_im_evt_handler, gcm_im_evt_handler}; -typedef struct { +struct im_connection { uint16_t conn_handle; - pm_peer_id_t peer_id; + uint16_t peer_id; ble_gap_addr_t peer_address; -} im_connection_t; +}; -static im_connection_t m_connections[IM_MAX_CONN_HANDLES]; -static uint8_t m_wlisted_peer_cnt; -static pm_peer_id_t m_wlisted_peers[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; +static struct im_connection connections[IM_MAX_CONN_HANDLES]; +static uint8_t wlisted_peer_cnt; +static uint16_t wlisted_peers[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; /** * @brief Function for sending an event to all registered event handlers. * - * @param[in] p_event The event to distribute. + * @param[in] event The event to distribute. */ -static void evt_send(pm_evt_t *p_event) +static void evt_send(struct pm_evt *event) { for (uint32_t i = 0; i < IM_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_event); + evt_handlers[i](event); } } @@ -68,17 +69,17 @@ static void evt_send(pm_evt_t *p_event) * * @detail An all-zero IRK is not valid. This function will check if a given IRK is valid. * - * @param[in] p_irk The IRK for which the validity is going to be checked. + * @param[in] irk The IRK for which the validity is going to be checked. * * @retval true The IRK is valid. * @retval false The IRK is invalid. */ -bool is_valid_irk(ble_gap_irk_t const *p_irk) +bool is_valid_irk(const ble_gap_irk_t *irk) { - NRF_PM_DEBUG_CHECK(p_irk != NULL); + __ASSERT_NO_MSG(irk != NULL); for (uint32_t i = 0; i < BLE_GAP_SEC_KEY_LEN; i++) { - if (p_irk->irk[i] != 0) { + if (irk->irk[i] != 0) { return true; } } @@ -90,31 +91,31 @@ bool is_valid_irk(ble_gap_irk_t const *p_irk) * * @note The address type need to be identical, as well as every bit in the address itself. * - * @param[in] p_addr1 The first address to be compared. - * @param[in] p_addr2 The second address to be compared. + * @param[in] addr1 The first address to be compared. + * @param[in] addr2 The second address to be compared. * * @retval true The addresses are identical. * @retval false The addresses are not identical. */ -bool addr_compare(ble_gap_addr_t const *p_addr1, ble_gap_addr_t const *p_addr2) +bool addr_compare(const ble_gap_addr_t *addr1, const ble_gap_addr_t *addr2) { - if ((p_addr1 == NULL) || (p_addr2 == NULL)) { + if ((addr1 == NULL) || (addr2 == NULL)) { return false; } /* Check that the addr type is identical, return false if it is not */ - if (p_addr1->addr_type != p_addr2->addr_type) { + if (addr1->addr_type != addr2->addr_type) { return false; } /* Check if the addr bytes are is identical */ - return (memcmp(p_addr1->addr, p_addr2->addr, BLE_GAP_ADDR_LEN) == 0); + return (memcmp(addr1->addr, addr2->addr, BLE_GAP_ADDR_LEN) == 0); } -void im_ble_evt_handler(ble_evt_t const *ble_evt) +void im_ble_evt_handler(const ble_evt_t *ble_evt) { ble_gap_evt_t gap_evt; - pm_peer_id_t bonded_matching_peer_id; + uint16_t bonded_matching_peer_id; if (ble_evt->header.evt_id != BLE_GAP_EVT_CONNECTED) { /* Nothing to do. */ @@ -139,12 +140,12 @@ void im_ble_evt_handler(ble_evt_t const *ble_evt) * identification. */ - pm_peer_id_t peer_id; - pm_peer_data_flash_t peer_data; + uint16_t peer_id; + struct pm_peer_data_const peer_data; uint8_t peer_data_buffer[PM_PEER_DATA_MAX_SIZE] = { 0 }; - pm_peer_id_t peer_id_iter; + uint16_t peer_id_iter; - peer_data.p_all_data = peer_data_buffer; + peer_data.all_data = peer_data_buffer; pds_peer_data_iterate_prepare(&peer_id_iter); @@ -155,7 +156,7 @@ void im_ble_evt_handler(ble_evt_t const *ble_evt) &peer_data, &peer_id_iter)) { if (addr_compare( &gap_evt.params.connected.peer_addr, - &peer_data.p_bonding_data->peer_ble_id.id_addr_info)) { + &peer_data.bonding_data->peer_ble_id.id_addr_info)) { bonded_matching_peer_id = peer_id; break; } @@ -167,7 +168,7 @@ void im_ble_evt_handler(ble_evt_t const *ble_evt) &peer_data, &peer_id_iter)) { if (im_address_resolve( &gap_evt.params.connected.peer_addr, - &peer_data.p_bonding_data->peer_ble_id.id_info)) { + &peer_data.bonding_data->peer_ble_id.id_info)) { bonded_matching_peer_id = peer_id; break; } @@ -175,18 +176,18 @@ void im_ble_evt_handler(ble_evt_t const *ble_evt) } break; default: - NRF_PM_DEBUG_CHECK(false); + __ASSERT_NO_MSG(false); break; } } - m_connections[idx].conn_handle = gap_evt.conn_handle; - m_connections[idx].peer_id = bonded_matching_peer_id; - m_connections[idx].peer_address = gap_evt.params.connected.peer_addr; + connections[idx].conn_handle = gap_evt.conn_handle; + connections[idx].peer_id = bonded_matching_peer_id; + connections[idx].peer_address = gap_evt.params.connected.peer_addr; if (bonded_matching_peer_id != PM_PEER_ID_INVALID) { /* Send a bonded peer event */ - pm_evt_t im_evt; + struct pm_evt im_evt; im_evt.conn_handle = gap_evt.conn_handle; im_evt.peer_id = bonded_matching_peer_id; @@ -199,62 +200,62 @@ void im_ble_evt_handler(ble_evt_t const *ble_evt) * @brief Function to compare two sets of bonding data to check if they belong to the same device. * @note Invalid irks will never match even though they are identical. * - * @param[in] p_bonding_data1 First bonding data for comparison - * @param[in] p_bonding_data2 Second bonding data for comparison + * @param[in] bonding_data1 First bonding data for comparison + * @param[in] bonding_data2 Second bonding data for comparison * * @return True if the input matches, false if it does not. */ -bool im_is_duplicate_bonding_data(pm_peer_data_bonding_t const *p_bonding_data1, - pm_peer_data_bonding_t const *p_bonding_data2) +bool im_is_duplicate_bonding_data(const struct pm_peer_data_bonding *bonding_data1, + const struct pm_peer_data_bonding *bonding_data2) { - NRF_PM_DEBUG_CHECK(p_bonding_data1 != NULL); - NRF_PM_DEBUG_CHECK(p_bonding_data2 != NULL); + __ASSERT_NO_MSG(bonding_data1 != NULL); + __ASSERT_NO_MSG(bonding_data2 != NULL); - ble_gap_addr_t const *p_addr1 = &p_bonding_data1->peer_ble_id.id_addr_info; - ble_gap_addr_t const *p_addr2 = &p_bonding_data2->peer_ble_id.id_addr_info; + const ble_gap_addr_t *addr1 = &bonding_data1->peer_ble_id.id_addr_info; + const ble_gap_addr_t *addr2 = &bonding_data2->peer_ble_id.id_addr_info; bool duplicate_irk = - ((memcmp(p_bonding_data1->peer_ble_id.id_info.irk, - p_bonding_data2->peer_ble_id.id_info.irk, BLE_GAP_SEC_KEY_LEN) == 0) && - is_valid_irk(&p_bonding_data1->peer_ble_id.id_info) && - is_valid_irk(&p_bonding_data2->peer_ble_id.id_info)); + ((memcmp(bonding_data1->peer_ble_id.id_info.irk, + bonding_data2->peer_ble_id.id_info.irk, BLE_GAP_SEC_KEY_LEN) == 0) && + is_valid_irk(&bonding_data1->peer_ble_id.id_info) && + is_valid_irk(&bonding_data2->peer_ble_id.id_info)); - bool duplicate_addr = addr_compare(p_addr1, p_addr2); + bool duplicate_addr = addr_compare(addr1, addr2); - bool id_addrs = ((p_addr1->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) && - (p_addr1->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) && - (p_addr2->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) && - (p_addr2->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE)); + bool id_addrs = ((addr1->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) && + (addr1->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) && + (addr2->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) && + (addr2->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE)); return (duplicate_addr && id_addrs) || (duplicate_irk && !id_addrs); } -pm_peer_id_t im_find_duplicate_bonding_data(pm_peer_data_bonding_t const *p_bonding_data, - pm_peer_id_t peer_id_skip) +uint16_t im_find_duplicate_bonding_data(const struct pm_peer_data_bonding *bonding_data, + uint16_t peer_id_skip) { - pm_peer_id_t peer_id; - pm_peer_data_flash_t peer_data_duplicate; + uint16_t peer_id; + struct pm_peer_data_const peer_data_duplicate; uint8_t peer_data_buffer[PM_PEER_DATA_MAX_SIZE] = { 0 }; - pm_peer_id_t peer_id_iter; + uint16_t peer_id_iter; - peer_data_duplicate.p_all_data = peer_data_buffer; + peer_data_duplicate.all_data = peer_data_buffer; - NRF_PM_DEBUG_CHECK(p_bonding_data != NULL); + __ASSERT_NO_MSG(bonding_data != NULL); pds_peer_data_iterate_prepare(&peer_id_iter); while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data_duplicate, &peer_id_iter)) { if ((peer_id != peer_id_skip) && - im_is_duplicate_bonding_data(p_bonding_data, - peer_data_duplicate.p_bonding_data)) { + im_is_duplicate_bonding_data(bonding_data, + peer_data_duplicate.bonding_data)) { return peer_id; } } return PM_PEER_ID_INVALID; } -pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle) +uint16_t im_peer_id_get_by_conn_handle(uint16_t conn_handle) { const int idx = nrf_sdh_ble_idx_get(conn_handle); @@ -262,12 +263,12 @@ pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle) return PM_PEER_ID_INVALID; } - return m_connections[idx].peer_id; + return connections[idx].peer_id; } -uint32_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t *p_ble_addr) +uint32_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t *ble_addr) { - NRF_PM_DEBUG_CHECK(p_ble_addr != NULL); + __ASSERT_NO_MSG(ble_addr != NULL); const int idx = nrf_sdh_ble_idx_get(conn_handle); @@ -275,47 +276,47 @@ uint32_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t *p_ble_addr) return BLE_ERROR_INVALID_CONN_HANDLE; } - *p_ble_addr = m_connections[idx].peer_address; + *ble_addr = connections[idx].peer_address; return NRF_SUCCESS; } -bool im_master_ids_compare(ble_gap_master_id_t const *p_master_id1, - ble_gap_master_id_t const *p_master_id2) +bool im_master_ids_compare(const ble_gap_master_id_t *master_id1, + const ble_gap_master_id_t *master_id2) { - NRF_PM_DEBUG_CHECK(p_master_id1 != NULL); - NRF_PM_DEBUG_CHECK(p_master_id2 != NULL); + __ASSERT_NO_MSG(master_id1 != NULL); + __ASSERT_NO_MSG(master_id2 != NULL); - if (!im_master_id_is_valid(p_master_id1)) { + if (!im_master_id_is_valid(master_id1)) { return false; } - if (p_master_id1->ediv != p_master_id2->ediv) { + if (master_id1->ediv != master_id2->ediv) { return false; } - return (memcmp(p_master_id1->rand, p_master_id2->rand, BLE_GAP_SEC_RAND_LEN) == 0); + return (memcmp(master_id1->rand, master_id2->rand, BLE_GAP_SEC_RAND_LEN) == 0); } -pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t const *p_master_id) +uint16_t im_peer_id_get_by_master_id(const ble_gap_master_id_t *master_id) { - pm_peer_id_t peer_id; - pm_peer_data_flash_t peer_data; + uint16_t peer_id; + struct pm_peer_data_const peer_data; uint8_t peer_data_buffer[PM_PEER_DATA_MAX_SIZE] = { 0 }; - pm_peer_id_t peer_id_iter; + uint16_t peer_id_iter; - NRF_PM_DEBUG_CHECK(p_master_id != NULL); + __ASSERT_NO_MSG(master_id != NULL); - peer_data.p_all_data = peer_data_buffer; + peer_data.all_data = peer_data_buffer; pds_peer_data_iterate_prepare(&peer_id_iter); - /* For each stored peer, check if the master_id matches p_master_id */ + /* For each stored peer, check if the master_id matches master_id */ while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data, &peer_id_iter)) { - if (im_master_ids_compare(p_master_id, - &peer_data.p_bonding_data->own_ltk.master_id) || - im_master_ids_compare(p_master_id, - &peer_data.p_bonding_data->peer_ltk.master_id)) { + if (im_master_ids_compare(master_id, + &peer_data.bonding_data->own_ltk.master_id) || + im_master_ids_compare(master_id, + &peer_data.bonding_data->peer_ltk.master_id)) { /* If a matching master ID is found then return the peer ID. */ return peer_id; } @@ -325,45 +326,45 @@ pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t const *p_master_id) return PM_PEER_ID_INVALID; } -uint16_t im_conn_handle_get(pm_peer_id_t peer_id) +uint16_t im_conn_handle_get(uint16_t peer_id) { if (peer_id == PM_PEER_ID_INVALID) { return BLE_CONN_HANDLE_INVALID; } for (uint16_t idx = 0; idx < IM_MAX_CONN_HANDLES; idx++) { - if ((m_connections[idx].peer_id == peer_id) && - ble_conn_state_valid(m_connections[idx].conn_handle)) { - return m_connections[idx].conn_handle; + if ((connections[idx].peer_id == peer_id) && + ble_conn_state_valid(connections[idx].conn_handle)) { + return connections[idx].conn_handle; } } return BLE_CONN_HANDLE_INVALID; } -bool im_master_id_is_valid(ble_gap_master_id_t const *p_master_id) +bool im_master_id_is_valid(const ble_gap_master_id_t *master_id) { - if (p_master_id->ediv != 0) { + if (master_id->ediv != 0) { return true; } for (uint32_t i = 0; i < BLE_GAP_SEC_RAND_LEN; i++) { - if (p_master_id->rand[i] != 0) { + if (master_id->rand[i] != 0) { return true; } } return false; } -void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id) +void im_new_peer_id(uint16_t conn_handle, uint16_t peer_id) { const int idx = nrf_sdh_ble_idx_get(conn_handle); if (idx >= 0 && idx < IM_MAX_CONN_HANDLES) { - m_connections[idx].peer_id = peer_id; + connections[idx].peer_id = peer_id; } } -uint32_t im_peer_free(pm_peer_id_t peer_id) +uint32_t im_peer_free(uint16_t peer_id) { const uint16_t conn_handle = im_conn_handle_get(peer_id); const int idx = nrf_sdh_ble_idx_get(conn_handle); @@ -371,47 +372,47 @@ uint32_t im_peer_free(pm_peer_id_t peer_id) if ((ret == NRF_SUCCESS) && (conn_handle != BLE_CONN_HANDLE_INVALID) && (idx >= 0) && (idx < IM_MAX_CONN_HANDLES)) { - m_connections[idx].peer_id = PM_PEER_ID_INVALID; + connections[idx].peer_id = PM_PEER_ID_INVALID; } return ret; } /** @brief Given a list of peers, loads their GAP address and IRK into the provided buffers. */ -static uint32_t peers_id_keys_get(pm_peer_id_t const *p_peers, uint32_t peer_cnt, - ble_gap_addr_t *p_gap_addrs, uint32_t *p_addr_cnt, - ble_gap_irk_t *p_gap_irks, uint32_t *p_irk_cnt) +static uint32_t peers_id_keys_get(const uint16_t *peers, uint32_t peer_cnt, + ble_gap_addr_t *gap_addrs, uint32_t *addr_cnt, + ble_gap_irk_t *gap_irks, uint32_t *irk_cnt) { uint32_t ret; - pm_peer_data_bonding_t bond_data; - pm_peer_data_t peer_data; + struct pm_peer_data_bonding bond_data; + struct pm_peer_data peer_data; - uint32_t const buf_size = sizeof(bond_data); + const uint32_t buf_size = sizeof(bond_data); bool copy_addrs = false; bool copy_irks = false; - NRF_PM_DEBUG_CHECK(p_peers != NULL); + __ASSERT_NO_MSG(peers != NULL); /* One of these two has to be provided. */ - NRF_PM_DEBUG_CHECK((p_gap_addrs != NULL) || (p_gap_irks != NULL)); + __ASSERT_NO_MSG((gap_addrs != NULL) || (gap_irks != NULL)); - if ((p_gap_addrs != NULL) && (p_addr_cnt != NULL)) { - NRF_PM_DEBUG_CHECK((*p_addr_cnt) >= peer_cnt); + if ((gap_addrs != NULL) && (addr_cnt != NULL)) { + __ASSERT_NO_MSG((*addr_cnt) >= peer_cnt); copy_addrs = true; - *p_addr_cnt = 0; + *addr_cnt = 0; } - if ((p_gap_irks != NULL) && (p_irk_cnt != NULL)) { - NRF_PM_DEBUG_CHECK((*p_irk_cnt) >= peer_cnt); + if ((gap_irks != NULL) && (irk_cnt != NULL)) { + __ASSERT_NO_MSG((*irk_cnt) >= peer_cnt); copy_irks = true; - *p_irk_cnt = 0; + *irk_cnt = 0; } memset(&peer_data, 0x00, sizeof(peer_data)); - peer_data.p_bonding_data = &bond_data; + peer_data.bonding_data = &bond_data; /* Read through flash memory and look for peers ID keys. */ @@ -419,7 +420,7 @@ static uint32_t peers_id_keys_get(pm_peer_id_t const *p_peers, uint32_t peer_cnt memset(&bond_data, 0x00, sizeof(bond_data)); /* Read peer data from flash. */ - ret = pds_peer_data_read(p_peers[i], PM_PEER_DATA_ID_BONDING, &peer_data, + ret = pds_peer_data_read(peers[i], PM_PEER_DATA_ID_BONDING, &peer_data, &buf_size); if ((ret == NRF_ERROR_NOT_FOUND) || (ret == NRF_ERROR_INVALID_PARAM)) { @@ -427,7 +428,7 @@ static uint32_t peers_id_keys_get(pm_peer_id_t const *p_peers, uint32_t peer_cnt return NRF_ERROR_NOT_FOUND; } - uint8_t const addr_type = bond_data.peer_ble_id.id_addr_info.addr_type; + const uint8_t addr_type = bond_data.peer_ble_id.id_addr_info.addr_type; if ((addr_type != BLE_GAP_ADDR_TYPE_PUBLIC) && (addr_type != BLE_GAP_ADDR_TYPE_RANDOM_STATIC)) { @@ -439,42 +440,42 @@ static uint32_t peers_id_keys_get(pm_peer_id_t const *p_peers, uint32_t peer_cnt /* Copy the GAP address. */ if (copy_addrs) { - memcpy(&p_gap_addrs[i], &bond_data.peer_ble_id.id_addr_info, + memcpy(&gap_addrs[i], &bond_data.peer_ble_id.id_addr_info, sizeof(ble_gap_addr_t)); - (*p_addr_cnt)++; + (*addr_cnt)++; } /* Copy the IRK. */ if (copy_irks) { - memcpy(&p_gap_irks[i], bond_data.peer_ble_id.id_info.irk, + memcpy(&gap_irks[i], bond_data.peer_ble_id.id_info.irk, BLE_GAP_SEC_KEY_LEN); - (*p_irk_cnt)++; + (*irk_cnt)++; } } return NRF_SUCCESS; } -uint32_t im_device_identities_list_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt) +uint32_t im_device_identities_list_set(const uint16_t *peers, uint32_t peer_cnt) { uint32_t ret; - pm_peer_data_t peer_data; - pm_peer_data_bonding_t bond_data; + struct pm_peer_data peer_data; + struct pm_peer_data_bonding bond_data; ble_gap_id_key_t keys[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; - ble_gap_id_key_t const *key_ptrs[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; + const ble_gap_id_key_t *key_ptrs[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; if (peer_cnt > BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT) { return NRF_ERROR_INVALID_PARAM; } - if ((p_peers == NULL) || (peer_cnt == 0)) { + if ((peers == NULL) || (peer_cnt == 0)) { /* Clear the device identities list. */ return sd_ble_gap_device_identities_set(NULL, NULL, 0); } - peer_data.p_bonding_data = &bond_data; - uint32_t const buf_size = sizeof(bond_data); + peer_data.bonding_data = &bond_data; + const uint32_t buf_size = sizeof(bond_data); memset(keys, 0x00, sizeof(keys)); for (uint32_t i = 0; i < BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT; i++) { @@ -485,25 +486,25 @@ uint32_t im_device_identities_list_set(pm_peer_id_t const *p_peers, uint32_t pee memset(&bond_data, 0x00, sizeof(bond_data)); /* Read peer data from flash. */ - ret = pds_peer_data_read(p_peers[i], PM_PEER_DATA_ID_BONDING, &peer_data, + ret = pds_peer_data_read(peers[i], PM_PEER_DATA_ID_BONDING, &peer_data, &buf_size); if ((ret == NRF_ERROR_NOT_FOUND) || (ret == NRF_ERROR_INVALID_PARAM)) { LOG_WRN("peer id %d: Peer data could not be found in flash. Remove " "the peer ID " "from the peer list and try again.", - p_peers[i]); + peers[i]); return NRF_ERROR_NOT_FOUND; } - uint8_t const addr_type = bond_data.peer_ble_id.id_addr_info.addr_type; + const uint8_t addr_type = bond_data.peer_ble_id.id_addr_info.addr_type; if ((addr_type != BLE_GAP_ADDR_TYPE_PUBLIC) && (addr_type != BLE_GAP_ADDR_TYPE_RANDOM_STATIC)) { LOG_WRN("peer id %d: The address shared by the peer during bonding cannot " "be " "whitelisted. Remove the peer ID from the peer list and try again.", - p_peers[i]); + peers[i]); return BLE_ERROR_GAP_INVALID_BLE_ADDR; } @@ -514,40 +515,40 @@ uint32_t im_device_identities_list_set(pm_peer_id_t const *p_peers, uint32_t pee return sd_ble_gap_device_identities_set(key_ptrs, NULL, peer_cnt); } -uint32_t im_id_addr_set(ble_gap_addr_t const *p_addr) +uint32_t im_id_addr_set(const ble_gap_addr_t *addr) { - return sd_ble_gap_addr_set(p_addr); + return sd_ble_gap_addr_set(addr); } -uint32_t im_id_addr_get(ble_gap_addr_t *p_addr) +uint32_t im_id_addr_get(ble_gap_addr_t *addr) { - NRF_PM_DEBUG_CHECK(p_addr != NULL); + __ASSERT_NO_MSG(addr != NULL); - return sd_ble_gap_addr_get(p_addr); + return sd_ble_gap_addr_get(addr); } -uint32_t im_privacy_set(pm_privacy_params_t const *p_privacy_params) +uint32_t im_privacy_set(const ble_gap_privacy_params_t *privacy_params) { - return sd_ble_gap_privacy_set(p_privacy_params); + return sd_ble_gap_privacy_set(privacy_params); } -uint32_t im_privacy_get(pm_privacy_params_t *p_privacy_params) +uint32_t im_privacy_get(ble_gap_privacy_params_t *privacy_params) { - return sd_ble_gap_privacy_get(p_privacy_params); + return sd_ble_gap_privacy_get(privacy_params); } /* Create a whitelist for the user using the cached list of peers. * This whitelist is meant to be provided by the application to the Advertising module. */ -uint32_t im_whitelist_get(ble_gap_addr_t *p_addrs, uint32_t *p_addr_cnt, ble_gap_irk_t *p_irks, - uint32_t *p_irk_cnt) +uint32_t im_whitelist_get(ble_gap_addr_t *addrs, uint32_t *addr_cnt, ble_gap_irk_t *irks, + uint32_t *irk_cnt) { /* One of the two buffers has to be provided. */ - NRF_PM_DEBUG_CHECK((p_addrs != NULL) || (p_irks != NULL)); - NRF_PM_DEBUG_CHECK((p_addr_cnt != NULL) || (p_irk_cnt != NULL)); + __ASSERT_NO_MSG((addrs != NULL) || (irks != NULL)); + __ASSERT_NO_MSG((addr_cnt != NULL) || (irk_cnt != NULL)); - if (((p_addr_cnt != NULL) && (m_wlisted_peer_cnt > *p_addr_cnt)) || - ((p_irk_cnt != NULL) && (m_wlisted_peer_cnt > *p_irk_cnt))) { + if (((addr_cnt != NULL) && (wlisted_peer_cnt > *addr_cnt)) || + ((irk_cnt != NULL) && (wlisted_peer_cnt > *irk_cnt))) { /* The size of the cached list of peers is larger than the provided buffers. */ return NRF_ERROR_NO_MEM; } @@ -556,8 +557,8 @@ uint32_t im_whitelist_get(ble_gap_addr_t *p_addrs, uint32_t *p_addr_cnt, ble_gap * NRF_ERROR_NOT_FOUND, if a peer or its data were not found. * BLE_ERROR_GAP_INVALID_BLE_ADDR, if a peer address can not be used for whitelisting. */ - return peers_id_keys_get(m_wlisted_peers, m_wlisted_peer_cnt, p_addrs, p_addr_cnt, p_irks, - p_irk_cnt); + return peers_id_keys_get(wlisted_peers, wlisted_peer_cnt, addrs, addr_cnt, irks, + irk_cnt); } /* Copies the peers to whitelist into a local cache. @@ -565,14 +566,14 @@ uint32_t im_whitelist_get(ble_gap_addr_t *p_addrs, uint32_t *p_addr_cnt, ble_gap * For SoftDevices 3x, also loads the peers' GAP addresses and whitelists them using * sd_ble_gap_whitelist_set(). */ -uint32_t im_whitelist_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt) +uint32_t im_whitelist_set(const uint16_t *peers, uint32_t peer_cnt) { /* Clear the cache of whitelisted peers. */ - memset(m_wlisted_peers, 0x00, sizeof(m_wlisted_peers)); + memset(wlisted_peers, 0x00, sizeof(wlisted_peers)); - if ((p_peers == NULL) || (peer_cnt == 0)) { + if ((peers == NULL) || (peer_cnt == 0)) { /* Clear the current whitelist. */ - m_wlisted_peer_cnt = 0; + wlisted_peer_cnt = 0; /* NRF_SUCCESS, or * BLE_GAP_ERROR_WHITELIST_IN_USE @@ -581,19 +582,19 @@ uint32_t im_whitelist_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt) } /* Copy the new whitelisted peers. */ - m_wlisted_peer_cnt = peer_cnt; - memcpy(m_wlisted_peers, p_peers, sizeof(pm_peer_id_t) * peer_cnt); + wlisted_peer_cnt = peer_cnt; + memcpy(wlisted_peers, peers, sizeof(*peers) * peer_cnt); uint32_t ret; - uint32_t wlist_addr_cnt = 0; + uint32_t wlist_addr_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT; - ble_gap_addr_t const *addr_ptrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; + const ble_gap_addr_t *addr_ptrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; ble_gap_addr_t addrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; memset(addrs, 0x00, sizeof(addrs)); /* Fetch GAP addresses for these peers, but don't fetch IRKs. */ - ret = peers_id_keys_get(p_peers, peer_cnt, addrs, &wlist_addr_cnt, NULL, NULL); + ret = peers_id_keys_get(peers, peer_cnt, addrs, &wlist_addr_cnt, NULL, NULL); if (ret != NRF_SUCCESS) { /* NRF_ERROR_NOT_FOUND, if a peer or its data were not found. @@ -624,54 +625,54 @@ uint32_t im_whitelist_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt) * @note The ECB expect little endian input and output. * This function expect big endian and will reverse the data as necessary. * - * @param[in] p_k The key used in the hash function. - * For address resolution this is should be the irk. - * The array must have a length of 16. - * @param[in] p_r The rand used in the hash function. For generating a new address - * this would be a random number. For resolving a resolvable address - * this would be the last half of the address being resolved. - * The array must have a length of 3. - * @param[out] p_local_hash The result of the hash operation. For address resolution this - * will match the first half of the address being resolved if and only - * if the irk used in the hash function is the same one used to generate - * the address. - * The array must have a length of 16. + * @param[in] key The key used in the hash function. + * For address resolution this is should be the irk. + * The array must have a length of 16. + * @param[in] rand The rand used in the hash function. For generating a new address + * this would be a random number. For resolving a resolvable address + * this would be the last half of the address being resolved. + * The array must have a length of 3. + * @param[out] local_hash The result of the hash operation. For address resolution this + * will match the first half of the address being resolved if and only + * if the irk used in the hash function is the same one used to generate + * the address. + * The array must have a length of 16. */ -void ah(uint8_t const *p_k, uint8_t const *p_r, uint8_t *p_local_hash) +void ah(const uint8_t *key, const uint8_t *rand, uint8_t *local_hash) { nrf_ecb_hal_data_t ecb_hal_data; for (uint32_t i = 0; i < SOC_ECB_KEY_LENGTH; i++) { - ecb_hal_data.key[i] = p_k[SOC_ECB_KEY_LENGTH - 1 - i]; + ecb_hal_data.key[i] = key[SOC_ECB_KEY_LENGTH - 1 - i]; } memset(ecb_hal_data.cleartext, 0, SOC_ECB_KEY_LENGTH - IM_ADDR_CLEARTEXT_LENGTH); for (uint32_t i = 0; i < IM_ADDR_CLEARTEXT_LENGTH; i++) { - ecb_hal_data.cleartext[SOC_ECB_KEY_LENGTH - 1 - i] = p_r[i]; + ecb_hal_data.cleartext[SOC_ECB_KEY_LENGTH - 1 - i] = rand[i]; } /* Can only return NRF_SUCCESS. */ (void)sd_ecb_block_encrypt(&ecb_hal_data); for (uint32_t i = 0; i < IM_ADDR_CIPHERTEXT_LENGTH; i++) { - p_local_hash[i] = ecb_hal_data.ciphertext[SOC_ECB_KEY_LENGTH - 1 - i]; + local_hash[i] = ecb_hal_data.ciphertext[SOC_ECB_KEY_LENGTH - 1 - i]; } } -bool im_address_resolve(ble_gap_addr_t const *p_addr, ble_gap_irk_t const *p_irk) +bool im_address_resolve(const ble_gap_addr_t *addr, const ble_gap_irk_t *irk) { uint8_t hash[IM_ADDR_CIPHERTEXT_LENGTH]; uint8_t local_hash[IM_ADDR_CIPHERTEXT_LENGTH]; uint8_t prand[IM_ADDR_CLEARTEXT_LENGTH]; - if (p_addr->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) { + if (addr->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) { return false; } - memcpy(hash, p_addr->addr, IM_ADDR_CIPHERTEXT_LENGTH); - memcpy(prand, &p_addr->addr[IM_ADDR_CIPHERTEXT_LENGTH], IM_ADDR_CLEARTEXT_LENGTH); - ah(p_irk->irk, prand, local_hash); + memcpy(hash, addr->addr, IM_ADDR_CIPHERTEXT_LENGTH); + memcpy(prand, &addr->addr[IM_ADDR_CIPHERTEXT_LENGTH], IM_ADDR_CLEARTEXT_LENGTH); + ah(irk->irk, prand, local_hash); return (memcmp(hash, local_hash, IM_ADDR_CIPHERTEXT_LENGTH) == 0); } diff --git a/lib/bluetooth/peer_manager/modules/nrf_ble_lesc.c b/lib/bluetooth/peer_manager/modules/nrf_ble_lesc.c index 4ac5cc0523..a2fbef19f2 100644 --- a/lib/bluetooth/peer_manager/modules/nrf_ble_lesc.c +++ b/lib/bluetooth/peer_manager/modules/nrf_ble_lesc.c @@ -20,7 +20,7 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); /** @brief Descriptor of the peer public key. */ -typedef struct { +struct lesc_peer_pub_key { /** @brief Peer public key. Stored in little-endian. */ uint8_t value[BLE_GAP_LESC_P256_PK_LEN]; /** @brief Peer connection handle. */ @@ -33,7 +33,7 @@ typedef struct { bool passkey_requested; /** @brief Flag indicating that the passkey display event has been received. */ bool passkey_displayed; -} nrf_ble_lesc_peer_pub_key_t; +}; /** * @brief The maximum number of peripheral and central connections combined. @@ -42,24 +42,24 @@ typedef struct { #define NRF_BLE_LESC_LINK_COUNT CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT /** LESC ECC Public Key. */ -__ALIGN(4) static ble_gap_lesc_p256_pk_t m_lesc_public_key; +__ALIGN(4) static ble_gap_lesc_p256_pk_t lesc_public_key; /** LESC ECC DH Key. */ -__ALIGN(4) static ble_gap_lesc_dhkey_t m_lesc_dh_key; +__ALIGN(4) static ble_gap_lesc_dhkey_t lesc_dh_key; /** Flag indicating that the module encountered an internal error. */ -static bool m_ble_lesc_internal_error; +static bool ble_lesc_internal_error; /** Flag indicating that the local ECDH key pair was generated. */ -static bool m_keypair_generated; +static bool keypair_generated; /** ID of ECC private/public key pair. */ -static psa_key_id_t m_keypair_id; +static psa_key_id_t keypair_id; /** Array of pointers to peer public keys, used for LESC DH generation. */ -static nrf_ble_lesc_peer_pub_key_t m_peer_keys[NRF_BLE_LESC_LINK_COUNT]; +static struct lesc_peer_pub_key peer_keys[NRF_BLE_LESC_LINK_COUNT]; -static bool m_lesc_oobd_own_generated; +static bool lesc_oobd_own_generated; /** LESC OOB data used in LESC OOB pairing mode. */ -static ble_gap_lesc_oob_data_t m_ble_lesc_oobd_own; -static nrf_ble_lesc_peer_oob_data_handler m_lesc_oobd_peer_handler; +static ble_gap_lesc_oob_data_t ble_lesc_oobd_own; +static nrf_ble_lesc_peer_oob_data_handler lesc_oobd_peer_handler; #define ECC_PUB_KEY_UNCOMPRESSED_FORMAT_MARKER 0x04 #define ECC_PUB_KEY_EXPORT_SIZE \ @@ -81,7 +81,7 @@ uint32_t nrf_ble_lesc_init(void) { psa_status_t status; - memset((void *)m_peer_keys, 0, sizeof(m_peer_keys)); + memset((void *)peer_keys, 0, sizeof(peer_keys)); /* Ensure that psa_crypto has been initialized. */ status = psa_crypto_init(); @@ -93,8 +93,8 @@ uint32_t nrf_ble_lesc_init(void) LOG_DBG("Initialized nrf_ble_lesc."); /* Reset module state. */ - m_ble_lesc_internal_error = false; - m_keypair_generated = false; + ble_lesc_internal_error = false; + keypair_generated = false; /* Generate ECC key pair. Only one key pair is automatically generated by this module. */ return nrf_ble_lesc_keypair_generate(); @@ -107,22 +107,22 @@ uint32_t nrf_ble_lesc_keypair_generate(void) size_t pub_key_len = 0; /* Check if any DH computation is pending */ - for (uint16_t i = 0; i < ARRAY_SIZE(m_peer_keys); i++) { - if (m_peer_keys[i].is_valid) { + for (uint16_t i = 0; i < ARRAY_SIZE(peer_keys); i++) { + if (peer_keys[i].is_valid) { return NRF_ERROR_BUSY; } } /* Update flag to indicate that there is no valid private key. */ - m_keypair_generated = false; - m_lesc_oobd_own_generated = false; + keypair_generated = false; + lesc_oobd_own_generated = false; /* Destroy the previous key pair (if any), to free up memory. */ - status = psa_destroy_key(m_keypair_id); + status = psa_destroy_key(keypair_id); if (status != PSA_SUCCESS && status != PSA_ERROR_INVALID_HANDLE) { LOG_ERR("psa_destroy_key returned status %d", status); } else { - m_keypair_id = 0; + keypair_id = 0; } LOG_DBG("Generating ECC key pair"); @@ -139,14 +139,14 @@ uint32_t nrf_ble_lesc_keypair_generate(void) psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); psa_set_key_bits(&key_attributes, 256); - status = psa_generate_key(&key_attributes, &m_keypair_id); + status = psa_generate_key(&key_attributes, &keypair_id); if (status != PSA_SUCCESS) { LOG_ERR("psa_generate_key() returned status %d", status); return NRF_ERROR_INTERNAL; } /* Export the raw representation of the public key. */ - status = psa_export_public_key(m_keypair_id, pub_key, sizeof(pub_key), &pub_key_len); + status = psa_export_public_key(keypair_id, pub_key, sizeof(pub_key), &pub_key_len); if (status != PSA_SUCCESS) { LOG_ERR("psa_export_public_key() returned status %d", status); return NRF_ERROR_INTERNAL; @@ -157,7 +157,7 @@ uint32_t nrf_ble_lesc_keypair_generate(void) size_t priv_key_len = 0; LOG_WRN("CONFIG_PM_LESC_PRIVATE_KEY_EXPORT is not to be used in production!"); - status = psa_export_key(m_keypair_id, priv_key, sizeof(priv_key), &priv_key_len); + status = psa_export_key(keypair_id, priv_key, sizeof(priv_key), &priv_key_len); if (status != PSA_SUCCESS) { LOG_ERR("psa_export_key() returned status %d", status); } else { @@ -174,10 +174,10 @@ uint32_t nrf_ble_lesc_keypair_generate(void) /* Convert from big-endian to little-endian. * Drop the first byte indicating the serialization format. */ - ecc_public_key_byte_order_invert(&pub_key[1], m_lesc_public_key.pk); + ecc_public_key_byte_order_invert(&pub_key[1], lesc_public_key.pk); /* Set the flag to indicate that there is a valid ECDH key pair generated. */ - m_keypair_generated = true; + keypair_generated = true; return NRF_SUCCESS; } @@ -186,13 +186,13 @@ uint32_t nrf_ble_lesc_own_oob_data_generate(void) { uint32_t err_code = NRF_ERROR_INVALID_STATE; - m_lesc_oobd_own_generated = false; + lesc_oobd_own_generated = false; - if (m_keypair_generated) { - err_code = sd_ble_gap_lesc_oob_data_get(BLE_CONN_HANDLE_INVALID, &m_lesc_public_key, - &m_ble_lesc_oobd_own); + if (keypair_generated) { + err_code = sd_ble_gap_lesc_oob_data_get(BLE_CONN_HANDLE_INVALID, &lesc_public_key, + &ble_lesc_oobd_own); if (err_code == NRF_SUCCESS) { - m_lesc_oobd_own_generated = true; + lesc_oobd_own_generated = true; } } @@ -201,78 +201,78 @@ uint32_t nrf_ble_lesc_own_oob_data_generate(void) ble_gap_lesc_p256_pk_t *nrf_ble_lesc_public_key_get(void) { - ble_gap_lesc_p256_pk_t *p_lesc_pk = NULL; + ble_gap_lesc_p256_pk_t *lesc_pk = NULL; - if (m_keypair_generated) { - p_lesc_pk = &m_lesc_public_key; + if (keypair_generated) { + lesc_pk = &lesc_public_key; } else { LOG_ERR("Trying to access LESC public key that has not been generated yet."); } - return p_lesc_pk; + return lesc_pk; } ble_gap_lesc_oob_data_t *nrf_ble_lesc_own_oob_data_get(void) { - ble_gap_lesc_oob_data_t *p_lesc_oobd_own = NULL; + ble_gap_lesc_oob_data_t *lesc_oobd_own = NULL; - if (m_lesc_oobd_own_generated) { - p_lesc_oobd_own = &m_ble_lesc_oobd_own; + if (lesc_oobd_own_generated) { + lesc_oobd_own = &ble_lesc_oobd_own; } else { LOG_ERR("Trying to access LESC OOB data that have not been generated yet."); } - return p_lesc_oobd_own; + return lesc_oobd_own; } void nrf_ble_lesc_peer_oob_data_handler_set(nrf_ble_lesc_peer_oob_data_handler handler) { - m_lesc_oobd_peer_handler = handler; + lesc_oobd_peer_handler = handler; } /** * @brief Function for calculating a DH key and responding to the DH key request. * - * @param[in] p_peer_public_key ECC peer public key, used to compute shared secret. + * @param[in] peer_public_key ECC peer public key, used to compute shared secret. * * @retval NRF_SUCCESS If the operation was successful. * @retval NRF_ERROR_INTERNAL If @ref psa_raw_key_agreement, or @ref psa_generate_random failed. * @returns Other error codes might be returned by @ref sd_ble_gap_lesc_dhkey_reply. * functions. */ -static uint32_t compute_and_give_dhkey(nrf_ble_lesc_peer_pub_key_t *p_peer_public_key) +static uint32_t compute_and_give_dhkey(struct lesc_peer_pub_key *peer_public_key) { psa_status_t status = PSA_ERROR_BAD_STATE; - uint8_t *p_shared_secret = m_lesc_dh_key.key; + uint8_t *shared_secret = lesc_dh_key.key; size_t shared_secret_size; uint8_t pub_key[ECC_PUB_KEY_EXPORT_SIZE]; /* Check if there is a valid generated and set a local ECDH public key. */ - if (!m_keypair_generated) { + if (!keypair_generated) { return NRF_ERROR_INTERNAL; } /* Check if the public_key is valid */ - if (p_peer_public_key->is_valid) { + if (peer_public_key->is_valid) { /* Add the uncompressed format marker. */ pub_key[0] = ECC_PUB_KEY_UNCOMPRESSED_FORMAT_MARKER; /* Convert from little-endian to big-endian. */ - ecc_public_key_byte_order_invert(p_peer_public_key->value, &pub_key[1]); + ecc_public_key_byte_order_invert(peer_public_key->value, &pub_key[1]); - status = psa_raw_key_agreement(PSA_ALG_ECDH, m_keypair_id, + status = psa_raw_key_agreement(PSA_ALG_ECDH, keypair_id, pub_key, sizeof(pub_key), - p_shared_secret, BLE_GAP_LESC_DHKEY_LEN, &shared_secret_size); + shared_secret, BLE_GAP_LESC_DHKEY_LEN, &shared_secret_size); } if ((status == PSA_SUCCESS) && (shared_secret_size == BLE_GAP_LESC_DHKEY_LEN)) { /* Convert secret from big-endian to little-endian. */ - sys_mem_swap(p_shared_secret, BLE_GAP_LESC_DHKEY_LEN); + sys_mem_swap(shared_secret, BLE_GAP_LESC_DHKEY_LEN); } else { LOG_ERR("psa_raw_key_agreement() returned status %d", status); LOG_WRN("Creating invalid shared secret to make LESC fail."); - status = psa_generate_random(p_shared_secret, BLE_GAP_LESC_DHKEY_LEN); + status = psa_generate_random(shared_secret, BLE_GAP_LESC_DHKEY_LEN); if (status != PSA_SUCCESS) { LOG_ERR("psa_generate_random() returned status %d", status); return NRF_ERROR_INTERNAL; @@ -280,9 +280,9 @@ static uint32_t compute_and_give_dhkey(nrf_ble_lesc_peer_pub_key_t *p_peer_publi } LOG_INF("Calling sd_ble_gap_lesc_dhkey_reply on conn_handle: %d", - p_peer_public_key->conn_handle); + peer_public_key->conn_handle); - return sd_ble_gap_lesc_dhkey_reply(p_peer_public_key->conn_handle, &m_lesc_dh_key); + return sd_ble_gap_lesc_dhkey_reply(peer_public_key->conn_handle, &lesc_dh_key); } uint32_t nrf_ble_lesc_request_handler(void) @@ -290,17 +290,17 @@ uint32_t nrf_ble_lesc_request_handler(void) uint32_t err_code; /* If the LESC module is in an invalid state, a restart is required. */ - if (m_ble_lesc_internal_error) { + if (ble_lesc_internal_error) { return NRF_ERROR_INTERNAL; } for (uint16_t i = 0; i < NRF_BLE_LESC_LINK_COUNT; i++) { - if (m_peer_keys[i].is_requested) { - err_code = compute_and_give_dhkey(&m_peer_keys[i]); - m_peer_keys[i].is_requested = false; - m_peer_keys[i].is_valid = false; - m_peer_keys[i].passkey_requested = false; - m_peer_keys[i].passkey_displayed = false; + if (peer_keys[i].is_requested) { + err_code = compute_and_give_dhkey(&peer_keys[i]); + peer_keys[i].is_requested = false; + peer_keys[i].is_valid = false; + peer_keys[i].passkey_requested = false; + peer_keys[i].passkey_displayed = false; if (err_code != NRF_SUCCESS) { return err_code; @@ -314,32 +314,32 @@ uint32_t nrf_ble_lesc_request_handler(void) /** * @brief Function for handling a DH key request event. * - * @param[in] conn_handle Connection handle. - * @param[in] idx Data index assigned to the connection handle. - * @param[in] p_dhkey_request DH key request descriptor. + * @param[in] conn_handle Connection handle. + * @param[in] idx Data index assigned to the connection handle. + * @param[in] dhkey_request DH key request descriptor. * * @retval NRF_SUCCESS If the operation was successful. * @returns Other error codes might be returned by @ref sd_ble_gap_auth_key_reply, and * @ref sd_ble_gap_disconnect. */ static uint32_t on_dhkey_request(uint16_t conn_handle, int idx, - ble_gap_evt_lesc_dhkey_request_t const *p_dhkey_request) + const ble_gap_evt_lesc_dhkey_request_t *dhkey_request) { uint32_t err_code = NRF_SUCCESS; - const uint8_t *const p_public_raw = p_dhkey_request->p_pk_peer->pk; + const uint8_t *const public_raw = dhkey_request->p_pk_peer->pk; /* Don't allow to pair with remote peer which uses the same public key. * Compare only X cordinate of the public key, bytes from 0 to 31. */ - if (memcmp(m_lesc_public_key.pk, p_public_raw, BLE_GAP_LESC_P256_PK_LEN / 2) == 0) { + if (memcmp(lesc_public_key.pk, public_raw, BLE_GAP_LESC_P256_PK_LEN / 2) == 0) { LOG_WRN("Remote peer is using identical public key."); - m_peer_keys[idx].is_valid = false; + peer_keys[idx].is_valid = false; /* In case when we have gotten passkey requested then we will respond to it with the * "NONE" key type to prevent us from going through Authentication Stage 1. */ - if (m_peer_keys[idx].passkey_requested) { - m_peer_keys[idx].passkey_requested = false; + if (peer_keys[idx].passkey_requested) { + peer_keys[idx].passkey_requested = false; err_code = sd_ble_gap_auth_key_reply(conn_handle, BLE_GAP_AUTH_KEY_TYPE_NONE, NULL); @@ -350,8 +350,8 @@ static uint32_t on_dhkey_request(uint16_t conn_handle, int idx, /* In case we have gotten passkey display event then we need to disconnect a link * to prevent us from going through Authentication Stage 1. */ - else if (m_peer_keys[idx].passkey_displayed) { - m_peer_keys[idx].passkey_displayed = false; + else if (peer_keys[idx].passkey_displayed) { + peer_keys[idx].passkey_displayed = false; err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); @@ -362,12 +362,12 @@ static uint32_t on_dhkey_request(uint16_t conn_handle, int idx, return NRF_SUCCESS; } } else { - memcpy(m_peer_keys[idx].value, p_public_raw, BLE_GAP_LESC_P256_PK_LEN); - m_peer_keys[idx].conn_handle = conn_handle; - m_peer_keys[idx].is_valid = true; + memcpy(peer_keys[idx].value, public_raw, BLE_GAP_LESC_P256_PK_LEN); + peer_keys[idx].conn_handle = conn_handle; + peer_keys[idx].is_valid = true; } - m_peer_keys[idx].is_requested = true; + peer_keys[idx].is_requested = true; return NRF_SUCCESS; } @@ -383,64 +383,64 @@ static uint32_t on_dhkey_request(uint16_t conn_handle, int idx, static uint32_t lesc_oob_data_set(uint16_t conn_handle) { uint32_t err_code; - ble_gap_lesc_oob_data_t *p_lesc_oobd_own; - ble_gap_lesc_oob_data_t *p_lesc_oobd_peer; + ble_gap_lesc_oob_data_t *lesc_oobd_own; + ble_gap_lesc_oob_data_t *lesc_oobd_peer; - p_lesc_oobd_own = (m_lesc_oobd_own_generated) ? &m_ble_lesc_oobd_own : NULL; - p_lesc_oobd_peer = - (m_lesc_oobd_peer_handler != NULL) ? m_lesc_oobd_peer_handler(conn_handle) : NULL; + lesc_oobd_own = (lesc_oobd_own_generated) ? &ble_lesc_oobd_own : NULL; + lesc_oobd_peer = + (lesc_oobd_peer_handler != NULL) ? lesc_oobd_peer_handler(conn_handle) : NULL; - err_code = sd_ble_gap_lesc_oob_data_set(conn_handle, p_lesc_oobd_own, p_lesc_oobd_peer); + err_code = sd_ble_gap_lesc_oob_data_set(conn_handle, lesc_oobd_own, lesc_oobd_peer); return err_code; } -void nrf_ble_lesc_on_ble_evt(ble_evt_t const *p_ble_evt) +void nrf_ble_lesc_on_ble_evt(const ble_evt_t *ble_evt) { uint32_t err_code = NRF_SUCCESS; - const uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle; + const uint16_t conn_handle = ble_evt->evt.gap_evt.conn_handle; const int idx = nrf_sdh_ble_idx_get(conn_handle); __ASSERT(idx >= 0, "Invalid idx %d for conn_handle %#x, evt_id %#x", - idx, conn_handle, p_ble_evt->header.evt_id); + idx, conn_handle, ble_evt->header.evt_id); - switch (p_ble_evt->header.evt_id) { + switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_DISCONNECTED: - m_peer_keys[idx].is_valid = false; - m_peer_keys[idx].is_requested = false; - m_peer_keys[idx].passkey_requested = false; - m_peer_keys[idx].passkey_displayed = false; + peer_keys[idx].is_valid = false; + peer_keys[idx].is_requested = false; + peer_keys[idx].passkey_requested = false; + peer_keys[idx].passkey_displayed = false; break; case BLE_GAP_EVT_AUTH_KEY_REQUEST: - if (p_ble_evt->evt.gap_evt.params.auth_key_request.key_type == + if (ble_evt->evt.gap_evt.params.auth_key_request.key_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY) { - m_peer_keys[idx].passkey_requested = true; + peer_keys[idx].passkey_requested = true; } break; case BLE_GAP_EVT_PASSKEY_DISPLAY: - m_peer_keys[idx].passkey_displayed = true; + peer_keys[idx].passkey_displayed = true; break; case BLE_GAP_EVT_LESC_DHKEY_REQUEST: LOG_DBG("BLE_GAP_EVT_LESC_DHKEY_REQUEST"); - if (p_ble_evt->evt.gap_evt.params.lesc_dhkey_request.oobd_req) { + if (ble_evt->evt.gap_evt.params.lesc_dhkey_request.oobd_req) { err_code = lesc_oob_data_set(conn_handle); if (err_code != NRF_SUCCESS) { LOG_ERR("sd_ble_gap_lesc_oob_data_set() returned error 0x%x.", err_code); - m_ble_lesc_internal_error = true; + ble_lesc_internal_error = true; } } err_code = on_dhkey_request(conn_handle, idx, - &p_ble_evt->evt.gap_evt.params.lesc_dhkey_request); + &ble_evt->evt.gap_evt.params.lesc_dhkey_request); if (err_code != NRF_SUCCESS) { - m_ble_lesc_internal_error = true; + ble_lesc_internal_error = true; } break; @@ -449,7 +449,7 @@ void nrf_ble_lesc_on_ble_evt(ble_evt_t const *p_ble_evt) /* Generate new pairing keys. */ err_code = nrf_ble_lesc_keypair_generate(); if (err_code != NRF_SUCCESS) { - m_ble_lesc_internal_error = true; + ble_lesc_internal_error = true; } break; diff --git a/lib/bluetooth/peer_manager/modules/peer_data_storage.c b/lib/bluetooth/peer_manager/modules/peer_data_storage.c index 8e8ed1ad1f..dd8088af10 100644 --- a/lib/bluetooth/peer_manager/modules/peer_data_storage.c +++ b/lib/bluetooth/peer_manager/modules/peer_data_storage.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -23,29 +23,21 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); -/* Macro for verifying that the peer id is within a valid range. */ -#define VERIFY_PEER_ID_IN_RANGE(id) \ - VERIFY_FALSE((id >= PM_PEER_ID_N_AVAILABLE_IDS), NRF_ERROR_INVALID_PARAM) - -/* Macro for verifying that the peer data id is within a valid range. */ -#define VERIFY_PEER_DATA_ID_IN_RANGE(id) \ - VERIFY_TRUE(peer_data_id_is_valid(id), NRF_ERROR_INVALID_PARAM) - /* The number of registered event handlers. */ -#define PDS_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define PDS_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* Peer Data Storage event handler in Peer Database. */ -extern void pdb_pds_evt_handler(pm_evt_t *evt); +extern void pdb_pds_evt_handler(struct pm_evt *evt); /* Peer Data Storage events' handlers. * The number of elements in this array is PDS_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t const m_evt_handlers[] = { +static const pm_evt_handler_internal_t evt_handlers[] = { pdb_pds_evt_handler, }; -static bool m_module_initialized; -static volatile bool m_peer_delete_deferred; +static bool module_initialized; +static volatile bool peer_delete_deferred; static struct bm_zms_fs fs; @@ -53,12 +45,12 @@ static struct bm_zms_fs fs; static atomic_t delete_counter; /* Function for dispatching events to all registered event handlers. */ -static void pds_evt_send(pm_evt_t *p_event) +static void pds_evt_send(struct pm_evt *event) { - p_event->conn_handle = BLE_CONN_HANDLE_INVALID; + event->conn_handle = BLE_CONN_HANDLE_INVALID; for (uint32_t i = 0; i < PDS_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_event); + evt_handlers[i](event); } } @@ -71,8 +63,7 @@ static void pds_evt_send(pm_evt_t *p_event) * @p peer_id is stored in the most significant 16 bits. * @p data_id is stored in the least significant 16 bits. */ -static uint32_t peer_id_peer_data_id_to_entry_id(pm_peer_id_t peer_id, - pm_peer_data_id_t data_id) +static uint32_t peer_id_peer_data_id_to_entry_id(uint16_t peer_id, enum pm_peer_data_id data_id) { return (peer_id << ENTRY_ID_PEER_ID_OFFSET_BITS) | (data_id & ENTRY_ID_DATA_ID_MASK); } @@ -80,14 +71,14 @@ static uint32_t peer_id_peer_data_id_to_entry_id(pm_peer_id_t peer_id, /** * @brief Unpack the given entry_id into a peer_id and data_id. */ -static void entry_id_to_peer_id_peer_data_id(uint32_t entry_id, pm_peer_id_t *peer_id, - pm_peer_data_id_t *data_id) +static void entry_id_to_peer_id_peer_data_id(uint32_t entry_id, uint16_t *peer_id, + enum pm_peer_data_id *data_id) { *data_id = entry_id & ENTRY_ID_DATA_ID_MASK; *peer_id = entry_id >> ENTRY_ID_PEER_ID_OFFSET_BITS; } -static bool peer_data_id_is_valid(pm_peer_data_id_t data_id) +static bool peer_data_id_is_valid(enum pm_peer_data_id data_id) { return ((data_id == PM_PEER_DATA_ID_BONDING) || (data_id == PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING) || @@ -104,23 +95,25 @@ static bool peer_data_id_is_valid(pm_peer_data_id_t data_id) * @param[in] peer_id The peer the event pertains to. * @param[in] err_code The unexpected error that occurred. */ -static void send_unexpected_error(pm_peer_id_t peer_id, uint32_t err_code) +static void send_unexpected_error(uint16_t peer_id, uint32_t err_code) { - pm_evt_t error_evt = {.evt_id = PM_EVT_ERROR_UNEXPECTED, - .peer_id = peer_id, - .params = {.error_unexpected = { - .error = err_code, - }}}; + struct pm_evt error_evt = { + .evt_id = PM_EVT_ERROR_UNEXPECTED, + .peer_id = peer_id, + .params.error_unexpected = { + .error = err_code, + }, + }; pds_evt_send(&error_evt); } /* Returns the next data entry or a negative errno. */ -static uint32_t find_next_data_entry_in_peer(pm_peer_id_t peer_id, uint32_t *next_entry_id) +static uint32_t find_next_data_entry_in_peer(uint16_t peer_id, uint32_t *next_entry_id) { ssize_t ret; uint8_t temp_buf[PM_PEER_DATA_MAX_SIZE] = { 0 }; - for (pm_peer_data_id_t i = 0; i < PM_PEER_DATA_ID_LAST; i++) { + for (enum pm_peer_data_id i = 0; i < PM_PEER_DATA_ID_LAST; i++) { uint32_t entry_id = peer_id_peer_data_id_to_entry_id(peer_id, i); ret = bm_zms_read(&fs, entry_id, temp_buf, sizeof(temp_buf)); @@ -148,10 +141,10 @@ static uint32_t find_next_data_entry_in_peer(pm_peer_id_t peer_id, uint32_t *nex static void peer_data_delete_process(void) { int err; - pm_peer_id_t peer_id; + uint16_t peer_id; uint32_t entry_id; - m_peer_delete_deferred = false; + peer_delete_deferred = false; peer_id = peer_id_get_next_deleted(PM_PEER_ID_INVALID); @@ -165,7 +158,7 @@ static void peer_data_delete_process(void) if (peer_id != PM_PEER_ID_INVALID) { err = bm_zms_delete(&fs, entry_id); if (err == -ENOMEM) { - m_peer_delete_deferred = true; + peer_delete_deferred = true; } else if (err < 0) { LOG_ERR("Could not delete peer data. bm_zms_delete() returned %d " "for peer_id: %d", err, peer_id); @@ -177,12 +170,12 @@ static void peer_data_delete_process(void) static void peer_ids_load(void) { - pm_peer_id_t peer_id; - pm_peer_id_t peer_id_iter; - pm_peer_data_flash_t peer_data = { 0 }; + uint16_t peer_id; + uint16_t peer_id_iter; + struct pm_peer_data_const peer_data = { 0 }; uint8_t peer_data_buffer[PM_PEER_DATA_MAX_SIZE] = { 0 }; - peer_data.p_all_data = peer_data_buffer; + peer_data.all_data = peer_data_buffer; /* Search through existing bonds to look for a duplicate. */ pds_peer_data_iterate_prepare(&peer_id_iter); @@ -193,31 +186,31 @@ static void peer_ids_load(void) } } -static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt) +static void bm_zms_evt_handler(const bm_zms_evt_t *evt) { - pm_peer_id_t peer_id; - pm_peer_data_id_t data_id; + uint16_t peer_id; + enum pm_peer_data_id data_id; - entry_id_to_peer_id_peer_data_id(p_evt->id, &peer_id, &data_id); + entry_id_to_peer_id_peer_data_id(evt->id, &peer_id, &data_id); - pm_evt_t pds_evt = { .peer_id = peer_id }; + struct pm_evt pds_evt = { .peer_id = peer_id }; - switch (p_evt->evt_id) { + switch (evt->evt_id) { case BM_ZMS_EVT_INIT: - if (p_evt->result) { - LOG_ERR("BM_ZMS initialization failed with error %d", p_evt->result); + if (evt->result) { + LOG_ERR("BM_ZMS initialization failed with error %d", evt->result); } break; case BM_ZMS_EVT_WRITE: pds_evt.params.peer_data_update_succeeded.data_id = data_id; pds_evt.params.peer_data_update_succeeded.action = PM_PEER_DATA_OP_UPDATE; - pds_evt.params.peer_data_update_succeeded.token = p_evt->id; + pds_evt.params.peer_data_update_succeeded.token = evt->id; - if (p_evt->result == 0) { + if (evt->result == 0) { pds_evt.evt_id = PM_EVT_PEER_DATA_UPDATE_SUCCEEDED; pds_evt.params.peer_data_update_succeeded.flash_changed = true; } else { - LOG_ERR("BM_ZMS write failed with error %d", p_evt->result); + LOG_ERR("BM_ZMS write failed with error %d", evt->result); pds_evt.evt_id = PM_EVT_PEER_DATA_UPDATE_FAILED; pds_evt.params.peer_data_update_failed.error = NRF_ERROR_INTERNAL; } @@ -228,24 +221,24 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt) if (!peer_id_is_deleted(peer_id)) { pds_evt.params.peer_data_update_succeeded.data_id = data_id; pds_evt.params.peer_data_update_succeeded.action = PM_PEER_DATA_OP_DELETE; - pds_evt.params.peer_data_update_succeeded.token = p_evt->id; + pds_evt.params.peer_data_update_succeeded.token = evt->id; - if (p_evt->result == 0) { + if (evt->result == 0) { pds_evt.evt_id = PM_EVT_PEER_DATA_UPDATE_SUCCEEDED; pds_evt.params.peer_data_update_succeeded.flash_changed = true; } else { - LOG_ERR("BM_ZMS delete failed with error %d", p_evt->result); + LOG_ERR("BM_ZMS delete failed with error %d", evt->result); pds_evt.evt_id = PM_EVT_PEER_DATA_UPDATE_FAILED; pds_evt.params.peer_data_update_failed.error = NRF_ERROR_INTERNAL; } pds_evt_send(&pds_evt); } else { - if (p_evt->result == -ENOMEM) { - m_peer_delete_deferred = true; - } else if (p_evt->result < 0) { + if (evt->result == -ENOMEM) { + peer_delete_deferred = true; + } else if (evt->result < 0) { /* Unrecoverable error. */ - LOG_ERR("BM_ZMS delete failed with error %d", p_evt->result); + LOG_ERR("BM_ZMS delete failed with error %d", evt->result); atomic_dec(&delete_counter); @@ -259,12 +252,12 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt) ret = find_next_data_entry_in_peer(peer_id, &next_entry_id); if (ret == NRF_SUCCESS) { /* Process the next entry for the peer. */ - m_peer_delete_deferred = true; + peer_delete_deferred = true; } else if (ret == NRF_ERROR_NOT_FOUND) { atomic_dec(&delete_counter); /* Process the next deleted peers, if any are present. */ - m_peer_delete_deferred = true; + peer_delete_deferred = true; pds_evt.evt_id = PM_EVT_PEER_DELETE_SUCCEEDED; peer_id_free(pds_evt.peer_id); @@ -279,7 +272,7 @@ static void bm_zms_evt_handler(bm_zms_evt_t const *p_evt) break; } - if (m_peer_delete_deferred) { + if (peer_delete_deferred) { peer_data_delete_process(); } } @@ -296,18 +289,18 @@ static void wait_for_init(void) } } -void pds_peer_data_iterate_prepare(pm_peer_id_t *p_peer_id_iter) +void pds_peer_data_iterate_prepare(uint16_t *peer_id_iter) { - *p_peer_id_iter = 0; + *peer_id_iter = 0; } -bool pds_peer_data_iterate(pm_peer_data_id_t data_id, pm_peer_id_t *const p_peer_id, - pm_peer_data_flash_t *const p_data, pm_peer_id_t *p_peer_id_iter) +bool pds_peer_data_iterate(enum pm_peer_data_id data_id, uint16_t *const peer_id, + struct pm_peer_data_const *const data, uint16_t *peer_id_iter) { ssize_t ret; uint8_t temp_buf[PM_PEER_DATA_MAX_SIZE] = { 0 }; - if (*p_peer_id_iter >= PM_PEER_ID_N_AVAILABLE_IDS) { + if (*peer_id_iter >= PM_PEER_ID_N_AVAILABLE_IDS) { return false; } @@ -315,32 +308,32 @@ bool pds_peer_data_iterate(pm_peer_data_id_t data_id, pm_peer_id_t *const p_peer * or the read had a catastrophical failure. */ do { - uint32_t entry_id = peer_id_peer_data_id_to_entry_id(*p_peer_id_iter, data_id); + uint32_t entry_id = peer_id_peer_data_id_to_entry_id(*peer_id_iter, data_id); ret = bm_zms_read(&fs, entry_id, temp_buf, sizeof(temp_buf)); if (ret < 0 && ret != -ENOENT) { LOG_ERR("Could not read data from NVM. bm_zms_read() returned %d. " "peer_id: %d", - ret, *p_peer_id_iter); + ret, *peer_id_iter); return false; } - (*p_peer_id_iter)++; - } while ((ret == -ENOENT) && (*p_peer_id_iter < PM_PEER_ID_N_AVAILABLE_IDS)); + (*peer_id_iter)++; + } while ((ret == -ENOENT) && (*peer_id_iter < PM_PEER_ID_N_AVAILABLE_IDS)); - if ((ret == -ENOENT) && (*p_peer_id_iter == PM_PEER_ID_N_AVAILABLE_IDS)) { + if ((ret == -ENOENT) && (*peer_id_iter == PM_PEER_ID_N_AVAILABLE_IDS)) { return false; } /* We found a suitable Peer ID. */ /* `p_peer_id_iter` counts the iterations, so the Peer ID is iterations minus one. */ - *p_peer_id = (*p_peer_id_iter) - 1; + *peer_id = (*peer_id_iter) - 1; /* `ret` is equal the exact amount of data contained in the entry, so copy that amount * safely. */ - memcpy((void *)p_data->p_all_data, temp_buf, ret); + memcpy((void *)data->all_data, temp_buf, ret); return true; } @@ -350,7 +343,7 @@ uint32_t pds_init(void) int err; /* Check for re-initialization if debugging. */ - NRF_PM_DEBUG_CHECK(!m_module_initialized); + __ASSERT_NO_MSG(!module_initialized); err = bm_zms_register(&fs, bm_zms_evt_handler); if (err) { @@ -372,23 +365,28 @@ uint32_t pds_init(void) peer_id_init(); peer_ids_load(); - m_module_initialized = true; + module_initialized = true; return NRF_SUCCESS; } -uint32_t pds_peer_data_read(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, - pm_peer_data_t *const p_data, uint32_t const *const p_buf_len) +uint32_t pds_peer_data_read(uint16_t peer_id, enum pm_peer_data_id data_id, + struct pm_peer_data *const data, const uint32_t *const buf_len) { ssize_t ret; uint8_t temp_buf[PM_PEER_DATA_MAX_SIZE] = { 0 }; - NRF_PM_DEBUG_CHECK(m_module_initialized); - NRF_PM_DEBUG_CHECK(p_data != NULL); - NRF_PM_DEBUG_CHECK(p_buf_len != NULL); + __ASSERT_NO_MSG(module_initialized); + __ASSERT_NO_MSG(data != NULL); + __ASSERT_NO_MSG(buf_len != NULL); - VERIFY_PEER_ID_IN_RANGE(peer_id); - VERIFY_PEER_DATA_ID_IN_RANGE(data_id); + if (peer_id >= PM_PEER_ID_N_AVAILABLE_IDS) { + return NRF_ERROR_INVALID_PARAM; + } + + if (!peer_data_id_is_valid(data_id)) { + return NRF_ERROR_INVALID_PARAM; + } uint32_t entry_id = peer_id_peer_data_id_to_entry_id(peer_id, data_id); @@ -405,30 +403,35 @@ uint32_t pds_peer_data_read(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, return NRF_ERROR_INTERNAL; } - memcpy(p_data->p_all_data, temp_buf, MIN(*p_buf_len, ret)); + memcpy(data->all_data, temp_buf, MIN(*buf_len, ret)); - if (*p_buf_len < ret) { + if (*buf_len < ret) { return NRF_ERROR_DATA_SIZE; } return NRF_SUCCESS; } -uint32_t pds_peer_data_store(pm_peer_id_t peer_id, pm_peer_data_const_t const *p_peer_data, - pm_store_token_t *p_store_token) +uint32_t pds_peer_data_store(uint16_t peer_id, const struct pm_peer_data_const *peer_data, + uint32_t *store_token) { ssize_t ret; - NRF_PM_DEBUG_CHECK(m_module_initialized); - NRF_PM_DEBUG_CHECK(p_peer_data != NULL); + __ASSERT_NO_MSG(module_initialized); + __ASSERT_NO_MSG(peer_data != NULL); - VERIFY_PEER_ID_IN_RANGE(peer_id); - VERIFY_PEER_DATA_ID_IN_RANGE(p_peer_data->data_id); + if (peer_id >= PM_PEER_ID_N_AVAILABLE_IDS) { + return NRF_ERROR_INVALID_PARAM; + } - uint32_t entry_id = peer_id_peer_data_id_to_entry_id(peer_id, p_peer_data->data_id); + if (!peer_data_id_is_valid(peer_data->data_id)) { + return NRF_ERROR_INVALID_PARAM; + } - ret = bm_zms_write(&fs, entry_id, p_peer_data->p_all_data, - p_peer_data->length_words * BYTES_PER_WORD); + uint32_t entry_id = peer_id_peer_data_id_to_entry_id(peer_id, peer_data->data_id); + + ret = bm_zms_write(&fs, entry_id, peer_data->all_data, + peer_data->length_words * BYTES_PER_WORD); if (ret < 0) { LOG_ERR("Could not write data to NVM. bm_zms_write() returned %d. " "peer_id: %d", @@ -436,22 +439,27 @@ uint32_t pds_peer_data_store(pm_peer_id_t peer_id, pm_peer_data_const_t const *p return NRF_ERROR_INTERNAL; } - if (p_store_token != NULL) { + if (store_token != NULL) { /* Update the store token. */ - *p_store_token = entry_id; + *store_token = entry_id; } return NRF_SUCCESS; } -uint32_t pds_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) +uint32_t pds_peer_data_delete(uint16_t peer_id, enum pm_peer_data_id data_id) { int err; - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); + + if (peer_id >= PM_PEER_ID_N_AVAILABLE_IDS) { + return NRF_ERROR_INVALID_PARAM; + } - VERIFY_PEER_ID_IN_RANGE(peer_id); - VERIFY_PEER_DATA_ID_IN_RANGE(data_id); + if (!peer_data_id_is_valid(data_id)) { + return NRF_ERROR_INVALID_PARAM; + } uint32_t entry_id = peer_id_peer_data_id_to_entry_id(peer_id, data_id); @@ -466,16 +474,19 @@ uint32_t pds_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) return NRF_SUCCESS; } -pm_peer_id_t pds_peer_id_allocate(void) +uint16_t pds_peer_id_allocate(void) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); return peer_id_allocate(PM_PEER_ID_INVALID); } -uint32_t pds_peer_id_free(pm_peer_id_t peer_id) +uint32_t pds_peer_id_free(uint16_t peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); - VERIFY_PEER_ID_IN_RANGE(peer_id); + __ASSERT_NO_MSG(module_initialized); + + if (peer_id >= PM_PEER_ID_N_AVAILABLE_IDS) { + return NRF_ERROR_INVALID_PARAM; + } (void)peer_id_delete(peer_id); @@ -490,32 +501,32 @@ uint32_t pds_peer_id_free(pm_peer_id_t peer_id) return NRF_SUCCESS; } -bool pds_peer_id_is_allocated(pm_peer_id_t peer_id) +bool pds_peer_id_is_allocated(uint16_t peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); return peer_id_is_allocated(peer_id); } -bool pds_peer_id_is_deleted(pm_peer_id_t peer_id) +bool pds_peer_id_is_deleted(uint16_t peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); return peer_id_is_deleted(peer_id); } -pm_peer_id_t pds_next_peer_id_get(pm_peer_id_t prev_peer_id) +uint16_t pds_next_peer_id_get(uint16_t prev_peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); return peer_id_get_next_used(prev_peer_id); } -pm_peer_id_t pds_next_deleted_peer_id_get(pm_peer_id_t prev_peer_id) +uint16_t pds_next_deleted_peer_id_get(uint16_t prev_peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); return peer_id_get_next_deleted(prev_peer_id); } uint32_t pds_peer_count_get(void) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); return peer_id_n_ids(); } diff --git a/lib/bluetooth/peer_manager/modules/peer_database.c b/lib/bluetooth/peer_manager/modules/peer_database.c index 8537747ad7..c498b0997b 100644 --- a/lib/bluetooth/peer_manager/modules/peer_database.c +++ b/lib/bluetooth/peer_manager/modules/peer_database.c @@ -7,9 +7,9 @@ #include #include #include +#include #include #include -#include #include #include #include @@ -33,21 +33,21 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); } while (0) /* The number of registered event handlers. */ -#define PDB_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define PDB_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* Peer Database event handlers in other Peer Manager submodules. */ -extern void pm_pdb_evt_handler(pm_evt_t *p_event); -extern void sm_pdb_evt_handler(pm_evt_t *p_event); +extern void pm_pdb_evt_handler(struct pm_evt *event); +extern void sm_pdb_evt_handler(struct pm_evt *event); #if defined(CONFIG_PM_SERVICE_CHANGED) -extern void gscm_pdb_evt_handler(pm_evt_t *p_event); +extern void gscm_pdb_evt_handler(struct pm_evt *event); #endif -extern void gcm_pdb_evt_handler(pm_evt_t *p_event); +extern void gcm_pdb_evt_handler(struct pm_evt *event); /** * @brief Peer Database events' handlers. * The number of elements in this array is PDB_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t const m_evt_handlers[] = { +static const pm_evt_handler_internal_t evt_handlers[] = { pm_pdb_evt_handler, sm_pdb_evt_handler, #if defined(CONFIG_PM_SERVICE_CHANGED) @@ -60,17 +60,17 @@ static pm_evt_handler_internal_t const m_evt_handlers[] = { * @brief Struct for keeping track of one write buffer, from allocation, until it is fully written * or cancelled. */ -typedef struct { +struct pdb_buffer_record { /** @brief The peer ID this buffer belongs to. */ - pm_peer_id_t peer_id; + uint16_t peer_id; /** @brief The data ID this buffer belongs to. */ - pm_peer_data_id_t data_id; + enum pm_peer_data_id data_id; /** * @brief Token given by Peer Data Storage when a flash write has been * successfully requested. This is used as the check for whether such * an operation has been successfully requested. */ - pm_store_token_t store_token; + uint32_t store_token; /** @brief The number of buffer blocks containing peer data. */ uint8_t n_bufs; /** @brief The index of the first (or only) buffer block containing peer data. */ @@ -86,50 +86,50 @@ typedef struct { * but a busy error was returned and the operation should be retried. */ uint8_t store_busy: 1; -} pdb_buffer_record_t; +}; -static bool m_module_initialized; +static bool module_initialized; /** @brief The internal states of the write buffer. */ -static pm_buffer_t m_write_buffer; +static struct pm_buffer write_buffer; /** @brief The available write buffer records. */ -static pdb_buffer_record_t m_write_buffer_records[CONFIG_PM_FLASH_BUFFERS]; +static struct pdb_buffer_record write_buffer_records[CONFIG_PM_FLASH_BUFFERS]; /** * @brief Whether there are any pending (Not yet successfully requested in Peer Data * Storage) store operations. This flag is for convenience only. The real bookkeeping - * is in the records (@ref m_write_buffer_records). + * is in the records (@ref write_buffer_records). */ -static bool m_pending_store; +static bool pending_store; /** * @brief Function for invalidating a record of a write buffer allocation. * - * @param[in] p_record The record to invalidate. + * @param[in] record The record to invalidate. */ -static void write_buffer_record_invalidate(pdb_buffer_record_t *p_record) +static void write_buffer_record_invalidate(struct pdb_buffer_record *record) { - p_record->peer_id = PM_PEER_ID_INVALID; - p_record->data_id = PM_PEER_DATA_ID_INVALID; - p_record->buffer_block_id = PM_BUFFER_INVALID_ID; - p_record->store_busy = false; - p_record->store_flash_full = false; - p_record->n_bufs = 0; - p_record->store_token = PM_STORE_TOKEN_INVALID; + record->peer_id = PM_PEER_ID_INVALID; + record->data_id = PM_PEER_DATA_ID_INVALID; + record->buffer_block_id = PM_BUFFER_INVALID_ID; + record->store_busy = false; + record->store_flash_full = false; + record->n_bufs = 0; + record->store_token = PM_STORE_TOKEN_INVALID; } /** * @brief Function for finding a record of a write buffer allocation. * * @param[in] peer_id The peer ID in the record. - * @param[inout] p_index In: The starting index, out: The index of the record + * @param[inout] index In: The starting index, out: The index of the record * * @return A pointer to the matching record, or NULL if none was found. */ -static pdb_buffer_record_t *write_buffer_record_find_next(pm_peer_id_t peer_id, uint32_t *p_index) +static struct pdb_buffer_record *write_buffer_record_find_next(uint16_t peer_id, uint32_t *index) { - for (uint32_t i = *p_index; i < CONFIG_PM_FLASH_BUFFERS; i++) { - if (m_write_buffer_records[i].peer_id == peer_id) { - *p_index = i; - return &m_write_buffer_records[i]; + for (uint32_t i = *index; i < CONFIG_PM_FLASH_BUFFERS; i++) { + if (write_buffer_records[i].peer_id == peer_id) { + *index = i; + return &write_buffer_records[i]; } } return NULL; @@ -143,20 +143,19 @@ static pdb_buffer_record_t *write_buffer_record_find_next(pm_peer_id_t peer_id, * * @return A pointer to the matching record, or NULL if none was found. */ -static pdb_buffer_record_t *write_buffer_record_find(pm_peer_id_t peer_id, - pm_peer_data_id_t data_id) +static struct pdb_buffer_record *write_buffer_record_find(uint16_t peer_id, enum pm_peer_data_id data_id) { uint32_t index = 0; - pdb_buffer_record_t *p_record = write_buffer_record_find_next(peer_id, &index); + struct pdb_buffer_record *record = write_buffer_record_find_next(peer_id, &index); - while ((p_record != NULL) && ((p_record->data_id != data_id) || (p_record->store_busy) || - (p_record->store_flash_full) || - (p_record->store_token != PM_STORE_TOKEN_INVALID))) { + while ((record != NULL) && ((record->data_id != data_id) || (record->store_busy) || + (record->store_flash_full) || + (record->store_token != PM_STORE_TOKEN_INVALID))) { index++; - p_record = write_buffer_record_find_next(peer_id, &index); + record = write_buffer_record_find_next(peer_id, &index); } - return p_record; + return record; } /** @@ -167,11 +166,11 @@ static pdb_buffer_record_t *write_buffer_record_find(pm_peer_id_t peer_id, * * @return A pointer to the matching record, or NULL if none was found. */ -static pdb_buffer_record_t *write_buffer_record_find_stored(pm_store_token_t store_token) +static struct pdb_buffer_record *write_buffer_record_find_stored(uint32_t store_token) { for (int i = 0; i < CONFIG_PM_FLASH_BUFFERS; i++) { - if (m_write_buffer_records[i].store_token == store_token) { - return &m_write_buffer_records[i]; + if (write_buffer_records[i].store_token == store_token) { + return &write_buffer_records[i]; } } return NULL; @@ -182,7 +181,7 @@ static pdb_buffer_record_t *write_buffer_record_find_stored(pm_store_token_t sto * * @return A pointer to the available record, or NULL if none was found. */ -static pdb_buffer_record_t *write_buffer_record_find_unused(void) +static struct pdb_buffer_record *write_buffer_record_find_unused(void) { return write_buffer_record_find(PM_PEER_ID_INVALID, PM_PEER_DATA_ID_INVALID); } @@ -192,17 +191,17 @@ static pdb_buffer_record_t *write_buffer_record_find_unused(void) * * @details This function will first release any buffers, then invalidate the record. * - * @param[inout] p_write_buffer_record The record to release. + * @param[inout] write_buffer_record The record to release. * * @return A pointer to the matching record, or NULL if none was found. */ -static void write_buffer_record_release(pdb_buffer_record_t *p_write_buffer_record) +static void write_buffer_record_release(struct pdb_buffer_record *write_buffer_record) { - for (uint32_t i = 0; i < p_write_buffer_record->n_bufs; i++) { - pm_buffer_release(&m_write_buffer, p_write_buffer_record->buffer_block_id + i); + for (uint32_t i = 0; i < write_buffer_record->n_bufs; i++) { + pm_buffer_release(&write_buffer, write_buffer_record->buffer_block_id + i); } - write_buffer_record_invalidate(p_write_buffer_record); + write_buffer_record_invalidate(write_buffer_record); } /** @@ -212,8 +211,8 @@ static void write_buffer_record_release(pdb_buffer_record_t *p_write_buffer_reco * @param[in] peer_id The peer ID this record should have. * @param[in] data_id The data ID this record should have. */ -static void write_buffer_record_acquire(pdb_buffer_record_t **pp_write_buffer_record, - pm_peer_id_t peer_id, pm_peer_data_id_t data_id) +static void write_buffer_record_acquire(struct pdb_buffer_record **pp_write_buffer_record, + uint16_t peer_id, enum pm_peer_data_id data_id) { if (pp_write_buffer_record == NULL) { return; @@ -230,12 +229,12 @@ static void write_buffer_record_acquire(pdb_buffer_record_t **pp_write_buffer_re /** * @brief Function for dispatching outbound events to all registered event handlers. * - * @param[in] p_event The event to dispatch. + * @param[in] event The event to dispatch. */ -static void pdb_evt_send(pm_evt_t *p_event) +static void pdb_evt_send(struct pm_evt *event) { for (uint32_t i = 0; i < PDB_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_event); + evt_handlers[i](event); } } @@ -247,42 +246,44 @@ static void pdb_evt_send(pm_evt_t *p_event) static void internal_state_reset(void) { for (uint32_t i = 0; i < CONFIG_PM_FLASH_BUFFERS; i++) { - write_buffer_record_invalidate(&m_write_buffer_records[i]); + write_buffer_record_invalidate(&write_buffer_records[i]); } } -static void peer_data_point_to_buffer(pm_peer_data_t *p_peer_data, pm_peer_data_id_t data_id, - uint8_t *p_buffer_memory, uint16_t n_bufs) +static void peer_data_point_to_buffer(struct pm_peer_data *peer_data, + enum pm_peer_data_id data_id, + uint8_t *buffer_memory, uint16_t n_bufs) { uint16_t n_bytes = n_bufs * PDB_WRITE_BUF_SIZE; - p_peer_data->data_id = data_id; + peer_data->data_id = data_id; - p_peer_data->p_all_data = (pm_peer_data_bonding_t *)p_buffer_memory; - p_peer_data->length_words = BYTES_TO_WORDS(n_bytes); + peer_data->all_data = (struct pm_peer_data_bonding *)buffer_memory; + peer_data->length_words = BYTES_TO_WORDS(n_bytes); } -static void peer_data_const_point_to_buffer(pm_peer_data_const_t *p_peer_data, - pm_peer_data_id_t data_id, uint8_t *p_buffer_memory, +static void peer_data_const_point_to_buffer(struct pm_peer_data_const *peer_data, + enum pm_peer_data_id data_id, uint8_t *buffer_memory, uint32_t n_bufs) { - peer_data_point_to_buffer((pm_peer_data_t *)p_peer_data, data_id, p_buffer_memory, n_bufs); + peer_data_point_to_buffer((struct pm_peer_data *)peer_data, data_id, buffer_memory, + n_bufs); } -static void write_buf_length_words_set(pm_peer_data_const_t *p_peer_data) +static void write_buf_length_words_set(struct pm_peer_data_const *peer_data) { - switch (p_peer_data->data_id) { + switch (peer_data->data_id) { case PM_PEER_DATA_ID_BONDING: - p_peer_data->length_words = PM_BONDING_DATA_N_WORDS(); + peer_data->length_words = PM_BONDING_DATA_N_WORDS(); break; case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING: - p_peer_data->length_words = PM_SC_STATE_N_WORDS(); + peer_data->length_words = PM_SC_STATE_N_WORDS(); break; case PM_PEER_DATA_ID_PEER_RANK: - p_peer_data->length_words = PM_USAGE_INDEX_N_WORDS(); + peer_data->length_words = PM_USAGE_INDEX_N_WORDS(); break; case PM_PEER_DATA_ID_GATT_LOCAL: - p_peer_data->length_words = PM_LOCAL_DB_N_WORDS(p_peer_data->p_local_gatt_db->len); + peer_data->length_words = PM_LOCAL_DB_N_WORDS(peer_data->local_gatt_db->len); break; default: /* No action needed. */ @@ -295,7 +296,7 @@ static void write_buf_length_words_set(pm_peer_data_const_t *p_peer_data) * * @note This will unlock the data after it has been written. * - * @param[in] p_write_buffer_record The write buffer record to write into persistent storage. + * @param[in] write_buffer_record The write buffer record to write into persistent storage. * * @retval NRF_SUCCESS Data storing was successfully started. * @retval NRF_ERROR_RESOURCES No space available in persistent storage. Please clear some @@ -305,45 +306,45 @@ static void write_buf_length_words_set(pm_peer_data_const_t *p_peer_data) * @retval NRF_ERROR_INVALID_STATE Module is not initialized. * @retval NRF_ERROR_INTERNAL Unexpected internal error. */ -uint32_t write_buf_store(pdb_buffer_record_t *p_write_buffer_record) +uint32_t write_buf_store(struct pdb_buffer_record *write_buffer_record) { uint32_t err_code = NRF_SUCCESS; - pm_peer_data_const_t peer_data = {.data_id = p_write_buffer_record->data_id}; - uint8_t *p_buffer_memory; + struct pm_peer_data_const peer_data = {.data_id = write_buffer_record->data_id}; + uint8_t *buffer_memory; - p_buffer_memory = - pm_buffer_ptr_get(&m_write_buffer, p_write_buffer_record->buffer_block_id); - if (p_buffer_memory == NULL) { + buffer_memory = + pm_buffer_ptr_get(&write_buffer, write_buffer_record->buffer_block_id); + if (buffer_memory == NULL) { LOG_ERR("pm_buffer_ptr_get() could not retrieve RAM buffer. block_id: %d", - p_write_buffer_record->buffer_block_id); + write_buffer_record->buffer_block_id); return NRF_ERROR_INTERNAL; } - peer_data_const_point_to_buffer(&peer_data, p_write_buffer_record->data_id, p_buffer_memory, - p_write_buffer_record->n_bufs); + peer_data_const_point_to_buffer(&peer_data, write_buffer_record->data_id, buffer_memory, + write_buffer_record->n_bufs); write_buf_length_words_set(&peer_data); - err_code = pds_peer_data_store(p_write_buffer_record->peer_id, &peer_data, - &p_write_buffer_record->store_token); + err_code = pds_peer_data_store(write_buffer_record->peer_id, &peer_data, + &write_buffer_record->store_token); switch (err_code) { case NRF_SUCCESS: - p_write_buffer_record->store_busy = false; - p_write_buffer_record->store_flash_full = false; + write_buffer_record->store_busy = false; + write_buffer_record->store_flash_full = false; break; case NRF_ERROR_BUSY: - p_write_buffer_record->store_busy = true; - p_write_buffer_record->store_flash_full = false; - m_pending_store = true; + write_buffer_record->store_busy = true; + write_buffer_record->store_flash_full = false; + pending_store = true; err_code = NRF_SUCCESS; break; case NRF_ERROR_RESOURCES: - p_write_buffer_record->store_busy = false; - p_write_buffer_record->store_flash_full = true; - m_pending_store = true; + write_buffer_record->store_busy = false; + write_buffer_record->store_flash_full = true; + pending_store = true; break; case NRF_ERROR_INVALID_PARAM: @@ -352,7 +353,7 @@ uint32_t write_buf_store(pdb_buffer_record_t *p_write_buffer_record) default: LOG_ERR("pds_peer_data_store() returned %s. peer_id: %d", - nrf_strerror_get(err_code), p_write_buffer_record->peer_id); + nrf_strerror_get(err_code), write_buffer_record->peer_id); err_code = NRF_ERROR_INTERNAL; break; } @@ -367,15 +368,15 @@ uint32_t write_buf_store(pdb_buffer_record_t *p_write_buffer_record) * * @return Whether or not the store operation succeeded. */ -static bool write_buf_store_in_event(pdb_buffer_record_t *p_write_buffer_record) +static bool write_buf_store_in_event(struct pdb_buffer_record *write_buffer_record) { uint32_t err_code; - pm_evt_t event; + struct pm_evt event; - err_code = write_buf_store(p_write_buffer_record); + err_code = write_buf_store(write_buffer_record); if (err_code != NRF_SUCCESS) { event.conn_handle = BLE_CONN_HANDLE_INVALID; - event.peer_id = p_write_buffer_record->peer_id; + event.peer_id = write_buffer_record->peer_id; if (err_code == NRF_ERROR_RESOURCES) { event.evt_id = PM_EVT_STORAGE_FULL; @@ -386,7 +387,7 @@ static bool write_buf_store_in_event(pdb_buffer_record_t *p_write_buffer_record) LOG_ERR("Some peer data was not properly written to flash. " "write_buf_store() " "returned %s for peer_id: %d", - nrf_strerror_get(err_code), p_write_buffer_record->peer_id); + nrf_strerror_get(err_code), write_buffer_record->peer_id); } pdb_evt_send(&event); @@ -406,19 +407,19 @@ static bool write_buf_store_in_event(pdb_buffer_record_t *p_write_buffer_record) */ static void reattempt_previous_operations(bool retry_flash_full) { - if (!m_pending_store) { + if (!pending_store) { return; } - m_pending_store = false; + pending_store = false; for (uint32_t i = 0; i < CONFIG_PM_FLASH_BUFFERS; i++) { - if ((m_write_buffer_records[i].store_busy) || - (m_write_buffer_records[i].store_flash_full)) { - m_pending_store = true; + if ((write_buffer_records[i].store_busy) || + (write_buffer_records[i].store_flash_full)) { + pending_store = true; - if (m_write_buffer_records[i].store_busy || retry_flash_full) { - if (!write_buf_store_in_event(&m_write_buffer_records[i])) { + if (write_buffer_records[i].store_busy || retry_flash_full) { + if (!write_buf_store_in_event(&write_buffer_records[i])) { return; } } @@ -430,33 +431,33 @@ static void reattempt_previous_operations(bool retry_flash_full) * @brief Function for handling events from the Peer Data Storage module. * This function is extern in Peer Data Storage. * - * @param[in] p_event The event to handle. + * @param[in] event The event to handle. */ -void pdb_pds_evt_handler(pm_evt_t *p_event) +void pdb_pds_evt_handler(struct pm_evt *event) { - pdb_buffer_record_t *p_write_buffer_record; + struct pdb_buffer_record *write_buffer_record; bool evt_send = true; bool retry_flash_full = false; - p_write_buffer_record = - write_buffer_record_find_stored(p_event->params.peer_data_update_succeeded.token); + write_buffer_record = + write_buffer_record_find_stored(event->params.peer_data_update_succeeded.token); - switch (p_event->evt_id) { + switch (event->evt_id) { case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: - if ((p_event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) && - (p_write_buffer_record != NULL)) { + if ((event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) && + (write_buffer_record != NULL)) { /* The write came from PDB. */ - write_buffer_record_release(p_write_buffer_record); + write_buffer_record_release(write_buffer_record); } break; case PM_EVT_PEER_DATA_UPDATE_FAILED: - if ((p_event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) && - (p_write_buffer_record != NULL)) { + if ((event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) && + (write_buffer_record != NULL)) { /* The write came from PDB, retry. */ - p_write_buffer_record->store_token = PM_STORE_TOKEN_INVALID; - p_write_buffer_record->store_busy = true; - m_pending_store = true; + write_buffer_record->store_token = PM_STORE_TOKEN_INVALID; + write_buffer_record->store_busy = true; + pending_store = true; evt_send = false; } break; @@ -471,7 +472,7 @@ void pdb_pds_evt_handler(pm_evt_t *p_event) if (evt_send) { /* Forward the event to all registered Peer Database event handlers. */ - pdb_evt_send(p_event); + pdb_evt_send(event); } reattempt_previous_operations(retry_flash_full); @@ -481,33 +482,33 @@ uint32_t pdb_init(void) { uint32_t err_code; - NRF_PM_DEBUG_CHECK(!m_module_initialized); + __ASSERT_NO_MSG(!module_initialized); internal_state_reset(); - PM_BUFFER_INIT(&m_write_buffer, CONFIG_PM_FLASH_BUFFERS, PDB_WRITE_BUF_SIZE, err_code); + PM_BUFFER_INIT(&write_buffer, CONFIG_PM_FLASH_BUFFERS, PDB_WRITE_BUF_SIZE, err_code); if (err_code != NRF_SUCCESS) { LOG_ERR("PM_BUFFER_INIT() returned %s.", nrf_strerror_get(err_code)); return NRF_ERROR_INTERNAL; } - m_module_initialized = true; + module_initialized = true; return NRF_SUCCESS; } -uint32_t pdb_peer_free(pm_peer_id_t peer_id) +uint32_t pdb_peer_free(uint16_t peer_id) { uint32_t err_code; - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); uint32_t index = 0; - pdb_buffer_record_t *p_record = write_buffer_record_find_next(peer_id, &index); + struct pdb_buffer_record *record = write_buffer_record_find_next(peer_id, &index); - while (p_record != NULL) { - err_code = pdb_write_buf_release(peer_id, p_record->data_id); + while (record != NULL) { + err_code = pdb_write_buf_release(peer_id, record->data_id); /* All return values are acceptable. */ UNUSED_VARIABLE(err_code); @@ -515,12 +516,12 @@ uint32_t pdb_peer_free(pm_peer_id_t peer_id) LOG_ERR("pdb_write_buf_release() returned %s which should not " "happen. peer_id: %d, " "data_id: %d", - nrf_strerror_get(err_code), peer_id, p_record->data_id); + nrf_strerror_get(err_code), peer_id, record->data_id); return NRF_ERROR_INTERNAL; } index++; - p_record = write_buffer_record_find_next(peer_id, &index); + record = write_buffer_record_find_next(peer_id, &index); } err_code = pds_peer_id_free(peer_id); @@ -535,88 +536,89 @@ uint32_t pdb_peer_free(pm_peer_id_t peer_id) return NRF_ERROR_INTERNAL; } -uint32_t pdb_write_buf_get(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, uint32_t n_bufs, - pm_peer_data_t *p_peer_data) +uint32_t pdb_write_buf_get(uint16_t peer_id, enum pm_peer_data_id data_id, uint32_t n_bufs, + struct pm_peer_data *peer_data) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); + + if (peer_data == NULL) { + return NRF_ERROR_NULL; + } - VERIFY_PARAM_NOT_NULL(p_peer_data); VERIFY_DATA_ID_WRITE_BUF(data_id); if ((n_bufs == 0) || (n_bufs > CONFIG_PM_FLASH_BUFFERS)) { return NRF_ERROR_INVALID_PARAM; } - pdb_buffer_record_t *p_write_buffer_record; - uint8_t *p_buffer_memory; + struct pdb_buffer_record *write_buffer_record; + uint8_t *buffer_memory; bool new_record = false; - p_write_buffer_record = write_buffer_record_find(peer_id, data_id); + write_buffer_record = write_buffer_record_find(peer_id, data_id); - if (p_write_buffer_record == NULL) { + if (write_buffer_record == NULL) { /* No buffer exists. */ - write_buffer_record_acquire(&p_write_buffer_record, peer_id, data_id); - if (p_write_buffer_record == NULL) { + write_buffer_record_acquire(&write_buffer_record, peer_id, data_id); + if (write_buffer_record == NULL) { return NRF_ERROR_BUSY; } - } else if (p_write_buffer_record->n_bufs != n_bufs) { + } else if (write_buffer_record->n_bufs != n_bufs) { /* Buffer exists with a different n_bufs from what was requested. */ return NRF_ERROR_FORBIDDEN; } - if (p_write_buffer_record->buffer_block_id == PM_BUFFER_INVALID_ID) { - p_write_buffer_record->buffer_block_id = - pm_buffer_block_acquire(&m_write_buffer, n_bufs); + if (write_buffer_record->buffer_block_id == PM_BUFFER_INVALID_ID) { + write_buffer_record->buffer_block_id = + pm_buffer_block_acquire(&write_buffer, n_bufs); - if (p_write_buffer_record->buffer_block_id == PM_BUFFER_INVALID_ID) { - write_buffer_record_invalidate(p_write_buffer_record); + if (write_buffer_record->buffer_block_id == PM_BUFFER_INVALID_ID) { + write_buffer_record_invalidate(write_buffer_record); return NRF_ERROR_BUSY; } new_record = true; } - p_write_buffer_record->n_bufs = n_bufs; + write_buffer_record->n_bufs = n_bufs; - p_buffer_memory = - pm_buffer_ptr_get(&m_write_buffer, p_write_buffer_record->buffer_block_id); + buffer_memory = pm_buffer_ptr_get(&write_buffer, write_buffer_record->buffer_block_id); - if (p_buffer_memory == NULL) { + if (buffer_memory == NULL) { LOG_ERR("Cannot store data to flash because pm_buffer_ptr_get() could not retrieve " "RAM buffer. Is block_id %d not allocated?", - p_write_buffer_record->buffer_block_id); + write_buffer_record->buffer_block_id); return NRF_ERROR_INTERNAL; } - peer_data_point_to_buffer(p_peer_data, data_id, p_buffer_memory, n_bufs); + peer_data_point_to_buffer(peer_data, data_id, buffer_memory, n_bufs); if (new_record && (data_id == PM_PEER_DATA_ID_GATT_LOCAL)) { - p_peer_data->p_local_gatt_db->len = PM_LOCAL_DB_LEN(p_peer_data->length_words); + peer_data->local_gatt_db->len = PM_LOCAL_DB_LEN(peer_data->length_words); } return NRF_SUCCESS; } -uint32_t pdb_write_buf_release(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) +uint32_t pdb_write_buf_release(uint16_t peer_id, enum pm_peer_data_id data_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); - pdb_buffer_record_t *p_write_buffer_record; + struct pdb_buffer_record *write_buffer_record; - p_write_buffer_record = write_buffer_record_find(peer_id, data_id); + write_buffer_record = write_buffer_record_find(peer_id, data_id); - if (p_write_buffer_record == NULL) { + if (write_buffer_record == NULL) { return NRF_ERROR_NOT_FOUND; } - write_buffer_record_release(p_write_buffer_record); + write_buffer_record_release(write_buffer_record); return NRF_SUCCESS; } -uint32_t pdb_write_buf_store(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, - pm_peer_id_t new_peer_id) +uint32_t pdb_write_buf_store(uint16_t peer_id, enum pm_peer_data_id data_id, uint16_t new_peer_id) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); VERIFY_DATA_ID_WRITE_BUF(data_id); @@ -624,13 +626,13 @@ uint32_t pdb_write_buf_store(pm_peer_id_t peer_id, pm_peer_data_id_t data_id, return NRF_ERROR_INVALID_PARAM; } - pdb_buffer_record_t *p_write_buffer_record = write_buffer_record_find(peer_id, data_id); + struct pdb_buffer_record *write_buffer_record = write_buffer_record_find(peer_id, data_id); - if (p_write_buffer_record == NULL) { + if (write_buffer_record == NULL) { return NRF_ERROR_NOT_FOUND; } - p_write_buffer_record->peer_id = new_peer_id; - p_write_buffer_record->data_id = data_id; - return write_buf_store(p_write_buffer_record); + write_buffer_record->peer_id = new_peer_id; + write_buffer_record->data_id = data_id; + return write_buf_store(write_buffer_record); } diff --git a/lib/bluetooth/peer_manager/modules/peer_id.c b/lib/bluetooth/peer_manager/modules/peer_id.c index 8a499e9b77..2a285a67cd 100644 --- a/lib/bluetooth/peer_manager/modules/peer_id.c +++ b/lib/bluetooth/peer_manager/modules/peer_id.c @@ -13,29 +13,29 @@ #include #include -typedef struct { +struct peer_id_flags { /** Bitmap designating which peer IDs are in use. */ ATOMIC_DEFINE(used_peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); /** Bitmap designating which peer IDs are marked for deletion. */ ATOMIC_DEFINE(deleted_peer_ids, PM_PEER_ID_N_AVAILABLE_IDS); -} pi_t; +}; -static pi_t m_pi = {{0}, {0}}; +static struct peer_id_flags peer_id_flags = {{0}, {0}}; -static void internal_state_reset(pi_t *p_pi) +static void internal_state_reset(struct peer_id_flags *pi_flags) { - memset(p_pi, 0, sizeof(pi_t)); + memset(pi_flags, 0, sizeof(struct peer_id_flags)); } void peer_id_init(void) { - internal_state_reset(&m_pi); + internal_state_reset(&peer_id_flags); } -static uint32_t find_and_set_flag(atomic_t *p_flags, uint32_t flag_count) +static uint32_t find_and_set_flag(atomic_t *pi_flags, uint32_t flag_count) { for (uint32_t i = 0; i < ATOMIC_BITMAP_SIZE(flag_count); i++) { - uint32_t word = atomic_get(&p_flags[i]); + uint32_t word = atomic_get(&pi_flags[i]); uint32_t inverted = ~word; while (inverted) { @@ -46,7 +46,7 @@ static uint32_t find_and_set_flag(atomic_t *p_flags, uint32_t flag_count) if (first_zero_global >= flag_count) { break; } - if (!atomic_test_and_set_bit(p_flags, first_zero_global)) { + if (!atomic_test_and_set_bit(pi_flags, first_zero_global)) { return first_zero_global; } inverted &= ~(1u << first_zero); @@ -55,77 +55,77 @@ static uint32_t find_and_set_flag(atomic_t *p_flags, uint32_t flag_count) return flag_count; } -static pm_peer_id_t claim(pm_peer_id_t peer_id, atomic_t *p_peer_id_flags) +static uint16_t claim(uint16_t peer_id, atomic_t *pi_flags) { - pm_peer_id_t allocated_peer_id = PM_PEER_ID_INVALID; + uint16_t allocated_peer_id = PM_PEER_ID_INVALID; if (peer_id == PM_PEER_ID_INVALID) { allocated_peer_id = - find_and_set_flag(p_peer_id_flags, PM_PEER_ID_N_AVAILABLE_IDS); + find_and_set_flag(pi_flags, PM_PEER_ID_N_AVAILABLE_IDS); if (allocated_peer_id == PM_PEER_ID_N_AVAILABLE_IDS) { allocated_peer_id = PM_PEER_ID_INVALID; } } else if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) { - bool lock_success = !atomic_test_and_set_bit(p_peer_id_flags, peer_id); + bool lock_success = !atomic_test_and_set_bit(pi_flags, peer_id); allocated_peer_id = lock_success ? peer_id : PM_PEER_ID_INVALID; } return allocated_peer_id; } -static void release(pm_peer_id_t peer_id, atomic_t *p_peer_id_flags) +static void release(uint16_t peer_id, atomic_t *pi_flags) { if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) { - atomic_clear_bit(p_peer_id_flags, peer_id); + atomic_clear_bit(pi_flags, peer_id); } } -pm_peer_id_t peer_id_allocate(pm_peer_id_t peer_id) +uint16_t peer_id_allocate(uint16_t peer_id) { - return claim(peer_id, m_pi.used_peer_ids); + return claim(peer_id, peer_id_flags.used_peer_ids); } -bool peer_id_delete(pm_peer_id_t peer_id) +bool peer_id_delete(uint16_t peer_id) { - pm_peer_id_t deleted_peer_id; + uint16_t deleted_peer_id; if (peer_id == PM_PEER_ID_INVALID) { return false; } - deleted_peer_id = claim(peer_id, m_pi.deleted_peer_ids); + deleted_peer_id = claim(peer_id, peer_id_flags.deleted_peer_ids); return (deleted_peer_id == peer_id); } -void peer_id_free(pm_peer_id_t peer_id) +void peer_id_free(uint16_t peer_id) { - release(peer_id, m_pi.used_peer_ids); - release(peer_id, m_pi.deleted_peer_ids); + release(peer_id, peer_id_flags.used_peer_ids); + release(peer_id, peer_id_flags.deleted_peer_ids); } -bool peer_id_is_allocated(pm_peer_id_t peer_id) +bool peer_id_is_allocated(uint16_t peer_id) { if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) { - return atomic_test_bit(m_pi.used_peer_ids, peer_id); + return atomic_test_bit(peer_id_flags.used_peer_ids, peer_id); } return false; } -bool peer_id_is_deleted(pm_peer_id_t peer_id) +bool peer_id_is_deleted(uint16_t peer_id) { if (peer_id < PM_PEER_ID_N_AVAILABLE_IDS) { - return atomic_test_bit(m_pi.deleted_peer_ids, peer_id); + return atomic_test_bit(peer_id_flags.deleted_peer_ids, peer_id); } return false; } -pm_peer_id_t next_id_get(pm_peer_id_t prev_peer_id, atomic_t *p_peer_id_flags) +uint16_t next_id_get(uint16_t prev_peer_id, atomic_t *pi_flags) { - pm_peer_id_t i = (prev_peer_id == PM_PEER_ID_INVALID) ? 0 : (prev_peer_id + 1); + uint16_t i = (prev_peer_id == PM_PEER_ID_INVALID) ? 0 : (prev_peer_id + 1); for (; i < PM_PEER_ID_N_AVAILABLE_IDS; i++) { - if (atomic_test_bit(p_peer_id_flags, i)) { + if (atomic_test_bit(pi_flags, i)) { return i; } } @@ -133,32 +133,32 @@ pm_peer_id_t next_id_get(pm_peer_id_t prev_peer_id, atomic_t *p_peer_id_flags) return PM_PEER_ID_INVALID; } -pm_peer_id_t peer_id_get_next_used(pm_peer_id_t peer_id) +uint16_t peer_id_get_next_used(uint16_t peer_id) { - peer_id = next_id_get(peer_id, m_pi.used_peer_ids); + peer_id = next_id_get(peer_id, peer_id_flags.used_peer_ids); while (peer_id != PM_PEER_ID_INVALID) { if (!peer_id_is_deleted(peer_id)) { return peer_id; } - peer_id = next_id_get(peer_id, m_pi.used_peer_ids); + peer_id = next_id_get(peer_id, peer_id_flags.used_peer_ids); } return peer_id; } -pm_peer_id_t peer_id_get_next_deleted(pm_peer_id_t prev_peer_id) +uint16_t peer_id_get_next_deleted(uint16_t prev_peer_id) { - return next_id_get(prev_peer_id, m_pi.deleted_peer_ids); + return next_id_get(prev_peer_id, peer_id_flags.deleted_peer_ids); } uint32_t peer_id_n_ids(void) { uint32_t n_ids = 0; - for (pm_peer_id_t i = 0; i < PM_PEER_ID_N_AVAILABLE_IDS; i++) { - n_ids += atomic_test_bit(m_pi.used_peer_ids, i); + for (uint16_t i = 0; i < PM_PEER_ID_N_AVAILABLE_IDS; i++) { + n_ids += atomic_test_bit(peer_id_flags.used_peer_ids, i); } return n_ids; diff --git a/lib/bluetooth/peer_manager/modules/peer_manager_handler.c b/lib/bluetooth/peer_manager/modules/peer_manager_handler.c index 1a32936279..751301745e 100644 --- a/lib/bluetooth/peer_manager/modules/peer_manager_handler.c +++ b/lib/bluetooth/peer_manager/modules/peer_manager_handler.c @@ -22,15 +22,15 @@ LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); -#define APP_ERROR_CHECK(err) +#define APP_ERROR_CHECK(err) (void)(err) -static const char * const m_roles_str[] = { +static const char *const roles_str[] = { "Invalid Role", "Peripheral", "Central", }; -static const char * const m_sec_procedure_str[] = { +static const char *const sec_procedure_str[] = { "Encryption", "Bonding", "Pairing", @@ -38,7 +38,7 @@ static const char * const m_sec_procedure_str[] = { #define PM_EVT_STR(_name) [_name] = STRINGIFY(_name) -static const char * const m_event_str[] = { +static const char *const event_str[] = { PM_EVT_STR(PM_EVT_BONDED_PEER_CONNECTED), PM_EVT_STR(PM_EVT_CONN_CONFIG_REQ), PM_EVT_STR(PM_EVT_CONN_SEC_START), @@ -63,7 +63,7 @@ static const char * const m_event_str[] = { PM_EVT_STR(PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED), }; -static const char * const m_data_id_str[] = { +static const char *const data_id_str[] = { "Outdated (0)", "Service changed pending flag", "Outdated (2)", "Outdated (3)", "Application data", "Remote database", @@ -71,32 +71,32 @@ static const char * const m_data_id_str[] = { "Local database", "Central address resolution", }; -static const char * const m_data_action_str[] = {"Update", "Delete"}; +static const char *const data_action_str[] = {"Update", "Delete"}; #define PM_SEC_ERR_STR(_name) \ { \ .error = _name, .error_str = #_name \ } -typedef struct { - pm_sec_error_code_t error; +struct sec_err_str { + uint16_t error; const char *error_str; -} sec_err_str_t; +}; -static const sec_err_str_t m_pm_sec_error_str[] = { +static const struct sec_err_str pm_sec_error_str[] = { PM_SEC_ERR_STR(PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING), PM_SEC_ERR_STR(PM_CONN_SEC_ERROR_MIC_FAILURE), PM_SEC_ERR_STR(PM_CONN_SEC_ERROR_DISCONNECT), PM_SEC_ERR_STR(PM_CONN_SEC_ERROR_SMP_TIMEOUT), }; -static const char *sec_err_string_get(pm_sec_error_code_t error) +static const char *sec_err_string_get(uint16_t error) { static char errstr[30]; - for (uint32_t i = 0; i < (sizeof(m_pm_sec_error_str) / sizeof(sec_err_str_t)); i++) { - if (m_pm_sec_error_str[i].error == error) { - return m_pm_sec_error_str[i].error_str; + for (uint32_t i = 0; i < (sizeof(pm_sec_error_str) / sizeof(struct sec_err_str)); i++) { + if (pm_sec_error_str[i].error == error) { + return pm_sec_error_str[i].error_str; } } @@ -107,12 +107,12 @@ static const char *sec_err_string_get(pm_sec_error_code_t error) return errstr; } -static void _conn_secure(uint16_t conn_handle, bool force) +static void conn_secure_impl(uint16_t conn_handle, bool force) { uint32_t err_code; if (!force) { - pm_conn_sec_status_t status; + struct pm_conn_sec_status status; err_code = pm_conn_sec_status_get(conn_handle, &status); if (err_code != BLE_ERROR_INVALID_CONN_HANDLE) { @@ -158,85 +158,87 @@ static void _conn_secure(uint16_t conn_handle, bool force) #if CONFIG_PM_HANDLER_SEC_DELAY_MS > 0 static struct bm_timer secure_delay_timer; -typedef union { +union conn_secure_context { struct { uint16_t conn_handle; bool force; } values; - void *p_void; -} conn_secure_context_t; - -STATIC_ASSERT(sizeof(conn_secure_context_t) <= sizeof(void *), - "conn_secure_context_t is too large."); + void *ptr; +}; -static void _conn_secure(uint16_t conn_handle, bool force); +BUILD_ASSERT(sizeof(union conn_secure_context) <= sizeof(void *), + "The size of 'union conn_secure_context' must be smaller than the size of a pointer"); static void delayed_conn_secure(void *context) { - conn_secure_context_t sec_context = {.p_void = context}; + /* The context argument is data and not a valid address. Copy it. */ + union conn_secure_context sec_context = {.ptr = context}; - _conn_secure(sec_context.values.conn_handle, sec_context.values.force); + conn_secure_impl(sec_context.values.conn_handle, sec_context.values.force); } +#endif static void conn_secure(uint16_t conn_handle, bool force) { +#if CONFIG_PM_HANDLER_SEC_DELAY_MS > 0 int err; static bool created; + union conn_secure_context sec_context = { + .values = { + .conn_handle = conn_handle, + .force = force, + }, + }; if (!created) { err = bm_timer_init(&secure_delay_timer, BM_TIMER_MODE_SINGLE_SHOT, - delayed_conn_secure); + delayed_conn_secure); APP_ERROR_CHECK(err); created = true; } - conn_secure_context_t sec_context = {0}; - - sec_context.values.conn_handle = conn_handle; - sec_context.values.force = force; - - err = bm_timer_start(secure_delay_timer, - BM_TIMER_MS_TO_TICKS(CONFIG_PM_HANDLER_SEC_DELAY_MS), - sec_context.p_void); + /* The conn_secure_context is smaller than a pointer and is copied into the context + * argument itself. The passed context pointer is not a valid address, it is data. + It is fine for sec_context to go out of scope because the values are copied. + */ + err = bm_timer_start(&secure_delay_timer, + BM_TIMER_MS_TO_TICKS(CONFIG_PM_HANDLER_SEC_DELAY_MS), + sec_context.ptr); APP_ERROR_CHECK(err); -} - #else -static void conn_secure(uint16_t conn_handle, bool force) -{ - _conn_secure(conn_handle, force); -} + conn_secure_impl(conn_handle, force); #endif +} -void pm_handler_on_pm_evt(pm_evt_t const *p_pm_evt) +void pm_handler_on_pm_evt(const struct pm_evt *pm_evt) { - pm_handler_pm_evt_log(p_pm_evt); + pm_handler_pm_evt_log(pm_evt); - if (p_pm_evt->evt_id == PM_EVT_BONDED_PEER_CONNECTED) { - conn_secure(p_pm_evt->conn_handle, false); - } else if (p_pm_evt->evt_id == PM_EVT_ERROR_UNEXPECTED) { + if (pm_evt->evt_id == PM_EVT_BONDED_PEER_CONNECTED) { + conn_secure(pm_evt->conn_handle, false); + } else if (pm_evt->evt_id == PM_EVT_ERROR_UNEXPECTED) { LOG_ERR("Asserting."); - APP_ERROR_CHECK(p_pm_evt->params.error_unexpected.error); + APP_ERROR_CHECK(pm_evt->params.error_unexpected.error); } } void pm_handler_flash_clean_on_return(void) { /* Trigger the mechanism to make more room in flash. */ - pm_evt_t storage_full_evt = {.evt_id = PM_EVT_STORAGE_FULL}; + struct pm_evt storage_full_evt = { .evt_id = PM_EVT_STORAGE_FULL }; pm_handler_flash_clean(&storage_full_evt); } -static void rank_highest(pm_peer_id_t peer_id) +static void rank_highest(uint16_t peer_id) { /* Trigger a pm_peer_rank_highest() with internal bookkeeping. */ - pm_evt_t connected_evt = {.evt_id = PM_EVT_BONDED_PEER_CONNECTED, .peer_id = peer_id}; + struct pm_evt connected_evt = {.evt_id = PM_EVT_BONDED_PEER_CONNECTED, .peer_id = peer_id}; pm_handler_flash_clean(&connected_evt); } -void pm_handler_flash_clean(pm_evt_t const *p_pm_evt) +void pm_handler_flash_clean(const struct pm_evt *pm_evt) { uint32_t err_code; /* Indicates whether a successful write happened after the last garbage @@ -251,16 +253,16 @@ void pm_handler_flash_clean(pm_evt_t const *p_pm_evt) #define RANK_QUEUE_INIT PM_PEER_ID_INVALID, /* Queue of rank_highest calls that failed because of full flash. */ - static pm_peer_id_t rank_queue[8] = {[0 ... RANK_QUEUE_SIZE - 1] = RANK_QUEUE_INIT}; + static uint16_t rank_queue[8] = {[0 ... RANK_QUEUE_SIZE - 1] = RANK_QUEUE_INIT}; /* Write pointer for rank_queue. */ static int rank_queue_wr; - switch (p_pm_evt->evt_id) { + switch (pm_evt->evt_id) { case PM_EVT_BONDED_PEER_CONNECTED: - err_code = pm_peer_rank_highest(p_pm_evt->peer_id); + err_code = pm_peer_rank_highest(pm_evt->peer_id); if ((err_code == NRF_ERROR_RESOURCES) || (err_code == NRF_ERROR_BUSY)) { /* Queue pm_peer_rank_highest() call and attempt to clean flash. */ - rank_queue[rank_queue_wr] = p_pm_evt->peer_id; + rank_queue[rank_queue_wr] = pm_evt->peer_id; rank_queue_wr = (rank_queue_wr + 1) % RANK_QUEUE_SIZE; pm_handler_flash_clean_on_return(); } else if ((err_code != NRF_ERROR_NOT_SUPPORTED) && @@ -269,7 +271,7 @@ void pm_handler_flash_clean(pm_evt_t const *p_pm_evt) APP_ERROR_CHECK(err_code); } else { LOG_DBG("pm_peer_rank_highest() returned %s for peer id %d", - nrf_strerror_get(err_code), p_pm_evt->peer_id); + nrf_strerror_get(err_code), pm_evt->peer_id); } break; @@ -280,11 +282,11 @@ void pm_handler_flash_clean(pm_evt_t const *p_pm_evt) /* PM_CONN_SEC_PROCEDURE_ENCRYPTION in case peer was not recognized at connection * time. */ - if ((p_pm_evt->params.conn_sec_succeeded.procedure == + if ((pm_evt->params.conn_sec_succeeded.procedure == PM_CONN_SEC_PROCEDURE_BONDING) || - (p_pm_evt->params.conn_sec_succeeded.procedure == + (pm_evt->params.conn_sec_succeeded.procedure == PM_CONN_SEC_PROCEDURE_ENCRYPTION)) { - rank_highest(p_pm_evt->peer_id); + rank_highest(pm_evt->peer_id); } break; @@ -324,15 +326,15 @@ void pm_handler_flash_clean(pm_evt_t const *p_pm_evt) } } -void pm_handler_pm_evt_log(pm_evt_t const *p_pm_evt) +void pm_handler_pm_evt_log(const struct pm_evt *pm_evt) { - LOG_DBG("Event %s", m_event_str[p_pm_evt->evt_id]); + LOG_DBG("Event %s", event_str[pm_evt->evt_id]); - switch (p_pm_evt->evt_id) { + switch (pm_evt->evt_id) { case PM_EVT_BONDED_PEER_CONNECTED: LOG_DBG("Previously bonded peer connected: role: %s, conn_handle: %d, peer_id: %d", - m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)], - p_pm_evt->conn_handle, p_pm_evt->peer_id); + roles_str[ble_conn_state_role(pm_evt->conn_handle)], + pm_evt->conn_handle, pm_evt->peer_id); break; case PM_EVT_CONN_CONFIG_REQ: @@ -342,27 +344,27 @@ void pm_handler_pm_evt_log(pm_evt_t const *p_pm_evt) case PM_EVT_CONN_SEC_START: LOG_DBG("Connection security procedure started: role: %s, conn_handle: %d, " "procedure: %s", - m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)], - p_pm_evt->conn_handle, - m_sec_procedure_str[p_pm_evt->params.conn_sec_start.procedure]); + roles_str[ble_conn_state_role(pm_evt->conn_handle)], + pm_evt->conn_handle, + sec_procedure_str[pm_evt->params.conn_sec_start.procedure]); break; case PM_EVT_CONN_SEC_SUCCEEDED: LOG_INF("Connection secured: role: %s, conn_handle: %d, procedure: %s", - m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)], - p_pm_evt->conn_handle, - m_sec_procedure_str[p_pm_evt->params.conn_sec_start.procedure]); + roles_str[ble_conn_state_role(pm_evt->conn_handle)], + pm_evt->conn_handle, + sec_procedure_str[pm_evt->params.conn_sec_start.procedure]); break; case PM_EVT_CONN_SEC_FAILED: LOG_INF("Connection security failed: role: %s, conn_handle: 0x%x, procedure: " "%s, error: %d", - m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)], - p_pm_evt->conn_handle, - m_sec_procedure_str[p_pm_evt->params.conn_sec_start.procedure], - p_pm_evt->params.conn_sec_failed.error); + roles_str[ble_conn_state_role(pm_evt->conn_handle)], + pm_evt->conn_handle, + sec_procedure_str[pm_evt->params.conn_sec_start.procedure], + pm_evt->params.conn_sec_failed.error); LOG_DBG("Error (decoded): %s", - sec_err_string_get(p_pm_evt->params.conn_sec_failed.error)); + sec_err_string_get(pm_evt->params.conn_sec_failed.error)); break; case PM_EVT_CONN_SEC_CONFIG_REQ: @@ -379,15 +381,15 @@ void pm_handler_pm_evt_log(pm_evt_t const *p_pm_evt) case PM_EVT_ERROR_UNEXPECTED: LOG_ERR("Unexpected fatal error occurred: error: %s", - nrf_strerror_get(p_pm_evt->params.error_unexpected.error)); + nrf_strerror_get(pm_evt->params.error_unexpected.error)); break; case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: LOG_INF("Peer data updated in flash: peer_id: %d, data_id: %s, action: %s%s", - p_pm_evt->peer_id, - m_data_id_str[p_pm_evt->params.peer_data_update_succeeded.data_id], - m_data_action_str[p_pm_evt->params.peer_data_update_succeeded.action], - p_pm_evt->params.peer_data_update_succeeded.flash_changed + pm_evt->peer_id, + data_id_str[pm_evt->params.peer_data_update_succeeded.data_id], + data_action_str[pm_evt->params.peer_data_update_succeeded.action], + pm_evt->params.peer_data_update_succeeded.flash_changed ? "" : ", no change"); break; @@ -395,19 +397,19 @@ void pm_handler_pm_evt_log(pm_evt_t const *p_pm_evt) case PM_EVT_PEER_DATA_UPDATE_FAILED: /* This can happen if the SoftDevice is too busy with BLE operations. */ LOG_WRN("Peer data updated failed: peer_id: %d, data_id: %s, action: %s, error: %s", - p_pm_evt->peer_id, - m_data_id_str[p_pm_evt->params.peer_data_update_failed.data_id], - m_data_action_str[p_pm_evt->params.peer_data_update_succeeded.action], - nrf_strerror_get(p_pm_evt->params.peer_data_update_failed.error)); + pm_evt->peer_id, + data_id_str[pm_evt->params.peer_data_update_failed.data_id], + data_action_str[pm_evt->params.peer_data_update_succeeded.action], + nrf_strerror_get(pm_evt->params.peer_data_update_failed.error)); break; case PM_EVT_PEER_DELETE_SUCCEEDED: - LOG_ERR("Peer deleted successfully: peer_id: %d", p_pm_evt->peer_id); + LOG_INF("Peer deleted successfully: peer_id: %d", pm_evt->peer_id); break; case PM_EVT_PEER_DELETE_FAILED: - LOG_ERR("Peer deletion failed: peer_id: %d, error: %s", p_pm_evt->peer_id, - nrf_strerror_get(p_pm_evt->params.peer_delete_failed.error)); + LOG_ERR("Peer deletion failed: peer_id: %d, error: %s", pm_evt->peer_id, + nrf_strerror_get(pm_evt->params.peer_delete_failed.error)); break; case PM_EVT_PEERS_DELETE_SUCCEEDED: @@ -416,18 +418,18 @@ void pm_handler_pm_evt_log(pm_evt_t const *p_pm_evt) case PM_EVT_PEERS_DELETE_FAILED: LOG_ERR("All peer deletion failed: error: %s", - nrf_strerror_get(p_pm_evt->params.peers_delete_failed_evt.error)); + nrf_strerror_get(pm_evt->params.peers_delete_failed_evt.error)); break; case PM_EVT_LOCAL_DB_CACHE_APPLIED: LOG_DBG("Previously stored local DB applied: conn_handle: %d, peer_id: %d", - p_pm_evt->conn_handle, p_pm_evt->peer_id); + pm_evt->conn_handle, pm_evt->peer_id); break; case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED: /* This can happen when the local DB has changed. */ LOG_WRN("Local DB could not be applied: conn_handle: %d, peer_id: %d", - p_pm_evt->conn_handle, p_pm_evt->peer_id); + pm_evt->conn_handle, pm_evt->peer_id); break; case PM_EVT_SERVICE_CHANGED_IND_SENT: @@ -448,22 +450,22 @@ void pm_handler_pm_evt_log(pm_evt_t const *p_pm_evt) case PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED: LOG_WRN("Flash garbage collection failed with error %s.", - nrf_strerror_get(p_pm_evt->params.garbage_collection_failed.error)); + nrf_strerror_get(pm_evt->params.garbage_collection_failed.error)); break; default: - LOG_WRN("Unexpected PM event ID: 0x%x.", p_pm_evt->evt_id); + LOG_WRN("Unexpected PM event ID: 0x%x.", pm_evt->evt_id); break; } } -void pm_handler_disconnect_on_sec_failure(pm_evt_t const *p_pm_evt) +void pm_handler_disconnect_on_sec_failure(const struct pm_evt *pm_evt) { uint32_t err_code; - if (p_pm_evt->evt_id == PM_EVT_CONN_SEC_FAILED) { - LOG_WRN("Disconnecting conn_handle %d.", p_pm_evt->conn_handle); - err_code = sd_ble_gap_disconnect(p_pm_evt->conn_handle, + if (pm_evt->evt_id == PM_EVT_CONN_SEC_FAILED) { + LOG_WRN("Disconnecting conn_handle %d.", pm_evt->conn_handle); + err_code = sd_ble_gap_disconnect(pm_evt->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_INVALID_CONN_HANDLE)) { @@ -472,27 +474,27 @@ void pm_handler_disconnect_on_sec_failure(pm_evt_t const *p_pm_evt) } } -void pm_handler_disconnect_on_insufficient_sec(pm_evt_t const *p_pm_evt, - pm_conn_sec_status_t *p_min_conn_sec) +void pm_handler_disconnect_on_insufficient_sec(const struct pm_evt *pm_evt, + struct pm_conn_sec_status *min_conn_sec) { - if (p_pm_evt->evt_id == PM_EVT_CONN_SEC_SUCCEEDED) { - if (!pm_sec_is_sufficient(p_pm_evt->conn_handle, p_min_conn_sec)) { + if (pm_evt->evt_id == PM_EVT_CONN_SEC_SUCCEEDED) { + if (!pm_sec_is_sufficient(pm_evt->conn_handle, min_conn_sec)) { LOG_WRN("Connection security is insufficient, disconnecting."); uint32_t err_code = sd_ble_gap_disconnect( - p_pm_evt->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); + pm_evt->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); APP_ERROR_CHECK(err_code); LOG_ERR("sd_ble_gap_disconnect() error 0x%x", err_code); } } } -void pm_handler_secure_on_connection(ble_evt_t const *p_ble_evt) +void pm_handler_secure_on_connection(const ble_evt_t *ble_evt) { - switch (p_ble_evt->header.evt_id) { + switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: LOG_DBG("Connected, securing connection. conn_handle: %d", - p_ble_evt->evt.gap_evt.conn_handle); - conn_secure(p_ble_evt->evt.gap_evt.conn_handle, false); + ble_evt->evt.gap_evt.conn_handle); + conn_secure(ble_evt->evt.gap_evt.conn_handle, false); break; #if CONFIG_PM_HANDLER_SEC_DELAY_MS > 0 @@ -508,19 +510,19 @@ void pm_handler_secure_on_connection(ble_evt_t const *p_ble_evt) } } -void pm_handler_secure_on_error(ble_evt_t const *p_ble_evt) +void pm_handler_secure_on_error(const ble_evt_t *ble_evt) { - if ((p_ble_evt->header.evt_id >= BLE_GATTC_EVT_BASE) && - (p_ble_evt->header.evt_id <= BLE_GATTC_EVT_LAST)) { - if ((p_ble_evt->evt.gattc_evt.gatt_status == + if ((ble_evt->header.evt_id >= BLE_GATTC_EVT_BASE) && + (ble_evt->header.evt_id <= BLE_GATTC_EVT_LAST)) { + if ((ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION) || - (p_ble_evt->evt.gattc_evt.gatt_status == + (ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION)) { LOG_INF("GATTC procedure (evt id 0x%x) failed because it needs " "encryption. Bonding: conn_handle=%d", - p_ble_evt->header.evt_id, - p_ble_evt->evt.gattc_evt.conn_handle); - conn_secure(p_ble_evt->evt.gattc_evt.conn_handle, true); + ble_evt->header.evt_id, + ble_evt->evt.gattc_evt.conn_handle); + conn_secure(ble_evt->evt.gattc_evt.conn_handle, true); } } } diff --git a/lib/bluetooth/peer_manager/modules/pm_buffer.c b/lib/bluetooth/peer_manager/modules/pm_buffer.c index 72de18b6a0..6f46da61bb 100644 --- a/lib/bluetooth/peer_manager/modules/pm_buffer.c +++ b/lib/bluetooth/peer_manager/modules/pm_buffer.c @@ -10,8 +10,8 @@ #include #include -#define BUFFER_IS_VALID(p_buffer) \ - ((p_buffer != NULL) && (p_buffer->p_memory != NULL) && (p_buffer->p_mutex != NULL)) +#define BUFFER_IS_VALID(buffer) \ + ((buffer != NULL) && (buffer->memory != NULL) && (buffer->mutex != NULL)) static bool mutex_lock(atomic_t *mutex, int index) { @@ -28,17 +28,17 @@ static bool mutex_lock_status_get(atomic_t *mutex, int index) return atomic_test_bit(mutex, index); } -uint32_t pm_buffer_init(pm_buffer_t *p_buffer, uint8_t *p_buffer_memory, - uint32_t buffer_memory_size, atomic_t *p_mutex_memory, +uint32_t pm_buffer_init(struct pm_buffer *buffer, uint8_t *buffer_memory, + uint32_t buffer_memory_size, atomic_t *mutex_memory, uint32_t n_blocks, uint32_t block_size) { - if ((p_buffer != NULL) && (p_buffer_memory != NULL) && (p_mutex_memory != NULL) && + if ((buffer != NULL) && (buffer_memory != NULL) && (mutex_memory != NULL) && (buffer_memory_size >= (n_blocks * block_size)) && (n_blocks != 0) && (block_size != 0)) { - p_buffer->p_memory = p_buffer_memory; - p_buffer->p_mutex = p_mutex_memory; - p_buffer->n_blocks = n_blocks; - p_buffer->block_size = block_size; + buffer->memory = buffer_memory; + buffer->mutex = mutex_memory; + buffer->n_blocks = n_blocks; + buffer->block_size = block_size; return NRF_SUCCESS; } else { @@ -46,16 +46,16 @@ uint32_t pm_buffer_init(pm_buffer_t *p_buffer, uint8_t *p_buffer_memory, } } -uint8_t pm_buffer_block_acquire(pm_buffer_t *p_buffer, uint32_t n_blocks) +uint8_t pm_buffer_block_acquire(struct pm_buffer *buffer, uint32_t n_blocks) { - if (!BUFFER_IS_VALID(p_buffer)) { + if (!BUFFER_IS_VALID(buffer)) { return PM_BUFFER_INVALID_ID; } uint8_t first_locked_mutex = PM_BUFFER_INVALID_ID; - for (uint8_t i = 0; i < p_buffer->n_blocks; i++) { - if (mutex_lock(p_buffer->p_mutex, i)) { + for (uint8_t i = 0; i < buffer->n_blocks; i++) { + if (mutex_lock(buffer->mutex, i)) { if (first_locked_mutex == PM_BUFFER_INVALID_ID) { first_locked_mutex = i; } @@ -64,7 +64,7 @@ uint8_t pm_buffer_block_acquire(pm_buffer_t *p_buffer, uint32_t n_blocks) } } else if (first_locked_mutex != PM_BUFFER_INVALID_ID) { for (uint8_t j = first_locked_mutex; j < i; j++) { - pm_buffer_release(p_buffer, j); + pm_buffer_release(buffer, j); } first_locked_mutex = PM_BUFFER_INVALID_ID; } @@ -73,23 +73,23 @@ uint8_t pm_buffer_block_acquire(pm_buffer_t *p_buffer, uint32_t n_blocks) return (PM_BUFFER_INVALID_ID); } -uint8_t *pm_buffer_ptr_get(pm_buffer_t *p_buffer, uint8_t id) +uint8_t *pm_buffer_ptr_get(struct pm_buffer *buffer, uint8_t id) { - if (!BUFFER_IS_VALID(p_buffer)) { + if (!BUFFER_IS_VALID(buffer)) { return NULL; } - if ((id != PM_BUFFER_INVALID_ID) && mutex_lock_status_get(p_buffer->p_mutex, id)) { - return &p_buffer->p_memory[id * p_buffer->block_size]; + if ((id != PM_BUFFER_INVALID_ID) && mutex_lock_status_get(buffer->mutex, id)) { + return &buffer->memory[id * buffer->block_size]; } else { return NULL; } } -void pm_buffer_release(pm_buffer_t *p_buffer, uint8_t id) +void pm_buffer_release(struct pm_buffer *buffer, uint8_t id) { - if (BUFFER_IS_VALID(p_buffer) && (id != PM_BUFFER_INVALID_ID) && - mutex_lock_status_get(p_buffer->p_mutex, id)) { - mutex_unlock(p_buffer->p_mutex, id); + if (BUFFER_IS_VALID(buffer) && (id != PM_BUFFER_INVALID_ID) && + mutex_lock_status_get(buffer->mutex, id)) { + mutex_unlock(buffer->mutex, id); } } diff --git a/lib/bluetooth/peer_manager/modules/security_dispatcher.c b/lib/bluetooth/peer_manager/modules/security_dispatcher.c index 696740bc6c..d91637d4c3 100644 --- a/lib/bluetooth/peer_manager/modules/security_dispatcher.c +++ b/lib/bluetooth/peer_manager/modules/security_dispatcher.c @@ -24,60 +24,61 @@ #include #include +#include LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); /* The number of registered event handlers. */ -#define SMD_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define SMD_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* Security Dispacher event handlers in Security Manager and GATT Cache Manager. */ -extern void sm_smd_evt_handler(pm_evt_t *p_event); +extern void sm_smd_evt_handler(struct pm_evt *event); /* Security Dispatcher events' handlers. * The number of elements in this array is SMD_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t const m_evt_handlers[] = {sm_smd_evt_handler}; +static const pm_evt_handler_internal_t evt_handlers[] = {sm_smd_evt_handler}; -static bool m_module_initialized; +static bool module_initialized; -static int m_flag_sec_proc = BLE_CONN_STATE_USER_FLAG_INVALID; -static int m_flag_sec_proc_pairing = BLE_CONN_STATE_USER_FLAG_INVALID; -static int m_flag_sec_proc_bonding = BLE_CONN_STATE_USER_FLAG_INVALID; -static int m_flag_allow_repairing = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_sec_proc = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_sec_proc_pairing = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_sec_proc_bonding = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_allow_repairing = BLE_CONN_STATE_USER_FLAG_INVALID; -static ble_gap_lesc_p256_pk_t m_peer_pk; +static ble_gap_lesc_p256_pk_t peer_pk; static __INLINE bool sec_procedure(uint16_t conn_handle) { - return ble_conn_state_user_flag_get(conn_handle, m_flag_sec_proc); + return ble_conn_state_user_flag_get(conn_handle, flag_sec_proc); } static __INLINE bool pairing(uint16_t conn_handle) { - return ble_conn_state_user_flag_get(conn_handle, m_flag_sec_proc_pairing); + return ble_conn_state_user_flag_get(conn_handle, flag_sec_proc_pairing); } static __INLINE bool bonding(uint16_t conn_handle) { - return ble_conn_state_user_flag_get(conn_handle, m_flag_sec_proc_bonding); + return ble_conn_state_user_flag_get(conn_handle, flag_sec_proc_bonding); } static __INLINE bool allow_repairing(uint16_t conn_handle) { - return ble_conn_state_user_flag_get(conn_handle, m_flag_allow_repairing); + return ble_conn_state_user_flag_get(conn_handle, flag_allow_repairing); } /** * @brief Function for sending an SMD event to all event handlers. * - * @param[in] p_event The event to pass to all event handlers. + * @param[in] event The event to pass to all event handlers. */ -static void evt_send(pm_evt_t *p_event) +static void evt_send(struct pm_evt *event) { - p_event->peer_id = im_peer_id_get_by_conn_handle(p_event->conn_handle); + event->peer_id = im_peer_id_get_by_conn_handle(event->conn_handle); for (uint32_t i = 0; i < SMD_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_event); + evt_handlers[i](event); } } @@ -87,11 +88,15 @@ static void evt_send(pm_evt_t *p_event) * @param[in] conn_handle The connection handle the event pertains to. * @param[in] procedure The procedure that has started on the connection. */ -static void sec_start_send(uint16_t conn_handle, pm_conn_sec_procedure_t procedure) +static void sec_start_send(uint16_t conn_handle, enum pm_conn_sec_procedure procedure) { - pm_evt_t evt = {.evt_id = PM_EVT_CONN_SEC_START, - .conn_handle = conn_handle, - .params = {.conn_sec_start = {.procedure = procedure}}}; + struct pm_evt evt = { + .evt_id = PM_EVT_CONN_SEC_START, + .conn_handle = conn_handle, + .params.conn_sec_start = { + .procedure = procedure, + }, + }; evt_send(&evt); } @@ -104,11 +109,13 @@ static void sec_start_send(uint16_t conn_handle, pm_conn_sec_procedure_t procedu */ static void send_unexpected_error(uint16_t conn_handle, uint32_t err_code) { - pm_evt_t error_evt = {.evt_id = PM_EVT_ERROR_UNEXPECTED, - .conn_handle = conn_handle, - .params = {.error_unexpected = { - .error = err_code, - }}}; + struct pm_evt error_evt = { + .evt_id = PM_EVT_ERROR_UNEXPECTED, + .conn_handle = conn_handle, + .params.error_unexpected = { + .error = err_code, + }, + }; evt_send(&error_evt); } @@ -120,7 +127,7 @@ static void send_unexpected_error(uint16_t conn_handle, uint32_t err_code) */ static void send_storage_full_evt(uint16_t conn_handle) { - pm_evt_t evt = {.evt_id = PM_EVT_STORAGE_FULL, .conn_handle = conn_handle}; + struct pm_evt evt = {.evt_id = PM_EVT_STORAGE_FULL, .conn_handle = conn_handle}; evt_send(&evt); } @@ -130,21 +137,23 @@ static void send_storage_full_evt(uint16_t conn_handle) * * @param[in] conn_handle The handle of the connection the security procedure happens on. * @param[in] procedure The procedure that failed. - * @param[in] error The error the procedure failed with. + * @param[in] error The error the procedure failed with. See @ref PM_SEC_ERRORS. * @param[in] error_src The party that raised the error. See @ref BLE_GAP_SEC_STATUS_SOURCES. */ -static void conn_sec_failure(uint16_t conn_handle, pm_conn_sec_procedure_t procedure, - pm_sec_error_code_t error, uint8_t error_src) +static void conn_sec_failure(uint16_t conn_handle, enum pm_conn_sec_procedure procedure, + uint16_t error, uint8_t error_src) { - pm_evt_t evt = {.evt_id = PM_EVT_CONN_SEC_FAILED, - .conn_handle = conn_handle, - .params = {.conn_sec_failed = { - .procedure = procedure, - .error = error, - .error_src = error_src, - }}}; + struct pm_evt evt = { + .evt_id = PM_EVT_CONN_SEC_FAILED, + .conn_handle = conn_handle, + .params.conn_sec_failed = { + .procedure = procedure, + .error = error, + .error_src = error_src, + }, + }; - ble_conn_state_user_flag_set(conn_handle, m_flag_sec_proc, false); + ble_conn_state_user_flag_set(conn_handle, flag_sec_proc, false); evt_send(&evt); } @@ -153,16 +162,16 @@ static void conn_sec_failure(uint16_t conn_handle, pm_conn_sec_procedure_t proce * @brief Function for cleaning up after a failed pairing procedure. * * @param[in] conn_handle The handle of the connection the pairing procedure happens on. - * @param[in] error The error the procedure failed with. + * @param[in] error The error the procedure failed with. See @ref PM_SEC_ERRORS. * @param[in] error_src The source of the error (local or remote). See @ref * BLE_GAP_SEC_STATUS_SOURCES. */ -static void pairing_failure(uint16_t conn_handle, pm_sec_error_code_t error, uint8_t error_src) +static void pairing_failure(uint16_t conn_handle, uint16_t error, uint8_t error_src) { uint32_t err_code = NRF_SUCCESS; - pm_conn_sec_procedure_t procedure = bonding(conn_handle) ? PM_CONN_SEC_PROCEDURE_BONDING - : PM_CONN_SEC_PROCEDURE_PAIRING; - pm_peer_id_t temp_peer_id; + enum pm_conn_sec_procedure procedure = bonding(conn_handle) ? PM_CONN_SEC_PROCEDURE_BONDING + : PM_CONN_SEC_PROCEDURE_PAIRING; + uint16_t temp_peer_id; err_code = pdb_temp_peer_id_get(conn_handle, &temp_peer_id); if (err_code == NRF_SUCCESS) { @@ -184,11 +193,10 @@ static void pairing_failure(uint16_t conn_handle, pm_sec_error_code_t error, uin * @brief Function for cleaning up after a failed encryption procedure. * * @param[in] conn_handle The handle of the connection the encryption procedure happens on. - * @param[in] error The error the procedure failed with. + * @param[in] error The error the procedure failed with. See @ref PM_SEC_ERRORS. * @param[in] error_src The party that raised the error. See @ref BLE_GAP_SEC_STATUS_SOURCES. */ -static __INLINE void encryption_failure(uint16_t conn_handle, pm_sec_error_code_t error, - uint8_t error_src) +static __INLINE void encryption_failure(uint16_t conn_handle, uint16_t error, uint8_t error_src) { conn_sec_failure(conn_handle, PM_CONN_SEC_PROCEDURE_ENCRYPTION, error, error_src); } @@ -197,10 +205,10 @@ static __INLINE void encryption_failure(uint16_t conn_handle, pm_sec_error_code_ * @brief Function for possibly cleaning up after a failed pairing or encryption procedure. * * @param[in] conn_handle The handle of the connection the pairing procedure happens on. - * @param[in] error The error the procedure failed with. + * @param[in] error The error the procedure failed with. See @ref PM_SEC_ERRORS. * @param[in] error_src The party that raised the error. See @ref BLE_GAP_SEC_STATUS_SOURCES. */ -static void link_secure_failure(uint16_t conn_handle, pm_sec_error_code_t error, uint8_t error_src) +static void link_secure_failure(uint16_t conn_handle, uint16_t error, uint8_t error_src) { if (sec_procedure(conn_handle)) { if (pairing(conn_handle)) { @@ -218,13 +226,13 @@ static void link_secure_failure(uint16_t conn_handle, pm_sec_error_code_t error, * @param[in] success Whether the procedure was started successfully. * @param[in] procedure The procedure that was started. */ -static void sec_proc_start(uint16_t conn_handle, bool success, pm_conn_sec_procedure_t procedure) +static void sec_proc_start(uint16_t conn_handle, bool success, enum pm_conn_sec_procedure procedure) { - ble_conn_state_user_flag_set(conn_handle, m_flag_sec_proc, success); + ble_conn_state_user_flag_set(conn_handle, flag_sec_proc, success); if (success) { - ble_conn_state_user_flag_set(conn_handle, m_flag_sec_proc_pairing, + ble_conn_state_user_flag_set(conn_handle, flag_sec_proc_pairing, (procedure != PM_CONN_SEC_PROCEDURE_ENCRYPTION)); - ble_conn_state_user_flag_set(conn_handle, m_flag_sec_proc_bonding, + ble_conn_state_user_flag_set(conn_handle, flag_sec_proc_bonding, (procedure == PM_CONN_SEC_PROCEDURE_BONDING)); sec_start_send(conn_handle, procedure); } @@ -235,28 +243,28 @@ static void sec_proc_start(uint16_t conn_handle, bool success, pm_conn_sec_proce * @brief Function for initiating encryption as a central. See @ref smd_link_secure for more * info. */ -static uint32_t link_secure_central_encryption(uint16_t conn_handle, pm_peer_id_t peer_id) +static uint32_t link_secure_central_encryption(uint16_t conn_handle, uint16_t peer_id) { - pm_peer_data_flash_t peer_data; + struct pm_peer_data_const peer_data; uint32_t err_code; - ble_gap_enc_key_t const *p_existing_key = NULL; + const ble_gap_enc_key_t *existing_key = NULL; bool lesc = false; - pm_peer_data_bonding_t bonding_data = { 0 }; + struct pm_peer_data_bonding bonding_data = { 0 }; - peer_data.p_all_data = &bonding_data; + peer_data.all_data = &bonding_data; err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_BONDING, &peer_data, - sizeof(pm_peer_data_bonding_t)); + sizeof(struct pm_peer_data_bonding)); if (err_code == NRF_SUCCESS) { /* Use peer's key since they are peripheral. */ - p_existing_key = &(bonding_data.peer_ltk); + existing_key = &(bonding_data.peer_ltk); lesc = bonding_data.own_ltk.enc_info.lesc; /* LESC was used during bonding. */ if (lesc) { /* For LESC, always use own key. */ - p_existing_key = &(bonding_data.own_ltk); + existing_key = &(bonding_data.own_ltk); } } @@ -272,15 +280,15 @@ static uint32_t link_secure_central_encryption(uint16_t conn_handle, pm_peer_id_ * bonding procedure is in ongoing, or that the records * in flash are in a bad state. */ - } else if (p_existing_key == NULL) { + } else if (existing_key == NULL) { err_code = NRF_ERROR_BUSY; - } else if (!lesc && !im_master_id_is_valid(&(p_existing_key->master_id))) { + } else if (!lesc && !im_master_id_is_valid(&(existing_key->master_id))) { /* No LTK to encrypt with. */ err_code = NRF_ERROR_INVALID_DATA; } else { /* Encrypt with existing LTK. */ - err_code = sd_ble_gap_encrypt(conn_handle, &(p_existing_key->master_id), - &(p_existing_key->enc_info)); + err_code = sd_ble_gap_encrypt(conn_handle, &(existing_key->master_id), + &(existing_key->enc_info)); } sec_proc_start(conn_handle, err_code == NRF_SUCCESS, PM_CONN_SEC_PROCEDURE_ENCRYPTION); @@ -289,20 +297,20 @@ static uint32_t link_secure_central_encryption(uint16_t conn_handle, pm_peer_id_ } /** @brief Function for intiating security as a central. See @ref smd_link_secure for more info. */ -static uint32_t link_secure_central(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, +static uint32_t link_secure_central(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, bool force_repairing) { uint32_t err_code; - pm_peer_id_t peer_id; + uint16_t peer_id; - if (p_sec_params == NULL) { + if (sec_params == NULL) { return link_secure_authenticate(conn_handle, NULL); } /* Set the default value for allowing repairing at the start of the sec proc. * (for central) */ - ble_conn_state_user_flag_set(conn_handle, m_flag_allow_repairing, force_repairing); + ble_conn_state_user_flag_set(conn_handle, flag_allow_repairing, force_repairing); peer_id = im_peer_id_get_by_conn_handle(conn_handle); @@ -315,10 +323,10 @@ static uint32_t link_secure_central(uint16_t conn_handle, ble_gap_sec_params_t * /* There are no existing keys, or repairing has been explicitly requested, so * pairing (possibly including bonding) will be performed to encrypt the link. */ - err_code = link_secure_authenticate(conn_handle, p_sec_params); - pm_conn_sec_procedure_t procedure = (p_sec_params && p_sec_params->bond) - ? PM_CONN_SEC_PROCEDURE_BONDING - : PM_CONN_SEC_PROCEDURE_PAIRING; + err_code = link_secure_authenticate(conn_handle, sec_params); + enum pm_conn_sec_procedure procedure = (sec_params && sec_params->bond) + ? PM_CONN_SEC_PROCEDURE_BONDING + : PM_CONN_SEC_PROCEDURE_PAIRING; sec_proc_start(conn_handle, err_code == NRF_SUCCESS, procedure); } @@ -328,18 +336,21 @@ static uint32_t link_secure_central(uint16_t conn_handle, ble_gap_sec_params_t * /** * @brief Function for processing the @ref BLE_GAP_EVT_SEC_REQUEST event from the SoftDevice. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void sec_request_process(ble_gap_evt_t const *p_gap_evt) +static void sec_request_process(const ble_gap_evt_t *gap_evt) { - if (sec_procedure(p_gap_evt->conn_handle)) { + if (sec_procedure(gap_evt->conn_handle)) { /* Ignore request as per spec. */ return; } - pm_evt_t evt = {.evt_id = PM_EVT_SLAVE_SECURITY_REQ, .conn_handle = p_gap_evt->conn_handle}; + struct pm_evt evt = { + .evt_id = PM_EVT_SLAVE_SECURITY_REQ, + .conn_handle = gap_evt->conn_handle + }; - memcpy(&evt.params.slave_security_req, &p_gap_evt->params.sec_request, + memcpy(&evt.params.slave_security_req, &gap_evt->params.sec_request, sizeof(ble_gap_evt_sec_request_t)); evt_send(&evt); @@ -349,53 +360,53 @@ static void sec_request_process(ble_gap_evt_t const *p_gap_evt) #ifdef BLE_GAP_ROLE_PERIPH /** @brief Function for processing the @ref BLE_GAP_EVT_SEC_INFO_REQUEST event from the SoftDevice. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void sec_info_request_process(ble_gap_evt_t const *p_gap_evt) +static void sec_info_request_process(const ble_gap_evt_t *gap_evt) { uint32_t err_code; - ble_gap_enc_info_t const *p_enc_info = NULL; - pm_peer_data_t peer_data; - pm_peer_id_t peer_id = - im_peer_id_get_by_master_id(&p_gap_evt->params.sec_info_request.master_id); + const ble_gap_enc_info_t *enc_info = NULL; + struct pm_peer_data peer_data; + uint16_t peer_id = + im_peer_id_get_by_master_id(&gap_evt->params.sec_info_request.master_id); if (peer_id == PM_PEER_ID_INVALID) { - peer_id = im_peer_id_get_by_conn_handle(p_gap_evt->conn_handle); + peer_id = im_peer_id_get_by_conn_handle(gap_evt->conn_handle); } else { /* The peer might have been unrecognized until now (since connecting). E.g. if using * a random non-resolvable advertising address. Report the discovered peer ID just * in case. */ - im_new_peer_id(p_gap_evt->conn_handle, peer_id); + im_new_peer_id(gap_evt->conn_handle, peer_id); } - sec_proc_start(p_gap_evt->conn_handle, true, PM_CONN_SEC_PROCEDURE_ENCRYPTION); + sec_proc_start(gap_evt->conn_handle, true, PM_CONN_SEC_PROCEDURE_ENCRYPTION); - pm_peer_data_bonding_t bonding_data = { 0 }; - uint32_t bonding_data_size = sizeof(pm_peer_data_bonding_t); + struct pm_peer_data_bonding bonding_data = { 0 }; + uint32_t bonding_data_size = sizeof(struct pm_peer_data_bonding); if (peer_id != PM_PEER_ID_INVALID) { - peer_data.p_all_data = &bonding_data; + peer_data.all_data = &bonding_data; err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_BONDING, &peer_data, &bonding_data_size); if (err_code == NRF_SUCCESS) { /* There is stored bonding data for this peer. */ - ble_gap_enc_key_t const *p_existing_key = + const ble_gap_enc_key_t *existing_key = &bonding_data.own_ltk; - if (p_gap_evt->params.sec_info_request.enc_info && - (p_existing_key->enc_info.lesc || + if (gap_evt->params.sec_info_request.enc_info && + (existing_key->enc_info.lesc || im_master_ids_compare( - &p_existing_key->master_id, - &p_gap_evt->params.sec_info_request.master_id))) { - p_enc_info = &p_existing_key->enc_info; + &existing_key->master_id, + &gap_evt->params.sec_info_request.master_id))) { + enc_info = &existing_key->enc_info; } } } - err_code = sd_ble_gap_sec_info_reply(p_gap_evt->conn_handle, p_enc_info, NULL, NULL); + err_code = sd_ble_gap_sec_info_reply(gap_evt->conn_handle, enc_info, NULL, NULL); if (err_code == NRF_ERROR_INVALID_STATE) { /* Do nothing. If disconnecting, it will be caught later by the handling of the @@ -408,10 +419,10 @@ static void sec_info_request_process(ble_gap_evt_t const *p_gap_evt) } else if (err_code != NRF_SUCCESS) { LOG_ERR("Could not complete encryption procedure. sd_ble_gap_sec_info_reply() " "returned %s. conn_handle: %d, peer_id: %d.", - nrf_strerror_get(err_code), p_gap_evt->conn_handle, peer_id); - send_unexpected_error(p_gap_evt->conn_handle, err_code); - } else if (p_gap_evt->params.sec_info_request.enc_info && (p_enc_info == NULL)) { - encryption_failure(p_gap_evt->conn_handle, PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING, + nrf_strerror_get(err_code), gap_evt->conn_handle, peer_id); + send_unexpected_error(gap_evt->conn_handle, err_code); + } else if (gap_evt->params.sec_info_request.enc_info && (enc_info == NULL)) { + encryption_failure(gap_evt->conn_handle, PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING, BLE_GAP_SEC_STATUS_SOURCE_LOCAL); } } @@ -424,7 +435,7 @@ static void sec_info_request_process(ble_gap_evt_t const *p_gap_evt) */ static void send_config_req(uint16_t conn_handle) { - pm_evt_t evt; + struct pm_evt evt; memset(&evt, 0, sizeof(evt)); @@ -434,45 +445,44 @@ static void send_config_req(uint16_t conn_handle) evt_send(&evt); } -void smd_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_conn_sec_config) +void smd_conn_sec_config_reply(uint16_t conn_handle, struct pm_conn_sec_config *conn_sec_config) { - NRF_PM_DEBUG_CHECK(m_module_initialized); - NRF_PM_DEBUG_CHECK(p_conn_sec_config != NULL); + __ASSERT_NO_MSG(module_initialized); + __ASSERT_NO_MSG(conn_sec_config != NULL); - ble_conn_state_user_flag_set(conn_handle, m_flag_allow_repairing, - p_conn_sec_config->allow_repairing); + ble_conn_state_user_flag_set(conn_handle, flag_allow_repairing, + conn_sec_config->allow_repairing); } /** * @brief Function for processing the @ref BLE_GAP_EVT_DISCONNECT event from the SoftDevice. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void disconnect_process(ble_gap_evt_t const *p_gap_evt) +static void disconnect_process(const ble_gap_evt_t *gap_evt) { - pm_sec_error_code_t error = (p_gap_evt->params.disconnected.reason == - BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE) - ? PM_CONN_SEC_ERROR_MIC_FAILURE - : PM_CONN_SEC_ERROR_DISCONNECT; + uint16_t error = (gap_evt->params.disconnected.reason == + BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE) + ? PM_CONN_SEC_ERROR_MIC_FAILURE : PM_CONN_SEC_ERROR_DISCONNECT; - link_secure_failure(p_gap_evt->conn_handle, error, BLE_GAP_SEC_STATUS_SOURCE_LOCAL); + link_secure_failure(gap_evt->conn_handle, error, BLE_GAP_SEC_STATUS_SOURCE_LOCAL); } /** * @brief Function for sending a PARAMS_REQ event. * * @param[in] conn_handle The connection the security parameters are needed for. - * @param[in] p_peer_params The security parameters from the peer. Can be NULL if the peer's + * @param[in] peer_params The security parameters from the peer. Can be NULL if the peer's * parameters are not yet available. */ -static void send_params_req(uint16_t conn_handle, ble_gap_sec_params_t const *p_peer_params) +static void send_params_req(uint16_t conn_handle, const ble_gap_sec_params_t *peer_params) { - pm_evt_t evt = { + struct pm_evt evt = { .evt_id = PM_EVT_CONN_SEC_PARAMS_REQ, .conn_handle = conn_handle, - .params = { - .conn_sec_params_req = {.peer_params = p_peer_params}, - }, + .params.conn_sec_params_req = { + .peer_params = peer_params, + }, }; evt_send(&evt); @@ -481,61 +491,62 @@ static void send_params_req(uint16_t conn_handle, ble_gap_sec_params_t const *p_ /** * @brief Function for processing the @ref BLE_GAP_EVT_SEC_PARAMS_REQUEST event from the SoftDevice. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void sec_params_request_process(ble_gap_evt_t const *p_gap_evt) +static void sec_params_request_process(const ble_gap_evt_t *gap_evt) { #ifdef BLE_GAP_ROLE_PERIPH - if (ble_conn_state_role(p_gap_evt->conn_handle) == BLE_GAP_ROLE_PERIPH) { - sec_proc_start(p_gap_evt->conn_handle, true, - p_gap_evt->params.sec_params_request.peer_params.bond + if (ble_conn_state_role(gap_evt->conn_handle) == BLE_GAP_ROLE_PERIPH) { + sec_proc_start(gap_evt->conn_handle, true, + gap_evt->params.sec_params_request.peer_params.bond ? PM_CONN_SEC_PROCEDURE_BONDING : PM_CONN_SEC_PROCEDURE_PAIRING); } #endif /* BLE_GAP_ROLE_PERIPH */ - send_params_req(p_gap_evt->conn_handle, &p_gap_evt->params.sec_params_request.peer_params); + send_params_req(gap_evt->conn_handle, &gap_evt->params.sec_params_request.peer_params); } /** * @brief Function for sending a Peer Manager event indicating that pairing has succeeded. * - * @param[in] p_gap_evt The AUTH_STATUS event from the SoftDevice that triggered this. + * @param[in] gap_evt The AUTH_STATUS event from the SoftDevice that triggered this. * @param[in] data_stored Whether bonding data was stored. */ -static void pairing_success_evt_send(ble_gap_evt_t const *p_gap_evt, bool data_stored) -{ - pm_evt_t pairing_success_evt; - - pairing_success_evt.evt_id = PM_EVT_CONN_SEC_SUCCEEDED; - pairing_success_evt.conn_handle = p_gap_evt->conn_handle; - pairing_success_evt.params.conn_sec_succeeded.procedure = - p_gap_evt->params.auth_status.bonded ? PM_CONN_SEC_PROCEDURE_BONDING - : PM_CONN_SEC_PROCEDURE_PAIRING; - pairing_success_evt.params.conn_sec_succeeded.data_stored = data_stored; +static void pairing_success_evt_send(const ble_gap_evt_t *gap_evt, bool data_stored) +{ + struct pm_evt evt = { + .evt_id = PM_EVT_CONN_SEC_SUCCEEDED, + .conn_handle = gap_evt->conn_handle, + .params.conn_sec_succeeded = { + .procedure = gap_evt->params.auth_status.bonded + ? PM_CONN_SEC_PROCEDURE_BONDING : PM_CONN_SEC_PROCEDURE_PAIRING, + .data_stored = data_stored, + }, + }; - evt_send(&pairing_success_evt); + evt_send(&evt); } /** * @brief Function for processing the @ref BLE_GAP_EVT_AUTH_STATUS event from the SoftDevice, when * the auth_status is success. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void auth_status_success_process(ble_gap_evt_t const *p_gap_evt) +static void auth_status_success_process(const ble_gap_evt_t *gap_evt) { uint32_t err_code; - uint16_t conn_handle = p_gap_evt->conn_handle; - pm_peer_id_t peer_id; - pm_peer_id_t temp_peer_id; - pm_peer_data_t peer_data; + uint16_t conn_handle = gap_evt->conn_handle; + uint16_t peer_id; + uint16_t temp_peer_id; + struct pm_peer_data peer_data; bool new_peer_id = false; - ble_conn_state_user_flag_set(conn_handle, m_flag_sec_proc, false); + ble_conn_state_user_flag_set(conn_handle, flag_sec_proc, false); - if (!p_gap_evt->params.auth_status.bonded) { - pairing_success_evt_send(p_gap_evt, false); + if (!gap_evt->params.auth_status.bonded) { + pairing_success_evt_send(gap_evt, false); return; } @@ -549,14 +560,14 @@ static void auth_status_success_process(ble_gap_evt_t const *p_gap_evt) "returned %s. conn_handle: %d.", nrf_strerror_get(err_code), conn_handle); send_unexpected_error(conn_handle, err_code); - pairing_success_evt_send(p_gap_evt, false); + pairing_success_evt_send(gap_evt, false); return; } peer_id = im_peer_id_get_by_conn_handle(conn_handle); if (peer_id == PM_PEER_ID_INVALID) { - peer_id = im_find_duplicate_bonding_data(peer_data.p_bonding_data, + peer_id = im_find_duplicate_bonding_data(peer_data.bonding_data, PM_PEER_ID_INVALID); if (peer_id != PM_PEER_ID_INVALID) { @@ -567,7 +578,7 @@ static void auth_status_success_process(ble_gap_evt_t const *p_gap_evt) if (!allow_repairing(conn_handle)) { send_config_req(conn_handle); if (!allow_repairing(conn_handle)) { - pairing_success_evt_send(p_gap_evt, false); + pairing_success_evt_send(gap_evt, false); return; } } @@ -579,7 +590,7 @@ static void auth_status_success_process(ble_gap_evt_t const *p_gap_evt) if (peer_id == PM_PEER_ID_INVALID) { LOG_ERR("Could not allocate new peer_id for incoming bond."); send_unexpected_error(conn_handle, NRF_ERROR_NO_MEM); - pairing_success_evt_send(p_gap_evt, false); + pairing_success_evt_send(gap_evt, false); return; } im_new_peer_id(conn_handle, peer_id); @@ -589,17 +600,17 @@ static void auth_status_success_process(ble_gap_evt_t const *p_gap_evt) err_code = pdb_write_buf_store(temp_peer_id, PM_PEER_DATA_ID_BONDING, peer_id); if (err_code == NRF_SUCCESS) { - pairing_success_evt_send(p_gap_evt, true); + pairing_success_evt_send(gap_evt, true); } else if (err_code == NRF_ERROR_RESOURCES) { send_storage_full_evt(conn_handle); - pairing_success_evt_send(p_gap_evt, true); + pairing_success_evt_send(gap_evt, true); } else { /* Unexpected error */ LOG_ERR("Could not store bond. pdb_write_buf_store() returned %s. " "conn_handle: %d, peer_id: %d", nrf_strerror_get(err_code), conn_handle, peer_id); send_unexpected_error(conn_handle, err_code); - pairing_success_evt_send(p_gap_evt, false); + pairing_success_evt_send(gap_evt, false); if (new_peer_id) { /* Unused return value. We are already in a bad state. */ (void)im_peer_free(peer_id); @@ -611,30 +622,30 @@ static void auth_status_success_process(ble_gap_evt_t const *p_gap_evt) * @brief Function for processing the @ref BLE_GAP_EVT_AUTH_STATUS event from the SoftDevice, when * the auth_status is failure. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void auth_status_failure_process(ble_gap_evt_t const *p_gap_evt) +static void auth_status_failure_process(const ble_gap_evt_t *gap_evt) { - link_secure_failure(p_gap_evt->conn_handle, p_gap_evt->params.auth_status.auth_status, - p_gap_evt->params.auth_status.error_src); + link_secure_failure(gap_evt->conn_handle, gap_evt->params.auth_status.auth_status, + gap_evt->params.auth_status.error_src); } /** * @brief Function for processing the @ref BLE_GAP_EVT_AUTH_STATUS event from the SoftDevice. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void auth_status_process(ble_gap_evt_t const *p_gap_evt) +static void auth_status_process(const ble_gap_evt_t *gap_evt) { - switch (p_gap_evt->params.auth_status.auth_status) { + switch (gap_evt->params.auth_status.auth_status) { case BLE_GAP_SEC_STATUS_SUCCESS: - auth_status_success_process(p_gap_evt); + auth_status_success_process(gap_evt); break; default: - auth_status_failure_process(p_gap_evt); + auth_status_failure_process(gap_evt); #if defined(CONFIG_PM_RA_PROTECTION) - ast_auth_error_notify(p_gap_evt->conn_handle); + ast_auth_error_notify(gap_evt->conn_handle); #endif /* CONFIG_PM_RA_PROTECTION */ break; } @@ -643,28 +654,30 @@ static void auth_status_process(ble_gap_evt_t const *p_gap_evt) /** * @brief Function for processing the @ref BLE_GAP_EVT_CONN_SEC_UPDATE event from the SoftDevice. * - * @param[in] p_gap_evt The event from the SoftDevice. + * @param[in] gap_evt The event from the SoftDevice. */ -static void conn_sec_update_process(ble_gap_evt_t const *p_gap_evt) +static void conn_sec_update_process(const ble_gap_evt_t *gap_evt) { - if (!pairing(p_gap_evt->conn_handle)) { + if (!pairing(gap_evt->conn_handle)) { /* This is an encryption procedure (not pairing), so this event marks the end of the * procedure. */ - if (!ble_conn_state_encrypted(p_gap_evt->conn_handle)) { - encryption_failure(p_gap_evt->conn_handle, + if (!ble_conn_state_encrypted(gap_evt->conn_handle)) { + encryption_failure(gap_evt->conn_handle, PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING, BLE_GAP_SEC_STATUS_SOURCE_REMOTE); } else { - ble_conn_state_user_flag_set(p_gap_evt->conn_handle, m_flag_sec_proc, + ble_conn_state_user_flag_set(gap_evt->conn_handle, flag_sec_proc, false); - pm_evt_t evt; - - evt.evt_id = PM_EVT_CONN_SEC_SUCCEEDED; - evt.conn_handle = p_gap_evt->conn_handle; - evt.params.conn_sec_succeeded.procedure = PM_CONN_SEC_PROCEDURE_ENCRYPTION; - evt.params.conn_sec_succeeded.data_stored = false; + struct pm_evt evt = { + .evt_id = PM_EVT_CONN_SEC_SUCCEEDED, + .conn_handle = gap_evt->conn_handle, + .params.conn_sec_succeeded = { + .procedure = PM_CONN_SEC_PROCEDURE_ENCRYPTION, + .data_stored = false, + }, + }; evt_send(&evt); } @@ -676,26 +689,26 @@ static void conn_sec_update_process(ble_gap_evt_t const *p_gap_evt) * * @param[out] flag_id The flag to initialize. */ -static void flag_id_init(int *p_flag_id) +static void flag_id_init(int *flag_id) { - if (*p_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { - *p_flag_id = ble_conn_state_user_flag_acquire(); + if (*flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { + *flag_id = ble_conn_state_user_flag_acquire(); } } uint32_t smd_init(void) { - NRF_PM_DEBUG_CHECK(!m_module_initialized); + __ASSERT_NO_MSG(!module_initialized); - flag_id_init(&m_flag_sec_proc); - flag_id_init(&m_flag_sec_proc_pairing); - flag_id_init(&m_flag_sec_proc_bonding); - flag_id_init(&m_flag_allow_repairing); + flag_id_init(&flag_sec_proc); + flag_id_init(&flag_sec_proc_pairing); + flag_id_init(&flag_sec_proc_bonding); + flag_id_init(&flag_allow_repairing); - if ((m_flag_sec_proc == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_sec_proc_pairing == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_sec_proc_bonding == BLE_CONN_STATE_USER_FLAG_INVALID) || - (m_flag_allow_repairing == BLE_CONN_STATE_USER_FLAG_INVALID)) { + if ((flag_sec_proc == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_sec_proc_pairing == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_sec_proc_bonding == BLE_CONN_STATE_USER_FLAG_INVALID) || + (flag_allow_repairing == BLE_CONN_STATE_USER_FLAG_INVALID)) { LOG_ERR("Could not acquire conn_state user flags. Increase " "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module."); return NRF_ERROR_INTERNAL; @@ -709,7 +722,7 @@ uint32_t smd_init(void) } #endif /* CONFIG_PM_RA_PROTECTION */ - m_module_initialized = true; + module_initialized = true; return NRF_SUCCESS; } @@ -718,10 +731,10 @@ uint32_t smd_init(void) * @brief Function for putting retrieving a buffer and putting pointers into a @ref * ble_gap_sec_keyset_t. * - * @param[in] conn_handle The connection the security procedure is happening on. - * @param[in] role Our role in the connection. - * @param[in] p_public_key Pointer to a buffer holding the public key, or NULL. - * @param[out] p_sec_keyset Pointer to the keyset to be filled. + * @param[in] conn_handle The connection the security procedure is happening on. + * @param[in] role Our role in the connection. + * @param[in] public_key Pointer to a buffer holding the public key, or NULL. + * @param[out] sec_keyset Pointer to the keyset to be filled. * * @retval NRF_SUCCESS Success. * @retval NRF_ERROR_BUSY Could not process request at this time. Reattempt later. @@ -730,15 +743,15 @@ uint32_t smd_init(void) * @retval NRF_ERROR_INTERNAL Fatal error. */ static uint32_t sec_keyset_fill(uint16_t conn_handle, uint8_t role, - ble_gap_lesc_p256_pk_t *p_public_key, - ble_gap_sec_keyset_t *p_sec_keyset) + ble_gap_lesc_p256_pk_t *public_key, + ble_gap_sec_keyset_t *sec_keyset) { uint32_t err_code; - pm_peer_id_t temp_peer_id; - pm_peer_data_t peer_data; + uint16_t temp_peer_id; + struct pm_peer_data peer_data; - if (p_sec_keyset == NULL) { - LOG_ERR("Internal error: %s received NULL for p_sec_keyset.", __func__); + if (sec_keyset == NULL) { + LOG_ERR("Internal error: %s received NULL for sec_keyset.", __func__); return NRF_ERROR_INTERNAL; } @@ -756,21 +769,21 @@ static uint32_t sec_keyset_fill(uint16_t conn_handle, uint8_t role, nrf_strerror_get(err_code), conn_handle); err_code = NRF_ERROR_INTERNAL; } else { - memset(peer_data.p_bonding_data, 0, sizeof(pm_peer_data_bonding_t)); + memset(peer_data.bonding_data, 0, sizeof(struct pm_peer_data_bonding)); - peer_data.p_bonding_data->own_role = role; + peer_data.bonding_data->own_role = role; - p_sec_keyset->keys_own.p_enc_key = &peer_data.p_bonding_data->own_ltk; - p_sec_keyset->keys_own.p_pk = p_public_key; - p_sec_keyset->keys_peer.p_enc_key = &peer_data.p_bonding_data->peer_ltk; - p_sec_keyset->keys_peer.p_id_key = &peer_data.p_bonding_data->peer_ble_id; - p_sec_keyset->keys_peer.p_pk = &m_peer_pk; + sec_keyset->keys_own.p_enc_key = &peer_data.bonding_data->own_ltk; + sec_keyset->keys_own.p_pk = public_key; + sec_keyset->keys_peer.p_enc_key = &peer_data.bonding_data->peer_ltk; + sec_keyset->keys_peer.p_id_key = &peer_data.bonding_data->peer_ble_id; + sec_keyset->keys_peer.p_pk = &peer_pk; /* Retrieve the address the peer used during connection establishment. * This address will be overwritten if ID is shared. Should not fail. */ err_code = im_ble_addr_get(conn_handle, - &peer_data.p_bonding_data->peer_ble_id.id_addr_info); + &peer_data.bonding_data->peer_ble_id.id_addr_info); if (err_code != NRF_SUCCESS) { LOG_WRN("im_ble_addr_get() returned %s. conn_handle: %d. Link was " "likely disconnected.", @@ -782,10 +795,10 @@ static uint32_t sec_keyset_fill(uint16_t conn_handle, uint8_t role, return err_code; } -uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, - ble_gap_lesc_p256_pk_t *p_public_key) +uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, + ble_gap_lesc_p256_pk_t *public_key) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); uint8_t role = ble_conn_state_role(conn_handle); uint32_t err_code = NRF_SUCCESS; @@ -798,7 +811,7 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para /* Set the default value for allowing repairing at the start of the sec proc. (for * peripheral) */ - ble_conn_state_user_flag_set(conn_handle, m_flag_allow_repairing, false); + ble_conn_state_user_flag_set(conn_handle, flag_allow_repairing, false); } #endif /* BLE_GAP_ROLE_PERIPH */ @@ -812,7 +825,7 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para sec_status = BLE_GAP_SEC_STATUS_REPEATED_ATTEMPTS; } else #endif /* CONFIG_PM_RA_PROTECTION */ - if (p_sec_params == NULL) { + if (sec_params == NULL) { /* NULL params means reject pairing. */ sec_status = BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP; } else { @@ -830,13 +843,13 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para } #endif /* BLE_GAP_ROLE_PERIPH */ - if (!p_sec_params->bond) { + if (!sec_params->bond) { /* Pairing, no bonding. */ - sec_keyset.keys_own.p_pk = p_public_key; - sec_keyset.keys_peer.p_pk = &m_peer_pk; + sec_keyset.keys_own.p_pk = public_key; + sec_keyset.keys_peer.p_pk = &peer_pk; } else if (sec_status != BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP) { /* Bonding is to be performed, prepare to receive bonding data. */ - err_code = sec_keyset_fill(conn_handle, role, p_public_key, + err_code = sec_keyset_fill(conn_handle, role, public_key, &sec_keyset); } } @@ -845,12 +858,12 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para /* Everything OK, reply to SoftDevice. If an error happened, the user is given an * opportunity to change the parameters and retry the call. */ - ble_gap_sec_params_t *p_aux_sec_params = NULL; + ble_gap_sec_params_t *aux_sec_params = NULL; #ifdef BLE_GAP_ROLE_PERIPH - p_aux_sec_params = (role == BLE_GAP_ROLE_PERIPH) ? p_sec_params : NULL; + aux_sec_params = (role == BLE_GAP_ROLE_PERIPH) ? sec_params : NULL; #endif /* BLE_GAP_ROLE_PERIPH */ - err_code = sd_ble_gap_sec_params_reply(conn_handle, sec_status, p_aux_sec_params, + err_code = sd_ble_gap_sec_params_reply(conn_handle, sec_status, aux_sec_params, &sec_keyset); } @@ -862,9 +875,9 @@ uint32_t smd_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_para * * See @ref smd_link_secure and @ref sd_ble_gap_authenticate for more information. */ -static uint32_t link_secure_authenticate(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params) +static uint32_t link_secure_authenticate(uint16_t conn_handle, ble_gap_sec_params_t *sec_params) { - uint32_t err_code = sd_ble_gap_authenticate(conn_handle, p_sec_params); + uint32_t err_code = sd_ble_gap_authenticate(conn_handle, sec_params); if (err_code == NRF_ERROR_NO_MEM) { /* sd_ble_gap_authenticate() returned NRF_ERROR_NO_MEM. Too many other sec @@ -880,29 +893,29 @@ static uint32_t link_secure_authenticate(uint16_t conn_handle, ble_gap_sec_param /** @brief Function for asking the central to secure the link. See @ref smd_link_secure for more * info. */ -static uint32_t link_secure_peripheral(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params) +static uint32_t link_secure_peripheral(uint16_t conn_handle, ble_gap_sec_params_t *sec_params) { uint32_t err_code = NRF_SUCCESS; - if (p_sec_params != NULL) { - err_code = link_secure_authenticate(conn_handle, p_sec_params); + if (sec_params != NULL) { + err_code = link_secure_authenticate(conn_handle, sec_params); } return err_code; } #endif -uint32_t smd_link_secure(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, +uint32_t smd_link_secure(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, bool force_repairing) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); uint8_t role = ble_conn_state_role(conn_handle); switch (role) { #ifdef BLE_GAP_ROLE_PERIPH case BLE_GAP_ROLE_PERIPH: - return link_secure_peripheral(conn_handle, p_sec_params); + return link_secure_peripheral(conn_handle, sec_params); #endif /* BLE_GAP_ROLE_PERIPH */ default: @@ -910,29 +923,29 @@ uint32_t smd_link_secure(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_param } } -void smd_ble_evt_handler(ble_evt_t const *p_ble_evt) +void smd_ble_evt_handler(const ble_evt_t *ble_evt) { - switch (p_ble_evt->header.evt_id) { + switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_DISCONNECTED: - disconnect_process(&(p_ble_evt->evt.gap_evt)); + disconnect_process(&(ble_evt->evt.gap_evt)); break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: - sec_params_request_process(&(p_ble_evt->evt.gap_evt)); + sec_params_request_process(&(ble_evt->evt.gap_evt)); break; #ifdef BLE_GAP_ROLE_PERIPH case BLE_GAP_EVT_SEC_INFO_REQUEST: - sec_info_request_process(&(p_ble_evt->evt.gap_evt)); + sec_info_request_process(&(ble_evt->evt.gap_evt)); break; #endif /* BLE_GAP_ROLE_PERIPH */ case BLE_GAP_EVT_AUTH_STATUS: - auth_status_process(&(p_ble_evt->evt.gap_evt)); + auth_status_process(&(ble_evt->evt.gap_evt)); break; case BLE_GAP_EVT_CONN_SEC_UPDATE: - conn_sec_update_process(&(p_ble_evt->evt.gap_evt)); + conn_sec_update_process(&(ble_evt->evt.gap_evt)); break; }; } diff --git a/lib/bluetooth/peer_manager/modules/security_manager.c b/lib/bluetooth/peer_manager/modules/security_manager.c index 2d4c152711..b6b112442f 100644 --- a/lib/bluetooth/peer_manager/modules/security_manager.c +++ b/lib/bluetooth/peer_manager/modules/security_manager.c @@ -12,8 +12,6 @@ #if defined(CONFIG_PM_LESC) #include #endif -#include - #include #include #include @@ -22,73 +20,74 @@ #include #include +#include LOG_MODULE_DECLARE(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); /* The number of registered event handlers. */ -#define SM_EVENT_HANDLERS_CNT ARRAY_SIZE(m_evt_handlers) +#define SM_EVENT_HANDLERS_CNT ARRAY_SIZE(evt_handlers) /* Security Manager event handler in Peer Manager. */ -extern void pm_sm_evt_handler(pm_evt_t *p_sm_evt); +extern void pm_sm_evt_handler(struct pm_evt *sm_evt); /* Security Manager events' handlers. * The number of elements in this array is SM_EVENT_HANDLERS_CNT. */ -static pm_evt_handler_internal_t const m_evt_handlers[] = {pm_sm_evt_handler}; +static const pm_evt_handler_internal_t evt_handlers[] = {pm_sm_evt_handler}; /* The context type that is used in PM_EVT_CONN_SEC_PARAMS_REQ events and in calls to * sm_sec_params_reply(). */ -typedef struct { +struct sec_params_reply_context { /* The security parameters to use in the call to the security_dispatcher */ - ble_gap_sec_params_t *p_sec_params; + ble_gap_sec_params_t *sec_params; /* The buffer for holding the security parameters. */ ble_gap_sec_params_t sec_params_mem; /* Whether @ref sm_sec_params_reply has been called for this context instance. */ bool params_reply_called; -} sec_params_reply_context_t; +}; /* Whether the Security Manager module has been initialized. */ -static bool m_module_initialized; +static bool module_initialized; /** The buffer for the default security parameters set by @ref sm_sec_params_set. */ -static ble_gap_sec_params_t m_sec_params; +static ble_gap_sec_params_t default_sec_params_buf; /** The default security parameters set by @ref sm_sec_params_set. */ -static ble_gap_sec_params_t *mp_sec_params; +static ble_gap_sec_params_t *default_sec_params; /** Whether @ref sm_sec_params_set has been called. */ -static bool m_sec_params_set; +static bool default_sec_params_set; #if !defined(CONFIG_PM_LESC) /* Pointer, provided by the user, to the public key to use for LESC procedures. */ -static ble_gap_lesc_p256_pk_t *m_p_public_key; +static ble_gap_lesc_p256_pk_t *lesc_public_key; #endif /* User flag indicating whether a connection has a pending call to @ref sm_link_secure because it * returned @ref NRF_ERROR_BUSY. */ -static int m_flag_link_secure_pending_busy = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_link_secure_pending_busy = BLE_CONN_STATE_USER_FLAG_INVALID; /* User flag indicating whether a pending call to @ref sm_link_secure should be called with true for * the force_repairing parameter. */ -static int m_flag_link_secure_force_repairing = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_link_secure_force_repairing = BLE_CONN_STATE_USER_FLAG_INVALID; /* User flag indicating whether a pending call to @ref sm_link_secure should be called with NULL * security parameters. */ -static int m_flag_link_secure_null_params = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_link_secure_null_params = BLE_CONN_STATE_USER_FLAG_INVALID; /* User flag indicating whether a connection has a pending call to @ref sm_sec_params_reply because * it returned @ref NRF_ERROR_BUSY. */ -static int m_flag_params_reply_pending_busy = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_params_reply_pending_busy = BLE_CONN_STATE_USER_FLAG_INVALID; /** * @brief Function for sending an SM event to all registered event handlers. * - * @param[in] p_event The event to send. + * @param[in] event The event to send. */ -static void evt_send(pm_evt_t *p_event) +static void evt_send(struct pm_evt *event) { for (uint32_t i = 0; i < SM_EVENT_HANDLERS_CNT; i++) { - m_evt_handlers[i](p_event); + evt_handlers[i](event); } } @@ -112,20 +111,23 @@ static void flags_set_from_err_code(uint16_t conn_handle, uint32_t err_code, boo } if (params_reply) { - ble_conn_state_user_flag_set(conn_handle, m_flag_params_reply_pending_busy, + ble_conn_state_user_flag_set(conn_handle, flag_params_reply_pending_busy, flag_value_busy); - ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_pending_busy, false); + ble_conn_state_user_flag_set(conn_handle, flag_link_secure_pending_busy, false); } else { - ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_pending_busy, + ble_conn_state_user_flag_set(conn_handle, flag_link_secure_pending_busy, flag_value_busy); } } -static inline pm_evt_t new_evt(pm_evt_id_t evt_id, uint16_t conn_handle) +static inline struct pm_evt new_evt(enum pm_evt_id evt_id, uint16_t conn_handle) { - pm_evt_t evt = {.evt_id = evt_id, - .conn_handle = conn_handle, - .peer_id = im_peer_id_get_by_conn_handle(conn_handle)}; + struct pm_evt evt = { + .evt_id = evt_id, + .conn_handle = conn_handle, + .peer_id = im_peer_id_get_by_conn_handle(conn_handle), + }; + return evt; } @@ -137,7 +139,7 @@ static inline pm_evt_t new_evt(pm_evt_id_t evt_id, uint16_t conn_handle) */ static void send_unexpected_error(uint16_t conn_handle, uint32_t err_code) { - pm_evt_t error_evt = new_evt(PM_EVT_ERROR_UNEXPECTED, conn_handle); + struct pm_evt error_evt = new_evt(PM_EVT_ERROR_UNEXPECTED, conn_handle); error_evt.params.error_unexpected.error = err_code; evt_send(&error_evt); @@ -150,14 +152,14 @@ static void send_unexpected_error(uint16_t conn_handle, uint32_t err_code) * * @return Whether the key is LESC or not. */ -static bool key_is_lesc(pm_peer_id_t peer_id) +static bool key_is_lesc(uint16_t peer_id) { - pm_peer_data_bonding_t bonding_data = { 0 }; - uint32_t bonding_data_size = sizeof(pm_peer_data_bonding_t); - pm_peer_data_t peer_data; + struct pm_peer_data_bonding bonding_data = { 0 }; + uint32_t bonding_data_size = sizeof(struct pm_peer_data_bonding); + struct pm_peer_data peer_data; uint32_t err_code; - peer_data.p_all_data = &bonding_data; + peer_data.all_data = &bonding_data; err_code = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_BONDING, &peer_data, &bonding_data_size); @@ -169,14 +171,14 @@ static bool key_is_lesc(pm_peer_id_t peer_id) * @brief Function for sending an event based on error codes returned from @ref smd_link_secure or * @ref smd_params_reply. * - * @param[in] conn_handle The connection the event pertains to. - * @param[in] err_code The error code returned from @ref smd_link_secure or - * @ref smd_params_reply. - * @param[in] p_sec_params The security parameters attempted to pass in the call to - * @ref smd_link_secure or @ref smd_params_reply. + * @param[in] conn_handle The connection the event pertains to. + * @param[in] err_code The error code returned from @ref smd_link_secure or + * @ref smd_params_reply. + * @param[in] sec_params The security parameters attempted to pass in the call to + * @ref smd_link_secure or @ref smd_params_reply. */ static void events_send_from_err_code(uint16_t conn_handle, uint32_t err_code, - ble_gap_sec_params_t *p_sec_params) + ble_gap_sec_params_t *sec_params) { if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_INVALID_STATE)) { @@ -188,10 +190,10 @@ static void events_send_from_err_code(uint16_t conn_handle, uint32_t err_code, "NRF_ERROR_TIMEOUT. conn_handle: %d", conn_handle); - pm_evt_t evt = new_evt(PM_EVT_CONN_SEC_FAILED, conn_handle); + struct pm_evt evt = new_evt(PM_EVT_CONN_SEC_FAILED, conn_handle); evt.params.conn_sec_failed.procedure = - ((p_sec_params != NULL) && p_sec_params->bond) + ((sec_params != NULL) && sec_params->bond) ? PM_CONN_SEC_PROCEDURE_BONDING : PM_CONN_SEC_PROCEDURE_PAIRING; evt.params.conn_sec_failed.error_src = BLE_GAP_SEC_STATUS_SOURCE_LOCAL; @@ -209,32 +211,32 @@ static void events_send_from_err_code(uint16_t conn_handle, uint32_t err_code, /** * @brief Function for sending an PM_EVT_CONN_SEC_PARAMS_REQ event. * - * @param[in] conn_handle The connection the event pertains to. - * @param[in] p_peer_params The peer's security parameters to include in the event. Can be NULL. - * @param[in] p_context Pointer to a context that the user must include in the call to @ref - * sm_sec_params_reply(). + * @param[in] conn_handle The connection the event pertains to. + * @param[in] peer_params The peer's security parameters to include in the event. Can be NULL. + * @param[in] context Pointer to a context that the user must include in the call to @ref + * sm_sec_params_reply(). */ -static void params_req_send(uint16_t conn_handle, ble_gap_sec_params_t const *p_peer_params, - sec_params_reply_context_t *p_context) +static void params_req_send(uint16_t conn_handle, const ble_gap_sec_params_t *peer_params, + struct sec_params_reply_context *context) { - pm_evt_t evt = new_evt(PM_EVT_CONN_SEC_PARAMS_REQ, conn_handle); + struct pm_evt evt = new_evt(PM_EVT_CONN_SEC_PARAMS_REQ, conn_handle); - evt.params.conn_sec_params_req.peer_params = p_peer_params; - evt.params.conn_sec_params_req.context = p_context; + evt.params.conn_sec_params_req.peer_params = peer_params; + evt.params.conn_sec_params_req.context = context; evt_send(&evt); } /** - * @brief Function for creating a new @ref sec_params_reply_context_t with the correct initial + * @brief Function for creating a new @ref sec_params_reply_context with the correct initial * values. * * @return The new context. */ -static sec_params_reply_context_t new_context_get(void) +static struct sec_params_reply_context new_context_get(void) { - sec_params_reply_context_t new_context = {.p_sec_params = mp_sec_params, - .params_reply_called = false}; + struct sec_params_reply_context new_context = {.sec_params = default_sec_params, + .params_reply_called = false}; return new_context; } @@ -253,31 +255,31 @@ static uint32_t link_secure(uint16_t conn_handle, bool null_params, bool force_r { uint32_t err_code; uint32_t return_err_code; - ble_gap_sec_params_t *p_sec_params; + ble_gap_sec_params_t *sec_params; if (null_params) { - p_sec_params = NULL; + sec_params = NULL; } else { - sec_params_reply_context_t context = new_context_get(); + struct sec_params_reply_context context = new_context_get(); params_req_send(conn_handle, NULL, &context); - p_sec_params = context.p_sec_params; + sec_params = context.sec_params; - if (!m_sec_params_set && !context.params_reply_called) { + if (!default_sec_params_set && !context.params_reply_called) { /* Security parameters have not been set. */ return NRF_ERROR_NOT_FOUND; } } - err_code = smd_link_secure(conn_handle, p_sec_params, force_repairing); + err_code = smd_link_secure(conn_handle, sec_params, force_repairing); flags_set_from_err_code(conn_handle, err_code, false); switch (err_code) { case NRF_ERROR_BUSY: - ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_null_params, + ble_conn_state_user_flag_set(conn_handle, flag_link_secure_null_params, null_params); - ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_force_repairing, + ble_conn_state_user_flag_set(conn_handle, flag_link_secure_force_repairing, force_repairing); return_err_code = NRF_SUCCESS; break; @@ -297,7 +299,7 @@ static uint32_t link_secure(uint16_t conn_handle, bool null_params, bool force_r } if (send_events) { - events_send_from_err_code(conn_handle, err_code, p_sec_params); + events_send_from_err_code(conn_handle, err_code, sec_params); } return return_err_code; @@ -307,43 +309,45 @@ static uint32_t link_secure(uint16_t conn_handle, bool null_params, bool force_r * @brief Function for requesting security parameters from the user and passing them to the * security_dispatcher. * - * @param[in] conn_handle The connection that needs security parameters. - * @param[in] p_peer_params The peer's security parameters if present. Otherwise NULL. + * @param[in] conn_handle The connection that needs security parameters. + * @param[in] peer_params The peer's security parameters if present. Otherwise NULL. */ static void smd_params_reply_perform(uint16_t conn_handle, - ble_gap_sec_params_t const *p_peer_params) + const ble_gap_sec_params_t *peer_params) { uint32_t err_code; - ble_gap_lesc_p256_pk_t *p_public_key; - sec_params_reply_context_t context = new_context_get(); + ble_gap_lesc_p256_pk_t *public_key; + struct sec_params_reply_context context = new_context_get(); - params_req_send(conn_handle, p_peer_params, &context); + params_req_send(conn_handle, peer_params, &context); #if defined(CONFIG_PM_LESC) - p_public_key = nrf_ble_lesc_public_key_get(); + public_key = nrf_ble_lesc_public_key_get(); #else - p_public_key = m_p_public_key; + public_key = lesc_public_key; #endif /* CONFIG_PM_LESC */ - err_code = smd_params_reply(conn_handle, context.p_sec_params, p_public_key); + err_code = smd_params_reply(conn_handle, context.sec_params, public_key); flags_set_from_err_code(conn_handle, err_code, true); - events_send_from_err_code(conn_handle, err_code, context.p_sec_params); + events_send_from_err_code(conn_handle, err_code, context.sec_params); } /** * @brief Function for handling @ref PM_EVT_CONN_SEC_PARAMS_REQ events. * - * @param[in] p_event The @ref PM_EVT_CONN_SEC_PARAMS_REQ event. + * @param[in] event The @ref PM_EVT_CONN_SEC_PARAMS_REQ event. */ -static __INLINE void params_req_process(pm_evt_t const *p_event) +static __INLINE void params_req_process(const struct pm_evt *event) { - smd_params_reply_perform(p_event->conn_handle, - p_event->params.conn_sec_params_req.peer_params); + smd_params_reply_perform(event->conn_handle, + event->params.conn_sec_params_req.peer_params); } -uint32_t sm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t *p_conn_sec_status) +uint32_t sm_conn_sec_status_get(uint16_t conn_handle, struct pm_conn_sec_status *conn_sec_status) { - VERIFY_PARAM_NOT_NULL(p_conn_sec_status); + if (conn_sec_status == NULL) { + return NRF_ERROR_NULL; + } int status = ble_conn_state_status(conn_handle); @@ -351,26 +355,26 @@ uint32_t sm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t *p_co return BLE_ERROR_INVALID_CONN_HANDLE; } - pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); + uint16_t peer_id = im_peer_id_get_by_conn_handle(conn_handle); - p_conn_sec_status->connected = (status == BLE_CONN_STATUS_CONNECTED); - p_conn_sec_status->bonded = (peer_id != PM_PEER_ID_INVALID); - p_conn_sec_status->encrypted = ble_conn_state_encrypted(conn_handle); - p_conn_sec_status->mitm_protected = ble_conn_state_mitm_protected(conn_handle); - p_conn_sec_status->lesc = ble_conn_state_lesc(conn_handle) || + conn_sec_status->connected = (status == BLE_CONN_STATUS_CONNECTED); + conn_sec_status->bonded = (peer_id != PM_PEER_ID_INVALID); + conn_sec_status->encrypted = ble_conn_state_encrypted(conn_handle); + conn_sec_status->mitm_protected = ble_conn_state_mitm_protected(conn_handle); + conn_sec_status->lesc = ble_conn_state_lesc(conn_handle) || (ble_conn_state_encrypted(conn_handle) && key_is_lesc(peer_id)); return NRF_SUCCESS; } -bool sm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t *p_sec_status_req) +bool sm_sec_is_sufficient(uint16_t conn_handle, struct pm_conn_sec_status *sec_status_req) { /* Set all bits in reserved to 1 so they are ignored in subsequent logic. */ - pm_conn_sec_status_t sec_status = {.reserved = ~0}; + struct pm_conn_sec_status sec_status = {.reserved = ~0}; uint32_t err_code = sm_conn_sec_status_get(conn_handle, &sec_status); - __ASSERT(sizeof(pm_conn_sec_status_t) == sizeof(uint8_t), ""); + __ASSERT_NO_MSG(sizeof(struct pm_conn_sec_status) == sizeof(uint8_t)); - uint8_t unmet_reqs = (~(*((uint8_t *)&sec_status)) & *((uint8_t *)p_sec_status_req)); + uint8_t unmet_reqs = (~(*((uint8_t *)&sec_status)) & *((uint8_t *)sec_status_req)); return (err_code == NRF_SUCCESS) && !unmet_reqs; } @@ -379,26 +383,26 @@ bool sm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t *p_sec_stat /** * @brief Function for handling @ref PM_EVT_SLAVE_SECURITY_REQ events. * - * @param[in] p_event The @ref PM_EVT_SLAVE_SECURITY_REQ event. + * @param[in] event The @ref PM_EVT_SLAVE_SECURITY_REQ event. */ -static void sec_req_process(pm_evt_t const *p_event) +static void sec_req_process(const struct pm_evt *event) { bool null_params = false; bool force_repairing = false; - if (mp_sec_params == NULL) { + if (default_sec_params == NULL) { null_params = true; - } else if (ble_conn_state_encrypted(p_event->conn_handle)) { - pm_conn_sec_status_t sec_status_req = { - .bonded = p_event->params.slave_security_req.bond, - .mitm_protected = p_event->params.slave_security_req.mitm, - .lesc = p_event->params.slave_security_req.lesc, + } else if (ble_conn_state_encrypted(event->conn_handle)) { + struct pm_conn_sec_status sec_status_req = { + .bonded = event->params.slave_security_req.bond, + .mitm_protected = event->params.slave_security_req.mitm, + .lesc = event->params.slave_security_req.lesc, }; - force_repairing = !sm_sec_is_sufficient(p_event->conn_handle, &sec_status_req); + force_repairing = !sm_sec_is_sufficient(event->conn_handle, &sec_status_req); } - (void)link_secure(p_event->conn_handle, null_params, force_repairing, true); + (void)link_secure(event->conn_handle, null_params, force_repairing, true); /* The error code has been properly handled inside link_secure(). */ } #endif @@ -407,53 +411,53 @@ static void sec_req_process(pm_evt_t const *p_event) * @brief Function for translating an SMD event to an SM event and passing it on to SM event * handlers. * - * @param[in] p_event The event to forward. + * @param[in] event The event to forward. */ -static void evt_forward(pm_evt_t *p_event) +static void evt_forward(struct pm_evt *event) { - evt_send(p_event); + evt_send(event); } /** * @brief Event handler for events from the Security Dispatcher module. * This handler is extern in Security Dispatcher. * - * @param[in] p_event The event that has happened. + * @param[in] event The event that has happened. */ -void sm_smd_evt_handler(pm_evt_t *p_event) +void sm_smd_evt_handler(struct pm_evt *event) { - switch (p_event->evt_id) { + switch (event->evt_id) { case PM_EVT_CONN_SEC_PARAMS_REQ: - params_req_process(p_event); + params_req_process(event); break; case PM_EVT_SLAVE_SECURITY_REQ: #if defined(CONFIG_SOFTDEVICE_CENTRAL) - sec_req_process(p_event); + sec_req_process(event); #endif /* fallthrough */ default: /* Forward the event to all registered Security Manager event handlers. */ - evt_forward(p_event); + evt_forward(event); break; } } /** @brief Function handling a pending params_reply. See @ref ble_conn_state_user_function_t. */ -static void params_reply_pending_handle(uint16_t conn_handle, void *p_context) +static void params_reply_pending_handle(uint16_t conn_handle, void *context) { - ARG_UNUSED(p_context); + ARG_UNUSED(context); smd_params_reply_perform(conn_handle, NULL); } /** @brief Function handling a pending link_secure. See @ref ble_conn_state_user_function_t. */ -static void link_secure_pending_handle(uint16_t conn_handle, void *p_context) +static void link_secure_pending_handle(uint16_t conn_handle, void *context) { - ARG_UNUSED(p_context); + ARG_UNUSED(context); bool force_repairing = - ble_conn_state_user_flag_get(conn_handle, m_flag_link_secure_force_repairing); + ble_conn_state_user_flag_get(conn_handle, flag_link_secure_force_repairing); bool null_params = - ble_conn_state_user_flag_get(conn_handle, m_flag_link_secure_null_params); + ble_conn_state_user_flag_get(conn_handle, flag_link_secure_null_params); /* If this fails, it will be automatically retried. */ (void)link_secure(conn_handle, null_params, force_repairing, true); @@ -463,19 +467,19 @@ static void link_secure_pending_handle(uint16_t conn_handle, void *p_context) * @brief Event handler for events from the Peer Database module. * This handler is extern in Peer Database. * - * @param[in] p_event The event that has happened. + * @param[in] event The event that has happened. */ -void sm_pdb_evt_handler(pm_evt_t *p_event) +void sm_pdb_evt_handler(struct pm_evt *event) { - switch (p_event->evt_id) { + switch (event->evt_id) { case PM_EVT_FLASH_GARBAGE_COLLECTED: case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: case PM_EVT_PEER_DATA_UPDATE_FAILED: case PM_EVT_PEER_DELETE_SUCCEEDED: case PM_EVT_PEER_DELETE_FAILED: - (void)ble_conn_state_for_each_set_user_flag(m_flag_params_reply_pending_busy, + (void)ble_conn_state_for_each_set_user_flag(flag_params_reply_pending_busy, params_reply_pending_handle, NULL); - (void)ble_conn_state_for_each_set_user_flag(m_flag_link_secure_pending_busy, + (void)ble_conn_state_for_each_set_user_flag(flag_link_secure_pending_busy, link_secure_pending_handle, NULL); break; default: @@ -489,16 +493,16 @@ void sm_pdb_evt_handler(pm_evt_t *p_event) * * @param[out] flag_id The flag to initialize. */ -static void flag_id_init(int *p_flag_id) +static void flag_id_init(int *flag_id) { - if (*p_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { - *p_flag_id = ble_conn_state_user_flag_acquire(); + if (*flag_id == BLE_CONN_STATE_USER_FLAG_INVALID) { + *flag_id = ble_conn_state_user_flag_acquire(); } } uint32_t sm_init(void) { - NRF_PM_DEBUG_CHECK(!m_module_initialized); + __ASSERT_NO_MSG(!module_initialized); #if defined(CONFIG_PM_LESC) uint32_t err_code = nrf_ble_lesc_init(); @@ -508,164 +512,167 @@ uint32_t sm_init(void) } #endif - flag_id_init(&m_flag_link_secure_pending_busy); - flag_id_init(&m_flag_link_secure_force_repairing); - flag_id_init(&m_flag_link_secure_null_params); - flag_id_init(&m_flag_params_reply_pending_busy); + flag_id_init(&flag_link_secure_pending_busy); + flag_id_init(&flag_link_secure_force_repairing); + flag_id_init(&flag_link_secure_null_params); + flag_id_init(&flag_params_reply_pending_busy); - if (m_flag_params_reply_pending_busy == BLE_CONN_STATE_USER_FLAG_INVALID) { + if (flag_params_reply_pending_busy == BLE_CONN_STATE_USER_FLAG_INVALID) { LOG_ERR("Could not acquire conn_state user flags. Increase " "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module."); return NRF_ERROR_INTERNAL; } - m_module_initialized = true; + module_initialized = true; return NRF_SUCCESS; } -void sm_ble_evt_handler(ble_evt_t const *p_ble_evt) +void sm_ble_evt_handler(const ble_evt_t *ble_evt) { - NRF_PM_DEBUG_CHECK(p_ble_evt != NULL); + __ASSERT_NO_MSG(ble_evt != NULL); - smd_ble_evt_handler(p_ble_evt); + smd_ble_evt_handler(ble_evt); #if defined(CONFIG_PM_LESC) - nrf_ble_lesc_on_ble_evt(p_ble_evt); + nrf_ble_lesc_on_ble_evt(ble_evt); #endif - (void)ble_conn_state_for_each_set_user_flag(m_flag_params_reply_pending_busy, + (void)ble_conn_state_for_each_set_user_flag(flag_params_reply_pending_busy, params_reply_pending_handle, NULL); - (void)ble_conn_state_for_each_set_user_flag(m_flag_link_secure_pending_busy, + (void)ble_conn_state_for_each_set_user_flag(flag_link_secure_pending_busy, link_secure_pending_handle, NULL); } /** * @brief Function for checking whether security parameters are valid. * - * @param[out] p_sec_params The security parameters to verify. + * @param[out] sec_params The security parameters to verify. * * @return Whether the security parameters are valid. */ -static bool sec_params_verify(ble_gap_sec_params_t *p_sec_params) +static bool sec_params_verify(ble_gap_sec_params_t *sec_params) { /* NULL check. */ - if (p_sec_params == NULL) { + if (sec_params == NULL) { return false; } /* OOB not allowed unless MITM. */ - if (!p_sec_params->mitm && p_sec_params->oob) { + if (!sec_params->mitm && sec_params->oob) { return false; } /* IO Capabilities must be one of the valid values from @ref BLE_GAP_IO_CAPS. */ - if (p_sec_params->io_caps > BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY) { + if (sec_params->io_caps > BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY) { return false; } /* Must have either IO capabilities or OOB if MITM. */ - if (p_sec_params->mitm && (p_sec_params->io_caps == BLE_GAP_IO_CAPS_NONE) && - !p_sec_params->oob) { + if (sec_params->mitm && (sec_params->io_caps == BLE_GAP_IO_CAPS_NONE) && + !sec_params->oob) { return false; } /* Minimum key size cannot be larger than maximum key size. */ - if (p_sec_params->min_key_size > p_sec_params->max_key_size) { + if (sec_params->min_key_size > sec_params->max_key_size) { return false; } /* Key size cannot be below 7 bytes. */ - if (p_sec_params->min_key_size < 7) { + if (sec_params->min_key_size < 7) { return false; } /* Key size cannot be above 16 bytes. */ - if (p_sec_params->max_key_size > 16) { + if (sec_params->max_key_size > 16) { return false; } /* Signing is not supported. */ - if (p_sec_params->kdist_own.sign || p_sec_params->kdist_peer.sign) { + if (sec_params->kdist_own.sign || sec_params->kdist_peer.sign) { return false; } /* link bit must be 0. */ - if (p_sec_params->kdist_own.link || p_sec_params->kdist_peer.link) { + if (sec_params->kdist_own.link || sec_params->kdist_peer.link) { return false; } /* If bonding is not enabled, no keys can be distributed. */ - if (!p_sec_params->bond && (p_sec_params->kdist_own.enc || p_sec_params->kdist_own.id || - p_sec_params->kdist_peer.enc || p_sec_params->kdist_peer.id)) { + if (!sec_params->bond && (sec_params->kdist_own.enc || sec_params->kdist_own.id || + sec_params->kdist_peer.enc || sec_params->kdist_peer.id)) { return false; } /* If bonding is enabled, one or more keys must be distributed. */ - if (p_sec_params->bond && !p_sec_params->kdist_own.enc && !p_sec_params->kdist_own.id && - !p_sec_params->kdist_peer.enc && !p_sec_params->kdist_peer.id) { + if (sec_params->bond && !sec_params->kdist_own.enc && !sec_params->kdist_own.id && + !sec_params->kdist_peer.enc && !sec_params->kdist_peer.id) { return false; } return true; } -uint32_t sm_sec_params_set(ble_gap_sec_params_t *p_sec_params) +uint32_t sm_sec_params_set(ble_gap_sec_params_t *sec_params) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); - if (p_sec_params == NULL) { - mp_sec_params = NULL; - m_sec_params_set = true; + if (sec_params == NULL) { + default_sec_params = NULL; + default_sec_params_set = true; return NRF_SUCCESS; - } else if (sec_params_verify(p_sec_params)) { - m_sec_params = *p_sec_params; - mp_sec_params = &m_sec_params; - m_sec_params_set = true; + } else if (sec_params_verify(sec_params)) { + default_sec_params_buf = *sec_params; + default_sec_params = &default_sec_params_buf; + default_sec_params_set = true; return NRF_SUCCESS; } else { return NRF_ERROR_INVALID_PARAM; } } -void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_conn_sec_config) +void sm_conn_sec_config_reply(uint16_t conn_handle, struct pm_conn_sec_config *conn_sec_config) { - NRF_PM_DEBUG_CHECK(m_module_initialized); - NRF_PM_DEBUG_CHECK(p_conn_sec_config != NULL); + __ASSERT_NO_MSG(module_initialized); + __ASSERT_NO_MSG(conn_sec_config != NULL); - smd_conn_sec_config_reply(conn_handle, p_conn_sec_config); + smd_conn_sec_config_reply(conn_handle, conn_sec_config); } -uint32_t sm_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, - void const *p_context) +uint32_t sm_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, + const void *context) { - NRF_PM_DEBUG_CHECK(m_module_initialized); - VERIFY_PARAM_NOT_NULL(p_context); + __ASSERT_NO_MSG(module_initialized); + + if (context == NULL) { + return NRF_ERROR_NULL; + } - sec_params_reply_context_t *p_sec_params_reply_context = - (sec_params_reply_context_t *)p_context; - if (p_sec_params == NULL) { + struct sec_params_reply_context *sec_params_reply_context = + (struct sec_params_reply_context *)context; + if (sec_params == NULL) { /* Set the store pointer to NULL, so that NULL is passed to the SoftDevice. */ - p_sec_params_reply_context->p_sec_params = NULL; - } else if (sec_params_verify(p_sec_params)) { + sec_params_reply_context->sec_params = NULL; + } else if (sec_params_verify(sec_params)) { /* Copy the provided sec_params into the store. */ - p_sec_params_reply_context->sec_params_mem = *p_sec_params; - p_sec_params_reply_context->p_sec_params = - &p_sec_params_reply_context->sec_params_mem; + sec_params_reply_context->sec_params_mem = *sec_params; + sec_params_reply_context->sec_params = + &sec_params_reply_context->sec_params_mem; } else { return NRF_ERROR_INVALID_PARAM; } - p_sec_params_reply_context->params_reply_called = true; + sec_params_reply_context->params_reply_called = true; return NRF_SUCCESS; } -uint32_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *p_public_key) +uint32_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *public_key) { - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); #if defined(CONFIG_PM_LESC) return NRF_ERROR_FORBIDDEN; #else - m_p_public_key = p_public_key; + lesc_public_key = public_key; return NRF_SUCCESS; #endif /* CONFIG_PM_LESC */ @@ -675,7 +682,7 @@ uint32_t sm_link_secure(uint16_t conn_handle, bool force_repairing) { uint32_t ret; - NRF_PM_DEBUG_CHECK(m_module_initialized); + __ASSERT_NO_MSG(module_initialized); ret = link_secure(conn_handle, false, force_repairing, false); return ret; diff --git a/lib/bluetooth/peer_manager/nrf_strerror.c b/lib/bluetooth/peer_manager/nrf_strerror.c index 19cc67b131..d20972b539 100644 --- a/lib/bluetooth/peer_manager/nrf_strerror.c +++ b/lib/bluetooth/peer_manager/nrf_strerror.c @@ -23,17 +23,17 @@ /** * @brief Array entity element that describes an error. */ -typedef struct { +struct strerror_desc { uint32_t code; /**< Error code. */ - char const *name; /**< Descriptive name (the same as the internal error mnemonic). */ -} nrf_strerror_desc_t; + const char *name; /**< Descriptive name (the same as the internal error mnemonic). */ +}; /** * @brief Unknown error code. * * The constant string used by @ref nrf_strerror_get when the error description was not found. */ -static char const m_unknown_str[] = "Unknown error code"; +static const char unknown_str[] = "Unknown error code"; /** * @brief Array with error codes. @@ -43,7 +43,7 @@ static char const m_unknown_str[] = "Unknown error code"; * @note It is required for this array to have error codes placed in ascending order. * This condition is checked in automatic unit test before the release. */ -static nrf_strerror_desc_t const nrf_strerror_array[] = { +static const struct strerror_desc nrf_strerror_array[] = { NRF_STRERROR_ENTITY(NRF_SUCCESS), NRF_STRERROR_ENTITY(NRF_ERROR_SVC_HANDLER_MISSING), NRF_STRERROR_ENTITY(NRF_ERROR_SOFTDEVICE_NOT_ENABLED), NRF_STRERROR_ENTITY(NRF_ERROR_INTERNAL), NRF_STRERROR_ENTITY(NRF_ERROR_NO_MEM), @@ -62,31 +62,31 @@ static nrf_strerror_desc_t const nrf_strerror_array[] = { #endif }; -char const *nrf_strerror_get(uint32_t code) +const char *nrf_strerror_get(uint32_t code) { - char const *p_ret = nrf_strerror_find(code); + const char *ret = nrf_strerror_find(code); - return (p_ret == NULL) ? m_unknown_str : p_ret; + return (ret == NULL) ? unknown_str : ret; } -char const *nrf_strerror_find(uint32_t code) +const char *nrf_strerror_find(uint32_t code) { - nrf_strerror_desc_t const *p_start; - nrf_strerror_desc_t const *p_end; + const struct strerror_desc *start; + const struct strerror_desc *end; - p_start = nrf_strerror_array; - p_end = nrf_strerror_array + ARRAY_SIZE(nrf_strerror_array); + start = nrf_strerror_array; + end = nrf_strerror_array + ARRAY_SIZE(nrf_strerror_array); - while (p_start < p_end) { - nrf_strerror_desc_t const *p_mid = p_start + ((p_end - p_start) / 2); - uint32_t mid_c = p_mid->code; + while (start < end) { + const struct strerror_desc *mid = start + ((end - start) / 2); + uint32_t mid_c = mid->code; if (mid_c > code) { - p_end = p_mid; + end = mid; } else if (mid_c < code) { - p_start = p_mid + 1; + start = mid + 1; } else { - return p_mid->name; + return mid->name; } } return NULL; diff --git a/lib/bluetooth/peer_manager/peer_manager.c b/lib/bluetooth/peer_manager/peer_manager.c index 2806b0fbef..e734b3ca4e 100644 --- a/lib/bluetooth/peer_manager/peer_manager.c +++ b/lib/bluetooth/peer_manager/peer_manager.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -28,41 +27,37 @@ LOG_MODULE_REGISTER(peer_manager, CONFIG_PEER_MANAGER_LOG_LEVEL); -/** Macro indicating whether the module has been initialized properly. */ -#define MODULE_INITIALIZED (m_module_initialized) -#define VERIFY_MODULE_INITIALIZED() VERIFY_TRUE((MODULE_INITIALIZED), NRF_ERROR_INVALID_STATE) - /** Whether or not @ref pm_init has been called successfully. */ -static bool m_module_initialized; +static bool module_initialized; /** Whether or not @ref rank_init has been called successfully. */ -static bool m_peer_rank_initialized; +static bool peer_rank_initialized; /** True from when @ref pm_peers_delete is called until all peers have been deleted. */ -static bool m_deleting_all; +static bool deleting_all; /** 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 pm_store_token_t m_peer_rank_token; +static uint32_t peer_rank_token; /** The current highest peer rank. Used by @ref pm_peer_rank_highest. */ -static uint32_t m_current_highest_peer_rank; +static uint32_t current_highest_peer_rank; /** The peer with the highest peer rank. Used by @ref pm_peer_rank_highest. */ -static pm_peer_id_t m_highest_ranked_peer; +static uint16_t highest_ranked_peer; /** The subscribers to Peer Manager events, as registered through @ref pm_register. */ -static pm_evt_handler_t m_evt_handlers[CONFIG_PM_MAX_REGISTRANTS]; +static pm_evt_handler_t evt_handlers[CONFIG_PM_MAX_REGISTRANTS]; /** The number of event handlers registered through @ref pm_register. */ -static uint8_t m_n_registrants; +static uint8_t n_registrants; /** User flag indicating whether a connection is excluded from being handled by the Peer Manager. */ -static int m_flag_conn_excluded = BLE_CONN_STATE_USER_FLAG_INVALID; +static int flag_conn_excluded = BLE_CONN_STATE_USER_FLAG_INVALID; /** * @brief Function for sending a Peer Manager event to all subscribers. * - * @param[in] p_pm_evt The event to send. + * @param[in] pm_evt The event to send. */ -static void evt_send(pm_evt_t const *p_pm_evt) +static void evt_send(const struct pm_evt *pm_evt) { - for (int i = 0; i < m_n_registrants; i++) { - m_evt_handlers[i](p_pm_evt); + for (int i = 0; i < n_registrants; i++) { + evt_handlers[i](pm_evt); } } @@ -71,14 +66,14 @@ static void evt_send(pm_evt_t const *p_pm_evt) static void rank_vars_update(void) { uint32_t err_code = - pm_peer_ranks_get(&m_highest_ranked_peer, &m_current_highest_peer_rank, NULL, NULL); + pm_peer_ranks_get(&highest_ranked_peer, ¤t_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; + highest_ranked_peer = PM_PEER_ID_INVALID; + current_highest_peer_rank = 0; } - m_peer_rank_initialized = ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND)); + peer_rank_initialized = ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND)); } #endif @@ -86,40 +81,38 @@ static void rank_vars_update(void) * @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. + * @param[in] pdb_evt The incoming Peer Database event. */ -void pm_pdb_evt_handler(pm_evt_t *p_pdb_evt) +void pm_pdb_evt_handler(struct pm_evt *pdb_evt) { bool send_evt = true; - p_pdb_evt->conn_handle = im_conn_handle_get(p_pdb_evt->peer_id); + pdb_evt->conn_handle = im_conn_handle_get(pdb_evt->peer_id); - switch (p_pdb_evt->evt_id) { + switch (pdb_evt->evt_id) { #if defined(CONFIG_PM_PEER_RANKS) 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 = + if (pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) { + if ((peer_rank_token != PM_STORE_TOKEN_INVALID) && + (peer_rank_token == pdb_evt->params.peer_data_update_succeeded.token)) { + peer_rank_token = PM_STORE_TOKEN_INVALID; + highest_ranked_peer = pdb_evt->peer_id; + + 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 == + } else if (peer_rank_initialized && + (pdb_evt->peer_id == highest_ranked_peer) && + (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 == + } else if (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 == + if (peer_rank_initialized && (pdb_evt->peer_id == highest_ranked_peer) && + (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. @@ -130,14 +123,13 @@ void pm_pdb_evt_handler(pm_evt_t *p_pdb_evt) 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 = + if (pdb_evt->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE) { + if ((peer_rank_token != PM_STORE_TOKEN_INVALID) && + (peer_rank_token == pdb_evt->params.peer_data_update_failed.token)) { + peer_rank_token = PM_STORE_TOKEN_INVALID; + current_highest_peer_rank -= 1; + + pdb_evt->params.peer_data_update_succeeded.token = PM_STORE_TOKEN_INVALID; } } @@ -146,17 +138,17 @@ void pm_pdb_evt_handler(pm_evt_t *p_pdb_evt) case PM_EVT_PEER_DELETE_SUCCEEDED: /* Check that no peers marked for deletion are left. */ - if (m_deleting_all && + if (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; + deleting_all = false; - pm_evt_t pm_delete_all_evt; + struct pm_evt pm_delete_all_evt; - memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t)); + memset(&pm_delete_all_evt, 0, sizeof(struct pm_evt)); 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; @@ -166,12 +158,12 @@ void pm_pdb_evt_handler(pm_evt_t *p_pdb_evt) /* Forward the event to all registered Peer Manager event handlers. * Ensure that PEER_DELETE_SUCCEEDED arrives before PEERS_DELETE_SUCCEEDED. */ - evt_send(p_pdb_evt); + evt_send(pdb_evt); evt_send(&pm_delete_all_evt); } #if defined(CONFIG_PM_PEER_RANKS) - if (m_peer_rank_initialized && (p_pdb_evt->peer_id == m_highest_ranked_peer)) { + if (peer_rank_initialized && (pdb_evt->peer_id == highest_ranked_peer)) { /* Update peer rank variable if highest ranked peer has been deleted. */ rank_vars_update(); } @@ -179,26 +171,26 @@ void pm_pdb_evt_handler(pm_evt_t *p_pdb_evt) break; case PM_EVT_PEER_DELETE_FAILED: - if (m_deleting_all) { + if (deleting_all) { /* pm_peers_delete() was called and has thus failed. */ - m_deleting_all = false; + deleting_all = false; - pm_evt_t pm_delete_all_evt; + struct pm_evt pm_delete_all_evt; - memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t)); + memset(&pm_delete_all_evt, 0, sizeof(struct pm_evt)); 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; + pdb_evt->params.peer_delete_failed.error; send_evt = false; /* Forward the event to all registered Peer Manager event handlers. * Ensure that PEER_DELETE_FAILED arrives before PEERS_DELETE_FAILED. */ - evt_send(p_pdb_evt); + evt_send(pdb_evt); evt_send(&pm_delete_all_evt); } break; @@ -210,7 +202,7 @@ void pm_pdb_evt_handler(pm_evt_t *p_pdb_evt) if (send_evt) { /* Forward the event to all registered Peer Manager event handlers. */ - evt_send(p_pdb_evt); + evt_send(pdb_evt); } } @@ -218,109 +210,113 @@ void pm_pdb_evt_handler(pm_evt_t *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. + * @param[in] sm_evt The incoming Security Manager event. */ -void pm_sm_evt_handler(pm_evt_t *p_sm_evt) +void pm_sm_evt_handler(struct pm_evt *sm_evt) { - VERIFY_PARAM_NOT_NULL_VOID(p_sm_evt); + if (sm_evt == NULL) { + return; + } /* Forward the event to all registered Peer Manager event handlers. */ - evt_send(p_sm_evt); + evt_send(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. + * @param[in] gcm_evt The incoming GATT Cache Manager event. */ -void pm_gcm_evt_handler(pm_evt_t *p_gcm_evt) +void pm_gcm_evt_handler(struct pm_evt *gcm_evt) { /* Forward the event to all registered Peer Manager event handlers. */ - evt_send(p_gcm_evt); + evt_send(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. + * @param[in] gscm_evt The incoming GATTS Cache Manager event. */ -void pm_gscm_evt_handler(pm_evt_t *p_gscm_evt) +void pm_gscm_evt_handler(struct pm_evt *gscm_evt) { /* Forward the event to all registered Peer Manager event handlers. */ - evt_send(p_gscm_evt); + evt_send(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. + * @param[in] im_evt The incoming ID Manager event. */ -void pm_im_evt_handler(pm_evt_t *p_im_evt) +void pm_im_evt_handler(struct pm_evt *im_evt) { /* Forward the event to all registered Peer Manager event handlers. */ - evt_send(p_im_evt); + evt_send(im_evt); } -static bool is_conn_handle_excluded(ble_evt_t const *p_ble_evt) +static bool is_conn_handle_excluded(const ble_evt_t *ble_evt) { - uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle; + uint16_t conn_handle = ble_evt->evt.gap_evt.conn_handle; - switch (p_ble_evt->header.evt_id) { + switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: { - pm_evt_t pm_conn_config_req_evt; + struct pm_evt pm_conn_config_req_evt; bool is_excluded = false; - memset(&pm_conn_config_req_evt, 0, sizeof(pm_evt_t)); + memset(&pm_conn_config_req_evt, 0, sizeof(struct pm_evt)); pm_conn_config_req_evt.evt_id = PM_EVT_CONN_CONFIG_REQ; pm_conn_config_req_evt.peer_id = PM_PEER_ID_INVALID; pm_conn_config_req_evt.conn_handle = conn_handle; pm_conn_config_req_evt.params.conn_config_req.peer_params = - &p_ble_evt->evt.gap_evt.params.connected; + &ble_evt->evt.gap_evt.params.connected; pm_conn_config_req_evt.params.conn_config_req.context = &is_excluded; evt_send(&pm_conn_config_req_evt); - ble_conn_state_user_flag_set(conn_handle, m_flag_conn_excluded, is_excluded); + ble_conn_state_user_flag_set(conn_handle, flag_conn_excluded, is_excluded); return is_excluded; } default: - return ble_conn_state_user_flag_get(conn_handle, m_flag_conn_excluded); + return ble_conn_state_user_flag_get(conn_handle, flag_conn_excluded); } } /** * @brief Function for handling BLE events. * - * @param[in] p_ble_evt Event received from the BLE stack. - * @param[in] p_context Context. + * @param[in] ble_evt Event received from the BLE stack. + * @param[in] context Context. */ -static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) +static void ble_evt_handler(const ble_evt_t *ble_evt, void *context) { - VERIFY_MODULE_INITIALIZED_VOID(); + if (!module_initialized) { + return; + } - if (is_conn_handle_excluded(p_ble_evt)) { + if (is_conn_handle_excluded(ble_evt)) { LOG_DBG("Filtering BLE event with ID: 0x%04X targeting 0x%04X connection handle", - p_ble_evt->header.evt_id, p_ble_evt->evt.gap_evt.conn_handle); + ble_evt->header.evt_id, ble_evt->evt.gap_evt.conn_handle); return; } - im_ble_evt_handler(p_ble_evt); - sm_ble_evt_handler(p_ble_evt); - gcm_ble_evt_handler(p_ble_evt); + im_ble_evt_handler(ble_evt); + sm_ble_evt_handler(ble_evt); + gcm_ble_evt_handler(ble_evt); } -NRF_SDH_BLE_OBSERVER(m_ble_evt_observer, ble_evt_handler, NULL, CONFIG_PM_BLE_OBSERVER_PRIO); +NRF_SDH_BLE_OBSERVER(ble_evt_observer, ble_evt_handler, NULL, CONFIG_PM_BLE_OBSERVER_PRIO); /** @brief Function for resetting the internal state of this module. */ static void internal_state_reset(void) { - m_highest_ranked_peer = PM_PEER_ID_INVALID; - m_peer_rank_token = PM_STORE_TOKEN_INVALID; + highest_ranked_peer = PM_PEER_ID_INVALID; + peer_rank_token = PM_STORE_TOKEN_INVALID; } uint32_t pm_init(void) @@ -371,41 +367,45 @@ uint32_t pm_init(void) internal_state_reset(); - m_peer_rank_initialized = false; - m_module_initialized = true; + peer_rank_initialized = false; + module_initialized = true; - m_flag_conn_excluded = ble_conn_state_user_flag_acquire(); + flag_conn_excluded = ble_conn_state_user_flag_acquire(); /* If CONFIG_PM_PEER_RANKS 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); + UNUSED_VARIABLE(peer_rank_initialized); + UNUSED_VARIABLE(peer_rank_token); + UNUSED_VARIABLE(current_highest_peer_rank); + UNUSED_VARIABLE(highest_ranked_peer); return NRF_SUCCESS; } uint32_t pm_register(pm_evt_handler_t event_handler) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } - if (m_n_registrants >= CONFIG_PM_MAX_REGISTRANTS) { + if (n_registrants >= CONFIG_PM_MAX_REGISTRANTS) { return NRF_ERROR_NO_MEM; } - m_evt_handlers[m_n_registrants] = event_handler; - m_n_registrants += 1; + evt_handlers[n_registrants] = event_handler; + n_registrants += 1; return NRF_SUCCESS; } -uint32_t pm_sec_params_set(ble_gap_sec_params_t *p_sec_params) +uint32_t pm_sec_params_set(ble_gap_sec_params_t *sec_params) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } uint32_t err_code; - err_code = sm_sec_params_set(p_sec_params); + err_code = sm_sec_params_set(sec_params); /* NRF_ERROR_INVALID_PARAM if parameters are invalid, * NRF_SUCCESS otherwise. @@ -415,7 +415,9 @@ uint32_t pm_sec_params_set(ble_gap_sec_params_t *p_sec_params) uint32_t pm_conn_secure(uint16_t conn_handle, bool force_repairing) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } uint32_t err_code; @@ -428,156 +430,213 @@ uint32_t pm_conn_secure(uint16_t conn_handle, bool force_repairing) return err_code; } -uint32_t pm_conn_exclude(uint16_t conn_handle, void const *p_context) +uint32_t pm_conn_exclude(uint16_t conn_handle, const void *context) { - VERIFY_PARAM_NOT_NULL(p_context); + if (context == NULL) { + return NRF_ERROR_NULL; + } - bool *p_is_conn_excluded = (bool *)p_context; + bool *is_conn_excluded = (bool *)context; - *p_is_conn_excluded = true; + *is_conn_excluded = true; return NRF_SUCCESS; } -void pm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t *p_conn_sec_config) +void pm_conn_sec_config_reply(uint16_t conn_handle, struct pm_conn_sec_config *conn_sec_config) { - if (p_conn_sec_config != NULL) { - sm_conn_sec_config_reply(conn_handle, p_conn_sec_config); + if (conn_sec_config != NULL) { + sm_conn_sec_config_reply(conn_handle, conn_sec_config); } } -uint32_t pm_conn_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *p_sec_params, - void const *p_context) +uint32_t pm_conn_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t *sec_params, + const void *context) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } - return sm_sec_params_reply(conn_handle, p_sec_params, p_context); + return sm_sec_params_reply(conn_handle, sec_params, context); } void pm_local_database_has_changed(void) { #if defined(CONFIG_PM_SERVICE_CHANGED) - VERIFY_MODULE_INITIALIZED_VOID(); + if (!module_initialized) { + return; + } gcm_local_database_has_changed(); #endif } -uint32_t pm_id_addr_set(ble_gap_addr_t const *p_addr) +uint32_t pm_id_addr_set(const ble_gap_addr_t *addr) { - VERIFY_MODULE_INITIALIZED(); - return im_id_addr_set(p_addr); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + return im_id_addr_set(addr); } -uint32_t pm_id_addr_get(ble_gap_addr_t *p_addr) +uint32_t pm_id_addr_get(ble_gap_addr_t *addr) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_addr); - return im_id_addr_get(p_addr); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (addr == NULL) { + return NRF_ERROR_NULL; + } + + return im_id_addr_get(addr); } -uint32_t pm_privacy_set(pm_privacy_params_t const *p_privacy_params) +uint32_t pm_privacy_set(const ble_gap_privacy_params_t *privacy_params) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_privacy_params); - return im_privacy_set(p_privacy_params); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (privacy_params == NULL) { + return NRF_ERROR_NULL; + } + + return im_privacy_set(privacy_params); } -uint32_t pm_privacy_get(pm_privacy_params_t *p_privacy_params) +uint32_t pm_privacy_get(ble_gap_privacy_params_t *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); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (privacy_params == NULL || privacy_params->p_device_irk == NULL) { + return NRF_ERROR_NULL; + } + + return im_privacy_get(privacy_params); } -bool pm_address_resolve(ble_gap_addr_t const *p_addr, ble_gap_irk_t const *p_irk) +bool pm_address_resolve(const ble_gap_addr_t *addr, const ble_gap_irk_t *irk) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } - if ((p_addr == NULL) || (p_irk == NULL)) { + if ((addr == NULL) || (irk == NULL)) { return false; } else { - return im_address_resolve(p_addr, p_irk); + return im_address_resolve(addr, irk); } } -uint32_t pm_whitelist_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt) +uint32_t pm_whitelist_set(const uint16_t *peers, uint32_t peer_cnt) { - VERIFY_MODULE_INITIALIZED(); - return im_whitelist_set(p_peers, peer_cnt); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + return im_whitelist_set(peers, peer_cnt); } -uint32_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) +uint32_t pm_whitelist_get(ble_gap_addr_t *addrs, uint32_t *addr_cnt, ble_gap_irk_t *irks, + uint32_t *irk_cnt) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } - if (((p_addrs == NULL) && (p_irks == NULL)) || - ((p_addrs != NULL) && (p_addr_cnt == NULL)) || - ((p_irks != NULL) && (p_irk_cnt == NULL))) { + if (((addrs == NULL) && (irks == NULL)) || + ((addrs != NULL) && (addr_cnt == NULL)) || + ((irks != NULL) && (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); + return im_whitelist_get(addrs, addr_cnt, irks, irk_cnt); } -uint32_t pm_device_identities_list_set(pm_peer_id_t const *p_peers, uint32_t peer_cnt) +uint32_t pm_device_identities_list_set(const uint16_t *peers, uint32_t peer_cnt) { - VERIFY_MODULE_INITIALIZED(); - return im_device_identities_list_set(p_peers, peer_cnt); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + return im_device_identities_list_set(peers, peer_cnt); } -uint32_t pm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t *p_conn_sec_status) +uint32_t pm_conn_sec_status_get(uint16_t conn_handle, struct pm_conn_sec_status *conn_sec_status) { - VERIFY_MODULE_INITIALIZED(); - return sm_conn_sec_status_get(conn_handle, p_conn_sec_status); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + return sm_conn_sec_status_get(conn_handle, conn_sec_status); } -bool pm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t *p_sec_status_req) +bool pm_sec_is_sufficient(uint16_t conn_handle, struct pm_conn_sec_status *sec_status_req) { - VERIFY_MODULE_INITIALIZED_BOOL(); - return sm_sec_is_sufficient(conn_handle, p_sec_status_req); + if (!module_initialized) { + return false; + } + + return sm_sec_is_sufficient(conn_handle, sec_status_req); } -uint32_t pm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *p_public_key) +uint32_t pm_lesc_public_key_set(ble_gap_lesc_p256_pk_t *public_key) { - VERIFY_MODULE_INITIALIZED(); - return sm_lesc_public_key_set(p_public_key); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + return sm_lesc_public_key_set(public_key); } -uint32_t pm_conn_handle_get(pm_peer_id_t peer_id, uint16_t *p_conn_handle) +uint32_t pm_conn_handle_get(uint16_t peer_id, uint16_t *conn_handle) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_conn_handle); - *p_conn_handle = im_conn_handle_get(peer_id); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (conn_handle == NULL) { + return NRF_ERROR_NULL; + } + + *conn_handle = im_conn_handle_get(peer_id); return NRF_SUCCESS; } -uint32_t pm_peer_id_get(uint16_t conn_handle, pm_peer_id_t *p_peer_id) +uint32_t pm_peer_id_get(uint16_t conn_handle, uint16_t *peer_id) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_peer_id); - *p_peer_id = im_peer_id_get_by_conn_handle(conn_handle); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (peer_id == NULL) { + return NRF_ERROR_NULL; + } + + *peer_id = im_peer_id_get_by_conn_handle(conn_handle); return NRF_SUCCESS; } uint32_t pm_peer_count(void) { - if (!MODULE_INITIALIZED) { + 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) +uint16_t pm_next_peer_id_get(uint16_t prev_peer_id) { - pm_peer_id_t next_peer_id = prev_peer_id; + uint16_t next_peer_id = prev_peer_id; - if (!MODULE_INITIALIZED) { + if (!module_initialized) { return PM_PEER_ID_INVALID; } @@ -591,12 +650,12 @@ pm_peer_id_t pm_next_peer_id_get(pm_peer_id_t prev_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. + * @param[in] irk Pointer to the Identity Resolving Key. */ -static bool peer_is_irk(ble_gap_irk_t const *const p_irk) +static bool peer_is_irk(const ble_gap_irk_t *const irk) { - for (uint32_t i = 0; i < ARRAY_SIZE(p_irk->irk); i++) { - if (p_irk->irk[i] != 0) { + for (uint32_t i = 0; i < ARRAY_SIZE(irk->irk); i++) { + if (irk->irk[i] != 0) { return true; } } @@ -604,30 +663,36 @@ static bool peer_is_irk(ble_gap_irk_t const *const p_irk) return false; } -uint32_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) +uint32_t pm_peer_id_list(uint16_t *peer_list, uint32_t *const list_size, + uint16_t first_peer_id, enum pm_peer_id_list_skip skip_id) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_list_size); - VERIFY_PARAM_NOT_NULL(p_peer_list); - uint32_t err_code; - uint32_t size = *p_list_size; + uint32_t 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; + struct pm_peer_data pm_car_data; + struct pm_peer_data pm_bond_data; + uint16_t current_peer_id = first_peer_id; + const ble_gap_addr_t *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; - if ((*p_list_size < 1) || + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (peer_list == NULL || list_size == NULL) { + return NRF_ERROR_NULL; + } + + size = *list_size; + + if ((*list_size < 1) || (skip_id > (PM_PEER_ID_LIST_SKIP_NO_ID_ADDR | PM_PEER_ID_LIST_SKIP_ALL))) { return NRF_ERROR_INVALID_PARAM; } - *p_list_size = 0; + *list_size = 0; if (current_peer_id == PM_PEER_ID_INVALID) { current_peer_id = pm_next_peer_id_get(current_peer_id); @@ -637,34 +702,34 @@ uint32_t pm_peer_id_list(pm_peer_id_t *p_peer_list, uint32_t *const p_list_size, } } - memset(&pm_car_data, 0, sizeof(pm_peer_data_t)); - memset(&pm_bond_data, 0, sizeof(pm_peer_data_t)); + memset(&pm_car_data, 0, sizeof(struct pm_peer_data)); + memset(&pm_bond_data, 0, sizeof(struct pm_peer_data)); while (current_peer_id != PM_PEER_ID_INVALID) { bool skip = false; if (skip_no_addr || skip_no_irk) { /* Get data */ - pm_peer_data_bonding_t bonding_data = { 0 }; - uint32_t bonding_data_size = sizeof(pm_peer_data_bonding_t); + struct pm_peer_data_bonding bonding_data = { 0 }; + uint32_t bonding_data_size = sizeof(bonding_data); - pm_bond_data.p_all_data = &bonding_data; + pm_bond_data.all_data = &bonding_data; err_code = pds_peer_data_read(current_peer_id, PM_PEER_DATA_ID_BONDING, &pm_bond_data, &bonding_data_size); if (err_code == NRF_ERROR_NOT_FOUND) { skip = true; - } else { - VERIFY_SUCCESS(err_code); + } else if (err_code != NRF_SUCCESS) { + return err_code; } /* Check data */ if (skip_no_addr) { - p_gap_addr = &bonding_data.peer_ble_id.id_addr_info; + gap_addr = &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)) { + if ((gap_addr->addr_type != BLE_GAP_ADDR_TYPE_PUBLIC) && + (gap_addr->addr_type != BLE_GAP_ADDR_TYPE_RANDOM_STATIC)) { skip = true; } } @@ -681,7 +746,7 @@ uint32_t pm_peer_id_list(pm_peer_id_t *p_peer_list, uint32_t *const p_list_size, uint32_t central_addr_res = 0; uint32_t central_addr_res_size = sizeof(uint32_t); - pm_car_data.p_all_data = ¢ral_addr_res; + pm_car_data.all_data = ¢ral_addr_res; err_code = pds_peer_data_read(current_peer_id, PM_PEER_DATA_ID_CENTRAL_ADDR_RES, @@ -689,8 +754,8 @@ uint32_t pm_peer_id_list(pm_peer_id_t *p_peer_list, uint32_t *const p_list_size, if (err_code == NRF_ERROR_NOT_FOUND) { skip = true; - } else { - VERIFY_SUCCESS(err_code); + } else if (err_code != NRF_SUCCESS) { + return err_code; } /* Check data */ @@ -700,7 +765,7 @@ uint32_t pm_peer_id_list(pm_peer_id_t *p_peer_list, uint32_t *const p_list_size, } if (!skip) { - p_peer_list[current_size++] = current_peer_id; + peer_list[current_size++] = current_peer_id; if (current_size >= size) { break; @@ -710,96 +775,109 @@ uint32_t pm_peer_id_list(pm_peer_id_t *p_peer_list, uint32_t *const p_list_size, current_peer_id = pm_next_peer_id_get(current_peer_id); } - *p_list_size = current_size; + *list_size = current_size; return NRF_SUCCESS; } -uint32_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) +uint32_t pm_peer_data_load(uint16_t peer_id, enum pm_peer_data_id data_id, void *data, + uint32_t *length) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_data); - VERIFY_PARAM_NOT_NULL(p_length); + struct pm_peer_data peer_data; - pm_peer_data_t peer_data; + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (data == NULL || length == NULL) { + return NRF_ERROR_NULL; + } memset(&peer_data, 0, sizeof(peer_data)); - peer_data.p_all_data = p_data; + peer_data.all_data = data; - return pds_peer_data_read(peer_id, data_id, &peer_data, p_length); + return pds_peer_data_read(peer_id, data_id, &peer_data, length); } -uint32_t pm_peer_data_bonding_load(pm_peer_id_t peer_id, pm_peer_data_bonding_t *p_data) +uint32_t pm_peer_data_bonding_load(uint16_t peer_id, struct pm_peer_data_bonding *data) { - uint32_t length = sizeof(pm_peer_data_bonding_t); + uint32_t length = sizeof(struct pm_peer_data_bonding); - return pm_peer_data_load(peer_id, PM_PEER_DATA_ID_BONDING, p_data, &length); + return pm_peer_data_load(peer_id, PM_PEER_DATA_ID_BONDING, data, &length); } -uint32_t pm_peer_data_remote_db_load(pm_peer_id_t peer_id, ble_gatt_db_srv_t *p_data, - uint32_t *p_length) +uint32_t pm_peer_data_remote_db_load(uint16_t peer_id, struct ble_gatt_db_srv *data, + uint32_t *length) { - return pm_peer_data_load(peer_id, PM_PEER_DATA_ID_GATT_REMOTE, p_data, p_length); + return pm_peer_data_load(peer_id, PM_PEER_DATA_ID_GATT_REMOTE, data, length); } -uint32_t pm_peer_data_app_data_load(pm_peer_id_t peer_id, void *p_data, uint32_t *p_length) +uint32_t pm_peer_data_app_data_load(uint16_t peer_id, void *data, uint32_t *length) { - return pm_peer_data_load(peer_id, PM_PEER_DATA_ID_APPLICATION, p_data, p_length); + return pm_peer_data_load(peer_id, PM_PEER_DATA_ID_APPLICATION, data, length); } -uint32_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) +uint32_t pm_peer_data_store(uint16_t peer_id, enum pm_peer_data_id data_id, const void *data, + uint32_t length, uint32_t *token) { - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_data); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (data == NULL) { + return NRF_ERROR_NULL; + } + if (!IS_ALIGNED(length, 4)) { return NRF_ERROR_INVALID_PARAM; } if (data_id == PM_PEER_DATA_ID_BONDING) { - pm_peer_id_t dupl_peer_id; + uint16_t dupl_peer_id; dupl_peer_id = - im_find_duplicate_bonding_data((pm_peer_data_bonding_t *)p_data, peer_id); + im_find_duplicate_bonding_data((struct pm_peer_data_bonding *)data, + peer_id); if (dupl_peer_id != PM_PEER_ID_INVALID) { return NRF_ERROR_FORBIDDEN; } } - pm_peer_data_flash_t peer_data; + struct pm_peer_data_const 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; + peer_data.all_data = data; - return pds_peer_data_store(peer_id, &peer_data, p_token); + return pds_peer_data_store(peer_id, &peer_data, token); } -uint32_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) +uint32_t pm_peer_data_bonding_store(uint16_t peer_id, const struct pm_peer_data_bonding *data, + uint32_t *token) { - return pm_peer_data_store(peer_id, PM_PEER_DATA_ID_BONDING, p_data, - ROUND_UP(sizeof(pm_peer_data_bonding_t), 4), p_token); + return pm_peer_data_store(peer_id, PM_PEER_DATA_ID_BONDING, data, + ROUND_UP(sizeof(struct pm_peer_data_bonding), 4), token); } -uint32_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) +uint32_t pm_peer_data_remote_db_store(uint16_t peer_id, const struct ble_gatt_db_srv *data, + uint32_t length, uint32_t *token) { - return pm_peer_data_store(peer_id, PM_PEER_DATA_ID_GATT_REMOTE, p_data, length, p_token); + return pm_peer_data_store(peer_id, PM_PEER_DATA_ID_GATT_REMOTE, data, length, token); } -uint32_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) +uint32_t pm_peer_data_app_data_store(uint16_t peer_id, const void *data, uint32_t length, + uint32_t *token) { - return pm_peer_data_store(peer_id, PM_PEER_DATA_ID_APPLICATION, p_data, length, p_token); + return pm_peer_data_store(peer_id, PM_PEER_DATA_ID_APPLICATION, data, length, token); } -uint32_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) +uint32_t pm_peer_data_delete(uint16_t peer_id, enum pm_peer_data_id data_id) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } if (data_id == PM_PEER_DATA_ID_BONDING) { return NRF_ERROR_INVALID_PARAM; @@ -808,57 +886,61 @@ uint32_t pm_peer_data_delete(pm_peer_id_t peer_id, pm_peer_data_id_t data_id) return pds_peer_data_delete(peer_id, data_id); } -uint32_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) +uint32_t pm_peer_new(uint16_t *new_peer_id, struct pm_peer_data_bonding *bonding_data, + uint32_t *token) { uint32_t err_code; - pm_peer_id_t peer_id; - pm_peer_id_t peer_id_iter; - pm_peer_data_flash_t peer_data; + uint16_t peer_id; + uint16_t peer_id_iter; + struct pm_peer_data_const peer_data; uint8_t peer_data_buffer[PM_PEER_DATA_MAX_SIZE] = { 0 }; - VERIFY_MODULE_INITIALIZED(); - VERIFY_PARAM_NOT_NULL(p_bonding_data); - VERIFY_PARAM_NOT_NULL(p_new_peer_id); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } + + if (new_peer_id == NULL || bonding_data == NULL) { + return NRF_ERROR_NULL; + } - memset(&peer_data, 0, sizeof(pm_peer_data_flash_t)); + memset(&peer_data, 0, sizeof(struct pm_peer_data_const)); - peer_data.p_all_data = peer_data_buffer; + peer_data.all_data = peer_data_buffer; /* Search through existing bonds to look for a duplicate. */ pds_peer_data_iterate_prepare(&peer_id_iter); while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data, &peer_id_iter)) { - if (im_is_duplicate_bonding_data(p_bonding_data, peer_data.p_bonding_data)) { - *p_new_peer_id = peer_id; + if (im_is_duplicate_bonding_data(bonding_data, peer_data.bonding_data)) { + *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(); + *new_peer_id = pds_peer_id_allocate(); - if (*p_new_peer_id == PM_PEER_ID_INVALID) { + if (*new_peer_id == PM_PEER_ID_INVALID) { return NRF_ERROR_NO_MEM; } - memset(&peer_data, 0, sizeof(pm_peer_data_flash_t)); + memset(&peer_data, 0, sizeof(struct pm_peer_data_const)); 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)); + peer_data.bonding_data = bonding_data; + peer_data.length_words = BYTES_TO_WORDS(sizeof(struct pm_peer_data_bonding)); - err_code = pds_peer_data_store(*p_new_peer_id, &peer_data, p_token); + err_code = pds_peer_data_store(*new_peer_id, &peer_data, token); if (err_code != NRF_SUCCESS) { - uint32_t err_code_free = im_peer_free(*p_new_peer_id); + uint32_t err_code_free = im_peer_free(*new_peer_id); if (err_code_free != NRF_SUCCESS) { LOG_ERR("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); + __func__, nrf_strerror_get(err_code_free), *new_peer_id); return NRF_ERROR_INTERNAL; } @@ -873,28 +955,32 @@ uint32_t pm_peer_new(pm_peer_id_t *p_new_peer_id, pm_peer_data_bonding_t *p_bond return NRF_SUCCESS; } -uint32_t pm_peer_delete(pm_peer_id_t peer_id) +uint32_t pm_peer_delete(uint16_t peer_id) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } return im_peer_free(peer_id); } uint32_t pm_peers_delete(void) { - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } - m_deleting_all = true; + deleting_all = true; - pm_peer_id_t current_peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID); + uint16_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; + deleting_all = false; - pm_evt_t pm_delete_all_evt; + struct pm_evt pm_delete_all_evt; - memset(&pm_delete_all_evt, 0, sizeof(pm_evt_t)); + memset(&pm_delete_all_evt, 0, sizeof(struct pm_evt)); 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; @@ -918,24 +1004,33 @@ uint32_t pm_peers_delete(void) return NRF_SUCCESS; } -uint32_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) +uint32_t pm_peer_ranks_get(uint16_t *highest_ranked_peer, uint32_t *highest_rank, + uint16_t *lowest_ranked_peer, uint32_t *lowest_rank) { #if !defined(CONFIG_PM_PEER_RANKS) return NRF_ERROR_NOT_SUPPORTED; #else - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } - pm_peer_id_t peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID); + uint16_t peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID); uint32_t peer_rank = 0; uint32_t length = sizeof(peer_rank); - pm_peer_data_t peer_data = {.p_peer_rank = &peer_rank}; + struct pm_peer_data peer_data = {.peer_rank = &peer_rank}; uint32_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; + struct { + uint32_t highest; + uint32_t lowest; + uint16_t highest_peer; + uint16_t lowest_peer; + } rank = { + .highest = 0, + .lowest = 0xFFFFFFFF, + .highest_peer = PM_PEER_ID_INVALID, + .lowest_peer = PM_PEER_ID_INVALID, + }; if (err_code == NRF_ERROR_INVALID_PARAM) { /* No peer IDs exist. */ @@ -944,13 +1039,13 @@ uint32_t pm_peer_ranks_get(pm_peer_id_t *p_highest_ranked_peer, uint32_t *p_high 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 >= rank.highest) { + rank.highest = peer_rank; + rank.highest_peer = peer_id; } - if (peer_rank < lowest_rank) { - lowest_rank = peer_rank; - lowest_ranked_peer = peer_id; + if (peer_rank < rank.lowest) { + rank.lowest = peer_rank; + rank.lowest_peer = peer_id; } } peer_id = pds_next_peer_id_get(peer_id); @@ -958,24 +1053,24 @@ uint32_t pm_peer_ranks_get(pm_peer_id_t *p_highest_ranked_peer, uint32_t *p_high 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)) { + if ((rank.highest_peer == PM_PEER_ID_INVALID) || + (rank.lowest_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 (highest_ranked_peer != NULL) { + *highest_ranked_peer = rank.highest_peer; } - if (p_highest_rank != NULL) { - *p_highest_rank = highest_rank; + if (highest_rank != NULL) { + *highest_rank = rank.highest; } - if (p_lowest_ranked_peer != NULL) { - *p_lowest_ranked_peer = lowest_ranked_peer; + if (lowest_ranked_peer != NULL) { + *lowest_ranked_peer = rank.lowest_peer; } - if (p_lowest_rank != NULL) { - *p_lowest_rank = lowest_rank; + if (lowest_rank != NULL) { + *lowest_rank = rank.lowest; } } else { LOG_ERR("Could not retrieve ranks. pdb_peer_data_load() returned %s. peer_id: %d", @@ -994,35 +1089,37 @@ static void rank_init(void) } #endif -uint32_t pm_peer_rank_highest(pm_peer_id_t peer_id) +uint32_t pm_peer_rank_highest(uint16_t peer_id) { #if !defined(CONFIG_PM_PEER_RANKS) return NRF_ERROR_NOT_SUPPORTED; #else - VERIFY_MODULE_INITIALIZED(); + if (!module_initialized) { + return NRF_ERROR_INVALID_STATE; + } uint32_t err_code; - pm_peer_data_flash_t peer_data = { - .length_words = BYTES_TO_WORDS(sizeof(m_current_highest_peer_rank)), + struct pm_peer_data_const peer_data = { + .length_words = BYTES_TO_WORDS(sizeof(current_highest_peer_rank)), .data_id = PM_PEER_DATA_ID_PEER_RANK, - .p_peer_rank = &m_current_highest_peer_rank}; + .peer_rank = ¤t_highest_peer_rank}; - if (!m_peer_rank_initialized) { + if (!peer_rank_initialized) { rank_init(); } - if (!m_peer_rank_initialized || (m_peer_rank_token != PM_STORE_TOKEN_INVALID)) { + if (!peer_rank_initialized || (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; + if ((peer_id == highest_ranked_peer) && (current_highest_peer_rank > 0)) { + struct pm_evt 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)); + memset(&pm_evt, 0, sizeof(struct 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; @@ -1034,15 +1131,15 @@ uint32_t pm_peer_rank_highest(pm_peer_id_t peer_id) evt_send(&pm_evt); } else { - if (m_current_highest_peer_rank == UINT32_MAX) { + if (current_highest_peer_rank == UINT32_MAX) { err_code = NRF_ERROR_DATA_SIZE; } else { - m_current_highest_peer_rank += 1; + current_highest_peer_rank += 1; err_code = pds_peer_data_store(peer_id, &peer_data, - &m_peer_rank_token); + &peer_rank_token); if (err_code != NRF_SUCCESS) { - m_peer_rank_token = PM_STORE_TOKEN_INVALID; - m_current_highest_peer_rank -= 1; + peer_rank_token = PM_STORE_TOKEN_INVALID; + current_highest_peer_rank -= 1; /* Assume INVALID_PARAM * refers to peer_id, not * data_id. diff --git a/samples/bluetooth/ble_hids_keyboard/src/main.c b/samples/bluetooth/ble_hids_keyboard/src/main.c index d6e34e9e69..77c12844b2 100644 --- a/samples/bluetooth/ble_hids_keyboard/src/main.c +++ b/samples/bluetooth/ble_hids_keyboard/src/main.c @@ -93,14 +93,14 @@ BLE_BAS_DEF(ble_bas); /* BLE Connection handle */ static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID; /* Peer ID */ -static pm_peer_id_t peer_id; +static uint16_t peer_id; /* Flag for ongoing authentication request */ static bool auth_key_request; /* State of caps lock */ static bool caps_on; /* Forward declaration */ -static void identities_set(pm_peer_id_list_skip_t skip); +static void identities_set(enum pm_peer_id_list_skip skip); /* FIFO for keeping track of keystrokes that can not be sent immediately. */ RING_BUF_DECLARE(report_fifo, CONFIG_BLE_HIDS_REPORT_FIFO_SIZE * @@ -290,7 +290,7 @@ static void ble_adv_evt_handler(struct ble_adv *ble_adv, const struct ble_adv_ev break; case BLE_ADV_EVT_PEER_ADDR_REQUEST: - pm_peer_data_bonding_t peer_bonding_data; + struct pm_peer_data_bonding peer_bonding_data; /* Only Give peer address if we have a handle to the bonded peer. */ if (peer_id != PM_PEER_ID_INVALID) { @@ -663,10 +663,10 @@ static void button_handler(uint8_t pin, uint8_t action) } } -static void whitelist_set(pm_peer_id_list_skip_t skip) +static void whitelist_set(enum pm_peer_id_list_skip skip) { uint32_t err; - pm_peer_id_t peer_ids[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; + uint16_t peer_ids[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; uint32_t peer_id_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT; err = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip); @@ -683,10 +683,10 @@ static void whitelist_set(pm_peer_id_list_skip_t skip) } } -static void identities_set(pm_peer_id_list_skip_t skip) +static void identities_set(enum pm_peer_id_list_skip skip) { uint32_t err; - pm_peer_id_t peer_ids[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; + uint16_t peer_ids[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; uint32_t peer_id_count = BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT; err = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip); @@ -730,7 +730,7 @@ static int advertising_start(bool erase_bonds) return err; } -static void pm_evt_handler(pm_evt_t const *evt) +static void pm_evt_handler(struct pm_evt const *evt) { pm_handler_on_pm_evt(evt); pm_handler_disconnect_on_sec_failure(evt); diff --git a/samples/bluetooth/ble_hids_mouse/src/main.c b/samples/bluetooth/ble_hids_mouse/src/main.c index ec0f086d2d..64fd34e6c7 100644 --- a/samples/bluetooth/ble_hids_mouse/src/main.c +++ b/samples/bluetooth/ble_hids_mouse/src/main.c @@ -93,9 +93,9 @@ BLE_QWR_DEF(ble_qwr); /* BLE Connection handle */ static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID; -static pm_peer_id_t peer_id; +static uint16_t peer_id; -static void identities_set(pm_peer_id_list_skip_t skip); +static void identities_set(enum pm_peer_id_list_skip skip); static bool boot_mode; static bool auth_key_request; @@ -173,7 +173,7 @@ static void ble_adv_evt_handler(struct ble_adv *ble_adv, const struct ble_adv_ev { uint32_t err; ble_gap_addr_t *peer_addr; - pm_peer_data_bonding_t peer_bonding_data; + struct pm_peer_data_bonding peer_bonding_data; ble_gap_addr_t whitelist_addrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; ble_gap_irk_t whitelist_irks[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; uint32_t addr_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT; @@ -514,10 +514,10 @@ static void button_handler(uint8_t pin, uint8_t action) } } -static void whitelist_set(pm_peer_id_list_skip_t skip) +static void whitelist_set(enum pm_peer_id_list_skip skip) { uint32_t err; - pm_peer_id_t peer_ids[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; + uint16_t peer_ids[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; uint32_t peer_id_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT; err = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip); @@ -534,10 +534,10 @@ static void whitelist_set(pm_peer_id_list_skip_t skip) } } -static void identities_set(pm_peer_id_list_skip_t skip) +static void identities_set(enum pm_peer_id_list_skip skip) { uint32_t err; - pm_peer_id_t peer_ids[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; + uint16_t peer_ids[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT]; uint32_t peer_id_count = BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT; err = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip); @@ -581,7 +581,7 @@ static uint32_t advertising_start(bool erase_bonds) return err; } -static void pm_evt_handler(pm_evt_t const *evt) +static void pm_evt_handler(struct pm_evt const *evt) { pm_handler_on_pm_evt(evt); pm_handler_disconnect_on_sec_failure(evt); diff --git a/samples/bluetooth/ble_hrs/src/main.c b/samples/bluetooth/ble_hrs/src/main.c index f73d0293d0..801305e105 100644 --- a/samples/bluetooth/ble_hrs/src/main.c +++ b/samples/bluetooth/ble_hrs/src/main.c @@ -378,7 +378,7 @@ static void advertising_start(bool erase_bonds) } } -static void pm_evt_handler(pm_evt_t const *p_evt) +static void pm_evt_handler(struct pm_evt const *p_evt) { pm_handler_on_pm_evt(p_evt); pm_handler_disconnect_on_sec_failure(p_evt);