2024-04-10 14:18:38

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ v3 1/4] shared/uhid: Add support for bt_uhid_replay

From: Luiz Augusto von Dentz <[email protected]>

This adds support for bt_uhid_replay which enablind replaying
GET/SET_REPORT messages stored during the first time a device is
created.
---
src/shared/uhid.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++
src/shared/uhid.h | 1 +
2 files changed, 124 insertions(+)

diff --git a/src/shared/uhid.c b/src/shared/uhid.c
index 46edb3bfa3ba..690d58d7bd78 100644
--- a/src/shared/uhid.c
+++ b/src/shared/uhid.c
@@ -30,6 +30,14 @@
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif

+struct uhid_replay {
+ bool active;
+ struct queue *out;
+ struct queue *in;
+ struct queue *rout;
+ struct queue *rin;
+};
+
struct bt_uhid {
int ref_count;
struct io *io;
@@ -38,6 +46,7 @@ struct bt_uhid {
struct queue *input;
bool created;
bool started;
+ struct uhid_replay *replay;
};

struct uhid_notify {
@@ -47,6 +56,18 @@ struct uhid_notify {
void *user_data;
};

+static void uhid_replay_free(struct uhid_replay *replay)
+{
+ if (!replay)
+ return;
+
+ queue_destroy(replay->rin, NULL);
+ queue_destroy(replay->in, free);
+ queue_destroy(replay->rout, NULL);
+ queue_destroy(replay->out, free);
+ free(replay);
+}
+
static void uhid_free(struct bt_uhid *uhid)
{
if (uhid->io)
@@ -58,6 +79,8 @@ static void uhid_free(struct bt_uhid *uhid)
if (uhid->input)
queue_destroy(uhid->input, free);

+ uhid_replay_free(uhid->replay);
+
free(uhid);
}

@@ -73,6 +96,42 @@ static void notify_handler(void *data, void *user_data)
notify->func(ev, notify->user_data);
}

+static struct uhid_replay *uhid_replay_new(void)
+{
+ struct uhid_replay *replay = new0(struct uhid_replay, 1);
+
+ replay->out = queue_new();
+ replay->in = queue_new();
+
+ return replay;
+}
+
+static int bt_uhid_record(struct bt_uhid *uhid, bool input,
+ struct uhid_event *ev)
+{
+ if (!uhid)
+ return -EINVAL;
+
+ /* Capture input events in replay mode and send the next replay event */
+ if (uhid->replay && uhid->replay->active && input) {
+ queue_pop_head(uhid->replay->rin);
+ bt_uhid_replay(uhid);
+ return -EALREADY;
+ }
+
+ if (!uhid->replay)
+ uhid->replay = uhid_replay_new();
+
+ if (input)
+ queue_push_tail(uhid->replay->in,
+ util_memdup(ev, sizeof(*ev)));
+ else
+ queue_push_tail(uhid->replay->out,
+ util_memdup(ev, sizeof(*ev)));
+
+ return 0;
+}
+
static bool uhid_read_handler(struct io *io, void *user_data)
{
struct bt_uhid *uhid = user_data;
@@ -93,6 +152,13 @@ static bool uhid_read_handler(struct io *io, void *user_data)
if ((size_t) len < sizeof(ev.type))
return false;

+ switch (ev.type) {
+ case UHID_GET_REPORT:
+ case UHID_SET_REPORT:
+ bt_uhid_record(uhid, false, &ev);
+ break;
+ }
+
queue_foreach(uhid->notify_list, notify_handler, &ev);

return true;
@@ -382,6 +448,9 @@ int bt_uhid_set_report_reply(struct bt_uhid *uhid, uint8_t id, uint8_t status)
rsp->id = id;
rsp->err = status;

+ if (bt_uhid_record(uhid, true, &ev) == -EALREADY)
+ return 0;
+
return bt_uhid_send(uhid, &ev);
}

@@ -412,6 +481,9 @@ int bt_uhid_get_report_reply(struct bt_uhid *uhid, uint8_t id, uint8_t number,
memcpy(&rsp->data[len], data, rsp->size - len);

done:
+ if (bt_uhid_record(uhid, true, &ev) == -EALREADY)
+ return 0;
+
return bt_uhid_send(uhid, &ev);
}

@@ -437,3 +509,54 @@ int bt_uhid_destroy(struct bt_uhid *uhid)

return err;
}
+
+static void queue_append(void *data, void *user_data)
+{
+ queue_push_tail(user_data, data);
+}
+
+static struct queue *queue_dup(struct queue *q)
+{
+ struct queue *dup;
+
+ if (!q || queue_isempty(q))
+ return NULL;
+
+ dup = queue_new();
+
+ queue_foreach(q, queue_append, dup);
+
+ return dup;
+}
+
+int bt_uhid_replay(struct bt_uhid *uhid)
+{
+ struct uhid_event *ev;
+
+ if (!uhid || !uhid->started)
+ return -EINVAL;
+
+ if (!uhid->replay)
+ return 0;
+
+ if (uhid->replay->active)
+ goto resend;
+
+ uhid->replay->active = true;
+ queue_destroy(uhid->replay->rin, NULL);
+ uhid->replay->rin = queue_dup(uhid->replay->in);
+
+ queue_destroy(uhid->replay->rout, NULL);
+ uhid->replay->rout = queue_dup(uhid->replay->out);
+
+resend:
+ ev = queue_pop_head(uhid->replay->rout);
+ if (!ev) {
+ uhid->replay->active = false;
+ return 0;
+ }
+
+ queue_foreach(uhid->notify_list, notify_handler, ev);
+
+ return 0;
+}
diff --git a/src/shared/uhid.h b/src/shared/uhid.h
index d70533882727..4e288cb192aa 100644
--- a/src/shared/uhid.h
+++ b/src/shared/uhid.h
@@ -42,3 +42,4 @@ int bt_uhid_set_report_reply(struct bt_uhid *uhid, uint8_t id, uint8_t status);
int bt_uhid_get_report_reply(struct bt_uhid *uhid, uint8_t id, uint8_t number,
uint8_t status, const void *data, size_t size);
int bt_uhid_destroy(struct bt_uhid *uhid);
+int bt_uhid_replay(struct bt_uhid *uhid);
--
2.44.0



