Skip to content

Commit 3553a45

Browse files
authored
Merge pull request #4178 from nvlsianpu/getAddrFromPeerTab_sd_api_5
[NRF52840]: SecurityManager::getAddressesFromBondTable
2 parents e229a49 + 39c1b3f commit 3553a45

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5/source/btle/btle_security.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,20 @@ bool btle_matchAddressAndIrk(ble_gap_addr_t const * p_addr, ble_gap_irk_t const
127127
*/
128128
void btle_generateResolvableAddress(const ble_gap_irk_t &irk, ble_gap_addr_t &address);
129129

130+
#if (NRF_SD_BLE_API_VERSION >= 3)
131+
/**
132+
* @brief Returns a list of addresses from peers in the stacks bond table.
133+
*
134+
* @param[in/out] addresses
135+
* (on input) @ref Gap::Whitelist_t structure where at
136+
* most addresses.capacity addresses from bonded peers will
137+
* be stored.
138+
* (on output) A copy of the addresses from bonded peers.
139+
*
140+
* @retval BLE_ERROR_NONE if successful.
141+
* @retval BLE_ERROR_UNSPECIFIED Bond data could not be found in flash or is inconsistent.
142+
*/
143+
ble_error_t btle_getAddressesFromBondTable(Gap::Whitelist_t &addrList);
144+
#endif
145+
130146
#endif /* _BTLE_SECURITY_H_ */

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5/source/btle/btle_security_pm.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,53 @@ btle_generateResolvableAddress(const ble_gap_irk_t &irk, ble_gap_addr_t &address
434434
/* Calculate the hash and store it in the top half of the address */
435435
ah(irk.irk, &address.addr[BLE_GAP_ADDR_LEN - 3], address.addr);
436436
}
437+
438+
439+
#if (NRF_SD_BLE_API_VERSION >= 3)
440+
ble_error_t btle_getAddressesFromBondTable(Gap::Whitelist_t &addrList)
441+
{
442+
pm_peer_id_t peer_id;
443+
ret_code_t ret;
444+
pm_peer_data_bonding_t bond_data;
445+
446+
addrList.size = 0;
447+
peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);
448+
449+
/**
450+
* Fill addresses list:
451+
* Copy addresses from bond table, or
452+
* for every private resolvable address in the bond table generate the resolvable address.
453+
*/
454+
while ((peer_id != PM_PEER_ID_INVALID) && (addrList.capacity > addrList.size)) {
455+
memset(&bond_data, 0x00, sizeof(bond_data));
456+
457+
// Read peer data from flash.
458+
ret = pm_peer_data_bonding_load(peer_id, &bond_data);
459+
460+
if ((ret == NRF_ERROR_NOT_FOUND) || (ret == NRF_ERROR_INVALID_PARAM)) {
461+
// Peer data could not be found in flash or peer ID is not valid.
462+
return BLE_ERROR_UNSPECIFIED;
463+
}
464+
465+
if (bond_data.peer_ble_id.id_addr_info.addr_type == BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE) {
466+
btle_generateResolvableAddress(bond_data.peer_ble_id.id_info,
467+
(ble_gap_addr_t &) addrList.addresses[addrList.size].address);
468+
} else {
469+
memcpy(&addrList.addresses[addrList.size].address,
470+
&bond_data.peer_ble_id.id_addr_info.addr,
471+
sizeof(addrList.addresses[0].address));
472+
}
473+
474+
addrList.addresses[addrList.size].type = static_cast<BLEProtocol::AddressType_t> (bond_data.peer_ble_id.id_addr_info.addr_type);
475+
476+
addrList.size++;
477+
478+
// get next peer id
479+
peer_id = pm_next_peer_id_get(peer_id);
480+
}
481+
482+
return BLE_ERROR_NONE;
483+
}
437484
#endif
485+
486+
#endif // defined(S130) || defined(S132) || defined(S140)

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5/source/nRF5xSecurityManager.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ class nRF5xSecurityManager : public SecurityManager
5757
*
5858
* @return
5959
* BLE_ERROR_NONE if successful.
60-
*
61-
* @todo check whether remove this function (because it is never called)
6260
*/
6361
virtual ble_error_t getAddressesFromBondTable(Gap::Whitelist_t &addresses) const {
6462
uint8_t i;
@@ -114,7 +112,26 @@ class nRF5xSecurityManager : public SecurityManager
114112

115113
return BLE_ERROR_NONE;
116114
}
117-
#endif
115+
#else // -> NRF_SD_BLE_API_VERSION >= 3
116+
/**
117+
* @brief Returns a list of addresses from peers in the stacks bond table.
118+
*
119+
* @param[in/out] addresses
120+
* (on input) @ref Gap::Whitelist_t structure where at
121+
* most addresses.capacity addresses from bonded peers will
122+
* be stored.
123+
* (on output) A copy of the addresses from bonded peers.
124+
*
125+
* @retval BLE_ERROR_NONE if successful.
126+
* @retval BLE_ERROR_UNSPECIFIED Bond data could not be found in flash or is inconsistent.
127+
*/
128+
virtual ble_error_t getAddressesFromBondTable(Gap::Whitelist_t &addresses) const {
129+
return btle_getAddressesFromBondTable(addresses);
130+
}
131+
#endif // #if (NRF_SD_BLE_API_VERSION <= 2)
132+
133+
134+
118135
/**
119136
* @brief Clear nRF5xSecurityManager's state.
120137
*

0 commit comments

Comments
 (0)