2020-09-15 17:45:26

by Abhishek Pandit-Subedi

[permalink] [raw]
Subject: [Bluez PATCH v5 0/4] adapter: Reconnect audio when resuming from suspend


Hi Luiz and Marcel,

This is a quality of life improvement for the behavior of audio devices
during system suspend. This depends on a kernel change that emits
suspend/resume events:

https://patchwork.kernel.org/project/bluetooth/list/?series=325771

Right now, audio devices will be disconnected as part of suspend but
won't be reconnected when the system resumes without user interaction.
This is annoying to some users as it causes an interruption to their
normal work flow.

This change reconnects audio devices that were disconnected for suspend
using the following logic:

* In the device disconnect callback, mark any devices with the A2DP
service uuid for reconnect. The reconnect will not be queued until
resume.
* In the controller resume callback, queue any policy items that are
marked to reconnect on resume for connection with the ResumeDelay
value (default = 2s).

A reconnect is queued after the controller resumes and the delay
between resume and reconnect is configurable via the ResumeDelay key in
the Policy settings. The 2s delay was chosen arbitrarily and I think
anywhere up to 10s is probably ok. A longer delay is better to account
for spurious wakeups and Wi-Fi reconnection time (avoiding any co-ex
issues) at the downside of reconnection speed.

Here are the tests I have done with this:
- Single suspend and verified the headphones reconnect
- Suspend stress test for 25 iterations and verify both Wi-Fi and
Bluetooth audio reconnect on resume. (Ran with wake minimum time of
10s)
- Suspend test with wake time = 1s to verify that BT reconnect isn't
attempted. Ran 5 iterations with low wake time and then let it stay
awake to confirm reconnect finally completed on last resume.
- Suspend test with wake time between 1s - 4s. Ran with 5 iterations and
verified it connected several times in the middle and finally at the
end.

I've tested this on a Pixelbook Go (AC-9260 controller) and HP
Chromebook 14a (RTL8822CE controller) with GID6B headset.

I've also tested this with the Pixel Buds 2. These earbuds actually
reconnect automatically to the Chromebook (even without this policy
change) and I verified that the new changes don't break the reconnection
mechanism.

Thanks
Abhishek


Changes in v5:
- Remove use of !! in has_kernel_features

Changes in v4:
- Set reconnect timer in disconnect if resume events aren't supported
- Only set reconnect timer if adapter matches current notification
- Refactor changes in src/adapter to its own commit
- Refactor enabling A2DP_SINK_UUID into its own commit

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

Abhishek Pandit-Subedi (4):
adapter: Refactor kernel feature globals
adapter: Handle controller resume and notify drivers
policy: Enable reconnect for a2dp-sink in defaults
policy: Reconnect audio on controller resume

plugins/policy.c | 87 +++++++++++++++++++++++++++++++++-------
src/adapter.c | 102 +++++++++++++++++++++++++++++++++--------------
src/adapter.h | 11 +++++
src/main.c | 1 +
src/main.conf | 11 ++++-
5 files changed, 168 insertions(+), 44 deletions(-)

--
2.28.0.618.gf4bc123cb7-goog


2020-09-15 17:45:56

by Abhishek Pandit-Subedi

[permalink] [raw]
Subject: [Bluez PATCH v5 3/4] policy: Enable reconnect for a2dp-sink in defaults

Add a2dp-sink to default reconnects list.
---

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

plugins/policy.c | 3 ++-
src/main.conf | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/plugins/policy.c b/plugins/policy.c
index de51e58b9..c18ca8d1f 100644
--- a/plugins/policy.c
+++ b/plugins/policy.c
@@ -65,7 +65,8 @@ struct reconnect_data {
};

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;
diff --git a/src/main.conf b/src/main.conf
index 42f7e41c5..e1d77cc47 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.
--
2.28.0.618.gf4bc123cb7-goog

2020-09-15 18:03:23

by Abhishek Pandit-Subedi

[permalink] [raw]
Subject: [Bluez PATCH v5 1/4] adapter: Refactor kernel feature globals

Move all the kernel specific feature globals into a single
kernel_features bitfield and replace all uses with the bitfield instead.
---