2024-04-10 14:29:33

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ v3 4/4] hog-lib: Destroy uHID device if there is traffic while disconnected

From: Luiz Augusto von Dentz <[email protected]>

This attempts to destroy input device if there is an attempt to
communicate with it while disconnected.
---
profiles/input/hog-lib.c | 47 +++++++++++++++++++++++++---------------
1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c
index 0291adb6eb23..2d8d0f359e57 100644
--- a/profiles/input/hog-lib.c
+++ b/profiles/input/hog-lib.c
@@ -825,6 +825,19 @@ static void set_report_cb(guint8 status, const guint8 *pdu,
error("bt_uhid_set_report_reply: %s", strerror(-err));
}

+static void uhid_destroy(struct bt_hog *hog)
+{
+ int err;
+
+ bt_uhid_unregister_all(hog->uhid);
+
+ err = bt_uhid_destroy(hog->uhid);
+ if (err < 0) {
+ error("bt_uhid_destroy: %s", strerror(-err));
+ return;
+ }
+}
+
static void set_report(struct uhid_event *ev, void *user_data)
{
struct bt_hog *hog = user_data;
@@ -833,6 +846,14 @@ static void set_report(struct uhid_event *ev, void *user_data)
int size;
int err;

+ /* Destroy input device if there is an attempt to communicate with it
+ * while disconnected.
+ */
+ if (hog->attrib == NULL) {
+ uhid_destroy(hog);
+ return;
+ }
+
/* uhid never sends reqs in parallel; if there's a req, it timed out */
if (hog->setrep_att) {
g_attrib_cancel(hog->attrib, hog->setrep_att);
@@ -856,11 +877,6 @@ static void set_report(struct uhid_event *ev, void *user_data)
--size;
}

- if (hog->attrib == NULL) {
- err = -ENOTCONN;
- goto fail;
- }
-
DBG("Sending report type %d ID %d to handle 0x%X", report->type,
report->id, report->value_handle);

@@ -928,6 +944,14 @@ static void get_report(struct uhid_event *ev, void *user_data)
struct report *report;
guint8 err;

+ /* Destroy input device if there is an attempt to communicate with it
+ * while disconnected.
+ */
+ if (hog->attrib == NULL) {
+ uhid_destroy(hog);
+ return;
+ }
+
/* uhid never sends reqs in parallel; if there's a req, it timed out */
if (hog->getrep_att) {
g_attrib_cancel(hog->attrib, hog->getrep_att);
@@ -1204,19 +1228,6 @@ static bool cancel_gatt_req(const void *data, const void *user_data)
return g_attrib_cancel(hog->attrib, req->id);
}

-static void uhid_destroy(struct bt_hog *hog)
-{
- int err;
-
- bt_uhid_unregister_all(hog->uhid);
-
- err = bt_uhid_destroy(hog->uhid);
- if (err < 0) {
- error("bt_uhid_destroy: %s", strerror(-err));
- return;
- }
-}
-
static void hog_free(void *data)
{
struct bt_hog *hog = data;
--
2.44.0


2024-04-10 14:44:14

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ v3 2/4] hog-lib: Make use of bt_uhid_replay

From: Luiz Augusto von Dentz <[email protected]>

This makes use of bt_uhid_replay to replay the GET/SET_REPORT messages
recorded during the first time the device is created.
---
profiles/input/hog-lib.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c
index 8071e1364b10..0291adb6eb23 100644
--- a/profiles/input/hog-lib.c
+++ b/profiles/input/hog-lib.c
@@ -856,12 +856,14 @@ static void set_report(struct uhid_event *ev, void *user_data)
--size;
}

