Skip to content

Commit 42fcae4

Browse files
committed
nRF5: whitelisting update fo SD API >-3.x.x
1 parent 8aa71fa commit 42fcae4

File tree

5 files changed

+151
-34
lines changed

5 files changed

+151
-34
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5_SDK13/source/nRF5xGap.cpp

Lines changed: 136 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#if (NRF_SD_BLE_API_VERSION >= 3)
3030
#include "peer_manager.h"
31+
#include "peer_data_storage.h"
3132
#endif
3233

3334

@@ -172,7 +173,7 @@ ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
172173
(params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) {
173174
return BLE_ERROR_PARAM_OUT_OF_RANGE;
174175
}
175-
uint32_t err ;
176+
uint32_t err;
176177
#if (NRF_SD_BLE_API_VERSION <= 2)
177178
/* Allocate the stack's whitelist statically */
178179
ble_gap_whitelist_t whitelist;
@@ -192,9 +193,7 @@ ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
192193
}
193194
}
194195
#else
195-
gapAdrHelper_t gapAdrHelper;
196-
getStackWhiteIdentityList(gapAdrHelper);
197-
err = apllyWhiteIdentityList(gapAdrHelper);
196+
err = updateWhiteAndIdentityListInStack();
198197

199198
if (err != BLE_ERROR_NONE) {
200199
return (ble_error_t)err;
@@ -247,9 +246,7 @@ ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams)
247246
}
248247
}
249248
#else
250-
gapAdrHelper_t gapAdrHelper;
251-
getStackWhiteIdentityList(gapAdrHelper);
252-
uint32_t err = apllyWhiteIdentityList(gapAdrHelper);
249+
uint32_t err = updateWhiteAndIdentityListInStack();
253250

254251
if (err != BLE_ERROR_NONE) {
255252
return (ble_error_t)err;
@@ -262,6 +259,11 @@ ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams)
262259
#if (NRF_SD_BLE_API_VERSION <= 2)
263260
scanParams.selective = scanningPolicyMode; /**< If 1, ignore unknown devices (non whitelisted). */
264261
scanParams.p_whitelist = &whitelist; /**< Pointer to whitelist, NULL if none is given. */
262+
#else
263+
scanParams.use_whitelist = scanningPolicyMode;
264+
scanParams.adv_dir_report = 0;
265+
266+
265267
#endif
266268
scanParams.interval = scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
267269
scanParams.window = scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
@@ -350,9 +352,7 @@ ble_error_t nRF5xGap::connect(const Address_t peerAddr,
350352
}
351353
}
352354
#else
353-
gapAdrHelper_t gapAdrHelper;
354-
getStackWhiteIdentityList(gapAdrHelper);
355-
uint32_t err = apllyWhiteIdentityList(gapAdrHelper);
355+
uint32_t err = updateWhiteAndIdentityListInStack();
356356

