1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include <errno.h>
36
37 #include <bluetooth/bluetooth.h>
38 #include <bluetooth/uuid.h>
39 #include <bluetooth/sdp.h>
40 #include <bluetooth/sdp_lib.h>
41
42 #include <glib.h>
43 #include <dbus/dbus.h>
44 #include <gdbus.h>
45
46 #include "log.h"
47 #include "textfile.h"
48
49 #include "att.h"
50 #include "hcid.h"
51 #include "adapter.h"
52 #include "device.h"
53 #include "dbus-common.h"
54 #include "event.h"
55 #include "error.h"
56 #include "glib-helper.h"
57 #include "gattrib.h"
58 #include "gatt.h"
59 #include "agent.h"
60 #include "sdp-xml.h"
61 #include "storage.h"
62 #include "btio.h"
63 #include "../attrib/client.h"
64
65 #define DISCONNECT_TIMER 2
66 #define DISCOVERY_TIMER 2
67
68 /* When all services should trust a remote device */
69 #define GLOBAL_TRUST "[all]"
70
71 struct btd_disconnect_data {
72 guint id;
73 disconnect_watch watch;
74 void *user_data;
75 GDestroyNotify destroy;
76 };
77
78 struct bonding_req {
79 DBusConnection *conn;
80 DBusMessage *msg;
81 GIOChannel *io;
82 guint listener_id;
83 struct btd_device *device;
84 };
85
86 struct authentication_req {
87 auth_type_t type;
88 void *cb;
89 struct agent *agent;
90 struct btd_device *device;
91 };
92
93 struct browse_req {
94 DBusConnection *conn;
95 DBusMessage *msg;
96 GAttrib *attrib;
97 struct btd_device *device;
98 GSList *match_uuids;
99 GSList *profiles_added;
100 GSList *profiles_removed;
101 sdp_list_t *records;
102 int search_uuid;
103 int reconnect_attempt;
104 guint listener_id;
105 };
106
107 struct btd_device {
108 bdaddr_t bdaddr;
109 device_type_t type;
110 gchar *path;
111 char name[MAX_NAME_LENGTH + 1];
112 char *alias;
113 struct btd_adapter *adapter;
114 GSList *uuids;
115 GSList *services; /* Primary services path */
116 GSList *primaries; /* List of primary services */
117 GSList *drivers; /* List of device drivers */
118 GSList *watches; /* List of disconnect_data */
119 gboolean temporary;
120 struct agent *agent;
121 guint disconn_timer;
122 guint discov_timer;
123 struct browse_req *browse; /* service discover request */
124 struct bonding_req *bonding;
125 struct authentication_req *authr; /* authentication request */
126 GSList *disconnects; /* disconnects message */
127
128 gboolean connected;
129
130 sdp_list_t *tmp_records;
131
132 gboolean trusted;
133 gboolean paired;
134 gboolean blocked;
135 gboolean bonded;
136
137 gboolean authorizing;
138 gint ref;
139 };
140
141 static uint16_t uuid_list[] = {
142 L2CAP_UUID,
143 PNP_INFO_SVCLASS_ID,
144 PUBLIC_BROWSE_GROUP,
145 0
146 };
147
148 static GSList *device_drivers = NULL;
149
browse_request_free(struct browse_req * req)150 static void browse_request_free(struct browse_req *req)
151 {
152 if (req->listener_id)
153 g_dbus_remove_watch(req->conn, req->listener_id);
154 if (req->msg)
155 dbus_message_unref(req->msg);
156 if (req->conn)
157 dbus_connection_unref(req->conn);
158 if (req->device)
159 btd_device_unref(req->device);
160 g_slist_foreach(req->profiles_added, (GFunc) g_free, NULL);
161 g_slist_free(req->profiles_added);
162 g_slist_free(req->profiles_removed);
163 if (req->records)
164 sdp_list_free(req->records, (sdp_free_func_t) sdp_record_free);
165
166 if (req->attrib)
167 g_attrib_unref(req->attrib);
168
169 g_free(req);
170 }
171
browse_request_cancel(struct browse_req * req)172 static void browse_request_cancel(struct browse_req *req)
173 {
174 struct btd_device *device = req->device;
175 struct btd_adapter *adapter = device->adapter;
176 bdaddr_t src;
177
178 if (device_is_creating(device, NULL))
179 device_set_temporary(device, TRUE);
180
181 adapter_get_address(adapter, &src);
182
183 bt_cancel_discovery(&src, &device->bdaddr);
184
185 device->browse = NULL;
186 browse_request_free(req);
187 }
188
device_free(gpointer user_data)189 static void device_free(gpointer user_data)
190 {
191 struct btd_device *device = user_data;
192 struct btd_adapter *adapter = device->adapter;
193 struct agent *agent = adapter_get_agent(adapter);
194
195 if (device->agent)
196 agent_free(device->agent);
197
198 if (agent && (agent_is_busy(agent, device) ||
199 agent_is_busy(agent, device->authr)))
200 agent_cancel(agent);
201
202 g_slist_foreach(device->services, (GFunc) g_free, NULL);
203 g_slist_free(device->services);
204
205 g_slist_foreach(device->uuids, (GFunc) g_free, NULL);
206 g_slist_free(device->uuids);
207
208 g_slist_foreach(device->primaries, (GFunc) g_free, NULL);
209 g_slist_free(device->primaries);
210
211 if (device->tmp_records)
212 sdp_list_free(device->tmp_records,
213 (sdp_free_func_t) sdp_record_free);
214
215 if (device->disconn_timer)
216 g_source_remove(device->disconn_timer);
217
218 if (device->discov_timer)
219 g_source_remove(device->discov_timer);
220
221 DBG("%p", device);
222
223 g_free(device->authr);
224 g_free(device->path);
225 g_free(device->alias);
226 g_free(device);
227 }
228
device_is_paired(struct btd_device * device)229 gboolean device_is_paired(struct btd_device *device)
230 {
231 return device->paired;
232 }
233
device_is_trusted(struct btd_device * device)234 gboolean device_is_trusted(struct btd_device *device)
235 {
236 return device->trusted;
237 }
238
get_properties(DBusConnection * conn,DBusMessage * msg,void * user_data)239 static DBusMessage *get_properties(DBusConnection *conn,
240 DBusMessage *msg, void *user_data)
241 {
242 struct btd_device *device = user_data;
243 struct btd_adapter *adapter = device->adapter;
244 DBusMessage *reply;
245 DBusMessageIter iter;
246 DBusMessageIter dict;
247 bdaddr_t src;
248 char name[MAX_NAME_LENGTH + 1], srcaddr[18], dstaddr[18];
249 char **str;
250 const char *ptr;
251 dbus_bool_t boolean;
252 uint32_t class;
253 int i;
254 GSList *l;
255
256 ba2str(&device->bdaddr, dstaddr);
257
258 reply = dbus_message_new_method_return(msg);
259 if (!reply)
260 return NULL;
261
262 dbus_message_iter_init_append(reply, &iter);
263
264 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
265 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
266 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
267 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
268
269 /* Address */
270 ptr = dstaddr;
271 dict_append_entry(&dict, "Address", DBUS_TYPE_STRING, &ptr);
272
273 /* Name */
274 ptr = NULL;
275 memset(name, 0, sizeof(name));
276 adapter_get_address(adapter, &src);
277 ba2str(&src, srcaddr);
278
279 ptr = device->name;
280 dict_append_entry(&dict, "Name", DBUS_TYPE_STRING, &ptr);
281
282 /* Alias (fallback to name or address) */
283 if (device->alias != NULL)
284 ptr = device->alias;
285 else if (strlen(ptr) == 0) {
286 g_strdelimit(dstaddr, ":", '-');
287 ptr = dstaddr;
288 }
289
290 dict_append_entry(&dict, "Alias", DBUS_TYPE_STRING, &ptr);
291
292 /* Class */
293 if (read_remote_class(&src, &device->bdaddr, &class) == 0) {
294 const char *icon = class_to_icon(class);
295
296 dict_append_entry(&dict, "Class", DBUS_TYPE_UINT32, &class);
297
298 if (icon)
299 dict_append_entry(&dict, "Icon",
300 DBUS_TYPE_STRING, &icon);
301 }
302
303 /* Paired */
304 boolean = device_is_paired(device);
305 dict_append_entry(&dict, "Paired", DBUS_TYPE_BOOLEAN, &boolean);
306
307 /* Trusted */
308 boolean = device_is_trusted(device);
309 dict_append_entry(&dict, "Trusted", DBUS_TYPE_BOOLEAN, &boolean);
310
311 /* Blocked */
312 boolean = device->blocked;
313 dict_append_entry(&dict, "Blocked", DBUS_TYPE_BOOLEAN, &boolean);
314
315 /* Connected */
316 dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN,
317 &device->connected);
318
319 /* UUIDs */
320 str = g_new0(char *, g_slist_length(device->uuids) + 1);
321 for (i = 0, l = device->uuids; l; l = l->next, i++)
322 str[i] = l->data;
323 dict_append_array(&dict, "UUIDs", DBUS_TYPE_STRING, &str, i);
324 g_free(str);
325
326 /* Services */
327 str = g_new0(char *, g_slist_length(device->services) + 1);
328 for (i = 0, l = device->services; l; l = l->next, i++)
329 str[i] = l->data;
330 dict_append_array(&dict, "Services", DBUS_TYPE_OBJECT_PATH, &str, i);
331 g_free(str);
332
333 /* Adapter */
334 ptr = adapter_get_path(adapter);
335 dict_append_entry(&dict, "Adapter", DBUS_TYPE_OBJECT_PATH, &ptr);
336
337 dbus_message_iter_close_container(&iter, &dict);
338
339 return reply;
340 }
341
set_alias(DBusConnection * conn,DBusMessage * msg,const char * alias,void * data)342 static DBusMessage *set_alias(DBusConnection *conn, DBusMessage *msg,
343 const char *alias, void *data)
344 {
345 struct btd_device *device = data;
346 struct btd_adapter *adapter = device->adapter;
347 char srcaddr[18], dstaddr[18];
348 bdaddr_t src;
349 int err;
350
351 /* No change */
352 if ((device->alias == NULL && g_str_equal(alias, "")) ||
353 g_strcmp0(device->alias, alias) == 0)
354 return dbus_message_new_method_return(msg);
355
356 adapter_get_address(adapter, &src);
357 ba2str(&src, srcaddr);
358 ba2str(&device->bdaddr, dstaddr);
359
360 /* Remove alias if empty string */
361 err = write_device_alias(srcaddr, dstaddr,
362 g_str_equal(alias, "") ? NULL : alias);
363 if (err < 0)
364 return btd_error_failed(msg, strerror(-err));
365
366 g_free(device->alias);
367 device->alias = g_str_equal(alias, "") ? NULL : g_strdup(alias);
368
369 emit_property_changed(conn, dbus_message_get_path(msg),
370 DEVICE_INTERFACE, "Alias",
371 DBUS_TYPE_STRING, &alias);
372
373 return dbus_message_new_method_return(msg);
374 }
375
set_trust(DBusConnection * conn,DBusMessage * msg,gboolean value,void * data)376 static DBusMessage *set_trust(DBusConnection *conn, DBusMessage *msg,
377 gboolean value, void *data)
378 {
379 struct btd_device *device = data;
380 struct btd_adapter *adapter = device->adapter;
381 char srcaddr[18], dstaddr[18];
382 bdaddr_t src;
383 int err;
384
385 if (device->trusted == value)
386 return dbus_message_new_method_return(msg);
387
388 adapter_get_address(adapter, &src);
389 ba2str(&src, srcaddr);
390 ba2str(&device->bdaddr, dstaddr);
391
392 err = write_trust(srcaddr, dstaddr, GLOBAL_TRUST, value);
393 if (err < 0)
394 return btd_error_failed(msg, strerror(-err));
395
396 device->trusted = value;
397
398 emit_property_changed(conn, dbus_message_get_path(msg),
399 DEVICE_INTERFACE, "Trusted",
400 DBUS_TYPE_BOOLEAN, &value);
401
402 return dbus_message_new_method_return(msg);
403 }
404
driver_remove(struct btd_device_driver * driver,struct btd_device * device)405 static void driver_remove(struct btd_device_driver *driver,
406 struct btd_device *device)
407 {
408 driver->remove(device);
409
410 device->drivers = g_slist_remove(device->drivers, driver);
411 }
412
do_disconnect(gpointer user_data)413 static gboolean do_disconnect(gpointer user_data)
414 {
415 struct btd_device *device = user_data;
416
417 device->disconn_timer = 0;
418
419 btd_adapter_disconnect_device(device->adapter, &device->bdaddr);
420
421 return FALSE;
422 }
423
device_block(DBusConnection * conn,struct btd_device * device)424 static int device_block(DBusConnection *conn, struct btd_device *device)
425 {
426 int err;
427 bdaddr_t src;
428
429 if (device->blocked)
430 return 0;
431
432 if (device->connected)
433 do_disconnect(device);
434
435 g_slist_foreach(device->drivers, (GFunc) driver_remove, device);
436
437 err = btd_adapter_block_address(device->adapter, &device->bdaddr);
438 if (err < 0)
439 return err;
440
441 device->blocked = TRUE;
442
443 adapter_get_address(device->adapter, &src);
444
445 err = write_blocked(&src, &device->bdaddr, TRUE);
446 if (err < 0)
447 error("write_blocked(): %s (%d)", strerror(-err), -err);
448
449 device_set_temporary(device, FALSE);
450
451 emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Blocked",
452 DBUS_TYPE_BOOLEAN, &device->blocked);
453
454 return 0;
455 }
456
device_unblock(DBusConnection * conn,struct btd_device * device,gboolean silent)457 static int device_unblock(DBusConnection *conn, struct btd_device *device,
458 gboolean silent)
459 {
460 int err;
461 bdaddr_t src;
462
463 if (!device->blocked)
464 return 0;
465
466 err = btd_adapter_unblock_address(device->adapter, &device->bdaddr);
467 if (err < 0)
468 return err;
469
470 device->blocked = FALSE;
471
472 adapter_get_address(device->adapter, &src);
473
474 err = write_blocked(&src, &device->bdaddr, FALSE);
475 if (err < 0)
476 error("write_blocked(): %s (%d)", strerror(-err), -err);
477
478 if (!silent) {
479 emit_property_changed(conn, device->path,
480 DEVICE_INTERFACE, "Blocked",
481 DBUS_TYPE_BOOLEAN, &device->blocked);
482 device_probe_drivers(device, device->uuids);
483 }
484
485 return 0;
486 }
487
set_blocked(DBusConnection * conn,DBusMessage * msg,gboolean value,void * data)488 static DBusMessage *set_blocked(DBusConnection *conn, DBusMessage *msg,
489 gboolean value, void *data)
490 {
491 struct btd_device *device = data;
492 int err;
493
494 if (value)
495 err = device_block(conn, device);
496 else
497 err = device_unblock(conn, device, FALSE);
498
499 switch (-err) {
500 case 0:
501 return dbus_message_new_method_return(msg);
502 case EINVAL:
503 return btd_error_failed(msg, "Kernel lacks blacklist support");
504 default:
505 return btd_error_failed(msg, strerror(-err));
506 }
507 }
508
set_property(DBusConnection * conn,DBusMessage * msg,void * data)509 static DBusMessage *set_property(DBusConnection *conn,
510 DBusMessage *msg, void *data)
511 {
512 DBusMessageIter iter;
513 DBusMessageIter sub;
514 const char *property;
515
516 if (!dbus_message_iter_init(msg, &iter))
517 return btd_error_invalid_args(msg);
518
519 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
520 return btd_error_invalid_args(msg);
521
522 dbus_message_iter_get_basic(&iter, &property);
523 dbus_message_iter_next(&iter);
524
525 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
526 return btd_error_invalid_args(msg);
527 dbus_message_iter_recurse(&iter, &sub);
528
529 if (g_str_equal("Trusted", property)) {
530 dbus_bool_t value;
531 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
532 return btd_error_invalid_args(msg);
533 dbus_message_iter_get_basic(&sub, &value);
534
535 return set_trust(conn, msg, value, data);
536 } else if (g_str_equal("Alias", property)) {
537 const char *alias;
538
539 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)
540 return btd_error_invalid_args(msg);
541 dbus_message_iter_get_basic(&sub, &alias);
542
543 return set_alias(conn, msg, alias, data);
544 } else if (g_str_equal("Blocked", property)) {
545 dbus_bool_t value;
546
547 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
548 return btd_error_invalid_args(msg);
549
550 dbus_message_iter_get_basic(&sub, &value);
551
552 return set_blocked(conn, msg, value, data);
553 }
554
555 return btd_error_invalid_args(msg);
556 }
557
discover_services_req_exit(DBusConnection * conn,void * user_data)558 static void discover_services_req_exit(DBusConnection *conn, void *user_data)
559 {
560 struct browse_req *req = user_data;
561
562 DBG("DiscoverServices requestor exited");
563
564 browse_request_cancel(req);
565 }
566
discover_services(DBusConnection * conn,DBusMessage * msg,void * user_data)567 static DBusMessage *discover_services(DBusConnection *conn,
568 DBusMessage *msg, void *user_data)
569 {
570 struct btd_device *device = user_data;
571 const char *pattern;
572 int err;
573
574 if (device->browse)
575 return btd_error_in_progress(msg);
576
577 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
578 DBUS_TYPE_INVALID) == FALSE)
579 return btd_error_invalid_args(msg);
580
581 if (strlen(pattern) == 0) {
582 err = device_browse_sdp(device, conn, msg, NULL, FALSE);
583 if (err < 0)
584 goto fail;
585 } else {
586 uuid_t uuid;
587
588 if (bt_string2uuid(&uuid, pattern) < 0)
589 return btd_error_invalid_args(msg);
590
591 sdp_uuid128_to_uuid(&uuid);
592
593 err = device_browse_sdp(device, conn, msg, &uuid, FALSE);
594 if (err < 0)
595 goto fail;
596 }
597
598 return NULL;
599
600 fail:
601 return btd_error_failed(msg, strerror(-err));
602 }
603
browse_request_get_requestor(struct browse_req * req)604 static const char *browse_request_get_requestor(struct browse_req *req)
605 {
606 if (!req->msg)
607 return NULL;
608
609 return dbus_message_get_sender(req->msg);
610 }
611
iter_append_record(DBusMessageIter * dict,uint32_t handle,const char * record)612 static void iter_append_record(DBusMessageIter *dict, uint32_t handle,
613 const char *record)
614 {
615 DBusMessageIter entry;
616
617 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
618 NULL, &entry);
619
620 dbus_message_iter_append_basic(&entry, DBUS_TYPE_UINT32, &handle);
621
622 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &record);
623
624 dbus_message_iter_close_container(dict, &entry);
625 }
626
discover_services_reply(struct browse_req * req,int err,sdp_list_t * recs)627 static void discover_services_reply(struct browse_req *req, int err,
628 sdp_list_t *recs)
629 {
630 DBusMessage *reply;
631 DBusMessageIter iter, dict;
632 sdp_list_t *seq;
633
634 if (err) {
635 const char *err_if;
636
637 if (err == -EHOSTDOWN)
638 err_if = ERROR_INTERFACE ".ConnectionAttemptFailed";
639 else
640 err_if = ERROR_INTERFACE ".Failed";
641
642 reply = dbus_message_new_error(req->msg, err_if,
643 strerror(-err));
644 g_dbus_send_message(req->conn, reply);
645 return;
646 }
647
648 reply = dbus_message_new_method_return(req->msg);
649 if (!reply)
650 return;
651
652 dbus_message_iter_init_append(reply, &iter);
653
654 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
655 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
656 DBUS_TYPE_UINT32_AS_STRING DBUS_TYPE_STRING_AS_STRING
657 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
658
659 for (seq = recs; seq; seq = seq->next) {
660 sdp_record_t *rec = (sdp_record_t *) seq->data;
661 GString *result;
662
663 if (!rec)
664 break;
665
666 result = g_string_new(NULL);
667
668 convert_sdp_record_to_xml(rec, result,
669 (void *) g_string_append);
670
671 if (result->len)
672 iter_append_record(&dict, rec->handle, result->str);
673
674 g_string_free(result, TRUE);
675 }
676
677 dbus_message_iter_close_container(&iter, &dict);
678
679 g_dbus_send_message(req->conn, reply);
680 }
681
cancel_discover(DBusConnection * conn,DBusMessage * msg,void * user_data)682 static DBusMessage *cancel_discover(DBusConnection *conn,
683 DBusMessage *msg, void *user_data)
684 {
685 struct btd_device *device = user_data;
686 const char *sender = dbus_message_get_sender(msg);
687 const char *requestor;
688
689 if (!device->browse)
690 return btd_error_does_not_exist(msg);
691
692 if (!dbus_message_is_method_call(device->browse->msg, DEVICE_INTERFACE,
693 "DiscoverServices"))
694 return btd_error_not_authorized(msg);
695
696 requestor = browse_request_get_requestor(device->browse);
697
698 /* only the discover requestor can cancel the inquiry process */
699 if (!requestor || !g_str_equal(requestor, sender))
700 return btd_error_not_authorized(msg);
701
702 discover_services_reply(device->browse, -ECANCELED, NULL);
703
704 browse_request_cancel(device->browse);
705
706 return dbus_message_new_method_return(msg);
707 }
708
bonding_request_cancel(struct bonding_req * bonding)709 static void bonding_request_cancel(struct bonding_req *bonding)
710 {
711 struct btd_device *device = bonding->device;
712 struct btd_adapter *adapter = device->adapter;
713
714 adapter_cancel_bonding(adapter, &device->bdaddr);
715 }
716
device_request_disconnect(struct btd_device * device,DBusMessage * msg)717 void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
718 {
719 DBusConnection *conn = get_dbus_connection();
720
721 if (device->bonding)
722 bonding_request_cancel(device->bonding);
723
724 if (device->browse) {
725 discover_services_reply(device->browse, -ECANCELED, NULL);
726 browse_request_cancel(device->browse);
727 }
728
729 if (msg)
730 device->disconnects = g_slist_append(device->disconnects,
731 dbus_message_ref(msg));
732
733 if (device->disconn_timer)
734 return;
735
736 while (device->watches) {
737 struct btd_disconnect_data *data = device->watches->data;
738
739 if (data->watch)
740 /* temporary is set if device is going to be removed */
741 data->watch(device, device->temporary,
742 data->user_data);
743
744 /* Check if the watch has been removed by callback function */
745 if (!g_slist_find(device->watches, data))
746 continue;
747
748 device->watches = g_slist_remove(device->watches, data);
749 g_free(data);
750 }
751
752 device->disconn_timer = g_timeout_add_seconds(DISCONNECT_TIMER,
753 do_disconnect, device);
754
755 g_dbus_emit_signal(conn, device->path,
756 DEVICE_INTERFACE, "DisconnectRequested",
757 DBUS_TYPE_INVALID);
758 }
759
disconnect(DBusConnection * conn,DBusMessage * msg,void * user_data)760 static DBusMessage *disconnect(DBusConnection *conn, DBusMessage *msg,
761 void *user_data)
762 {
763 struct btd_device *device = user_data;
764
765 if (!device->connected)
766 return btd_error_not_connected(msg);
767
768 device_request_disconnect(device, msg);
769
770 return NULL;
771 }
772
get_service_attribute_value_reply(DBusMessage * msg,DBusConnection * conn,sdp_data_t * attr)773 static DBusMessage *get_service_attribute_value_reply(DBusMessage *msg, DBusConnection *conn,
774 sdp_data_t *attr)
775 {
776 DBusMessage *reply;
777 DBusMessageIter iter;
778
779 reply = dbus_message_new_method_return(msg);
780 if (!reply)
781 return NULL;
782 sdp_data_t *curr;
783 sdp_list_t *ap = 0;
784 for (; attr; attr = attr->next) {
785 sdp_list_t *pds = 0;
786 for (curr = attr->val.dataseq; curr; curr = curr->next)
787 pds = sdp_list_append(pds, curr->val.dataseq);
788 ap = sdp_list_append(ap, pds);
789 }
790
791 int ch = sdp_get_proto_port(ap, RFCOMM_UUID);
792 sdp_list_foreach(ap, (sdp_list_func_t) sdp_list_free, NULL);
793 sdp_list_free(ap, NULL);
794 ap = NULL;
795
796 dbus_message_append_args(reply, DBUS_TYPE_INT32, &ch, DBUS_TYPE_INVALID);
797
798 return reply;
799 }
800
get_service_attribute_value(DBusConnection * conn,DBusMessage * msg,void * user_data)801 static DBusMessage *get_service_attribute_value(DBusConnection *conn,
802 DBusMessage *msg,
803 void *user_data)
804 {
805 struct btd_device *device = user_data;
806 const sdp_record_t *rec;
807 sdp_data_t *attr_data;
808 const char *pattern;
809 uint16_t attrId;
810 int err;
811
812 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
813 DBUS_TYPE_UINT16, &attrId,
814 DBUS_TYPE_INVALID) == FALSE)
815 goto fail;
816
817 if (strlen(pattern) == 0)
818 return btd_error_invalid_args(msg);
819
820 rec = btd_device_get_record(device, pattern);
821 if (rec == NULL) {
822 error("rec is NULL");
823 goto fail;
824 }
825
826 attr_data = sdp_data_get(rec, attrId);
827
828 if (attr_data == NULL) {
829 error("attr in null");
830 goto fail;
831 }
832 return get_service_attribute_value_reply(msg, conn, attr_data);
833 fail:
834 return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
835 "GetServiceAttribute Failed");
836 }
837
838 static GDBusMethodTable device_methods[] = {
839 { "GetProperties", "", "a{sv}", get_properties },
840 { "SetProperty", "sv", "", set_property },
841 { "DiscoverServices", "s", "a{us}", discover_services,
842 G_DBUS_METHOD_FLAG_ASYNC},
843 { "CancelDiscovery", "", "", cancel_discover },
844 { "Disconnect", "", "", disconnect,
845 G_DBUS_METHOD_FLAG_ASYNC},
846 { "GetServiceAttributeValue", "sq", "i", get_service_attribute_value},
847 { }
848 };
849
850 static GDBusSignalTable device_signals[] = {
851 { "PropertyChanged", "sv" },
852 { "DisconnectRequested", "" },
853 { }
854 };
855
device_is_connected(struct btd_device * device)856 gboolean device_is_connected(struct btd_device *device)
857 {
858 return device->connected;
859 }
860
device_add_connection(struct btd_device * device,DBusConnection * conn)861 void device_add_connection(struct btd_device *device, DBusConnection *conn)
862 {
863 if (device->connected) {
864 char addr[18];
865 ba2str(&device->bdaddr, addr);
866 error("Device %s is already connected", addr);
867 return;
868 }
869
870 device->connected = TRUE;
871
872 emit_property_changed(conn, device->path,
873 DEVICE_INTERFACE, "Connected",
874 DBUS_TYPE_BOOLEAN, &device->connected);
875 }
876
device_remove_connection(struct btd_device * device,DBusConnection * conn)877 void device_remove_connection(struct btd_device *device, DBusConnection *conn)
878 {
879 if (!device->connected) {
880 char addr[18];
881 ba2str(&device->bdaddr, addr);
882 error("Device %s isn't connected", addr);
883 return;
884 }
885
886 device->connected = FALSE;
887
888 if (device->disconn_timer > 0) {
889 g_source_remove(device->disconn_timer);
890 device->disconn_timer = 0;
891 }
892
893 while (device->disconnects) {
894 DBusMessage *msg = device->disconnects->data;
895
896 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
897 device->disconnects = g_slist_remove(device->disconnects, msg);
898 }
899
900 if (device_is_paired(device) && !device->bonded)
901 device_set_paired(device, FALSE);
902
903 emit_property_changed(conn, device->path,
904 DEVICE_INTERFACE, "Connected",
905 DBUS_TYPE_BOOLEAN, &device->connected);
906 }
907
device_add_disconnect_watch(struct btd_device * device,disconnect_watch watch,void * user_data,GDestroyNotify destroy)908 guint device_add_disconnect_watch(struct btd_device *device,
909 disconnect_watch watch, void *user_data,
910 GDestroyNotify destroy)
911 {
912 struct btd_disconnect_data *data;
913 static guint id = 0;
914
915 data = g_new0(struct btd_disconnect_data, 1);
916 data->id = ++id;
917 data->watch = watch;
918 data->user_data = user_data;
919 data->destroy = destroy;
920
921 device->watches = g_slist_append(device->watches, data);
922
923 return data->id;
924 }
925
device_remove_disconnect_watch(struct btd_device * device,guint id)926 void device_remove_disconnect_watch(struct btd_device *device, guint id)
927 {
928 GSList *l;
929
930 for (l = device->watches; l; l = l->next) {
931 struct btd_disconnect_data *data = l->data;
932
933 if (data->id == id) {
934 device->watches = g_slist_remove(device->watches,
935 data);
936 if (data->destroy)
937 data->destroy(data->user_data);
938 g_free(data);
939 return;
940 }
941 }
942 }
943
device_create(DBusConnection * conn,struct btd_adapter * adapter,const gchar * address,device_type_t type)944 struct btd_device *device_create(DBusConnection *conn,
945 struct btd_adapter *adapter,
946 const gchar *address, device_type_t type)
947 {
948 gchar *address_up;
949 struct btd_device *device;
950 const gchar *adapter_path = adapter_get_path(adapter);
951 bdaddr_t src;
952 char srcaddr[18], alias[MAX_NAME_LENGTH + 1];
953
954 device = g_try_malloc0(sizeof(struct btd_device));
955 if (device == NULL)
956 return NULL;
957
958 address_up = g_ascii_strup(address, -1);
959 device->path = g_strdup_printf("%s/dev_%s", adapter_path, address_up);
960 g_strdelimit(device->path, ":", '_');
961 g_free(address_up);
962
963 DBG("Creating device %s", device->path);
964
965 if (g_dbus_register_interface(conn, device->path, DEVICE_INTERFACE,
966 device_methods, device_signals, NULL,
967 device, device_free) == FALSE) {
968 device_free(device);
969 return NULL;
970 }
971
972 str2ba(address, &device->bdaddr);
973 device->adapter = adapter;
974 device->type = type;
975 adapter_get_address(adapter, &src);
976 ba2str(&src, srcaddr);
977 read_device_name(srcaddr, address, device->name);
978 if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
979 device->alias = g_strdup(alias);
980 device->trusted = read_trust(&src, address, GLOBAL_TRUST);
981
982 if (read_blocked(&src, &device->bdaddr))
983 device_block(conn, device);
984
985 if (read_link_key(&src, &device->bdaddr, NULL, NULL) == 0) {
986 device->paired = TRUE;
987 device_set_bonded(device, TRUE);
988 }
989
990 return btd_device_ref(device);
991 }
992
device_set_name(struct btd_device * device,const char * name)993 void device_set_name(struct btd_device *device, const char *name)
994 {
995 DBusConnection *conn = get_dbus_connection();
996
997 if (strncmp(name, device->name, MAX_NAME_LENGTH) == 0)
998 return;
999
1000 strncpy(device->name, name, MAX_NAME_LENGTH);
1001
1002 emit_property_changed(conn, device->path,
1003 DEVICE_INTERFACE, "Name",
1004 DBUS_TYPE_STRING, &name);
1005
1006 if (device->alias != NULL)
1007 return;
1008
1009 emit_property_changed(conn, device->path,
1010 DEVICE_INTERFACE, "Alias",
1011 DBUS_TYPE_STRING, &name);
1012 }
1013
device_get_name(struct btd_device * device,char * name,size_t len)1014 void device_get_name(struct btd_device *device, char *name, size_t len)
1015 {
1016 strncpy(name, device->name, len);
1017 }
1018
device_get_type(struct btd_device * device)1019 device_type_t device_get_type(struct btd_device *device)
1020 {
1021 return device->type;
1022 }
1023
device_remove_bonding(struct btd_device * device)1024 void device_remove_bonding(struct btd_device *device)
1025 {
1026 char filename[PATH_MAX + 1];
1027 char srcaddr[18], dstaddr[18];
1028 bdaddr_t bdaddr;
1029
1030 adapter_get_address(device->adapter, &bdaddr);
1031 ba2str(&bdaddr, srcaddr);
1032 ba2str(&device->bdaddr, dstaddr);
1033
1034 create_name(filename, PATH_MAX, STORAGEDIR, srcaddr,
1035 "linkkeys");
1036
1037 /* Delete the link key from storage */
1038 textfile_casedel(filename, dstaddr);
1039 device_set_bonded(device, FALSE);
1040
1041 btd_adapter_remove_bonding(device->adapter, &device->bdaddr);
1042 }
1043
device_remove_stored(struct btd_device * device)1044 static void device_remove_stored(struct btd_device *device)
1045 {
1046 bdaddr_t src;
1047 char addr[18];
1048 DBusConnection *conn = get_dbus_connection();
1049
1050 adapter_get_address(device->adapter, &src);
1051 ba2str(&device->bdaddr, addr);
1052
1053 if (device->paired)
1054 device_remove_bonding(device);
1055 delete_entry(&src, "profiles", addr);
1056 delete_entry(&src, "trusts", addr);
1057 delete_entry(&src, "types", addr);
1058 delete_entry(&src, "primary", addr);
1059 delete_all_records(&src, &device->bdaddr);
1060 delete_device_service(&src, &device->bdaddr);
1061
1062 if (device->blocked)
1063 device_unblock(conn, device, TRUE);
1064 }
1065
device_remove(struct btd_device * device,gboolean remove_stored)1066 void device_remove(struct btd_device *device, gboolean remove_stored)
1067 {
1068
1069 DBG("Removing device %s", device->path);
1070
1071 if (device->agent)
1072 agent_free(device->agent);
1073
1074 if (device->bonding) {
1075 uint8_t status;
1076
1077 if (device->connected)
1078 status = HCI_OE_USER_ENDED_CONNECTION;
1079 else
1080 status = HCI_PAGE_TIMEOUT;
1081
1082 device_cancel_bonding(device, status);
1083 }
1084
1085 if (device->browse) {
1086 discover_services_reply(device->browse, -ECANCELED, NULL);
1087 browse_request_cancel(device->browse);
1088 }
1089
1090 if (device->connected)
1091 do_disconnect(device);
1092
1093 if (remove_stored)
1094 device_remove_stored(device);
1095
1096 g_slist_foreach(device->drivers, (GFunc) driver_remove, device);
1097 g_slist_free(device->drivers);
1098 device->drivers = NULL;
1099
1100 attrib_client_unregister(device);
1101
1102 btd_device_unref(device);
1103 }
1104
device_address_cmp(struct btd_device * device,const gchar * address)1105 gint device_address_cmp(struct btd_device *device, const gchar *address)
1106 {
1107 char addr[18];
1108
1109 ba2str(&device->bdaddr, addr);
1110 return strcasecmp(addr, address);
1111 }
1112
record_has_uuid(const sdp_record_t * rec,const char * profile_uuid)1113 static gboolean record_has_uuid(const sdp_record_t *rec,
1114 const char *profile_uuid)
1115 {
1116 sdp_list_t *pat;
1117
1118 for (pat = rec->pattern; pat != NULL; pat = pat->next) {
1119 char *uuid;
1120 int ret;
1121
1122 uuid = bt_uuid2string(pat->data);
1123 if (!uuid)
1124 continue;
1125
1126 ret = strcasecmp(uuid, profile_uuid);
1127
1128 g_free(uuid);
1129
1130 if (ret == 0)
1131 return TRUE;
1132 }
1133
1134 return FALSE;
1135 }
1136
device_match_pattern(struct btd_device * device,const char * match_uuid,GSList * profiles)1137 static GSList *device_match_pattern(struct btd_device *device,
1138 const char *match_uuid,
1139 GSList *profiles)
1140 {
1141 GSList *l, *uuids = NULL;
1142
1143 for (l = profiles; l; l = l->next) {
1144 char *profile_uuid = l->data;
1145 const sdp_record_t *rec;
1146
1147 rec = btd_device_get_record(device, profile_uuid);
1148 if (!rec)
1149 continue;
1150
1151 if (record_has_uuid(rec, match_uuid))
1152 uuids = g_slist_append(uuids, profile_uuid);
1153 }
1154
1155 return uuids;
1156 }
1157
device_match_driver(struct btd_device * device,struct btd_device_driver * driver,GSList * profiles)1158 static GSList *device_match_driver(struct btd_device *device,
1159 struct btd_device_driver *driver,
1160 GSList *profiles)
1161 {
1162 const char **uuid;
1163 GSList *uuids = NULL;
1164
1165 for (uuid = driver->uuids; *uuid; uuid++) {
1166 GSList *match;
1167
1168 /* skip duplicated uuids */
1169 if (g_slist_find_custom(uuids, *uuid,
1170 (GCompareFunc) strcasecmp))
1171 continue;
1172
1173 /* match profile driver */
1174 match = g_slist_find_custom(profiles, *uuid,
1175 (GCompareFunc) strcasecmp);
1176 if (match) {
1177 uuids = g_slist_append(uuids, match->data);
1178 continue;
1179 }
1180
1181 /* match pattern driver */
1182 match = device_match_pattern(device, *uuid, profiles);
1183 uuids = g_slist_concat(uuids, match);
1184 }
1185
1186 return uuids;
1187 }
1188
device_probe_drivers(struct btd_device * device,GSList * profiles)1189 void device_probe_drivers(struct btd_device *device, GSList *profiles)
1190 {
1191 GSList *list;
1192 char addr[18];
1193 int err;
1194
1195 ba2str(&device->bdaddr, addr);
1196
1197 if (device->blocked) {
1198 DBG("Skipping drivers for blocked device %s", addr);
1199 goto add_uuids;
1200 }
1201
1202 DBG("Probing drivers for %s", addr);
1203
1204 for (list = device_drivers; list; list = list->next) {
1205 struct btd_device_driver *driver = list->data;
1206 GSList *probe_uuids;
1207
1208 probe_uuids = device_match_driver(device, driver, profiles);
1209
1210 if (!probe_uuids)
1211 continue;
1212
1213 err = driver->probe(device, probe_uuids);
1214 if (err < 0) {
1215 error("%s driver probe failed for device %s",
1216 driver->name, addr);
1217 g_slist_free(probe_uuids);
1218 continue;
1219 }
1220
1221 device->drivers = g_slist_append(device->drivers, driver);
1222 g_slist_free(probe_uuids);
1223 }
1224
1225 add_uuids:
1226 for (list = profiles; list; list = list->next) {
1227 GSList *l = g_slist_find_custom(device->uuids, list->data,
1228 (GCompareFunc) strcasecmp);
1229 if (l)
1230 continue;
1231
1232 device->uuids = g_slist_insert_sorted(device->uuids,
1233 g_strdup(list->data),
1234 (GCompareFunc) strcasecmp);
1235 }
1236 }
1237
device_remove_drivers(struct btd_device * device,GSList * uuids)1238 static void device_remove_drivers(struct btd_device *device, GSList *uuids)
1239 {
1240 struct btd_adapter *adapter = device_get_adapter(device);
1241 GSList *list, *next;
1242 char srcaddr[18], dstaddr[18];
1243 bdaddr_t src;
1244 sdp_list_t *records;
1245
1246 adapter_get_address(adapter, &src);
1247 ba2str(&src, srcaddr);
1248 ba2str(&device->bdaddr, dstaddr);
1249
1250 records = read_records(&src, &device->bdaddr);
1251
1252 DBG("Removing drivers for %s", dstaddr);
1253
1254 for (list = device->drivers; list; list = next) {
1255 struct btd_device_driver *driver = list->data;
1256 const char **uuid;
1257
1258 next = list->next;
1259
1260 for (uuid = driver->uuids; *uuid; uuid++) {
1261 if (!g_slist_find_custom(uuids, *uuid,
1262 (GCompareFunc) strcasecmp))
1263 continue;
1264
1265 DBG("UUID %s was removed from device %s",
1266 *uuid, dstaddr);
1267
1268 driver->remove(device);
1269 device->drivers = g_slist_remove(device->drivers,
1270 driver);
1271 break;
1272 }
1273 }
1274
1275 for (list = uuids; list; list = list->next) {
1276 sdp_record_t *rec;
1277
1278 device->uuids = g_slist_remove(device->uuids, list->data);
1279
1280 rec = find_record_in_list(records, list->data);
1281 if (!rec)
1282 continue;
1283
1284 delete_record(srcaddr, dstaddr, rec->handle);
1285
1286 records = sdp_list_remove(records, rec);
1287 sdp_record_free(rec);
1288
1289 }
1290
1291 if (records)
1292 sdp_list_free(records, (sdp_free_func_t) sdp_record_free);
1293 }
1294
services_changed(struct btd_device * device)1295 static void services_changed(struct btd_device *device)
1296 {
1297 DBusConnection *conn = get_dbus_connection();
1298 char **uuids;
1299 GSList *l;
1300 int i;
1301
1302 uuids = g_new0(char *, g_slist_length(device->uuids) + 1);
1303 for (i = 0, l = device->uuids; l; l = l->next, i++)
1304 uuids[i] = l->data;
1305
1306 emit_array_property_changed(conn, device->path, DEVICE_INTERFACE,
1307 "UUIDs", DBUS_TYPE_STRING, &uuids, i);
1308
1309 g_free(uuids);
1310 }
1311
rec_cmp(const void * a,const void * b)1312 static int rec_cmp(const void *a, const void *b)
1313 {
1314 const sdp_record_t *r1 = a;
1315 const sdp_record_t *r2 = b;
1316
1317 return r1->handle - r2->handle;
1318 }
1319
update_services(struct browse_req * req,sdp_list_t * recs)1320 static void update_services(struct browse_req *req, sdp_list_t *recs)
1321 {
1322 struct btd_device *device = req->device;
1323 struct btd_adapter *adapter = device_get_adapter(device);
1324 sdp_list_t *seq;
1325 char srcaddr[18], dstaddr[18];
1326 bdaddr_t src;
1327
1328 adapter_get_address(adapter, &src);
1329 ba2str(&src, srcaddr);
1330 ba2str(&device->bdaddr, dstaddr);
1331
1332 for (seq = recs; seq; seq = seq->next) {
1333 sdp_record_t *rec = (sdp_record_t *) seq->data;
1334 sdp_list_t *svcclass = NULL;
1335 gchar *profile_uuid;
1336 GSList *l;
1337
1338 if (!rec)
1339 break;
1340
1341 if (sdp_get_service_classes(rec, &svcclass) < 0)
1342 continue;
1343
1344 /* Check for empty service classes list */
1345 if (svcclass == NULL) {
1346 DBG("Skipping record with no service classes");
1347 continue;
1348 }
1349
1350 /* Extract the first element and skip the remainning */
1351 profile_uuid = bt_uuid2string(svcclass->data);
1352 if (!profile_uuid) {
1353 sdp_list_free(svcclass, free);
1354 continue;
1355 }
1356
1357 if (!strcasecmp(profile_uuid, PNP_UUID)) {
1358 uint16_t source, vendor, product, version;
1359 sdp_data_t *pdlist;
1360
1361 pdlist = sdp_data_get(rec, SDP_ATTR_VENDOR_ID_SOURCE);
1362 source = pdlist ? pdlist->val.uint16 : 0x0000;
1363
1364 pdlist = sdp_data_get(rec, SDP_ATTR_VENDOR_ID);
1365 vendor = pdlist ? pdlist->val.uint16 : 0x0000;
1366
1367 pdlist = sdp_data_get(rec, SDP_ATTR_PRODUCT_ID);
1368 product = pdlist ? pdlist->val.uint16 : 0x0000;
1369
1370 pdlist = sdp_data_get(rec, SDP_ATTR_VERSION);
1371 version = pdlist ? pdlist->val.uint16 : 0x0000;
1372
1373 if (source || vendor || product || version)
1374 store_device_id(srcaddr, dstaddr, source,
1375 vendor, product, version);
1376 }
1377
1378 /* Check for duplicates */
1379 if (sdp_list_find(req->records, rec, rec_cmp)) {
1380 g_free(profile_uuid);
1381 sdp_list_free(svcclass, free);
1382 continue;
1383 }
1384
1385 store_record(srcaddr, dstaddr, rec);
1386
1387 /* Copy record */
1388 req->records = sdp_list_append(req->records,
1389 sdp_copy_record(rec));
1390
1391 l = g_slist_find_custom(device->uuids, profile_uuid,
1392 (GCompareFunc) strcmp);
1393 if (!l)
1394 req->profiles_added =
1395 g_slist_append(req->profiles_added,
1396 profile_uuid);
1397 else {
1398 req->profiles_removed =
1399 g_slist_remove(req->profiles_removed,
1400 l->data);
1401 g_free(profile_uuid);
1402 }
1403
1404 sdp_list_free(svcclass, free);
1405 }
1406 }
1407
store_profiles(struct btd_device * device)1408 static void store_profiles(struct btd_device *device)
1409 {
1410 struct btd_adapter *adapter = device->adapter;
1411 bdaddr_t src;
1412 char *str;
1413
1414 adapter_get_address(adapter, &src);
1415
1416 if (!device->uuids) {
1417 write_device_profiles(&src, &device->bdaddr, "");
1418 return;
1419 }
1420
1421 str = bt_list2string(device->uuids);
1422 write_device_profiles(&src, &device->bdaddr, str);
1423 g_free(str);
1424 }
1425
create_device_reply(struct btd_device * device,struct browse_req * req)1426 static void create_device_reply(struct btd_device *device, struct browse_req *req)
1427 {
1428 DBusMessage *reply;
1429
1430 reply = dbus_message_new_method_return(req->msg);
1431 if (!reply)
1432 return;
1433
1434 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &device->path,
1435 DBUS_TYPE_INVALID);
1436
1437 g_dbus_send_message(req->conn, reply);
1438 }
1439
device_services_from_record(struct btd_device * device,GSList * profiles)1440 GSList *device_services_from_record(struct btd_device *device, GSList *profiles)
1441 {
1442 GSList *l, *prim_list = NULL;
1443 char *att_uuid;
1444 uuid_t proto_uuid;
1445
1446 sdp_uuid16_create(&proto_uuid, ATT_UUID);
1447 att_uuid = bt_uuid2string(&proto_uuid);
1448
1449 for (l = profiles; l; l = l->next) {
1450 const char *profile_uuid = l->data;
1451 const sdp_record_t *rec;
1452 struct att_primary *prim;
1453 uint16_t start = 0, end = 0, psm = 0;
1454 uuid_t prim_uuid;
1455
1456 rec = btd_device_get_record(device, profile_uuid);
1457 if (!rec)
1458 continue;
1459
1460 if (!record_has_uuid(rec, att_uuid))
1461 continue;
1462
1463 if (!gatt_parse_record(rec, &prim_uuid, &psm, &start, &end))
1464 continue;
1465
1466 prim = g_new0(struct att_primary, 1);
1467 prim->start = start;
1468 prim->end = end;
1469 sdp_uuid2strn(&prim_uuid, prim->uuid, sizeof(prim->uuid));
1470
1471 prim_list = g_slist_append(prim_list, prim);
1472 }
1473
1474 g_free(att_uuid);
1475
1476 return prim_list;
1477 }
1478
search_cb(sdp_list_t * recs,int err,gpointer user_data)1479 static void search_cb(sdp_list_t *recs, int err, gpointer user_data)
1480 {
1481 struct browse_req *req = user_data;
1482 struct btd_device *device = req->device;
1483 char addr[18];
1484
1485 ba2str(&device->bdaddr, addr);
1486
1487 if (err < 0) {
1488 error("%s: error updating services: %s (%d)",
1489 addr, strerror(-err), -err);
1490 goto send_reply;
1491 }
1492
1493 update_services(req, recs);
1494
1495 if (device->tmp_records)
1496 sdp_list_free(device->tmp_records,
1497 (sdp_free_func_t) sdp_record_free);
1498
1499 device->tmp_records = req->records;
1500 req->records = NULL;
1501
1502 if (!req->profiles_added && !req->profiles_removed) {
1503 DBG("%s: No service update", addr);
1504 goto send_reply;
1505 }
1506
1507 /* Probe matching drivers for services added */
1508 if (req->profiles_added) {
1509 GSList *list;
1510
1511 device_probe_drivers(device, req->profiles_added);
1512
1513 list = device_services_from_record(device, req->profiles_added);
1514 if (list)
1515 device_register_services(req->conn, device, list,
1516 ATT_PSM);
1517 }
1518
1519 /* Remove drivers for services removed */
1520 if (req->profiles_removed)
1521 device_remove_drivers(device, req->profiles_removed);
1522
1523 /* Propagate services changes */
1524 services_changed(req->device);
1525
1526 send_reply:
1527 if (!req->msg)
1528 goto cleanup;
1529
1530 if (dbus_message_is_method_call(req->msg, DEVICE_INTERFACE,
1531 "DiscoverServices"))
1532 discover_services_reply(req, err, device->tmp_records);
1533 else if (dbus_message_is_method_call(req->msg, ADAPTER_INTERFACE,
1534 "CreatePairedDevice"))
1535 create_device_reply(device, req);
1536 else if (dbus_message_is_method_call(req->msg, ADAPTER_INTERFACE,
1537 "CreateDevice")) {
1538 if (err < 0) {
1539 DBusMessage *reply;
1540 reply = btd_error_failed(req->msg, strerror(-err));
1541 g_dbus_send_message(req->conn, reply);
1542 goto cleanup;
1543 }
1544
1545 create_device_reply(device, req);
1546 device_set_temporary(device, FALSE);
1547 }
1548
1549 cleanup:
1550 if (!device->temporary) {
1551 bdaddr_t sba, dba;
1552
1553 adapter_get_address(device->adapter, &sba);
1554 device_get_address(device, &dba);
1555
1556 store_profiles(device);
1557 write_device_type(&sba, &dba, device->type);
1558 }
1559
1560 device->browse = NULL;
1561 browse_request_free(req);
1562 }
1563
browse_cb(sdp_list_t * recs,int err,gpointer user_data)1564 static void browse_cb(sdp_list_t *recs, int err, gpointer user_data)
1565 {
1566 struct browse_req *req = user_data;
1567 struct btd_device *device = req->device;
1568 struct btd_adapter *adapter = device->adapter;
1569 bdaddr_t src;
1570 uuid_t uuid;
1571
1572 /* If we have a valid response and req->search_uuid == 2, then L2CAP
1573 * UUID & PNP searching was successful -- we are done */
1574 if (err < 0 || (req->search_uuid == 2 && req->records)) {
1575 if (err == -ECONNRESET && req->reconnect_attempt < 1) {
1576 req->search_uuid--;
1577 req->reconnect_attempt++;
1578 } else
1579 goto done;
1580 }
1581
1582 update_services(req, recs);
1583
1584 adapter_get_address(adapter, &src);
1585
1586 /* Search for mandatory uuids */
1587 if (uuid_list[req->search_uuid]) {
1588 sdp_uuid16_create(&uuid, uuid_list[req->search_uuid++]);
1589 bt_search_service(&src, &device->bdaddr, &uuid,
1590 browse_cb, user_data, NULL);
1591 return;
1592 }
1593
1594 done:
1595 search_cb(recs, err, user_data);
1596 }
1597
init_browse(struct browse_req * req,gboolean reverse)1598 static void init_browse(struct browse_req *req, gboolean reverse)
1599 {
1600 GSList *l;
1601
1602 /* If we are doing reverse-SDP don't try to detect removed profiles
1603 * since some devices hide their service records while they are
1604 * connected
1605 */
1606 if (reverse)
1607 return;
1608
1609 for (l = req->device->uuids; l; l = l->next)
1610 req->profiles_removed = g_slist_append(req->profiles_removed,
1611 l->data);
1612 }
1613
primary_list_to_string(GSList * primary_list)1614 static char *primary_list_to_string(GSList *primary_list)
1615 {
1616 GString *services;
1617 GSList *l;
1618
1619 services = g_string_new(NULL);
1620
1621 for (l = primary_list; l; l = l->next) {
1622 struct att_primary *primary = l->data;
1623 char service[64];
1624
1625 memset(service, 0, sizeof(service));
1626
1627 snprintf(service, sizeof(service), "%04X#%04X#%s ",
1628 primary->start, primary->end, primary->uuid);
1629
1630 services = g_string_append(services, service);
1631 }
1632
1633 return g_string_free(services, FALSE);
1634 }
1635
store_services(struct btd_device * device)1636 static void store_services(struct btd_device *device)
1637 {
1638 struct btd_adapter *adapter = device->adapter;
1639 bdaddr_t dba, sba;
1640 char *str = primary_list_to_string(device->primaries);
1641
1642 adapter_get_address(adapter, &sba);
1643 device_get_address(device, &dba);
1644
1645 write_device_type(&sba, &dba, device->type);
1646 write_device_services(&sba, &dba, str);
1647
1648 g_free(str);
1649 }
1650
primary_cb(GSList * services,guint8 status,gpointer user_data)1651 static void primary_cb(GSList *services, guint8 status, gpointer user_data)
1652 {
1653 struct browse_req *req = user_data;
1654 struct btd_device *device = req->device;
1655 GSList *l, *uuids = NULL;
1656
1657 if (status) {
1658 DBusMessage *reply;
1659 reply = btd_error_failed(req->msg, att_ecode2str(status));
1660 g_dbus_send_message(req->conn, reply);
1661 goto done;
1662 }
1663
1664 services_changed(device);
1665 device_set_temporary(device, FALSE);
1666
1667 for (l = services; l; l = l->next) {
1668 struct att_primary *prim = l->data;
1669 uuids = g_slist_append(uuids, prim->uuid);
1670 }
1671
1672 device_probe_drivers(device, uuids);
1673
1674 device_register_services(req->conn, device, g_slist_copy(services), -1);
1675
1676 g_slist_free(uuids);
1677
1678 create_device_reply(device, req);
1679
1680 store_services(device);
1681
1682 done:
1683 device->browse = NULL;
1684 browse_request_free(req);
1685 }
1686
gatt_connect_cb(GIOChannel * io,GError * gerr,gpointer user_data)1687 static void gatt_connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
1688 {
1689 struct browse_req *req = user_data;
1690 struct btd_device *device = req->device;
1691
1692 if (gerr) {
1693 DBusMessage *reply;
1694
1695 DBG("%s", gerr->message);
1696
1697 reply = btd_error_failed(req->msg, gerr->message);
1698 g_dbus_send_message(req->conn, reply);
1699
1700 device->browse = NULL;
1701 browse_request_free(req);
1702
1703 return;
1704 }
1705
1706 req->attrib = g_attrib_new(io);
1707 g_io_channel_unref(io);
1708
1709 gatt_discover_primary(req->attrib, NULL, primary_cb, req);
1710 }
1711
device_browse_primary(struct btd_device * device,DBusConnection * conn,DBusMessage * msg,gboolean secure)1712 int device_browse_primary(struct btd_device *device, DBusConnection *conn,
1713 DBusMessage *msg, gboolean secure)
1714 {
1715 struct btd_adapter *adapter = device->adapter;
1716 struct browse_req *req;
1717 BtIOSecLevel sec_level;
1718 GIOChannel *io;
1719 bdaddr_t src;
1720
1721 if (device->browse)
1722 return -EBUSY;
1723
1724 req = g_new0(struct browse_req, 1);
1725 req->device = btd_device_ref(device);
1726
1727 adapter_get_address(adapter, &src);
1728
1729 sec_level = secure ? BT_IO_SEC_HIGH : BT_IO_SEC_LOW;
1730
1731 io = bt_io_connect(BT_IO_L2CAP, gatt_connect_cb, req, NULL, NULL,
1732 BT_IO_OPT_SOURCE_BDADDR, &src,
1733 BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
1734 BT_IO_OPT_CID, ATT_CID,
1735 BT_IO_OPT_SEC_LEVEL, sec_level,
1736 BT_IO_OPT_INVALID);
1737
1738 if (io == NULL ) {
1739 browse_request_free(req);
1740 return -EIO;
1741 }
1742
1743 if (conn == NULL)
1744 conn = get_dbus_connection();
1745
1746 req->conn = dbus_connection_ref(conn);
1747 device->browse = req;
1748
1749 if (msg) {
1750 const char *sender = dbus_message_get_sender(msg);
1751
1752 req->msg = dbus_message_ref(msg);
1753 /* Track the request owner to cancel it
1754 * automatically if the owner exits */
1755 req->listener_id = g_dbus_add_disconnect_watch(conn,
1756 sender,
1757 discover_services_req_exit,
1758 req, NULL);
1759 }
1760
1761 return 0;
1762 }
1763
device_browse_sdp(struct btd_device * device,DBusConnection * conn,DBusMessage * msg,uuid_t * search,gboolean reverse)1764 int device_browse_sdp(struct btd_device *device, DBusConnection *conn,
1765 DBusMessage *msg, uuid_t *search, gboolean reverse)
1766 {
1767 struct btd_adapter *adapter = device->adapter;
1768 struct browse_req *req;
1769 bt_callback_t cb;
1770 bdaddr_t src;
1771 uuid_t uuid;
1772 int err;
1773
1774 if (device->browse)
1775 return -EBUSY;
1776
1777 adapter_get_address(adapter, &src);
1778
1779 req = g_new0(struct browse_req, 1);
1780 req->device = btd_device_ref(device);
1781 if (search) {
1782 memcpy(&uuid, search, sizeof(uuid_t));
1783 cb = search_cb;
1784 } else {
1785 sdp_uuid16_create(&uuid, uuid_list[req->search_uuid++]);
1786 init_browse(req, reverse);
1787 cb = browse_cb;
1788 }
1789
1790 err = bt_search_service(&src, &device->bdaddr, &uuid, cb, req, NULL);
1791 if (err < 0) {
1792 browse_request_free(req);
1793 return err;
1794 }
1795
1796 if (conn == NULL)
1797 conn = get_dbus_connection();
1798
1799 req->conn = dbus_connection_ref(conn);
1800 device->browse = req;
1801
1802 if (msg) {
1803 const char *sender = dbus_message_get_sender(msg);
1804
1805 req->msg = dbus_message_ref(msg);
1806 /* Track the request owner to cancel it
1807 * automatically if the owner exits */
1808 req->listener_id = g_dbus_add_disconnect_watch(conn,
1809 sender,
1810 discover_services_req_exit,
1811 req, NULL);
1812 }
1813
1814 return err;
1815 }
1816
device_get_adapter(struct btd_device * device)1817 struct btd_adapter *device_get_adapter(struct btd_device *device)
1818 {
1819 if (!device)
1820 return NULL;
1821
1822 return device->adapter;
1823 }
1824
device_get_address(struct btd_device * device,bdaddr_t * bdaddr)1825 void device_get_address(struct btd_device *device, bdaddr_t *bdaddr)
1826 {
1827 bacpy(bdaddr, &device->bdaddr);
1828 }
1829
device_get_path(struct btd_device * device)1830 const gchar *device_get_path(struct btd_device *device)
1831 {
1832 if (!device)
1833 return NULL;
1834
1835 return device->path;
1836 }
1837
device_get_agent(struct btd_device * device)1838 struct agent *device_get_agent(struct btd_device *device)
1839 {
1840 if (!device)
1841 return NULL;
1842
1843 if (device->agent)
1844 return device->agent;
1845
1846 return adapter_get_agent(device->adapter);
1847 }
1848
device_is_busy(struct btd_device * device)1849 gboolean device_is_busy(struct btd_device *device)
1850 {
1851 return device->browse ? TRUE : FALSE;
1852 }
1853
device_is_temporary(struct btd_device * device)1854 gboolean device_is_temporary(struct btd_device *device)
1855 {
1856 return device->temporary;
1857 }
1858
device_set_temporary(struct btd_device * device,gboolean temporary)1859 void device_set_temporary(struct btd_device *device, gboolean temporary)
1860 {
1861 if (!device)
1862 return;
1863
1864 DBG("temporary %d", temporary);
1865
1866 device->temporary = temporary;
1867 }
1868
device_set_bonded(struct btd_device * device,gboolean bonded)1869 void device_set_bonded(struct btd_device *device, gboolean bonded)
1870 {
1871 if (!device)
1872 return;
1873
1874 DBG("bonded %d", bonded);
1875
1876 device->bonded = bonded;
1877 }
1878
device_set_type(struct btd_device * device,device_type_t type)1879 void device_set_type(struct btd_device *device, device_type_t type)
1880 {
1881 if (!device)
1882 return;
1883
1884 device->type = type;
1885 }
1886
start_discovery(gpointer user_data)1887 static gboolean start_discovery(gpointer user_data)
1888 {
1889 struct btd_device *device = user_data;
1890
1891 device_browse_sdp(device, NULL, NULL, NULL, TRUE);
1892
1893 device->discov_timer = 0;
1894
1895 return FALSE;
1896 }
1897
new_authentication_return(DBusMessage * msg,int status)1898 static DBusMessage *new_authentication_return(DBusMessage *msg, int status)
1899 {
1900 switch (status) {
1901 case 0x00: /* success */
1902 return dbus_message_new_method_return(msg);
1903
1904 case 0x04: /* page timeout */
1905 return dbus_message_new_error(msg,
1906 ERROR_INTERFACE ".ConnectionAttemptFailed",
1907 "Page Timeout");
1908 case 0x08: /* connection timeout */
1909 return dbus_message_new_error(msg,
1910 ERROR_INTERFACE ".ConnectionAttemptFailed",
1911 "Connection Timeout");
1912 case 0x10: /* connection accept timeout */
1913 case 0x22: /* LMP response timeout */
1914 case 0x28: /* instant passed - is this a timeout? */
1915 return dbus_message_new_error(msg,
1916 ERROR_INTERFACE ".AuthenticationTimeout",
1917 "Authentication Timeout");
1918 case 0x17: /* too frequent pairing attempts */
1919 return dbus_message_new_error(msg,
1920 ERROR_INTERFACE ".RepeatedAttempts",
1921 "Repeated Attempts");
1922
1923 case 0x06:
1924 case 0x18: /* pairing not allowed (e.g. gw rejected attempt) */
1925 return dbus_message_new_error(msg,
1926 ERROR_INTERFACE ".AuthenticationRejected",
1927 "Authentication Rejected");
1928
1929 case 0x07: /* memory capacity */
1930 case 0x09: /* connection limit */
1931 case 0x0a: /* synchronous connection limit */
1932 case 0x0d: /* limited resources */
1933 case 0x13: /* user ended the connection */
1934 case 0x14: /* terminated due to low resources */
1935 case 0x16: /* connection terminated */
1936 return dbus_message_new_error(msg,
1937 ERROR_INTERFACE ".AuthenticationCanceled",
1938 "Authentication Canceled");
1939
1940 case 0x05: /* authentication failure */
1941 case 0x0E: /* rejected due to security reasons - is this auth failure? */
1942 case 0x25: /* encryption mode not acceptable - is this auth failure? */
1943 case 0x26: /* link key cannot be changed - is this auth failure? */
1944 case 0x29: /* pairing with unit key unsupported - is this auth failure? */
1945 case 0x2f: /* insufficient security - is this auth failure? */
1946 default:
1947 return dbus_message_new_error(msg,
1948 ERROR_INTERFACE ".AuthenticationFailed",
1949 "Authentication Failed");
1950 }
1951 }
1952
bonding_request_free(struct bonding_req * bonding)1953 static void bonding_request_free(struct bonding_req *bonding)
1954 {
1955 struct btd_device *device;
1956
1957 if (!bonding)
1958 return;
1959
1960 if (bonding->listener_id)
1961 g_dbus_remove_watch(bonding->conn, bonding->listener_id);
1962
1963 if (bonding->msg)
1964 dbus_message_unref(bonding->msg);
1965
1966 if (bonding->conn)
1967 dbus_connection_unref(bonding->conn);
1968
1969 if (bonding->io)
1970 g_io_channel_unref(bonding->io);
1971
1972 device = bonding->device;
1973 g_free(bonding);
1974
1975 if (!device)
1976 return;
1977
1978 device->bonding = NULL;
1979
1980 adapter_resume_discovery(device->adapter);
1981
1982 if (!device->agent)
1983 return;
1984
1985 agent_cancel(device->agent);
1986 agent_free(device->agent);
1987 device->agent = NULL;
1988 }
1989
device_set_paired(struct btd_device * device,gboolean value)1990 void device_set_paired(struct btd_device *device, gboolean value)
1991 {
1992 DBusConnection *conn = get_dbus_connection();
1993
1994 if (device->paired == value)
1995 return;
1996
1997 device->paired = value;
1998
1999 emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Paired",
2000 DBUS_TYPE_BOOLEAN, &value);
2001 }
2002
device_agent_removed(struct agent * agent,void * user_data)2003 static void device_agent_removed(struct agent *agent, void *user_data)
2004 {
2005 struct btd_device *device = user_data;
2006
2007 device->agent = NULL;
2008
2009 if (device->authr)
2010 device->authr->agent = NULL;
2011 }
2012
bonding_request_new(DBusConnection * conn,DBusMessage * msg,struct btd_device * device,const char * agent_path,uint8_t capability)2013 static struct bonding_req *bonding_request_new(DBusConnection *conn,
2014 DBusMessage *msg,
2015 struct btd_device *device,
2016 const char *agent_path,
2017 uint8_t capability)
2018 {
2019 struct bonding_req *bonding;
2020 const char *name = dbus_message_get_sender(msg);
2021 struct agent *agent;
2022 char addr[18];
2023
2024 ba2str(&device->bdaddr, addr);
2025 DBG("Requesting bonding for %s", addr);
2026
2027 if (!agent_path)
2028 goto proceed;
2029
2030 agent = agent_create(device->adapter, name, agent_path,
2031 capability,
2032 device_agent_removed,
2033 device);
2034 if (!agent) {
2035 error("Unable to create a new agent");
2036 return NULL;
2037 }
2038
2039 device->agent = agent;
2040
2041 DBG("Temporary agent registered for %s at %s:%s",
2042 addr, name, agent_path);
2043
2044 proceed:
2045 bonding = g_new0(struct bonding_req, 1);
2046
2047 bonding->conn = dbus_connection_ref(conn);
2048 bonding->msg = dbus_message_ref(msg);
2049
2050 adapter_suspend_discovery(device->adapter);
2051
2052 return bonding;
2053 }
2054
create_bond_req_exit(DBusConnection * conn,void * user_data)2055 static void create_bond_req_exit(DBusConnection *conn, void *user_data)
2056 {
2057 struct btd_device *device = user_data;
2058 char addr[18];
2059
2060 ba2str(&device->bdaddr, addr);
2061 DBG("%s: requestor exited before bonding was completed", addr);
2062
2063 if (device->authr)
2064 device_cancel_authentication(device, FALSE);
2065
2066 if (device->bonding) {
2067 device->bonding->listener_id = 0;
2068 device_request_disconnect(device, NULL);
2069 }
2070 }
2071
device_create_bonding(struct btd_device * device,DBusConnection * conn,DBusMessage * msg,const char * agent_path,uint8_t capability)2072 DBusMessage *device_create_bonding(struct btd_device *device,
2073 DBusConnection *conn,
2074 DBusMessage *msg,
2075 const char *agent_path,
2076 uint8_t capability)
2077 {
2078 char filename[PATH_MAX + 1];
2079 char *str, srcaddr[18], dstaddr[18];
2080 struct btd_adapter *adapter = device->adapter;
2081 struct bonding_req *bonding;
2082 bdaddr_t src;
2083 int err;
2084
2085 adapter_get_address(adapter, &src);
2086 ba2str(&src, srcaddr);
2087 ba2str(&device->bdaddr, dstaddr);
2088
2089 if (device->bonding)
2090 return btd_error_in_progress(msg);
2091
2092 /* check if a link key already exists */
2093 create_name(filename, PATH_MAX, STORAGEDIR, srcaddr,
2094 "linkkeys");
2095
2096 str = textfile_caseget(filename, dstaddr);
2097 if (str) {
2098 free(str);
2099 return btd_error_already_exists(msg);
2100 }
2101
2102 err = adapter_create_bonding(adapter, &device->bdaddr, capability);
2103 if (err < 0)
2104 return btd_error_failed(msg, strerror(-err));
2105
2106 bonding = bonding_request_new(conn, msg, device, agent_path,
2107 capability);
2108 if (!bonding) {
2109 adapter_cancel_bonding(adapter, &device->bdaddr);
2110 return NULL;
2111 }
2112
2113 bonding->listener_id = g_dbus_add_disconnect_watch(conn,
2114 dbus_message_get_sender(msg),
2115 create_bond_req_exit, device,
2116 NULL);
2117
2118 device->bonding = bonding;
2119 bonding->device = device;
2120
2121 return NULL;
2122 }
2123
device_simple_pairing_complete(struct btd_device * device,uint8_t status)2124 void device_simple_pairing_complete(struct btd_device *device, uint8_t status)
2125 {
2126 struct authentication_req *auth = device->authr;
2127
2128 if (auth && auth->type == AUTH_TYPE_NOTIFY && auth->agent)
2129 agent_cancel(auth->agent);
2130 }
2131
device_auth_req_free(struct btd_device * device)2132 static void device_auth_req_free(struct btd_device *device)
2133 {
2134 g_free(device->authr);
2135 device->authr = NULL;
2136 }
2137
device_bonding_complete(struct btd_device * device,uint8_t status)2138 void device_bonding_complete(struct btd_device *device, uint8_t status)
2139 {
2140 struct bonding_req *bonding = device->bonding;
2141 struct authentication_req *auth = device->authr;
2142 bdaddr_t bdaddr;
2143
2144 DBG("bonding %p status 0x%02x", bonding, status);
2145
2146 if (auth && auth->type == AUTH_TYPE_NOTIFY && auth->agent)
2147 agent_cancel(auth->agent);
2148
2149 // Temporary hack till we move to mgmt interface.
2150 if (status == 0x06 && auth == NULL) {
2151 device_remove_bonding(device);
2152 device_get_address(device, &bdaddr);
2153 btd_adapter_retry_authentication(device->adapter, &bdaddr);
2154 return;
2155 } else if (status) {
2156 device_cancel_authentication(device, TRUE);
2157 device_cancel_bonding(device, status);
2158 return;
2159 }
2160
2161 device_auth_req_free(device);
2162
2163 /* If we're already paired nothing more is needed */
2164 if (device->paired)
2165 return;
2166
2167 device_set_paired(device, TRUE);
2168
2169 /* If we were initiators start service discovery immediately.
2170 * However if the other end was the initator wait a few seconds
2171 * before SDP. This is due to potential IOP issues if the other
2172 * end starts doing SDP at the same time as us */
2173 if (bonding) {
2174 DBG("Proceeding with service discovery");
2175 /* If we are initiators remove any discovery timer and just
2176 * start discovering services directly */
2177 if (device->discov_timer) {
2178 g_source_remove(device->discov_timer);
2179 device->discov_timer = 0;
2180 }
2181
2182 device_browse_sdp(device, bonding->conn, bonding->msg,
2183 NULL, FALSE);
2184
2185 bonding_request_free(bonding);
2186 } else {
2187 if (!device->browse && !device->discov_timer &&
2188 main_opts.reverse_sdp) {
2189 /* If we are not initiators and there is no currently
2190 * active discovery or discovery timer, set discovery
2191 * timer */
2192 DBG("setting timer for reverse service discovery");
2193 device->discov_timer = g_timeout_add_seconds(
2194 DISCOVERY_TIMER,
2195 start_discovery,
2196 device);
2197 }
2198 }
2199 }
2200
device_is_creating(struct btd_device * device,const char * sender)2201 gboolean device_is_creating(struct btd_device *device, const char *sender)
2202 {
2203 DBusMessage *msg;
2204
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;
2209 else
2210 return FALSE;
2211
2212 if (!dbus_message_is_method_call(msg, ADAPTER_INTERFACE,
2213 "CreatePairedDevice") &&
2214 !dbus_message_is_method_call(msg, ADAPTER_INTERFACE,
2215 "CreateDevice"))
2216 return FALSE;
2217
2218 if (sender == NULL)
2219 return TRUE;
2220
2221 return g_str_equal(sender, dbus_message_get_sender(msg));
2222 }
2223
device_is_bonding(struct btd_device * device,const char * sender)2224 gboolean device_is_bonding(struct btd_device *device, const char *sender)
2225 {
2226 struct bonding_req *bonding = device->bonding;
2227
2228 if (!device->bonding)
2229 return FALSE;
2230
2231 if (!sender)
2232 return TRUE;
2233
2234 return g_str_equal(sender, dbus_message_get_sender(bonding->msg));
2235 }
2236
device_cancel_bonding(struct btd_device * device,uint8_t status)2237 void device_cancel_bonding(struct btd_device *device, uint8_t status)
2238 {
2239 struct bonding_req *bonding = device->bonding;
2240 DBusMessage *reply;
2241 char addr[18];
2242
2243 if (!bonding)
2244 return;
2245
2246 ba2str(&device->bdaddr, addr);
2247 DBG("Canceling bonding request for %s", addr);
2248
2249 if (device->authr)
2250 device_cancel_authentication(device, FALSE);
2251
2252 reply = new_authentication_return(bonding->msg, status);
2253 g_dbus_send_message(bonding->conn, reply);
2254
2255 bonding_request_cancel(bonding);
2256 bonding_request_free(bonding);
2257 }
2258
pincode_cb(struct agent * agent,DBusError * err,const char * pincode,void * data)2259 static void pincode_cb(struct agent *agent, DBusError *err,
2260 const char *pincode, void *data)
2261 {
2262 struct authentication_req *auth = data;
2263 struct btd_device *device = auth->device;
2264
2265 /* No need to reply anything if the authentication already failed */
2266 if (auth->cb == NULL)
2267 return;
2268
2269 ((agent_pincode_cb) auth->cb)(agent, err, pincode, device);
2270
2271 device->authr->cb = NULL;
2272 device->authr->agent = NULL;
2273 }
2274
confirm_cb(struct agent * agent,DBusError * err,void * data)2275 static void confirm_cb(struct agent *agent, DBusError *err, void *data)
2276 {
2277 struct authentication_req *auth = data;
2278 struct btd_device *device = auth->device;
2279
2280 /* No need to reply anything if the authentication already failed */
2281 if (auth->cb == NULL)
2282 return;
2283
2284 ((agent_cb) auth->cb)(agent, err, device);
2285
2286 device->authr->cb = NULL;
2287 device->authr->agent = NULL;
2288 }
2289
passkey_cb(struct agent * agent,DBusError * err,uint32_t passkey,void * data)2290 static void passkey_cb(struct agent *agent, DBusError *err,
2291 uint32_t passkey, void *data)
2292 {
2293 struct authentication_req *auth = data;
2294 struct btd_device *device = auth->device;
2295
2296 /* No need to reply anything if the authentication already failed */
2297 if (auth->cb == NULL)
2298 return;
2299
2300 ((agent_passkey_cb) auth->cb)(agent, err, passkey, device);
2301
2302 device->authr->cb = NULL;
2303 device->authr->agent = NULL;
2304 }
2305
pairing_consent_cb(struct agent * agent,DBusError * err,void * data)2306 static void pairing_consent_cb(struct agent *agent, DBusError *err, void *data)
2307 {
2308 struct authentication_req *auth = data;
2309 struct btd_device *device = auth->device;
2310
2311 /* No need to reply anything if the authentication already failed */
2312 if (!auth->cb)
2313 return;
2314
2315 ((agent_cb) auth->cb)(agent, err, device);
2316
2317 auth->cb = NULL;
2318 }
2319
device_request_authentication(struct btd_device * device,auth_type_t type,uint32_t passkey,void * cb)2320 int device_request_authentication(struct btd_device *device, auth_type_t type,
2321 uint32_t passkey, void *cb)
2322 {
2323 struct authentication_req *auth;
2324 struct agent *agent;
2325 char addr[18];
2326 int err;
2327
2328 ba2str(&device->bdaddr, addr);
2329 DBG("Requesting agent authentication for %s", addr);
2330
2331 if (device->authr) {
2332 error("Authentication already requested for %s", addr);
2333 return -EALREADY;
2334 }
2335
2336 agent = device_get_agent(device);
2337 if (!agent) {
2338 error("No agent available for request type %d", type);
2339 return -EPERM;
2340 }
2341
2342 auth = g_new0(struct authentication_req, 1);
2343 auth->agent = agent;
2344 auth->device = device;
2345 auth->cb = cb;
2346 auth->type = type;
2347 device->authr = auth;
2348
2349 switch (type) {
2350 case AUTH_TYPE_PINCODE:
2351 err = agent_request_pincode(agent, device, pincode_cb,
2352 auth, NULL);
2353 break;
2354 case AUTH_TYPE_PASSKEY:
2355 err = agent_request_passkey(agent, device, passkey_cb,
2356 auth, NULL);
2357 break;
2358 case AUTH_TYPE_CONFIRM:
2359 err = agent_request_confirmation(agent, device, passkey,
2360 confirm_cb, auth, NULL);
2361 break;
2362 case AUTH_TYPE_NOTIFY:
2363 err = agent_display_passkey(agent, device, passkey);
2364 break;
2365 case AUTH_TYPE_AUTO:
2366 err = 0;
2367 break;
2368 case AUTH_TYPE_PAIRING_CONSENT:
2369 err = agent_request_pairing_consent(agent, device,
2370 pairing_consent_cb, auth, NULL);
2371 break;
2372 default:
2373 err = -EINVAL;
2374 }
2375
2376 if (err < 0) {
2377 error("Failed requesting authentication");
2378 device_auth_req_free(device);
2379 }
2380
2381 return err;
2382 }
2383
cancel_authentication(struct authentication_req * auth)2384 static void cancel_authentication(struct authentication_req *auth)
2385 {
2386 struct btd_device *device;
2387 struct agent *agent;
2388 DBusError err;
2389
2390 if (!auth || !auth->cb)
2391 return;
2392
2393 device = auth->device;
2394 agent = auth->agent;
2395
2396 dbus_error_init(&err);
2397 dbus_set_error_const(&err, "org.bluez.Error.Canceled", NULL);
2398
2399 switch (auth->type) {
2400 case AUTH_TYPE_PINCODE:
2401 ((agent_pincode_cb) auth->cb)(agent, &err, NULL, device);
2402 break;
2403 case AUTH_TYPE_CONFIRM:
2404 ((agent_cb) auth->cb)(agent, &err, device);
2405 break;
2406 case AUTH_TYPE_PASSKEY:
2407 ((agent_passkey_cb) auth->cb)(agent, &err, 0, device);
2408 break;
2409 case AUTH_TYPE_PAIRING_CONSENT:
2410 ((agent_cb) auth->cb) (agent, &err, device);
2411 break;
2412 case AUTH_TYPE_NOTIFY:
2413 /* User Notify doesn't require any reply */
2414 break;
2415 }
2416
2417 dbus_error_free(&err);
2418 auth->cb = NULL;
2419 }
2420
device_cancel_authentication(struct btd_device * device,gboolean aborted)2421 void device_cancel_authentication(struct btd_device *device, gboolean aborted)
2422 {
2423 struct authentication_req *auth = device->authr;
2424 char addr[18];
2425
2426 if (!auth)
2427 return;
2428
2429 ba2str(&device->bdaddr, addr);
2430 DBG("Canceling authentication request for %s", addr);
2431
2432 if (auth->agent)
2433 agent_cancel(auth->agent);
2434
2435 if (!aborted)
2436 cancel_authentication(auth);
2437
2438 device_auth_req_free(device);
2439 }
2440
device_is_authenticating(struct btd_device * device)2441 gboolean device_is_authenticating(struct btd_device *device)
2442 {
2443 return (device->authr != NULL);
2444 }
2445
device_is_authorizing(struct btd_device * device)2446 gboolean device_is_authorizing(struct btd_device *device)
2447 {
2448 return device->authorizing;
2449 }
2450
device_set_authorizing(struct btd_device * device,gboolean auth)2451 void device_set_authorizing(struct btd_device *device, gboolean auth)
2452 {
2453 device->authorizing = auth;
2454 }
2455
device_register_services(DBusConnection * conn,struct btd_device * device,GSList * prim_list,int psm)2456 void device_register_services(DBusConnection *conn, struct btd_device *device,
2457 GSList *prim_list, int psm)
2458 {
2459 device->services = attrib_client_register(conn, device, psm, NULL,
2460 prim_list);
2461 device->primaries = g_slist_concat(device->primaries, prim_list);
2462 }
2463
btd_device_get_primaries(struct btd_device * device)2464 GSList *btd_device_get_primaries(struct btd_device *device)
2465 {
2466 return device->primaries;
2467 }
2468
btd_device_add_uuid(struct btd_device * device,const char * uuid)2469 void btd_device_add_uuid(struct btd_device *device, const char *uuid)
2470 {
2471 GSList *uuid_list;
2472 char *new_uuid;
2473
2474 if (g_slist_find_custom(device->uuids, uuid,
2475 (GCompareFunc) strcasecmp))
2476 return;
2477
2478 new_uuid = g_strdup(uuid);
2479 uuid_list = g_slist_append(NULL, new_uuid);
2480
2481 device_probe_drivers(device, uuid_list);
2482
2483 g_free(new_uuid);
2484 g_slist_free(uuid_list);
2485
2486 store_profiles(device);
2487 services_changed(device);
2488 }
2489
btd_device_get_record(struct btd_device * device,const char * uuid)2490 const sdp_record_t *btd_device_get_record(struct btd_device *device,
2491 const char *uuid)
2492 {
2493 bdaddr_t src;
2494
2495 if (device->tmp_records) {
2496 const sdp_record_t *record;
2497
2498 record = find_record_in_list(device->tmp_records, uuid);
2499 if (record != NULL)
2500 return record;
2501 }
2502
2503 adapter_get_address(device->adapter, &src);
2504
2505 device->tmp_records = read_records(&src, &device->bdaddr);
2506 if (!device->tmp_records)
2507 return NULL;
2508
2509 return find_record_in_list(device->tmp_records, uuid);
2510 }
2511
btd_register_device_driver(struct btd_device_driver * driver)2512 int btd_register_device_driver(struct btd_device_driver *driver)
2513 {
2514 device_drivers = g_slist_append(device_drivers, driver);
2515
2516 return 0;
2517 }
2518
btd_unregister_device_driver(struct btd_device_driver * driver)2519 void btd_unregister_device_driver(struct btd_device_driver *driver)
2520 {
2521 device_drivers = g_slist_remove(device_drivers, driver);
2522 }
2523
btd_device_ref(struct btd_device * device)2524 struct btd_device *btd_device_ref(struct btd_device *device)
2525 {
2526 device->ref++;
2527
2528 DBG("%p: ref=%d", device, device->ref);
2529
2530 return device;
2531 }
2532
btd_device_unref(struct btd_device * device)2533 void btd_device_unref(struct btd_device *device)
2534 {
2535 DBusConnection *conn = get_dbus_connection();
2536 gchar *path;
2537
2538 device->ref--;
2539
2540 DBG("%p: ref=%d", device, device->ref);
2541
2542 if (device->ref > 0)
2543 return;
2544
2545 path = g_strdup(device->path);
2546
2547 g_dbus_unregister_interface(conn, path, DEVICE_INTERFACE);
2548
2549 g_free(path);
2550 }
2551
device_set_class(struct btd_device * device,uint32_t value)2552 void device_set_class(struct btd_device *device, uint32_t value)
2553 {
2554 DBusConnection *conn = get_dbus_connection();
2555
2556 emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Class",
2557 DBUS_TYPE_UINT32, &value);
2558 }
2559