Skip to content

Commit 145cce9

Browse files
authored
Merge pull request #6596 from Neradoc/ble-name-in-dotenv
Setup default BLE name in dotenv
2 parents 02c804c + 44a91c1 commit 145cce9

File tree

5 files changed

+87
-21
lines changed

5 files changed

+87
-21
lines changed

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
#include "shared-bindings/_bleio/ScanEntry.h"
5050
#include "shared-bindings/time/__init__.h"
5151

52+
#if CIRCUITPY_DOTENV
53+
#include "shared-module/dotenv/__init__.h"
54+
#endif
55+
5256
#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
5357
#define SEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000000) / (RESOLUTION))
5458
#define UNITS_TO_SEC(TIME, RESOLUTION) (((TIME)*(RESOLUTION)) / 1000000)
@@ -278,17 +282,27 @@ char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0
278282
// Get various values and limits set by the adapter.
279283
// Set event mask.
280284
STATIC void bleio_adapter_hci_init(bleio_adapter_obj_t *self) {
285+
mp_int_t name_len = 0;
281286

282-
const size_t len = sizeof(default_ble_name);
283-
284-
bt_addr_t addr;
285-
hci_check_error(hci_read_bd_addr(&addr));
286-
287-
default_ble_name[len - 4] = nibble_to_hex_lower[addr.val[1] >> 4 & 0xf];
288-
default_ble_name[len - 3] = nibble_to_hex_lower[addr.val[1] & 0xf];
289-
default_ble_name[len - 2] = nibble_to_hex_lower[addr.val[0] >> 4 & 0xf];
290-
default_ble_name[len - 1] = nibble_to_hex_lower[addr.val[0] & 0xf];
291-
self->name = mp_obj_new_str(default_ble_name, len);
287+
#if CIRCUITPY_DOTENV
288+
char ble_name[32];
289+
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
290+
if (name_len > 0) {
291+
self->name = mp_obj_new_str(ble_name, (size_t)name_len);
292+
}
293+
#endif
294+
295+
if (name_len <= 0) {
296+
name_len = sizeof(default_ble_name);
297+
bt_addr_t addr;
298+
hci_check_error(hci_read_bd_addr(&addr));
299+
300+
default_ble_name[name_len - 4] = nibble_to_hex_lower[addr.val[1] >> 4 & 0xf];
301+
default_ble_name[name_len - 3] = nibble_to_hex_lower[addr.val[1] & 0xf];
302+
default_ble_name[name_len - 2] = nibble_to_hex_lower[addr.val[0] >> 4 & 0xf];
303+
default_ble_name[name_len - 1] = nibble_to_hex_lower[addr.val[0] & 0xf];
304+
self->name = mp_obj_new_str(default_ble_name, (uint8_t)name_len);
305+
}
292306

293307
// Get version information.
294308
if (hci_read_local_version(&self->hci_version, &self->hci_revision, &self->lmp_version,

docs/environment.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ CircuitPython behavior
3131
CircuitPython will also read the environment to configure its behavior. Other
3232
keys are ignored by CircuitPython. Here are the keys it uses:
3333

34+
CIRCUITPY_BLE_NAME
35+
~~~~~~~~~~~~~~~~~~
36+
Default BLE name the board advertises as, including for the BLE workflow.
37+
38+
CIRCUITPY_WEB_API_PASSWORD
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
Password required to make modifications to the board from the Web Workflow.
41+
3442
CIRCUITPY_WIFI_PASSWORD
3543
~~~~~~~~~~~~~~~~~~~~~~~
36-
Wi-Fi password used to auto connect to CIRCUITPY_WIFI_SSID
44+
Wi-Fi password used to auto connect to CIRCUITPY_WIFI_SSID.
3745

3846
CIRCUITPY_WIFI_SSID
3947
~~~~~~~~~~~~~~~~~~~

docs/workflows.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ using a rotating key rather than a static one. Non-bonded devices won't be able
4545
connection, the central device can discover two default services. One for file transfer and one for
4646
CircuitPython specifically that includes serial characteristics.
4747

48+
To change the default BLE advertising name without (or before) running user code, the desired name
49+
can be put in the `/.env` file. The key is `CIRCUITPY_BLE_NAME`. It's limited to approximately
50+
30 characters depending on the port's settings and will be truncated if longer.
51+
4852
### File Transfer API
4953

5054
CircuitPython uses [an open File Transfer API](https://github.com/adafruit/Adafruit_CircuitPython_BLE_File_Transfer)

ports/espressif/common-hal/_bleio/Adapter.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
#include "esp_bt.h"
6060
#include "esp_nimble_hci.h"
6161

62+
#if CIRCUITPY_DOTENV
63+
#include "shared-module/dotenv/__init__.h"
64+
#endif
65+
6266
bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
6367

6468
// static void bluetooth_adapter_background(void *data) {
@@ -96,7 +100,23 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
96100
// ble_hs_cfg.reset_cb = blecent_on_reset;
97101
ble_hs_cfg.sync_cb = _on_sync;
98102
// ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
103+
104+
#if CIRCUITPY_DOTENV
105+
mp_int_t name_len = 0;
106+
char ble_name[32];
107+
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
108+
if (name_len > 0) {
109+
if (name_len > MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH) {
110+
name_len = MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH;
111+
}
112+
ble_name[name_len] = '\0';
113+
ble_svc_gap_device_name_set(ble_name);
114+
} else {
115+
ble_svc_gap_device_name_set("CIRCUITPY");
116+
}
117+
#else
99118
ble_svc_gap_device_name_set("CIRCUITPY");
119+
#endif
100120

101121
// Clear all of the internal connection objects.
102122
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#include "shared-bindings/_bleio/ScanEntry.h"
5353
#include "shared-bindings/time/__init__.h"
5454

55+
#if CIRCUITPY_DOTENV
56+
#include "shared-module/dotenv/__init__.h"
57+
#endif
58+
5559
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
5660
#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
5761
#define BLE_SLAVE_LATENCY 0
@@ -329,18 +333,30 @@ STATIC void get_address(bleio_adapter_obj_t *self, ble_gap_addr_t *address) {
329333
char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0, 0};
330334

331335
STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
332-
uint8_t len = sizeof(default_ble_name) - 1;
336+
// setup the default name
337+
ble_gap_addr_t addr; // local_address
338+
get_address(self, &addr);
339+
mp_int_t len = sizeof(default_ble_name) - 1;
340+
default_ble_name[len - 4] = nibble_to_hex_lower[addr.addr[1] >> 4 & 0xf];
341+
default_ble_name[len - 3] = nibble_to_hex_lower[addr.addr[1] & 0xf];
342+
default_ble_name[len - 2] = nibble_to_hex_lower[addr.addr[0] >> 4 & 0xf];
343+
default_ble_name[len - 1] = nibble_to_hex_lower[addr.addr[0] & 0xf];
344+
default_ble_name[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
333345

334-
ble_gap_addr_t local_address;
335-
get_address(self, &local_address);
346+
mp_int_t name_len = 0;
336347

337-
default_ble_name[len - 4] = nibble_to_hex_lower[local_address.addr[1] >> 4 & 0xf];
338-
default_ble_name[len - 3] = nibble_to_hex_lower[local_address.addr[1] & 0xf];
339-
default_ble_name[len - 2] = nibble_to_hex_lower[local_address.addr[0] >> 4 & 0xf];
340-
default_ble_name[len - 1] = nibble_to_hex_lower[local_address.addr[0] & 0xf];
341-
default_ble_name[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
348+
#if CIRCUITPY_DOTENV
349+
char ble_name[32];
350+
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
351+
if (name_len > 0) {
352+
ble_name[name_len] = '\0';
353+
common_hal_bleio_adapter_set_name(self, (char *)ble_name);
354+
}
355+
#endif
342356

343-
common_hal_bleio_adapter_set_name(self, (char *)default_ble_name);
357+
if (name_len <= 0) {
358+
common_hal_bleio_adapter_set_name(self, (char *)default_ble_name);
359+
}
344360
}
345361

346362
static void bluetooth_adapter_background(void *data) {
@@ -448,7 +464,11 @@ void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *na
448464
ble_gap_conn_sec_mode_t sec;
449465
sec.lv = 0;
450466
sec.sm = 0;
451-
sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, strlen(name));
467+
uint16_t len = strlen(name);
468+
if (len > BLE_GAP_DEVNAME_MAX_LEN) {
469+
len = BLE_GAP_DEVNAME_MAX_LEN;
470+
}
471+
sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, len);
452472
}
453473

454474
STATIC uint32_t _update_identities(bool is_central) {

0 commit comments

Comments
 (0)