@@ -158,7 +158,7 @@ bool addressEqual(const hal_ble_addr_t& srcAddr, const hal_ble_addr_t& destAddr)
158
158
return (srcAddr.addr_type == destAddr.addr_type && !memcmp (srcAddr.addr , destAddr.addr , BLE_SIG_ADDR_LEN));
159
159
}
160
160
161
- hal_ble_addr_t chipDefaultAddress () {
161
+ hal_ble_addr_t chipDefaultPublicAddress () {
162
162
hal_ble_addr_t localAddr = {};
163
163
hal_get_ble_mac_address (localAddr.addr , BLE_SIG_ADDR_LEN, nullptr );
164
164
localAddr.addr_type = BLE_SIG_ADDR_TYPE_PUBLIC;
@@ -924,23 +924,37 @@ int BleGap::getDeviceName(char* deviceName, size_t len) const {
924
924
}
925
925
926
926
int BleGap::setDeviceAddress (const hal_ble_addr_t * address) {
927
- hal_ble_addr_t newAddr = {};
928
- if (address == nullptr ) {
929
- // default to reserved MAC address for this platform
930
- newAddr = chipDefaultAddress ();
931
- } else {
932
- newAddr = *address;
927
+ CHECK_FALSE (isAdvertising (), SYSTEM_ERROR_INVALID_STATE);
928
+ CHECK_FALSE (scanning (), SYSTEM_ERROR_INVALID_STATE);
929
+ // RTL872x doesn't support changing the the public address.
930
+ // But to be compatible with existing BLE platforms, it should accept nulllptr.
931
+ if (!address) {
932
+ addr_ = chipDefaultPublicAddress ();
933
+ return SYSTEM_ERROR_NONE;
933
934
}
934
- if (newAddr. addr_type != BLE_SIG_ADDR_TYPE_PUBLIC && newAddr. addr_type != BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
935
+ if (address-> addr_type != BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
935
936
return SYSTEM_ERROR_INVALID_ARGUMENT;
936
937
}
937
- if (newAddr. addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC && (newAddr. addr [5 ] & 0xC0 ) != 0xC0 ) {
938
+ if ((address-> addr [5 ] & 0xC0 ) != 0xC0 ) {
938
939
// For random static address, the two most significant bits of the address shall be equal to 1.
939
940
return SYSTEM_ERROR_INVALID_ARGUMENT;
940
941
}
942
+ uint8_t addr[BLE_SIG_ADDR_LEN] = {};
943
+ memcpy (addr, address->addr , BLE_SIG_ADDR_LEN);
944
+ CHECK_RTL (le_cfg_local_identity_address (addr, GAP_IDENT_ADDR_RAND));
945
+ CHECK_RTL (le_set_gap_param (GAP_PARAM_RANDOM_ADDR, BLE_SIG_ADDR_LEN, addr));
946
+ addr_ = *address;
941
947
942
- CHECK_RTL (le_cfg_local_identity_address (newAddr.addr , (newAddr.addr_type == BLE_SIG_ADDR_TYPE_PUBLIC) ? GAP_IDENT_ADDR_PUBLIC : GAP_IDENT_ADDR_RAND));
943
- addr_ = newAddr;
948
+ uint8_t advLocalAddrType = GAP_LOCAL_ADDR_LE_PUBLIC;
949
+ if (address->addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
950
+ advLocalAddrType = GAP_LOCAL_ADDR_LE_RANDOM;
951
+ }
952
+ CHECK_RTL (le_adv_set_param (GAP_PARAM_ADV_LOCAL_ADDR_TYPE, sizeof (advLocalAddrType), &advLocalAddrType));
953
+ uint8_t scanLocalAddrType = GAP_LOCAL_ADDR_LE_PUBLIC;
954
+ if (address->addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
955
+ scanLocalAddrType = GAP_LOCAL_ADDR_LE_RANDOM;
956
+ }
957
+ CHECK_RTL (le_scan_set_param (GAP_PARAM_SCAN_LOCAL_ADDR_TYPE, sizeof (uint8_t ), &scanLocalAddrType));
944
958
945
959
return SYSTEM_ERROR_NONE;
946
960
}
0 commit comments