Skip to content

Commit 8dae92c

Browse files
author
Jamie C. Driver
committed
esp32s3: update BLE code to better support s3 controller model
1 parent 4e59c37 commit 8dae92c

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

main/ble/ble.c

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include <esp_system.h>
1717
#include <freertos/event_groups.h>
1818
#include <host/ble_hs.h>
19+
#ifdef CONFIG_IDF_TARGET_ESP32
1920
#include <host/ble_hs_pvcy.h>
21+
#endif
2022
#include <host/ble_store.h>
2123
#include <host/ble_uuid.h>
2224
#include <host/util/util.h>
@@ -278,22 +280,29 @@ static void ble_start_advertising(void)
278280
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
279281
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
280282

283+
JADE_LOGI("Advertised address type: %u", own_addr_type);
281284
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL);
282285
if (rc != 0) {
283286
JADE_LOGE("ble_gap_adv_start() failed with error %d", rc);
284287
}
285288

289+
#ifdef CONFIG_IDF_TARGET_ESP32
286290
// Log advertised address
291+
int isnrpa = 0;
287292
uint8_t addr_val[6] = { 0 };
288-
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
289-
JADE_LOGI("Advertising started, with address:");
293+
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, &isnrpa);
294+
if (rc) {
295+
JADE_LOGE("ble_hs_id_copy_addr(%u) failed with error: %d", own_addr_type, rc);
296+
}
297+
JADE_LOGI("Advertising started, (type %u, nrpa %d) with address:", own_addr_type, isnrpa);
290298
print_addr(addr_val);
291299

292300
// Refeed entropy - this is called whenever the advertisied address changes - ie.
293301
// when BLE enabled, and every minute or so all the time no client is connected.
294302
// Called again when the client disconnects. So frequent (if BLE enabled) but not
295303
// completely predictable ...
296304
refeed_entropy(addr_val, sizeof(addr_val));
305+
#endif
297306
}
298307

299308
static bool write_ble(const uint8_t* msg, const size_t towrite, void* ignore)
@@ -357,21 +366,22 @@ static void ble_on_reset(int reason) { JADE_LOGI("ble resetting state; reason=%d
357366

358367
static void ble_on_sync(void)
359368
{
369+
int rc;
370+
360371
// In a debug unattended ci build do not use RPA as it doesn't appear to
361372
// to work on the CI machine atm, but is preferred for android/ios apps.
362373
#ifdef CONFIG_DEBUG_UNATTENDED_CI
363-
int rc = ble_hs_util_ensure_addr(0);
374+
JADE_LOGI("ble sync() - Debug/CI mode using non-RPA fixed address");
375+
rc = ble_hs_util_ensure_addr(0);
364376
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_util_ensure_addr(0); rc=%d", rc);
365377

366378
// From the bleprph example main.c
367-
JADE_LOGI("ble sync() - Debug/CI mode using non-RPA fixed address");
368379
rc = ble_hs_id_infer_auto(0, &own_addr_type);
369380
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_id_infer_auto(0,...); rc=%d", rc);
370381
#else
371-
int rc = ble_hs_util_ensure_addr(1);
372-
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_util_ensure_addr(1); rc=%d", rc);
373-
#ifdef CONFIG_IDF_TARGET_ESP32
382+
JADE_LOGI("ble sync() - Using RPA address");
374383

384+
#ifdef CONFIG_IDF_TARGET_ESP32
375385
// From the bleprph example README (no actual example code provided):
376386
// For RPA feature (currently Host based privacy feature is supported), use API
377387
// `ble_hs_pvcy_rpa_config` to enable/disable host based privacy, `own_addr_type`
@@ -387,10 +397,33 @@ static void ble_on_sync(void)
387397
//
388398
// NOTE: There is also an attached patch at:
389399
// https://github.com/espressif/esp-nimble/issues/8#issuecomment-615130885
390-
JADE_LOGI("ble sync() - Using RPA address");
400+
rc = ble_hs_util_ensure_addr(1);
401+
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_util_ensure_addr(1); rc=%d", rc);
391402
own_addr_type = BLE_OWN_ADDR_RANDOM;
392403
rc = ble_hs_pvcy_rpa_config(1);
393404
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_pvcy_rpa_config(1); rc=%d", rc);
405+
#else
406+
// NOTE: need to generate and set a new address if using ble_hs_util_ensure_addr(1) or will receive
407+
// error BLE_HS_EROLE when trying to re-start advertising (ble_gap_adv_start() failed with error 530)
408+
// (appears fine on initial start but fails if ble stopped/restarted).
409+
ble_addr_t addr;
410+
411+
// generate new private address
412+
rc = ble_hs_id_gen_rnd(0, &addr);
413+
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_id_gen_rnd(0); rc=%d", rc);
414+
415+
// set generated address
416+
rc = ble_hs_id_set_rnd(addr.val);
417+
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_id_set_rnd(); rc=%d", rc);
418+
419+
// configure address, prefer random
420+
rc = ble_hs_util_ensure_addr(1);
421+
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_util_ensure_addr(0); rc=%d", rc);
422+
423+
// determine own_addr_type
424+
rc = ble_hs_id_infer_auto(1, &own_addr_type);
425+
JADE_ASSERT_MSG(rc == 0, "Error from ble_hs_id_infer_auto(1,...); rc=%d", rc);
426+
JADE_LOGI("Inferred address type: %u", own_addr_type);
394427
#endif // CONFIG_IDF_TARGET_ESP32
395428
#endif // CONFIG_DEBUG_UNATTENDED_CI
396429

@@ -483,8 +516,8 @@ void ble_start(void)
483516
// In a CI build do not set these as they don't appear to work on the CI
484517
// machine, but are necessary for RPA to work (as used in non-CI builds).
485518
#ifndef CONFIG_DEBUG_UNATTENDED_CI
486-
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
487-
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
519+
ble_hs_cfg.sm_our_key_dist = (BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID);
520+
ble_hs_cfg.sm_their_key_dist = (BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID);
488521
#endif
489522

490523
int rc = gatt_svr_init();

0 commit comments

Comments
 (0)