3434#include < lib/support/CodeUtils.h>
3535#include < lib/support/logging/CHIPLogging.h>
3636#include < platform/CommissionableDataProvider.h>
37+ #include < platform/DeviceInstanceInfoProvider.h>
3738#include < platform/internal/BLEManager.h>
3839#include < setup_payload/AdditionalDataPayloadGenerator.h>
3940#include < system/SystemTimer.h>
@@ -107,6 +108,10 @@ const ble_uuid128_t UUID_CHIPoBLEChar_C3 = {
107108
108109SemaphoreHandle_t semaphoreHandle = NULL ;
109110
111+ // LE Random Device Address
112+ // (see Bluetooth® Core Specification 4.2 Vol 6, Part B, Section 1.3.2.1 "Static device address")
113+ uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;
114+
110115} // unnamed namespace
111116
112117BLEManagerImpl BLEManagerImpl::sInstance ;
@@ -126,7 +131,7 @@ const struct ble_gatt_svc_def BLEManagerImpl::CHIPoBLEGATTAttrs[] = {
126131 {
127132 .uuid = (ble_uuid_t *) (&UUID_CHIPoBLEChar_TX),
128133 .access_cb = gatt_svr_chr_access,
129- .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
134+ .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE ,
130135 .val_handle = &sInstance .mTXCharCCCDAttrHandle ,
131136 },
132137#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
@@ -295,7 +300,6 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
295300
296301 case DeviceEventType::kServiceProvisioningChange :
297302 case DeviceEventType::kWiFiConnectivityChange :
298-
299303 // If CHIPOBLE_DISABLE_ADVERTISING_WHEN_PROVISIONED is enabled, and there is a change to the
300304 // device's provisioning state, then automatically disable CHIPoBLE advertising if the device
301305 // is now fully provisioned.
@@ -501,7 +505,7 @@ void BLEManagerImpl::DriveBLEState(void)
501505
502506 // Add delay of 500msec while NimBLE host task gets up and running
503507 {
504- vTaskDelay (500 / portTICK_RATE_MS );
508+ vTaskDelay (500 / portTICK_PERIOD_MS );
505509 }
506510 }
507511
@@ -624,6 +628,34 @@ void BLEManagerImpl::bleprph_on_reset(int reason)
624628 ESP_LOGE (TAG, " Resetting state; reason=%d\n " , reason);
625629}
626630
631+ CHIP_ERROR BLEManagerImpl::bleprph_set_random_addr (void )
632+ {
633+ ble_addr_t addr;
634+
635+ // Generates a new static random address
636+ int rc = ble_hs_id_gen_rnd (0 , &addr);
637+ if (rc != 0 )
638+ {
639+ ESP_LOGE (TAG, " Failed to generate random address err: %d" , rc);
640+ return CHIP_ERROR_INTERNAL;
641+ }
642+ // Set generated address
643+ rc = ble_hs_id_set_rnd (addr.val );
644+ if (rc != 0 )
645+ {
646+ ESP_LOGE (TAG, " Failed to set random address err: %d" , rc);
647+ return CHIP_ERROR_INTERNAL;
648+ }
649+ // Try to configure the device with random static address
650+ rc = ble_hs_util_ensure_addr (1 );
651+ if (rc != 0 )
652+ {
653+ ESP_LOGE (TAG, " Failed to configure random address err: %d" , rc);
654+ return CHIP_ERROR_INTERNAL;
655+ }
656+ return CHIP_NO_ERROR;
657+ }
658+
627659void BLEManagerImpl::bleprph_on_sync (void )
628660{
629661 ESP_LOGI (TAG, " BLE host-controller synced" );
@@ -705,6 +737,7 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)
705737 sInstance .mFlags .Set (Flags::kESPBLELayerInitialized );
706738 sInstance .mFlags .Set (Flags::kGATTServiceStarted );
707739
740+ err = bleprph_set_random_addr ();
708741exit:
709742 return err;
710743}
@@ -1110,7 +1143,7 @@ void BLEManagerImpl::HandleC3CharRead(struct ble_gatt_char_context * param)
11101143 uint8_t rotatingDeviceIdUniqueId[ConfigurationManager::kRotatingDeviceIDUniqueIDLength ] = {};
11111144 MutableByteSpan rotatingDeviceIdUniqueIdSpan (rotatingDeviceIdUniqueId);
11121145
1113- err = ConfigurationMgr (). GetRotatingDeviceIdUniqueId (rotatingDeviceIdUniqueIdSpan);
1146+ err = DeviceLayer::GetDeviceInstanceInfoProvider ()-> GetRotatingDeviceIdUniqueId (rotatingDeviceIdUniqueIdSpan);
11141147 SuccessOrExit (err);
11151148 err = ConfigurationMgr ().GetLifetimeCounter (additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter );
11161149 SuccessOrExit (err);
@@ -1212,11 +1245,6 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
12121245 CHIP_ERROR err;
12131246 ble_gap_adv_params adv_params;
12141247 memset (&adv_params, 0 , sizeof (adv_params));
1215- #ifdef CONFIG_BT_NIMBLE_HOST_BASED_PRIVACY
1216- uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;
1217- #else
1218- uint8_t own_addr_type = BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT;
1219- #endif
12201248 adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
12211249
12221250 mFlags .Clear (Flags::kAdvertisingRefreshNeeded );
0 commit comments