357357
if (err != BLE_ERROR_NONE) {
358358
return (ble_error_t)err;
@@ -363,6 +363,9 @@ ble_error_t nRF5xGap::connect(const Address_t peerAddr,
363363
#if (NRF_SD_BLE_API_VERSION <= 2)
364364
scanParams.selective = scanningPolicyMode; /**< If 1, ignore unknown devices (non whitelisted). */
365365
scanParams.p_whitelist = &whitelist; /**< Pointer to whitelist, NULL if none is given. */
366+
#else
367+
scanParams.use_whitelist = scanningPolicyMode;
368+
scanParams.adv_dir_report = 0;
366369
#endif
367370

368371
if (scanParamsIn != NULL) {
@@ -1015,32 +1018,124 @@ ble_error_t nRF5xGap::generateStackWhitelist(ble_gap_whitelist_t &whitelist)
10151018
#endif
10161019

10171020
#if (NRF_SD_BLE_API_VERSION >= 3)
1021+
1022+
/**
1023+
* Fuction for preparing setting of the whitelist-feature and identiti-reseolv-feature (privacy).
1024+
*
1025+
* Created setting are intended to be used to configure SoftDevices.
1026+
*
1027+
* @param[out] gapAdrHelper Reference to the struct for storing settings.
1028+
*/
10181029

1030+
ble_error_t nRF5xGap::getStackWhiteIdentityList(GapWhiteAndIdentityList_t &gapAdrHelper)
1031+
{
1032+
uint32_t peers_to_check = pm_peer_count();
1033+
pm_peer_id_t peer_id;
1034+
1035+
ret_code_t ret;
10191036

1020-
ble_error_t nRF5xGap::getStackWhiteIdentityList(gapAdrHelper_t &gapAdrHelper)
1021-
{
1022-
// it's a mock
1023-
//@todo non trivial implementation
1024-
gapAdrHelper.num_of_whitelist_items = 0;
1025-
gapAdrHelper.num_of_identiti_items = 0;
1037+
pm_peer_data_bonding_t bond_data;
1038+
pm_peer_data_t peer_data;
1039+
uint32_t const buf_size = sizeof(bond_data);
1040+
1041+
memset(&peer_data, 0x00, sizeof(peer_data));
1042+
peer_data.p_bonding_data = &bond_data;
1043+
1044+
1045+
1046+
uint8_t irk_fund[YOTTA_CFG_WHITELIST_MAX_SIZE];
1047+
1048+
memset(irk_fund, 0x00, sizeof(irk_fund));
1049+
1050+
1051+
gapAdrHelper.identities_cnt = 0;
1052+
1053+
1054+
peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);
1055+
1056+
nRF5xSecurityManager& securityManager = (nRF5xSecurityManager&) nRF5xn::Instance(0).getSecurityManager();
1057+
1058+
/**
1059+
* Build identities list:
1060+
* For every private resolvable address in the bond table check if
1061+
* there is maching address in th provided whitelist.
1062+
*/
1063+
while ((peer_id != PM_PEER_ID_INVALID) && (peers_to_check--))
1064+
{
1065+
memset(&bond_data, 0x00, sizeof(bond_data));
1066+
1067+
// Read peer data from flash.
1068+
ret = pds_peer_data_read(peer_id, PM_PEER_DATA_ID_BONDING,
1069+
&peer_data, &buf_size);
1070+
1071+
1072+
if ((ret == NRF_ERROR_NOT_FOUND) || (ret == NRF_ERROR_INVALID_PARAM))
1073+
{
1074+
// Peer data coulnd't be found in flash or peer ID is not valid.
1075+
return BLE_ERROR_UNSPECIFIED;
1076+
}
1077+
1078+
if ( bond_data.peer_ble_id.id_addr_info.addr_type == BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE)
1079+
{
1080+
for (uint8_t i = 0; i < whitelistAddressesSize; ++i)
1081+
{
1082+
if (!irk_fund[i])
1083+
{
1084+
if (whitelistAddresses[i].addr_type == BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE)
1085+
{
1086+
1087+
//ble_gap_irk_t *p_dfg = &bond_data.peer_ble_id.id_info;
1088+
if (securityManager.matchAddressAndIrk(&whitelistAddresses[i], &bond_data.peer_ble_id.id_info))
1089+
{
1090+
// Copy data to the buffer.
1091+
memcpy(&gapAdrHelper.identity[i], &bond_data.peer_ble_id, sizeof(ble_gap_id_key_t));
1092+
gapAdrHelper.pp_identities[i] = &gapAdrHelper.identity[i];
1093+
gapAdrHelper.identities_cnt++;
1094+
1095+
irk_fund[i] = 1; // don't look at this address again
1096+
}
1097+
}
1098+
}
1099+
}
1100+
}
1101+
1102+
// get next peer id
1103+
peer_id = pm_next_peer_id_get(peer_id);
1104+
}
1105+
1106+
gapAdrHelper.addr_cnt = 0;
1107+
1108+
/**
1109+
* Bulida whitelist from the rest of addresses (explicite addressess6)
1110+
*/
1111+
for (uint8_t i = 0; i < whitelistAddressesSize; ++i)
1112+
{
1113+
if (!irk_fund[i])
1114+
{
1115+
memcpy(&gapAdrHelper.addr[i], &bond_data.peer_ble_id.id_addr_info, sizeof(ble_gap_addr_t));
1116+
gapAdrHelper.pp_addr[i] = &gapAdrHelper.addr[i];
1117+
gapAdrHelper.addr_cnt++;
1118+
}
1119+
}
1120+
10261121
return BLE_ERROR_NONE;
10271122
}
10281123

1029-
ble_error_t nRF5xGap::apllyWhiteIdentityList(gapAdrHelper_t &gapAdrHelper)
1124+
ble_error_t nRF5xGap::apllyWhiteIdentityList(GapWhiteAndIdentityList_t &gapAdrHelper)
10301125
{
10311126
uint32_t retc;
10321127

1033-
if (gapAdrHelper.num_of_identiti_items == 0) {
1128+
if (gapAdrHelper.identities_cnt == 0) {
10341129
retc = sd_ble_gap_device_identities_set(NULL, NULL, 0);
10351130
} else {
1036-
retc = sd_ble_gap_device_identities_set(gapAdrHelper.identities, NULL /* Don't use local IRKs*/,gapAdrHelper.num_of_identiti_items);
1131+
retc = sd_ble_gap_device_identities_set(gapAdrHelper.pp_identities, NULL /* Don't use local IRKs*/,gapAdrHelper.identities_cnt);
10371132
}
10381133

10391134
if (retc == NRF_SUCCESS) {
1040-
if (gapAdrHelper.num_of_whitelist_items == 0) {
1135+
if (gapAdrHelper.addr_cnt == 0) {
10411136
retc = sd_ble_gap_whitelist_set(NULL, 0);
10421137
} else {
1043-
retc = sd_ble_gap_whitelist_set(gapAdrHelper.whitelist, gapAdrHelper.num_of_whitelist_items);
1138+
retc = sd_ble_gap_whitelist_set(gapAdrHelper.pp_addr, gapAdrHelper.addr_cnt);
10441139
}
10451140
}
10461141

@@ -1063,4 +1158,24 @@ ble_error_t nRF5xGap::apllyWhiteIdentityList(gapAdrHelper_t &gapAdrHelper)
10631158
}
10641159
}
10651160

1161+
ble_error_t nRF5xGap::updateWhiteAndIdentityListInStack(void)
1162+
{
1163+
GapWhiteAndIdentityList_t whiteAndIdentityList;
1164+
uint32_t err;
1165+
1166+
/* Add missing IRKs to nRF5xGap's whitelist from the bond table held by the Peer Manager */
1167+
if (advertisingPolicyMode != Gap::ADV_POLICY_IGNORE_WHITELIST) {
1168+
err = getStackWhiteIdentityList(whiteAndIdentityList);
1169+
1170+
if (err != BLE_ERROR_NONE) {
1171+
return (ble_error_t)err;
1172+
}
1173+
} else {
1174+
whiteAndIdentityList.addr_cnt = 0;
1175+
whiteAndIdentityList.identities_cnt = 0;
1176+
}
1177+
1178+
1179+
return apllyWhiteIdentityList(whiteAndIdentityList);
1180+
}
10661181
#endif

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5_SDK13/source/nRF5xGap.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ class nRF5xGap : public Gap
152152
#if (NRF_SD_BLE_API_VERSION >= 3)
153153
typedef struct
154154
{
155-
ble_gap_addr_t private_device[YOTTA_CFG_WHITELIST_MAX_SIZE + YOTTA_CFG_IRK_TABLE_MAX_SIZE];
156-
ble_gap_addr_t * whitelist[YOTTA_CFG_WHITELIST_MAX_SIZE + YOTTA_CFG_IRK_TABLE_MAX_SIZE];
157-
uint32_t num_of_whitelist_items;
158-
ble_gap_id_key_t identitie[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
159-
ble_gap_id_key_t * identities[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
160-
uint32_t num_of_identiti_items;
161-
} gapAdrHelper_t;
155+
ble_gap_addr_t addr[YOTTA_CFG_WHITELIST_MAX_SIZE];
156+
ble_gap_addr_t * pp_addr[YOTTA_CFG_WHITELIST_MAX_SIZE];
157+
uint32_t addr_cnt;
158+
159+
ble_gap_id_key_t identity[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
160+
ble_gap_id_key_t * pp_identities[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
161+
uint32_t identities_cnt;
162+
} GapWhiteAndIdentityList_t;
162163

163-
ble_error_t getStackWhiteIdentityList(gapAdrHelper_t &gapAdrHelper);
164-
ble_error_t apllyWhiteIdentityList(gapAdrHelper_t &gapAdrHelper);
164+
ble_error_t getStackWhiteIdentityList(GapWhiteAndIdentityList_t &whiteAndIdentityList);
165+
ble_error_t apllyWhiteIdentityList(GapWhiteAndIdentityList_t &whiteAndIdentityList);
166+
ble_error_t updateWhiteAndIdentityListInStack(void);
165167
#endif
166168

167169
private:

targets/TARGET_NORDIC/TARGET_NRF5_SDK13/TARGET_MCU_NRF52840/device/TOOLCHAIN_ARM_STD/nRF52832.sct

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ LR_IROM1 0x21000 0x00DF000 {
44
*(InRoot$$Sections)
55
.ANY (+RO)
66
}
7-
RW_IRAM0 0x20002EF8 UNINIT 0x000000F8 { ;no init section
7+
RW_IRAM0 0x20003288 UNINIT 0x000000F8 { ;no init section
88
*(noinit)
99
}
10-
RW_IRAM1 0x20002FF0 0x0003D010 {
10+
RW_IRAM1 0x20003380 0x0003cc80 {
1111
.ANY (+RW +ZI)
1212
}
1313
}

targets/TARGET_NORDIC/TARGET_NRF5_SDK13/TARGET_MCU_NRF52840/device/TOOLCHAIN_GCC_ARM/NRF52840.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
MEMORY
2020
{
2121
FLASH (rx) : ORIGIN = 0x21000, LENGTH = 0xDF000
22-
RAM (rwx) : ORIGIN = 0x20002ef8, LENGTH = 0x3d108
22+
RAM (rwx) : ORIGIN = 0x20003288, LENGTH = 0x3cd78
2323
}
2424

2525

targets/TARGET_NORDIC/TARGET_NRF5_SDK13/TARGET_MCU_NRF52840/device/TOOLCHAIN_IAR/nRF52832.icf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define symbol __ICFEDIT_intvec_start__ = 0x21000;
66
/*-Memory Regions-*/
77
define symbol __ICFEDIT_region_ROM_start__ = 0x21000;
88
define symbol __ICFEDIT_region_ROM_end__ = 0xfffff;
9-
define symbol __ICFEDIT_region_RAM_start__ = 0x20002ef8;
9+
define symbol __ICFEDIT_region_RAM_start__ = 0x20003288;
1010
define symbol __ICFEDIT_region_RAM_end__ = 0x2003ffff;
1111
export symbol __ICFEDIT_region_RAM_start__;
1212
export symbol __ICFEDIT_region_RAM_end__;

0 commit comments

Comments
 (0)