26
26
#include " headers/ble_hci.h"
27
27
#include " ble/pal/ConnectionEventMonitor.h"
28
28
#include " nRF5xPalSecurityManager.h"
29
+ #include < algorithm>
29
30
30
31
using ble::pal::vendor::nordic::nRF5xSecurityManager;
31
32
using ble::ArrayView;
32
33
using ble::pal::advertising_peer_address_type_t ;
34
+ using ble::peer_address_type_t ;
35
+
36
+ typedef BLEProtocol::AddressType LegacyAddressType;
37
+ typedef BLEProtocol::AddressType_t LegacyAddressType_t;
33
38
34
39
namespace {
35
40
@@ -85,29 +90,32 @@ bool is_advertising_non_connectable(const GapAdvertisingParams ¶ms) {
85
90
}
86
91
}
87
92
88
- bool is_identity_address (BLEProtocol::AddressType_t address_type) {
89
- switch (address_type) {
90
- case BLEProtocol::AddressType::PUBLIC_IDENTITY:
91
- case BLEProtocol::AddressType::RANDOM_STATIC_IDENTITY:
92
- return true ;
93
- default :
94
- return false ;
95
- }
93
+ bool is_identity_address (peer_address_type_t address_type) {
94
+ return address_type == peer_address_type_t ::PUBLIC_IDENTITY ||
95
+ address_type == peer_address_type_t ::RANDOM_STATIC_IDENTITY;
96
96
}
97
97
98
- BLEProtocol::AddressType_t convert_nordic_address (uint8_t address) {
99
- if (address == BLE_GAP_ADDR_TYPE_PUBLIC) {
100
- return BLEProtocol::AddressType::PUBLIC;
98
+ peer_address_type_t convert_nordic_address (bool identity, uint8_t address) {
99
+ if (identity) {
100
+ if (address == BLE_GAP_ADDR_TYPE_PUBLIC) {
101
+ return peer_address_type_t ::PUBLIC_IDENTITY;
102
+ } else {
103
+ return peer_address_type_t ::RANDOM_STATIC_IDENTITY;
104
+ }
101
105
} else {
102
- return BLEProtocol::AddressType::RANDOM;
106
+ if (address == BLE_GAP_ADDR_TYPE_PUBLIC) {
107
+ return peer_address_type_t ::PUBLIC;
108
+ } else {
109
+ return peer_address_type_t ::RANDOM;
110
+ }
103
111
}
104
112
}
105
113
106
- BLEProtocol::AddressType_t convert_identity_address (advertising_peer_address_type_t address) {
114
+ peer_address_type_t convert_identity_address (advertising_peer_address_type_t address) {
107
115
if (address == advertising_peer_address_type_t ::PUBLIC_ADDRESS) {
108
- return BLEProtocol::AddressType ::PUBLIC_IDENTITY;
116
+ return peer_address_type_t ::PUBLIC_IDENTITY;
109
117
} else {
110
- return BLEProtocol::AddressType ::RANDOM_STATIC_IDENTITY;
118
+ return peer_address_type_t ::RANDOM_STATIC_IDENTITY;
111
119
}
112
120
}
113
121
@@ -129,7 +137,7 @@ nRF5xGap::nRF5xGap() : Gap(),
129
137
_privacy_enabled(false ),
130
138
_peripheral_privacy_configuration(default_peripheral_privacy_configuration),
131
139
_central_privacy_configuration(default_central_privacy_configuration),
132
- _non_private_address_type(BLEProtocol::AddressType::RANDOM )
140
+ _non_private_address_type(LegacyAddressType::RANDOM_STATIC )
133
141
{
134
142
m_connectionHandle = BLE_CONN_HANDLE_INVALID;
135
143
}
@@ -453,11 +461,60 @@ ble_error_t nRF5xGap::stopAdvertising(void)
453
461
return BLE_ERROR_NONE;
454
462
}
455
463
456
- ble_error_t nRF5xGap::connect (const Address_t peerAddr,
457
- BLEProtocol::AddressType_t peerAddrType,
458
- const ConnectionParams_t *connectionParams,
459
- const GapScanningParams *scanParamsIn)
460
- {
464
+ ble_error_t nRF5xGap::connect (
465
+ const Address_t peerAddr,
466
+ peer_address_type_t peerAddrType,
467
+ const ConnectionParams_t *connectionParams,
468
+ const GapScanningParams *scanParamsIn
469
+ ) {
470
+ // NOTE: Nordic address type is an closer to LegacyAddressType: resolved
471
+ // address are treaded either as PUBLIC or RANDOM STATIC adresses.
472
+ // The idea is to get the conversion done here and call the legacy function.
473
+
474
+ LegacyAddressType_t legacy_address;
475
+
476
+ switch (peerAddrType.value ()) {
477
+ case peer_address_type_t ::PUBLIC:
478
+ case peer_address_type_t ::PUBLIC_IDENTITY:
479
+ legacy_address = LegacyAddressType::PUBLIC;
480
+ break ;
481
+ case peer_address_type_t ::RANDOM_STATIC_IDENTITY:
482
+ legacy_address = LegacyAddressType::RANDOM_STATIC;
483
+ break ;
484
+ case peer_address_type_t ::RANDOM: {
485
+ RandomAddressType_t random_address_type (RandomAddressType_t::STATIC);
486
+ ble_error_t err = getRandomAddressType (peerAddr, &random_address_type);
487
+ if (err) {
488
+ return err;
489
+ }
490
+ switch (random_address_type.value ()) {
491
+ case RandomAddressType_t::STATIC:
492
+ legacy_address = LegacyAddressType::RANDOM_STATIC;
493
+ break ;
494
+ case RandomAddressType_t::NON_RESOLVABLE_PRIVATE:
495
+ legacy_address = LegacyAddressType::RANDOM_PRIVATE_NON_RESOLVABLE;
496
+ break ;
497
+ case RandomAddressType_t::RESOLVABLE_PRIVATE:
498
+ legacy_address = LegacyAddressType::RANDOM_PRIVATE_RESOLVABLE;
499
+ break ;
500
+ default :
501
+ return BLE_ERROR_UNSPECIFIED;
502
+ }
503
+ } break ;
504
+ default :
505
+ return BLE_ERROR_INVALID_PARAM;
506
+ }
507
+
508
+ return connect (peerAddr, legacy_address, connectionParams, scanParamsIn);
509
+ }
510
+
511
+
512
+ ble_error_t nRF5xGap::connect (
513
+ const Address_t peerAddr,
514
+ LegacyAddressType_t peerAddrType,
515
+ const ConnectionParams_t *connectionParams,
516
+ const GapScanningParams *scanParamsIn
517
+ ) {
461
518
ble_gap_addr_t addr;
462
519
ble_gap_addr_t * addr_ptr = &addr;
463
520
addr.addr_type = peerAddrType;
@@ -722,11 +779,11 @@ uint16_t nRF5xGap::getConnectionHandle(void)
722
779
@endcode
723
780
*/
724
781
/* *************************************************************************/
725
- ble_error_t nRF5xGap::setAddress (AddressType_t type, const Address_t address)
782
+ ble_error_t nRF5xGap::setAddress (LegacyAddressType_t type, const Address_t address)
726
783
{
727
- using BLEProtocol::AddressType;
728
-
729
- if (type != AddressType::PUBLIC || type != AddressType::RANDOM_STATIC ) {
784
+ if (type != LegacyAddressType::PUBLIC &&
785
+ type != LegacyAddressType::RANDOM_STATIC
786
+ ) {
730
787
return BLE_ERROR_INVALID_PARAM;
731
788
}
732
789
@@ -736,7 +793,7 @@ ble_error_t nRF5xGap::setAddress(AddressType_t type, const Address_t address)
736
793
737
794
ble_gap_addr_t dev_addr;
738
795
memcpy (dev_addr.addr , address, ADDR_LEN);
739
- if (type == AddressType ::PUBLIC) {
796
+ if (type == LegacyAddressType ::PUBLIC) {
740
797
dev_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
741
798
} else {
742
799
dev_addr.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
@@ -752,8 +809,9 @@ ble_error_t nRF5xGap::setAddress(AddressType_t type, const Address_t address)
752
809
case NRF_SUCCESS:
753
810
return BLE_ERROR_NONE;
754
811
case NRF_ERROR_INVALID_ADDR:
755
- case BLE_ERROR_GAP_INVALID_BLE_ADDR:
756
812
return BLE_ERROR_INVALID_PARAM;
813
+ case BLE_ERROR_GAP_INVALID_BLE_ADDR:
814
+ return BLE_ERROR_PARAM_OUT_OF_RANGE;
757
815
case NRF_ERROR_BUSY:
758
816
return BLE_STACK_BUSY;
759
817
case NRF_ERROR_INVALID_STATE:
@@ -781,11 +839,11 @@ ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address)
781
839
782
840
switch (dev_addr.addr_type ) {
783
841
case BLE_GAP_ADDR_TYPE_PUBLIC:
784
- *typeP = BLEProtocol::AddressType ::PUBLIC;
842
+ *typeP = LegacyAddressType ::PUBLIC;
785
843
break ;
786
844
787
845
case BLE_GAP_ADDR_TYPE_RANDOM_STATIC:
788
- *typeP = BLEProtocol::AddressType ::RANDOM_STATIC;
846
+ *typeP = LegacyAddressType ::RANDOM_STATIC;
789
847
break ;
790
848
791
849
default :
@@ -924,7 +982,7 @@ ble_error_t nRF5xGap::getWhitelist(Gap::Whitelist_t &whitelistOut) const
924
982
uint32_t i;
925
983
for (i = 0 ; i < whitelistAddressesSize && i < whitelistOut.capacity ; ++i) {
926
984
memcpy ( &whitelistOut.addresses [i].address , &whitelistAddresses[i].addr , sizeof (whitelistOut.addresses [0 ].address ));
927
- whitelistOut.addresses [i].type = static_cast <BLEProtocol::AddressType_t > (whitelistAddresses[i].addr_type );
985
+ whitelistOut.addresses [i].type = static_cast <LegacyAddressType_t > (whitelistAddresses[i].addr_type );
928
986
929
987
930
988
}
@@ -971,7 +1029,9 @@ ble_error_t nRF5xGap::setWhitelist(const Gap::Whitelist_t &whitelistIn)
971
1029
972
1030
/* Test for invalid parameters before we change the internal state */
973
1031
for (uint32_t i = 0 ; i < whitelistIn.size ; ++i) {
974
- if (whitelistIn.addresses [i].type == BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE) {
1032
+ if (whitelistIn.addresses [i].type == LegacyAddressType::RANDOM_PRIVATE_NON_RESOLVABLE ||
1033
+ whitelistIn.addresses [i].type == LegacyAddressType::RANDOM_PRIVATE_RESOLVABLE
1034
+ ) {
975
1035
/* This is not allowed because it is completely meaningless */
976
1036
return BLE_ERROR_INVALID_PARAM;
977
1037
}
@@ -1260,15 +1320,13 @@ ble_error_t nRF5xGap::update_identities_list(bool resolution_enabled)
1260
1320
}
1261
1321
1262
1322
void nRF5xGap::on_connection (Gap::Handle_t handle, const ble_gap_evt_connected_t & evt) {
1263
- using BLEProtocol::AddressType;
1264
-
1265
1323
// set the new connection handle as the _default_ handle in gap
1266
1324
setConnectionHandle (handle);
1267
1325
1268
1326
// deal with own address
1269
- AddressType_t own_addr_type;
1270
- Address_t own_address;
1271
- const uint8_t * own_resolvable_address = NULL ;
1327
+ LegacyAddressType_t own_addr_type;
1328
+ ble:: address_t own_address;
1329
+ ble:: address_t own_resolvable_address;
1272
1330
1273
1331
#if (NRF_SD_BLE_API_VERSION <= 2)
1274
1332
if (evt.own_addr .addr_type == BLE_GAP_ADDR_TYPE_PUBLIC) {
@@ -1278,53 +1336,54 @@ void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t
1278
1336
}
1279
1337
memcpy (own_address, evt.own_addr .addr , sizeof (own_address));
1280
1338
#else
1281
- // FIXME: handle privacy ???
1282
- getAddress (&own_addr_type, own_address);
1339
+ getAddress (&own_addr_type, own_address.data ());
1283
1340
#endif
1284
1341
1285
1342
if (_privacy_enabled) {
1286
- own_resolvable_address = own_address;
1343
+ // swap own address with own resolvable address as when privacy is
1344
+ // enabled own_address is invalid and the address returned by getAddress
1345
+ // is the resolvable one.
1346
+ std::swap (own_address, own_resolvable_address);
1347
+ own_addr_type = LegacyAddressType::RANDOM_PRIVATE_RESOLVABLE;
1287
1348
}
1288
1349
1289
- // deal with the peer address: If privacy is enabled then the softdevice
1290
- // indicates if the address has been resolved or not. If the address has
1291
- // been resolved then the identity address should be passed to the application.
1292
- // Depending on the privacy chosen by the application, connection request
1293
- // from privacy enabled peers may trigger a disconnection, the pairing procedure
1294
- // or the authentication procedure.
1295
- AddressType_t peer_addr_type;
1296
- const uint8_t * peer_address;
1297
- const uint8_t * peer_resolvable_address;
1298
-
1299
1350
#if (NRF_SD_BLE_API_VERSION <= 2)
1300
1351
bool private_peer_known = evt.irk_match ;
1301
1352
#else
1302
1353
bool private_peer_known = evt.peer_addr .addr_id_peer ;
1303
1354
#endif
1304
1355
1305
-
1306
- if (private_peer_known) {
1307
- peer_addr_type = convert_nordic_address (evt.peer_addr .addr_type );;
1308
- peer_address = evt.peer_addr .addr ;
1309
- peer_resolvable_address = evt.peer_addr .addr ;
1310
- } else {
1311
- if (_privacy_enabled &&
1312
- evt.role == BLE_GAP_ROLE_PERIPH &&
1313
- _peripheral_privacy_configuration.resolution_strategy == PeripheralPrivacyConfiguration_t::REJECT_NON_RESOLVED_ADDRESS &&
1314
- evt.peer_addr .addr_type == BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE &&
1315
- get_sm ().get_resolving_list ().size () > 0
1316
- ) {
1317
- // FIXME: should use BLE_HCI_AUTHENTICATION_FAILURE; not possible
1318
- // with the softdevice ...
1319
- sd_ble_gap_disconnect (handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
1320
- return ;
1321
- }
1322
-
1323
- peer_addr_type = convert_nordic_address (evt.peer_addr .addr_type );
1324
- peer_address = evt.peer_addr .addr ;
1325
- peer_resolvable_address = NULL ;
1356
+ // Filter out private address non resolved if the its required by the
1357
+ // resolution policy
1358
+ if (_privacy_enabled &&
1359
+ evt.role == BLE_GAP_ROLE_PERIPH &&
1360
+ _peripheral_privacy_configuration.resolution_strategy == PeripheralPrivacyConfiguration_t::REJECT_NON_RESOLVED_ADDRESS &&
1361
+ evt.peer_addr .addr_type == BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE &&
1362
+ private_peer_known == false &&
1363
+ get_sm ().get_resolving_list ().size () > 0
1364
+ ) {
1365
+ // FIXME: should use BLE_HCI_AUTHENTICATION_FAILURE; not possible
1366
+ // with the softdevice ...
1367
+ sd_ble_gap_disconnect (handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
1368
+ return ;
1326
1369
}
1327
1370
1371
+ // deal with the peer address: If privacy is enabled then the softdevice
1372
+ // indicates if the address has been resolved or not. If the address has
1373
+ // been resolved then the identity address should be passed to the application.
1374
+ // Depending on the privacy chosen by the application, connection request
1375
+ // from privacy enabled peers may trigger a disconnection, the pairing procedure
1376
+ // or the authentication procedure.
1377
+ peer_address_type_t peer_addr_type = convert_nordic_address (
1378
+ private_peer_known,
1379
+ evt.peer_addr .addr_type
1380
+ );
1381
+ // NOTE: when privacy is enabled, the only address returned is the resolved
1382
+ // address; set peer and resolved address to the same value in such case.
1383
+ const uint8_t * peer_address = evt.peer_addr .addr ;
1384
+ const uint8_t * peer_resolvable_address =
1385
+ private_peer_known ? peer_address : NULL ;
1386
+
1328
1387
// notify internal event handler before applying the resolution strategy
1329
1388
if (_connection_event_handler) {
1330
1389
_connection_event_handler->on_connected (
@@ -1333,14 +1392,15 @@ void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t
1333
1392
peer_addr_type,
1334
1393
peer_address,
1335
1394
own_addr_type,
1336
- own_address,
1337
- reinterpret_cast <const ConnectionParams_t *>(&(evt.conn_params ))
1395
+ own_address.data (),
1396
+ reinterpret_cast <const ConnectionParams_t *>(&(evt.conn_params )),
1397
+ peer_resolvable_address
1338
1398
);
1339
1399
}
1340
1400
1341
1401
// Apply authentication strategy before application notification
1342
- if (!private_peer_known &&
1343
- _privacy_enabled &&
1402
+ if (_privacy_enabled &&
1403
+ !private_peer_known &&
1344
1404
evt.role == BLE_GAP_ROLE_PERIPH &&
1345
1405
evt.peer_addr .addr_type == BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE
1346
1406
) {
@@ -1364,24 +1424,27 @@ void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t
1364
1424
peer_addr_type,
1365
1425
peer_address,
1366
1426
own_addr_type,
1367
- own_address,
1427
+ own_address. data () ,
1368
1428
reinterpret_cast <const ConnectionParams_t *>(&(evt.conn_params )),
1369
1429
peer_resolvable_address,
1370
- own_resolvable_address
1430
+ own_resolvable_address. data ()
1371
1431
);
1372
1432
}
1373
1433
1374
1434
void nRF5xGap::on_advertising_packet (const ble_gap_evt_adv_report_t &evt) {
1375
- using BLEProtocol::AddressType ;
1435
+ bool peer_address_resolved = evt. peer_addr . addr_id_peer ;
1376
1436
1377
1437
if (_privacy_enabled &&
1378
- evt. peer_addr . addr_id_peer == 0 &&
1438
+ peer_address_resolved == false &&
1379
1439
_central_privacy_configuration.resolution_strategy == CentralPrivacyConfiguration_t::RESOLVE_AND_FILTER
1380
1440
) {
1381
1441
return ;
1382
1442
}
1383
1443
1384
- AddressType_t peer_addr_type = convert_nordic_address (evt.peer_addr .addr_type );
1444
+ peer_address_type_t peer_addr_type = convert_nordic_address (
1445
+ evt.peer_addr .addr_id_peer ,
1446
+ evt.peer_addr .addr_type
1447
+ );
1385
1448
const uint8_t * peer_address = evt.peer_addr .addr ;
1386
1449
1387
1450
processAdvertisementReport (
0 commit comments