From patchwork Fri Apr 5 21:11:43 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: 786535 Received: from mail-vs1-f54.google.com (mail-vs1-f54.google.com [209.85.217.54]) (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 37F981DA32 for ; Fri, 5 Apr 2024 21:11:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712351510; cv=none; b=ETLxujK921rpOuWodTKwXqzsPmUNPmoFUPVMRV/6tFJL6A4ZtH1Kyp4MrJwx6tiAzM1apk4LDD71ytp3rtf4sKyZjMdiyucgsuJlxcejRbcH7j42SO0mhgh8hNSKiwUlyd9Fh1QoZtEubpOLho67JmO0HeIapk8j9ICZETyzBho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712351510; c=relaxed/simple; bh=jc27TBSDxlljRPF07T3OnDVRGhlpgDIJWgSIqWSiYiM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=MR55tUddJ5jycliccdjewy2v5D1xxjphqtQ/TBUU5iliP0sUBFZ4sFWCf9TQMDpIGr8gvMhVSw7Bew3uRsYNF576PxdMihBSwlmuHn8TZqFeljiT1OhL8GVUr8BmkvELU+s28wjgRqhnuErC1UmuNgewHf8IXSsLlt0aqi/eAKk= 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=N3qgATBY; arc=none smtp.client-ip=209.85.217.54 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="N3qgATBY" Received: by mail-vs1-f54.google.com with SMTP id ada2fe7eead31-479e57ff63bso167327137.1 for ; Fri, 05 Apr 2024 14:11:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712351507; x=1712956307; 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=cXA+oiwz2b8EHFOBjQLtlpM7ubcXnMrzwUHSkgQ2sUk=; b=N3qgATBYyUgWjGgwF2CBAr39oKBsVEadn36cNMt9pyU038nguPJP1SgAU/Cx7oEik+ M5DFHnPzHH8Pl7S9f5KIQniYjpXKfKXuhuZfk80WpoFTEqyj3qRcfV/rvPuuWhHmGOiz SI0HQDbySUowVnNYREWBHY0frItIGpx+mNkQ3I5QjWeyTFFCZ9gKl98ZOBcH4vKglQde eQGbFjZX6TwQDmlvRzoENuZPodubII1z3042cYi6wUGtTLaG6Pne09PfydeH8COc4woM odlB7hHHH5GbcEA7Dp+IBCRD5LzR5HR1f3wb4O1Z95UNM0HDpPo1D5tC0bhmyPrg9tXw WIMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712351507; x=1712956307; 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=cXA+oiwz2b8EHFOBjQLtlpM7ubcXnMrzwUHSkgQ2sUk=; b=R+0LzXCDtgEDvjXmI199W6vUA4NFl+4ekdIn+EFVWVKBFzdP2Ic1oaSDYZtBaQYOup 5KjO71+mIDvCrdAb0iJIS5HyOhHZlcS1MqOP6Helx4IACMBTKxSN6EDbFbggjMrU80CD bO7qyYJ6mLa4oDmd78QEFtAwq/hWHduSPySOcgw+FOdX/sjZ/69Eaaf4D6hxr+40xC6b VXKFa3evdk/bI+11eGItioc6eoyK2VJUXM9DupmlZbqbtkm7tQElA7ITvVMkytbRr8dZ nz8odwn7fgbdkfgxfk3MxEoe+zjUKXaEHFX+vqn0akzvAfXyTcoAEcnWxtreQnxyRKRc iLUA== X-Gm-Message-State: AOJu0YyBfbHV3ucNaTYPikRI4YsqyW1by6icA9wOqzeKjjcKACcelXZQ 0dvEQlDu7lQAcmhW+iQeCxQCeG8dCKEhdEr5SJaKaeLxiAz6d7Iaq2oI1qME X-Google-Smtp-Source: AGHT+IHuuYN+3moh7vrMn2KCJiPpqtA0Nw0zCzuJmbXs1NYW8QarrieBc2DHBtISh2CVnW2S785bOA== X-Received: by 2002:a05:6102:4703:b0:478:406a:cbf8 with SMTP id ei3-20020a056102470300b00478406acbf8mr2179966vsb.35.1712351506817; Fri, 05 Apr 2024 14:11:46 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id dx26-20020a056130291a00b007e3d1347195sm306723uab.3.2024.04.05.14.11.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 14:11:46 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v6 1/3] input/device: Fix not handling IdleTimeout when uhid is in use Date: Fri, 5 Apr 2024 17:11:43 -0400 Message-ID: <20240405211145.3463154-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 When uhid is in use IdleTimeout was not taking any effect, this also attempt to force the destroy the input device node to make it useful for users that don't want to keep the input node forever. --- profiles/input/device.c | 86 +++++++++++++++++++++++++++------------ profiles/input/input.conf | 2 +- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/profiles/input/device.c b/profiles/input/device.c index 1b28cdc174b1..b622ee8cd681 100644 --- a/profiles/input/device.c +++ b/profiles/input/device.c @@ -77,6 +77,7 @@ struct input_device { unsigned int report_req_timer; uint32_t report_rsp_id; bool virtual_cable_unplug; + unsigned int idle_timer; }; static int idle_timeout = 0; @@ -139,6 +140,9 @@ static void input_device_free(struct input_device *idev) g_free(idev->req); } + if (idev->idle_timer) + timeout_remove(idev->idle_timer); + if (idev->reconnect_timer > 0) timeout_remove(idev->reconnect_timer); @@ -156,8 +160,54 @@ static void virtual_cable_unplug(struct input_device *idev) idev->virtual_cable_unplug = false; } -static bool hidp_send_message(GIOChannel *chan, uint8_t hdr, - const uint8_t *data, size_t size) +static int uhid_disconnect(struct input_device *idev, bool force) +{ + int err; + + if (!bt_uhid_created(idev->uhid)) + return 0; + + /* Only destroy the node if virtual cable unplug flag has been set */ + if (!idev->virtual_cable_unplug && !force) + return 0; + + bt_uhid_unregister_all(idev->uhid); + + err = bt_uhid_destroy(idev->uhid); + if (err < 0) { + error("bt_uhid_destroy: %s", strerror(-err)); + return err; + } + + return err; +} + +static bool input_device_idle_timeout(gpointer user_data) +{ + struct input_device *idev = user_data; + + idev->idle_timer = 0; + + DBG("path=%s", idev->path); + + uhid_disconnect(idev, true); + connection_disconnect(idev, 0); + + return false; +} + +static void input_device_idle_reset(struct input_device *idev) +{ + timeout_remove(idev->idle_timer); + + if (idle_timeout) + idev->idle_timer = timeout_add_seconds(idle_timeout, + input_device_idle_timeout, idev, + NULL); +} + +static bool hidp_send_message(struct input_device *idev, GIOChannel *chan, + uint8_t hdr, const uint8_t *data, size_t size) { int fd; ssize_t len; @@ -191,6 +241,8 @@ static bool hidp_send_message(GIOChannel *chan, uint8_t hdr, return false; } + input_device_idle_reset(idev); + return true; } @@ -200,13 +252,13 @@ static bool hidp_send_ctrl_message(struct input_device *idev, uint8_t hdr, if (hdr == (HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG)) idev->virtual_cable_unplug = true; - return hidp_send_message(idev->ctrl_io, hdr, data, size); + return hidp_send_message(idev, idev->ctrl_io, hdr, data, size); } static bool hidp_send_intr_message(struct input_device *idev, uint8_t hdr, const uint8_t *data, size_t size) { - return hidp_send_message(idev->intr_io, hdr, data, size); + return hidp_send_message(idev, idev->intr_io, hdr, data, size); } static bool uhid_send_get_report_reply(struct input_device *idev, @@ -297,6 +349,8 @@ static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev) return true; } + input_device_idle_reset(idev); + hdr = data[0]; if (hdr != (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) { DBG("unsupported HIDP protocol header 0x%02x", hdr); @@ -313,28 +367,6 @@ static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev) return true; } -static int uhid_disconnect(struct input_device *idev, bool force) -{ - int err; - - if (!bt_uhid_created(idev->uhid)) - return 0; - - /* Only destroy the node if virtual cable unplug flag has been set */ - if (!idev->virtual_cable_unplug && !force) - return 0; - - bt_uhid_unregister_all(idev->uhid); - - err = bt_uhid_destroy(idev->uhid); - if (err < 0) { - error("bt_uhid_destroy: %s", strerror(-err)); - return err; - } - - return err; -} - static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data) { struct input_device *idev = data; @@ -520,6 +552,8 @@ static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev) return true; } + input_device_idle_reset(idev); + hdr = data[0]; type = hdr & HIDP_HEADER_TRANS_MASK; param = hdr & HIDP_HEADER_PARAM_MASK; diff --git a/profiles/input/input.conf b/profiles/input/input.conf index 00a34eb63de1..fc20c58b6b32 100644 --- a/profiles/input/input.conf +++ b/profiles/input/input.conf @@ -6,7 +6,7 @@ # Set idle timeout (in minutes) before the connection will # be disconnect (defaults to 0 for no timeout) -#IdleTimeout=30 +#IdleTimeout=0 # Enable HID protocol handling in userspace input profile # Defaults to true (Use UHID instead of kernel HIDP) From patchwork Fri Apr 5 21:11:44 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: 786302 Received: from mail-vs1-f54.google.com (mail-vs1-f54.google.com [209.85.217.54]) (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 D18AB174ED2 for ; Fri, 5 Apr 2024 21:11:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712351512; cv=none; b=P07hi2jbOhcylq9uIF3u9AVNNXa3jjcmaq9Obsf3C9HMy18TPLzbtB3mugr5uhOHG2357kdd2kkvEMTjBbP6yDTeP+RbI0iAI6l87rXuMVzoiPruiZxa/7JIxn66V6M2w+Uzlzdp78wwk75IlCZMv2XRzwK9/5TB6gQ+q21VpUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712351512; c=relaxed/simple; bh=oBYiZuw4EXH6CclXvwVXPz++tRo1ZT9usZqsMq50P3o=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tBxO/VbVwaGpO5F0mwhhRLRgkscZRCcpx43mHNOt0YZIwdhvnov2A4NINEne31f0Z89fE+kGpHsGqxpI2vO45KLoyQlMCecOP6YeB2PyZyhGHQwt/yao0Pw1O7GJT3T/BwKept1yRkY8yrEnU2QpE3sEerG36ZqvGdDIHG5oBV0= 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=mY0P1KDy; arc=none smtp.client-ip=209.85.217.54 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="mY0P1KDy" Received: by mail-vs1-f54.google.com with SMTP id ada2fe7eead31-479e857876fso61275137.0 for ; Fri, 05 Apr 2024 14:11:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712351509; x=1712956309; 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=5xhyNVQ9ZSEd+DznykOtJOMpyp8tgw0j029Fgjisiwo=; b=mY0P1KDykpBGGqJNrE01PgSfNXqz2Ad2UUAsAHunmKbjaqIQbuB718rvUKxIiEr8es PrkuBVDwZ7Mzb+mMhzmdbrlw3U6CtX/OAYY8QWele+3w16K/IoGijwZ+s7vqvn7ToNp4 OH5oJxNq8kOKcpj+3zLLKiAWDsKz1soAdC4STuNk0UK0DRFXquudkQLrg6CkcU4IMSad aqnD6fyRES6qQOz4SNObJyX3cbCcJ3N5sSsXg8TpwP9BKTtr7JzjD322JPA+CDoKXQ9N jAKYm/G7ZWdTeYYyEUFfIClvruGE+AO/tSakzXkAyj5813In6bDRfL76PJhTDeELOg0Y fWsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712351509; x=1712956309; 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=5xhyNVQ9ZSEd+DznykOtJOMpyp8tgw0j029Fgjisiwo=; b=XJIcgYcqobFUoG30slpK/oRekBLM/wYPGViYZ4LlveTBCqofvPN0Q/wLSO50qLc0oW DhxyM3+oXdR2X4hV1YmB+4u3gEjXaU6Tw0JmuB3XX7HT1++L7dtik4sjAxmS3z0wNavN nXh8R5+zIByhRsgig/H/JwXZuLmk8PpL70RBWgbz+TTRmz9kyQ6hLgPzE9A7lqmKmZ3R XT682wKk3JqJDjmZr6kSbQ2tuqUgORpbpKx6+8LeE8+uBBrIFW6ps3E0ddUz4zTsfjiN fXfG6YfUKGpFPTEbh4cJ2+/JB+6YJcN35fq2IdhARWivMiRXUX4VSR1q4qbn08L90VB+ /Uyw== X-Gm-Message-State: AOJu0YxMYGBNP81VE0pG/GToP63gLbSgWKENjS6dGi5OGzJhoTfPZPUI W5WbZbefdvwF1dUL+Ox/fkzM7SV3dtNrQDLM6VPQu5OjSCy+zyXVRshY/EV3 X-Google-Smtp-Source: AGHT+IEr7oPWl7tbMdMc8+69RnbZb7N/tA1lD7dYKtCXCpbVXYz6jETZ1M/8TRArlX8bqpACBDVY+Q== X-Received: by 2002:a05:6102:dcc:b0:478:763a:e0cb with SMTP id e12-20020a0561020dcc00b00478763ae0cbmr3410021vst.31.1712351509123; Fri, 05 Apr 2024 14:11:49 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id dx26-20020a056130291a00b007e3d1347195sm306723uab.3.2024.04.05.14.11.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 14:11:47 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v6 2/3] input.conf: Change IdleTimeout unit to be in seconds Date: Fri, 5 Apr 2024 17:11:44 -0400 Message-ID: <20240405211145.3463154-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405211145.3463154-1-luiz.dentz@gmail.com> References: <20240405211145.3463154-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 changes IdleTimeout unit to be in seconds instead of in minutes which offer better granularity. --- profiles/input/input.conf | 5 +++-- profiles/input/manager.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/profiles/input/input.conf b/profiles/input/input.conf index fc20c58b6b32..273e15bc9c80 100644 --- a/profiles/input/input.conf +++ b/profiles/input/input.conf @@ -4,8 +4,9 @@ # particular interface [General] -# Set idle timeout (in minutes) before the connection will -# be disconnect (defaults to 0 for no timeout) +# Set idle timeout (in seconds) before the connection will be disconnect and +# the input device is removed. +# Defaults: 0 (disabled) #IdleTimeout=0 # Enable HID protocol handling in userspace input profile diff --git a/profiles/input/manager.c b/profiles/input/manager.c index 92789a003c89..f4598bcd4e47 100644 --- a/profiles/input/manager.c +++ b/profiles/input/manager.c @@ -89,7 +89,7 @@ static int input_init(void) "IdleTimeout", &err); if (!err) { DBG("input.conf: IdleTimeout=%d", idle_timeout); - input_set_idle_timeout(idle_timeout * 60); + input_set_idle_timeout(idle_timeout); } else g_clear_error(&err); From patchwork Fri Apr 5 21:11:45 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: 786534 Received: from mail-vs1-f47.google.com (mail-vs1-f47.google.com [209.85.217.47]) (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 C72EB174EFF for ; Fri, 5 Apr 2024 21:11:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712351514; cv=none; b=jMFCWO/LS87VwMllkyRGY4U/01pcxsoOwWgabX3WzdnXNj7gfkl2MhnoxrecNShZZ7oNgCuSDyARGlleJ04XJXZzbxFIHpBHLQ60jLTBB6gRMztQpg9bWxhDyQavUKEewBfX03S7uKFkHH2vB6XStICYcBWqs99urM//xgO274U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712351514; c=relaxed/simple; bh=1yzERKIilyRiTFlJHPGsD6mNM+htdmPWv2jvYHpSa5g=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZjHHLY5lHyOEoqvOCTkp6b9RgmSbX+QK+TDrHPOj5D5nvqKWkWasfLgVr8d1U9CG/k6dINgUHhrnnBxKs7mmToA8byMGZVlW+4mIzstyymjD5ZVDOHAeCuEvfdm3PSR6kYrMDlnnJJlV6Wgaxxsq1lt676bCIct3DO8FkXOO/Ec= 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=YU5yYSZy; arc=none smtp.client-ip=209.85.217.47 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="YU5yYSZy" Received: by mail-vs1-f47.google.com with SMTP id ada2fe7eead31-479dbcdba2dso443390137.3 for ; Fri, 05 Apr 2024 14:11:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712351511; x=1712956311; 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=jRkz3xIMUtWDjyEVKXZ0P/9jWENEqXJMLXLfsYwVQmE=; b=YU5yYSZyZP+fPJUekSLxrFWh6Xb+wdNZ4kOV2L9JUowc4L327o8wyveDT/9Jnbb1qN 5OELm7miYXJXzIv+KpD3wcTRuWJ1fcVH6Dn1Dcq4N9tfQdXTr1XZEYTgcZJsmeKNwrVB EDjVMbu0T2rAaNSd7vM6yo/AXpurN53DH6279RmYIPB3ps5UNaDch0oFUe3hBxhqjE5K yWNHsseR0eIBEjEMRHP7DJN7/XvKMDjTfRwPXr77KL/2GxiXN/nQb4RuKsqFMHuQdMfP 9Bwwn5XE69wltNffEvkhVElt673ioUevOh5x4wuQliv6fXOwu7F3bjn9RGtpxGk+pZ5T j8rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712351511; x=1712956311; 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=jRkz3xIMUtWDjyEVKXZ0P/9jWENEqXJMLXLfsYwVQmE=; b=iynJycww9F1NnhjuflqwGrySZJYI/ZiqjurgR5g5nQWhkAYvjVwNmWGuuKt3ieFeLU RVULbQhjxIGmVItMkozt1vg/UdXXgHBvtHHTEyZQvA8bhynoFwIM7F5Uig6QfZHoAjX1 qYQNeY1SylVT7v2qKThTQhTBZuOO3U7hRvL7QNrgj2UzEs8YpGwZPKekw+h89oQsdaJz 0mYjb1kI8JNc+bg5fEm58ADb6fsXGuCnYUD7vcy3iHetJEnhaO83cCvLD6ZZ6QmxbZby IvPRvkZW6rICcAKcykMw/ROvSFRoWqhSLF26XwYRtIA9Ukiw+uelCnNymUKGp44mCtOJ slUQ== X-Gm-Message-State: AOJu0Yw1O9ZlAkZk5Ql7cKgsIQb6C8gnPp7R+uurZiP8fQL8W89K8s55 ccPFr57j/rnInjGaLJKiopwbBo3wdAZjt/5bPNHP6S0QaL/a15f6UDrfdooe X-Google-Smtp-Source: AGHT+IGa9qq9/PeqVcD06wPH0Fm4OYMm3IDZGo8DqnzmpmO6K4uykiM7wiHVdPEtnkZnkhs/xciq9Q== X-Received: by 2002:a05:6102:4189:b0:478:37c4:b20e with SMTP id cd9-20020a056102418900b0047837c4b20emr2751640vsb.20.1712351510986; Fri, 05 Apr 2024 14:11:50 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id dx26-20020a056130291a00b007e3d1347195sm306723uab.3.2024.04.05.14.11.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 14:11:49 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v6 3/3] input/device: Add replay support Date: Fri, 5 Apr 2024 17:11:45 -0400 Message-ID: <20240405211145.3463154-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405211145.3463154-1-luiz.dentz@gmail.com> References: <20240405211145.3463154-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 adds replay support when uhid is in use and the input node is keep while disconnected: Fixes: https://github.com/bluez/bluez/issues/777 --- profiles/input/device.c | 174 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 1 deletion(-) diff --git a/profiles/input/device.c b/profiles/input/device.c index b622ee8cd681..21da16155b0c 100644 --- a/profiles/input/device.c +++ b/profiles/input/device.c @@ -42,6 +42,8 @@ #include "src/sdp-client.h" #include "src/shared/timeout.h" #include "src/shared/uhid.h" +#include "src/shared/util.h" +#include "src/shared/queue.h" #include "device.h" #include "hidp_defs.h" @@ -55,6 +57,19 @@ enum reconnect_mode_t { RECONNECT_ANY }; +struct hidp_msg { + uint8_t hdr; + 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; @@ -78,6 +93,7 @@ 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; @@ -113,8 +129,30 @@ 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); @@ -171,6 +209,10 @@ 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); @@ -246,12 +288,96 @@ 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); } @@ -558,6 +684,12 @@ 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); @@ -575,6 +707,9 @@ 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; } @@ -973,12 +1108,49 @@ 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)) + if (bt_uhid_created(idev->uhid)) { + hidp_replay_init(idev); return 0; + } err = bt_uhid_create(idev->uhid, req->name, &idev->src, &idev->dst, req->vendor, req->product, req->version,