+ if (hog->attrib == NULL) {
+ err = -ENOTCONN;
+ goto fail;
+ }
+
DBG("Sending report type %d ID %d to handle 0x%X", report->type,
report->id, report->value_handle);

- if (hog->attrib == NULL)
- return;
-
hog->setrep_att = gatt_write_char(hog->attrib,
report->value_handle,
data, size, set_report_cb,
@@ -1726,6 +1728,11 @@ bool bt_hog_attach(struct bt_hog *hog, void *gatt)
"handle 0x%04x", r->value_handle);
}

+ /* Attempt to replay get/set report messages since the driver might not
+ * be aware the device has been disconnected in the meantime.
+ */
+ bt_uhid_replay(hog->uhid);
+
return true;
}

--
2.44.0


2024-04-10 15:30:34

by patchwork-bot+bluetooth

[permalink] [raw]
Subject: Re: [PATCH BlueZ v3 1/4] shared/uhid: Add support for bt_uhid_replay

Hello:

This series was applied to bluetooth/bluez.git (master)
by Luiz Augusto von Dentz <[email protected]>:

On Wed, 10 Apr 2024 10:02:02 -0400 you wrote:
> From: Luiz Augusto von Dentz <[email protected]>
>
> This adds support for bt_uhid_replay which enablind replaying
> GET/SET_REPORT messages stored during the first time a device is
> created.
> ---
> src/shared/uhid.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++
> src/shared/uhid.h | 1 +
> 2 files changed, 124 insertions(+)

Here is the summary with links:
- [BlueZ,v3,1/4] shared/uhid: Add support for bt_uhid_replay
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=991ec8e2e088
- [BlueZ,v3,2/4] hog-lib: Make use of bt_uhid_replay
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=a78c839b5d85
- [BlueZ,v3,3/4] input/device: Make use of bt_uhid_replay
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=528f5a8c7d76
- [BlueZ,v3,4/4] hog-lib: Destroy uHID device if there is traffic while disconnected
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=b163e2bd0303

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



2024-04-10 16:57:06

by bluez.test.bot

[permalink] [raw]
Subject: RE: [BlueZ,v3,1/4] shared/uhid: Add support for bt_uhid_replay

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=843276

---Test result---

Test Summary:
CheckPatch PASS 1.87 seconds
GitLint FAIL 1.47 seconds
BuildEll PASS 24.39 seconds
BluezMake PASS 1644.01 seconds
MakeCheck PASS 13.61 seconds
MakeDistcheck PASS 175.33 seconds
CheckValgrind PASS 244.98 seconds
CheckSmatch PASS 348.45 seconds
bluezmakeextell PASS 118.19 seconds
IncrementalBuild PASS 6108.40 seconds
ScanBuild PASS 976.83 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[BlueZ,v3,4/4] hog-lib: Destroy uHID device if there is traffic while disconnected

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (82>80): "[BlueZ,v3,4/4] hog-lib: Destroy uHID device if there is traffic while disconnected"


---
Regards,
Linux Bluetooth