From 47c3f68ea75171073645cde98fbee12c8efa200c Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Sun, 12 Jan 2025 00:40:03 -0700 Subject: [PATCH] test(split): Add peripheral input test. Test event propagation from peripheral input devices. --- app/tests/ble/central/src/main.c | 73 +++++++++++++++++-- app/tests/ble/split/basic/snapshot.log | 3 + .../split/multiple-peripherals/snapshot.log | 3 + .../ble/split/peripheral-encoder/snapshot.log | 3 + .../split/peripheral-input/events.patterns | 1 + .../split/peripheral-input/nrf52_bsim.conf | 2 + .../split/peripheral-input/nrf52_bsim.keymap | 27 +++++++ .../split/peripheral-input/peripheral.overlay | 28 +++++++ .../ble/split/peripheral-input/shared.dtsi | 16 ++++ .../ble/split/peripheral-input/siblings.txt | 2 + .../ble/split/peripheral-input/snapshot.log | 23 ++++++ 11 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 app/tests/ble/split/peripheral-input/events.patterns create mode 100644 app/tests/ble/split/peripheral-input/nrf52_bsim.conf create mode 100644 app/tests/ble/split/peripheral-input/nrf52_bsim.keymap create mode 100644 app/tests/ble/split/peripheral-input/peripheral.overlay create mode 100644 app/tests/ble/split/peripheral-input/shared.dtsi create mode 100644 app/tests/ble/split/peripheral-input/siblings.txt create mode 100644 app/tests/ble/split/peripheral-input/snapshot.log diff --git a/app/tests/ble/central/src/main.c b/app/tests/ble/central/src/main.c index 3c5c08887bc..f33d50e0e80 100644 --- a/app/tests/ble/central/src/main.c +++ b/app/tests/ble/central/src/main.c @@ -45,6 +45,7 @@ static bool skip_set_security_on_connect = false; static bool skip_discovery_on_connect = false; static bool read_directly_on_discovery = false; static bool write_hid_indicators_on_discovery = false; +static bool subscribe_to_pointer_report = false; static int32_t wait_on_start = 0; static void ble_central_native_posix_options(void) { @@ -74,6 +75,11 @@ static void ble_central_native_posix_options(void) { .type = 'b', .dest = (void *)&read_hid_report_on_connect, .descript = "Read the peripheral HID report after connecting"}, + {.is_switch = true, + .option = "subscribe_to_pointer_report", + .type = 'b', + .dest = (void *)&subscribe_to_pointer_report, + .descript = "Subscribe to peripheral mouse HID report after connecting"}, {.is_switch = true, .option = "skip_discovery_on_connect", .type = 'b', @@ -110,6 +116,8 @@ static struct bt_conn *default_conn; static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0); static struct bt_gatt_discover_params discover_params; static struct bt_gatt_subscribe_params subscribe_params; +static struct bt_gatt_subscribe_params consumer_subscribe_params; +static struct bt_gatt_subscribe_params pointer_subscribe_params; static uint8_t notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, uint16_t length) { @@ -167,6 +175,28 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; subscribe_params.value_handle = bt_gatt_attr_value_handle(attr); + err = bt_gatt_discover(conn, &discover_params); + if (err) { + LOG_DBG("[Discover failed] (err %d)", err); + } + } else if (!consumer_subscribe_params.value_handle) { + memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.start_handle = attr->handle + 2; + discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; + consumer_subscribe_params.value_handle = bt_gatt_attr_value_handle(attr); + + err = bt_gatt_discover(conn, &discover_params); + if (err) { + LOG_DBG("[Discover failed] (err %d)", err); + } + } else if (subscribe_to_pointer_report && !pointer_subscribe_params.value_handle) { + memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.start_handle = attr->handle + 2; + discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; + pointer_subscribe_params.value_handle = bt_gatt_attr_value_handle(attr); + err = bt_gatt_discover(conn, &discover_params); if (err) { LOG_DBG("[Discover failed] (err %d)", err); @@ -182,18 +212,45 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at return BT_GATT_ITER_CONTINUE; } } else if (discover_params.type == BT_GATT_DISCOVER_DESCRIPTOR) { - subscribe_params.notify = notify_func; - subscribe_params.value = BT_GATT_CCC_NOTIFY; - subscribe_params.ccc_handle = attr->handle; + if (!subscribe_params.ccc_handle) { - err = bt_gatt_subscribe(conn, &subscribe_params); - if (err && err != -EALREADY) { - LOG_DBG("[Subscribe failed] (err %d)", err); + subscribe_params.notify = notify_func; + subscribe_params.value = BT_GATT_CCC_NOTIFY; + subscribe_params.ccc_handle = attr->handle; + + err = bt_gatt_subscribe(conn, &subscribe_params); + if (err && err != -EALREADY) { + LOG_DBG("[Subscribe failed] (err %d)", err); + } else { + LOG_DBG("[SUBSCRIBED]"); + } + } else if (!consumer_subscribe_params.ccc_handle) { + + consumer_subscribe_params.notify = notify_func; + consumer_subscribe_params.value = BT_GATT_CCC_NOTIFY; + consumer_subscribe_params.ccc_handle = attr->handle; + + err = bt_gatt_subscribe(conn, &consumer_subscribe_params); + if (err && err != -EALREADY) { + LOG_DBG("[Subscribe failed] (err %d)", err); + } else { + LOG_DBG("[CONSUMER SUBSCRIBED]"); + } } else { - LOG_DBG("[SUBSCRIBED]"); + pointer_subscribe_params.notify = notify_func; + pointer_subscribe_params.value = BT_GATT_CCC_NOTIFY; + pointer_subscribe_params.ccc_handle = attr->handle; + + err = bt_gatt_subscribe(conn, &pointer_subscribe_params); + if (err && err != -EALREADY) { + LOG_DBG("[Subscribe failed] (err %d)", err); + } else { + LOG_DBG("[MOUSE SUBSCRIBED]"); + } } - find_next_hids_report = write_hid_indicators_on_discovery; + find_next_hids_report = !consumer_subscribe_params.ccc_handle || + write_hid_indicators_on_discovery || subscribe_to_pointer_report; } if (find_next_hids_report) { diff --git a/app/tests/ble/split/basic/snapshot.log b/app/tests/ble/split/basic/snapshot.log index c3804f64afb..545a0f365ed 100644 --- a/app/tests/ble/split/basic/snapshot.log +++ b/app/tests/ble/split/basic/snapshot.log @@ -13,6 +13,9 @@ profile 0 ble_central: discover_func: [ATTRIBUTE] handle 23 profile 0 ble_central: discover_func: [ATTRIBUTE] handle 28 profile 0 ble_central: discover_func: [ATTRIBUTE] handle 30 profile 0 ble_central: discover_func: [SUBSCRIBED] +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 32 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 34 +profile 0 ble_central: discover_func: [CONSUMER SUBSCRIBED] profile 0 ble_central: notify_func: payload profile 0 00 00 04 00 00 00 00 00 |........ profile 0 ble_central: notify_func: payload diff --git a/app/tests/ble/split/multiple-peripherals/snapshot.log b/app/tests/ble/split/multiple-peripherals/snapshot.log index 58de7954e30..7c19d59c403 100644 --- a/app/tests/ble/split/multiple-peripherals/snapshot.log +++ b/app/tests/ble/split/multiple-peripherals/snapshot.log @@ -13,6 +13,9 @@ profile 0 ble_central: discover_func: [ATTRIBUTE] handle 23 profile 0 ble_central: discover_func: [ATTRIBUTE] handle 28 profile 0 ble_central: discover_func: [ATTRIBUTE] handle 30 profile 0 ble_central: discover_func: [SUBSCRIBED] +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 32 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 34 +profile 0 ble_central: discover_func: [CONSUMER SUBSCRIBED] profile 0 ble_central: notify_func: payload profile 0 00 00 05 00 00 00 00 00 |........ profile 0 ble_central: notify_func: payload diff --git a/app/tests/ble/split/peripheral-encoder/snapshot.log b/app/tests/ble/split/peripheral-encoder/snapshot.log index c3804f64afb..545a0f365ed 100644 --- a/app/tests/ble/split/peripheral-encoder/snapshot.log +++ b/app/tests/ble/split/peripheral-encoder/snapshot.log @@ -13,6 +13,9 @@ profile 0 ble_central: discover_func: [ATTRIBUTE] handle 23 profile 0 ble_central: discover_func: [ATTRIBUTE] handle 28 profile 0 ble_central: discover_func: [ATTRIBUTE] handle 30 profile 0 ble_central: discover_func: [SUBSCRIBED] +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 32 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 34 +profile 0 ble_central: discover_func: [CONSUMER SUBSCRIBED] profile 0 ble_central: notify_func: payload profile 0 00 00 04 00 00 00 00 00 |........ profile 0 ble_central: notify_func: payload diff --git a/app/tests/ble/split/peripheral-input/events.patterns b/app/tests/ble/split/peripheral-input/events.patterns new file mode 100644 index 00000000000..f8cf363c2e0 --- /dev/null +++ b/app/tests/ble/split/peripheral-input/events.patterns @@ -0,0 +1 @@ +s/^d_02: @[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9] .{19}/profile 0 /p diff --git a/app/tests/ble/split/peripheral-input/nrf52_bsim.conf b/app/tests/ble/split/peripheral-input/nrf52_bsim.conf new file mode 100644 index 00000000000..08e4ff087db --- /dev/null +++ b/app/tests/ble/split/peripheral-input/nrf52_bsim.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_POINTING=y \ No newline at end of file diff --git a/app/tests/ble/split/peripheral-input/nrf52_bsim.keymap b/app/tests/ble/split/peripheral-input/nrf52_bsim.keymap new file mode 100644 index 00000000000..d1a0f3eacc7 --- /dev/null +++ b/app/tests/ble/split/peripheral-input/nrf52_bsim.keymap @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "shared.dtsi" + +&kscan { + /delete-property/ exit-after; + events = <>; +}; + +&split_listener { + status = "okay"; +}; +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { + bindings = < + &kp A &kp B + &bt BT_SEL 0 &bt BT_CLR>; + + sensor-bindings = <&inc_dec_kp A B>; + }; + }; +}; diff --git a/app/tests/ble/split/peripheral-input/peripheral.overlay b/app/tests/ble/split/peripheral-input/peripheral.overlay new file mode 100644 index 00000000000..fa699c62dc3 --- /dev/null +++ b/app/tests/ble/split/peripheral-input/peripheral.overlay @@ -0,0 +1,28 @@ + +#include +#include + +#include "shared.dtsi" + +&kscan { + events = <>; + + /delete-property/ exit-after; +}; + +/ { + mock_input: mock_input { + compatible = "zmk,input-mock"; + status = "okay"; + event-startup-delay = <4000>; + event-period = <2000>; + events + = + , + ; + }; +}; + +&split_input { + device = <&mock_input>; +}; diff --git a/app/tests/ble/split/peripheral-input/shared.dtsi b/app/tests/ble/split/peripheral-input/shared.dtsi new file mode 100644 index 00000000000..3a31a28f671 --- /dev/null +++ b/app/tests/ble/split/peripheral-input/shared.dtsi @@ -0,0 +1,16 @@ +/ { + splits { + #address-cells = <1>; + #size-cells = <0>; + split_input: split_input@0 { + compatible = "zmk,input-split"; + reg = <0>; + }; + }; + + split_listener: split_listener { + compatible = "zmk,input-listener"; + status = "disabled"; + device = <&split_input>; + }; +}; \ No newline at end of file diff --git a/app/tests/ble/split/peripheral-input/siblings.txt b/app/tests/ble/split/peripheral-input/siblings.txt new file mode 100644 index 00000000000..16ee60971f9 --- /dev/null +++ b/app/tests/ble/split/peripheral-input/siblings.txt @@ -0,0 +1,2 @@ +./ble_test_central.exe -d=2 -subscribe_to_pointer_report +./tests_ble_split_peripheral-input_peripheral.exe -d=3 diff --git a/app/tests/ble/split/peripheral-input/snapshot.log b/app/tests/ble/split/peripheral-input/snapshot.log new file mode 100644 index 00000000000..aa8517f8898 --- /dev/null +++ b/app/tests/ble/split/peripheral-input/snapshot.log @@ -0,0 +1,23 @@ +profile 0 bt_id: No static addresses stored in controller +profile 0 ble_central: main: [Bluetooth initialized] +profile 0 ble_central: start_scan: [Scanning successfully started] +profile 0 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -56 +profile 0 ble_central: eir_found: [AD]: 25 data_len 2 +profile 0 ble_central: eir_found: [AD]: 1 data_len 1 +profile 0 ble_central: eir_found: [AD]: 2 data_len 4 +profile 0 ble_central: connected: [Connected]: FD:9E:B2:48:47:39 (random) +profile 0 ble_central: connected: [Setting the security for the connection] +profile 0 ble_central: pairing_complete: Pairing complete +profile 0 ble_central: discover_conn: [Discovery started for conn] +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 23 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 28 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 30 +profile 0 ble_central: discover_func: [SUBSCRIBED] +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 32 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 34 +profile 0 ble_central: discover_func: [CONSUMER SUBSCRIBED] +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 36 +profile 0 ble_central: discover_func: [ATTRIBUTE] handle 38 +profile 0 ble_central: discover_func: [MOUSE SUBSCRIBED] +profile 0 ble_central: notify_func: payload +profile 0 00 64 00 64 00 00 00 00 00 |.d.d.... .