From patchwork Sat Aug 29 01:02:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Pandit-Subedi X-Patchwork-Id: 11743863 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 19853913 for ; Sat, 29 Aug 2020 01:02:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF83C20CC7 for ; Sat, 29 Aug 2020 01:02:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="J//h6N7G" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726446AbgH2BCZ (ORCPT ); Fri, 28 Aug 2020 21:02:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726392AbgH2BCT (ORCPT ); Fri, 28 Aug 2020 21:02:19 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91DF0C061264 for ; Fri, 28 Aug 2020 18:02:19 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id ls14so326398pjb.3 for ; Fri, 28 Aug 2020 18:02:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3xXRDB2sGPzoGqKOB+6TwO7ShGY5615JtErKHfxz8fk=; b=J//h6N7GjrFtY2260Hs1jViiPMfa9CkX6rEyGHVXoAzr2/ByhHU4ruyy1eUbKUQVnZ pvV5LRMRoC4f9xx4Yw+M4g7zgVgtBPpqpbp80xgkHsUt37xeGn5xaiFcUzSOfqBoGn4F yiEZk6JnQJ5R2rn4J9yOUMV8M78plctwY+Er4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3xXRDB2sGPzoGqKOB+6TwO7ShGY5615JtErKHfxz8fk=; b=StYjNdr4/2g84FdX4crv7bnzHgkYLs2u89SCnIh7rPkqmjxXvnGAF3RntMDr9TL85t rSegutqpJFDJ6F0DIF18ZuxMWsAF886PQqpxLVqAKCiwk/SLTfItPtdAc2cPor/n9otP eBIpYHfJwKwj/+bXYFmN1fVQxM8YSZQBsUrNTqQ5uyruXruem+3KIe9NKvbMg8EM+YIH 8jlloGG/pjNd2OfaDFNByJXP0Cmej324b6jE66lGTy81GstcntiUHhBcVG4ZGnKvpPYM JHea4k6lVqsFKc0MCWhClnbRG3C0djLCiSkO9akbP2A9o93GvisGccAANMYFDu293Mzv XRWg== X-Gm-Message-State: AOAM530nv+7rMkJah9A87ZbCfAC0dAaWrndzc2IKLC9Ih3cFNbgeT1Ti bjmH22/8m4r2aBbkKsqdzmx9z8aelXL3sA== X-Google-Smtp-Source: ABdhPJwNSjzZME0Uxua5lrWAjcNhkGmnvFct4vFvYBJV+jN0jDBnr6vTVdlN6ZSeGmiRa9BYkyexJg== X-Received: by 2002:a17:902:d88c:: with SMTP id b12mr594759plz.96.1598662938763; Fri, 28 Aug 2020 18:02:18 -0700 (PDT) Received: from apsdesk.mtv.corp.google.com ([2620:15c:202:1:7220:84ff:fe09:2b94]) by smtp.gmail.com with ESMTPSA id 78sm630360pfv.200.2020.08.28.18.02.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 18:02:18 -0700 (PDT) From: Abhishek Pandit-Subedi To: luiz.dentz@gmail.com, marcel@holtmann.org Cc: chromeos-bluetooth-upstreaming@chromium.org, linux-bluetooth@vger.kernel.org, Abhishek Pandit-Subedi , Sonny Sasaka Subject: [Bluez PATCH v3 1/3] mgmt: Add controller suspend and resume events Date: Fri, 28 Aug 2020 18:02:08 -0700 Message-Id: <20200828180157.Bluez.v3.1.I14a96397d181666c124e1c413b834428faf3db7a@changeid> X-Mailer: git-send-email 2.28.0.402.g5ffc5be6b7-goog In-Reply-To: <20200829010210.871471-1-abhishekpandit@chromium.org> References: <20200829010210.871471-1-abhishekpandit@chromium.org> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Add the controller suspend and resume events. Reviewed-by: Sonny Sasaka --- Changes in v3: None Changes in v2: None lib/mgmt.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/mgmt.h b/lib/mgmt.h index a800bcab4..46d894ae9 100644 --- a/lib/mgmt.h +++ b/lib/mgmt.h @@ -772,6 +772,7 @@ struct mgmt_ev_device_connected { #define MGMT_DEV_DISCONN_TIMEOUT 0x01 #define MGMT_DEV_DISCONN_LOCAL_HOST 0x02 #define MGMT_DEV_DISCONN_REMOTE 0x03 +#define MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND 0x05 #define MGMT_EV_DEVICE_DISCONNECTED 0x000C struct mgmt_ev_device_disconnected { @@ -959,6 +960,17 @@ struct mgmt_ev_adv_monitor_removed { uint16_t monitor_handle; } __packed; +#define MGMT_EV_CONTROLLER_SUSPEND 0x002d +struct mgmt_ev_controller_suspend { + uint8_t suspend_state; +} __packed; + +#define MGMT_EV_CONTROLLER_RESUME 0x002e +struct mgmt_ev_controller_resume { + struct mgmt_addr_info addr; + uint8_t wake_reason; +} __packed; + static const char *mgmt_op[] = { "<0x0000>", "Read Version", @@ -1088,6 +1100,8 @@ static const char *mgmt_ev[] = { "Device Flags Changed", "Advertisement Monitor Added", /* 0x002b */ "Advertisement Monitor Removed", + "Controller Suspend", + "Controller Resume", }; static const char *mgmt_status[] = { From patchwork Sat Aug 29 01:02:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Pandit-Subedi X-Patchwork-Id: 11743867 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1732D14E5 for ; Sat, 29 Aug 2020 01:02:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F387F2098B for ; Sat, 29 Aug 2020 01:02:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="h8nYGDaF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726395AbgH2BCa (ORCPT ); Fri, 28 Aug 2020 21:02:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726418AbgH2BCW (ORCPT ); Fri, 28 Aug 2020 21:02:22 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4EE0C061232 for ; Fri, 28 Aug 2020 18:02:21 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id p15so431301pli.6 for ; Fri, 28 Aug 2020 18:02:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qCwx0WjM+Tg/OUIJ0l+mpEQceJ2qn7yAxNpmvP8bdS0=; b=h8nYGDaFfEGx5g99mTM6U/CtMtRrpaRC0JKHy1KTPJ5AZCnQcb9uvAB8aIHfyWbbLX ws2b/ATWLNCPLq9/6zQaZHCBlBNI0Y+v1QlPI/ZtZPMFEqBrh5aFCPpRt8s7LCrvI9QH PCX0Gp2Ycg6FRjliqdKkBYAA5rlL3t4ZwLDXQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qCwx0WjM+Tg/OUIJ0l+mpEQceJ2qn7yAxNpmvP8bdS0=; b=rMSYsEZjCwHzXMDJBZ+oFQzOyER6zd8bsxJZttrMI3HAYYvWerdGnoPmjanCEj9XQy 5VfC0fagHxmcQtlazRunR4DmEaoiUFPTAHPuwuH/LVi4YhVph8No/lCJiiOcPqtCmyis Pg1TJnLtBl8nLZjy+A6dv9qDKfjU4RKt5FZp2yTSD7GORG21PVhGWlsAsscPITdfzmLQ 8ZIqKZ6wcTIVhf7fW1CROk7/zUeyZ7lCjEnseGEJL3KpVAI7xjFmpnPx4+K1Z2f3DZBn TfBsi5epihpbU9ri4QvjEBVMyVW0MJrerom8AZkiyhW/S+y8DTe9OjL9la3BUaEAtxpd pV6A== X-Gm-Message-State: AOAM530AlD+ijEE1QiPxXTMJTskMlw2gENF9KaivPriu31d/T9Ux9iVG XBTTlVmRE8LAnSMkbIaL4Sx0GQ== X-Google-Smtp-Source: ABdhPJxp31rxPdZ7f51B3qg3IoieSzyWo5PQRAG9BHSnCS2oHPHFS3gKXjcsP7gro8npPLhJoyHvvg== X-Received: by 2002:a17:90a:b00e:: with SMTP id x14mr1232309pjq.203.1598662939741; Fri, 28 Aug 2020 18:02:19 -0700 (PDT) Received: from apsdesk.mtv.corp.google.com ([2620:15c:202:1:7220:84ff:fe09:2b94]) by smtp.gmail.com with ESMTPSA id 78sm630360pfv.200.2020.08.28.18.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 18:02:19 -0700 (PDT) From: Abhishek Pandit-Subedi To: luiz.dentz@gmail.com, marcel@holtmann.org Cc: chromeos-bluetooth-upstreaming@chromium.org, linux-bluetooth@vger.kernel.org, Abhishek Pandit-Subedi , Sonny Sasaka , Miao-chen Chou Subject: [Bluez PATCH v3 2/3] monitor: Add btmon support for Suspend and Resume events Date: Fri, 28 Aug 2020 18:02:09 -0700 Message-Id: <20200828180157.Bluez.v3.2.Id78b98210807c1326ee2c187af73a325fc97f1e1@changeid> X-Mailer: git-send-email 2.28.0.402.g5ffc5be6b7-goog In-Reply-To: <20200829010210.871471-1-abhishekpandit@chromium.org> References: <20200829010210.871471-1-abhishekpandit@chromium.org> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Add support to pretty print Suspend and Resume mgmt events in btmon. Example: @ MGMT Event: Controller Suspended (0x002d) plen 1 Suspend state: Page scanning and/or passive scanning (2) @ MGMT Event: Controller Resumed (0x002e) plen 8 Wake reason: Remote wake due to peer device connection (2) LE Address: CD:F3:CD:13:C5:9A (OUI CD-F3-CD) Reviewed-by: Sonny Sasaka Reviewed-by: Miao-chen Chou --- Changes in v3: None Changes in v2: None monitor/packet.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/monitor/packet.c b/monitor/packet.c index 431a39b66..451630e04 100644 --- a/monitor/packet.c +++ b/monitor/packet.c @@ -13555,6 +13555,9 @@ static void mgmt_device_disconnected_evt(const void *data, uint16_t size) case 0x04: str = "Connection terminated due to authentication failure"; break; + case 0x05: + str = "Connection terminated by local host for suspend"; + break; default: str = "Reserved"; break; @@ -13782,6 +13785,54 @@ static void mgmt_device_flags_changed_evt(const void *data, uint16_t size) mgmt_print_added_device_flags("Current Flags", current_flags); } +static void mgmt_controller_suspend_evt(const void *data, uint16_t size) +{ + uint8_t state = get_u8(data); + char *str; + + switch (state) { + case 0x0: + str = "Controller running (failed to suspend)"; + break; + case 0x1: + str = "Disconnected and not scanning"; + break; + case 0x2: + str = "Page scanning and/or passive scanning"; + break; + default: + str = "Unknown suspend state"; + break; + } + + print_field("Suspend state: %s (%d)", str, state); +} + +static void mgmt_controller_resume_evt(const void *data, uint16_t size) +{ + uint8_t addr_type = get_u8(data + 6); + uint8_t wake_reason = get_u8(data + 7); + char *str; + + switch (wake_reason) { + case 0x0: + str = "Resume from non-Bluetooth wake source"; + break; + case 0x1: + str = "Wake due to unexpected event"; + break; + case 0x2: + str = "Remote wake due to peer device connection"; + break; + default: + str = "Unknown wake reason"; + break; + } + + print_field("Wake reason: %s (%d)", str, wake_reason); + mgmt_print_address(data, addr_type); +} + static const struct mgmt_data mgmt_event_table[] = { { 0x0001, "Command Complete", mgmt_command_complete_evt, 3, false }, @@ -13863,6 +13914,10 @@ static const struct mgmt_data mgmt_event_table[] = { mgmt_exp_feature_changed_evt, 20, true }, { 0x002a, "Device Flags Changed", mgmt_device_flags_changed_evt, 15, true }, + { 0x002d, "Controller Suspended", + mgmt_controller_suspend_evt, 1, true }, + { 0x002e, "Controller Resumed", + mgmt_controller_resume_evt, 8, true }, { } }; From patchwork Sat Aug 29 01:02:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Pandit-Subedi X-Patchwork-Id: 11743869 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 96F501667 for ; Sat, 29 Aug 2020 01:02:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AB662098B for ; Sat, 29 Aug 2020 01:02:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="FTd0+jLm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726396AbgH2BCb (ORCPT ); Fri, 28 Aug 2020 21:02:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726436AbgH2BCW (ORCPT ); Fri, 28 Aug 2020 21:02:22 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3713C061233 for ; Fri, 28 Aug 2020 18:02:21 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id y6so438183plt.3 for ; Fri, 28 Aug 2020 18:02:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EgVLhgo2q6+uzn37hzuq4exT5rVSGJyUMMk1KlmWH+Y=; b=FTd0+jLmC0VFf0yOezHQ5xCRyo+I07pit0tXKVs9dpf+q5R7LA2uw9zjGO4Azu2lFn A0y1v8tEOacOM+VIOGOrp9VgDAv52Ahf0/nUlBXqzVC+EqobnkdDZ7xjwYR8CmfSMv+K jp3jStLhulHGjipZcoItuOw6N4jPU5rNaVfZk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EgVLhgo2q6+uzn37hzuq4exT5rVSGJyUMMk1KlmWH+Y=; b=NW0etC0vYHg46aONnsnQvO4tAy+x1Sf+EURjOYRjBV/jYFkwsrzad/eLPkbXyszgSE D/hn/OH3jplfQp7YiZ+VElbGPng+DsbQPGvfP236ztooZ/eRvA48jOeDINaiMExxjHS+ bPBrGUzU0yhC2Gu2EsgxXjPvf2+zhGGJNQ5VX2YtyIkF+UhkspUuIA+ZrBmi1htmPq2d oKfUNZ/LSPmlq8O+92IORdA5di0SWzsJqr182ISGNSSlhRSlVQcHHE8rikP8V6YtZbPM z1V9rlcke80xEAszG2u8BAi+LGnYZ1wDefYCZaDppFBaEfksUruMGej8YUwm6Puhxyg4 ZbEQ== X-Gm-Message-State: AOAM53094tdEOlRopRF4z1Ue0pGh2vSIvHc5zCvYSeH1E938sKeSUwZX ZL3ZXeDbFECFy7VG7PTuSWvKSg== X-Google-Smtp-Source: ABdhPJxDHy/Oo0u0LpMqbIu2d36kNLMlYuPkhCKHxJtgyMOETBhMW9Fi+tsZp6A7lXgzkS+dQNApSg== X-Received: by 2002:a17:90a:9483:: with SMTP id s3mr1329325pjo.98.1598662940733; Fri, 28 Aug 2020 18:02:20 -0700 (PDT) Received: from apsdesk.mtv.corp.google.com ([2620:15c:202:1:7220:84ff:fe09:2b94]) by smtp.gmail.com with ESMTPSA id 78sm630360pfv.200.2020.08.28.18.02.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 18:02:20 -0700 (PDT) From: Abhishek Pandit-Subedi To: luiz.dentz@gmail.com, marcel@holtmann.org Cc: chromeos-bluetooth-upstreaming@chromium.org, linux-bluetooth@vger.kernel.org, Abhishek Pandit-Subedi Subject: [Bluez PATCH v3 3/3] policy: Reconnect audio on controller resume Date: Fri, 28 Aug 2020 18:02:10 -0700 Message-Id: <20200828180157.Bluez.v3.3.I26efd89de3a70af1cd9775d457d0c10f4aafd4cb@changeid> X-Mailer: git-send-email 2.28.0.402.g5ffc5be6b7-goog In-Reply-To: <20200829010210.871471-1-abhishekpandit@chromium.org> References: <20200829010210.871471-1-abhishekpandit@chromium.org> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org During system suspend, all peer devices are disconnected. On resume, HID devices will reconnect but audio devices stay disconnected. As a quality of life improvement, mark audio devices that were disconnected due to suspend and attempt to reconnect them when the controller resumes (after a delay for better co-existence with Wi-Fi). --- Changes in v3: - Refactored resume notification to use btd_adapter_driver - Renamed ReconnectAudioDelay to ResumeDelay and set default to 2 - Added A2DP_SINK_UUID to default reconnect list Changes in v2: - Refactored to use policy instead of connecting directly in adapter plugins/policy.c | 80 +++++++++++++++++++++++++++++++++++++++--------- src/adapter.c | 36 ++++++++++++++++++++++ src/adapter.h | 2 ++ src/main.c | 1 + src/main.conf | 11 ++++++- 5 files changed, 115 insertions(+), 15 deletions(-) diff --git a/plugins/policy.c b/plugins/policy.c index de51e58b9..5ab65b991 100644 --- a/plugins/policy.c +++ b/plugins/policy.c @@ -62,10 +62,12 @@ struct reconnect_data { guint timer; bool active; unsigned int attempt; + bool on_resume; }; static const char *default_reconnect[] = { - HSP_AG_UUID, HFP_AG_UUID, A2DP_SOURCE_UUID, NULL }; + HSP_AG_UUID, HFP_AG_UUID, A2DP_SOURCE_UUID, + A2DP_SINK_UUID, NULL }; static char **reconnect_uuids = NULL; static const size_t default_attempts = 7; @@ -75,6 +77,9 @@ static const int default_intervals[] = { 1, 2, 4, 8, 16, 32, 64 }; static int *reconnect_intervals = NULL; static size_t reconnect_intervals_len = 0; +static const int default_resume_delay = 2; +static int resume_delay; + static GSList *reconnects = NULL; static unsigned int service_id = 0; @@ -711,6 +716,9 @@ static gboolean reconnect_timeout(gpointer data) /* Mark the GSource as invalid */ reconnect->timer = 0; + /* Mark any reconnect on resume as handled */ + reconnect->on_resume = false; + err = btd_device_connect_services(reconnect->dev, reconnect->services); if (err < 0) { error("Reconnecting services failed: %s (%d)", @@ -724,14 +732,17 @@ static gboolean reconnect_timeout(gpointer data) return FALSE; } -static void reconnect_set_timer(struct reconnect_data *reconnect) +static void reconnect_set_timer(struct reconnect_data *reconnect, int timeout) { - static int timeout = 0; + static int interval_timeout = 0; reconnect->active = true; if (reconnect->attempt < reconnect_intervals_len) - timeout = reconnect_intervals[reconnect->attempt]; + interval_timeout = reconnect_intervals[reconnect->attempt]; + + if (timeout < 0) + timeout = interval_timeout; DBG("attempt %u/%zu %d seconds", reconnect->attempt + 1, reconnect_attempts, timeout); @@ -743,10 +754,14 @@ static void reconnect_set_timer(struct reconnect_data *reconnect) static void disconnect_cb(struct btd_device *dev, uint8_t reason) { struct reconnect_data *reconnect; + struct btd_service *service; + struct policy_data *data; DBG("reason %u", reason); - if (reason != MGMT_DEV_DISCONN_TIMEOUT) + /* Only attempt reconnect for the following reasons */ + if (reason != MGMT_DEV_DISCONN_TIMEOUT && + reason != MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND) return; reconnect = reconnect_find(dev); @@ -755,10 +770,40 @@ static void disconnect_cb(struct btd_device *dev, uint8_t reason) reconnect_reset(reconnect); - DBG("Device %s identified for auto-reconnection", - device_get_path(dev)); + DBG("Device %s identified for auto-reconnection", device_get_path(dev)); - reconnect_set_timer(reconnect); + switch(reason) + { + case MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND: + if (btd_device_get_service(dev, A2DP_SINK_UUID)) { + reconnect->on_resume = true; + DBG("%s configured to reconnect on resume", + device_get_path(dev)); + } + break; + case MGMT_DEV_DISCONN_TIMEOUT: + reconnect_set_timer(reconnect, -1); + break; + default: + DBG("Developer error. Reason = %d", reason); + break; + } +} + +static void policy_adapter_resume(struct btd_adapter *adapter, + uint8_t wake_reason, const bdaddr_t *addr, + const uint8_t addr_type) +{ + GSList *l; + + /* Check if any devices needed to be reconnected on resume */ + for (l = reconnects; l; l = g_slist_next(l)) { + struct reconnect_data *reconnect = l->data; + + if (reconnect->on_resume) { + reconnect_set_timer(reconnect, resume_delay); + } + } } static void conn_fail_cb(struct btd_device *dev, uint8_t status) @@ -786,14 +831,15 @@ static void conn_fail_cb(struct btd_device *dev, uint8_t status) return; } - reconnect_set_timer(reconnect); + reconnect_set_timer(reconnect, -1); } static int policy_adapter_probe(struct btd_adapter *adapter) { DBG(""); - btd_adapter_restore_powered(adapter); + if (auto_enable) + btd_adapter_restore_powered(adapter); return 0; } @@ -801,6 +847,7 @@ static int policy_adapter_probe(struct btd_adapter *adapter) static struct btd_adapter_driver policy_driver = { .name = "policy", .probe = policy_adapter_probe, + .resume = policy_adapter_resume, }; static int policy_init(void) @@ -854,14 +901,20 @@ static int policy_init(void) auto_enable = g_key_file_get_boolean(conf, "Policy", "AutoEnable", NULL); + resume_delay = g_key_file_get_integer( + conf, "Policy", "ResumeDelay", &gerr); + + if (gerr) { + g_clear_error(&gerr); + resume_delay = default_resume_delay; + } done: if (reconnect_uuids && reconnect_uuids[0] && reconnect_attempts) { btd_add_disconnect_cb(disconnect_cb); btd_add_conn_fail_cb(conn_fail_cb); } - if (auto_enable) - btd_register_adapter_driver(&policy_driver); + btd_register_adapter_driver(&policy_driver); return 0; } @@ -882,8 +935,7 @@ static void policy_exit(void) btd_service_remove_state_cb(service_id); - if (auto_enable) - btd_unregister_adapter_driver(&policy_driver); + btd_unregister_adapter_driver(&policy_driver); } BLUETOOTH_PLUGIN_DEFINE(policy, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, diff --git a/src/adapter.c b/src/adapter.c index 1435e2bd7..bf355bfa7 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -8776,6 +8776,37 @@ static void connected_callback(uint16_t index, uint16_t length, eir_data_free(&eir_data); } +static void controller_resume_notify(struct btd_adapter *adapter, + const uint8_t wake_reason, + const bdaddr_t *addr, + const uint8_t addr_type) +{ + GSList *l; + + for (l = adapter->drivers; l; l = g_slist_next(l)) { + struct btd_adapter_driver *driver = l->data; + if (driver->resume) + driver->resume(adapter, wake_reason, addr, addr_type); + } +} + +static void controller_resume_callback(uint16_t index, uint16_t length, + const void *param, void *user_data) +{ + const struct mgmt_ev_controller_resume *ev = param; + struct btd_adapter *adapter = user_data; + + if (length < sizeof(*ev)) { + btd_error(adapter->dev_id, "Too small device resume event"); + return; + } + + info("Controller resume with wake event 0x%x", ev->wake_reason); + + controller_resume_notify(adapter, ev->wake_reason, &ev->addr.bdaddr, + ev->addr.type); +} + static void device_blocked_callback(uint16_t index, uint16_t length, const void *param, void *user_data) { @@ -9399,6 +9430,11 @@ static void read_info_complete(uint8_t status, uint16_t length, user_passkey_notify_callback, adapter, NULL); + mgmt_register(adapter->mgmt, MGMT_EV_CONTROLLER_RESUME, + adapter->dev_id, + controller_resume_callback, + adapter, NULL); + set_dev_class(adapter); set_name(adapter, btd_adapter_get_name(adapter)); diff --git a/src/adapter.h b/src/adapter.h index f8ac20261..b81ef6a04 100644 --- a/src/adapter.h +++ b/src/adapter.h @@ -113,6 +113,8 @@ struct btd_adapter_driver { const char *name; int (*probe) (struct btd_adapter *adapter); void (*remove) (struct btd_adapter *adapter); + void (*resume)(struct btd_adapter *adapter, uint8_t wake_reason, + const bdaddr_t *addr, const uint8_t addr_type); }; typedef void (*service_auth_cb) (DBusError *derr, void *user_data); diff --git a/src/main.c b/src/main.c index b37c32948..038f867b5 100644 --- a/src/main.c +++ b/src/main.c @@ -131,6 +131,7 @@ static const char *policy_options[] = { "ReconnectAttempts", "ReconnectIntervals", "AutoEnable", + "ResumeDelay", NULL }; diff --git a/src/main.conf b/src/main.conf index 42f7e41c5..9f882e65a 100644 --- a/src/main.conf +++ b/src/main.conf @@ -186,7 +186,7 @@ # timeout). The policy plugin should contain a sane set of values by # default, but this list can be overridden here. By setting the list to # empty the reconnection feature gets disabled. -#ReconnectUUIDs=00001112-0000-1000-8000-00805f9b34fb,0000111f-0000-1000-8000-00805f9b34fb,0000110a-0000-1000-8000-00805f9b34fb +#ReconnectUUIDs=00001112-0000-1000-8000-00805f9b34fb,0000111f-0000-1000-8000-00805f9b34fb,0000110a-0000-1000-8000-00805f9b34fb,0000110b-0000-1000-8000-00805f9b34fb # ReconnectAttempts define the number of attempts to reconnect after a link # lost. Setting the value to 0 disables reconnecting feature. @@ -202,3 +202,12 @@ # This includes adapters present on start as well as adapters that are plugged # in later on. Defaults to 'false'. #AutoEnable=false + +# Audio devices that were disconnected due to suspend will be reconnected on +# resume. ResumeDelay determines the delay between when the controller +# resumes from suspend and a connection attempt is made. A longer delay is +# better for better co-existence with Wi-Fi. +# The value is in seconds. +# Default: 2 +#ResumeDelay = 2 +