15
15
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
17
18
- #undef LOG_COMPILE_TIME_LEVEL
18
+ #define LOG_COMPILE_TIME_LEVEL LOG_LEVEL_TRACE
19
19
#include " logging.h"
20
20
LOG_SOURCE_CATEGORY (" hal.ble" );
21
21
22
22
#include " ble_hal.h"
23
23
24
24
#if HAL_PLATFORM_BLE
25
25
26
- #ifdef __cplusplus
27
26
extern " C" {
28
- #endif
29
27
#include " rtl8721d.h"
30
- #ifdef __cplusplus
31
- } // extern "C"
32
- #endif
33
-
34
- extern " C" {
28
+ #include " rtl8721d_efuse.h"
35
29
#include " rtk_coex.h"
36
30
}
37
31
@@ -43,6 +37,7 @@ extern "C" {
43
37
#include < gap_adv.h>
44
38
#include < gap_scan.h>
45
39
#include < gap_bond_le.h>
40
+ #include < gap_le.h>
46
41
#include < gap_conn_le.h>
47
42
#include < profile_server.h>
48
43
#include < gatt_builtin_services.h>
@@ -57,7 +52,6 @@ extern "C" {
57
52
#include < wifi/wifi_conf.h>
58
53
#include " rtk_coex.h"
59
54
#include " ftl_int.h"
60
- #include " rtl8721d.h"
61
55
#include " bt_intf.h"
62
56
63
57
// FIXME
@@ -164,6 +158,19 @@ bool addressEqual(const hal_ble_addr_t& srcAddr, const hal_ble_addr_t& destAddr)
164
158
return (srcAddr.addr_type == destAddr.addr_type && !memcmp (srcAddr.addr , destAddr.addr , BLE_SIG_ADDR_LEN));
165
159
}
166
160
161
+ hal_ble_addr_t chipDefaultPublicAddress () {
162
+ hal_ble_addr_t localAddr = {};
163
+ uint8_t mac[BLE_SIG_ADDR_LEN] = {};
164
+ if (hal_get_mac_address (HAL_DEVICE_MAC_BLE, mac, BLE_SIG_ADDR_LEN, nullptr ) == BLE_SIG_ADDR_LEN) {
165
+ // As per BLE spec, we store BLE data in little-endian
166
+ for (uint8_t i = 0 , j = BLE_SIG_ADDR_LEN - 1 ; i < BLE_SIG_ADDR_LEN; i++, j--) {
167
+ localAddr.addr [i] = mac[j];
168
+ }
169
+ localAddr.addr_type = BLE_SIG_ADDR_TYPE_PUBLIC;
170
+ }
171
+ return localAddr;
172
+ }
173
+
167
174
} // anonymous namespace
168
175
169
176
@@ -329,6 +336,8 @@ class BleGap {
329
336
int setAppearance (uint16_t appearance) const ;
330
337
int setDeviceName (const char * deviceName, size_t len);
331
338
int getDeviceName (char * deviceName, size_t len) const ;
339
+ int setDeviceAddress (const hal_ble_addr_t * address);
340
+ int getDeviceAddress (hal_ble_addr_t * address) const ;
332
341
333
342
int setAdvertisingParameters (const hal_ble_adv_params_t * params);
334
343
int getAdvertisingParameters (hal_ble_adv_params_t * params) const ;
@@ -391,6 +400,7 @@ class BleGap {
391
400
: initialized_(false ),
392
401
btStackStarted_ (false ),
393
402
state_{},
403
+ addr_{},
394
404
advParams_{},
395
405
advTimeoutTimer_ (nullptr ),
396
406
isScanning_ (false ),
@@ -483,6 +493,7 @@ class BleGap {
483
493
bool initialized_;
484
494
bool btStackStarted_;
485
495
volatile RtlGapDevState state_; /* *< This should be atomically r/w as the struct is <= uint32_t */
496
+ hal_ble_addr_t addr_;
486
497
hal_ble_adv_params_t advParams_;
487
498
os_timer_t advTimeoutTimer_; /* *< Timer for advertising timeout. */
488
499
volatile bool isScanning_; /* *< If it is scanning or not. */
@@ -793,6 +804,14 @@ int BleGap::init() {
793
804
CHECK (setDeviceName (devName_, devNameLen_));
794
805
CHECK (setAdvertisingParameters (&advParams_));
795
806
CHECK (setScanParams (&scanParams_));
807
+
808
+ constexpr uint8_t zeros[BLE_SIG_ADDR_LEN] = {0 ,0 ,0 ,0 ,0 ,0 };
809
+ if (!memcmp (addr_.addr , zeros, BLE_SIG_ADDR_LEN)) {
810
+ CHECK (setDeviceAddress (nullptr ));
811
+ } else {
812
+ CHECK (setDeviceAddress (&addr_));
813
+ }
814
+
796
815
/* register gap message callback */
797
816
le_register_app_cb (gapEventCallback);
798
817
@@ -910,6 +929,48 @@ int BleGap::getDeviceName(char* deviceName, size_t len) const {
910
929
return SYSTEM_ERROR_NONE;
911
930
}
912
931
932
+ int BleGap::setDeviceAddress (const hal_ble_addr_t * address) {
933
+ CHECK_FALSE (isAdvertising (), SYSTEM_ERROR_INVALID_STATE);
934
+ CHECK_FALSE (scanning (), SYSTEM_ERROR_INVALID_STATE);
935
+ // RTL872x doesn't support changing the the public address.
936
+ // But to be compatible with existing BLE platforms, it should accept nulllptr.
937
+ if (!address) {
938
+ addr_ = chipDefaultPublicAddress ();
939
+ return SYSTEM_ERROR_NONE;
940
+ }
941
+ if (address->addr_type != BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
942
+ return SYSTEM_ERROR_INVALID_ARGUMENT;
943
+ }
944
+ if ((address->addr [5 ] & 0xC0 ) != 0xC0 ) {
945
+ // For random static address, the two most significant bits of the address shall be equal to 1.
946
+ return SYSTEM_ERROR_INVALID_ARGUMENT;
947
+ }
948
+ uint8_t addr[BLE_SIG_ADDR_LEN] = {};
949
+ memcpy (addr, address->addr , BLE_SIG_ADDR_LEN);
950
+ CHECK_RTL (le_cfg_local_identity_address (addr, GAP_IDENT_ADDR_RAND));
951
+ CHECK_RTL (le_set_gap_param (GAP_PARAM_RANDOM_ADDR, BLE_SIG_ADDR_LEN, addr));
952
+ addr_ = *address;
953
+
954
+ uint8_t advLocalAddrType = GAP_LOCAL_ADDR_LE_PUBLIC;
955
+ if (address->addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
956
+ advLocalAddrType = GAP_LOCAL_ADDR_LE_RANDOM;
957
+ }
958
+ CHECK_RTL (le_adv_set_param (GAP_PARAM_ADV_LOCAL_ADDR_TYPE, sizeof (advLocalAddrType), &advLocalAddrType));
959
+ uint8_t scanLocalAddrType = GAP_LOCAL_ADDR_LE_PUBLIC;
960
+ if (address->addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
961
+ scanLocalAddrType = GAP_LOCAL_ADDR_LE_RANDOM;
962
+ }
963
+ CHECK_RTL (le_scan_set_param (GAP_PARAM_SCAN_LOCAL_ADDR_TYPE, sizeof (uint8_t ), &scanLocalAddrType));
964
+
965
+ return SYSTEM_ERROR_NONE;
966
+ }
967
+
968
+ int BleGap::getDeviceAddress (hal_ble_addr_t * address) const {
969
+ CHECK_TRUE (address, SYSTEM_ERROR_INVALID_ARGUMENT);
970
+ *address = addr_;
971
+ return SYSTEM_ERROR_NONE;
972
+ }
973
+
913
974
int BleGap::setAdvertisingParameters (const hal_ble_adv_params_t * params) {
914
975
hal_ble_adv_params_t tempParams = {};
915
976
tempParams.version = BLE_API_VERSION;
@@ -2057,13 +2118,15 @@ int hal_ble_set_callback_on_periph_link_events(hal_ble_on_link_evt_cb_t callback
2057
2118
int hal_ble_gap_set_device_address (const hal_ble_addr_t * address, void * reserved) {
2058
2119
BleLock lk;
2059
2120
LOG_DEBUG (TRACE, " hal_ble_gap_set_device_address()." );
2060
- return SYSTEM_ERROR_NOT_SUPPORTED;
2121
+ CHECK_TRUE (BleGap::getInstance ().initialized (), SYSTEM_ERROR_INVALID_STATE);
2122
+ return BleGap::getInstance ().setDeviceAddress (address);
2061
2123
}
2062
2124
2063
2125
int hal_ble_gap_get_device_address (hal_ble_addr_t * address, void * reserved) {
2064
2126
BleLock lk;
2065
2127
LOG_DEBUG (TRACE, " hal_ble_gap_get_device_address()." );
2066
- return SYSTEM_ERROR_NOT_SUPPORTED;
2128
+ CHECK_TRUE (BleGap::getInstance ().initialized (), SYSTEM_ERROR_INVALID_STATE);
2129
+ return BleGap::getInstance ().getDeviceAddress (address);
2067
2130
}
2068
2131
2069
2132
int hal_ble_gap_set_device_name (const char * device_name, size_t len, void * reserved) {
0 commit comments