From 335951dc405f6baeeb8e96d19ec5adabf3f40bc5 Mon Sep 17 00:00:00 2001 From: Chris Andreae Date: Wed, 15 Feb 2023 14:10:21 +0900 Subject: [PATCH] Include the serial number in the Bluetooth device name --- app/boards/arm/glove80/Kconfig | 10 +++++++ app/boards/arm/glove80/glove80_lh_defconfig | 5 ++++ app/boards/arm/glove80/usb_serial_number.c | 31 +++++++++++++-------- app/src/ble.c | 31 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/app/boards/arm/glove80/Kconfig b/app/boards/arm/glove80/Kconfig index 204af94f6f8..8a78eee67c4 100644 --- a/app/boards/arm/glove80/Kconfig +++ b/app/boards/arm/glove80/Kconfig @@ -5,3 +5,13 @@ config BOARD_ENABLE_DCDC select SOC_DCDC_NRF52X default y depends on (BOARD_GLOVE80_LH || BOARD_GLOVE80_RH) + +config BT_DEVICE_NAME_APPEND_SN + bool "Append serial number to BT device name" + default n + depends on BT_DEVICE_NAME_DYNAMIC + +config BT_DEVICE_NAME_SN_BYTES + int "Number of bytes of serial number to append to the BT device name" + default 4 + depends on BT_DEVICE_NAME_APPEND_SN diff --git a/app/boards/arm/glove80/glove80_lh_defconfig b/app/boards/arm/glove80/glove80_lh_defconfig index 71a858c63c5..80717364f07 100644 --- a/app/boards/arm/glove80/glove80_lh_defconfig +++ b/app/boards/arm/glove80/glove80_lh_defconfig @@ -11,6 +11,11 @@ CONFIG_USB_DEVICE_VID=0x16c0 CONFIG_USB_DEVICE_MANUFACTURER="MoErgo" CONFIG_USB_DEVICE_SN="moergo.com:GLV80-0123456789ABCDEF" +CONFIG_BT_DEVICE_NAME="Glove80 " +CONFIG_BT_DEVICE_NAME_DYNAMIC=y +CONFIG_BT_DEVICE_NAME_APPEND_SN=y +CONFIG_BT_DEVICE_NAME_SN_BYTES=6 + CONFIG_BT_DIS_PNP_PID=0x27db CONFIG_BT_DIS_PNP_VID=0x16c0 CONFIG_BT_DIS_MANUF="MoErgo" diff --git a/app/boards/arm/glove80/usb_serial_number.c b/app/boards/arm/glove80/usb_serial_number.c index 44d7ee20363..6254cdc4d73 100644 --- a/app/boards/arm/glove80/usb_serial_number.c +++ b/app/boards/arm/glove80/usb_serial_number.c @@ -14,14 +14,15 @@ LOG_MODULE_DECLARE(usb_descriptor); int base16_encode(const uint8_t *data, int length, uint8_t *result, int bufSize); +void fill_serial_number(char *buf, int length); +/** + * nrf52840 hwinfo returns a 64-bit hardware id. Glove80 uses this as a + * serial number, encoded as base16 into the last 16 characters of the + * CONFIG_USB_DEVICE_SN template. If insufficient template space is + * available, instead return the static serial number string. + */ uint8_t *usb_update_sn_string_descriptor(void) { - /* - * nrf52840 hwinfo returns a 64-bit hardware id. Glove80 uses this as a - * serial number, encoded as base16 into the last 16 characters of the - * CONFIG_USB_DEVICE_SN template. If insufficient template space is - * available, instead return the static serial number string. - */ const uint8_t template_len = sizeof(CONFIG_USB_DEVICE_SN); const uint8_t sn_len = 16; @@ -33,17 +34,25 @@ uint8_t *usb_update_sn_string_descriptor(void) { static uint8_t serial[sizeof(CONFIG_USB_DEVICE_SN)]; strncpy(serial, CONFIG_USB_DEVICE_SN, template_len); + const uint8_t offset = template_len - sn_len - 1; + fill_serial_number(serial + offset, sn_len); + serial[template_len - 1] = '\0'; + + return serial; +} + +/** + * Writes the first `length` bytes of the device serial number into buf + */ +void fill_serial_number(char *buf, int length) { uint8_t hwid[8]; memset(hwid, 0, sizeof(hwid)); uint8_t hwlen = hwinfo_get_device_id(hwid, sizeof(hwid)); if (hwlen > 0) { - const uint8_t offset = template_len - sn_len - 1; - LOG_HEXDUMP_DBG(&hwid, sn_len, "Serial Number"); - base16_encode(hwid, hwlen, serial + offset, sn_len + 1); + LOG_HEXDUMP_DBG(&hwid, 16, "Serial Number"); + base16_encode(hwid, hwlen, buf, length); } - - return serial; } int base16_encode(const uint8_t *data, int length, uint8_t *result, int bufSize) { diff --git a/app/src/ble.c b/app/src/ble.c index f73976c87f9..6f330eafcf2 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -62,9 +62,31 @@ enum advertising_type { static struct zmk_ble_profile profiles[ZMK_BLE_PROFILE_COUNT]; static uint8_t active_profile; +#if IS_ENABLED(CONFIG_BT_DEVICE_NAME_APPEND_SN) + +static char bt_device_name[sizeof(CONFIG_BT_DEVICE_NAME) + CONFIG_BT_DEVICE_NAME_SN_BYTES]; + +void fill_serial_number(char *buf, int length); + +// configure the BT device name by appending a serial number prefix to +// CONFIG_BT_DEVICE_NAME +void init_bt_device_name() { + strncpy(bt_device_name, CONFIG_BT_DEVICE_NAME, sizeof(bt_device_name)); + fill_serial_number(bt_device_name + sizeof(CONFIG_BT_DEVICE_NAME) - 1, + CONFIG_BT_DEVICE_NAME_SN_BYTES); + bt_device_name[sizeof(bt_device_name) - 1] = '\0'; +} + +#define DEVICE_NAME bt_device_name +#define DEVICE_NAME_LEN (sizeof(bt_device_name) - 1) + +#else + #define DEVICE_NAME CONFIG_BT_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) +#endif + BUILD_ASSERT(DEVICE_NAME_LEN <= 16, "ERROR: BLE device name is too long. Max length: 16"); static const struct bt_data zmk_ble_ad[] = { @@ -626,6 +648,10 @@ static void zmk_ble_ready(int err) { } static int zmk_ble_init(const struct device *_arg) { +#if IS_ENABLED(CONFIG_BT_DEVICE_NAME_APPEND_SN) + init_bt_device_name(); +#endif + int err = bt_enable(NULL); if (err) { @@ -646,7 +672,12 @@ static int zmk_ble_init(const struct device *_arg) { settings_load_subtree("ble"); settings_load_subtree("bt"); +#endif +#if IS_ENABLED(CONFIG_BT_DEVICE_NAME_APPEND_SN) + if (strcmp(bt_get_name(), bt_device_name) != 0) { + bt_set_name(bt_device_name); + } #endif #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START)