None

Dependents:   nRF51822

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers id_manager.h Source File

id_manager.h

00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 #ifndef PEER_ID_MANAGER_H__
00034 #define PEER_ID_MANAGER_H__
00035 
00036 #include "stdint.h"
00037 #include "sdk_errors.h "
00038 #include "ble.h"
00039 #include "ble_gap.h"
00040 #include "peer_manager_types.h "
00041 
00042 
00043 /**
00044  * @defgroup id_manager ID Manager
00045  * @ingroup peer_manager
00046  * @{
00047  * @brief An internal module of @ref peer_manager. A module for keeping track of peer identities
00048  *       (IRK and peer address).
00049  */
00050 
00051 
00052 /**@brief Events that can come from the ID Manager module.
00053  */
00054 typedef enum
00055 {
00056     IM_EVT_DUPLICATE_ID,          /**< The ID Manager module has detected that two stored peers represent the same peer. */
00057     IM_EVT_BONDED_PEER_CONNECTED, /**< A connected peer has been identified as one of the bonded peers. This can happen immediately on connection, or at a later time. */
00058 } im_evt_id_t;
00059 
00060 
00061 typedef struct
00062 {
00063     im_evt_id_t evt_id;
00064     uint16_t    conn_handle;
00065     union
00066     {
00067         struct
00068         {
00069             pm_peer_id_t peer_id_1;
00070             pm_peer_id_t peer_id_2;
00071         } duplicate_id;
00072     } params;
00073 } im_evt_t;
00074 
00075 
00076 /**@brief Event handler for events from the ID Manager module.
00077  *
00078  * @param[in]  p_event   The event that has happened.
00079  */
00080 typedef void (*im_evt_handler_t)(im_evt_t const * p_event);
00081 
00082 /**@brief Function for registering for events from the ID Manager module.
00083  *
00084  * @note This will also initialize the module if needed.
00085  *
00086  * @param[in]  evt_handler  Callback for events from the ID Manager module.
00087  *
00088  * @retval NRF_SUCCESS       Registration was successful.
00089  * @retval NRF_ERROR_NO_MEM  No more registrations possible.
00090  * @retval NRF_ERROR_NULL    evt_handler was NULL.
00091  */
00092 ret_code_t im_register(im_evt_handler_t evt_handler);
00093 
00094 
00095 /**@brief Function for dispatching SoftDevice events to the ID Manager module.
00096  *
00097  * @param[in]  p_ble_evt  The SoftDevice event.
00098  */
00099 void im_ble_evt_handler(ble_evt_t * p_ble_evt);
00100 
00101 
00102 /**@brief Function for getting the corresponding peer ID from a connection handle.
00103  *
00104  * @param[in]  conn_handle  The connection handle.
00105  *
00106  * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
00107  */
00108 pm_peer_id_t im_peer_id_get_by_conn_handle(uint16_t conn_handle);
00109 
00110 
00111 /**@brief Function for getting the corresponding peer ID from a master ID (EDIV and rand).
00112  *
00113  * @param[in]  p_master_id  The master ID.
00114  *
00115  * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
00116  */
00117 pm_peer_id_t im_peer_id_get_by_master_id(ble_gap_master_id_t * p_master_id);
00118 
00119 
00120 /**@brief Function for getting the corresponding peer ID from an IRK match index, see @ref
00121  *        ble_gap_evt_connected_t.
00122  *
00123  * @param[in]  irk_match_idx  The IRK match index.
00124  *
00125  * @return The corresponding peer ID, or @ref PM_PEER_ID_INVALID if none could be resolved.
00126  */
00127 pm_peer_id_t im_peer_id_get_by_irk_match_idx(uint8_t irk_match_idx);
00128 
00129 
00130 /**@brief Function for getting the corresponding connection handle from a peer ID.
00131  *
00132  * @param[in] peer_id  The peer ID.
00133  *
00134  * @return The corresponding connection handle, or @ref BLE_CONN_HANDLE_INVALID if none could be
00135  *         resolved.
00136  */
00137 uint16_t im_conn_handle_get(pm_peer_id_t peer_id);
00138 
00139 
00140 /**@brief Function for getting the BLE address used by the peer when connecting.
00141  *
00142  * @param[in]  conn_handle  The connection handle.
00143  * @param[out] p_ble_addr   The BLE address used by the peer when the connection specified by
00144  *                          conn_handle was established.
00145  *
00146  * @retval NRF_SUCCESS                   The address was found and copied.
00147  * @retval NRF_ERROR_INVALID_STATE       Module not initialized.
00148  * @retval BLE_ERROR_CONN_HANDLE_INVALID conn_handle does not refer to an active connection.
00149  * @retval NRF_ERROR_NULL                p_ble_addr was NULL.
00150  */
00151 ret_code_t im_ble_addr_get(uint16_t conn_handle, ble_gap_addr_t * p_ble_addr);
00152 
00153 
00154 /**@brief Function for checking whether a master ID is valid or invalid
00155  *
00156  * @param[in]  p_master_id  The master ID.
00157  *
00158  * @retval true   The master id is valid.
00159  * @retval true   The master id is invalid (i.e. all zeros).
00160  */
00161 bool im_master_id_is_valid(ble_gap_master_id_t const * p_master_id);
00162 
00163 
00164 /**@brief Function for reporting that a new peer ID has been allocated for a specified connection.
00165  *
00166  * @param[in]  conn_handle  The connection.
00167  * @param[in]  peer_id      The new peer ID.
00168  */
00169 void im_new_peer_id(uint16_t conn_handle, pm_peer_id_t peer_id);
00170 
00171 
00172 /**
00173  * @brief Function for informing this module of what whitelist will be used.
00174  *
00175  * @details This function is meant to be used when the app wants to use a custom whitelist.
00176  *          When using peer manager, this function must be used if a custom whitelist is used.
00177  *
00178  * @note When using a whitelist, always use the whitelist created/set by the most recent
00179  *       call to @ref im_wlist_create or to this function, whichever happened most recently.
00180  * @note Do not call this function while scanning with another whitelist.
00181  * @note Do not add any irks to the whitelist that are not present in the bonding data of a peer in
00182  *       the peer database.
00183  *
00184  * @param[in] p_whitelist  The whitelist.
00185  *
00186  * @retval NRF_SUCCESS         Whitelist successfully set.
00187  * @retval NRF_ERROR_NULL      p_whitelist was NULL.
00188  * @retval NRF_ERROR_NOT_FOUND One or more of the whitelists irks was not found in the peer_database.
00189  */
00190 ret_code_t im_wlist_set(ble_gap_whitelist_t * p_whitelist);
00191 
00192 
00193 /**
00194  * @brief Function for constructing a whitelist for use when advertising.
00195  *
00196  * @note When advertising with whitelist, always use the whitelist created/set by the most recent
00197  *       call to this function or to @ref im_wlist_set, whichever happened most recently.
00198  * @note Do not call this function while advertising with another whitelist.
00199  *
00200  * @param[in]     p_peer_ids   The ids of the peers to be added to the whitelist.
00201  * @param[in]     n_peer_ids   The number of peer ids in p_peer_ids.
00202  * @param[in,out] p_whitelist  The constructed whitelist. Note that p_adv_whitelist->pp_addrs
00203  *                             must be NULL or point to an array with size @ref
00204  *                             BLE_GAP_WHITELIST_ADDR_MAX_COUNT and p_adv_whitelist->pp_irks
00205  *                             must be NULL or point to an array with size @ref
00206  *                             BLE_GAP_WHITELIST_IRK_MAX_COUNT.
00207  *
00208  * @retval NRF_SUCCESS     Whitelist successfully created.
00209  * @retval NRF_ERROR_NULL  p_whitelist was NULL.
00210  */
00211 ret_code_t im_wlist_create(pm_peer_id_t        * p_peer_ids,
00212                            uint8_t               n_peer_ids,
00213                            ble_gap_whitelist_t * p_whitelist);
00214 
00215 /**
00216  * @brief Function for resolving a resolvable address with an identity resolution key (IRK).
00217  *
00218  * @details This function will use the ECB peripheral to resolve a resolvable address.
00219  *          This can be used to resolve the identity of a device distributing a random
00220  *          resolvable address based on any IRKs you have received earlier. If an address is
00221  *          resolved by an IRK, the device disributing the address must also know the IRK.
00222  *
00223  * @param[in] p_addr  A random resolvable address.
00224  * @param[in] p_irk   An identity resolution key (IRK).
00225  *
00226  * @retval true   The irk used matched the one used to create the address.
00227  * @retval false  The irk used did not match the one used to create the address, or an argument was
00228  *                NULL.
00229  */
00230 bool im_address_resolve(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk);
00231 
00232 /**@brief Function for calculating the ah() hash function described in Bluetooth core specification
00233  *        4.2 section 3.H.2.2.2.
00234  *
00235  * @detail  BLE uses a hash function to calculate the first half of a resolvable address
00236  *          from the second half of the address and an irk. This function will use the ECB
00237  *          periferal to hash these data acording to the Bluetooth core specification.
00238  *
00239  * @note The ECB expect little endian input and output.
00240  *       This function expect big endian and will reverse the data as necessary.
00241  *
00242  * @param[in]  p_k          The key used in the hash function.
00243  *                          For address resolution this is should be the irk.
00244  *                          The array must have a length of 16.
00245  * @param[in]  p_r          The rand used in the hash function. For generating a new address
00246  *                          this would be a random number. For resolving a resolvable address
00247  *                          this would be the last half of the address being resolved.
00248  *                          The array must have a length of 3.
00249  * @param[out] p_local_hash The result of the hash operation. For address resolution this
00250  *                          will match the first half of the address being resolved if and only
00251  *                          if the irk used in the hash function is the same one used to generate
00252  *                          the address.
00253  *                          The array must have a length of 16.
00254  *
00255  * @note    ====IMPORTANT====
00256  *          This is a special modification to the original nRF51 SDK required by the mbed BLE API
00257  *          to be able to generate BLE private resolvable addresses. This function is used by
00258  *          the BLE API implementation for nRF5xSecurityManager::getAddressFromBondTable() in the
00259  *          ble-nrf51822 yotta module.
00260  *          =================
00261  */
00262 void ah(uint8_t const * p_k, uint8_t const * p_r, uint8_t * p_local_hash);
00263 
00264 /** @} */
00265 
00266 #endif /* PEER_ID_MANAGER_H__ */