Lines Matching full:device
52 #include "device.h"
68 /* When all services should trust a remote device */
83 struct btd_device *device;
90 struct btd_device *device;
97 struct btd_device *device;
117 GSList *drivers; /* List of device drivers */
158 if (req->device)
159 btd_device_unref(req->device);
174 struct btd_device *device = req->device;
175 struct btd_adapter *adapter = device->adapter;
178 if (device_is_creating(device, NULL))
179 device_set_temporary(device, TRUE);
183 bt_cancel_discovery(&src, &device->bdaddr);
185 device->browse = NULL;
191 struct btd_device *device = user_data;
192 struct btd_adapter *adapter = device->adapter;
195 if (device->agent)
196 agent_free(device->agent);
198 if (agent && (agent_is_busy(agent, device) ||
199 agent_is_busy(agent, device->authr)))
202 g_slist_foreach(device->services, (GFunc) g_free, NULL);
203 g_slist_free(device->services);
205 g_slist_foreach(device->uuids, (GFunc) g_free, NULL);
206 g_slist_free(device->uuids);
208 g_slist_foreach(device->primaries, (GFunc) g_free, NULL);
209 g_slist_free(device->primaries);
211 if (device->tmp_records)
212 sdp_list_free(device->tmp_records,
215 if (device->disconn_timer)
216 g_source_remove(device->disconn_timer);
218 if (device->discov_timer)
219 g_source_remove(device->discov_timer);
221 DBG("%p", device);
223 g_free(device->authr);
224 g_free(device->path);
225 g_free(device->alias);
226 g_free(device);
229 gboolean device_is_paired(struct btd_device *device)
231 return device->paired;
234 gboolean device_is_trusted(struct btd_device *device)
236 return device->trusted;
242 struct btd_device *device = user_data;
243 struct btd_adapter *adapter = device->adapter;
256 ba2str(&device->bdaddr, dstaddr);
279 ptr = device->name;
283 if (device->alias != NULL)
284 ptr = device->alias;
293 if (read_remote_class(&src, &device->bdaddr, &class) == 0) {
304 boolean = device_is_paired(device);
308 boolean = device_is_trusted(device);
312 boolean = device->blocked;
317 &device->connected);
320 str = g_new0(char *, g_slist_length(device->uuids) + 1);
321 for (i = 0, l = device->uuids; l; l = l->next, i++)
327 str = g_new0(char *, g_slist_length(device->services) + 1);
328 for (i = 0, l = device->services; l; l = l->next, i++)
345 struct btd_device *device = data;
346 struct btd_adapter *adapter = device->adapter;
352 if ((device->alias == NULL && g_str_equal(alias, "")) ||
353 g_strcmp0(device->alias, alias) == 0)
358 ba2str(&device->bdaddr, dstaddr);
366 g_free(device->alias);
367 device->alias = g_str_equal(alias, "") ? NULL : g_strdup(alias);
379 struct btd_device *device = data;
380 struct btd_adapter *adapter = device->adapter;
385 if (device->trusted == value)
390 ba2str(&device->bdaddr, dstaddr);
396 device->trusted = value;
406 struct btd_device *device)
408 driver->remove(device);
410 device->drivers = g_slist_remove(device->drivers, driver);
415 struct btd_device *device = user_data;
417 device->disconn_timer = 0;
419 btd_adapter_disconnect_device(device->adapter, &device->bdaddr);
424 static int device_block(DBusConnection *conn, struct btd_device *device)
429 if (device->blocked)
432 if (device->connected)
433 do_disconnect(device);
435 g_slist_foreach(device->drivers, (GFunc) driver_remove, device);
437 err = btd_adapter_block_address(device->adapter, &device->bdaddr);
441 device->blocked = TRUE;
443 adapter_get_address(device->adapter, &src);
445 err = write_blocked(&src, &device->bdaddr, TRUE);
449 device_set_temporary(device, FALSE);
451 emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Blocked",
452 DBUS_TYPE_BOOLEAN, &device->blocked);
457 static int device_unblock(DBusConnection *conn, struct btd_device *device,
463 if (!device->blocked)
466 err = btd_adapter_unblock_address(device->adapter, &device->bdaddr);
470 device->blocked = FALSE;
472 adapter_get_address(device->adapter, &src);
474 err = write_blocked(&src, &device->bdaddr, FALSE);
479 emit_property_changed(conn, device->path,
481 DBUS_TYPE_BOOLEAN, &device->blocked);
482 device_probe_drivers(device, device->uuids);
491 struct btd_device *device = data;
495 err = device_block(conn, device);
497 err = device_unblock(conn, device, FALSE);
570 struct btd_device *device = user_data;
574 if (device->browse)
582 err = device_browse_sdp(device, conn, msg, NULL, FALSE);
593 err = device_browse_sdp(device, conn, msg, &uuid, FALSE);
685 struct btd_device *device = user_data;
689 if (!device->browse)
692 if (!dbus_message_is_method_call(device->browse->msg, DEVICE_INTERFACE,
696 requestor = browse_request_get_requestor(device->browse);
702 discover_services_reply(device->browse, -ECANCELED, NULL);
704 browse_request_cancel(device->browse);
711 struct btd_device *device = bonding->device;
712 struct btd_adapter *adapter = device->adapter;
714 adapter_cancel_bonding(adapter, &device->bdaddr);
717 void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
721 if (device->bonding)
722 bonding_request_cancel(device->bonding);
724 if (device->browse) {
725 discover_services_reply(device->browse, -ECANCELED, NULL);
726 browse_request_cancel(device->browse);
730 device->disconnects = g_slist_append(device->disconnects,
733 if (device->disconn_timer)
736 while (device->watches) {
737 struct btd_disconnect_data *data = device->watches->data;
740 /* temporary is set if device is going to be removed */
741 data->watch(device, device->temporary,
745 if (!g_slist_find(device->watches, data))
748 device->watches = g_slist_remove(device->watches, data);
752 device->disconn_timer = g_timeout_add_seconds(DISCONNECT_TIMER,
753 do_disconnect, device);
755 g_dbus_emit_signal(conn, device->path,
763 struct btd_device *device = user_data;
765 if (!device->connected)
768 device_request_disconnect(device, msg);
805 struct btd_device *device = user_data;
820 rec = btd_device_get_record(device, pattern);
856 gboolean device_is_connected(struct btd_device *device)
858 return device->connected;
861 void device_add_connection(struct btd_device *device, DBusConnection *conn)
863 if (device->connected) {
865 ba2str(&device->bdaddr, addr);
866 error("Device %s is already connected", addr);
870 device->connected = TRUE;
872 emit_property_changed(conn, device->path,
874 DBUS_TYPE_BOOLEAN, &device->connected);
877 void device_remove_connection(struct btd_device *device, DBusConnection *conn)
879 if (!device->connected) {
881 ba2str(&device->bdaddr, addr);
882 error("Device %s isn't connected", addr);
886 device->connected = FALSE;
888 if (device->disconn_timer > 0) {
889 g_source_remove(device->disconn_timer);
890 device->disconn_timer = 0;
893 while (device->disconnects) {
894 DBusMessage *msg = device->disconnects->data;
897 device->disconnects = g_slist_remove(device->disconnects, msg);
900 if (device_is_paired(device) && !device->bonded)
901 device_set_paired(device, FALSE);
903 emit_property_changed(conn, device->path,
905 DBUS_TYPE_BOOLEAN, &device->connected);
908 guint device_add_disconnect_watch(struct btd_device *device,
921 device->watches = g_slist_append(device->watches, data);
926 void device_remove_disconnect_watch(struct btd_device *device, guint id)
930 for (l = device->watches; l; l = l->next) {
934 device->watches = g_slist_remove(device->watches,
949 struct btd_device *device;
954 device = g_try_malloc0(sizeof(struct btd_device));
955 if (device == NULL)
959 device->path = g_strdup_printf("%s/dev_%s", adapter_path, address_up);
960 g_strdelimit(device->path, ":", '_');
963 DBG("Creating device %s", device->path);
965 if (g_dbus_register_interface(conn, device->path, DEVICE_INTERFACE,
967 device, device_free) == FALSE) {
968 device_free(device);
972 str2ba(address, &device->bdaddr);
973 device->adapter = adapter;
974 device->type = type;
977 read_device_name(srcaddr, address, device->name);
979 device->alias = g_strdup(alias);
980 device->trusted = read_trust(&src, address, GLOBAL_TRUST);
982 if (read_blocked(&src, &device->bdaddr))
983 device_block(conn, device);
985 if (read_link_key(&src, &device->bdaddr, NULL, NULL) == 0) {
986 device->paired = TRUE;
987 device_set_bonded(device, TRUE);
990 return btd_device_ref(device);
993 void device_set_name(struct btd_device *device, const char *name)
997 if (strncmp(name, device->name, MAX_NAME_LENGTH) == 0)
1000 strncpy(device->name, name, MAX_NAME_LENGTH);
1002 emit_property_changed(conn, device->path,
1006 if (device->alias != NULL)
1009 emit_property_changed(conn, device->path,
1014 void device_get_name(struct btd_device *device, char *name, size_t len)
1016 strncpy(name, device->name, len);
1019 device_type_t device_get_type(struct btd_device *device)
1021 return device->type;
1024 void device_remove_bonding(struct btd_device *device)
1030 adapter_get_address(device->adapter, &bdaddr);
1032 ba2str(&device->bdaddr, dstaddr);
1039 device_set_bonded(device, FALSE);
1041 btd_adapter_remove_bonding(device->adapter, &device->bdaddr);
1044 static void device_remove_stored(struct btd_device *device)
1050 adapter_get_address(device->adapter, &src);
1051 ba2str(&device->bdaddr, addr);
1053 if (device->paired)
1054 device_remove_bonding(device);
1059 delete_all_records(&src, &device->bdaddr);
1060 delete_device_service(&src, &device->bdaddr);
1062 if (device->blocked)
1063 device_unblock(conn, device, TRUE);
1066 void device_remove(struct btd_device *device, gboolean remove_stored)
1069 DBG("Removing device %s", device->path);
1071 if (device->agent)
1072 agent_free(device->agent);
1074 if (device->bonding) {
1077 if (device->connected)
1082 device_cancel_bonding(device, status);
1085 if (device->browse) {
1086 discover_services_reply(device->browse, -ECANCELED, NULL);
1087 browse_request_cancel(device->browse);
1090 if (device->connected)
1091 do_disconnect(device);
1094 device_remove_stored(device);
1096 g_slist_foreach(device->drivers, (GFunc) driver_remove, device);
1097 g_slist_free(device->drivers);
1098 device->drivers = NULL;
1100 attrib_client_unregister(device);
1102 btd_device_unref(device);
1105 gint device_address_cmp(struct btd_device *device, const gchar *address)
1109 ba2str(&device->bdaddr, addr);
1137 static GSList *device_match_pattern(struct btd_device *device,
1147 rec = btd_device_get_record(device, profile_uuid);
1158 static GSList *device_match_driver(struct btd_device *device,
1182 match = device_match_pattern(device, *uuid, profiles);
1189 void device_probe_drivers(struct btd_device *device, GSList *profiles)
1195 ba2str(&device->bdaddr, addr);
1197 if (device->blocked) {
1198 DBG("Skipping drivers for blocked device %s", addr);
1208 probe_uuids = device_match_driver(device, driver, profiles);
1213 err = driver->probe(device, probe_uuids);
1215 error("%s driver probe failed for device %s",
1221 device->drivers = g_slist_append(device->drivers, driver);
1227 GSList *l = g_slist_find_custom(device->uuids, list->data,
1232 device->uuids = g_slist_insert_sorted(device->uuids,
1238 static void device_remove_drivers(struct btd_device *device, GSList *uuids)
1240 struct btd_adapter *adapter = device_get_adapter(device);
1248 ba2str(&device->bdaddr, dstaddr);
1250 records = read_records(&src, &device->bdaddr);
1254 for (list = device->drivers; list; list = next) {
1265 DBG("UUID %s was removed from device %s",
1268 driver->remove(device);
1269 device->drivers = g_slist_remove(device->drivers,
1278 device->uuids = g_slist_remove(device->uuids, list->data);
1295 static void services_changed(struct btd_device *device)
1302 uuids = g_new0(char *, g_slist_length(device->uuids) + 1);
1303 for (i = 0, l = device->uuids; l; l = l->next, i++)
1306 emit_array_property_changed(conn, device->path, DEVICE_INTERFACE,
1322 struct btd_device *device = req->device;
1323 struct btd_adapter *adapter = device_get_adapter(device);
1330 ba2str(&device->bdaddr, dstaddr);
1391 l = g_slist_find_custom(device->uuids, profile_uuid,
1408 static void store_profiles(struct btd_device *device)
1410 struct btd_adapter *adapter = device->adapter;
1416 if (!device->uuids) {
1417 write_device_profiles(&src, &device->bdaddr, "");
1421 str = bt_list2string(device->uuids);
1422 write_device_profiles(&src, &device->bdaddr, str);
1426 static void create_device_reply(struct btd_device *device, struct browse_req *req)
1434 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &device->path,
1440 GSList *device_services_from_record(struct btd_device *device, GSList *profiles)
1456 rec = btd_device_get_record(device, profile_uuid);
1482 struct btd_device *device = req->device;
1485 ba2str(&device->bdaddr, addr);
1495 if (device->tmp_records)
1496 sdp_list_free(device->tmp_records,
1499 device->tmp_records = req->records;
1511 device_probe_drivers(device, req->profiles_added);
1513 list = device_services_from_record(device, req->profiles_added);
1515 device_register_services(req->conn, device, list,
1521 device_remove_drivers(device, req->profiles_removed);
1524 services_changed(req->device);
1532 discover_services_reply(req, err, device->tmp_records);
1535 create_device_reply(device, req);
1545 create_device_reply(device, req);
1546 device_set_temporary(device, FALSE);
1550 if (!device->temporary) {
1553 adapter_get_address(device->adapter, &sba);
1554 device_get_address(device, &dba);
1556 store_profiles(device);
1557 write_device_type(&sba, &dba, device->type);
1560 device->browse = NULL;
1567 struct btd_device *device = req->device;
1568 struct btd_adapter *adapter = device->adapter;
1589 bt_search_service(&src, &device->bdaddr, &uuid,
1609 for (l = req->device->uuids; l; l = l->next)
1636 static void store_services(struct btd_device *device)
1638 struct btd_adapter *adapter = device->adapter;
1640 char *str = primary_list_to_string(device->primaries);
1643 device_get_address(device, &dba);
1645 write_device_type(&sba, &dba, device->type);
1654 struct btd_device *device = req->device;
1664 services_changed(device);
1665 device_set_temporary(device, FALSE);
1672 device_probe_drivers(device, uuids);
1674 device_register_services(req->conn, device, g_slist_copy(services), -1);
1678 create_device_reply(device, req);
1680 store_services(device);
1683 device->browse = NULL;
1690 struct btd_device *device = req->device;
1700 device->browse = NULL;
1712 int device_browse_primary(struct btd_device *device, DBusConnection *conn,
1715 struct btd_adapter *adapter = device->adapter;
1721 if (device->browse)
1725 req->device = btd_device_ref(device);
1733 BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
1747 device->browse = req;
1764 int device_browse_sdp(struct btd_device *device, DBusConnection *conn,
1767 struct btd_adapter *adapter = device->adapter;
1774 if (device->browse)
1780 req->device = btd_device_ref(device);
1790 err = bt_search_service(&src, &device->bdaddr, &uuid, cb, req, NULL);
1800 device->browse = req;
1817 struct btd_adapter *device_get_adapter(struct btd_device *device)
1819 if (!device)
1822 return device->adapter;
1825 void device_get_address(struct btd_device *device, bdaddr_t *bdaddr)
1827 bacpy(bdaddr, &device->bdaddr);
1830 const gchar *device_get_path(struct btd_device *device)
1832 if (!device)
1835 return device->path;
1838 struct agent *device_get_agent(struct btd_device *device)
1840 if (!device)
1843 if (device->agent)
1844 return device->agent;
1846 return adapter_get_agent(device->adapter);
1849 gboolean device_is_busy(struct btd_device *device)
1851 return device->browse ? TRUE : FALSE;
1854 gboolean device_is_temporary(struct btd_device *device)
1856 return device->temporary;
1859 void device_set_temporary(struct btd_device *device, gboolean temporary)
1861 if (!device)
1866 device->temporary = temporary;
1869 void device_set_bonded(struct btd_device *device, gboolean bonded)
1871 if (!device)
1876 device->bonded = bonded;
1879 void device_set_type(struct btd_device *device, device_type_t type)
1881 if (!device)
1884 device->type = type;
1889 struct btd_device *device = user_data;
1891 device_browse_sdp(device, NULL, NULL, NULL, TRUE);
1893 device->discov_timer = 0;
1955 struct btd_device *device;
1972 device = bonding->device;
1975 if (!device)
1978 device->bonding = NULL;
1980 adapter_resume_discovery(device->adapter);
1982 if (!device->agent)
1985 agent_cancel(device->agent);
1986 agent_free(device->agent);
1987 device->agent = NULL;
1990 void device_set_paired(struct btd_device *device, gboolean value)
1994 if (device->paired == value)
1997 device->paired = value;
1999 emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Paired",
2005 struct btd_device *device = user_data;
2007 device->agent = NULL;
2009 if (device->authr)
2010 device->authr->agent = NULL;
2015 struct btd_device *device,
2024 ba2str(&device->bdaddr, addr);
2030 agent = agent_create(device->adapter, name, agent_path,
2033 device);
2039 device->agent = agent;
2050 adapter_suspend_discovery(device->adapter);
2057 struct btd_device *device = user_data;
2060 ba2str(&device->bdaddr, addr);
2063 if (device->authr)
2064 device_cancel_authentication(device, FALSE);
2066 if (device->bonding) {
2067 device->bonding->listener_id = 0;
2068 device_request_disconnect(device, NULL);
2072 DBusMessage *device_create_bonding(struct btd_device *device,
2080 struct btd_adapter *adapter = device->adapter;
2087 ba2str(&device->bdaddr, dstaddr);
2089 if (device->bonding)
2102 err = adapter_create_bonding(adapter, &device->bdaddr, capability);
2106 bonding = bonding_request_new(conn, msg, device, agent_path,
2109 adapter_cancel_bonding(adapter, &device->bdaddr);
2115 create_bond_req_exit, device,
2118 device->bonding = bonding;
2119 bonding->device = device;
2124 void device_simple_pairing_complete(struct btd_device *device, uint8_t status)
2126 struct authentication_req *auth = device->authr;
2132 static void device_auth_req_free(struct btd_device *device)
2134 g_free(device->authr);
2135 device->authr = NULL;
2138 void device_bonding_complete(struct btd_device *device, uint8_t status)
2140 struct bonding_req *bonding = device->bonding;
2141 struct authentication_req *auth = device->authr;
2151 device_remove_bonding(device);
2152 device_get_address(device, &bdaddr);
2153 btd_adapter_retry_authentication(device->adapter, &bdaddr);
2156 device_cancel_authentication(device, TRUE);
2157 device_cancel_bonding(device, status);
2161 device_auth_req_free(device);
2164 if (device->paired)
2167 device_set_paired(device, TRUE);
2177 if (device->discov_timer) {
2178 g_source_remove(device->discov_timer);
2179 device->discov_timer = 0;
2182 device_browse_sdp(device, bonding->conn, bonding->msg,
2187 if (!device->browse && !device->discov_timer &&
2193 device->discov_timer = g_timeout_add_seconds(
2196 device);
2201 gboolean device_is_creating(struct btd_device *device, const char *sender)
2205 if (device->bonding && device->bonding->msg)
2206 msg = device->bonding->msg;
2207 else if (device->browse && device->browse->msg)
2208 msg = device->browse->msg;
2224 gboolean device_is_bonding(struct btd_device *device, const char *sender)
2226 struct bonding_req *bonding = device->bonding;
2228 if (!device->bonding)
2237 void device_cancel_bonding(struct btd_device *device, uint8_t status)
2239 struct bonding_req *bonding = device->bonding;
2246 ba2str(&device->bdaddr, addr);
2249 if (device->authr)
2250 device_cancel_authentication(device, FALSE);
2263 struct btd_device *device = auth->device;
2269 ((agent_pincode_cb) auth->cb)(agent, err, pincode, device);
2271 device->authr->cb = NULL;
2272 device->authr->agent = NULL;
2278 struct btd_device *device = auth->device;
2284 ((agent_cb) auth->cb)(agent, err, device);
2286 device->authr->cb = NULL;
2287 device->authr->agent = NULL;
2294 struct btd_device *device = auth->device;
2300 ((agent_passkey_cb) auth->cb)(agent, err, passkey, device);
2302 device->authr->cb = NULL;
2303 device->authr->agent = NULL;
2309 struct btd_device *device = auth->device;
2315 ((agent_cb) auth->cb)(agent, err, device);
2320 int device_request_authentication(struct btd_device *device, auth_type_t type,
2328 ba2str(&device->bdaddr, addr);
2331 if (device->authr) {
2336 agent = device_get_agent(device);
2344 auth->device = device;
2347 device->authr = auth;
2351 err = agent_request_pincode(agent, device, pincode_cb,
2355 err = agent_request_passkey(agent, device, passkey_cb,
2359 err = agent_request_confirmation(agent, device, passkey,
2363 err = agent_display_passkey(agent, device, passkey);
2369 err = agent_request_pairing_consent(agent, device,
2378 device_auth_req_free(device);
2386 struct btd_device *device;
2393 device = auth->device;
2401 ((agent_pincode_cb) auth->cb)(agent, &err, NULL, device);
2404 ((agent_cb) auth->cb)(agent, &err, device);
2407 ((agent_passkey_cb) auth->cb)(agent, &err, 0, device);
2410 ((agent_cb) auth->cb) (agent, &err, device);
2421 void device_cancel_authentication(struct btd_device *device, gboolean aborted)
2423 struct authentication_req *auth = device->authr;
2429 ba2str(&device->bdaddr, addr);
2438 device_auth_req_free(device);
2441 gboolean device_is_authenticating(struct btd_device *device)
2443 return (device->authr != NULL);
2446 gboolean device_is_authorizing(struct btd_device *device)
2448 return device->authorizing;
2451 void device_set_authorizing(struct btd_device *device, gboolean auth)
2453 device->authorizing = auth;
2456 void device_register_services(DBusConnection *conn, struct btd_device *device,
2459 device->services = attrib_client_register(conn, device, psm, NULL,
2461 device->primaries = g_slist_concat(device->primaries, prim_list);
2464 GSList *btd_device_get_primaries(struct btd_device *device)
2466 return device->primaries;
2469 void btd_device_add_uuid(struct btd_device *device, const char *uuid)
2474 if (g_slist_find_custom(device->uuids, uuid,
2481 device_probe_drivers(device, uuid_list);
2486 store_profiles(device);
2487 services_changed(device);
2490 const sdp_record_t *btd_device_get_record(struct btd_device *device,
2495 if (device->tmp_records) {
2498 record = find_record_in_list(device->tmp_records, uuid);
2503 adapter_get_address(device->adapter, &src);
2505 device->tmp_records = read_records(&src, &device->bdaddr);
2506 if (!device->tmp_records)
2509 return find_record_in_list(device->tmp_records, uuid);
2524 struct btd_device *btd_device_ref(struct btd_device *device)
2526 device->ref++;
2528 DBG("%p: ref=%d", device, device->ref);
2530 return device;
2533 void btd_device_unref(struct btd_device *device)
2538 device->ref--;
2540 DBG("%p: ref=%d", device, device->ref);
2542 if (device->ref > 0)
2545 path = g_strdup(device->path);
2552 void device_set_class(struct btd_device *device, uint32_t value)
2556 emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Class",