Received: by 2002:ab2:687:0:b0:1f4:6588:b3a7 with SMTP id s7csp31274lqe; Tue, 9 Apr 2024 13:27:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW/QGq4LzZsM40LOztcZarfCGV2cD0C4V7wJxj4ud8LdNABFjvh224qz8eOCbda/YYQ9QaYXmpzfsiFmCB0ZOqZwvNy/D5uQVXVo+jf5g== X-Google-Smtp-Source: AGHT+IHxyp5iM6IefgXDhB81FA95GeknE/+VvjbuahSxwHltDpv2/wm6kiq4ho+hZLwMSFpUMg5M X-Received: by 2002:a05:6a00:939a:b0:6ea:c2c7:5d6a with SMTP id ka26-20020a056a00939a00b006eac2c75d6amr873933pfb.2.1712694452874; Tue, 09 Apr 2024 13:27:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712694452; cv=pass; d=google.com; s=arc-20160816; b=Q3iSWa5Nln56ene4pCzRPiK5+m7aZABk5UMUsQD70o95Lv2yj6yL68eZKrogA1plIB vSFBzQ/EmLKNZEp4YiUXuyZZ7pAjrPygkKtOqBUV5VyT8tyFkHKeAd/WTgyimB1lWYFG J71xWrnmZHMZSO1vjLLWh0upajR6TpzcS1p2Sa10CVUvZI7odeGZYV40ze4zH6q0XqTs ZkVcAdeO9bXYOool+f7Skd30VIR4Oi52/aZs5UQ/f9VDU49koSM1iuwXQODK/hotoGS0 jd0svlke+RDU2WShuCGN3JwOIdyJ+5lJ6nk/BEbig56A3M8Bal6c6bTCbcYjFj8l98zv WX6Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from :dkim-signature; bh=jb6IJaEcSDhAiBNDodo1z6fcnlHWYbR338+e9/I0VzI=; fh=4lGQI6056MBa4/oovMyIYaKLn+Wz/24RGvUivMmQP8E=; b=PCqndtF/VMfFTlApPWiD6dxkautFUoc5toPWZMaCUo+sce2OeOuxxXB+kLDr0tVaFk uwTxSI2de6ny3wD+okxZTv8jqFI3gtlC+8XcdIM5+ye7pfNOubEG3vnnho/aFiqNew8g 63JvnlDgdgD8H3g+gqvtWsUEAq0xNSNSNgVOgkssaxLe1j91Kgh3QNASIWVUFwnwmZ/x GVNO4uDNH0G4nemN74MukqlEOub2zQYwjg9/3y7Y/JLq94j0ydy2uDaiXqLFWb74MCZ+ uLqDkLR65v1IV9kn05gKHfmGAENskj2BGTzPU3Szwm5jdA6dVqunc1Xz1+TapRuJBk2Q QAhA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=b+BkuWNu; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-bluetooth+bounces-3411-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-3411-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bv7-20020a632e07000000b005e45abc8c8fsi9337279pgb.306.2024.04.09.13.27.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 13:27:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-3411-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=b+BkuWNu; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-bluetooth+bounces-3411-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-3411-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 574A5282C22 for ; Tue, 9 Apr 2024 20:27:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F05E315820D; Tue, 9 Apr 2024 20:27:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="b+BkuWNu" X-Original-To: linux-bluetooth@vger.kernel.org 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 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-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 Content-Transfer-Encoding: 8bit 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); -- 2.44.0