Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append a serial number prefix to the BT device name #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/boards/arm/glove80/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions app/boards/arm/glove80/glove80_lh_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 20 additions & 11 deletions app/boards/arm/glove80/usb_serial_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Loading