diff mbox series

[BlueZ,v3,4/4] hog-lib: Destroy uHID device if there is traffic while disconnected

Message ID 20240410140205.4056047-4-luiz.dentz@gmail.com
State New
Headers show
Series [BlueZ,v3,1/4] shared/uhid: Add support for bt_uhid_replay | expand

Commit Message

Luiz Augusto von Dentz April 10, 2024, 2:02 p.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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 mbox series

Patch

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;