diff --git a/samples/bluetooth/peripheral_hids/Kconfig b/samples/bluetooth/peripheral_hids/Kconfig new file mode 100644 index 0000000000..efea45cdea --- /dev/null +++ b/samples/bluetooth/peripheral_hids/Kconfig @@ -0,0 +1,13 @@ +# Copyright 2023 Google LLC +# SPDX-License-Identifier: Apache-2.0 + +mainmenu "Bluetooth: Peripheral HIDs" + +config SAMPLE_BT_USE_AUTHENTICATION + bool "Enable passkey authentication" + default y + help + Enable the passkey authentication callback and register the GATT + read and and write attributes as authentication required. + +source "Kconfig.zephyr" diff --git a/samples/bluetooth/peripheral_hids/README.rst b/samples/bluetooth/peripheral_hids/README.rst index 9fc6e43e6a..de1e35aaa3 100644 --- a/samples/bluetooth/peripheral_hids/README.rst +++ b/samples/bluetooth/peripheral_hids/README.rst @@ -10,6 +10,11 @@ Similar to the :ref:`Peripheral ` sample, except that this application specifically exposes the HID GATT Service. The report map used is for a generic mouse. +In the default configuration the sample uses passkey authentication (displays a +code on the peripheral and requires that to be entered on the host during +pairing) and requires an authenticated link to access the GATT characteristics. +To disable authentication and just use encrypted channels instead, build the +sample with `CONFIG_SAMPLE_BT_USE_AUTHENTICATION=n`. Requirements ************ diff --git a/samples/bluetooth/peripheral_hids/sample.yaml b/samples/bluetooth/peripheral_hids/sample.yaml index 5dd680aa41..1c73ba4e45 100644 --- a/samples/bluetooth/peripheral_hids/sample.yaml +++ b/samples/bluetooth/peripheral_hids/sample.yaml @@ -8,3 +8,11 @@ tests: tags: bluetooth integration_platforms: - qemu_cortex_m3 + sample.bluetooth.peripheral_hids.no_authentication: + harness: bluetooth + extra_configs: + - CONFIG_SAMPLE_BT_USE_AUTHENTICATION=n + platform_allow: qemu_cortex_m3 qemu_x86 + tags: bluetooth + integration_platforms: + - qemu_cortex_m3 diff --git a/samples/bluetooth/peripheral_hids/src/hog.c b/samples/bluetooth/peripheral_hids/src/hog.c index be8104c33b..cf67521517 100644 --- a/samples/bluetooth/peripheral_hids/src/hog.c +++ b/samples/bluetooth/peripheral_hids/src/hog.c @@ -141,6 +141,16 @@ static ssize_t write_ctrl_point(struct bt_conn *conn, return len; } +#if CONFIG_SAMPLE_BT_USE_AUTHENTICATION +/* Require encryption using authenticated link-key. */ +#define SAMPLE_BT_PERM_READ BT_GATT_PERM_READ_AUTHEN +#define SAMPLE_BT_PERM_WRITE BT_GATT_PERM_WRITE_AUTHEN +#else +/* Require encryption. */ +#define SAMPLE_BT_PERM_READ BT_GATT_PERM_READ_ENCRYPT +#define SAMPLE_BT_PERM_WRITE BT_GATT_PERM_WRITE_ENCRYPT +#endif + /* HID Service Declaration */ BT_GATT_SERVICE_DEFINE(hog_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS), @@ -150,10 +160,10 @@ BT_GATT_SERVICE_DEFINE(hog_svc, BT_GATT_PERM_READ, read_report_map, NULL, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, - BT_GATT_PERM_READ_AUTHEN, + SAMPLE_BT_PERM_READ, read_input_report, NULL, NULL), BT_GATT_CCC(input_ccc_changed, - BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN), + SAMPLE_BT_PERM_READ | SAMPLE_BT_PERM_WRITE), BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, read_report, NULL, &input), BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, diff --git a/samples/bluetooth/peripheral_hids/src/main.c b/samples/bluetooth/peripheral_hids/src/main.c index 52067c4776..111b4957ac 100644 --- a/samples/bluetooth/peripheral_hids/src/main.c +++ b/samples/bluetooth/peripheral_hids/src/main.c @@ -137,7 +137,10 @@ void main(void) return; } - bt_conn_auth_cb_register(&auth_cb_display); + if (IS_ENABLED(CONFIG_SAMPLE_BT_USE_AUTHENTICATION)) { + bt_conn_auth_cb_register(&auth_cb_display); + printk("Bluetooth authentication callbacks registered.\n"); + } hog_button_loop(); }