Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp169852lqj; Wed, 10 Apr 2024 07:18:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWXlOY64g0VgJtaJRCcxLCcSFSO7wJsD2IZ1dBxOg6VCKxaPGy//x5J1qw5WLulLWcL0SvfdL8gYMAHPRpHP4iDAgNdErHVbzQaJIBG7Q== X-Google-Smtp-Source: AGHT+IG4XoawzdcFO+aU7jtchboGYwPD1I03Ho1ey7nkjJ2HPfmw3IlUXJyRTjey5ZTOsS733qe0 X-Received: by 2002:a19:3816:0:b0:515:920e:eedb with SMTP id f22-20020a193816000000b00515920eeedbmr2030244lfa.39.1712758718768; Wed, 10 Apr 2024 07:18:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712758718; cv=pass; d=google.com; s=arc-20160816; b=fS2rStArVo+o+C520vE/toglOWWkTycAGV6HGsgNFI5Z6pWW6kHzAfjQHVklTwyu81 ObF7qR0zuQ64j/HzKnYo5N/u8+MCPVEXQ3eS169//KaV0miL9cRyO1ppQ9fgCEM2c/ic qHHNqVU9sKr5ZgNRp2vAZqSdFOHp0Sp6RTyxU6OmJHvygnbFXvrrC1GOTkABo2had5Jf ROcEldDMv//1cDChPcJ18wiV90zCt47wKgiut5FrY881DLWumjZ9GXDQcbtBKtYnaaqT ckMjAbE1tRA+MKCZgdICPEd6d/lBGtt6jK1+LnFSFiOkpOk2SHvayCq/D81/gwPHqNtZ j0+g== 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=znUCbzAFqqxj2EC3Fo1pRbFzH6GbgNSgZ0IkeF55glU=; fh=4lGQI6056MBa4/oovMyIYaKLn+Wz/24RGvUivMmQP8E=; b=JhT655negb1i9v7yCDBFxVY+21Pvc1dM3ZN1tq3QoZveHdT3QyZCjN3DqDBtByLZ/F kaKGYdP2e9mVlB2FGYJSK2Bi/gc1ZeGI9dMLYnjMbuhETE0fzP0SLKIFD3IsG8RnSY7L W516wVabSxMKbUDNflEWkS8dr9E5W1+LT1mJCs/vOzQlbd9fh35lAhIO4Jwk1RxmsamD 1K1r51NoOJlA/Cf94DfMR1DIELhayQqSIaoMngwDbhKHttSzJDJ3D59sKtze6xI/pAm6 +vq8CpOyhs5NxjQcgpyhhFF0HVA5Dhxqz8nTzviq3GkC7kGfrXFbvoOM1+xthCJ+jVG4 4a2w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=dQvrh8hn; 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-3444-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-3444-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id x2-20020a50ba82000000b0056e47321892si4610685ede.374.2024.04.10.07.18.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 07:18:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-3444-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=dQvrh8hn; 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-3444-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-3444-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 347F81F2A573 for ; Wed, 10 Apr 2024 14:08:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CCEAE179642; Wed, 10 Apr 2024 14:02:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dQvrh8hn" X-Original-To: linux-bluetooth@vger.kernel.org Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (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 6848516EC05 for ; Wed, 10 Apr 2024 14:02:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757732; cv=none; b=l/XOKJLm33wOxv+D8DQ5slxer4nlT5YoOdwNZV9gZxOk+vre7mlnzy2/+B43UytA+yYn4tT9HaXKbH84fs/h/j82WFPPR+7dxTq/fpo8El14eEFxJPPGymVwqtHIbaAAF5ZtCq3jdg7SmMOlKl4iHgnQlgXQywRY7jCRwuEg2fY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757732; c=relaxed/simple; bh=9qEeY8ce9NTsYeXB9XR4V94CfHOjFW4i3Rh4UyztQt0=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=LWlnp7TcR69gjESYfeq54mH/M+J8AWUVm6D+NQyuOVXfR69vwGmKNoVGrguTtXor215lFkbbb2u0GiDWt5HnQd8Eh7WEMZ2ms62TtSLyApKJQmjb/lGqtkYB2b/j0HUOmLgg7deG53QvrSlA3wSTvf+XFFU4B+EEBqjyrB5K8XM= 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=dQvrh8hn; arc=none smtp.client-ip=209.85.210.45 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-ot1-f45.google.com with SMTP id 46e09a7af769-6ea1ef9e4e9so1161193a34.1 for ; Wed, 10 Apr 2024 07:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712757727; x=1713362527; 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=znUCbzAFqqxj2EC3Fo1pRbFzH6GbgNSgZ0IkeF55glU=; b=dQvrh8hn7kO0YcRI1dI2rcZrfcBsRmHu8m9pUmFwO8k40hWC9KQCoihThcIiuCMPsM LEKatxQl/qrpyQJA+vOM3RVX5aG3xsjC6gA+K7PRoirn//oc2Keto+Ovq+ctZAbBs3KF qUL891FnkyfbCANJ7QENSnu7z9ypnePMz+nsa5OAB2PEZOW0LLbq+DQ+BexHKL9B/w4C dTLEliRdAujrDRbm+i/sId+zYSzcrPZMl17OPX1F5SJixh9l7s48WgV//X1bBRgKUlZu BV2sbaj28HumZ6l/EI5Od7hSNRownp6OwW/ZfvNTvc1coX2Aou/nYc8oKJmS6VXQ1bj3 LdlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712757727; x=1713362527; 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=znUCbzAFqqxj2EC3Fo1pRbFzH6GbgNSgZ0IkeF55glU=; b=O0AMEYKtLa2ID+tGnpIn6O+ZUNZ431/J5H8+E72Q8omy4iK61tVp5WfNgkw0cLynWr pmjYI3kYWYTu37SGPO3BNNZCwdcnoAslL0TU40NUlWxuLFoAs0Xjxd/w0t8JQj6mAr6R EQYqbSspnE5vX8XOTGZmUYWnhyyc/oOmgScVKLfA1nLN4pcJU49jFORaN61vV/UQ1PMJ TgTIU93uCJmMy8GGbj4QQZeEHuclQZyAzJnrBwTAz4AQiX9xUyJ4V/FHZvBa+8LlWMlb lwsgvz8+qi8z6wbCANNY9BNDeNeGf+Y9wIU5kwdDqnMNxAL/aMaCub2onW+f7NmN9ZZd bcgA== X-Gm-Message-State: AOJu0Yyy8WUp7Csvki/9/EEsj/exb7T0lIrTSw7gqYEz2UKa+hQsusAa e41LG+cKb58YQdrh408fkH83iIbCgTGdG/HKxrCkkkPH4LMW9uBrLnUc1c+2 X-Received: by 2002:a05:6808:3a96:b0:3c5:e03a:23be with SMTP id fb22-20020a0568083a9600b003c5e03a23bemr2602536oib.42.1712757727271; Wed, 10 Apr 2024 07:02:07 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id k21-20020ab07555000000b007e3199adc5dsm1382051uaq.0.2024.04.10.07.02.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 07:02:06 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v3 1/4] shared/uhid: Add support for bt_uhid_replay Date: Wed, 10 Apr 2024 10:02:02 -0400 Message-ID: <20240410140205.4056047-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 | 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