From b3f941fb782fbb884990b8a3467ec292533324b8 Mon Sep 17 00:00:00 2001 From: Martin Tverdal Date: Thu, 6 Jul 2023 13:03:22 +0200 Subject: [PATCH] samples: Bluetooth: encrypt llpm sample It makes most sense to demonstrate llpm on an ecrypted link. Also setting down conection interval to the smallest bluetooth allows. 7.5ms. Since encyrpting the link takes quite a lot of packets back and forth, this will speed up setup. But also it makes sense that the sample compares the best bluetooth can do with the best llpm can do. Signed-off-by: Martin Tverdal --- samples/bluetooth/llpm/src/main.c | 33 +++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/samples/bluetooth/llpm/src/main.c b/samples/bluetooth/llpm/src/main.c index abda26f4039f..f8aec8a73d6a 100644 --- a/samples/bluetooth/llpm/src/main.c +++ b/samples/bluetooth/llpm/src/main.c @@ -27,8 +27,8 @@ #define DEVICE_NAME CONFIG_BT_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) -#define INTERVAL_MIN 0x50 /* 80 units, 100 ms */ -#define INTERVAL_MAX 0x50 /* 80 units, 100 ms */ +#define INTERVAL_MIN 0x6 /* 6 units, 7.5 ms */ +#define INTERVAL_MAX 0x6 /* 6 units, 7.5 ms */ #define INTERVAL_LLPM 0x0D01 /* Proprietary 1 ms */ #define INTERVAL_LLPM_US 1000 @@ -195,18 +195,16 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_scan_stop(); } else { bt_le_adv_stop(); + err = bt_conn_set_security(conn, BT_SECURITY_L2); + if (err) { + printk("Failed to set security: %d\n", err); + } } printk("Connected as %s\n", conn_info.role == BT_CONN_ROLE_CENTRAL ? "central" : "peripheral"); printk("Conn. interval is %u units (1.25 ms/unit)\n", conn_info.le.interval); - - err = bt_gatt_dm_start(default_conn, BT_UUID_LATENCY, &discovery_cb, - &latency_client); - if (err) { - printk("Discover failed (err %d)\n", err); - } } static void disconnected(struct bt_conn *conn, uint8_t reason) @@ -414,10 +412,29 @@ static void test_run(void) } } +void security_changed(struct bt_conn *conn, bt_security_t level, + enum bt_security_err err) +{ + printk("Security changed: level %i, err: %i\n", level, err); + + if (err != 0) { + printk("Failed to encrypt link\n"); + bt_conn_disconnect(conn, BT_HCI_ERR_PAIRING_NOT_SUPPORTED); + return; + } + /*Start service discovery when link is encrypted*/ + err = bt_gatt_dm_start(default_conn, BT_UUID_LATENCY, &discovery_cb, + &latency_client); + if (err) { + printk("Discover failed (err %d)\n", err); + } +} + BT_CONN_CB_DEFINE(conn_callbacks) = { .connected = connected, .disconnected = disconnected, .le_param_updated = le_param_updated, + .security_changed = security_changed, }; int main(void)