Skip to content

Commit 91d10ac

Browse files
MarekPietakapi-no
authored andcommitted
applications: nrf_desktop: Improve HID subscription in HIDS
Change ensures that the module would unsubscribe from previously used HID protocol mode before subscribing for the new HID protocol mode. This ensures that HID boot and HID report protocol mode subscriptions are not enabled at the same time. Jira: NCSDK-34349 Signed-off-by: Marek Pieta <[email protected]>
1 parent 8f59076 commit 91d10ac

File tree

1 file changed

+34
-17
lines changed
  • applications/nrf_desktop/src/modules

1 file changed

+34
-17
lines changed

applications/nrf_desktop/src/modules/hids.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ static bool protocol_boot;
7676
static struct config_channel_transport cfg_chan_transport;
7777
static struct k_work_delayable notify_secured;
7878

79-
static void broadcast_subscription_change(uint8_t report_id, bool enabled)
79+
80+
static bool is_hid_boot_report(uint8_t report_id)
8081
{
81-
bool boot = (report_id == REPORT_ID_BOOT_MOUSE) ||
82-
(report_id == REPORT_ID_BOOT_KEYBOARD);
82+
return (report_id == REPORT_ID_BOOT_MOUSE) || (report_id == REPORT_ID_BOOT_KEYBOARD);
83+
}
8384

84-
enabled = enabled && (protocol_boot == boot);
85+
static bool is_subscribed(uint8_t report_id)
86+
{
87+
return report_enabled[report_id] && (protocol_boot == is_hid_boot_report(report_id));
88+
}
8589

86-
if (enabled == subscribed[report_id]) {
90+
static void broadcast_subscription_change(uint8_t report_id, bool subscribe)
91+
{
92+
if (subscribe == subscribed[report_id]) {
8793
/* No change in subscription. */
8894
return;
8995
}
@@ -93,13 +99,13 @@ static void broadcast_subscription_change(uint8_t report_id, bool enabled)
9399
return;
94100
}
95101

96-
subscribed[report_id] = enabled;
102+
subscribed[report_id] = subscribe;
97103

98104
struct hid_report_subscription_event *event =
99105
new_hid_report_subscription_event();
100106

101107
event->report_id = report_id;
102-
event->enabled = enabled;
108+
event->enabled = subscribe;
103109
event->subscriber = cur_conn;
104110

105111
LOG_INF("Notifications for report 0x%x are %sabled", report_id,
@@ -108,6 +114,24 @@ static void broadcast_subscription_change(uint8_t report_id, bool enabled)
108114
APP_EVENT_SUBMIT(event);
109115
}
110116

117+
static void broadcast_all_subscription_changes_internal(bool subscribed_filter)
118+
{
119+
for (size_t r_id = 0; r_id < REPORT_ID_COUNT; r_id++) {
120+
if (is_subscribed(r_id) == subscribed_filter) {
121+
broadcast_subscription_change(r_id, subscribed_filter);
122+
}
123+
}
124+
}
125+
126+
static void broadcast_all_subscription_changes(void)
127+
{
128+
/* First disable old subscriptions, then enable new subscriptions. This is done to ensure
129+
* that HID boot and HID report mode subscriptions would never be enabled at the same time.
130+
*/
131+
broadcast_all_subscription_changes_internal(false);
132+
broadcast_all_subscription_changes_internal(true);
133+
}
134+
111135
static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
112136
{
113137
switch (evt) {
@@ -125,10 +149,7 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
125149
break;
126150
}
127151

128-
for (size_t r_id = 0; r_id < REPORT_ID_COUNT; r_id++) {
129-
bool enabled = report_enabled[r_id];
130-
broadcast_subscription_change(r_id, enabled);
131-
}
152+
broadcast_all_subscription_changes();
132153
}
133154

134155
static void sync_notif_handler(const struct hid_notification_event *event)
@@ -145,7 +166,7 @@ static void sync_notif_handler(const struct hid_notification_event *event)
145166

146167
report_enabled[report_id] = enabled;
147168

148-
broadcast_subscription_change(report_id, enabled);
169+
broadcast_subscription_change(report_id, is_subscribed(report_id));
149170
}
150171

151172
static void notification_change_handler_async(uint8_t report_id, enum bt_hids_notify_evt evt)
@@ -504,11 +525,7 @@ static void send_hid_report(const struct hid_report_event *event)
504525
static void notify_secured_fn(struct k_work *work)
505526
{
506527
secured = true;
507-
508-
for (size_t r_id = 0; r_id < REPORT_ID_COUNT; r_id++) {
509-
bool enabled = report_enabled[r_id];
510-
broadcast_subscription_change(r_id, enabled);
511-
}
528+
broadcast_all_subscription_changes();
512529
}
513530

514531
static void broadcast_hids_subscriber_state(void *subscriber, bool enabled)

0 commit comments

Comments
 (0)