From patchwork Fri Apr 5 15:04:30 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: 13619165 Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.48]) (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 704D016F840 for ; Fri, 5 Apr 2024 15:04:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712329479; cv=none; b=PYO2KzhiMwcqGEzN4e42gN0R/fVKlY//tL2DWW382SWDrSrUTOgicUZQz7I2i8D3EiJmAVDPj1mk1bJKBfPg2Y8uI5rJykZl0QfcWnnTacyp61Ll0GniRPhZlFhjOPvUm1KY4s+qgZ1wjRYUrFeXWwV0cdGpDAzeBHDlQQb+Urs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712329479; c=relaxed/simple; bh=jc27TBSDxlljRPF07T3OnDVRGhlpgDIJWgSIqWSiYiM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=GLCQJ7w9i37mJKSRBIbQL0WZa0yk/Cp5QHoJ+2VOvMBmzfdR/gXguh9t7dZcZ85BkNNT6UbhvaXTQPlViBblCd8zlHCZnfFLYCUgQSPfvJVzazgFPc8ghMKgakLikZkiHgpkBihST1oDmCnIOHhN2z6Br+XzZMPJ5O0hi0gx8+Y= 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=eQi81saQ; arc=none smtp.client-ip=209.85.160.48 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="eQi81saQ" Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-22ed075a629so458667fac.3 for ; Fri, 05 Apr 2024 08:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712329475; x=1712934275; 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=eQi81saQjriXW/oYkKQaQhJtWgy/BHDnIb6yRnWgioNi3DaFll8tlVGgVuqMy83Zga kBBOm2coGijxNINJANGIQtlBzfYuUJS2Bn/U3jYKslyEy5zbdbRrqLdI8rYKHOSu7HGs eJ0y6IpyWJXBUo5rT9UIjuRhtxw1bN7isMF3ogpqW7ZlIap75UZjASB9pWWo3ygdupQ+ C4rz12Tinu3LSBEL29Vyzx6A24uJVg4l66oCd5/bVhTYXQJrwgtgSttY3ajJhWBrxZd/ AIbaWF8RfMRMy8PkQTD8jfEiIhcknVTUMcKahYvhf3py4luB0Ismm3dcxVaWdZhNCyma hhOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712329475; x=1712934275; 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=KC1elIjkZxokAxqicPplwi4d8ZjULdGqIvyhYGOOPksLDhc6Le/ZtkGFaBScDZ+ON0 VBUo7EW61E5jXFgrfZMKP6t7OjfGNllVm8FcBBDUc8CE2idGWFwkfBVuYMi0Gq4selEJ J7cDTrhZGYMrwBTcW+dD8bNdApEUS7EYJTCKdRhT28216tqUCvYcDBgUFrS8RUfVFR1H CBAoqWn0AKF/a9iKfdcCMwndlfR2ce1zQON4x9a2fE3l3H3ZwAoZfcZbJxV4V+MoVXA0 ZvimnV0noPfC2Gfb30yfMuq0bLkzW1tcRIpb9SnRWRFXZyfLi2hmqeQwq08Q762wpi6x bPHA== X-Gm-Message-State: AOJu0YwkFl11jQiTIRXUUCSJqSXHfEnNH1149Dv+9arjXOx5d54oz0R5 qJLMKh0HB715vn6I8/vmiENJ8rtsVHpp8ctd1yCeZCPWPk5SggeP+jebDnOU X-Google-Smtp-Source: AGHT+IECNMQlklHUetqzvw6wwM7qo8fi7/Ylzg9tlbAefO2i7iDyKES+TSJdiXAcSZdaktaC0VaO4g== X-Received: by 2002:a05:6870:912c:b0:22a:b358:268 with SMTP id o44-20020a056870912c00b0022ab3580268mr2067066oae.25.1712329475116; Fri, 05 Apr 2024 08:04:35 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id l9-20020a9d4c09000000b006e82f65ad4esm306451otf.11.2024.04.05.08.04.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 08:04:34 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v5 1/3] input/device: Fix not handling IdleTimeout when uhid is in use Date: Fri, 5 Apr 2024 11:04:30 -0400 Message-ID: <20240405150432.3360157-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 15:04:31 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: 13619166 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (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 7397016F84B for ; Fri, 5 Apr 2024 15:04:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712329480; cv=none; b=sSIfMoA4P3TpzxmQGK3KRPVvdK/AqJ9BNlT/9hVSrq+CqKbxAjz6JIc+4BXGxIT3iev3UeQGVyGU4ZHfPXXkYm4fqBI6pIGOKDBZGeCmOf3EJflhzPjVko5vJB1mKZE/uUGJb1vdUO++/Y1rRFRET+5RFpurSAgaNLrUaPokr1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712329480; c=relaxed/simple; bh=oBYiZuw4EXH6CclXvwVXPz++tRo1ZT9usZqsMq50P3o=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WybgzgCrxxJF87RqtYPVdPBK7UNdxLGppxEWaluIrNXg5OHV1Ig9QSgGSUsNamhYKP95anaaZSYkA2weyfUZ3tJsiGB7LBbtZ8az5ylxusm7FcQc6eTC8e0AYNeO3rFVGck0PkFV+MXl4xa5FiM9A+8LU0fXVqmObT9Ps5KjVx4= 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=bqFsn7T+; arc=none smtp.client-ip=209.85.160.49 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="bqFsn7T+" Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-22eccfeee22so514711fac.2 for ; Fri, 05 Apr 2024 08:04:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712329477; x=1712934277; 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=bqFsn7T+x/c7hj4ydcRKWIZWemOhgNAhxQHbYomUHam9o0HO3olYNZM+ujX+QSwr5y BXqbg88aH0VU2oDfFiVH1an9NBQcWTaitWYVYELjj2BI3xnoJ0qHcE5foIrQkseulAFP WfmQDYcOazZwufTYTd1GfbHVqdL/YFJh+T0sazf9OUdWpg3AaDfpAMbA9i6RvfqjgCn3 WsSKUZWHMGRsL3KMn9srURdfHpAPGtR5OHh0MtaXJFi+r/c9olzVLdEsrWzuYsmggoF+ mIuXx7wjWdko4RwFF/1+Z+lJnBT/q3jkdX+G1dwngcng5eR6bBKhGzTzUwzrImcG/45t kznQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712329477; x=1712934277; 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=LaXkJQdzIhsSlW+ezknFsdwm7ovFkEEE1vxNGzobBuEvb4+0HZau6xavw87iOQbwdA bK7w7Gz2V8a9NAT7a8htMoblkLEuN+db8LIhH+Krn1ZeYn8CMhL7oXhbgsXO9hVyFfKz djk8my8yEgXBZr0smVqLmKzkQAWZPSBrL/tIlH1UsD23SQBxw8ywuFbJLPS8DAOGSr2G Avqpu+G60mLiDK7POeGa/foR4i5oKOoe6zFdfSKvD5uLi4h+ReyXcUyCpfXKa1Ddgmuf GzQCNzX7IdcGdRn6xw2J/LUQKcIzg3NHLtf+2xXnLjaG9DYE09Bu1Oc+1dR5IeQGSk5P wtWA== X-Gm-Message-State: AOJu0Ywj8LrAwLLKk2iyvMOjQhHT8nMrE7YSLpHAxvuWw2gyhHLHZL3f ZEJmx9rfngliD5KOG3QpejUmS4KLpP/efFFcwER4JTBVFq8oLBelzJ4idtml X-Google-Smtp-Source: AGHT+IF0yySSTadtBjG0tQQE2ifzimgK/US22rZmR7LyQMHy1fVf/GyPxVYLS8pK0WlFjTexBeqUeQ== X-Received: by 2002:a05:6871:3211:b0:22e:c08c:b1cd with SMTP id mo17-20020a056871321100b0022ec08cb1cdmr1407217oac.3.1712329477587; Fri, 05 Apr 2024 08:04:37 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id l9-20020a9d4c09000000b006e82f65ad4esm306451otf.11.2024.04.05.08.04.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 08:04:35 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v5 2/3] input.conf: Change IdleTimeout unit to be in seconds Date: Fri, 5 Apr 2024 11:04:31 -0400 Message-ID: <20240405150432.3360157-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405150432.3360157-1-luiz.dentz@gmail.com> References: <20240405150432.3360157-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 15:04:32 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: 13619167 Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 E008716F84B for ; Fri, 5 Apr 2024 15:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712329483; cv=none; b=D/60OIADdZ0fWR00ez7Wgy2VHh2+tRsL4K4EMo/Iv22nwvy9dtsT2KN4ZVTznkSQ9EmG+iei6JSFVMoGeSikyEaHB4/+UXS9tpzI8buUpuZ/sjrjRBU/w2YIEfz1t6N2aywx68I1bBq4jsyqSqre0/QqVlHMTIXQzDc863qL08U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712329483; c=relaxed/simple; bh=PZedFUojdZGevPqhCkU4fbPhrOFUesMfi8wf2Fzd15o=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KmIMvu18PmMPYwZEtFYMYYzgXFVjbjfA+c8zP3d1lEejlFW1sP7/Ype4oYZyYtlnHz5Go/aQDhoUSxSTAego4DIqSyD4n7mi41whbhI9S76LhEQ+OR2OcRL6gWnxrl8GcwNkyW7JW0/U7YL559L1lQhHTzH8iICzrz+hWmC1lIQ= 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=fRth6ZH3; arc=none smtp.client-ip=209.85.210.46 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="fRth6ZH3" Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6e9eccfc4b7so1073909a34.0 for ; Fri, 05 Apr 2024 08:04:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712329480; x=1712934280; 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=roXnatHc1olblQwivZ+qgo/qtCf91xZF52/uQoJhlnk=; b=fRth6ZH3MWIDZhMGS3eiudBVJFZCt+IENl2goKMHoFvbUfHYleIU3qLNPD+LQVtVsn tyd2TY5t678MIRnP3UNDJqiDtXesIfdRV3A6WwEVvUez4a7guuTwNh+cMorWkP/dI4+u RET11bJlzb649k5nV3sOjE3KH38UxZ/R5SSm7nOnVGa2GsgGvt6L97lIb6086E2zb8SI RAIZeSWEokb5Jmw62bQntiO1YsBQ2gExJ8kbVBYDIcJaHuawik3fEe8W4k1gNuV/xEqc es9154ArOzHrIkNzgVyqpTz+SStHfAv+x75vwVU2r9lAvrlfj3cMj7nuVmwsoWOHqqua nRzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712329480; x=1712934280; 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=roXnatHc1olblQwivZ+qgo/qtCf91xZF52/uQoJhlnk=; b=eq6e2YmfT/Od1NKckeXUp38AnjFYsef+Kd49pQp3Z3JyqGexRcKdErXtrkRjwryMKr 1l3MX1m4wKakUD886Ypcnph77ioC30fjTmXKb5mJvGNOp6EPhkLDsvgzpRlndML6vjoT BJXFMoRQoaEyIAX3kXbwjPO7zVIkGesaXhazSTArXFmN2zKfI8v5QJuIeVfPZEdWr9Bv Z6jNjXC4zbrQXTrmuXBNA/sx3H9X36i0Dt5zSilErhfdJRTFRosY/G1aR2Gd59JEiUz4 9z7OxxADTxBgsozwsFF7y3TwGkQ4jSFiyOnX37b7VWQ9m0li6BQwEkTPjjwrgWhUUI/U juRQ== X-Gm-Message-State: AOJu0Yz0zI+ZodWss7B80SlqZ1RIr6DZeW+K2YtjKkR0Q8DWKch34gQw NQ6fYMfDawAKJ17FvyB0zy3/AaLMgnyh35RxfwyIHwoc/0feqYA//87w8NEc X-Google-Smtp-Source: AGHT+IEHMLrigG8S3HKZp3yTxQPggWw5z6nNfRL73GV6jN8i4/NrLUjyQrFePx1R8/YhZn3+wD00Ug== X-Received: by 2002:a05:6830:217:b0:6e6:c4e0:a2bf with SMTP id em23-20020a056830021700b006e6c4e0a2bfmr1884522otb.24.1712329479741; Fri, 05 Apr 2024 08:04:39 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id l9-20020a9d4c09000000b006e82f65ad4esm306451otf.11.2024.04.05.08.04.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 08:04:38 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v5 3/3] input/device: Add replay support Date: Fri, 5 Apr 2024 11:04:32 -0400 Message-ID: <20240405150432.3360157-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405150432.3360157-1-luiz.dentz@gmail.com> References: <20240405150432.3360157-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 | 173 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 1 deletion(-) diff --git a/profiles/input/device.c b/profiles/input/device.c index b622ee8cd681..4cb4870196dc 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,95 @@ static bool hidp_send_message(struct input_device *idev, GIOChannel *chan, return true; } +static void hidp_replay_send(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; + } + + DBG("hdr 0x%02x size %zu", msg->hdr, msg->iov->iov_len); + + hidp_send_message(idev, NULL, msg->hdr, msg->iov->iov_base, + msg->iov->iov_len); +} + +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_send(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 +683,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 +706,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 +1107,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_send(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,