Retrieve the BLE name from dotenv if available

Format:
CIRCUITPY_BLE_NAME = My BLE Board

- the length is limited to 31 characters
- for the NRF version it repeatedly truncates the name if it's too long
- the ESP version defaults to "nimble" if the name is too long
This commit is contained in:
Neradoc 2022-07-02 21:47:14 +02:00
parent 3515003b30
commit 50a9beda04
3 changed files with 76 additions and 19 deletions

View File

@ -49,6 +49,10 @@
#include "shared-bindings/_bleio/ScanEntry.h"
#include "shared-bindings/time/__init__.h"
#if CIRCUITPY_DOTENV
#include "shared-module/dotenv/__init__.h"
#endif
#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
#define SEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000000) / (RESOLUTION))
#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
// Get various values and limits set by the adapter.
// Set event mask.
STATIC void bleio_adapter_hci_init(bleio_adapter_obj_t *self) {
mp_int_t name_len = 0;
const size_t len = sizeof(default_ble_name);
#if CIRCUITPY_DOTENV
char ble_name[32];
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
if (name_len > 0) {
self->name = mp_obj_new_str(ble_name, (size_t)name_len);
}
#endif
bt_addr_t addr;
hci_check_error(hci_read_bd_addr(&addr));
if (name_len <= 0) {
name_len = sizeof(default_ble_name);
bt_addr_t addr;
hci_check_error(hci_read_bd_addr(&addr));
default_ble_name[len - 4] = nibble_to_hex_lower[addr.val[1] >> 4 & 0xf];
default_ble_name[len - 3] = nibble_to_hex_lower[addr.val[1] & 0xf];
default_ble_name[len - 2] = nibble_to_hex_lower[addr.val[0] >> 4 & 0xf];
default_ble_name[len - 1] = nibble_to_hex_lower[addr.val[0] & 0xf];
self->name = mp_obj_new_str(default_ble_name, len);
default_ble_name[name_len - 4] = nibble_to_hex_lower[addr.val[1] >> 4 & 0xf];
default_ble_name[name_len - 3] = nibble_to_hex_lower[addr.val[1] & 0xf];
default_ble_name[name_len - 2] = nibble_to_hex_lower[addr.val[0] >> 4 & 0xf];
default_ble_name[name_len - 1] = nibble_to_hex_lower[addr.val[0] & 0xf];
self->name = mp_obj_new_str(default_ble_name, (uint8_t)name_len);
}
// Get version information.
if (hci_read_local_version(&self->hci_version, &self->hci_revision, &self->lmp_version,

View File

@ -59,6 +59,10 @@
#include "esp_bt.h"
#include "esp_nimble_hci.h"
#if CIRCUITPY_DOTENV
#include "shared-module/dotenv/__init__.h"
#endif
bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
// static void bluetooth_adapter_background(void *data) {
@ -96,7 +100,20 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
// ble_hs_cfg.reset_cb = blecent_on_reset;
ble_hs_cfg.sync_cb = _on_sync;
// ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
#if CIRCUITPY_DOTENV
mp_int_t name_len = 0;
char ble_name[32];
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
if (name_len > 0) {
ble_name[name_len] = '\0';
ble_svc_gap_device_name_set(ble_name);
} else {
ble_svc_gap_device_name_set("CIRCUITPY");
}
#else
ble_svc_gap_device_name_set("CIRCUITPY");
#endif
// Clear all of the internal connection objects.
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {

View File

@ -52,6 +52,10 @@
#include "shared-bindings/_bleio/ScanEntry.h"
#include "shared-bindings/time/__init__.h"
#if CIRCUITPY_DOTENV
#include "shared-module/dotenv/__init__.h"
#endif
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
#define BLE_SLAVE_LATENCY 0
@ -329,18 +333,30 @@ STATIC void get_address(bleio_adapter_obj_t *self, ble_gap_addr_t *address) {
char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0, 0};
STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
uint8_t len = sizeof(default_ble_name) - 1;
ble_gap_addr_t local_address;
get_address(self, &local_address);
default_ble_name[len - 4] = nibble_to_hex_lower[local_address.addr[1] >> 4 & 0xf];
default_ble_name[len - 3] = nibble_to_hex_lower[local_address.addr[1] & 0xf];
default_ble_name[len - 2] = nibble_to_hex_lower[local_address.addr[0] >> 4 & 0xf];
default_ble_name[len - 1] = nibble_to_hex_lower[local_address.addr[0] & 0xf];
// setup the default name
ble_gap_addr_t addr; // local_address
get_address(self, &addr);
mp_int_t len = sizeof(default_ble_name) - 1;
default_ble_name[len - 4] = nibble_to_hex_lower[addr.addr[1] >> 4 & 0xf];
default_ble_name[len - 3] = nibble_to_hex_lower[addr.addr[1] & 0xf];
default_ble_name[len - 2] = nibble_to_hex_lower[addr.addr[0] >> 4 & 0xf];
default_ble_name[len - 1] = nibble_to_hex_lower[addr.addr[0] & 0xf];
default_ble_name[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
common_hal_bleio_adapter_set_name(self, (char *)default_ble_name);
mp_int_t name_len = 0;
#if CIRCUITPY_DOTENV
char ble_name[32];
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
if (name_len > 0) {
ble_name[name_len] = '\0';
common_hal_bleio_adapter_set_name(self, (char *)ble_name);
}
#endif
if (name_len <= 0) {
common_hal_bleio_adapter_set_name(self, (char *)default_ble_name);
}
}
static void bluetooth_adapter_background(void *data) {
@ -448,7 +464,17 @@ void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *na
ble_gap_conn_sec_mode_t sec;
sec.lv = 0;
sec.sm = 0;
sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, strlen(name));
int result;
result = sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, strlen(name));
for (int name_len = strlen(name); name_len > 0; --name_len) {
result = sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, name_len);
// expecting NRF_ERROR_DATA_SIZE when name too long
if (result == NRF_SUCCESS) {
return;
}
}
// default back to default if all fails
sd_ble_gap_device_name_set(&sec, (const uint8_t *)default_ble_name, sizeof(default_ble_name) - 1);
}
STATIC uint32_t _update_identities(bool is_central) {