From patchwork Wed Apr 10 14:02:02 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: 787655 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dQvrh8hn" 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-Google-Smtp-Source: AGHT+IEARmW5oLs+hnd5Hy9a/l8b20theT6ProWHhtL4hqHi83e+YklvD8wqleIUaHJ7uXQWuXpdEQ== 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 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); From patchwork Wed Apr 10 14:02:03 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: 787654 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (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 50F5217964E for ; Wed, 10 Apr 2024 14:02:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757734; cv=none; b=oO26qzbExwwNX8qvPVmsKdA0TBCLfG/voODMQwIKLUmRP2Cr9oZnD52pHKBtFArbuA99aVwW6PlQXWcprotyQ2PupGEDSfL8BTHXUUAN5y4VgeQfC3rYzi5AS6mvrxRez4FTJNLUYBDpBl45S1PdSzMG+IdjejIwUxCYhefmImw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757734; c=relaxed/simple; bh=9aJoy5YWJ+2dlXqHyIFDhuX6VFc2cSpxJgETMwfMlRQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=guldXeQtMpc4b+ns70BqJE/FRgpM2ORKoBKu/Re05dIwexAoOLs2GZ9ak3GqFMUT+2J7jy+NGDUZp0tJlxo27yfNb63k20xTi5ucEpLwy4ZFLP7j1ntfkFlQcyVkATHr1BLYc6ovEcTw8lwrXzmNdrh2h4eZYcVCU4Uac/cea2w= 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=ZQHbNmpH; arc=none smtp.client-ip=209.85.167.169 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="ZQHbNmpH" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3c397193878so3581234b6e.3 for ; Wed, 10 Apr 2024 07:02:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712757731; x=1713362531; 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=ZQHbNmpHLnqJvOc9DNlmcCwedfaU969NfwdjdefhR4rJgc8qyFzc1y9Ve2izyTna5u ytPRT+8sGE+Iykiu4uGG8sKpOmL2L1L67b7+n0ge5+6mn1H+mowx3h7vUOto8qnXCg/G QgVXiOFsAcKpBME6mL6eW7B49WlyReJsPhyZuVnY+AIPCmHvzE8twJFtPFbrb552Ii97 krMB8qmEM5B90e1GlkqIDIro0QXjjfU/6FJw3FIk6pEqK2irYP1eK/olviienQyZpfVL nCr9Vu+FIEKOk1UoMrFenQU4hyyFWyVRuLTfRDYDiQ7S+FP1OKjzRLnzAIgE7WRT9FEp wL9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712757731; x=1713362531; 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=at1UPIjgG/kYxe5T/xSKIFQAjwX778NjZHbY67/nn3aQ0t80VJtKhWGh522H0zLUbo p6jwOdBn3zgeTBrYba/p8Jh+dj1VtxYzRnee86lamiUsUP106wdQzUQfpf7ZsZxu82d8 7ZCic+3xr/vMk54jH4AILfI1ZcSmP4gHWTN8UKqLvBWuUcUt23zoqIShECYV55JvgTfM t3lVpfr8SsGVw8xHlTNW95hakK78VZaTmbXjGqy6v3DWGtCXeI0EwRqBTgIOSLRMBr4T pWAf/4eXBbZjuPZM/nz6TIxf7bIb5PND2zB4Ei+exHPXdcBcZBN7YxYAe2FmLTHOd+eU VC4g== X-Gm-Message-State: AOJu0Yx/TgH44PGIJU73z9HMlhpgUXAkGlqz0fXkLnmj0PNBikXPiMqR 9miYYNQcowf3Mwbsl9eS2CZ597T8djABKs3lqb4zXo0g9JNMpauzY6ufYdRY X-Google-Smtp-Source: AGHT+IHk+D4ytnxOpEO+ZpjXIjS15JmSg5psQ0v70BI+Fq0Ec2j6Bhf0T1AwF/W72mlw9BTFH9lBcQ== X-Received: by 2002:a54:451a:0:b0:3c5:fee2:76b9 with SMTP id l26-20020a54451a000000b003c5fee276b9mr2612941oil.57.1712757729504; Wed, 10 Apr 2024 07:02:09 -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.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 07:02:08 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v3 2/4] hog-lib: Make use of bt_uhid_replay Date: Wed, 10 Apr 2024 10:02:03 -0400 Message-ID: <20240410140205.4056047-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240410140205.4056047-1-luiz.dentz@gmail.com> References: <20240410140205.4056047-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 Wed Apr 10 14:02:04 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: 788025 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (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 D3CA9179644 for ; Wed, 10 Apr 2024 14:02:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757734; cv=none; b=lJEUrrIRxuEL12VUZ++/FgrwAk7p57Zqg/rcP/DDKvhgbbmNyOZRCXXGr2swPvndGYlchJY3e+tufEqNokAmFGBi6VRXq1YjfUmBU/R5yMp9SCqjF2tGZ9caRw0NSs9BNcJunhIDUyHQfn/qZquMA4dNni5FSBFn+HlIJJQdtY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757734; c=relaxed/simple; bh=Eh/QMPyDPIZZIN031rH+qdMfCb7Mj+s6Bjn7dSxtHTg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CsHhNr3cIW3cI+RIEkS8A2B8ey/HGQo9Vi+0u1b3GztOHYnGM3Z1UEAPGv6Kg1FTCg61BnZpHTZT/8OSKcL5CcNz6GMssWmFnWZZociCIcmqUct7i45Aq4bWojpk27ou8WlYkTTpgxiwuhK8ShyOY13pgW0up+V4yvsl+MuZh8U= 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=JJBpgEOI; arc=none smtp.client-ip=209.85.167.179 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="JJBpgEOI" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3c611c787c1so34061b6e.3 for ; Wed, 10 Apr 2024 07:02:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712757731; x=1713362531; 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=PHi/mYCVvH0el8XaLWPn/wMGXvr+8yMQlWG5yX1GsbA=; b=JJBpgEOI+5rW49kxXip4ibCkOApB7FeW7OvtZe8570X8zU7srzZFrH5XJ3Fcwbur8W CJJPMJd5cOqnzffbE8NANzNYtutMP/5a414Uskl6jJUavOQEPwDFm7TAmJ3ojOAhbzog H1nByu9nZzsMxrOTFoY8cvZsEqvLOo8Ormh7EmMhFwN/L7u87QNybr2YbCPLofagMQsA TviUrjobyCTd80mrao7posltJ338cWYD9LguI6RWDiDNO9bpFFTMIzxd7JEtAH5QLOEt NZtEYDi4+Xa+kry8d5sryCU//ZlhJoQQL17/ZDmsoE9doneeIsofUbafDYfK6DpoJX0r 1Byg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712757731; x=1713362531; 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=PHi/mYCVvH0el8XaLWPn/wMGXvr+8yMQlWG5yX1GsbA=; b=dkVlteSpjrxG3hikGdSYRQ7/4PnmMS9KNha1SDWefuX/pC/H4WOTTD7h3VhRDPmC2T GwocSkxfVBFP1PKmosluFSxOyYrWXhsbiRUqjIBXCbeFJ/xpQopH7e/NT5GZqhBkjqMT hURvj1/yA83imrBGMfWFN4r2xJnrjqFSKUDchJEJTnyFwI35lWf0xhypyvnGeQNyUxl8 nbtBz3iCN4zwT8xjgwbQVjG4SVBpM7RmH/WnMqtGEel8KMf1nL5E6mbPBstBT1iwJlfZ LEp/sNUT4HwKLfDG7De+DfaZaPLdRU4ooOwnr72KK/CDsD5HuNukJjd0LfuG+uWmaLN1 +Ojg== X-Gm-Message-State: AOJu0YyKF7aEJkn32MaMxT4ghkhd0IEp7RZI0v2HqQ1IPv3xqr+L1HYK QjLoDzmY+Ea2IzDtKaBoQSg7TQTz/tnoVyASCVA7Ao0ZosotijTemhaocF+b X-Google-Smtp-Source: AGHT+IF3V8P2XyjvF8wa5s9jitkXHsCjC4o+jTCn2WmrvnfaVsaSTRLhveF084uW+6KLR315W3G8ZA== X-Received: by 2002:a05:6808:146:b0:3c5:f303:7b1c with SMTP id h6-20020a056808014600b003c5f3037b1cmr2944613oie.1.1712757731097; Wed, 10 Apr 2024 07:02:11 -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.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 07:02:10 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v3 3/4] input/device: Make use of bt_uhid_replay Date: Wed, 10 Apr 2024 10:02:04 -0400 Message-ID: <20240410140205.4056047-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240410140205.4056047-1-luiz.dentz@gmail.com> References: <20240410140205.4056047-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 690d58d7bd78..c1092b70781b 100644 --- a/src/shared/uhid.c +++ b/src/shared/uhid.c @@ -506,6 +506,7 @@ int bt_uhid_destroy(struct bt_uhid *uhid) return err; uhid->created = false; + uhid_replay_free(uhid->replay); return err; } From patchwork Wed Apr 10 14:02:05 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: 788024 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) (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 F1B6A179206 for ; Wed, 10 Apr 2024 14:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757737; cv=none; b=uskLQoaooXmqCkzgpDIKRlhuLkrRUSZU/AQCVUjKoWqQKzwdGvXsDu0VUnUfud+8gTSR27QobEKQ1yK14dmWYXaSeoBuXN96Wa2Ur8ubvX6zXa4t6eRxWOR3hL6q1r8Rv7Oh8D8yHlDVLDKu0o20sw6wmf4GJbfP391+RYFIr80= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712757737; c=relaxed/simple; bh=ODgoe/SbsjLJwPOVANXXfSxVfs9NajiC5bhbWaRZ5FY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jbUHIqngC1p+J0QjodyU2NsaqObDTzGyJV1hhuPEHSt7COarDGMqH23+wGWdlsm4mvIl2+ixbaFrDeXsTFcEtkgxsKkx/IlDn0QNirxmi5+EhkTYjhEuZ8iTUvno/8PIwaaJ5Oi6tkV5Ko7P8FHEpyRsuuas7smyv2fTLF4fpiA= 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=aifgxLIj; arc=none smtp.client-ip=209.85.219.53 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="aifgxLIj" Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-69b21b11173so19282796d6.1 for ; Wed, 10 Apr 2024 07:02:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712757734; x=1713362534; 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=OlPPoFA4kIS8orhrFOQTgWYnFKgrAIT9g1HLOJOhjiM=; b=aifgxLIj7ITiVM+VuVgTdH1nWxnWsiMgYAlNSYXxqbqzrHLVcIz0uUT6dZB+OY54ZB UoK1mEjnrWWxru6WRltwoBtrnA1iVg3wOAni1pq1WtYFJAESiNyY/PLdCyHlgCPd3gat bLqHvBPI35ewi/owHgr2g+1Wf7lZ17hfDB7NFy+sW+DZh93gW+c8wAXut51XC1T+Asqs uh0mDQlnfyuUqIL3MZsw8fuUES2MtXq+ekypuA4QCY/qVlQUf9/W/2iNQgqEy5iiD6OP XYgSYDnGGkIwVZ/3FfyjtaEHZCdQP85fDdKp+nzKBHPgh4rWu7IiVXQjD37RL8elXbga UVGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712757734; x=1713362534; 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=OlPPoFA4kIS8orhrFOQTgWYnFKgrAIT9g1HLOJOhjiM=; b=stbma72cqdMuV9uD96aiIyKQvhaC1ZGs/b2WiCAsUcsnsEYJEdpHfIqCeGHIpXvED3 b3XtLJN33GqFyevL24uVEujwBJK1iT9MCXMmuUmLKwDxQTtL/mq2VGDQDuL6TltpcyNs gGavLGD6+sCGQi3q3Y78UIuBlJn9BKUtHd/8SeOWqpqHmyuIMKLOPaeIECu0w/qG0hML nMYVLzRTxC3Ya4zUtR9DbF9TQfGSrE/f8nIfXqtgaTdhAf0kTn6Fe9TyZw+1TmDfdFE3 /+V3E1Lxzq7JqETJ7nVE8OvyDXiF96SrqoydN8FPvJIGE1o2iFmbIchPfJHzmaF1q6n4 xkgg== X-Gm-Message-State: AOJu0YyugQxmUv9kKuUF57f7Jvn9LtqySCzl8SkscjQMBUp6hdzhry6h +eMSf4Ge/ASuAEgs8Fur7M44NmV3SgXr/eCRLnZN/IJ2CSGgICleMv7Vw7eo X-Google-Smtp-Source: AGHT+IEdRxxsP3VhyPQU+9/UdxzrWNLGKtfLEdVW1YioWPKVCm0Vdxp7H0Sj6XLGghx1WT8rtPL5vA== X-Received: by 2002:a05:6214:108e:b0:69b:4341:6477 with SMTP id o14-20020a056214108e00b0069b43416477mr1099740qvr.26.1712757733896; Wed, 10 Apr 2024 07:02:13 -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.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 07:02:11 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v3 4/4] hog-lib: Destroy uHID device if there is traffic while disconnected Date: Wed, 10 Apr 2024 10:02:05 -0400 Message-ID: <20240410140205.4056047-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240410140205.4056047-1-luiz.dentz@gmail.com> References: <20240410140205.4056047-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 | 47 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c index 0291adb6eb23..2d8d0f359e57 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); @@ -928,6 +944,14 @@ static void get_report(struct uhid_event *ev, void *user_data) struct report *report; guint8 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->getrep_att) { g_attrib_cancel(hog->attrib, hog->getrep_att); @@ -1204,19 +1228,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;