From 92f9d578033bad9067ea448d4fa2ca3773ec5f50 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Thu, 1 Mar 2012 11:15:03 +0200 Subject: [PATCH] core: Make adapter_ops->set_discoverable to take discoverable timeout This enables the driver to implements its own handling of the timeout --- plugins/hciops.c | 92 ++++++++++++++++++++++++++++++++++++-------- plugins/mgmtops.c | 13 ++++--- src/adapter.c | 98 ++++++----------------------------------------- src/adapter.h | 7 +++- 4 files changed, 101 insertions(+), 109 deletions(-) diff --git a/plugins/hciops.c b/plugins/hciops.c index 3d68796f0..fdc127101 100644 --- a/plugins/hciops.c +++ b/plugins/hciops.c @@ -171,6 +171,9 @@ static struct dev_info { GSList *need_name; guint stop_scan_id; + + uint16_t discoverable_timeout; + guint discoverable_id; } *devs = NULL; static int found_dev_rssi_cmp(gconstpointer a, gconstpointer b) @@ -495,7 +498,8 @@ static int init_ssp_mode(int index) return 0; } -static int hciops_set_discoverable(int index, gboolean discoverable) +static int hciops_set_discoverable(int index, gboolean discoverable, + uint16_t timeout) { struct dev_info *dev = &devs[index]; uint8_t mode; @@ -511,6 +515,8 @@ static int hciops_set_discoverable(int index, gboolean discoverable) 1, &mode) < 0) return -errno; + dev->discoverable_timeout = timeout; + return 0; } @@ -752,6 +758,7 @@ static gboolean init_adapter(int index) uint8_t mode, on_mode, major, minor; gboolean pairable, discoverable; const char *name; + uint16_t discoverable_timeout; if (!dev->registered) { adapter = btd_manager_register_adapter(index, TRUE); @@ -766,7 +773,9 @@ static gboolean init_adapter(int index) if (adapter == NULL) return FALSE; - btd_adapter_get_mode(adapter, &mode, &on_mode, &pairable); + btd_adapter_get_mode(adapter, &mode, &on_mode, + &discoverable_timeout, + &pairable); if (existing_adapter) mode = on_mode; @@ -789,7 +798,7 @@ static gboolean init_adapter(int index) discoverable = (mode == MODE_DISCOVERABLE); - hciops_set_discoverable(index, discoverable); + hciops_set_discoverable(index, discoverable, discoverable_timeout); hciops_set_pairable(index, pairable); if (dev->already_up) @@ -1723,20 +1732,13 @@ static inline void cmd_status(int index, void *ptr) cs_inquiry_evt(index, evt->status); } -static void read_scan_complete(int index, uint8_t status, void *ptr) +static gboolean discoverable_timeout_handler(gpointer user_data) { - struct btd_adapter *adapter; - read_scan_enable_rp *rp = ptr; + struct dev_info *dev = user_data; - DBG("hci%d status %u", index, status); + hciops_set_discoverable(dev->id, FALSE, 0); - adapter = manager_find_adapter_by_id(index); - if (!adapter) { - error("Unable to find matching adapter"); - return; - } - - adapter_mode_changed(adapter, rp->enable); + return FALSE; } /* Limited Discoverable bit mask in CoD */ @@ -1776,6 +1778,65 @@ static int hciops_set_limited_discoverable(int index, gboolean limited) return write_class(index, dev->wanted_cod); } +static void reset_discoverable_timeout(int index) +{ + struct dev_info *dev = &devs[index]; + + if (dev->discoverable_id > 0) { + g_source_remove(dev->discoverable_id); + dev->discoverable_id = 0; + } +} + +static void set_discoverable_timeout(int index) +{ + struct dev_info *dev = &devs[index]; + + reset_discoverable_timeout(index); + + if (dev->discoverable_timeout == 0) { + hciops_set_limited_discoverable(index, FALSE); + return; + } + + /* Set limited discoverable if pairable and interval between 0 to 60 + sec */ + if (dev->pairable && dev->discoverable_timeout <= 60) + hciops_set_limited_discoverable(index, TRUE); + else + hciops_set_limited_discoverable(index, FALSE); + + dev->discoverable_id = g_timeout_add_seconds(dev->discoverable_timeout, + discoverable_timeout_handler, + dev); +} + +static void read_scan_complete(int index, uint8_t status, void *ptr) +{ + struct btd_adapter *adapter; + read_scan_enable_rp *rp = ptr; + + DBG("hci%d status %u", index, status); + + switch (rp->enable) { + case (SCAN_PAGE | SCAN_INQUIRY): + case SCAN_INQUIRY: + set_discoverable_timeout(index); + break; + default: + reset_discoverable_timeout(index); + hciops_set_limited_discoverable(index, FALSE); + } + + adapter = manager_find_adapter_by_id(index); + if (!adapter) { + error("Unable to find matching adapter"); + return; + } + + adapter_mode_changed(adapter, rp->enable); +} + static void write_class_complete(int index, uint8_t status) { struct dev_info *dev = &devs[index]; @@ -2854,6 +2915,7 @@ static void device_event(int event, int index) devs[index].pending_cod = 0; devs[index].cache_enable = TRUE; devs[index].discov_state = DISCOV_HALTED; + reset_discoverable_timeout(index); if (!devs[index].pending) { struct btd_adapter *adapter; @@ -3824,8 +3886,8 @@ static struct btd_adapter_ops hci_ops = { .cleanup = hciops_cleanup, .set_powered = hciops_set_powered, .set_discoverable = hciops_set_discoverable, - .set_pairable = hciops_set_pairable, .set_limited_discoverable = hciops_set_limited_discoverable, + .set_pairable = hciops_set_pairable, .start_discovery = hciops_start_discovery, .stop_discovery = hciops_stop_discovery, .set_name = hciops_set_name, diff --git a/plugins/mgmtops.c b/plugins/mgmtops.c index dd5f7a38b..51279ef4a 100644 --- a/plugins/mgmtops.c +++ b/plugins/mgmtops.c @@ -216,7 +216,8 @@ static int mgmt_set_connectable(int index, gboolean connectable) return mgmt_set_mode(index, MGMT_OP_SET_CONNECTABLE, connectable); } -static int mgmt_set_discoverable(int index, gboolean discoverable) +static int mgmt_set_discoverable(int index, gboolean discoverable, + uint16_t timeout) { char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_discoverable)]; struct mgmt_hdr *hdr = (void *) buf; @@ -230,6 +231,7 @@ static int mgmt_set_discoverable(int index, gboolean discoverable) hdr->len = htobs(sizeof(*cp)); cp->val = discoverable; + cp->timeout = timeout; if (write(mgmt_sock, buf, sizeof(buf)) < 0) return -errno; @@ -306,16 +308,17 @@ static void update_settings(struct btd_adapter *adapter, uint32_t settings) struct controller_info *info; gboolean pairable; uint8_t on_mode; - uint16_t index; + uint16_t index, discoverable_timeout; - btd_adapter_get_mode(adapter, NULL, &on_mode, &pairable); + btd_adapter_get_mode(adapter, NULL, &on_mode, &discoverable_timeout, + &pairable); index = adapter_get_dev_id(adapter); info = &controllers[index]; if (on_mode == MODE_DISCOVERABLE && !mgmt_discoverable(settings)) - mgmt_set_discoverable(index, TRUE); + mgmt_set_discoverable(index, TRUE, discoverable_timeout); else if (on_mode == MODE_CONNECTABLE && !mgmt_connectable(settings)) mgmt_set_connectable(index, TRUE); else if (mgmt_powered(settings)) @@ -1085,7 +1088,7 @@ static void read_info_complete(int sk, uint16_t index, void *buf, size_t len) btd_adapter_get_class(adapter, &major, &minor); mgmt_set_dev_class(index, major, minor); - btd_adapter_get_mode(adapter, &mode, NULL, NULL); + btd_adapter_get_mode(adapter, &mode, NULL, NULL, NULL); if (mode == MODE_OFF && mgmt_powered(info->current_settings)) { mgmt_set_powered(index, FALSE); return; diff --git a/src/adapter.c b/src/adapter.c index d9ab4fda7..b9879bde0 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -114,7 +114,6 @@ struct btd_adapter { uint32_t dev_class; /* Class of Device */ char *name; /* adapter name */ gboolean allow_name_changes; /* whether the adapter name can be changed */ - guint discov_timeout_id; /* discoverable timeout id */ guint stop_discov_id; /* stop inquiry/scanning id */ uint32_t discov_timeout; /* discoverable time(sec) */ guint pairable_timeout_id; /* pairable timeout id */ @@ -215,54 +214,6 @@ static void adapter_set_limited_discoverable(struct btd_adapter *adapter, adapter_ops->set_limited_discoverable(adapter->dev_id, limited); } -static void adapter_remove_discov_timeout(struct btd_adapter *adapter) -{ - if (!adapter) - return; - - if (adapter->discov_timeout_id == 0) - return; - - g_source_remove(adapter->discov_timeout_id); - adapter->discov_timeout_id = 0; -} - -static gboolean discov_timeout_handler(gpointer user_data) -{ - struct btd_adapter *adapter = user_data; - - adapter->discov_timeout_id = 0; - - adapter_ops->set_discoverable(adapter->dev_id, FALSE); - - return FALSE; -} - -static void adapter_set_discov_timeout(struct btd_adapter *adapter, - guint interval) -{ - if (adapter->discov_timeout_id) { - g_source_remove(adapter->discov_timeout_id); - adapter->discov_timeout_id = 0; - } - - if (interval == 0) { - adapter_set_limited_discoverable(adapter, FALSE); - return; - } - - /* Set limited discoverable if pairable and interval between 0 to 60 - sec */ - if (adapter->pairable && interval <= 60) - adapter_set_limited_discoverable(adapter, TRUE); - else - adapter_set_limited_discoverable(adapter, FALSE); - - adapter->discov_timeout_id = g_timeout_add_seconds(interval, - discov_timeout_handler, - adapter); -} - static struct session_req *session_ref(struct session_req *req) { req->refcount++; @@ -302,22 +253,12 @@ static int adapter_set_mode(struct btd_adapter *adapter, uint8_t mode) int err; if (mode == MODE_CONNECTABLE) - err = adapter_ops->set_discoverable(adapter->dev_id, FALSE); + err = adapter_ops->set_discoverable(adapter->dev_id, FALSE, 0); else - err = adapter_ops->set_discoverable(adapter->dev_id, TRUE); - - if (err < 0) - return err; - - if (mode == MODE_CONNECTABLE) - return 0; - - adapter_remove_discov_timeout(adapter); - - if (adapter->discov_timeout) - adapter_set_discov_timeout(adapter, adapter->discov_timeout); + err = adapter_ops->set_discoverable(adapter->dev_id, TRUE, + adapter->discov_timeout); - return 0; + return err; } static struct session_req *find_session_by_msg(GSList *list, const DBusMessage *msg) @@ -710,7 +651,7 @@ static DBusMessage *set_discoverable_timeout(DBusConnection *conn, return dbus_message_new_method_return(msg); if (adapter->scan_mode & SCAN_INQUIRY) - adapter_set_discov_timeout(adapter, timeout); + adapter_ops->set_discoverable(adapter->dev_id, TRUE, timeout); adapter->discov_timeout = timeout; @@ -2194,7 +2135,9 @@ static void emit_device_disappeared(gpointer data, gpointer user_data) } void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode, - uint8_t *on_mode, gboolean *pairable) + uint8_t *on_mode, + uint16_t *discoverable_timeout, + gboolean *pairable) { char str[14], address[18]; @@ -2212,6 +2155,9 @@ void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode, if (on_mode) *on_mode = get_mode(&adapter->bdaddr, "on"); + if (discoverable_timeout) + *discoverable_timeout = get_discoverable_timeout(address); + if (pairable) *pairable = adapter->pairable; } @@ -2349,12 +2295,6 @@ int btd_adapter_stop(struct btd_adapter *adapter) { gboolean prop_false = FALSE; - /* cancel pending timeout */ - if (adapter->discov_timeout_id) { - g_source_remove(adapter->discov_timeout_id); - adapter->discov_timeout_id = 0; - } - /* check pending requests */ reply_pending_requests(adapter); @@ -2984,8 +2924,6 @@ void adapter_mode_changed(struct btd_adapter *adapter, uint8_t scan_mode) if (adapter->scan_mode == scan_mode) return; - adapter_remove_discov_timeout(adapter); - switch (scan_mode) { case SCAN_DISABLED: adapter->mode = MODE_OFF; @@ -3001,18 +2939,7 @@ void adapter_mode_changed(struct btd_adapter *adapter, uint8_t scan_mode) adapter->mode = MODE_DISCOVERABLE; discoverable = TRUE; pairable = adapter->pairable; - if (adapter->discov_timeout != 0) - adapter_set_discov_timeout(adapter, - adapter->discov_timeout); break; - case SCAN_INQUIRY: - /* Address the scenario where a low-level application like - * hciconfig changed the scan mode */ - if (adapter->discov_timeout != 0) - adapter_set_discov_timeout(adapter, - adapter->discov_timeout); - - /* ignore, this event should not be sent */ default: /* ignore, reserved */ return; @@ -3024,9 +2951,6 @@ void adapter_mode_changed(struct btd_adapter *adapter, uint8_t scan_mode) ADAPTER_INTERFACE, "Pairable", DBUS_TYPE_BOOLEAN, &pairable); - if (!discoverable) - adapter_set_limited_discoverable(adapter, FALSE); - emit_property_changed(connection, path, ADAPTER_INTERFACE, "Discoverable", DBUS_TYPE_BOOLEAN, &discoverable); diff --git a/src/adapter.h b/src/adapter.h index e6ee2da8e..c8de28912 100644 --- a/src/adapter.h +++ b/src/adapter.h @@ -87,7 +87,9 @@ void btd_adapter_start(struct btd_adapter *adapter); int btd_adapter_stop(struct btd_adapter *adapter); void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode, - uint8_t *on_mode, gboolean *pairable); + uint8_t *on_mode, + uint16_t *discoverable_timeout, + gboolean *pairable); void btd_adapter_get_class(struct btd_adapter *adapter, uint8_t *major, uint8_t *minor); @@ -184,7 +186,8 @@ struct btd_adapter_ops { int (*setup) (void); void (*cleanup) (void); int (*set_powered) (int index, gboolean powered); - int (*set_discoverable) (int index, gboolean discoverable); + int (*set_discoverable) (int index, gboolean discoverable, + uint16_t timeout); int (*set_pairable) (int index, gboolean pairable); int (*set_limited_discoverable) (int index, gboolean limited); int (*start_discovery) (int index); -- 2.47.3