diff options
-rw-r--r-- | src/adapter.c | 51 | ||||
-rw-r--r-- | src/adapter.h | 7 | ||||
-rw-r--r-- | src/dbus-hci.c | 117 |
3 files changed, 114 insertions, 61 deletions
diff --git a/src/adapter.c b/src/adapter.c index 0657b838..d2ca9636 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -1090,7 +1090,7 @@ static DBusMessage *create_bonding(DBusConnection *conn, DBusMessage *msg, str2ba(address, &bdaddr); /* check if there is a pending discover: requested by D-Bus/non clients */ - if (adapter->discov_active) + if (adapter->state & STD_INQUIRY) return in_progress(msg, "Discover in progress"); pending_remote_name_cancel(adapter); @@ -1175,7 +1175,7 @@ static DBusMessage *adapter_start_periodic(DBusConnection *conn, return invalid_args(msg); } - if (adapter->discov_active || adapter->pdiscov_active) + if ((adapter->state & STD_INQUIRY) || (adapter->state & PERIODIC_INQUIRY)) return in_progress(msg, "Discover in progress"); pending_remote_name_cancel(adapter); @@ -1217,11 +1217,6 @@ static DBusMessage *adapter_start_periodic(DBusConnection *conn, adapter->pdiscov_requestor = g_strdup(dbus_message_get_sender(msg)); - if (adapter->pdiscov_resolve_names) - adapter->discov_type = PERIODIC_INQUIRY | RESOLVE_NAME; - else - adapter->discov_type = PERIODIC_INQUIRY; - hci_close_dev(dd); /* track the request owner to cancel it automatically if the owner @@ -1243,7 +1238,7 @@ static DBusMessage *adapter_stop_periodic(DBusConnection *conn, if (!adapter->up) return adapter_not_ready(msg); - if (!adapter->pdiscov_active) + if (!(adapter->state & PERIODIC_INQUIRY)) return g_dbus_create_error(msg, ERROR_INTERFACE ".NotAuthorized", "Not authorized"); @@ -1290,7 +1285,7 @@ static DBusMessage *adapter_discover_devices(DBusConnection *conn, if (!dbus_message_has_signature(msg, DBUS_TYPE_INVALID_AS_STRING)) return invalid_args(msg); - if (adapter->discov_active) + if (adapter->state & STD_INQUIRY) return in_progress(msg, "Discover in progress"); pending_remote_name_cancel(adapter); @@ -1331,7 +1326,7 @@ static DBusMessage *adapter_discover_devices(DBusConnection *conn, return failed_strerror(msg, bt_error(rp.status)); } - adapter->discov_type |= (STD_INQUIRY | RESOLVE_NAME); + adapter->state |= (STD_INQUIRY | RESOLVE_NAME); adapter->discov_requestor = g_strdup(dbus_message_get_sender(msg)); @@ -1361,7 +1356,7 @@ static DBusMessage *adapter_cancel_discovery(DBusConnection *conn, /* is there discover pending? or discovery cancel was requested * previously */ - if (!adapter->discov_active || adapter->discovery_cancel) + if (!(adapter->state & STD_INQUIRY) || adapter->discovery_cancel) return g_dbus_create_error(msg, ERROR_INTERFACE ".NotAuthorized", "Not Authorized"); @@ -1404,6 +1399,7 @@ static DBusMessage *get_properties(DBusConnection *conn, DBusMessageIter dict; bdaddr_t ba; char str[249]; + gboolean discov_active; if (check_address(adapter->address) < 0) return adapter_not_ready(msg); @@ -1443,9 +1439,11 @@ static DBusMessage *get_properties(DBusConnection *conn, dbus_message_iter_append_dict_entry(&dict, "DiscoverableTimeout", DBUS_TYPE_UINT32, &adapter->discov_timeout); + discov_active = (adapter->state & PERIODIC_INQUIRY) ? TRUE:FALSE; + /* PeriodicDiscovery */ dbus_message_iter_append_dict_entry(&dict, "PeriodicDiscovery", - DBUS_TYPE_BOOLEAN, &adapter->pdiscov_active); + DBUS_TYPE_BOOLEAN, &discov_active); dbus_message_iter_close_container(&iter, &dict); @@ -2227,7 +2225,7 @@ static void adapter_up(struct adapter *adapter, int dd) adapter->up = 1; adapter->discov_timeout = get_discoverable_timeout(adapter->dev_id); - adapter->discov_type = DISCOVER_TYPE_NONE; + adapter->state = DISCOVER_TYPE_NONE; adapter->scan_mode = get_startup_scan(adapter->dev_id); hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, @@ -2355,9 +2353,9 @@ int adapter_start(struct adapter *adapter) setup: if (hci_test_bit(HCI_INQUIRY, &di.flags)) - adapter->discov_active = 1; + adapter->state |= STD_INQUIRY; else - adapter->discov_active = 0; + adapter->state &= ~STD_INQUIRY; adapter_setup(adapter, dd); adapter_up(adapter, dd); @@ -2402,7 +2400,7 @@ static void reply_pending_requests(struct adapter *adapter) adapter->discovery_cancel = NULL; } - if (adapter->discov_active) { + if (adapter->state & STD_INQUIRY) { /* Send discovery completed signal if there isn't name * to resolve */ g_dbus_emit_signal(connection, adapter->path, @@ -2414,7 +2412,7 @@ static void reply_pending_requests(struct adapter *adapter) cancel_discovery(adapter); } - if (adapter->pdiscov_active) { + if (adapter->state & PERIODIC_INQUIRY) { /* Stop periodic inquiry initiated by D-Bus client */ if (adapter->pdiscov_requestor) cancel_periodic_discovery(adapter); @@ -2480,10 +2478,8 @@ int adapter_stop(struct adapter *adapter) adapter->up = 0; adapter->scan_mode = SCAN_DISABLED; adapter->mode = MODE_OFF; - adapter->discov_active = 0; - adapter->pdiscov_active = 0; adapter->pinq_idle = 0; - adapter->discov_type = DISCOVER_TYPE_NONE; + adapter->state = DISCOVER_TYPE_NONE; info("Adapter %s has been disabled", adapter->path); @@ -2572,7 +2568,7 @@ struct adapter *adapter_create(DBusConnection *conn, int id) } adapter->dev_id = id; - adapter->pdiscov_resolve_names = 1; + adapter->state |= RESOLVE_NAME; adapter->path = g_strdup(path); if (!g_dbus_register_interface(conn, path, ADAPTER_INTERFACE, @@ -2722,3 +2718,16 @@ uint8_t adapter_get_mode(struct adapter *adapter) { return adapter->mode; } + +void adapter_set_state(struct adapter *adapter, int state) +{ + if (!adapter) + return; + + adapter->state = state; +} + +int adapter_get_state(struct adapter *adapter) +{ + return adapter->state; +} diff --git a/src/adapter.h b/src/adapter.h index 19a1a9a8..7ea6fb23 100644 --- a/src/adapter.h +++ b/src/adapter.h @@ -108,11 +108,8 @@ struct adapter { uint8_t scan_mode; /* scan mode: SCAN_DISABLED, SCAN_PAGE, SCAN_INQUIRY */ uint8_t mode; /* off, connectable, discoverable, limited */ uint8_t global_mode; /* last valid global mode */ - int discov_active; /* standard discovery active: includes name resolution step */ - int pdiscov_active; /* periodic discovery active */ int pinq_idle; /* tracks the idle time for periodic inquiry */ - int discov_type; /* type requested */ - int pdiscov_resolve_names; /* Resolve names when doing periodic discovery */ + int state; /* standard inq, periodic inq, name resloving */ GSList *found_devices; GSList *oor_devices; /* out of range device list */ char *pdiscov_requestor; /* periodic discovery requestor unique name */ @@ -181,3 +178,5 @@ void adapter_set_scan_mode(struct adapter *adapter, uint8_t scan_mode); uint8_t adapter_get_scan_mode(struct adapter *adapter); void adapter_set_mode(struct adapter *adapter, uint8_t mode); uint8_t adapter_get_mode(struct adapter *adapter); +void adapter_set_state(struct adapter *adapter, int state); +int adapter_get_state(struct adapter *adapter); diff --git a/src/dbus-hci.c b/src/dbus-hci.c index 9b96af7c..9370cf73 100644 --- a/src/dbus-hci.c +++ b/src/dbus-hci.c @@ -800,6 +800,8 @@ void hcid_dbus_inquiry_start(bdaddr_t *local) { struct adapter *adapter; const gchar *path; + gboolean discov_active; + int state; adapter = manager_find_adapter(local); if (!adapter) { @@ -807,23 +809,29 @@ void hcid_dbus_inquiry_start(bdaddr_t *local) return; } - adapter->discov_active = 1; + state = adapter_get_state(adapter); + state |= STD_INQUIRY; + adapter_set_state(adapter, state); /* * Cancel pending remote name request and clean the device list * when inquiry is supported in periodic inquiry idle state. */ - if (adapter->pdiscov_active) + if (adapter_get_state(adapter) & PERIODIC_INQUIRY) pending_remote_name_cancel(adapter); /* Disable name resolution for non D-Bus clients */ - if (!adapter->discov_requestor) - adapter->discov_type &= ~RESOLVE_NAME; - + if (!adapter->discov_requestor) { + state = adapter_get_state(adapter); + state &= ~RESOLVE_NAME; + adapter_set_state(adapter, state); + } path = adapter_get_path(adapter); + discov_active = (adapter_get_state(adapter) & STD_INQUIRY) ? TRUE : FALSE; + dbus_connection_emit_property_changed(connection, path, ADAPTER_INTERFACE, "PeriodicDiscovery", - DBUS_TYPE_BOOLEAN, &adapter->discov_active); + DBUS_TYPE_BOOLEAN, &discov_active); g_dbus_emit_signal(connection, path, ADAPTER_INTERFACE, "DiscoveryStarted", @@ -929,6 +937,7 @@ void hcid_dbus_inquiry_complete(bdaddr_t *local) struct remote_dev_info *dev; bdaddr_t tmp; const gchar *path; + int state; adapter = manager_find_adapter(local); if (!adapter) { @@ -939,7 +948,8 @@ void hcid_dbus_inquiry_complete(bdaddr_t *local) path = adapter_get_path(adapter); /* Out of range verification */ - if (adapter->pdiscov_active && !adapter->discov_active) { + if ((adapter_get_state(adapter) & PERIODIC_INQUIRY) && + !(adapter_get_state(adapter) & STD_INQUIRY)) { GSList *l; send_out_of_range(path, adapter->oor_devices); @@ -964,9 +974,11 @@ void hcid_dbus_inquiry_complete(bdaddr_t *local) * Enable resolution again: standard inquiry can be * received in the periodic inquiry idle state. */ - if (adapter->pdiscov_requestor && adapter->pdiscov_resolve_names) - adapter->discov_type |= RESOLVE_NAME; - + if (adapter->pdiscov_requestor) { + state = adapter_get_state(adapter); + state |= RESOLVE_NAME; + adapter_set_state(adapter, state); + } /* * The following scenarios can happen: * 1. standard inquiry: always send discovery completed signal @@ -982,12 +994,14 @@ void hcid_dbus_inquiry_complete(bdaddr_t *local) if (!found_device_req_name(adapter)) return; /* skip - there is name to resolve */ - if (adapter->discov_active) { + if (adapter_get_state(adapter) & STD_INQUIRY) { g_dbus_emit_signal(connection, path, ADAPTER_INTERFACE, "DiscoveryCompleted", DBUS_TYPE_INVALID); - adapter->discov_active = 0; + state = adapter_get_state(adapter); + state &= ~STD_INQUIRY; + adapter_set_state(adapter, state); } /* free discovered devices list */ @@ -1012,7 +1026,9 @@ void hcid_dbus_inquiry_complete(bdaddr_t *local) } /* reset the discover type for standard inquiry only */ - adapter->discov_type &= ~STD_INQUIRY; + state = adapter_get_state(adapter); + state &= ~STD_INQUIRY; + adapter_set_state(adapter, state); } } @@ -1020,6 +1036,8 @@ void hcid_dbus_periodic_inquiry_start(bdaddr_t *local, uint8_t status) { struct adapter *adapter; const gchar *path; + gboolean discov_active; + int state; /* Don't send the signal if the cmd failed */ if (status) @@ -1031,23 +1049,32 @@ void hcid_dbus_periodic_inquiry_start(bdaddr_t *local, uint8_t status) return; } - adapter->pdiscov_active = 1; + state = adapter_get_state(adapter); + state |= PERIODIC_INQUIRY; + adapter_set_state(adapter, state); /* Disable name resolution for non D-Bus clients */ - if (!adapter->pdiscov_requestor) - adapter->discov_type &= ~RESOLVE_NAME; + if (!adapter->pdiscov_requestor) { + state = adapter_get_state(adapter); + state &= ~RESOLVE_NAME; + adapter_set_state(adapter, state); + } path = adapter_get_path(adapter); + discov_active = (adapter_get_state(adapter) & PERIODIC_INQUIRY) ? TRUE:FALSE; + dbus_connection_emit_property_changed(connection, path, ADAPTER_INTERFACE, "PeriodicDiscovery", - DBUS_TYPE_BOOLEAN, &adapter->pdiscov_active); + DBUS_TYPE_BOOLEAN, &discov_active); } void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status) { struct adapter *adapter; const gchar *path; + gboolean discov_active; + int state; /* Don't send the signal if the cmd failed */ if (status) @@ -1061,8 +1088,9 @@ void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status) /* reset the discover type to be able to handle D-Bus and non D-Bus * requests */ - adapter->pdiscov_active = 0; - adapter->discov_type &= ~(PERIODIC_INQUIRY | RESOLVE_NAME); + state = adapter_get_state(adapter); + state &= ~(PERIODIC_INQUIRY | RESOLVE_NAME); + adapter_set_state(adapter, state); /* free discovered devices list */ g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); @@ -1085,18 +1113,22 @@ void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status) /* workaround: inquiry completed is not sent when exiting from * periodic inquiry */ - if (adapter->discov_active) { + if (adapter_get_state(adapter) & STD_INQUIRY) { g_dbus_emit_signal(connection, path, ADAPTER_INTERFACE, "DiscoveryCompleted", DBUS_TYPE_INVALID); - adapter->discov_active = 0; + state = adapter_get_state(adapter); + state &= ~STD_INQUIRY; + adapter_set_state(adapter, state); } + discov_active = (adapter_get_state(adapter) & STD_INQUIRY) ? TRUE : FALSE; + /* Send discovery completed signal if there isn't name to resolve */ dbus_connection_emit_property_changed(connection, path, ADAPTER_INTERFACE, "PeriodicDiscovery", - DBUS_TYPE_BOOLEAN, &adapter->discov_active); + DBUS_TYPE_BOOLEAN, &discov_active); } static char *extract_eir_name(uint8_t *data, uint8_t *type) @@ -1182,6 +1214,7 @@ void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class, uint8_t name_type = 0x00; name_status_t name_status; const gchar *path; + int state; ba2str(local, local_addr); ba2str(peer, peer_addr); @@ -1201,11 +1234,15 @@ void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class, * workaround to identify situation when the daemon started and * a standard inquiry or periodic inquiry was already running */ - if (!adapter->discov_active && !adapter->pdiscov_active) - adapter->pdiscov_active = 1; + if (!(adapter_get_state(adapter) & STD_INQUIRY) && + !(adapter_get_state(adapter) & PERIODIC_INQUIRY)) { + state = adapter_get_state(adapter); + state |= PERIODIC_INQUIRY; + adapter_set_state(adapter, state); + } /* reset the idle flag when the inquiry complete event arrives */ - if (adapter->pdiscov_active) { + if (adapter_get_state(adapter) & PERIODIC_INQUIRY) { adapter->pinq_idle = 0; /* Out of range list update */ @@ -1229,7 +1266,7 @@ void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class, return; /* the inquiry result can be triggered by NON D-Bus client */ - if (adapter->discov_type & RESOLVE_NAME) + if (adapter_get_state(adapter) & RESOLVE_NAME) name_status = NAME_REQUIRED; else name_status = NAME_NOT_REQUIRED; @@ -1326,6 +1363,7 @@ void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status, const char *paddr = peer_addr; const gchar *dev_path; const gchar *path; + int state; adapter = manager_find_adapter(local); if (!adapter) { @@ -1380,18 +1418,21 @@ void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status, } /* Disable name resolution for non D-Bus clients */ - if (!adapter->pdiscov_requestor) - adapter->discov_type &= ~RESOLVE_NAME; + if (!adapter->pdiscov_requestor) { + state = adapter_get_state(adapter); + state &= ~RESOLVE_NAME; + adapter_set_state(adapter, state); + } } - path = adapter_get_path(adapter); - if (adapter->discov_active) { + if (adapter_get_state(adapter) & STD_INQUIRY) { g_dbus_emit_signal(connection, path, ADAPTER_INTERFACE, "DiscoveryCompleted", DBUS_TYPE_INVALID); - - adapter->discov_active = 0; + state = adapter_get_state(adapter); + state &= ~STD_INQUIRY; + adapter_set_state(adapter, state); } } @@ -1886,8 +1927,9 @@ int cancel_discovery(struct adapter *adapter) GSList *l; int dd, err = 0; uint16_t dev_id = adapter_get_dev_id(adapter); + int state; - if (!adapter->discov_active) + if (!(adapter_get_state(adapter) & STD_INQUIRY)) goto cleanup; dd = hci_open_dev(dev_id); @@ -1933,8 +1975,11 @@ cleanup: adapter->found_devices = NULL; /* Disable name resolution for non D-Bus clients */ - if (!adapter->pdiscov_requestor) - adapter->discov_type &= ~RESOLVE_NAME; + if (!adapter->pdiscov_requestor) { + state = adapter_get_state(adapter); + state &= ~RESOLVE_NAME; + adapter_set_state(adapter, state); + } return err; } @@ -1969,7 +2014,7 @@ int cancel_periodic_discovery(struct adapter *adapter) int dd, err = 0; uint16_t dev_id = adapter_get_dev_id(adapter); - if (!adapter->pdiscov_active) + if (!(adapter_get_state(adapter) & PERIODIC_INQUIRY)) goto cleanup; dd = hci_open_dev(dev_id); |