Skip to content

Commit

Permalink
test(split): Add peripheral input test.
Browse files Browse the repository at this point in the history
Test event propagation from peripheral input devices.
  • Loading branch information
petejohanson committed Jan 13, 2025
1 parent e2f8891 commit 47c3f68
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 8 deletions.
73 changes: 65 additions & 8 deletions app/tests/ble/central/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions app/tests/ble/split/basic/snapshot.log
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 23
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 28
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 30
profile 0 <dbg> ble_central: discover_func: [SUBSCRIBED]
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 32
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 34
profile 0 <dbg> ble_central: discover_func: [CONSUMER SUBSCRIBED]
profile 0 <dbg> ble_central: notify_func: payload
profile 0 00 00 04 00 00 00 00 00 |........
profile 0 <dbg> ble_central: notify_func: payload
Expand Down
3 changes: 3 additions & 0 deletions app/tests/ble/split/multiple-peripherals/snapshot.log
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 23
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 28
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 30
profile 0 <dbg> ble_central: discover_func: [SUBSCRIBED]
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 32
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 34
profile 0 <dbg> ble_central: discover_func: [CONSUMER SUBSCRIBED]
profile 0 <dbg> ble_central: notify_func: payload
profile 0 00 00 05 00 00 00 00 00 |........
profile 0 <dbg> ble_central: notify_func: payload
Expand Down
3 changes: 3 additions & 0 deletions app/tests/ble/split/peripheral-encoder/snapshot.log
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 23
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 28
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 30
profile 0 <dbg> ble_central: discover_func: [SUBSCRIBED]
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 32
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 34
profile 0 <dbg> ble_central: discover_func: [CONSUMER SUBSCRIBED]
profile 0 <dbg> ble_central: notify_func: payload
profile 0 00 00 04 00 00 00 00 00 |........
profile 0 <dbg> ble_central: notify_func: payload
Expand Down
1 change: 1 addition & 0 deletions app/tests/ble/split/peripheral-input/events.patterns
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/tests/ble/split/peripheral-input/nrf52_bsim.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ZMK_SPLIT=y
CONFIG_ZMK_POINTING=y
27 changes: 27 additions & 0 deletions app/tests/ble/split/peripheral-input/nrf52_bsim.keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <behaviors.dtsi>
#include <dt-bindings/zmk/bt.h>
#include <dt-bindings/zmk/keys.h>

#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>;
};
};
};
28 changes: 28 additions & 0 deletions app/tests/ble/split/peripheral-input/peripheral.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#include <dt-bindings/zmk/kscan_mock.h>
#include <zephyr/dt-bindings/input/input-event-codes.h>

#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
= <INPUT_EV_REL INPUT_REL_X 100 0>
, <INPUT_EV_REL INPUT_REL_Y 100 1>
;
};
};

&split_input {
device = <&mock_input>;
};
16 changes: 16 additions & 0 deletions app/tests/ble/split/peripheral-input/shared.dtsi
Original file line number Diff line number Diff line change
@@ -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>;
};
};
2 changes: 2 additions & 0 deletions app/tests/ble/split/peripheral-input/siblings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
./ble_test_central.exe -d=2 -subscribe_to_pointer_report
./tests_ble_split_peripheral-input_peripheral.exe -d=3
23 changes: 23 additions & 0 deletions app/tests/ble/split/peripheral-input/snapshot.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
profile 0 <wrn> bt_id: No static addresses stored in controller
profile 0 <dbg> ble_central: main: [Bluetooth initialized]
profile 0 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 0 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -56
profile 0 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 0 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 0 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
profile 0 <dbg> ble_central: connected: [Connected]: FD:9E:B2:48:47:39 (random)
profile 0 <dbg> ble_central: connected: [Setting the security for the connection]
profile 0 <dbg> ble_central: pairing_complete: Pairing complete
profile 0 <dbg> ble_central: discover_conn: [Discovery started for conn]
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 23
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 28
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 30
profile 0 <dbg> ble_central: discover_func: [SUBSCRIBED]
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 32
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 34
profile 0 <dbg> ble_central: discover_func: [CONSUMER SUBSCRIBED]
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 36
profile 0 <dbg> ble_central: discover_func: [ATTRIBUTE] handle 38
profile 0 <dbg> ble_central: discover_func: [MOUSE SUBSCRIBED]
profile 0 <dbg> ble_central: notify_func: payload
profile 0 00 64 00 64 00 00 00 00 00 |.d.d.... .

0 comments on commit 47c3f68

Please sign in to comment.