From 5a80fa0d0408aa883417e083763d7fd57bd635c9 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Fri, 18 Jul 2014 10:52:39 +0300 Subject: [PATCH] core: Make pairable mode dependent on the existence of a default agent This patch updates the adapter code to only enable pairable mode when there is a default agent available. A new main.conf option called AlwaysPairable is also introduced to allow keeping the old behavior, however the default is the new behavior (i.e. this option defaults to FALSE). --- src/adapter.c | 23 +++++++++++++++++++++-- src/agent.c | 4 ++++ src/hcid.h | 1 + src/main.c | 10 ++++++++++ src/main.conf | 5 +++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 88b720ace..db8a73762 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -3135,7 +3135,7 @@ void adapter_set_pairable(struct btd_adapter *adapter, bool enable) if (current == enable) return; - set_mode(adapter, MGMT_OP_SET_PAIRABLE, 0x01); + set_mode(adapter, MGMT_OP_SET_PAIRABLE, enable ? 0x01 : 0x00); } bool btd_adapter_get_powered(struct btd_adapter *adapter) @@ -7024,7 +7024,26 @@ static void read_info_complete(uint8_t status, uint16_t length, !(adapter->current_settings & MGMT_SETTING_LE)) set_mode(adapter, MGMT_OP_SET_LE, 0x01); - set_mode(adapter, MGMT_OP_SET_PAIRABLE, 0x01); + if (main_opts.always_pairable) { + if (!(adapter->current_settings & MGMT_SETTING_PAIRABLE)) + set_mode(adapter, MGMT_OP_SET_PAIRABLE, 0x01); + } else { + struct agent *agent = agent_get(NULL); + + if (adapter->current_settings & MGMT_SETTING_PAIRABLE) { + if (!agent) + set_mode(adapter, MGMT_OP_SET_PAIRABLE, 0x00); + } else { + if (agent) + set_mode(adapter, MGMT_OP_SET_PAIRABLE, 0x01); + } + + if (agent) { + agent_unref(agent); + agent = NULL; + } + } + if (!kernel_conn_control) set_mode(adapter, MGMT_OP_SET_CONNECTABLE, 0x01); diff --git a/src/agent.c b/src/agent.c index 8c1211c33..70786c3b0 100644 --- a/src/agent.c +++ b/src/agent.c @@ -42,6 +42,7 @@ #include "log.h" #include "error.h" +#include "hcid.h" #include "dbus-common.h" #include "adapter.h" #include "device.h" @@ -151,6 +152,9 @@ static void set_io_cap(struct btd_adapter *adapter, gpointer user_data) io_cap = IO_CAPABILITY_NOINPUTNOOUTPUT; adapter_set_io_capability(adapter, io_cap); + + if (!main_opts.always_pairable) + adapter_set_pairable(adapter, agent ? true : false); } static bool add_default_agent(struct agent *agent) diff --git a/src/hcid.h b/src/hcid.h index 6040c71d2..595612423 100644 --- a/src/hcid.h +++ b/src/hcid.h @@ -29,6 +29,7 @@ struct main_opts { uint16_t autoto; uint32_t pairto; uint32_t discovto; + gboolean always_pairable; gboolean reverse_sdp; gboolean name_resolv; gboolean debug_keys; diff --git a/src/main.c b/src/main.c index 15f98cf9b..5ec1ab3ca 100644 --- a/src/main.c +++ b/src/main.c @@ -72,6 +72,7 @@ static const char * const supported_options[] = { "Name", "Class", "DiscoverableTimeout", + "AlwaysPairable", "PairableTimeout", "AutoConnectTimeout", "DeviceID", @@ -200,6 +201,14 @@ static void parse_config(GKeyFile *config) DBG("parsing main.conf"); + boolean = g_key_file_get_boolean(config, "General", + "AlwaysPairable", &err); + if (err) { + DBG("%s", err->message); + g_clear_error(&err); + } else + main_opts.always_pairable = boolean; + val = g_key_file_get_integer(config, "General", "DiscoverableTimeout", &err); if (err) { @@ -291,6 +300,7 @@ static void init_defaults(void) memset(&main_opts, 0, sizeof(main_opts)); main_opts.name = g_strdup_printf("BlueZ %s", VERSION); main_opts.class = 0x000000; + main_opts.always_pairable = FALSE; main_opts.pairto = DEFAULT_PAIRABLE_TIMEOUT; main_opts.discovto = DEFAULT_DISCOVERABLE_TIMEOUT; main_opts.reverse_sdp = TRUE; diff --git a/src/main.conf b/src/main.conf index 3ebaddeb4..1d62f24a7 100644 --- a/src/main.conf +++ b/src/main.conf @@ -10,6 +10,11 @@ # considered. Defaults to '0x000000'. #Class = 0x000100 +# Whether adapters should always be set as pairable, regardless of +# whether there is any agents registered. Setting to false (the default) +# will make adapters only pairable once the first agent registers. +#AlwaysPairable = false + # How long to stay in discoverable mode before going back to non-discoverable # The value is in seconds. Default is 180, i.e. 3 minutes. # 0 = disable timer, i.e. stay discoverable forever -- 2.47.3