Changes in v5:
- Remove use of !! in has_kernel_features

Changes in v4: None
Changes in v3: None
Changes in v2: None

src/adapter.c | 59 ++++++++++++++++++++++++++-------------------------
src/adapter.h | 9 ++++++++
2 files changed, 39 insertions(+), 29 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 1435e2bd7..88b5202d9 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -116,13 +116,7 @@ static const struct mgmt_blocked_key_info blocked_keys[] = {

static DBusConnection *dbus_conn = NULL;

-static bool kernel_conn_control = false;
-
-static bool kernel_blocked_keys_supported = false;
-
-static bool kernel_set_system_config = false;
-
-static bool kernel_exp_features = false;
+static uint32_t kernel_features = 0;

static GList *adapter_list = NULL;
static unsigned int adapter_remaining = 0;
@@ -678,7 +672,7 @@ static bool set_discoverable(struct btd_adapter *adapter, uint8_t mode,

DBG("sending set mode command for index %u", adapter->dev_id);

- if (kernel_conn_control) {
+ if (has_kernel_features(KERNEL_CONN_CONTROL)) {
if (mode)
set_mode(adapter, MGMT_OP_SET_CONNECTABLE, mode);
else
@@ -1334,7 +1328,7 @@ static void trigger_passive_scanning(struct btd_adapter *adapter)
* no need to start any discovery. The kernel will keep scanning
* as long as devices are in its auto-connection list.
*/
- if (kernel_conn_control)
+ if (has_kernel_features(KERNEL_CONN_CONTROL))
return;

/*
@@ -1385,7 +1379,7 @@ static void stop_passive_scanning_complete(uint8_t status, uint16_t length,
* no need to stop any discovery. The kernel will handle the
* auto-connection by itself.
*/
- if (kernel_conn_control)
+ if (has_kernel_features(KERNEL_CONN_CONTROL))
return;

/*
@@ -2816,7 +2810,7 @@ static void property_set_mode_complete(uint8_t status, uint16_t length,

static void clear_discoverable(struct btd_adapter *adapter)
{
- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return;

if (!(adapter->current_settings & MGMT_SETTING_DISCOVERABLE))
@@ -2876,7 +2870,7 @@ static void property_set_mode(struct btd_adapter *adapter, uint32_t setting,

break;
case MGMT_SETTING_DISCOVERABLE:
- if (kernel_conn_control) {
+ if (has_kernel_features(KERNEL_CONN_CONTROL)) {
if (mode) {
set_mode(adapter, MGMT_OP_SET_CONNECTABLE,
mode);
@@ -4193,7 +4187,8 @@ static void load_default_system_params(struct btd_adapter *adapter)
size_t len = 0;
unsigned int err;

- if (!main_opts.default_params.num_entries || !kernel_set_system_config)
+ if (!main_opts.default_params.num_entries ||
+ !has_kernel_features(KERNEL_SET_SYSTEM_CONFIG))
return;

params = malloc0(sizeof(*params) *
@@ -4878,7 +4873,7 @@ int adapter_connect_list_add(struct btd_adapter *adapter,
* adapter_auto_connect_add() function is used to maintain what to
* connect.
*/
- if (kernel_conn_control)
+ if (has_kernel_features(KERNEL_CONN_CONTROL))
return 0;

if (g_slist_find(adapter->connect_list, device)) {
@@ -4918,7 +4913,7 @@ void adapter_connect_list_remove(struct btd_adapter *adapter,
if (device == adapter->connect_le)
adapter->connect_le = NULL;

- if (kernel_conn_control)
+ if (has_kernel_features(KERNEL_CONN_CONTROL))
return;

if (!g_slist_find(adapter->connect_list, device)) {
@@ -4980,7 +4975,7 @@ void adapter_whitelist_add(struct btd_adapter *adapter, struct btd_device *dev)
{
struct mgmt_cp_add_device cp;

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return;

memset(&cp, 0, sizeof(cp));
@@ -5019,7 +5014,7 @@ void adapter_whitelist_remove(struct btd_adapter *adapter, struct btd_device *de
{
struct mgmt_cp_remove_device cp;

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return;

memset(&cp, 0, sizeof(cp));
@@ -5075,7 +5070,7 @@ void adapter_auto_connect_add(struct btd_adapter *adapter,
uint8_t bdaddr_type;
unsigned int id;

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return;

if (g_slist_find(adapter->connect_list, device)) {
@@ -5147,7 +5142,7 @@ void adapter_set_device_wakeable(struct btd_adapter *adapter,
const bdaddr_t *bdaddr;
uint8_t bdaddr_type;

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return;

bdaddr = device_get_address(device);
@@ -5224,7 +5219,7 @@ void adapter_auto_connect_remove(struct btd_adapter *adapter,
uint8_t bdaddr_type;
unsigned int id;

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return;

if (!g_slist_find(adapter->connect_list, device)) {
@@ -6764,7 +6759,7 @@ connect_le:
* If kernel background scan is used then the kernel is
* responsible for connecting.
*/
- if (kernel_conn_control)
+ if (has_kernel_features(KERNEL_CONN_CONTROL))
return;

/*
@@ -8964,7 +8959,7 @@ static int clear_devices(struct btd_adapter *adapter)
{
struct mgmt_cp_remove_device cp;

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
return 0;

memset(&cp, 0, sizeof(cp));
@@ -9282,7 +9277,7 @@ static void read_info_complete(uint8_t status, uint16_t length,
(missing_settings & MGMT_SETTING_FAST_CONNECTABLE))
set_mode(adapter, MGMT_OP_SET_FAST_CONNECTABLE, 0x01);

- if (kernel_exp_features)
+ if (has_kernel_features(KERNEL_EXP_FEATURES))
read_exp_features(adapter);

err = adapter_register(adapter);
@@ -9403,7 +9398,8 @@ static void read_info_complete(uint8_t status, uint16_t length,

set_name(adapter, btd_adapter_get_name(adapter));

- if (kernel_blocked_keys_supported && !set_blocked_keys(adapter)) {
+ if (has_kernel_features(KERNEL_BLOCKED_KEYS_SUPPORTED) &&
+ !set_blocked_keys(adapter)) {
btd_error(adapter->dev_id,
"Failed to set blocked keys for index %u",
adapter->dev_id);
@@ -9414,7 +9410,7 @@ static void read_info_complete(uint8_t status, uint16_t length,
!(adapter->current_settings & MGMT_SETTING_BONDABLE))
set_mode(adapter, MGMT_OP_SET_BONDABLE, 0x01);

- if (!kernel_conn_control)
+ if (!has_kernel_features(KERNEL_CONN_CONTROL))
set_mode(adapter, MGMT_OP_SET_CONNECTABLE, 0x01);
else if (adapter->current_settings & MGMT_SETTING_CONNECTABLE)
set_mode(adapter, MGMT_OP_SET_CONNECTABLE, 0x00);
@@ -9590,19 +9586,19 @@ static void read_commands_complete(uint8_t status, uint16_t length,
switch (op) {
case MGMT_OP_ADD_DEVICE:
DBG("enabling kernel-side connection control");
- kernel_conn_control = true;
+ kernel_features |= KERNEL_CONN_CONTROL;
break;
case MGMT_OP_SET_BLOCKED_KEYS:
DBG("kernel supports the set_blocked_keys op");
- kernel_blocked_keys_supported = true;
+ kernel_features |= KERNEL_BLOCKED_KEYS_SUPPORTED;
break;
case MGMT_OP_SET_DEF_SYSTEM_CONFIG:
DBG("kernel supports set system confic");
- kernel_set_system_config = true;
+ kernel_features |= KERNEL_SET_SYSTEM_CONFIG;
break;
case MGMT_OP_READ_EXP_FEATURES_INFO:
DBG("kernel supports exp features");
- kernel_exp_features = true;
+ kernel_features |= KERNEL_EXP_FEATURES;
break;
default:
break;
@@ -9768,3 +9764,8 @@ bool btd_le_connect_before_pairing(void)

return false;
}
+
+bool has_kernel_features(uint32_t features)
+{
+ return (kernel_features & features);
+}
diff --git a/src/adapter.h b/src/adapter.h
index f8ac20261..b0ed4915f 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -233,3 +233,12 @@ void btd_adapter_for_each_device(struct btd_adapter *adapter,
void *data);

bool btd_le_connect_before_pairing(void);
+
+enum kernel_features {
+ KERNEL_CONN_CONTROL = 1 << 0,
+ KERNEL_BLOCKED_KEYS_SUPPORTED = 1 << 1,
+ KERNEL_SET_SYSTEM_CONFIG = 1 << 2,
+ KERNEL_EXP_FEATURES = 1 << 3,
+};
+
+bool has_kernel_features(uint32_t feature);
--
2.28.0.618.gf4bc123cb7-goog

2020-09-15 18:21:21

by Abhishek Pandit-Subedi

[permalink] [raw]
Subject: Re: [Bluez PATCH v5 0/4] adapter: Reconnect audio when resuming from suspend

Awesome, thanks!

On Tue, Sep 15, 2020 at 11:05 AM Luiz Augusto von Dentz
<[email protected]> wrote:
>
> Hi Abhishek,
>
> On Tue, Sep 15, 2020 at 10:41 AM Abhishek Pandit-Subedi
> <[email protected]> wrote:
> >
> >
> > Hi Luiz and Marcel,
> >
> > This is a quality of life improvement for the behavior of audio devices
> > during system suspend. This depends on a kernel change that emits
> > suspend/resume events:
> >
> > https://patchwork.kernel.org/project/bluetooth/list/?series=325771
> >
> > Right now, audio devices will be disconnected as part of suspend but
> > won't be reconnected when the system resumes without user interaction.
> > This is annoying to some users as it causes an interruption to their
> > normal work flow.
> >
> > This change reconnects audio devices that were disconnected for suspend
> > using the following logic:
> >
> > * In the device disconnect callback, mark any devices with the A2DP
> > service uuid for reconnect. The reconnect will not be queued until
> > resume.
> > * In the controller resume callback, queue any policy items that are
> > marked to reconnect on resume for connection with the ResumeDelay
> > value (default = 2s).
> >
> > A reconnect is queued after the controller resumes and the delay
> > between resume and reconnect is configurable via the ResumeDelay key in
> > the Policy settings. The 2s delay was chosen arbitrarily and I think
> > anywhere up to 10s is probably ok. A longer delay is better to account
> > for spurious wakeups and Wi-Fi reconnection time (avoiding any co-ex
> > issues) at the downside of reconnection speed.
> >
> > Here are the tests I have done with this:
> > - Single suspend and verified the headphones reconnect
> > - Suspend stress test for 25 iterations and verify both Wi-Fi and
> > Bluetooth audio reconnect on resume. (Ran with wake minimum time of
> > 10s)
> > - Suspend test with wake time = 1s to verify that BT reconnect isn't
> > attempted. Ran 5 iterations with low wake time and then let it stay
> > awake to confirm reconnect finally completed on last resume.
> > - Suspend test with wake time between 1s - 4s. Ran with 5 iterations and
> > verified it connected several times in the middle and finally at the
> > end.
> >
> > I've tested this on a Pixelbook Go (AC-9260 controller) and HP
> > Chromebook 14a (RTL8822CE controller) with GID6B headset.
> >
> > I've also tested this with the Pixel Buds 2. These earbuds actually
> > reconnect automatically to the Chromebook (even without this policy
> > change) and I verified that the new changes don't break the reconnection
> > mechanism.
> >
> > Thanks
> > Abhishek
> >
> >
> > Changes in v5:
> > - Remove use of !! in has_kernel_features
>
> It seems I end up merging the old version by mistake, but I fixed this
> one change myself and added btd_ prefix to it since that is required
> to be exported for plugins that are not built-in.
>
> > Changes in v4:
> > - Set reconnect timer in disconnect if resume events aren't supported
> > - Only set reconnect timer if adapter matches current notification
> > - Refactor changes in src/adapter to its own commit
> > - Refactor enabling A2DP_SINK_UUID into its own commit
> >
> > 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
> >
> > Abhishek Pandit-Subedi (4):
> > adapter: Refactor kernel feature globals
> > adapter: Handle controller resume and notify drivers
> > policy: Enable reconnect for a2dp-sink in defaults
> > policy: Reconnect audio on controller resume
> >
> > plugins/policy.c | 87 +++++++++++++++++++++++++++++++++-------
> > src/adapter.c | 102 +++++++++++++++++++++++++++++++++--------------
> > src/adapter.h | 11 +++++
> > src/main.c | 1 +
> > src/main.conf | 11 ++++-
> > 5 files changed, 168 insertions(+), 44 deletions(-)
> >
> > --
> > 2.28.0.618.gf4bc123cb7-goog
> >
>
>
> --
> Luiz Augusto von Dentz

2020-09-15 18:33:56

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [Bluez PATCH v5 0/4] adapter: Reconnect audio when resuming from suspend

Hi Abhishek,

On Tue, Sep 15, 2020 at 10:41 AM Abhishek Pandit-Subedi
<[email protected]> wrote:
>
>
> Hi Luiz and Marcel,
>
> This is a quality of life improvement for the behavior of audio devices
> during system suspend. This depends on a kernel change that emits
> suspend/resume events:
>
> https://patchwork.kernel.org/project/bluetooth/list/?series=325771
>
> Right now, audio devices will be disconnected as part of suspend but
> won't be reconnected when the system resumes without user interaction.
> This is annoying to some users as it causes an interruption to their
> normal work flow.
>
> This change reconnects audio devices that were disconnected for suspend
> using the following logic:
>
> * In the device disconnect callback, mark any devices with the A2DP
> service uuid for reconnect. The reconnect will not be queued until
> resume.
> * In the controller resume callback, queue any policy items that are
> marked to reconnect on resume for connection with the ResumeDelay
> value (default = 2s).
>
> A reconnect is queued after the controller resumes and the delay
> between resume and reconnect is configurable via the ResumeDelay key in
> the Policy settings. The 2s delay was chosen arbitrarily and I think
> anywhere up to 10s is probably ok. A longer delay is better to account
> for spurious wakeups and Wi-Fi reconnection time (avoiding any co-ex
> issues) at the downside of reconnection speed.
>
> Here are the tests I have done with this:
> - Single suspend and verified the headphones reconnect
> - Suspend stress test for 25 iterations and verify both Wi-Fi and
> Bluetooth audio reconnect on resume. (Ran with wake minimum time of
> 10s)
> - Suspend test with wake time = 1s to verify that BT reconnect isn't
> attempted. Ran 5 iterations with low wake time and then let it stay
> awake to confirm reconnect finally completed on last resume.
> - Suspend test with wake time between 1s - 4s. Ran with 5 iterations and
> verified it connected several times in the middle and finally at the
> end.
>
> I've tested this on a Pixelbook Go (AC-9260 controller) and HP
> Chromebook 14a (RTL8822CE controller) with GID6B headset.
>
> I've also tested this with the Pixel Buds 2. These earbuds actually
> reconnect automatically to the Chromebook (even without this policy
> change) and I verified that the new changes don't break the reconnection
> mechanism.
>
> Thanks
> Abhishek
>
>
> Changes in v5:
> - Remove use of !! in has_kernel_features

It seems I end up merging the old version by mistake, but I fixed this
one change myself and added btd_ prefix to it since that is required
to be exported for plugins that are not built-in.

> Changes in v4:
> - Set reconnect timer in disconnect if resume events aren't supported
> - Only set reconnect timer if adapter matches current notification
> - Refactor changes in src/adapter to its own commit
> - Refactor enabling A2DP_SINK_UUID into its own commit
>
> 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
>
> Abhishek Pandit-Subedi (4):
> adapter: Refactor kernel feature globals
> adapter: Handle controller resume and notify drivers
> policy: Enable reconnect for a2dp-sink in defaults
> policy: Reconnect audio on controller resume
>
> plugins/policy.c | 87 +++++++++++++++++++++++++++++++++-------
> src/adapter.c | 102 +++++++++++++++++++++++++++++++++--------------
> src/adapter.h | 11 +++++
> src/main.c | 1 +
> src/main.conf | 11 ++++-
> 5 files changed, 168 insertions(+), 44 deletions(-)
>
> --
> 2.28.0.618.gf4bc123cb7-goog
>


--
Luiz Augusto von Dentz