aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeon Rinkel <[email protected]>2023-10-28 16:05:16 +0200
committerLeon Rinkel <[email protected]>2023-10-28 16:05:16 +0200
commitb84f9691faf564e112481698c0bf02860b8b9359 (patch)
treeacae5df51848a71b084f7c569e222eeaf7c58457
parentd75c0a23b028ccf2b5a06ce71cee140b6a292fec (diff)
Unique device name from hardware id
-rw-r--r--app/prj.conf2
-rw-r--r--app/src/main.c48
2 files changed, 50 insertions, 0 deletions
diff --git a/app/prj.conf b/app/prj.conf
index 3d4bda5..cb8cd82 100644
--- a/app/prj.conf
+++ b/app/prj.conf
@@ -15,6 +15,8 @@ CONFIG_BT_DEVICE_NAME="lumen"
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
CONFIG_BT_DEVICE_NAME_MAX=64
+CONFIG_HWINFO=y
+
# This option enables the MCUmgr support, which is required for the DFU process.
CONFIG_MCUMGR=y
# This option enables the MCUmgr image management handlers, which are required
diff --git a/app/src/main.c b/app/src/main.c
index cf74cad..ca9eaca 100644
--- a/app/src/main.c
+++ b/app/src/main.c
@@ -8,6 +8,7 @@
#include <zephyr/sys_clock.h>
#include <zephyr/device.h>
#include <zephyr/drivers/led_strip.h>
+#include <zephyr/drivers/hwinfo.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
@@ -179,6 +180,46 @@ void color_wheel(uint8_t pos, uint8_t* r, uint8_t* g, uint8_t* b)
}
}
+static int set_adv_name(void)
+{
+ int i;
+
+ const ssize_t device_id_len = 8;
+ uint8_t device_id[device_id_len];
+ ssize_t actual_device_id_len;
+
+ const char id_dict[] = \
+ "abcdefghijklmnopqrstuvwxyz" \
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+ "0123456789";
+ const size_t id_dict_len = strlen(id_dict);
+
+ char adv_name[CONFIG_BT_DEVICE_NAME_MAX + 1] = "Lumen ";
+ size_t adv_name_len = strlen(adv_name);
+
+ actual_device_id_len =
+ hwinfo_get_device_id(device_id, device_id_len);
+ if (actual_device_id_len < 0)
+ {
+ return actual_device_id_len;
+ }
+
+ for (
+ i = 0;
+ (i < actual_device_id_len) &&
+ (adv_name_len < CONFIG_BT_DEVICE_NAME_MAX);
+ i++
+ )
+ {
+ adv_name[adv_name_len++] =
+ id_dict[device_id[i] % id_dict_len];
+ }
+
+ adv_name[adv_name_len] = 0;
+
+ return bt_set_name(adv_name);
+}
+
int main(void)
{
int err;
@@ -201,6 +242,13 @@ int main(void)
}
printk("bluetooth enabled\n");
+ err = set_adv_name();
+ if (err < 0)
+ {
+ printk("unable to set adv name (err %d)\n", err);
+ return 0;
+ }
+
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME,
ad, ARRAY_SIZE(ad), NULL, 0);
if (err < 0)