From: "Paweł Owoc" <frut3k7@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: "Paweł Owoc" <frut3k7@gmail.com>
Subject: [PATCH v3 1/1] device: Add per-device bearer restrictions
Date: Sun, 29 Nov 2026 07:34:44 +0100 [thread overview]
Message-ID: <20261129063444.120414-2-frut3k7@gmail.com> (raw)
In-Reply-To: <20261129063444.120414-1-frut3k7@gmail.com>
Add the experimental AllowedBearers property to restrict a
dual-mode device to BR/EDR, LE, or both bearers.
Persist the policy, enforce it alongside controller mode and device
capabilities, and expose it through bluetoothctl. Add functional tests
and documentation.
Assisted-by: GitHub-Copilot:gpt-6-luna
---
client/main.c | 33 ++
doc/bluetoothctl.rst | 21 ++
doc/functional-bearer.rst | 49 +++
doc/org.bluez.Device.rst | 25 +-
doc/settings-storage.txt | 1 +
src/adapter.c | 12 +-
src/bearer.c | 9 +
src/device.c | 535 +++++++++++++++++++++++++++++----
src/device.h | 1 +
test/functional/test_bearer.py | 331 ++++++++++++++++++++
10 files changed, 951 insertions(+), 66 deletions(-)
diff --git a/client/main.c b/client/main.c
index 1131c2ae3..57740e22c 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2059,6 +2059,7 @@ static void cmd_info(int argc, char *argv[])
print_property(proxy, "AdvertisingFlags");
print_property(proxy, "AdvertisingData");
print_property(proxy, "Sets");
+ print_property(proxy, "AllowedBearers");
print_property(proxy, "PreferredBearer");
battery_proxy = find_proxies_by_path(battery_proxies,
@@ -2644,6 +2645,36 @@ static void cmd_bearer(int argc, char *argv[])
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
+static void cmd_allowed_bearers(int argc, char *argv[])
+{
+ GDBusProxy *proxy;
+ char *str;
+
+ proxy = find_device(argc, argv);
+ if (!proxy)
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+ if (argc <= 2) {
+ print_property(proxy, "AllowedBearers");
+ return;
+ }
+
+ if (strcmp(argv[2], "dual") && strcmp(argv[2], "bredr") &&
+ strcmp(argv[2], "le")) {
+ bt_shell_printf("Invalid allowed bearers mode: %s\n", argv[2]);
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+ }
+
+ str = strdup(argv[2]);
+
+ if (g_dbus_proxy_set_property_basic(proxy, "AllowedBearers",
+ DBUS_TYPE_STRING, &str,
+ generic_callback, str, free))
+ return;
+
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
static void cmd_list_attributes(int argc, char *argv[])
{
GDBusProxy *proxy;
@@ -4161,6 +4192,8 @@ static const struct bt_shell_menu main_menu = {
dev_generator },
{ "bearer", "<dev> [last-seen/bredr/le]", cmd_bearer,
"Get/Set preferred bearer", dev_generator },
+ { "allowed-bearers", "<dev> [dual/bredr/le]", cmd_allowed_bearers,
+ "Get/Set allowed bearers", dev_generator },
{ } },
};
diff --git a/doc/bluetoothctl.rst b/doc/bluetoothctl.rst
index 23f92a512..d9e9a5f04 100644
--- a/doc/bluetoothctl.rst
+++ b/doc/bluetoothctl.rst
@@ -524,6 +524,27 @@ Get/Set preferred bearer.
| [CHG] Device <addr> PreferredBearer: bredr
| Changing bredr succeeded
+allowed-bearers
+---------------
+
+Get/Set the bearers allowed for a dual-mode device.
+
+:Usage: **> allowed-bearers <dev> [dual/bredr/le]**
+:Uses: **org.bluez.Device(5)** property **AllowedBearers**
+:<dev>: Device MAC address (XX:XX:XX:XX:XX:XX) or object path (required)
+:[dual/bredr/le]: "dual" allows BR/EDR and LE, "bredr" allows BR/EDR only, and "le" allows LE only (optional)
+:Example get allowed bearers:
+ | > allowed-bearers <addr>
+ | AllowedBearers: dual
+:Example allow only LE:
+ | > allowed-bearers <addr> le
+ | [CHG] Device <addr> AllowedBearers: le
+ | Changing le succeeded
+:Example allow only BR/EDR:
+ | > allowed-bearers <addr> bredr
+ | [CHG] Device <addr> AllowedBearers: bredr
+ | Changing bredr succeeded
+
Advertise Submenu
=================
diff --git a/doc/functional-bearer.rst b/doc/functional-bearer.rst
index 8331a2a6d..0a65a7cfd 100644
--- a/doc/functional-bearer.rst
+++ b/doc/functional-bearer.rst
@@ -181,3 +181,52 @@ test_bearer_bredr_pair
:Notes: The ACL link is terminated a short time after the profiles are
disconnected, so the ``Disconnected`` signal follows the reply to
``Disconnect``.
+
+test_bearer_allowed_bearers
+----------------------------
+
+:Setup: As for test_bearer_bredr_pair, with
+ ``Experimental = true`` enabled on both hosts.
+
+:Steps:
+ 1. Host0 pairs with host1 over BR/EDR.
+ 2. Host0 reads ``AllowedBearers`` and sets it to ``le``, ``bredr``,
+ and ``dual`` in turn.
+ 3. Host0 attempts to set ``AllowedBearers`` to ``invalid``.
+
+:Expected:
+ 1. ``Pair`` replies and both hosts have dual-mode bearer objects.
+ 2. ``AllowedBearers`` reports each successfully selected mode. Selecting
+ ``le`` removes the BR/EDR bearer interface and selecting ``bredr``
+ removes the LE bearer interface.
+ 3. The invalid value is rejected with
+ ``org.bluez.Error.InvalidArguments`` and the previous valid mode is
+ unchanged.
+
+test_bearer_allowed_bearers_persistence
+---------------------------------------
+
+:Setup: As above, with host0 using a restartable **bluetoothd(8)** and
+ ``Experimental = true`` enabled.
+
+:Steps:
+ 1. Host0 pairs with host1 over BR/EDR and sets ``AllowedBearers`` to
+ ``le``.
+ 2. Host0 restarts **bluetoothd(8)** using the same state directory.
+ 3. Host0 removes the stored ``AllowedBearers`` value and restarts
+ **bluetoothd(8)** again.
+
+:Expected:
+ 1. After the first restart, ``AllowedBearers`` remains ``le``, the
+ BR/EDR bearer interface is unavailable, and a BR/EDR connection
+ attempt fails.
+ 2. When no value is stored, ``AllowedBearers`` defaults to ``dual``,
+ the BR/EDR bearer interface is available, and a BR/EDR connection
+ succeeds.
+
+The LE-only controller mode test verifies that the per-device policy cannot
+enable BR/EDR. In BR/EDR-only mode, a newly discovered BR/EDR peer is not known
+to support LE, so ``AllowedBearers`` is not exposed for that device. A bearer
+is available only if it is enabled by both the controller mode and the device's
+supported bearers; this can leave both bearers available when the controller
+supports them and the device supports a dual-mode configuration.
diff --git a/doc/org.bluez.Device.rst b/doc/org.bluez.Device.rst
index f3e4bfa4a..988b4d120 100644
--- a/doc/org.bluez.Device.rst
+++ b/doc/org.bluez.Device.rst
@@ -457,8 +457,31 @@ which can contain the following:
Rank of the device in the Set.
+AllowedBearers [readwrite, optional, experimental]
+``````````````````````````````````````````````````
+
+This string property restricts the bearers that can be used for this
+dual-mode device. Possible values are ``dual``, ``bredr``, and ``le``:
+
+:"dual": Allow both BR/EDR and LE.
+
+:"bredr": Allow BR/EDR only and block LE.
+
+:"le": Allow LE only and block BR/EDR.
+
+The setter rejects unsupported values, bearers disabled by the global
+``ControllerMode``, and unknown values with
+``org.bluez.Error.InvalidArguments``. Setting this property prevents the other
+bearer from being connected, including incoming connections and automatic
+connections. Changing the property disconnects bearer links that are no longer
+allowed; it does not disconnect links on an allowed bearer.
+
+The property is persisted and is only available for dual-mode devices. Invalid
+persisted values are reset to the effective mode for the supported bearers.
+Legacy persisted arrays are converted to these string values.
+
string PreferredBearer [readwrite, optional, experimental]
-``````````````````````````````````````````````````````````
+````````````````````````````````````````````````````````````````
Indicate the preferred bearer when initiating a connection, only available for
dual-mode devices.
diff --git a/doc/settings-storage.txt b/doc/settings-storage.txt
index 8bd677e24..f7b5f8d01 100644
--- a/doc/settings-storage.txt
+++ b/doc/settings-storage.txt
@@ -293,6 +293,7 @@ Long term key) related to a remote device.
strings remote in 128-bits UUID format,
separated by ";"
+ AllowedBearers String Allowed bearer mode for remote dual-mode device
PreferredBearer String Preferred bearer for remote device
LastUsedBearer String Last used bearer for remote device
diff --git a/src/adapter.c b/src/adapter.c
index 112312651..626f225d9 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -4949,9 +4949,9 @@ static uint8_t get_addr_type(GKeyFile *keyfile)
uint8_t addr_type;
char *type;
- /* The AddressType is written to file only When dev->le is
- * set to true, as referenced in the update_technologies().
- * Therefore, When type is NULL, it default to BDADDR_BREDR.
+ /* AddressType is stored when LE support is known, as referenced in
+ * update_technologies(). Therefore, when type is NULL, it defaults to
+ * BDADDR_BREDR.
*/
type = g_key_file_get_string(keyfile, "General", "AddressType", NULL);
if (!type)
@@ -5681,6 +5681,12 @@ static void adapter_add_connection(struct btd_adapter *adapter,
uint8_t bdaddr_type,
uint32_t flags)
{
+ if (!btd_device_bearer_allowed(device, bdaddr_type)) {
+ btd_adapter_disconnect_device(adapter,
+ device_get_address(device), bdaddr_type);
+ return;
+ }
+
device_add_connection(device, bdaddr_type, flags);
if (g_slist_find(adapter->connections, device)) {
diff --git a/src/bearer.c b/src/bearer.c
index 154e132ae..c2f3fd77c 100644
--- a/src/bearer.c
+++ b/src/bearer.c
@@ -139,6 +139,12 @@ static DBusMessage *bearer_connect(DBusConnection *conn, DBusMessage *msg,
DBusMessage *reply;
int err;
+ if (!btd_device_bearer_allowed(device, bearer->type)) {
+ if (msg)
+ return btd_error_failed(msg, "Bearer not allowed");
+ return NULL;
+ }
+
if (btd_device_bdaddr_type_connected(device, bearer->type)) {
if (msg)
return btd_error_already_connected(msg);
@@ -532,6 +538,9 @@ struct btd_bearer *btd_bearer_new(struct btd_device *device, uint8_t type)
{
struct btd_bearer *bearer;
+ if (!btd_device_bearer_allowed(device, type))
+ return NULL;
+
switch (btd_opts.mode) {
case BT_MODE_LE:
if (type == BDADDR_BREDR)
diff --git a/src/device.c b/src/device.c
index 36a980d1e..a83d354c0 100644
--- a/src/device.c
+++ b/src/device.c
@@ -274,6 +274,12 @@ struct btd_device {
struct btd_gatt_client *client_dbus;
uint8_t prefer_bearer;
+ bool allowed_bredr;
+ bool allowed_le;
+ bool allowed_bearers_set;
+ bool bredr_supported;
+ bool le_supported;
+ uint8_t le_address_type;
struct bearer_state bredr_state;
struct bearer_state le_state;
@@ -332,7 +338,7 @@ static struct bearer_state *get_state(struct btd_device *dev,
static bool device_connectable_known(struct btd_device *dev)
{
- if (dev->bredr)
+ if (dev->bredr_supported)
return true;
return get_state(dev, dev->bdaddr_type)->last_seen != 0;
@@ -397,7 +403,7 @@ static GSList *find_service_with_uuid(GSList *list, char *uuid)
static const char *device_prefer_bearer_str(struct btd_device *device)
{
/* Check if both BR/EDR and LE bearer are supported */
- if (!device->bredr || !device->le)
+ if (!device->bredr_supported || !device->le_supported)
return NULL;
switch (device->prefer_bearer) {
@@ -414,6 +420,99 @@ static const char *device_prefer_bearer_str(struct btd_device *device)
return NULL;
}
+bool btd_device_bearer_allowed(struct btd_device *device, uint8_t bdaddr_type)
+{
+ if (bdaddr_type == BDADDR_BREDR)
+ return device->bredr_supported && device->allowed_bredr &&
+ btd_opts.mode != BT_MODE_LE;
+
+ return device->le_supported && device->allowed_le &&
+ btd_opts.mode != BT_MODE_BREDR;
+}
+
+static void device_normalize_allowed_bearers(struct btd_device *device)
+{
+ device->allowed_bredr = device->bredr_supported &&
+ btd_opts.mode != BT_MODE_LE;
+ device->allowed_le = device->le_supported &&
+ btd_opts.mode != BT_MODE_BREDR;
+}
+
+static bool device_parse_allowed_bearers(struct btd_device *device,
+ const char *mode,
+ bool check_adapter_mode,
+ bool *bredr, bool *le)
+{
+ if (!bredr || !le)
+ return false;
+
+ if (!strcmp(mode, "dual")) {
+ if (!device->bredr_supported || !device->le_supported)
+ return false;
+
+ if (check_adapter_mode &&
+ (btd_opts.mode == BT_MODE_LE ||
+ btd_opts.mode == BT_MODE_BREDR))
+ return false;
+
+ *bredr = true;
+ *le = true;
+ return true;
+ }
+
+ if (!strcmp(mode, "bredr") && device->bredr_supported) {
+ if (check_adapter_mode && btd_opts.mode == BT_MODE_LE)
+ return false;
+
+ *bredr = true;
+ *le = false;
+ return true;
+ }
+
+ if (!strcmp(mode, "le") && device->le_supported) {
+ if (check_adapter_mode && btd_opts.mode == BT_MODE_BREDR)
+ return false;
+
+ *bredr = false;
+ *le = true;
+ return true;
+ }
+
+ return false;
+}
+
+static const char *device_allowed_bearers_str(struct btd_device *device)
+{
+ if (device->allowed_bredr && device->allowed_le)
+ return "dual";
+
+ if (device->allowed_bredr)
+ return "bredr";
+
+ if (device->allowed_le)
+ return "le";
+
+ return NULL;
+}
+
+static const char *device_effective_allowed_bearers_str(
+ struct btd_device *device)
+{
+ bool bredr = btd_device_bearer_allowed(device, BDADDR_BREDR);
+ bool le = btd_device_bearer_allowed(device, BDADDR_LE_PUBLIC);
+
+ if (bredr && le)
+ return "dual";
+
+ if (bredr)
+ return "bredr";
+
+ if (le)
+ return "le";
+
+ return NULL;
+}
+
static bool device_set_prefer_bearer(struct btd_device *device, uint8_t bearer)
{
switch (bearer) {
@@ -463,10 +562,10 @@ static void update_technologies(GKeyFile *file, struct btd_device *dev)
size_t len = 0;
const char *bearer;
- if (dev->bredr)
+ if (dev->bredr_supported)
list[len++] = "BR/EDR";
- if (dev->le) {
+ if (dev->le_supported) {
const char *type;
if (dev->bdaddr_type == BDADDR_LE_PUBLIC)
@@ -482,6 +581,14 @@ static void update_technologies(GKeyFile *file, struct btd_device *dev)
g_key_file_set_string_list(file, "General", "SupportedTechnologies",
list, len);
+ if (dev->bredr_supported && dev->le_supported) {
+ const char *mode = device_allowed_bearers_str(dev);
+
+ if (mode)
+ g_key_file_set_string(file, "General", "AllowedBearers",
+ mode);
+ }
+
/* Store the PreferredBearer in case of dual-mode devices */
bearer = device_prefer_bearer_str(dev);
if (bearer) {
@@ -527,6 +634,11 @@ static void store_sirk(struct sirk_info *sirk, GKeyFile *key_file,
g_key_file_set_integer(key_file, group, "Rank", sirk->rank);
}
+static void store_allowed_bearers(GKeyFile *key_file, const char *bearers)
+{
+ g_key_file_set_string(key_file, "General", "AllowedBearers", bearers);
+}
+
static gboolean store_device_info_cb(gpointer user_data)
{
struct btd_device *device = user_data;
@@ -1010,7 +1122,8 @@ static gboolean property_get_address_type(const GDBusPropertyTable *property,
struct btd_device *device = user_data;
const char *str;
- if (device->le && device->bdaddr_type == BDADDR_LE_RANDOM)
+ if (device->le_supported &&
+ device->le_address_type == BDADDR_LE_RANDOM)
str = "random";
else
str = "public";
@@ -1967,11 +2080,11 @@ int device_block(struct btd_device *device, gboolean update_only)
}
if (!update_only) {
- if (device->le)
+ if (device->le_supported)
err = btd_adapter_block_address(device->adapter,
&device->bdaddr,
device->bdaddr_type);
- if (!err && device->bredr)
+ if (!err && device->bredr_supported)
err = btd_adapter_block_address(device->adapter,
&device->bdaddr,
BDADDR_BREDR);
@@ -2001,11 +2114,11 @@ int device_unblock(struct btd_device *device, gboolean silent,
return 0;
if (!update_only) {
- if (device->le)
+ if (device->le_supported)
err = btd_adapter_unblock_address(device->adapter,
&device->bdaddr,
device->bdaddr_type);
- if (!err && device->bredr)
+ if (!err && device->bredr_supported)
err = btd_adapter_unblock_address(device->adapter,
&device->bdaddr,
BDADDR_BREDR);
@@ -2331,9 +2444,13 @@ static void device_set_auto_connect(struct btd_device *device, gboolean enable)
char addr[18];
const char *bearer;
- if (!device || !device->le)
+ if (!device || !device->le_supported)
return;
+ if (enable && !btd_device_bearer_allowed(
+ device, device->le_address_type))
+ enable = FALSE;
+
ba2str(&device->bdaddr, addr);
DBG("%s auto connect: %d", addr, enable);
@@ -2489,7 +2606,9 @@ done:
if (err) {
/* Fallback to LE bearer if supported */
- if (err == -EHOSTDOWN && dev->le && !dev->le_state.connected) {
+ if (err == -EHOSTDOWN && !dev->le_state.connected &&
+ btd_device_bearer_allowed(dev,
+ BDADDR_LE_PUBLIC)) {
err = device_connect_le(dev);
if (err == 0)
return;
@@ -2499,7 +2618,8 @@ done:
btd_error_bredr_errno(dev->connect, err));
} else {
/* Start passive SDP discovery to update known services */
- if (dev->bredr && !dev->svc_refreshed && dev->refresh_discovery)
+ if (btd_device_bearer_allowed(dev, BDADDR_BREDR) &&
+ !dev->svc_refreshed && dev->refresh_discovery)
device_browse_sdp(dev, NULL);
g_dbus_send_reply(dbus_conn, dev->connect, DBUS_TYPE_INVALID);
}
@@ -2742,6 +2862,12 @@ static uint8_t select_conn_bearer(struct btd_device *dev)
time_t bredr_last = NVAL_TIME, le_last = NVAL_TIME;
time_t current = time(NULL);
+ if (!dev->allowed_bredr)
+ return dev->le_address_type;
+
+ if (!dev->allowed_le)
+ return BDADDR_BREDR;
+
/* Use preferred bearer or bonded bearer in case only one is bonded */
if (dev->bredr_state.prefer ||
(dev->bredr_state.bonded && !dev->le_state.bonded))
@@ -2769,10 +2895,12 @@ static uint8_t select_conn_bearer(struct btd_device *dev)
if (le_last == NVAL_TIME && bredr_last == NVAL_TIME)
return dev->bdaddr_type;
- if (dev->bredr && (!dev->le || le_last == NVAL_TIME))
+ if (dev->bredr_supported &&
+ (!dev->le_supported || le_last == NVAL_TIME))
return BDADDR_BREDR;
- if (dev->le && (!dev->bredr || bredr_last == NVAL_TIME))
+ if (dev->le_supported &&
+ (!dev->bredr_supported || bredr_last == NVAL_TIME))
return dev->bdaddr_type;
/*
@@ -2808,6 +2936,9 @@ int btd_device_connect_services(struct btd_device *dev, GSList *services)
return -ENETDOWN;
bdaddr_type = select_conn_bearer(dev);
+ if (!btd_device_bearer_allowed(dev, bdaddr_type))
+ return -ENOTSUP;
+
if (bdaddr_type != BDADDR_BREDR) {
if (dev->le_state.connected)
return -EALREADY;
@@ -2849,6 +2980,9 @@ DBusMessage *device_connect_profiles(struct btd_device *dev,
struct bearer_state *state = get_state(dev, bdaddr_type);
int err;
+ if (!btd_device_bearer_allowed(dev, bdaddr_type))
+ return btd_error_failed(msg, "Bearer not allowed");
+
DBG("%s %s, client %s", dev->path, uuid ? uuid : "(all)",
dbus_message_get_sender(msg));
@@ -2924,10 +3058,17 @@ static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
bdaddr_type = dev->bdaddr_type;
else
bdaddr_type = BDADDR_BREDR;
- } else if (dev->le_state.connected && dev->bredr)
- bdaddr_type = BDADDR_BREDR;
- else
+ } else if (dev->le_state.connected) {
+ if (btd_device_bearer_allowed(dev, BDADDR_BREDR))
+ bdaddr_type = BDADDR_BREDR;
+ else
+ bdaddr_type = dev->le_address_type;
+ } else {
bdaddr_type = select_conn_bearer(dev);
+ }
+
+ if (!btd_device_bearer_allowed(dev, bdaddr_type))
+ return btd_error_failed(msg, "Bearer not allowed");
dev->auth_failures = 0;
@@ -3189,8 +3330,11 @@ static void browse_request_complete(struct browse_req *req, uint8_t type,
if (err) {
/* Fallback to LE bearer if supported */
- if (err == -EHOSTDOWN && bdaddr_type == BDADDR_BREDR &&
- dev->le && !dev->le_state.connected) {
+ if (err == -EHOSTDOWN &&
+ bdaddr_type == BDADDR_BREDR &&
+ !dev->le_state.connected &&
+ btd_device_bearer_allowed(dev,
+ BDADDR_LE_PUBLIC)) {
err = device_connect_le(dev);
if (err == 0)
goto done;
@@ -3432,24 +3576,21 @@ static DBusMessage *pair_device(DBusConnection *conn, DBusMessage *msg,
enum mgmt_io_capability io_cap;
int err;
- btd_device_set_temporary(device, false);
-
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);
if (device->bonding || device->connect)
return btd_error_in_progress(msg);
- /* Only use this selection algorithms when device is combo
- * chip. Otherwise, it will use the wrong bearer to establish
- * a connection if the device is already paired, which will
- * stall the pairing procedure. For example, for a BLE only
- * device, if the device is already paired, and upper layer
- * issue the pair device again, it will set bdaddr_type to
- * BDADDR_BREDR since LE is bonded, then it goes with BR/EDR
- * bearer.
- */
- if (device->bredr && device->le) {
+ if (!device->allowed_bredr) {
+ bdaddr_type = device->le_address_type;
+ } else if (!device->allowed_le) {
+ bdaddr_type = BDADDR_BREDR;
+ } else if (device->bredr_supported && device->le_supported) {
+ /* Use the existing selection algorithm only for a combo device.
+ * Otherwise pairing could select the wrong bearer for a
+ * BLE-only or BR/EDR-only device.
+ */
if (device->bredr_state.bonded)
bdaddr_type = device->bdaddr_type;
else if (device->le_state.bonded)
@@ -3460,11 +3601,16 @@ static DBusMessage *pair_device(DBusConnection *conn, DBusMessage *msg,
bdaddr_type = device->bdaddr_type;
}
+ if (!btd_device_bearer_allowed(device, bdaddr_type))
+ return btd_error_failed(msg, "Bearer not allowed");
+
state = get_state(device, bdaddr_type);
if (state->bonded)
return btd_error_already_exists(msg);
+ btd_device_set_temporary(device, false);
+
sender = dbus_message_get_sender(msg);
agent = agent_get(sender);
@@ -3693,6 +3839,118 @@ static const GDBusSignalTable device_signals[] = {
{ }
};
+static void device_sync_bearers(struct btd_device *device);
+
+static void device_apply_allowed_bearers(struct btd_device *device,
+ bool bredr, bool le)
+{
+ device->allowed_bredr = bredr;
+ device->allowed_le = le;
+ device->allowed_bearers_set = true;
+
+ if (!bredr) {
+ device_cancel_browse(device, BDADDR_BREDR);
+ device_remove_pending_services(device, BDADDR_BREDR);
+
+ if (device->bonding &&
+ device->bonding->bdaddr_type == BDADDR_BREDR)
+ device_cancel_bonding(device, MGMT_STATUS_CANCELLED);
+
+ }
+
+ if (!le) {
+ device_cancel_browse(device, device->bdaddr_type);
+ device_remove_pending_services(device, device->bdaddr_type);
+
+ if (device->bonding &&
+ device->bonding->bdaddr_type != BDADDR_BREDR)
+ device_cancel_bonding(device, MGMT_STATUS_CANCELLED);
+ }
+
+ if (!bredr) {
+ if (device->bredr_state.connected)
+ btd_adapter_disconnect_device(device->adapter,
+ &device->bdaddr, BDADDR_BREDR);
+
+ if (le)
+ device_set_prefer_bearer(device, PREFER_LE);
+ } else if (!le) {
+ if (device->le_state.connected)
+ btd_adapter_disconnect_device(device->adapter,
+ &device->bdaddr,
+ device->le_address_type);
+
+ device_set_prefer_bearer(device, PREFER_BREDR);
+ }
+
+ device_sync_bearers(device);
+
+ if (!bredr)
+ device_set_auto_connect(device, TRUE);
+ else
+ device_set_auto_connect(device, le);
+
+ g_dbus_emit_property_changed(dbus_conn, device->path,
+ DEVICE_INTERFACE, "PreferredBearer");
+ g_dbus_emit_property_changed(dbus_conn, device->path,
+ DEVICE_INTERFACE, "AllowedBearers");
+ store_device_info(device);
+}
+
+static gboolean
+dev_property_get_allowed_bearers(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct btd_device *device = data;
+ const char *bearer = device_effective_allowed_bearers_str(device);
+
+ if (!bearer)
+ return FALSE;
+
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &bearer);
+
+ return TRUE;
+}
+
+static void dev_property_set_allowed_bearers(const GDBusPropertyTable *property,
+ DBusMessageIter *value,
+ GDBusPendingPropertySet id, void *data)
+{
+ struct btd_device *device = data;
+ const char *bearer;
+ bool bredr;
+ bool le;
+
+ if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_STRING) {
+ g_dbus_pending_property_error(id,
+ ERROR_INTERFACE ".InvalidArguments",
+ "Invalid arguments in method call");
+ return;
+ }
+
+ dbus_message_iter_get_basic(value, &bearer);
+
+ if (!device_parse_allowed_bearers(device, bearer, true, &bredr, &le)) {
+ g_dbus_pending_property_error(id,
+ ERROR_INTERFACE ".InvalidArguments",
+ "Invalid arguments in method call");
+ return;
+ }
+
+ device_apply_allowed_bearers(device, bredr, le);
+ g_dbus_pending_property_success(id);
+}
+
+static gboolean
+dev_property_allowed_bearers_exists(const GDBusPropertyTable *property,
+ void *data)
+{
+ struct btd_device *device = data;
+
+ return device->bredr_supported && device->le_supported &&
+ device_effective_allowed_bearers_str(device) != NULL;
+}
+
static gboolean
dev_property_get_prefer_bearer(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
@@ -3826,6 +4084,10 @@ static const GDBusPropertyTable device_properties[] = {
dev_property_wake_allowed_exist },
{ "Sets", "a{oa{sv}}", dev_property_get_set, NULL,
dev_property_set_exists },
+ { "AllowedBearers", "s", dev_property_get_allowed_bearers,
+ dev_property_set_allowed_bearers,
+ dev_property_allowed_bearers_exists,
+ G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
{ "PreferredBearer", "s", dev_property_get_prefer_bearer,
dev_property_set_prefer_bearer,
dev_property_prefer_bearer_exists,
@@ -4167,6 +4429,7 @@ void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type,
}
device_disconnected(device, reason);
+ device_sync_bearers(device);
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "Connected");
@@ -4488,28 +4751,121 @@ static void load_info(struct btd_device *device, const char *local,
for (t = techno; *t; t++) {
if (g_str_equal(*t, "BR/EDR"))
- device->bredr = btd_bearer_new(device, BDADDR_BREDR);
+ device->bredr_supported = true;
else if (g_str_equal(*t, "LE"))
- device->le = btd_bearer_new(device, BDADDR_LE_PUBLIC);
+ device->le_supported = true;
else
error("Unknown device technology");
}
- if (!device->le) {
- device->bdaddr_type = BDADDR_BREDR;
- } else {
+ if (device->le_supported) {
+ device->bdaddr_type = BDADDR_LE_PUBLIC;
+ device->le_address_type = BDADDR_LE_PUBLIC;
+
str = g_key_file_get_string(key_file, "General",
- "AddressType", NULL);
+ "AddressType", NULL);
- if (str && g_str_equal(str, "public"))
+ if (str && g_str_equal(str, "public")) {
device->bdaddr_type = BDADDR_LE_PUBLIC;
- else if (str && g_str_equal(str, "static"))
+ device->le_address_type = BDADDR_LE_PUBLIC;
+ } else if (str && g_str_equal(str, "static")) {
device->bdaddr_type = BDADDR_LE_RANDOM;
- else
+ device->le_address_type = BDADDR_LE_RANDOM;
+ } else if (str) {
error("Unknown LE device technology");
+ }
g_free(str);
+ } else {
+ device->bdaddr_type = BDADDR_BREDR;
+ device->le_address_type = BDADDR_LE_PUBLIC;
+ }
+
+ {
+ char *allowed = NULL;
+ char **legacy;
+ const char *effective;
+
+ legacy = g_key_file_get_string_list(key_file,
+ "General", "AllowedBearers", NULL, NULL);
+ if (legacy) {
+ bool bredr = false;
+ bool le = false;
+ bool dual = false;
+ bool invalid = false;
+
+ for (char **p = legacy; *p; p++) {
+ if (g_str_equal(*p, "dual") && !dual &&
+ !bredr && !le)
+ dual = true;
+ else if (g_str_equal(*p, "bredr") &&
+ !dual && !bredr)
+ bredr = true;
+ else if (g_str_equal(*p, "le") && !dual && !le)
+ le = true;
+ else
+ invalid = true;
+ }
+
+ if (!invalid) {
+ if (dual || (bredr && le))
+ allowed = g_strdup("dual");
+ else if (bredr)
+ allowed = g_strdup("bredr");
+ else if (le)
+ allowed = g_strdup("le");
+ }
+ g_strfreev(legacy);
+ } else {
+ allowed = g_key_file_get_string(key_file, "General",
+ "AllowedBearers",
+ NULL);
+ }
+
+ if (allowed) {
+ bool bredr;
+ bool le;
+
+ if (device_parse_allowed_bearers(device, allowed, true,
+ &bredr,
+ &le)) {
+ device->allowed_bredr = bredr;
+ device->allowed_le = le;
+ device->allowed_bearers_set = true;
+ store_allowed_bearers(key_file, allowed);
+ } else {
+ device->allowed_bearers_set = false;
+ device_normalize_allowed_bearers(device);
+ effective =
+ device_effective_allowed_bearers_str(
+ device);
+ if (effective)
+ store_allowed_bearers(key_file,
+ effective);
+ }
+
+ g_free(allowed);
+ } else {
+ device->allowed_bearers_set = false;
+ device_normalize_allowed_bearers(device);
+ effective =
+ device_effective_allowed_bearers_str(
+ device);
+ if (effective)
+ store_allowed_bearers(key_file, effective);
+ }
+ }
+
+ if (device->bredr_supported && device->allowed_bredr)
+ device->bredr = btd_bearer_new(device, BDADDR_BREDR);
+
+ if (device->le_supported && device->allowed_le)
+ device->le = btd_bearer_new(device, device->le_address_type);
+
+ if (!device->le_supported) {
+ device->bdaddr_type = BDADDR_BREDR;
+ } else {
device->local_csrk = load_csrk(key_file, "LocalSignatureKey");
device->remote_csrk = load_csrk(key_file, "RemoteSignatureKey");
@@ -5087,6 +5443,10 @@ static struct btd_device *device_new(struct btd_adapter *adapter,
device->tx_power = 127;
device->volume = -1;
device->wake_id = -1U;
+ device->le_address_type = BDADDR_LE_PUBLIC;
+ device->allowed_bredr = true;
+ device->allowed_le = true;
+ device->allowed_bearers_set = false;
device->db = gatt_db_new();
if (!device->db) {
@@ -5179,10 +5539,18 @@ struct btd_device *device_create(struct btd_adapter *adapter,
device->bdaddr_type = bdaddr_type;
- if (bdaddr_type == BDADDR_BREDR)
- device->bredr = btd_bearer_new(device, BDADDR_BREDR);
- else
- device->le = btd_bearer_new(device, BDADDR_LE_PUBLIC);
+ if (bdaddr_type == BDADDR_BREDR) {
+ if (!device->allowed_bearers_set)
+ device->allowed_bredr = true;
+ device->bredr_supported = true;
+ } else {
+ if (!device->allowed_bearers_set)
+ device->allowed_le = true;
+ device->le_supported = true;
+ device->le_address_type = bdaddr_type;
+ }
+
+ device_sync_bearers(device);
storage_dir = btd_adapter_get_storage_dir(adapter);
str = load_cached_name(device, storage_dir, dst);
@@ -5358,8 +5726,10 @@ void device_update_addr(struct btd_device *device, const bdaddr_t *bdaddr,
/* Since this function is only used for LE SMP Identity
* Resolving purposes we can now assume LE is supported.
*/
- if (!device->le)
- device->le = btd_bearer_new(device, BDADDR_LE_PUBLIC);
+ if (!device->allowed_bearers_set)
+ device->allowed_le = true;
+
+ device->le_supported = true;
/* Remove old address from accept/auto-connect list since its address
* will be changed.
@@ -5369,6 +5739,9 @@ void device_update_addr(struct btd_device *device, const bdaddr_t *bdaddr,
bacpy(&device->bdaddr, bdaddr);
device->bdaddr_type = bdaddr_type;
+ device->le_address_type = bdaddr_type;
+
+ device_sync_bearers(device);
if (device->temporary)
btd_device_set_temporary(device, false);
@@ -5384,30 +5757,57 @@ void device_update_addr(struct btd_device *device, const bdaddr_t *bdaddr,
device_set_auto_connect(device, TRUE);
}
+static void device_sync_bearers(struct btd_device *device)
+{
+ if (!btd_device_bearer_allowed(device, BDADDR_BREDR) &&
+ device->bredr &&
+ !device->bredr_state.connected) {
+ btd_bearer_destroy(device->bredr);
+ device->bredr = NULL;
+ } else if (btd_device_bearer_allowed(device, BDADDR_BREDR) &&
+ !device->bredr) {
+ device->bredr = btd_bearer_new(device, BDADDR_BREDR);
+ }
+
+ if (!btd_device_bearer_allowed(device, BDADDR_LE_PUBLIC) &&
+ device->le && !device->le_state.connected) {
+ btd_bearer_destroy(device->le);
+ device->le = NULL;
+ } else if (btd_device_bearer_allowed(device, BDADDR_LE_PUBLIC) &&
+ !device->le) {
+ device->le = btd_bearer_new(device, device->le_address_type);
+ }
+}
+
void device_set_bredr_support(struct btd_device *device)
{
- if (btd_opts.mode == BT_MODE_LE || device->bredr)
- return;
+ if (!device->allowed_bearers_set)
+ device->allowed_bredr = true;
- if (!device->bredr)
- device->bredr = btd_bearer_new(device, BDADDR_BREDR);
+ device->bredr_supported = true;
+
+ device_sync_bearers(device);
if (device->le)
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "PreferredBearer");
+ g_dbus_emit_property_changed(dbus_conn, device->path,
+ DEVICE_INTERFACE, "AllowedBearers");
+
store_device_info(device);
}
void device_set_le_support(struct btd_device *device, uint8_t bdaddr_type)
{
- if (btd_opts.mode == BT_MODE_BREDR || device->le)
- return;
-
- if (!device->le)
- device->le = btd_bearer_new(device, BDADDR_LE_PUBLIC);
+ if (!device->allowed_bearers_set)
+ device->allowed_le = true;
+ device->le_supported = true;
device->bdaddr_type = bdaddr_type;
+ device->le_address_type = bdaddr_type;
+
+ device_sync_bearers(device);
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "AddressType");
@@ -5416,6 +5816,9 @@ void device_set_le_support(struct btd_device *device, uint8_t bdaddr_type)
g_dbus_emit_property_changed(dbus_conn, device->path,
DEVICE_INTERFACE, "PreferredBearer");
+ g_dbus_emit_property_changed(dbus_conn, device->path,
+ DEVICE_INTERFACE, "AllowedBearers");
+
store_device_info(device);
}
@@ -5469,8 +5872,10 @@ void device_merge_duplicate(struct btd_device *dev, struct btd_device *dup)
/* dup keeps its own bearer, which is destroyed along with it, so
* dev needs a bearer of its own unless it already has one.
*/
- if (!dev->bredr)
- dev->bredr = btd_bearer_new(dev, BDADDR_BREDR);
+ if (!dev->allowed_bearers_set)
+ dev->allowed_bredr = true;
+ dev->bredr_supported = true;
+ device_sync_bearers(dev);
/* Connection state stays with dup, which owns the link and gets
* its disconnection, so only the pairing state is inherited.
@@ -5770,13 +6175,13 @@ int device_addr_type_cmp(gconstpointer a, gconstpointer b)
return 0;
if (addr->bdaddr_type == BDADDR_BREDR) {
- if (!dev->bredr)
+ if (!dev->bredr_supported)
return -1;
return cmp;
}
- if (!dev->le)
+ if (!dev->le_supported)
return -1;
if (addr->bdaddr_type != dev->bdaddr_type) {
@@ -6861,6 +7266,9 @@ int device_connect_le(struct btd_device *dev)
GError *gerr = NULL;
char addr[18];
+ if (!btd_device_bearer_allowed(dev, dev->le_address_type))
+ return -ENOTSUP;
+
/* There is one connection attempt going on */
if (dev->att_io || dev->att)
return -EALREADY;
@@ -7086,6 +7494,9 @@ int device_discover_services(struct btd_device *device,
{
int err;
+ if (!btd_device_bearer_allowed(device, bdaddr_type))
+ return -ENOTSUP;
+
if (bdaddr_type == BDADDR_BREDR)
err = device_browse_sdp(device, msg);
else
@@ -7113,7 +7524,7 @@ const bdaddr_t *device_get_address(struct btd_device *device)
}
uint8_t device_get_le_address_type(struct btd_device *device)
{
- return device->bdaddr_type;
+ return device->le_address_type;
}
const char *device_get_path(const struct btd_device *device)
@@ -7427,7 +7838,7 @@ bool device_is_connectable(struct btd_device *device)
if (!device)
return false;
- if (device->bredr)
+ if (device->bredr_supported)
return true;
state = get_state(device, device->bdaddr_type);
@@ -7439,7 +7850,7 @@ static bool start_discovery_cb(gpointer user_data)
{
struct btd_device *device = user_data;
- if (device->bredr)
+ if (btd_device_bearer_allowed(device, BDADDR_BREDR))
device_browse_sdp(device, NULL);
else
device_browse_gatt(device, NULL);
diff --git a/src/device.h b/src/device.h
index 047a894c3..4d61b2a41 100644
--- a/src/device.h
+++ b/src/device.h
@@ -125,6 +125,7 @@ void device_set_rssi(struct btd_device *device, int8_t rssi);
void device_set_tx_power(struct btd_device *device, int8_t tx_power);
void device_set_flags(struct btd_device *device, uint8_t flags);
bool btd_device_is_connected(struct btd_device *dev);
+bool btd_device_bearer_allowed(struct btd_device *dev, uint8_t bdaddr_type);
bool btd_device_bearer_is_connected(struct btd_device *dev);
bool btd_device_bdaddr_type_connected(struct btd_device *dev, uint8_t type);
bool btd_device_bdaddr_type_connectable(struct btd_device *dev, uint8_t type);
diff --git a/test/functional/test_bearer.py b/test/functional/test_bearer.py
index 3f1033b35..693441169 100644
--- a/test/functional/test_bearer.py
+++ b/test/functional/test_bearer.py
@@ -8,6 +8,10 @@ interfaces of the device object it has for the other.
"""
import logging
+import os
+import re
+import subprocess
+from pathlib import Path
import dbus
import pytest
@@ -57,6 +61,10 @@ CONN_PARAMS = ("Interval", "Latency", "SupervisionTimeout", "Subrate")
# ReadRSSI is a testing method
TESTING_CONF = LE_CONF + "Testing = true\n"
+ALLOWED_CONF = "[General]\nExperimental = true\n"
+LE_ALLOWED_CONF = "[General]\nControllerMode = le\nExperimental = true\n"
+BREDR_ALLOWED_CONF = "[General]\nControllerMode = bredr\nExperimental = true\n"
+
class Bearers(HostPlugin, EventPluginMixin):
"""
@@ -119,6 +127,51 @@ class Bearers(HostPlugin, EventPluginMixin):
return None
+ @mainloop_wrap
+ def get_device_property(self, address, name):
+ """Return a property of the remote Device1 interface."""
+ path = self._device_path(address)
+ if path is None:
+ return None
+
+ props = dbus.Interface(self.bus.get_object(BUS_NAME, path), PROPS_INTERFACE)
+ try:
+ value = props.Get(DEVICE_INTERFACE, name)
+ except dbus.exceptions.DBusException:
+ return None
+
+ return str(value)
+
+ @mainloop_wrap
+ def set_device_property(self, address, name, value):
+ """Set a Device1 property and return its D-Bus error, if any."""
+ path = self._device_path(address)
+ if path is None:
+ return "org.bluez.Error.Failed"
+
+ props = dbus.Interface(self.bus.get_object(BUS_NAME, path), PROPS_INTERFACE)
+ try:
+ props.Set(DEVICE_INTERFACE, name, dbus.String(value))
+ except dbus.exceptions.DBusException as exc:
+ return exc.get_dbus_name()
+
+ return None
+
+ @mainloop_wrap
+ def set_device_property_raw(self, address, name, value):
+ """Set a raw Device1 property and return its D-Bus error."""
+ path = self._device_path(address)
+ if path is None:
+ return "org.bluez.Error.Failed"
+
+ props = dbus.Interface(self.bus.get_object(BUS_NAME, path), PROPS_INTERFACE)
+ try:
+ props.Set(DEVICE_INTERFACE, name, value)
+ except dbus.exceptions.DBusException as exc:
+ return exc.get_dbus_name()
+
+ return None
+
@mainloop_wrap
def get(self, address, interface, name):
"""
@@ -140,6 +193,16 @@ class Bearers(HostPlugin, EventPluginMixin):
return str(value)
+ @mainloop_wrap
+ def call_device(self, address, method):
+ """Call a method on the remote Device1 interface."""
+ path = self._device_path(address)
+ if path is None:
+ return
+
+ device = dbus.Interface(self.bus.get_object(BUS_NAME, path), DEVICE_INTERFACE)
+ self._object_method(device, method)
+
@mainloop_wrap
def call(self, address, interface, method):
"""
@@ -181,6 +244,97 @@ bredr_config = host_config(
[Bluetoothd(args=ARGS), Agent(), Bearers()],
)
+allowed_config = host_config(
+ [Bluetoothd(conf=ALLOWED_CONF, args=ARGS), Agent(), Bearers()],
+ [Bluetoothd(conf=ALLOWED_CONF, args=ARGS), Agent(), Bearers()],
+)
+
+
+class RestartableBluetoothd(Bluetoothd):
+ """Bluetoothd plugin that can restart while keeping its state directory."""
+
+ def restart(self):
+ command = self.job.args
+ self.job.terminate()
+ self.job.wait()
+
+ env = os.environ.copy()
+ env["STATE_DIRECTORY"] = str(Path(self.tmpdir.name) / "state")
+ self.job = subprocess.Popen(
+ command,
+ env=env,
+ stdin=subprocess.DEVNULL,
+ stdout=self.log_stream.stream,
+ stderr=subprocess.STDOUT,
+ )
+
+ def clear_allowed_bearers(self, address):
+ info_file = self._device_info_file(address)
+ contents = info_file.read_text(encoding="utf-8")
+ contents, count = re.subn(
+ r"^AllowedBearers=.*(?:\r?\n|$)",
+ "",
+ contents,
+ flags=re.MULTILINE,
+ )
+ if count != 1:
+ raise ValueError(f"AllowedBearers not found for {address}")
+
+ info_file.write_text(contents, encoding="utf-8")
+
+ def stored_allowed_bearers(self, address):
+ try:
+ info_file = self._device_info_file(address)
+ except FileNotFoundError:
+ return None
+
+ contents = info_file.read_text(encoding="utf-8")
+ match = re.search(
+ r"^AllowedBearers=(.*?)\r?$",
+ contents,
+ flags=re.MULTILINE,
+ )
+ return match.group(1) if match else None
+
+ def _device_info_file(self, address):
+ storage = Path(self.tmpdir.name) / "state" / "bluetooth"
+ info_files = list(storage.glob(f"*/{address.upper()}/info"))
+ if not info_files:
+ raise FileNotFoundError(f"Device info not found for {address}")
+ if len(info_files) != 1:
+ raise ValueError(f"Multiple device info files found for {address}")
+
+ return info_files[0]
+
+
+restartable_allowed_config = host_config(
+ [
+ RestartableBluetoothd(conf=ALLOWED_CONF, args=ARGS),
+ Agent(),
+ Bearers(),
+ ],
+ [
+ Bluetoothd(conf=ALLOWED_CONF, args=ARGS),
+ Agent(),
+ Bearers(),
+ ],
+)
+
+le_allowed_config = host_config(
+ [Bluetoothd(conf=LE_ALLOWED_CONF, args=ARGS), Agent(), Bearers()],
+ [
+ Bluetoothd(conf=LE_ALLOWED_CONF, args=ARGS),
+ LeAdvertiser(),
+ Agent(capability="NoInputNoOutput"),
+ Bearers(),
+ ],
+)
+
+bredr_allowed_config = host_config(
+ [Bluetoothd(conf=BREDR_ALLOWED_CONF, args=ARGS), Agent(), Bearers()],
+ [Bluetoothd(conf=BREDR_ALLOWED_CONF, args=ARGS), Agent(), Bearers()],
+)
+
def start_discovery(host):
host.agent.adapter_method("StartDiscovery")
@@ -418,6 +572,183 @@ def test_bearer_le_pair(hosts):
assert host.bearers.get(remote.bdaddr, BREDR_INTERFACE, "Paired") is None
+@le_allowed_config
+def test_bearer_allowed_bearers_le_mode(hosts):
+ host0, host1 = hosts
+
+ pair_le(host0, host1)
+
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "le")
+ is None
+ )
+
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "bredr")
+ == "org.bluez.Error.InvalidArguments"
+ )
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "dual")
+ == "org.bluez.Error.InvalidArguments"
+ )
+
+
+@bredr_allowed_config
+def test_bearer_allowed_bearers_bredr_mode(hosts):
+ host0, host1 = hosts
+
+ pair_bredr(hosts, True)
+
+ assert host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers") is None
+
+
+@allowed_config
+def test_bearer_allowed_bearers(hosts):
+ host0, host1 = hosts
+
+ pair_bredr(hosts, True)
+
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "dual"
+ )
+
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "le")
+ is None
+ )
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "le"
+ )
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, LE_INTERFACE, "Connected") is False
+ )
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, BREDR_INTERFACE, "Connected") is None
+ )
+
+ host0.bearers.call(host1.bdaddr, BREDR_INTERFACE, "Connect")
+ event = host0.bearers.expect(f"{BREDR_INTERFACE}.Connect:error")
+ assert event.error.get_dbus_name() == "org.freedesktop.DBus.Error.UnknownMethod"
+
+ host0.bearers.call_device(host1.bdaddr, "Connect")
+ event = host0.bearers.expect("org.bluez.Device1.Connect:error")
+ assert event.error.get_dbus_name() == "org.bluez.Error.Failed"
+
+ host0.bearers.call(host1.bdaddr, LE_INTERFACE, "Connect")
+ host0.bearers.expect(f"{LE_INTERFACE}.Connect:reply")
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, LE_INTERFACE, "Connected") is True
+ )
+
+ host0.bearers.call_device(host1.bdaddr, "Connect")
+ host0.bearers.expect("org.bluez.Device1.Connect:reply")
+
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "bredr")
+ is None
+ )
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "bredr"
+ )
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, BREDR_INTERFACE, "Connected") is False
+ )
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, LE_INTERFACE, "Connected") is None
+ )
+
+ host0.bearers.call(host1.bdaddr, LE_INTERFACE, "Connect")
+ event = host0.bearers.expect(f"{LE_INTERFACE}.Connect:error")
+ assert event.error.get_dbus_name() == "org.bluez.Error.Failed"
+
+ host0.bearers.call_device(host1.bdaddr, "Connect")
+ event = host0.bearers.expect("org.bluez.Device1.Connect:error")
+ assert event.error.get_dbus_name() == "org.bluez.Error.Failed"
+
+ host0.bearers.call(host1.bdaddr, BREDR_INTERFACE, "Connect")
+ host0.bearers.expect(f"{BREDR_INTERFACE}.Connect:reply")
+
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, BREDR_INTERFACE, "Connected") is True
+ )
+
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "dual")
+ is None
+ )
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "dual"
+ )
+
+ assert (
+ host0.bearers.set_device_property_raw(
+ host1.bdaddr, "AllowedBearers", dbus.Boolean(False)
+ )
+ == "org.bluez.Error.InvalidArguments"
+ )
+
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "invalid")
+ == "org.bluez.Error.InvalidArguments"
+ )
+ assert (
+ host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "dual"
+ )
+
+
+@restartable_allowed_config
+def test_bearer_allowed_bearers_persistence(hosts):
+ host0, host1 = hosts
+
+ pair_bredr(hosts, True)
+
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "dual"
+ )
+ assert (
+ host0.bearers.set_device_property(host1.bdaddr, "AllowedBearers", "le")
+ is None
+ )
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "le"
+ )
+ wait_until(
+ lambda: host0.bluetoothd.stored_allowed_bearers(host1.bdaddr) == "le"
+ )
+
+ host0.bluetoothd.restart()
+
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "le"
+ )
+ assert host0.bearers.get(host1.bdaddr, BREDR_INTERFACE, "Connected") is None
+ host0.bearers.call(host1.bdaddr, BREDR_INTERFACE, "Connect")
+ event = host0.bearers.expect(f"{BREDR_INTERFACE}.Connect:error")
+ assert event.error.get_dbus_name() == "org.freedesktop.DBus.Error.UnknownMethod"
+
+ host0.bluetoothd.clear_allowed_bearers(host1.bdaddr)
+ host0.bluetoothd.restart()
+
+ wait_until(
+ lambda: host0.bearers.get_device_property(host1.bdaddr, "AllowedBearers")
+ == "dual"
+ )
+ assert host0.bearers.get(host1.bdaddr, BREDR_INTERFACE, "Connected") is False
+ host0.bearers.call(host1.bdaddr, BREDR_INTERFACE, "Connect")
+ host0.bearers.expect(f"{BREDR_INTERFACE}.Connect:reply")
+ wait_until(
+ lambda: host0.bearers.get(host1.bdaddr, BREDR_INTERFACE, "Connected") is True
+ )
+
+
@bredr_config
def test_bearer_bredr_pair(hosts):
host0, host1 = hosts
--
2.56.1
next prev parent reply other threads:[~2026-11-29 6:35 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 10:20 [PATCH 0/1] device: Add per-device bearer restrictions Paweł Owoc
2026-11-28 10:20 ` [PATCH 1/1] " Paweł Owoc
2026-11-28 13:55 ` bluez.test.bot
2026-11-28 14:45 ` [PATCH v2 0/1] " Paweł Owoc
2026-11-28 14:45 ` [PATCH v2 1/1] " Paweł Owoc
2026-11-28 22:03 ` bluez.test.bot
2026-11-29 6:34 ` [PATCH v3 0/1] " Paweł Owoc
2026-11-29 6:34 ` Paweł Owoc [this message]
2026-11-29 11:54 ` bluez.test.bot
2026-12-01 15:54 ` Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129063444.120414-2-frut3k7@gmail.com \
--to=frut3k7@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.