diff mbox series

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

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

Commit Message

Luiz Augusto von Dentz April 9, 2024, 8:27 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 | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c
index 0291adb6eb23..d94a3ff102ef 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);
 
@@ -1204,19 +1220,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;