From patchwork Tue Apr 9 20:27:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 787285 Received: from mail-vk1-f173.google.com (mail-vk1-f173.google.com [209.85.221.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDFAA4D9F4 for ; Tue, 9 Apr 2024 20:27:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694446; cv=none; b=AqYcQE82+JuAFWiTXA20LiO2QP03mBlqPSJoWC9pfA2nF33fpQklwHij5howue/HL+PJ+OILhh8Sms6l6PfcVnmlkK9TAaF/TijU+YdpSi2/ZSEYFUaGjb1NeAHUKhZ9LX4O/KZ7UTZ/5FE7SwAVExXsYMlc07Z+SgY4eFF26Zo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694446; c=relaxed/simple; bh=ILElJJf6pzqU4jWqZleD4XVJyNPanZRvKDIV156pH4Y=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=sHfeTXY3heGYWZae/kXuIx4ZziHUOCGMRD3CMIxWb7OciI3CEmhuPbu+ZnIohJFJxpsRANfIwJr3Kb7BuJ2ZNOqL/sVBVEYArGiCnYWWXjL8bt53fjw5QGgPNWzu2+7oKSueO7CR0tI17V/a5MPXpDZfv9PBS7+koJrv6qnwpaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=b+BkuWNu; arc=none smtp.client-ip=209.85.221.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="b+BkuWNu" Received: by mail-vk1-f173.google.com with SMTP id 71dfb90a1353d-4dad331b828so952066e0c.2 for ; Tue, 09 Apr 2024 13:27:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712694442; x=1713299242; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=jb6IJaEcSDhAiBNDodo1z6fcnlHWYbR338+e9/I0VzI=; b=b+BkuWNumZXY6oWJ8hVvsW/YOTPw9/FabIkko45NmKF1XlziGD9HcJU0N2bks9jvFX bcVPb2TQesDKh8czGu8wfyQYNM7v9glitL+h4xPpDmpmiSaG6/j/52KqpfXGhyT1lZ40 GfOInNELKQDrmTZmDHVichhdioLJwGQEhXKtlF5RYSI9RGjjvtpPruoscc709Vq5JJhh l1sCF9OZI34UFGSYl61n1oannY4LOxHv5QUyMiNf1qymGvbaE4KF3FBzo7jrGVkYJO3S 7xwsF4FL5UyHQOQ0RirbMaXwt2ODSoJRYMiwJE5B/GCWs1xbLzRM08w8UeOV23So5bci xicg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712694442; x=1713299242; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jb6IJaEcSDhAiBNDodo1z6fcnlHWYbR338+e9/I0VzI=; b=oIMILv5XN8T2GzO1y2bClvvZK8K89IO2s88n2i3mfXLLI6e+TaH+wLAj6bkypW3RfJ tIqdL09C/9/D+TsW65wmzSneHx3EIs6v3Qtrdng1SUtULKAKe3AQkUw7WCpg0+vl8x85 GuGZDrhLLl292Frn4BSa+K6Pjn9sYSKUlLx+rrengeETlaF7cQ9/kQfM4vrlVZnOQbFL +cWa7pIt+kneAgFRvuU7XQUvRCHuh+VCOuZl4dkZuc3VicQOerEiIsFf/+WC6SNVFyQP 6+AdiZ3tdEmIfM5zgMzz5FjACn3gWM0ei4TJJdELO8JAGNChZe6Ibj8d5869hIra6Q1b qI0A== X-Gm-Message-State: AOJu0YyJEjxrMIfaKI5SIwGpqURSzlTnpn6iA+taIb8mXJAdo3r9MGzV RxwbCoFTGVMwUHjNd8+CR+ipXIbkFSPagmtI6+n53Fwg006DwKidWWqxqxWS X-Google-Smtp-Source: AGHT+IGDQ/4/EB+7wXEdySk8FWEuEePO2YhvWx5zw6ceajGvRyf66jFP4zDH9yEpx8kopTcTVEGT4Q== X-Received: by 2002:a05:6122:1817:b0:4d4:b89:bd2a with SMTP id ay23-20020a056122181700b004d40b89bd2amr1118621vkb.3.1712694442335; Tue, 09 Apr 2024 13:27:22 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id s184-20020a1ff4c1000000b004d42135c509sm1435323vkh.5.2024.04.09.13.27.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 13:27:21 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 1/4] shared/uhid: Add support for bt_uhid_replay Date: Tue, 9 Apr 2024 16:27:17 -0400 Message-ID: <20240409202720.3894858-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz 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 | 125 ++++++++++++++++++++++++++++++++++++++++++++++ src/shared/uhid.h | 1 + 2 files changed, 126 insertions(+) diff --git a/src/shared/uhid.c b/src/shared/uhid.c index 46edb3bfa3ba..1d04dc5a780c 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,44 @@ 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; + + if (uhid->replay && uhid->replay->active) { + if (input) + queue_pop_head(uhid->replay->rin); + else + queue_pop_head(uhid->replay->rout); + + return bt_uhid_replay(uhid); + } + + 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 +154,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 +450,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 +483,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 +511,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); From patchwork Tue Apr 9 20:27:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 787668 Received: from mail-vk1-f173.google.com (mail-vk1-f173.google.com [209.85.221.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B60D8562A for ; Tue, 9 Apr 2024 20:27:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694447; cv=none; b=O7VAziXR4kn96WoYrtm5SdvNFFdFLWxp3C8AF8wTJZjYUoyL1SMah1QVKPb/4UhfJVe6tvh+voV6YYQl7di4/tn23uo7V5vcmlv99bmrs7IvEN96VrpfE8tTg7sEgsrk8yncpe3oO+VBLnRPVjjhWxD/p96VldSY1O9Bou+GwJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694447; c=relaxed/simple; bh=9aJoy5YWJ+2dlXqHyIFDhuX6VFc2cSpxJgETMwfMlRQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eucrLTT/KLkdmSnZvTn9OOA1IUVNcojxZB1yuoeWeA8IZXUMW/tHSA01i7Zz6uQ5wnF9ZAIKY9sDsKyPl9FIJ9cQSHdTLky2fLc30l0gUqb+QuFZTeuuWu4yhMU4E5OCV7T8oirilxbT06VtA6PXI0q5nSbVgIJBKvEXN1TBK5U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Dz7XZZdY; arc=none smtp.client-ip=209.85.221.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Dz7XZZdY" Received: by mail-vk1-f173.google.com with SMTP id 71dfb90a1353d-4daa1be011dso1776562e0c.0 for ; Tue, 09 Apr 2024 13:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712694444; x=1713299244; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Wl+SppwAKGBp8KZCd6/T4Y1i/PcNDdL3WeE9JQ5bmM0=; b=Dz7XZZdYAl8vipvTGJZRIvvub+e51SGzXvoQo5DJ7al2+5P1OrZiXMIZoOHb7XCmuW 70d1bmuczZFaHDsfWez0ENgARuTzNkKRCHCBoKqL0lnQ2j8p/F6VefE6fAY87Nv9HIs+ qmkYZDTzPz6UIogH7VcwIbZfHVSyU9p74+x+3k5NlTULm7LNBNT2eGve7VAFBHzLtcfi wlZWS97aUeIo8zkC2UMB4sFzR96VC1BZpUVHac3boerZ4IgNYDei5+uLDHhiNnvA/HfL 7E9Q0SuPzBPogPibILzX89tSmoyXU4iw5ylII2kMoHiRlHnk8uTtYahayCegCulVyCgv DhUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712694444; x=1713299244; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wl+SppwAKGBp8KZCd6/T4Y1i/PcNDdL3WeE9JQ5bmM0=; b=OYcW1SaQ3BttDcydY8LHCFbfuF28SbSkPkm//A+ZsjujxYK2A0VsBsgvmViXOX7M+g echs2YB0cN9qmzrzwtQUj+6ei/QhrH4k2tERkTTZluOklnjrujDPN5nHpWTJZm+TK9Cx JZavT3bD8qRL5KADhkwMn5P/Fs7cRjgQnlFkaK9a1ZCHCixVuRQJpzkQTWvMa8X96eiK /eO9MFuc5vQSYait6gcZcN0kPgHotV0zP8xc3Hg91Rh1LclIJaizDGWVix4++gjj3GB7 VAfb/JuxLxyHGwt2/0Ynyud5s+cALzo+0TEQSi1nQ2bF5a8lWWQ94vOn9w1pN/JGkJrP RxOQ== X-Gm-Message-State: AOJu0YzkPSBdOGSP+x7Nka/VST91ixfV5AVSJMbLDC3jpt3eQw3zzR+t /YvG4/LErkfgkyP6egV0QW3BLHF3/f059H15LJkYJPkBh+6165VJ5RavceAZ X-Google-Smtp-Source: AGHT+IGPQHRJXl/4cKI9sSsNGbE4o0QeA+aK6U9MZfHHn7I9s+LeaadZSaJYsfnJOKXKu0s3thprTw== X-Received: by 2002:a05:6122:50c:b0:4d3:35ac:3553 with SMTP id x12-20020a056122050c00b004d335ac3553mr1059943vko.10.1712694443999; Tue, 09 Apr 2024 13:27:23 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id s184-20020a1ff4c1000000b004d42135c509sm1435323vkh.5.2024.04.09.13.27.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 13:27:22 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 2/4] hog-lib: Make use of bt_uhid_replay Date: Tue, 9 Apr 2024 16:27:18 -0400 Message-ID: <20240409202720.3894858-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240409202720.3894858-1-luiz.dentz@gmail.com> References: <20240409202720.3894858-1-luiz.dentz@gmail.com> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz 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; } From patchwork Tue Apr 9 20:27:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 787284 Received: from mail-vk1-f173.google.com (mail-vk1-f173.google.com [209.85.221.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25EA015821B for ; Tue, 9 Apr 2024 20:27:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694449; cv=none; b=HrKjfhhsC22maiIgg7R1RzzkOgBopjZ8y8J+qFCiA3XtyH6vFO00fmsu/n8PHjZ7bl90kb83sGRnBiX0pa+5vb6cAhelV7yqCkulx0sz43FStRKQcA1DtOtMi4lsRgjrjp/XB5mHsUzYhNmdySMIIpA+itpuQA3ZHAZW3NOhj54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694449; c=relaxed/simple; bh=Dck4espvsr4r0JfVqykZjMrOuRh8kFc8nrTrXlfaidA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VSfq242U3/hV8SqHYQcKKdagemCOsjX7Sobef6Fy4fAjWfSizYb6Z0KuFmpQ+vy0E/soBhDZHalDsRX8uOrmfQGNmeamjZtFmgK+xLPScTTId+t4wU1tvVpqAYAyrCVmxkWUSlX4p8XS5mJwSZaGVaox95jppYWGuEOhPcoXBJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nDy8xzMq; arc=none smtp.client-ip=209.85.221.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nDy8xzMq" Received: by mail-vk1-f173.google.com with SMTP id 71dfb90a1353d-4dabc6f141bso1134709e0c.2 for ; Tue, 09 Apr 2024 13:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712694446; x=1713299246; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=B4JNlJdEJQVd6pskQVkKzxRA5s2a8Gk65I3u5U2jAkc=; b=nDy8xzMqqNF8LnOnfxUitATE6ZJK5VaSSgoQNj3nXFtcHJcqDsYqKErGbIahw0sVf7 CtFnU9rLLrbPZR4g+exZZk2y9v2z1Z6ZMa9KJkpNQiN/BM8KfqAQD6/M+PzyiLUfkf58 LvtBJsjzfJb+48GTX26n+PMm+iI3RgFHS64TPp2jnW5FFfpeil5Ghl73FINfpsKeohkR a8Knxm1bTEUh9W7u0M5UMKOgqO3utFP/IbiEWCBKSX0sWBYtTaDNczQwAEG0FacBuoNC F44SPdjBbW/PV+MSfZFfDLwGSbAZY8uQ+GH1J1tDZWW788aTKN96HDfqmn2Ry4Yl4qyD zz5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712694446; x=1713299246; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B4JNlJdEJQVd6pskQVkKzxRA5s2a8Gk65I3u5U2jAkc=; b=Nt4aJhzASDytdWCtj8UsEF+gIzsCC8hLRouikzSFAmjFq1E0650eebO0VpqXGdv9+v /XrfRT3QwabRySMNx8W465d+7Hbm53ACvcRYhJMtHzSDtEUAROk3h6/wbWr414oD4xKz 4wxlxV1oQGD2yuf5Hp9QxvWtdhBLqQhCq7io1yGOciWKl9dtxNzOgTd+pmTM2Uhn6/bx Q//8to7iB65Nmsu7PmiWV+EnkNKhed4Alac9+ij/0uKT1qtUzU0gYzIHfysi1bwbX8Tf L6Km1J3Z9IEe5oZGOOhYTAMCAOzoOzA09q862BA7O1PST+wWk1j+FTWIWl1D9RE+8Vte m5Gw== X-Gm-Message-State: AOJu0Yz1gIDgusVD3c+uczbAEZ4cIgalCo0vWcfcubVChplrSuOqcApU AAwO/8d9KP3Qsjf62iE7WVsOaxWXSKlIkKCRiFojkJmwfa0A+fWHih/3a4Wv X-Google-Smtp-Source: AGHT+IFyNRL74wB5X9dlrs6An0bm9+EhB2Fj5jMfC6F2KCCeZRAIoc6E6rCJR/y81LAt8DjNTyKoHw== X-Received: by 2002:a05:6122:446:b0:4d3:cff6:79f0 with SMTP id f6-20020a056122044600b004d3cff679f0mr935047vkk.4.1712694446310; Tue, 09 Apr 2024 13:27:26 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id s184-20020a1ff4c1000000b004d42135c509sm1435323vkh.5.2024.04.09.13.27.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 13:27:24 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 3/4] input/device: Make use of bt_uhid_replay Date: Tue, 9 Apr 2024 16:27:19 -0400 Message-ID: <20240409202720.3894858-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240409202720.3894858-1-luiz.dentz@gmail.com> References: <20240409202720.3894858-1-luiz.dentz@gmail.com> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz This makes use of bt_uhid_replay to replay the GET/SET_REPORT messages recorded as it offer the same functionality as hidp_replay. --- profiles/input/device.c | 169 +--------------------------------------- src/shared/uhid.c | 1 + 2 files changed, 3 insertions(+), 167 deletions(-) diff --git a/profiles/input/device.c b/profiles/input/device.c index 21da16155b0c..2145c2da20f0 100644 --- a/profiles/input/device.c +++ b/profiles/input/device.c @@ -62,14 +62,6 @@ struct hidp_msg { struct iovec *iov; }; -struct hidp_replay { - bool replaying; - struct queue *out; - struct queue *in; - struct queue *re_out; - struct queue *re_in; -}; - struct input_device { struct btd_service *service; struct btd_device *device; @@ -93,7 +85,6 @@ struct input_device { uint32_t report_rsp_id; bool virtual_cable_unplug; unsigned int idle_timer; - struct hidp_replay *replay; }; static int idle_timeout = 0; @@ -129,30 +120,8 @@ static bool input_device_bonded(struct input_device *idev) btd_device_get_bdaddr_type(idev->device)); } -static void hidp_msg_free(void *data) -{ - struct hidp_msg *msg = data; - - util_iov_free(msg->iov, 1); - free(msg); -} - -static void hidp_replay_free(struct hidp_replay *replay) -{ - if (!replay) - return; - - queue_destroy(replay->re_in, NULL); - queue_destroy(replay->in, hidp_msg_free); - queue_destroy(replay->re_out, NULL); - queue_destroy(replay->out, hidp_msg_free); - free(replay); -} - static void input_device_free(struct input_device *idev) { - hidp_replay_free(idev->replay); - bt_uhid_unref(idev->uhid); btd_service_unref(idev->service); btd_device_unref(idev->device); @@ -209,10 +178,6 @@ static int uhid_disconnect(struct input_device *idev, bool force) if (!idev->virtual_cable_unplug && !force) return 0; - /* Destroy replay messages */ - hidp_replay_free(idev->replay); - idev->replay = NULL; - bt_uhid_unregister_all(idev->uhid); err = bt_uhid_destroy(idev->uhid); @@ -288,96 +253,12 @@ static bool hidp_send_message(struct input_device *idev, GIOChannel *chan, return true; } -static void hidp_replay_resend(struct input_device *idev) -{ - struct hidp_msg *msg; - - if (!idev->replay || !idev->replay->replaying) - return; - - msg = queue_pop_head(idev->replay->re_out); - if (!msg) { - DBG("uhid replay finished"); - idev->replay->replaying = false; - return; - } - - if (hidp_send_message(idev, NULL, msg->hdr, msg->iov->iov_base, - msg->iov->iov_len)) - DBG("hdr 0x%02x size %zu", msg->hdr, msg->iov->iov_len); - else - error("uhid replay resend failed"); -} - -static void hidp_replay_recv(struct input_device *idev, uint8_t hdr, - const uint8_t *data, size_t size) -{ - struct hidp_msg *msg; - - if (!idev->replay || !idev->replay->replaying) - return; - - msg = queue_pop_head(idev->replay->re_in); - - if (msg && (msg->hdr != hdr || msg->iov->iov_len != size || - memcmp(msg->iov->iov_base, data, size))) - error("uhid replay input error... discarding"); - - hidp_replay_resend(idev); -} - -static struct hidp_replay *hidp_replay_new(void) -{ - struct hidp_replay *replay = new0(struct hidp_replay, 1); - - replay->out = queue_new(); - replay->in = queue_new(); - - return replay; -} - -static void hidp_record_message(struct input_device *idev, bool out, - uint8_t hdr, const uint8_t *data, size_t size) -{ - struct hidp_msg *msg; - struct iovec iov = { (void *)data, size }; - - /* Only record messages if uhid has been created */ - if (!bt_uhid_created(idev->uhid)) - return; - - if (idev->replay && idev->replay->replaying) { - if (!out) - hidp_replay_recv(idev, hdr, data, size); - return; - } - - if (!idev->replay) - idev->replay = hidp_replay_new(); - - msg = new0(struct hidp_msg, 1); - msg->hdr = hdr; - msg->iov = util_iov_dup(&iov, 1); - - if (out) { - DBG("output[%u]: hdr 0x%02x size %zu", - queue_length(idev->replay->out), hdr, size); - queue_push_tail(idev->replay->out, msg); - } else { - DBG("input[%u]: hdr 0x%02x size %zu", - queue_length(idev->replay->in), hdr, size); - queue_push_tail(idev->replay->in, msg); - } -} - static bool hidp_send_ctrl_message(struct input_device *idev, uint8_t hdr, const uint8_t *data, size_t size) { if (hdr == (HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG)) idev->virtual_cable_unplug = true; - hidp_record_message(idev, true, hdr, data, size); - return hidp_send_message(idev, idev->ctrl_io, hdr, data, size); } @@ -684,12 +565,6 @@ static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev) type = hdr & HIDP_HEADER_TRANS_MASK; param = hdr & HIDP_HEADER_PARAM_MASK; - /* While replaying don't involve the driver since it will likely get - * confused with messages it already things it has received. - */ - if (idev->replay && idev->replay->replaying) - goto done; - switch (type) { case HIDP_TRANS_HANDSHAKE: hidp_recv_ctrl_handshake(idev, param); @@ -707,9 +582,6 @@ static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev) break; } -done: - hidp_record_message(idev, false, hdr, data + 1, len - 1); - return true; } @@ -1108,49 +980,12 @@ static int ioctl_disconnect(struct input_device *idev, uint32_t flags) 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; -} - -static void hidp_replay_init(struct input_device *idev) -{ - if (!idev->replay || idev->replay->replaying) - return; - - idev->replay->replaying = true; - - queue_destroy(idev->replay->re_in, NULL); - idev->replay->re_in = queue_dup(idev->replay->in); - - queue_destroy(idev->replay->re_out, NULL); - idev->replay->re_out = queue_dup(idev->replay->out); - - hidp_replay_resend(idev); -} - static int uhid_connadd(struct input_device *idev, struct hidp_connadd_req *req) { int err; - if (bt_uhid_created(idev->uhid)) { - hidp_replay_init(idev); - return 0; - } + if (bt_uhid_created(idev->uhid)) + return bt_uhid_replay(idev->uhid); err = bt_uhid_create(idev->uhid, req->name, &idev->src, &idev->dst, req->vendor, req->product, req->version, diff --git a/src/shared/uhid.c b/src/shared/uhid.c index 1d04dc5a780c..0a74c6d5c1ae 100644 --- a/src/shared/uhid.c +++ b/src/shared/uhid.c @@ -508,6 +508,7 @@ int bt_uhid_destroy(struct bt_uhid *uhid) return err; uhid->created = false; + uhid_replay_free(uhid->replay); return err; } From patchwork Tue Apr 9 20:27:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 787667 Received: from mail-vk1-f177.google.com (mail-vk1-f177.google.com [209.85.221.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E860615820F for ; Tue, 9 Apr 2024 20:27:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694451; cv=none; b=TyxWXDKZL1mq3u/IWJt2X/wzlJf27/WQI2K5gc7hwbAZzQ+tbVBs9Gw/e3sHAo+5pfDBpgx4yiJpYlnAD6t1oSxTeH6n/M5+rB4++EL6WvR9jZg4iG9LWwVggz56OwtNBDwRJJGA3/oPUK4s/uO92E/d1+56IcOXQzeUz7h7zfA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712694451; c=relaxed/simple; bh=bEtBSs/PzR4Jfnc4B/zXoqmXngHqsX2i2hGZo8/0Ask=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mrPPwexGtH4WC65MTKH0rCKIpEYtvzCwTWME8XxhY19Ny2n/RsOOMJXw1oL0c09M0aqL5U1OB+5DATUWs9GacToVhg1ABV0qXcJR9HGhCcZ5532eO9WXluUEdzcchvV5xZzYc5ittTD7vG+F2ZQdmCoFklDi1utrxf6/Gck+7ho= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZKNEAeAW; arc=none smtp.client-ip=209.85.221.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZKNEAeAW" Received: by mail-vk1-f177.google.com with SMTP id 71dfb90a1353d-4dab344ace5so1115180e0c.3 for ; Tue, 09 Apr 2024 13:27:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712694448; x=1713299248; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=oOH3ubTnedfhHdPof6cho9QbE7XQXN+fhPilDPmv3G4=; b=ZKNEAeAWt0N/WTTWuUoAGcc1YGv9hmv9qWHaLUorCUDUOd34IfLF/kRoS2RrKZhPfl uhT//CEWKmMCcnTf74lHbEGsubkrJDjy7KPVj+IqBll/xTn58SlhKV1BT+Zka/ra03vf 9BGndpggLzC6CmUhH7eg3RAAJwQy9X8Hkjgh0brWzoOmTmfxYHX8oIc0rGi8IjZ4vb+g ncwyw7KvZppuQ35zIvtB0YHvxW6ySIjxGFceDAiaKKhFZl5ez+hHQWSTiiUGcLUxFYaR V9rA+53IWCmrgaCDzZtAjxALaIdtirQs6DTjcjEz2LQim9xCoayijBsvT/cla1TUd5uj m6fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712694448; x=1713299248; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oOH3ubTnedfhHdPof6cho9QbE7XQXN+fhPilDPmv3G4=; b=FWkTpPByiIIbcTJZSRGDQvljxobxWiI8y4DF/pgTQBOiq2mMqertcxl2QRYCzyt84u 1Qg0IzdUlO8A7/gp2mEm6erlaqj4hgZKu+zbn4pkJwthN1rDEF/ErH952PvfncWbRpIh bwK9hw//HptVApl5DMzh+nVTFsQoPNFTLje0CY3YuwXdUhM49LFPh3bbAKGwc1nS8TC9 3KwAgX2pA7yPfXOTNG5Xvguta72P6XRQZl2wp/GaCofvu3vkRaRKGvIFAm5IRJBMXjD3 fHSe89MAvTA/sZVOItuuh1VGAS2708hUY+dSaDLBm61JbDvpvoAvFmP2tnJqnHPzvtpR F8kw== X-Gm-Message-State: AOJu0YxSq8A55JjbX/IlzzF+0uomfVlv9rn4ABF0D9sGI5knyJUzKmj5 YovRujUVf4RaiSn8BCDnlzOxK8xni3JQoNz09c5muD02wvYWF3j7Wervpa9b X-Google-Smtp-Source: AGHT+IH3qAlGZwa0wxhU6wB3vW7r1N+zD027AaQh1WceFA9u568aTKuyFSJ1PUejLuONb1IuSRqXMA== X-Received: by 2002:a05:6122:91e:b0:4da:9d3e:a7df with SMTP id j30-20020a056122091e00b004da9d3ea7dfmr1232097vka.5.1712694448108; Tue, 09 Apr 2024 13:27:28 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id s184-20020a1ff4c1000000b004d42135c509sm1435323vkh.5.2024.04.09.13.27.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 13:27:27 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 4/4] hog-lib: Destroy uHID device if there is traffic while disconnected Date: Tue, 9 Apr 2024 16:27:20 -0400 Message-ID: <20240409202720.3894858-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240409202720.3894858-1-luiz.dentz@gmail.com> References: <20240409202720.3894858-1-luiz.dentz@gmail.com> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz 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 --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;