From patchwork Mon Apr 8 21:42:51 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: 13621603 Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) (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 32F0911725 for ; Mon, 8 Apr 2024 21:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612580; cv=none; b=R05uD48slph9ygIWh+S2Oqq/LikKTLOyLnd9Eo8QfH0wMJfO2BoeAFFyQQxr24nqckUwFYWDFICICiyjCW8QiW/ECZYOskN5qUTVuLdeuRAgx2ssKzeKfujyP4RZVIns7LQ2tIg/OtqPbg+yDScYawqnMzMfJ/+OAma2t3+ROYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612580; c=relaxed/simple; bh=ILElJJf6pzqU4jWqZleD4XVJyNPanZRvKDIV156pH4Y=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ZLsJRcUj/27CspCCp5i7/oHpBdHOKToSRgq0sgB0eC8Ny+mYHUyw+UL1e0/IO5HaUB+PG/WB2IEEan51JHDAf0hF/O/l46gJ2/XqZipUZCmROx/wR0CN3bFqF3QNKxDFiTG1oDPxyPL6b+Fz/+VjDkB5odahoLZn1JLpERN5HtQ= 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=CzwlyPRY; arc=none smtp.client-ip=209.85.221.181 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="CzwlyPRY" Received: by mail-vk1-f181.google.com with SMTP id 71dfb90a1353d-4dab344ace5so757038e0c.3 for ; Mon, 08 Apr 2024 14:42:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712612577; x=1713217377; 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=CzwlyPRY+h+ABZsZJuZQMigNUw4EurtF7P439XDu8vYs+LWdsJLUyMqEQWAjSwqd38 TaOc5go0c3MthOCrKkot2T8irs1raPsM4WVCuZ1HZVvoEpk1JMH4Nv7vbYXQgkAC/rC5 ngTOUuHS+E7kRPNV44J6ehwnthf5UAuPHTPOtpb15upNRy/KoG0NWm0pEvsIaxQ/FFOI oOZkbtlrb7EFXW3TwRibJBfedmc3gkqmQXJ+QFBlzuy8LJOckmxgiokAWNy5LUIERCZm +NLiP9LQP1fmQTFjZQ1kxeA5cEzsTh/4InqfDFkaulOQfKcEQuKvxiEp9khNz19oW84f gOfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712612577; x=1713217377; 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=Hb38Rughd4GT+EuFgUnQodUae2YqqRHAp1/fjckp5dCItwwzIw3/9OM0b+GCwPNvz8 es5UTtl473B6W9B2VufwxjBvIQJ2Yv0Ui/rYR7p8sb4T9w2d6glhGsbYgm7KMFgHE2+h 8YvaNf+bsVKO65qmRIIZMVnhEhDaElPncq+fwnBZ1ulukxioe2n0kpoLX0nWkAxxujwk NGFEKFjXUYW3h5HDLjUBPzYBAL4q/Jd6KO/W6dKvlflvimCPyFzvlnVcZSUTaisxE6Vh Li53RiYxJdRJy3830JlfhxAnltiS/H9rh4VXAIeHMA9SiC3Mxq4nh5oPKVXo+byjJ5y0 cICQ== X-Gm-Message-State: AOJu0YztYO+OU2yBM5bMTNb27+tk+ZyeDFRfJh5Xgcma/o2bJUIS3KLz BneZlwQtNIRmCabaDEag/NTcJu6XBlebWcIuu8OmFH1f+YEf9gxahAVmNZrz X-Google-Smtp-Source: AGHT+IG/bKdlyK6s3WWMlD+5QPIUxi8bG4PtF+JdvjgILimURc+aCqX6w/9i0pQTaQAB27prOMBPdw== X-Received: by 2002:a05:6122:4d1c:b0:4d8:9541:41a0 with SMTP id fi28-20020a0561224d1c00b004d8954141a0mr7481016vkb.12.1712612576227; Mon, 08 Apr 2024 14:42:56 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id h20-20020ac5c294000000b004ca7514ee1esm1063625vkk.32.2024.04.08.14.42.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 14:42:55 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v1 1/3] shared/uhid: Add support for bt_uhid_replay Date: Mon, 8 Apr 2024 17:42:51 -0400 Message-ID: <20240408214253.3758319-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 Mon Apr 8 21:42:52 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: 13621604 Received: from mail-vk1-f175.google.com (mail-vk1-f175.google.com [209.85.221.175]) (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 728DE1487E9 for ; Mon, 8 Apr 2024 21:43:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612583; cv=none; b=n0RmYOghUNkLB32Y/O9huy739SJp3odfIZ2ppzYL5A+dAZ0p0q4Mf9JmpWBTYW5tcVPfqOOQBEF2bkcoeIKaTCov8XUVu95IpWLFdGjOUqgbFSK2yPb6A4bgxjZQomzGPbsbVPaZ5O/QLZV0dG0/5gPxYUMZK471TeoslcFmsG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612583; c=relaxed/simple; bh=9aJoy5YWJ+2dlXqHyIFDhuX6VFc2cSpxJgETMwfMlRQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DiriGXJgW1Q9t74af+U1uojn9OZdiZBzhL88Lc1Vf8QfLj09fmzr8igiscnMjD/HrrXk+9Okti4cRTz2vJxPJB0ApyNdl+7WOxtPHn/InrYyiaHmjLqsIuYlnAesdzZtZlhM2x/9yUXVXSBgSTJaylptiGdCxS0/p0svnLSPXG4= 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=QHlvgN9g; arc=none smtp.client-ip=209.85.221.175 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="QHlvgN9g" Received: by mail-vk1-f175.google.com with SMTP id 71dfb90a1353d-4daa91c0344so1625368e0c.3 for ; Mon, 08 Apr 2024 14:43:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712612579; x=1713217379; 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=QHlvgN9grb26iyY00oFckwDblYBrxBaF4ZQltPf+XGUNaWTz8La7k5MRfKu+KxZTzK N71+XsWiSymUX9kbgnNdkglZc2JF57Rc1YstfTSixvFW28UllAWPbTLv+4krn8aCLhM9 tgLC5jiAVqu/3PdTnRB/MAfv07dxepKTLZurIznh43lrQ0yFh5k1oSJ6j3doN30wg1FB Vc7ccKXTZcsU4/lW5z6Sp+svnvceUj86NgAn+wExkF6Gl8lzPfUSWi8bBbfSSa7w3FuS 97A8DPLVlmEADXJMnBzURVXhdG9acalC3b6iMnMhUt0oCfIi8zi0UT0kT5isv6FM3Li1 +/2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712612579; x=1713217379; 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=FZ+wBXR4YYbofdyvF45lACaXgcI6t+LrhnIQay4XxQPsxZdDZuwn0XBw6Yn9Kzulel JnkOBYlCQY9A1qzWEUP3XcKZ2FzLxbVbp3amC7sOIC6Y60h0117US8c0Cdih5uyksUJM AT1rpBHWoWxEqcjbp115a+JgyyQhniQYsShi+4Xjp2xhEQJ3x4yTGTxQQcyHZ/q5iQZU lJhsdDg0ciZHEm0LyFdnwDwftDIVDx6tZgrGQC94dWUNUeeBX9i/AOCZS9Ii3UBvZacj xlSk1fpo0TfL2Y8WdYAd2kvKM+c5xBFltOfUjNGWQsgM11PV8R4mrlCNCa4PQFJ4N5wo 42tg== X-Gm-Message-State: AOJu0Ywuk3iR+LrsoGxJn7Pj8kEFnOecY6PmX1FzhzEoamFvgxCwICIo 942vqJF7O9G0auf+kqqU5V7kjwl3V2yLpxwfhvLWc2I5V2z1ClFtUQjG4aRW X-Google-Smtp-Source: AGHT+IGnj44Y4FQRuIa4nwhMBKtP0iWIWuAUbW1iTGxfnyaHyrkPJdI7op4KBn/B/j1lzDmMpBiRAg== X-Received: by 2002:a05:6122:1aa6:b0:4d3:3b1b:aa92 with SMTP id dz6-20020a0561221aa600b004d33b1baa92mr5949955vkb.11.1712612577533; Mon, 08 Apr 2024 14:42:57 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id h20-20020ac5c294000000b004ca7514ee1esm1063625vkk.32.2024.04.08.14.42.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 14:42:56 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v1 2/3] hog-lib: Make use of bt_uhid_replay Date: Mon, 8 Apr 2024 17:42:52 -0400 Message-ID: <20240408214253.3758319-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408214253.3758319-1-luiz.dentz@gmail.com> References: <20240408214253.3758319-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 Mon Apr 8 21:42:53 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: 13621605 Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) (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 8405511725 for ; Mon, 8 Apr 2024 21:43:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612584; cv=none; b=AE7YNegYjx2LFXoSZcZSNTZ01FND0hiNih07odeWd03t5kABFaSgCY9OqKUJyCMnZBpjjh14+i+UaFpTrh7nyyS3vqq83yn3fc0wVZ59CEVzUl/PjxL25ZzFn5TaY5kO2XQeWJkIKC8JggfIew9Ke1zQVBm5u+3Nkw8hbjzZgSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712612584; c=relaxed/simple; bh=Dck4espvsr4r0JfVqykZjMrOuRh8kFc8nrTrXlfaidA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xc5KCpRkbA/taeYfptXCq6lEDwvpxOu3kaM/W0vF5ImIgXJG4H/SCCT+ukNb1s3OrFyWOg1TghUdYdFURtgiTg8PiVM8TcRPIePtH0DfNwh4tlivFFhiwg7cK9UYw+xCnd46JHIwnwRTaoLH/ZWIDrzrKpDJWPFe2evFYRdSD8U= 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=I5QE1dCC; arc=none smtp.client-ip=209.85.221.181 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="I5QE1dCC" Received: by mail-vk1-f181.google.com with SMTP id 71dfb90a1353d-4daa513e430so1446724e0c.2 for ; Mon, 08 Apr 2024 14:43:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712612580; x=1713217380; 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=I5QE1dCCPkYIx4TgmWSQsvBU5il2GSbaxfiO+qj2CeGWujROb7ULXaPpT7VpNijAcH NnqwI6RWLkbNt9bEteK6sAAiUJAJ3JFDDsKI2iyvuZkcEKmgZv9xIoZ7/BSuT4xNk2lu 5GxaDHodco5q8nSxfOg6F1BZD12fn+ex57ZCfxnawr6OnyMVZl69Cf4mqHyogJnP9THb tMldXoI9kyzvmzuOWMnYCIneWtcr+mAL4R0KfOJ+sKz642svBA3lkZpzMMv+fZU6dCiE 6liHwpq1hSRpfeQ/0iXjW3McnwRK2CQuphHRKELrS6nTc0ts4JASwJE1w2nyI3CUJJjr Wtow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712612580; x=1713217380; 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=fast1GtsMhIh48URvlfGLYj3mL7J1+P1485bFE6UnKiWmRvG5hlJH+/Sz0nyayPkjr z8Bxyvp9evtBgWH2FK1F17jdkNTAAI7+xetoODb4jDPLfcKezqfaQQd6bkh/WrRslkV3 w6WPsJ+kfAHpu23499PE0Polxi7Djlg+JUTqKWiRHYW3+5dRwxS5Mk4qxox596sZLJG3 66+xxGOM29h8mEWZrqaEIQl0sJCK8Zeevejvo+KVg+UgJbaS8ZSjBNcqe8fqvD3Cm4ld L7OFitntwuL+bIByShHp5XNu6vvWgSy+ar5M9AVzo7Aaa5S57Rg5WQqaI0My2Z1iKKam VCqA== X-Gm-Message-State: AOJu0Yzpj3fnWrc49eQxnSyxaVp/dDg8xuEtfPP8oeg8jAQNMgnUq2mn t9+Yy5I9RTL3X6y5jf5M9cmFFkPxmJCFAH9GsyZYvtX9p9/SpPE0+UoX61WQ X-Google-Smtp-Source: AGHT+IGJNCT3vpvgO+hi7cE6CrtSAywN11VGaIzhnxKjXVoz6bbKkncfZetzdQ/TnLJyK4YSO2I0dA== X-Received: by 2002:a05:6122:17a6:b0:4d4:872:c68a with SMTP id o38-20020a05612217a600b004d40872c68amr6800080vkf.1.1712612579355; Mon, 08 Apr 2024 14:42:59 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id h20-20020ac5c294000000b004ca7514ee1esm1063625vkk.32.2024.04.08.14.42.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 14:42:57 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v1 3/3] input/device: Make use of bt_uhid_replay Date: Mon, 8 Apr 2024 17:42:53 -0400 Message-ID: <20240408214253.3758319-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408214253.3758319-1-luiz.dentz@gmail.com> References: <20240408214253.3758319-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; }