From 9bcad1eb449d7569de9aba77d6f6a6b5f1e51f7c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 5 Jan 2013 12:22:23 -0800 Subject: [PATCH] core: Keep the current discovery type inside adapter handling --- src/adapter.c | 35 +++++++++++++++++++++++--------- src/mgmt.c | 55 ++++----------------------------------------------- src/mgmt.h | 5 ++--- 3 files changed, 32 insertions(+), 63 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 56a715ce1..0e0cc474d 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -159,6 +159,7 @@ struct btd_adapter { GSList *devices; /* Devices structure pointers */ guint remove_temp; /* Remove devices timer */ GSList *disc_sessions; /* Discovery sessions */ + uint8_t discov_type; struct session_req *scanning_session; GSList *connect_list; /* Devices to connect when found */ guint discov_id; /* Discovery timer */ @@ -624,7 +625,7 @@ static void stop_discovery(struct btd_adapter *adapter) } if (mgmt_powered(adapter->current_settings)) - mgmt_stop_discovery(adapter->dev_id); + mgmt_stop_discovery(adapter->dev_id, adapter->discov_type); else discovery_cleanup(adapter); } @@ -1192,12 +1193,18 @@ static gboolean discovery_cb(gpointer user_data) struct btd_adapter *adapter = user_data; adapter->discov_id = 0; + adapter->discov_type = 0; - if (adapter->scanning_session && - g_slist_length(adapter->disc_sessions) == 1) - mgmt_start_le_scanning(adapter->dev_id); - else - mgmt_start_discovery(adapter->dev_id); + if (adapter->current_settings & MGMT_SETTING_LE) { + hci_set_bit(BDADDR_LE_PUBLIC, &adapter->discov_type); + hci_set_bit(BDADDR_LE_RANDOM, &adapter->discov_type); + } + + if (!adapter->scanning_session || + g_slist_length(adapter->disc_sessions) != 1) + hci_set_bit(BDADDR_BREDR, &adapter->discov_type); + + mgmt_start_discovery(adapter->dev_id, adapter->discov_type); return FALSE; } @@ -1225,7 +1232,17 @@ static DBusMessage *adapter_start_discovery(DBusConnection *conn, if (adapter->discov_suspended) goto done; - err = mgmt_start_discovery(adapter->dev_id); + adapter->discov_type = 0; + + if (adapter->current_settings & MGMT_SETTING_BREDR) + hci_set_bit(BDADDR_BREDR, &adapter->discov_type); + + if (adapter->current_settings & MGMT_SETTING_LE) { + hci_set_bit(BDADDR_LE_PUBLIC, &adapter->discov_type); + hci_set_bit(BDADDR_LE_RANDOM, &adapter->discov_type); + } + + err = mgmt_start_discovery(adapter->dev_id, adapter->discov_type); if (err < 0) return btd_error_failed(msg, strerror(-err)); @@ -3388,7 +3405,7 @@ static void suspend_discovery(struct btd_adapter *adapter) g_source_remove(adapter->discov_id); adapter->discov_id = 0; } else - mgmt_stop_discovery(adapter->dev_id); + mgmt_stop_discovery(adapter->dev_id, adapter->discov_type); } static gboolean clean_connecting_state(GIOChannel *io, GIOCondition cond, @@ -3909,7 +3926,7 @@ void adapter_bonding_complete(struct btd_adapter *adapter, if (adapter->discov_suspended) { adapter->discov_suspended = FALSE; - mgmt_start_discovery(adapter->dev_id); + mgmt_start_discovery(adapter->dev_id, adapter->discov_type); } check_oob_bonding_complete(adapter, bdaddr, status); diff --git a/src/mgmt.c b/src/mgmt.c index c83320f7b..cffc60943 100644 --- a/src/mgmt.c +++ b/src/mgmt.c @@ -57,7 +57,6 @@ static int max_index = -1; static struct controller_info { gboolean valid; uint32_t current_settings; - uint8_t discov_type; } *controllers = NULL; static int mgmt_sock = -1; @@ -1464,65 +1463,20 @@ void mgmt_cleanup(void) } } -int mgmt_start_discovery(int index) +int mgmt_start_discovery(int index, uint8_t type) { char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)]; struct mgmt_hdr *hdr = (void *) buf; struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)]; - struct controller_info *info = &controllers[index]; DBG("index %d", index); - info->discov_type = 0; - - if (mgmt_bredr(info->current_settings)) - hci_set_bit(BDADDR_BREDR, &info->discov_type); - - if (mgmt_low_energy(info->current_settings)) { - hci_set_bit(BDADDR_LE_PUBLIC, &info->discov_type); - hci_set_bit(BDADDR_LE_RANDOM, &info->discov_type); - } - - memset(buf, 0, sizeof(buf)); - hdr->opcode = htobs(MGMT_OP_START_DISCOVERY); - hdr->len = htobs(sizeof(*cp)); - hdr->index = htobs(index); - - cp->type = info->discov_type; - - if (write(mgmt_sock, buf, sizeof(buf)) < 0) { - int err = -errno; - error("failed to write to MGMT socket: %s", strerror(-err)); - return err; - } - - return 0; -} - -int mgmt_start_le_scanning(int index) -{ - char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)]; - struct mgmt_hdr *hdr = (void *) buf; - struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)]; - struct controller_info *info = &controllers[index]; - - DBG("index %d", index); - - if (!mgmt_low_energy(info->current_settings)) { - error("scanning failed: Low Energy not enabled/supported"); - return -ENOTSUP; - } - - info->discov_type = 0; - hci_set_bit(BDADDR_LE_PUBLIC, &info->discov_type); - hci_set_bit(BDADDR_LE_RANDOM, &info->discov_type); - memset(buf, 0, sizeof(buf)); hdr->opcode = htobs(MGMT_OP_START_DISCOVERY); hdr->len = htobs(sizeof(*cp)); hdr->index = htobs(index); - cp->type = info->discov_type; + cp->type = type; if (write(mgmt_sock, buf, sizeof(buf)) < 0) { int err = -errno; @@ -1533,12 +1487,11 @@ int mgmt_start_le_scanning(int index) return 0; } -int mgmt_stop_discovery(int index) +int mgmt_stop_discovery(int index, uint8_t type) { char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_start_discovery)]; struct mgmt_hdr *hdr = (void *) buf; struct mgmt_cp_start_discovery *cp = (void *) &buf[sizeof(*hdr)]; - struct controller_info *info = &controllers[index]; DBG("index %d", index); @@ -1547,7 +1500,7 @@ int mgmt_stop_discovery(int index) hdr->len = htobs(sizeof(*cp)); hdr->index = htobs(index); - cp->type = info->discov_type; + cp->type = type; if (write(mgmt_sock, buf, sizeof(buf)) < 0) return -errno; diff --git a/src/mgmt.h b/src/mgmt.h index 5fa214ff8..07fcd1340 100644 --- a/src/mgmt.h +++ b/src/mgmt.h @@ -25,9 +25,8 @@ int mgmt_setup(void); void mgmt_cleanup(void); -int mgmt_start_discovery(int index); -int mgmt_start_le_scanning(int index); -int mgmt_stop_discovery(int index); +int mgmt_start_discovery(int index, uint8_t type); +int mgmt_stop_discovery(int index, uint8_t type); int mgmt_block_device(int index, const bdaddr_t *bdaddr, uint8_t bdaddr_type); int mgmt_unblock_device(int index, const bdaddr_t *bdaddr, uint8_t bdaddr_type); -- 2.47.3