Skip to content

Commit

Permalink
samples: bluetooth: encrypt throgputsample
Browse files Browse the repository at this point in the history
And conenct with 7.5ms interval to speed up encryption.

Signed-off-by: Martin Tverdal <martin.tverdal@nordicsemi.no>
  • Loading branch information
martintv committed Sep 12, 2023
1 parent 0ac0d6d commit fe632b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ Bluetooth samples

* Updated by disabling the :kconfig:option:`CONFIG_BT_SETTINGS_CCC_LAZY_LOADING` Kconfig option as a workaround fix for the `Zephyr issue #61033`_.

* :ref:`ble_throughput` sample:

* Enabled encryption in the sample.
The measured throughput is calculated over the encrypted data, which is how most of the Bluetooth products use this protocol.

Bluetooth mesh samples
----------------------

Expand Down
36 changes: 32 additions & 4 deletions samples/bluetooth/throughput/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
#define INTERVAL_MIN 0x140 /* 320 units, 400 ms */
#define INTERVAL_MAX 0x140 /* 320 units, 400 ms */
#define INTERVAL_MIN 0x6 /* 6 units, 7.5 ms, only used to setup connection */
#define INTERVAL_MAX 0x6 /* 6 units, 7.5 ms, only used to setup connection */

#define THROUGHPUT_CONFIG_TIMEOUT K_SECONDS(20)

Expand Down Expand Up @@ -205,12 +205,34 @@ static void connected(struct bt_conn *conn, uint8_t hci_err)
info.role == BT_CONN_ROLE_CENTRAL ? "central" : "peripheral");
printk("Conn. interval is %u units\n", info.le.interval);

if (info.role == BT_CONN_ROLE_PERIPHERAL) {
err = bt_conn_set_security(conn, BT_SECURITY_L2);
if (err) {
printk("Failed to set security: %d\n", err);
}
}
}

void security_changed(struct bt_conn *conn, bt_security_t level,
enum bt_security_err security_err)
{
printk("Security changed: level %i, err: %i\n", level, security_err);

if (security_err != 0) {
printk("Failed to encrypt link\n");
bt_conn_disconnect(conn, BT_HCI_ERR_PAIRING_NOT_SUPPORTED);
return;
}

struct bt_conn_info info = {0};
int err;

err = bt_conn_get_info(default_conn, &info);
if (info.role == BT_CONN_ROLE_CENTRAL) {
err = bt_gatt_dm_start(default_conn,
BT_UUID_THROUGHPUT,
&discovery_cb,
&throughput);

if (err) {
printk("Discover failed (err %d)\n", err);
}
Expand Down Expand Up @@ -297,6 +319,11 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
return;
}

err = bt_unpair(info.id, info.le.remote);
if (err) {
printk("Cannot unpair peer (err %d)", err);
}

/* Re-connect using same roles */
if (info.role == BT_CONN_ROLE_CENTRAL) {
scan_start();
Expand Down Expand Up @@ -627,7 +654,8 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
.le_param_req = le_param_req,
.le_param_updated = le_param_updated,
.le_phy_updated = le_phy_updated,
.le_data_len_updated = le_data_length_updated
.le_data_len_updated = le_data_length_updated,
.security_changed = security_changed
};

int main(void)
Expand Down

0 comments on commit fe632b6

Please sign in to comment.