aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeon Rinkel <[email protected]>2023-11-07 19:40:58 +0100
committerLeon Rinkel <[email protected]>2023-11-07 19:40:58 +0100
commit88076229aca498496aecb5ed5f90d9198ddb25a3 (patch)
tree8e172f1ba0d8760bf6c799a6b5ca028a7afc84b6
parentabe76c0c1cb799db5122adc82462b0005ad1df93 (diff)
Allow changing device name when paired
-rw-r--r--app/prj.conf19
-rw-r--r--app/src/main.c89
2 files changed, 68 insertions, 40 deletions
diff --git a/app/prj.conf b/app/prj.conf
index 7c6a8c8..6148f8c 100644
--- a/app/prj.conf
+++ b/app/prj.conf
@@ -8,15 +8,30 @@ CONFIG_SPI=y
CONFIG_LED_STRIP=y
CONFIG_LUMEN_WS2812_STRIP=y
+CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
+CONFIG_BT_SETTINGS=y
+CONFIG_FLASH=y
+CONFIG_FLASH_PAGE_LAYOUT=y
+CONFIG_FLASH_MAP=y
+CONFIG_NVS=y
+CONFIG_SETTINGS=y
+CONFIG_SETTINGS_RUNTIME=y
+CONFIG_BT_DIS_SETTINGS=y
+CONFIG_BT_DIS_STR_MAX=64
+
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
+CONFIG_BT_SMP=y
+CONFIG_BT_SMP_ENFORCE_MITM=y
+CONFIG_BT_SMP_SC_ONLY=y
+CONFIG_BT_SIGNING=y
+CONFIG_BT_PRIVACY=y
+CONFIG_BT_FIXED_PASSKEY=y
CONFIG_BT_DIS=y
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 ca9eaca..628f87e 100644
--- a/app/src/main.c
+++ b/app/src/main.c
@@ -8,11 +8,11 @@
#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>
#include <zephyr/bluetooth/gatt.h>
+#include <zephyr/settings/settings.h>
#include <app_version.h>
@@ -180,45 +180,43 @@ void color_wheel(uint8_t pos, uint8_t* r, uint8_t* g, uint8_t* b)
}
}
-static int set_adv_name(void)
+static void auth_passkey_display(struct bt_conn* conn, unsigned int passkey)
{
- 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 addr[BT_ADDR_LE_STR_LEN];
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+ printk("passkey for %s: %06u\n", addr, passkey);
+}
- char adv_name[CONFIG_BT_DEVICE_NAME_MAX + 1] = "Lumen ";
- size_t adv_name_len = strlen(adv_name);
+static void auth_cancel(struct bt_conn* conn)
+{
+ char addr[BT_ADDR_LE_STR_LEN];
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+ printk("pairing cancelled for %s\n", addr);
+}
- actual_device_id_len =
- hwinfo_get_device_id(device_id, device_id_len);
- if (actual_device_id_len < 0)
- {
- return actual_device_id_len;
- }
+static void pairing_complete(struct bt_conn* conn, bool bonded)
+{
+ char addr[BT_ADDR_LE_STR_LEN];
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+ printk("pairing completed for %s, bonded: %d\n", addr, bonded);
+}
- 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];
- }
+static void pairing_failed(struct bt_conn* conn, enum bt_security_err reason)
+{
+}
- adv_name[adv_name_len] = 0;
+static struct bt_conn_auth_cb conn_auth_callbacks =
+{
+ .passkey_display = auth_passkey_display,
+ .cancel = auth_cancel,
+ .pairing_confirm = NULL,
+};
- return bt_set_name(adv_name);
-}
+static struct bt_conn_auth_info_cb conn_auth_info_callbacks =
+{
+ .pairing_complete = pairing_complete,
+ .pairing_failed = pairing_failed,
+};
int main(void)
{
@@ -234,6 +232,23 @@ int main(void)
return 0;
}
+ err = bt_conn_auth_cb_register(&conn_auth_callbacks);
+ if (err < 0)
+ {
+ printk("failed to register auth cb (err %d)\n", err);
+ return 0;
+ }
+
+ err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
+ if (err < 0)
+ {
+ printk("failed to register auth info cb (err %d)\n", err);
+ return 0;
+ }
+
+ unsigned int passkey = 123456;
+ bt_passkey_set(passkey);
+
err = bt_enable(NULL);
if (err < 0)
{
@@ -242,11 +257,9 @@ int main(void)
}
printk("bluetooth enabled\n");
- err = set_adv_name();
- if (err < 0)
+ if (IS_ENABLED(CONFIG_SETTINGS))
{
- printk("unable to set adv name (err %d)\n", err);
- return 0;
+ settings_load();
}
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME,