• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2007  Nokia Corporation
6  *  Copyright (C) 2004-2008  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 <errno.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <unistd.h>
34 #include <sys/time.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 
40 #include <bluetooth/bluetooth.h>
41 #include <bluetooth/hci.h>
42 #include <bluetooth/hci_lib.h>
43 #include <bluetooth/sdp.h>
44 #include <bluetooth/sdp_lib.h>
45 
46 #include <glib.h>
47 #include <dbus/dbus.h>
48 #include <gdbus.h>
49 
50 #include "hcid.h"
51 #include "sdpd.h"
52 
53 #include "logging.h"
54 #include "textfile.h"
55 #include "oui.h"
56 
57 #include "adapter.h"
58 #include "device.h"
59 #include "dbus-common.h"
60 #include "dbus-hci.h"
61 #include "dbus-service.h"
62 #include "error.h"
63 #include "glib-helper.h"
64 #include "agent.h"
65 #include "dbus-sdp.h"
66 #include "sdp-xml.h"
67 
68 #define MAX_DEVICES		16
69 #define DISCONNECT_TIMER	2
70 
71 #define DEVICE_INTERFACE "org.bluez.Device"
72 
73 struct browse_req {
74 	DBusConnection *conn;
75 	DBusMessage *msg;
76 	struct device *device;
77 	GSList *uuids_added;
78 	GSList *uuids_removed;
79 	int search_uuid;
80 	gboolean browse;
81 };
82 
83 struct hci_peer {
84 	struct timeval lastseen;
85 	struct timeval lastused;
86 
87 	bdaddr_t bdaddr;
88 	uint32_t class;
89 	int8_t   rssi;
90 	uint8_t  data[240];
91 	uint8_t  name[248];
92 
93 	uint8_t  pscan_rep_mode;
94 	uint8_t  pscan_period_mode;
95 	uint8_t  pscan_mode;
96 	uint16_t clock_offset;
97 
98 	struct hci_peer *next;
99 };
100 
101 struct hci_conn {
102 	bdaddr_t bdaddr;
103 	uint16_t handle;
104 
105 	struct hci_conn *next;
106 };
107 
108 struct hci_dev {
109 	int ignore;
110 
111 	bdaddr_t bdaddr;
112 	uint8_t  features[8];
113 	uint8_t  lmp_ver;
114 	uint16_t lmp_subver;
115 	uint16_t hci_rev;
116 	uint16_t manufacturer;
117 
118 	uint8_t  ssp_mode;
119 	uint8_t  name[248];
120 	uint8_t  class[3];
121 
122 	struct hci_peer *peers;
123 	struct hci_conn *conns;
124 };
125 
126 static struct hci_dev devices[MAX_DEVICES];
127 
128 #define ASSERT_DEV_ID { if (dev_id >= MAX_DEVICES) return -ERANGE; }
129 
130 static GSList *drivers = NULL;
131 
132 static uint16_t uuid_list[] = {
133 	PUBLIC_BROWSE_GROUP,
134 	HID_SVCLASS_ID,
135 	GENERIC_AUDIO_SVCLASS_ID,
136 	ADVANCED_AUDIO_SVCLASS_ID,
137 	AV_REMOTE_SVCLASS_ID,
138 	0
139 };
140 
init_adapters(void)141 void init_adapters(void)
142 {
143 	int i;
144 
145 	for (i = 0; i < MAX_DEVICES; i++)
146 		memset(devices + i, 0, sizeof(struct hci_dev));
147 }
148 
device_read_bdaddr(uint16_t dev_id,bdaddr_t * bdaddr)149 static int device_read_bdaddr(uint16_t dev_id, bdaddr_t *bdaddr)
150 {
151 	int dd, err;
152 
153 	dd = hci_open_dev(dev_id);
154 	if (dd < 0) {
155 		err = errno;
156 		error("Can't open device hci%d: %s (%d)",
157 					dev_id, strerror(err), err);
158 		return -err;
159 	}
160 
161 	if (hci_read_bd_addr(dd, bdaddr, 2000) < 0) {
162 		err = errno;
163 		error("Can't read address for hci%d: %s (%d)",
164 					dev_id, strerror(err), err);
165 		hci_close_dev(dd);
166 		return -err;
167 	}
168 
169 	hci_close_dev(dd);
170 
171 	return 0;
172 }
173 
add_adapter(uint16_t dev_id)174 int add_adapter(uint16_t dev_id)
175 {
176 	struct hci_dev *dev;
177 	struct hci_dev_info di;
178 
179 	ASSERT_DEV_ID;
180 
181 	dev = &devices[dev_id];
182 
183 	if (hci_devinfo(dev_id, &di) < 0) {
184 		dev->ignore = 1;
185 		return -errno;
186 	}
187 
188 	if (hci_test_bit(HCI_RAW, &di.flags)) {
189 		info("Device hci%d is using raw mode", dev_id);
190 		dev->ignore = 1;
191 	}
192 
193 	if (bacmp(&di.bdaddr, BDADDR_ANY))
194 		bacpy(&dev->bdaddr, &di.bdaddr);
195 	else {
196 		int err = device_read_bdaddr(dev_id, &dev->bdaddr);
197 		if (err < 0)
198 			return err;
199 	}
200 	memcpy(dev->features, di.features, 8);
201 
202 	info("Device hci%d has been added", dev_id);
203 
204 	return 0;
205 }
206 
remove_adapter(uint16_t dev_id)207 int remove_adapter(uint16_t dev_id)
208 {
209 	struct hci_dev *dev;
210 
211 	ASSERT_DEV_ID;
212 
213 	dev = &devices[dev_id];
214 
215 	memset(dev, 0, sizeof(struct hci_dev));
216 
217 	info("Device hci%d has been removed", dev_id);
218 
219 	return 0;
220 }
221 
get_inquiry_mode(struct hci_dev * dev)222 static inline uint8_t get_inquiry_mode(struct hci_dev *dev)
223 {
224 	if (dev->features[6] & LMP_EXT_INQ)
225 		return 2;
226 
227 	if (dev->features[3] & LMP_RSSI_INQ)
228 		return 1;
229 
230 	if (dev->manufacturer == 11 &&
231 			dev->hci_rev == 0x00 && dev->lmp_subver == 0x0757)
232 		return 1;
233 
234 	if (dev->manufacturer == 15) {
235 		if (dev->hci_rev == 0x03 && dev->lmp_subver == 0x6963)
236 			return 1;
237 		if (dev->hci_rev == 0x09 && dev->lmp_subver == 0x6963)
238 			return 1;
239 		if (dev->hci_rev == 0x00 && dev->lmp_subver == 0x6965)
240 			return 1;
241 	}
242 
243 	if (dev->manufacturer == 31 &&
244 			dev->hci_rev == 0x2005 && dev->lmp_subver == 0x1805)
245 		return 1;
246 
247 	return 0;
248 }
249 
update_ext_inquiry_response(int dd,struct hci_dev * dev)250 static void update_ext_inquiry_response(int dd, struct hci_dev *dev)
251 {
252 	uint8_t fec = 0, data[240];
253 
254 	if (!(dev->features[6] & LMP_EXT_INQ))
255 		return;
256 
257 	memset(data, 0, sizeof(data));
258 
259 	if (dev->ssp_mode > 0)
260 		create_ext_inquiry_response((char *) dev->name, data);
261 
262 	if (hci_write_ext_inquiry_response(dd, fec, data, 2000) < 0)
263 		error("Can't write extended inquiry response: %s (%d)",
264 						strerror(errno), errno);
265 }
266 
start_adapter(uint16_t dev_id)267 int start_adapter(uint16_t dev_id)
268 {
269 	struct hci_dev *dev;
270 	struct hci_version ver;
271 	uint8_t features[8], inqmode;
272 	uint8_t events[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00 };
273 	char name[249];
274 	int dd, err;
275 
276 	ASSERT_DEV_ID;
277 
278 	dev = &devices[dev_id];
279 
280 	if (dev->ignore)
281 		return 0;
282 
283 	dd = hci_open_dev(dev_id);
284 	if (dd < 0) {
285 		err = errno;
286 		error("Can't open device hci%d: %s (%d)",
287 					dev_id, strerror(err), err);
288 		return -err;
289 	}
290 
291 	if (hci_read_local_version(dd, &ver, 1000) < 0) {
292 		err = errno;
293 		error("Can't read version info for hci%d: %s (%d)",
294 					dev_id, strerror(err), err);
295 		hci_close_dev(dd);
296 		return -err;
297 	}
298 
299 	dev->hci_rev = ver.hci_rev;
300 	dev->lmp_ver = ver.lmp_ver;
301 	dev->lmp_subver = ver.lmp_subver;
302 	dev->manufacturer = ver.manufacturer;
303 
304 	if (hci_read_local_features(dd, features, 1000) < 0) {
305 		err = errno;
306 		error("Can't read features for hci%d: %s (%d)",
307 					dev_id, strerror(err), err);
308 		hci_close_dev(dd);
309 		return -err;
310 	}
311 
312 	memcpy(dev->features, features, 8);
313 
314 	if (hci_read_class_of_dev(dd, dev->class, 1000) < 0) {
315 		err = errno;
316 		error("Can't read class of device on hci%d: %s (%d)",
317 						dev_id, strerror(err), err);
318 		hci_close_dev(dd);
319 		return -err;
320 	}
321 
322 	if (hci_read_local_name(dd, sizeof(name), name, 2000) < 0) {
323 		err = errno;
324 		error("Can't read local name on hci%d: %s (%d)",
325 						dev_id, strerror(err), err);
326 		hci_close_dev(dd);
327 		return -err;
328 	}
329 
330 	memcpy(dev->name, name, 248);
331 
332 	if (!(features[6] & LMP_SIMPLE_PAIR))
333 		goto setup;
334 
335 	if (hcid_dbus_use_experimental()) {
336 		if (ioctl(dd, HCIGETAUTHINFO, NULL) < 0 && errno != EINVAL)
337 			hci_write_simple_pairing_mode(dd, 0x01, 2000);
338 	}
339 
340 	if (hci_read_simple_pairing_mode(dd, &dev->ssp_mode, 1000) < 0) {
341 		err = errno;
342 		error("Can't read simple pairing mode on hci%d: %s (%d)",
343 						dev_id, strerror(err), err);
344 		hci_close_dev(dd);
345 		return -err;
346 	}
347 
348 setup:
349 	if (ver.hci_rev > 1) {
350 		if (features[5] & LMP_SNIFF_SUBR)
351 			events[5] |= 0x20;
352 
353 		if (features[5] & LMP_PAUSE_ENC)
354 			events[5] |= 0x80;
355 
356 		if (features[6] & LMP_EXT_INQ)
357 			events[5] |= 0x40;
358 
359 		if (features[6] & LMP_NFLUSH_PKTS)
360 			events[7] |= 0x01;
361 
362 		if (features[7] & LMP_LSTO)
363 			events[6] |= 0x80;
364 
365 		if (features[6] & LMP_SIMPLE_PAIR) {
366 			events[6] |= 0x01;	/* IO Capability Request */
367 			events[6] |= 0x02;	/* IO Capability Response */
368 			events[6] |= 0x04;	/* User Confirmation Request */
369 			events[6] |= 0x08;	/* User Passkey Request */
370 			events[6] |= 0x10;	/* Remote OOB Data Request */
371 			events[6] |= 0x20;	/* Simple Pairing Complete */
372 			events[7] |= 0x04;	/* User Passkey Notification */
373 			events[7] |= 0x08;	/* Keypress Notification */
374 			events[7] |= 0x10;	/* Remote Host Supported Features Notification */
375 		}
376 
377 		hci_send_cmd(dd, OGF_HOST_CTL, OCF_SET_EVENT_MASK,
378 						sizeof(events), events);
379 	}
380 
381 	if (read_local_name(&dev->bdaddr, name) == 0) {
382 		memcpy(dev->name, name, 248);
383 		hci_write_local_name(dd, name, 5000);
384         }
385 
386 	update_ext_inquiry_response(dd, dev);
387 
388 	inqmode = get_inquiry_mode(dev);
389 	if (inqmode < 1)
390 		goto done;
391 
392 	if (hci_write_inquiry_mode(dd, inqmode, 2000) < 0) {
393 		err = errno;
394 		error("Can't write inquiry mode for hci%d: %s (%d)",
395 						dev_id, strerror(err), err);
396 		hci_close_dev(dd);
397 		return -err;
398 	}
399 
400 done:
401 	hci_close_dev(dd);
402 
403 	info("Device hci%d has been activated", dev_id);
404 
405 	return 0;
406 }
407 
stop_adapter(uint16_t dev_id)408 int stop_adapter(uint16_t dev_id)
409 {
410 	ASSERT_DEV_ID;
411 
412 	info("Device hci%d has been disabled", dev_id);
413 
414 	return 0;
415 }
416 
update_adapter(uint16_t dev_id)417 int update_adapter(uint16_t dev_id)
418 {
419 	struct hci_dev *dev;
420 	int dd;
421 
422 	ASSERT_DEV_ID;
423 
424 	dev = &devices[dev_id];
425 
426 	if (dev->ignore)
427 		return 0;
428 
429 	dd = hci_open_dev(dev_id);
430 	if (dd < 0) {
431 		int err = errno;
432 		error("Can't open device hci%d: %s (%d)",
433 					dev_id, strerror(err), err);
434 		return -err;
435 	}
436 
437 	update_ext_inquiry_response(dd, dev);
438 
439 	hci_close_dev(dd);
440 
441 	return 0;
442 }
443 
get_device_address(uint16_t dev_id,char * address,size_t size)444 int get_device_address(uint16_t dev_id, char *address, size_t size)
445 {
446 	struct hci_dev *dev;
447 
448 	ASSERT_DEV_ID;
449 
450 	if (size < 18)
451 		return -ENOBUFS;
452 
453 	dev = &devices[dev_id];
454 
455 	return ba2str(&dev->bdaddr, address);
456 }
457 
get_device_class(uint16_t dev_id,uint8_t * cls)458 int get_device_class(uint16_t dev_id, uint8_t *cls)
459 {
460 	struct hci_dev *dev;
461 
462 	ASSERT_DEV_ID;
463 
464 	dev = &devices[dev_id];
465 	memcpy(cls, dev->class, 3);
466 
467 	return 0;
468 }
469 
set_device_class(uint16_t dev_id,uint8_t * cls)470 int set_device_class(uint16_t dev_id, uint8_t *cls)
471 {
472 	struct hci_dev *dev;
473 
474 	ASSERT_DEV_ID;
475 	dev = &devices[dev_id];
476 	memcpy(dev->class, cls, 3);
477 
478 	return 0;
479 }
480 
get_device_version(uint16_t dev_id,char * version,size_t size)481 int get_device_version(uint16_t dev_id, char *version, size_t size)
482 {
483 	struct hci_dev *dev;
484 	char edr[7], *tmp;
485 	int err;
486 
487 	ASSERT_DEV_ID;
488 
489 	if (size < 14)
490 		return -ENOBUFS;
491 
492 	dev = &devices[dev_id];
493 
494 	if ((dev->lmp_ver == 0x03 || dev->lmp_ver == 0x04) &&
495 			(dev->features[3] & (LMP_EDR_ACL_2M | LMP_EDR_ACL_3M)))
496 		sprintf(edr, " + EDR");
497 	else
498 		edr[0] = '\0';
499 
500 	tmp = lmp_vertostr(dev->lmp_ver);
501 
502 	if (strlen(tmp) == 0)
503 		err = snprintf(version, size, "not assigned");
504 	else
505 		err = snprintf(version, size, "Bluetooth %s%s", tmp, edr);
506 
507 	bt_free(tmp);
508 
509 	return err;
510 }
511 
digi_revision(uint16_t dev_id,char * revision,size_t size)512 static int digi_revision(uint16_t dev_id, char *revision, size_t size)
513 {
514 	struct hci_request rq;
515 	unsigned char req[] = { 0x07 };
516 	unsigned char buf[102];
517 	int dd, err;
518 
519 	dd = hci_open_dev(dev_id);
520 	if (dd < 0) {
521 		err = errno;
522 		error("Can't open device hci%d: %s (%d)",
523 					dev_id, strerror(err), err);
524 		return -err;
525 	}
526 
527 	memset(&rq, 0, sizeof(rq));
528 	rq.ogf    = OGF_VENDOR_CMD;
529 	rq.ocf    = 0x000e;
530 	rq.cparam = req;
531 	rq.clen   = sizeof(req);
532 	rq.rparam = &buf;
533 	rq.rlen   = sizeof(buf);
534 
535 	if (hci_send_req(dd, &rq, 2000) < 0) {
536 		err = errno;
537 		error("Can't read revision for hci%d: %s (%d)",
538 					dev_id, strerror(err), err);
539 		hci_close_dev(dd);
540 		return -err;
541 	}
542 
543 	hci_close_dev(dd);
544 
545 	return snprintf(revision, size, "%s", buf + 1);
546 }
547 
get_device_revision(uint16_t dev_id,char * revision,size_t size)548 int get_device_revision(uint16_t dev_id, char *revision, size_t size)
549 {
550 	struct hci_dev *dev;
551 	int err;
552 
553 	ASSERT_DEV_ID;
554 
555 	dev = &devices[dev_id];
556 
557 	switch (dev->manufacturer) {
558 	case 10:
559 		err = snprintf(revision, size, "Build %d", dev->lmp_subver);
560 		break;
561 	case 12:
562 		err = digi_revision(dev_id, revision, size);
563 		break;
564 	case 15:
565 		err = snprintf(revision, size, "%d.%d / %d",
566 				dev->hci_rev & 0xff,
567 				dev->lmp_subver >> 8, dev->lmp_subver & 0xff);
568 		break;
569 	default:
570 		err = snprintf(revision, size, "0x%02x", dev->lmp_subver);
571 		break;
572 	}
573 
574 	return err;
575 }
576 
get_device_manufacturer(uint16_t dev_id,char * manufacturer,size_t size)577 int get_device_manufacturer(uint16_t dev_id, char *manufacturer, size_t size)
578 {
579 	char *tmp;
580 
581 	ASSERT_DEV_ID;
582 
583 	tmp = bt_compidtostr(devices[dev_id].manufacturer);
584 
585 	return snprintf(manufacturer, size, "%s", tmp);
586 }
587 
get_device_company(uint16_t dev_id,char * company,size_t size)588 int get_device_company(uint16_t dev_id, char *company, size_t size)
589 {
590 	char *tmp, oui[9];
591 	int err;
592 
593 	ASSERT_DEV_ID;
594 
595 	ba2oui(&devices[dev_id].bdaddr, oui);
596 	tmp = ouitocomp(oui);
597 
598 	err = snprintf(company, size, "%s", tmp);
599 
600 	free(tmp);
601 
602 	return err;
603 }
604 
set_simple_pairing_mode(uint16_t dev_id,uint8_t mode)605 int set_simple_pairing_mode(uint16_t dev_id, uint8_t mode)
606 {
607 	struct hci_dev *dev;
608 	int dd;
609 
610 	ASSERT_DEV_ID;
611 
612 	dev = &devices[dev_id];
613 
614 	dev->ssp_mode = mode;
615 
616 	dd = hci_open_dev(dev_id);
617 	if (dd < 0) {
618 		int err = errno;
619 		error("Can't open device hci%d: %s (%d)",
620 					dev_id, strerror(err), err);
621 		return -err;
622 	}
623 
624 	update_ext_inquiry_response(dd, dev);
625 
626 	hci_close_dev(dd);
627 
628 	return 0;
629 }
630 
get_device_name(uint16_t dev_id,char * name,size_t size)631 int get_device_name(uint16_t dev_id, char *name, size_t size)
632 {
633 	char tmp[249];
634 	int dd, err;
635 
636 	ASSERT_DEV_ID;
637 
638 	memset(tmp, 0, sizeof(tmp));
639 
640 	dd = hci_open_dev(dev_id);
641 	if (dd < 0) {
642 		err = errno;
643 		error("Can't open device hci%d: %s (%d)",
644 					dev_id, strerror(err), err);
645 		return -err;
646 	}
647 
648 	if (hci_read_local_name(dd, sizeof(tmp), tmp, 2000) < 0) {
649 		err = errno;
650 		error("Can't read name for hci%d: %s (%d)",
651 					dev_id, strerror(err), err);
652 		hci_close_dev(dd);
653 		return -err;
654 	}
655 
656 	hci_close_dev(dd);
657 
658 	memcpy(devices[dev_id].name, tmp, 248);
659 
660 	return snprintf(name, size, "%s", tmp);
661 }
662 
set_device_name(uint16_t dev_id,const char * name)663 int set_device_name(uint16_t dev_id, const char *name)
664 {
665 	struct hci_dev *dev;
666 	int dd, err;
667 
668 	ASSERT_DEV_ID;
669 
670 	dev = &devices[dev_id];
671 
672 	dd = hci_open_dev(dev_id);
673 	if (dd < 0) {
674 		err = errno;
675 		error("Can't open device hci%d: %s (%d)",
676 					dev_id, strerror(err), err);
677 		return -err;
678 	}
679 
680 	if (hci_write_local_name(dd, name, 5000) < 0) {
681 		err = errno;
682 		error("Can't write name for hci%d: %s (%d)",
683 					dev_id, strerror(err), err);
684 		hci_close_dev(dd);
685 		return -err;
686 	}
687 
688 	strncpy((char *) dev->name, name, 248);
689 
690 	update_ext_inquiry_response(dd, dev);
691 
692 	hci_close_dev(dd);
693 
694 	return 0;
695 }
696 
get_device_alias(uint16_t dev_id,const bdaddr_t * bdaddr,char * alias,size_t size)697 int get_device_alias(uint16_t dev_id, const bdaddr_t *bdaddr, char *alias, size_t size)
698 {
699 	char filename[PATH_MAX + 1], addr[18], *tmp;
700 	int err;
701 
702 	ASSERT_DEV_ID;
703 
704 	ba2str(&devices[dev_id].bdaddr, addr);
705 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "aliases");
706 
707 	ba2str(bdaddr, addr);
708 
709 	tmp = textfile_get(filename, addr);
710 	if (!tmp)
711 		return -ENXIO;
712 
713 	err = snprintf(alias, size, "%s", tmp);
714 
715 	free(tmp);
716 
717 	return err;
718 }
719 
set_device_alias(uint16_t dev_id,const bdaddr_t * bdaddr,const char * alias)720 int set_device_alias(uint16_t dev_id, const bdaddr_t *bdaddr, const char *alias)
721 {
722 	char filename[PATH_MAX + 1], addr[18];
723 
724 	ASSERT_DEV_ID;
725 
726 	ba2str(&devices[dev_id].bdaddr, addr);
727 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "aliases");
728 
729 	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
730 
731 	ba2str(bdaddr, addr);
732 
733 	return textfile_put(filename, addr, alias);
734 }
735 
remove_device_alias(uint16_t dev_id,const bdaddr_t * bdaddr)736 int remove_device_alias(uint16_t dev_id, const bdaddr_t *bdaddr)
737 {
738 	char filename[PATH_MAX + 1], addr[18];
739 
740 	ASSERT_DEV_ID;
741 
742 	ba2str(&devices[dev_id].bdaddr, addr);
743 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "aliases");
744 
745 	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
746 
747 	ba2str(bdaddr, addr);
748 
749 	return textfile_del(filename, addr);
750 }
751 
get_encryption_key_size(uint16_t dev_id,const bdaddr_t * baddr)752 int get_encryption_key_size(uint16_t dev_id, const bdaddr_t *baddr)
753 {
754 	struct hci_dev *dev;
755 	int size;
756 
757 	ASSERT_DEV_ID;
758 
759 	dev = &devices[dev_id];
760 
761 	switch (dev->manufacturer) {
762 	default:
763 		size = -ENOENT;
764 		break;
765 	}
766 
767 	return size;
768 }
769 
device_free(gpointer user_data)770 static void device_free(gpointer user_data)
771 {
772 	struct device *device = user_data;
773 
774 	if (device->agent)
775 		agent_destroy(device->agent, FALSE);
776 
777 	g_slist_foreach(device->uuids, (GFunc) g_free, NULL);
778 	g_slist_free(device->uuids);
779 
780 	if (device->disconn_timer)
781 		g_source_remove(device->disconn_timer);
782 
783 	g_free(device->address);
784 	g_free(device->path);
785 	g_free(device);
786 }
787 
device_is_paired(struct device * device)788 static gboolean device_is_paired(struct device *device)
789 {
790 	struct adapter *adapter = device->adapter;
791 	char filename[PATH_MAX + 1], *str;
792 	gboolean ret;
793 
794 	create_name(filename, PATH_MAX, STORAGEDIR,
795 			adapter->address, "linkkeys");
796 	str = textfile_caseget(filename, device->address);
797 	ret = str ? TRUE : FALSE;
798 	g_free(str);
799 
800 	return ret;
801 }
802 
device_get_name(struct device * device)803 static char *device_get_name(struct device *device)
804 {
805 	struct adapter *adapter = device->adapter;
806 	char filename[PATH_MAX + 1];
807 
808 	create_name(filename, PATH_MAX, STORAGEDIR, adapter->address, "names");
809 	return textfile_caseget(filename, device->address);
810 }
811 
get_properties(DBusConnection * conn,DBusMessage * msg,void * user_data)812 static DBusMessage *get_properties(DBusConnection *conn,
813 				DBusMessage *msg, void *user_data)
814 {
815 	struct device *device = user_data;
816 	struct adapter *adapter = device->adapter;
817 	DBusMessage *reply;
818 	DBusMessageIter iter;
819 	DBusMessageIter dict;
820 	bdaddr_t src, dst;
821 	char path[MAX_PATH_LENGTH];
822 	char buf[64];
823 	const char *ptr;
824 	char *name, *ppath, **uuids;
825 	dbus_bool_t boolean;
826 	uint32_t class;
827 	int i;
828 	GSList *l;
829 
830 	reply = dbus_message_new_method_return(msg);
831 	if (!reply)
832 		return NULL;
833 
834 	dbus_message_iter_init_append(reply, &iter);
835 
836 	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
837 			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
838 			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
839 			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
840 
841 	/* Address */
842 	dbus_message_iter_append_dict_entry(&dict, "Address", DBUS_TYPE_STRING,
843 			&device->address);
844 
845 	/* Name */
846 	name = device_get_name(device);
847 	if (name) {
848 		dbus_message_iter_append_dict_entry(&dict, "Name",
849 				DBUS_TYPE_STRING, &name);
850 	}
851 
852 	str2ba(adapter->address, &src);
853 	str2ba(device->address, &dst);
854 
855 	/* Class */
856 	if (read_remote_class(&src, &dst, &class) == 0) {
857 		dbus_message_iter_append_dict_entry(&dict, "Class",
858 				DBUS_TYPE_UINT32, &class);
859 	}
860 
861 	/* Alias */
862 	if (get_device_alias(adapter->dev_id, &dst, buf, sizeof(buf)) > 0) {
863 		ptr = buf;
864 		dbus_message_iter_append_dict_entry(&dict, "Alias",
865 				DBUS_TYPE_STRING, &ptr);
866 	} else if (name) {
867 		dbus_message_iter_append_dict_entry(&dict, "Alias",
868 				DBUS_TYPE_STRING, &name);
869 		free(name);
870 	}
871 
872 	/* Paired */
873 	boolean = device_is_paired(device);
874 	dbus_message_iter_append_dict_entry(&dict, "Paired",
875 			DBUS_TYPE_BOOLEAN, &boolean);
876 
877 	/* Trusted */
878 	boolean = read_trust(&src, device->address, GLOBAL_TRUST);
879 	dbus_message_iter_append_dict_entry(&dict, "Trusted",
880 			DBUS_TYPE_BOOLEAN, &boolean);
881 
882 	/* Connected */
883 	if (g_slist_find_custom(adapter->active_conn, &dst,
884 				active_conn_find_by_bdaddr))
885 		boolean = TRUE;
886 	else
887 		boolean = FALSE;
888 
889 	dbus_message_iter_append_dict_entry(&dict, "Connected",
890 			DBUS_TYPE_BOOLEAN, &boolean);
891 
892 	/* UUIDs */
893 	uuids = g_new0(char *, g_slist_length(device->uuids) + 1);
894 	for (i = 0, l = device->uuids; l; l = l->next, i++)
895 		uuids[i] = l->data;
896 	dbus_message_iter_append_dict_entry(&dict, "UUIDs",
897 			DBUS_TYPE_ARRAY, &uuids);
898 	g_free(uuids);
899 
900 	/* Adapter */
901 	snprintf(path, sizeof(path), "/hci%d", adapter->dev_id);
902 	ppath = path;
903 	dbus_message_iter_append_dict_entry(&dict, "Adapter",
904 			DBUS_TYPE_OBJECT_PATH, &ppath);
905 
906 	dbus_message_iter_close_container(&iter, &dict);
907 
908 	return reply;
909 }
910 
set_alias(DBusConnection * conn,DBusMessage * msg,const char * alias,void * data)911 static DBusMessage *set_alias(DBusConnection *conn, DBusMessage *msg,
912 					const char *alias, void *data)
913 {
914 	struct device *device = data;
915 	struct adapter *adapter = device->adapter;
916 	bdaddr_t bdaddr;
917 	int ecode;
918 	char *str, filename[PATH_MAX + 1], path[MAX_PATH_LENGTH];
919 
920 	str2ba(device->address, &bdaddr);
921 
922 	/* Remove alias if empty string */
923 	if (g_str_equal(alias, "")) {
924 		create_name(filename, PATH_MAX, STORAGEDIR, adapter->address,
925 				"names");
926 		str = textfile_caseget(filename, device->address);
927 		ecode = remove_device_alias(adapter->dev_id, &bdaddr);
928 	} else {
929 		str = g_strdup(alias);
930 		ecode = set_device_alias(adapter->dev_id, &bdaddr, alias);
931 	}
932 
933 	if (ecode < 0)
934 		return g_dbus_create_error(msg,
935 				ERROR_INTERFACE ".Failed",
936 				strerror(-ecode));
937 
938 	snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, adapter->dev_id);
939 
940 	g_dbus_emit_signal(conn, path,
941 					ADAPTER_INTERFACE, "RemoteAliasChanged",
942 					DBUS_TYPE_STRING, &device->address,
943 					DBUS_TYPE_STRING, &str,
944 					DBUS_TYPE_INVALID);
945 
946 	dbus_connection_emit_property_changed(conn, dbus_message_get_path(msg),
947 					DEVICE_INTERFACE, "Alias",
948 					DBUS_TYPE_STRING, &str);
949 
950 	g_free(str);
951 
952 	return dbus_message_new_method_return(msg);
953 }
954 
set_trust(DBusConnection * conn,DBusMessage * msg,dbus_bool_t value,void * data)955 static DBusMessage *set_trust(DBusConnection *conn, DBusMessage *msg,
956 					dbus_bool_t value, void *data)
957 {
958 	struct device *device = data;
959 	struct adapter *adapter = device->adapter;
960 	bdaddr_t local;
961 	char path[MAX_PATH_LENGTH];
962 
963 	str2ba(adapter->address, &local);
964 
965 	write_trust(&local, device->address, GLOBAL_TRUST, value);
966 
967 	snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, adapter->dev_id);
968 
969 	g_dbus_emit_signal(conn, path,
970 					ADAPTER_INTERFACE,
971 					value ? "TrustAdded" : "TrustRemoved",
972 					DBUS_TYPE_STRING, &device->address,
973 					DBUS_TYPE_INVALID);
974 
975 	dbus_connection_emit_property_changed(conn, dbus_message_get_path(msg),
976 					DEVICE_INTERFACE, "Trusted",
977 					DBUS_TYPE_BOOLEAN, &value);
978 
979 	return dbus_message_new_method_return(msg);
980 }
981 
invalid_args(DBusMessage * msg)982 static inline DBusMessage *invalid_args(DBusMessage *msg)
983 {
984 	return g_dbus_create_error(msg,
985 			ERROR_INTERFACE ".InvalidArguments",
986 			"Invalid arguments in method call");
987 }
988 
set_property(DBusConnection * conn,DBusMessage * msg,void * data)989 static DBusMessage *set_property(DBusConnection *conn,
990 				DBusMessage *msg, void *data)
991 {
992 	DBusMessageIter iter;
993 	DBusMessageIter sub;
994 	const char *property;
995 
996 	if (!dbus_message_iter_init(msg, &iter))
997 		return invalid_args(msg);
998 
999 	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1000 		return invalid_args(msg);
1001 
1002 	dbus_message_iter_get_basic(&iter, &property);
1003 	dbus_message_iter_next(&iter);
1004 
1005 	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1006 		return invalid_args(msg);
1007 	dbus_message_iter_recurse(&iter, &sub);
1008 
1009 	if (g_str_equal("Trusted", property)) {
1010 		dbus_bool_t value;
1011 
1012 		if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1013 			return invalid_args(msg);
1014 		dbus_message_iter_get_basic(&sub, &value);
1015 
1016 		return set_trust(conn, msg, value, data);
1017 	} else if (g_str_equal("Alias", property)) {
1018 		char *alias;
1019 
1020 		if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)
1021 			return invalid_args(msg);
1022 		dbus_message_iter_get_basic(&sub, &alias);
1023 
1024 		return set_alias(conn, msg, alias, data);
1025 	}
1026 
1027 	return invalid_args(msg);
1028 }
1029 
discover_services_req_exit(void * user_data)1030 static void discover_services_req_exit(void *user_data)
1031 {
1032 	struct device *device = user_data;
1033 	struct adapter *adapter = device->adapter;
1034 	bdaddr_t src, dst;
1035 
1036 	debug("DiscoverDevices requestor exited");
1037 
1038 	str2ba(adapter->address, &src);
1039 	str2ba(device->address, &dst);
1040 
1041 	bt_cancel_discovery(&src, &dst);
1042 }
1043 
discover_services(DBusConnection * conn,DBusMessage * msg,void * user_data)1044 static DBusMessage *discover_services(DBusConnection *conn,
1045 					DBusMessage *msg, void *user_data)
1046 {
1047 	struct device *device = user_data;
1048 	const char *pattern;
1049 	int err;
1050 
1051 	if (device->discov_active)
1052 		return g_dbus_create_error(msg, ERROR_INTERFACE ".InProgress",
1053 							"Discover in progress");
1054 
1055 	if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
1056 						DBUS_TYPE_INVALID) == FALSE)
1057 		goto fail;
1058 
1059 	if (strlen(pattern) == 0) {
1060 		err = device_browse(device, conn, msg, NULL);
1061 		if (err < 0)
1062 			goto fail;
1063 	} else {
1064 		uuid_t uuid;
1065 
1066 		if (bt_string2uuid(&uuid, pattern) < 0)
1067 			return invalid_args(msg);
1068 
1069 		err = device_browse(device, conn, msg, &uuid);
1070 		if (err < 0)
1071 			goto fail;
1072 	}
1073 
1074 	return NULL;
1075 
1076 fail:
1077 	return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
1078 					"Discovery Failed");
1079 }
1080 
cancel_discover(DBusConnection * conn,DBusMessage * msg,void * user_data)1081 static DBusMessage *cancel_discover(DBusConnection *conn,
1082 					DBusMessage *msg, void *user_data)
1083 {
1084 	struct device *device = user_data;
1085 	struct adapter *adapter = device->adapter;
1086 	bdaddr_t src, dst;
1087 
1088 	if (!device->discov_active)
1089 		return g_dbus_create_error(msg,
1090 				ERROR_INTERFACE ".Failed",
1091 				"No pending discovery");
1092 
1093 	/* only the discover requestor can cancel the inquiry process */
1094 	if (!device->discov_requestor ||
1095 			strcmp(device->discov_requestor, dbus_message_get_sender(msg)))
1096 		return g_dbus_create_error(msg,
1097 				ERROR_INTERFACE ".NotAuthorized",
1098 				"Not Authorized");
1099 
1100  	str2ba(adapter->address, &src);
1101 	str2ba(device->address, &dst);
1102 
1103 	if (bt_cancel_discovery(&src, &dst) < 0)
1104 		return g_dbus_create_error(msg,
1105 				ERROR_INTERFACE ".Failed",
1106 				"No pending discover");
1107 
1108 	return dbus_message_new_method_return(msg);
1109 }
1110 
disconnect_timeout(gpointer user_data)1111 static gboolean disconnect_timeout(gpointer user_data)
1112 {
1113 	struct device *device = user_data;
1114 	struct active_conn_info *ci;
1115 	GSList *l;
1116 	disconnect_cp cp;
1117 	bdaddr_t bda;
1118 	int dd;
1119 
1120 	device->disconn_timer = 0;
1121 
1122 	str2ba(device->address, &bda);
1123 	l = g_slist_find_custom(device->adapter->active_conn,
1124 				&bda, active_conn_find_by_bdaddr);
1125 	if (!l)
1126 		return FALSE;
1127 
1128 	ci = l->data;
1129 	dd = hci_open_dev(device->adapter->dev_id);
1130 	if (dd < 0)
1131 		goto fail;
1132 
1133 	memset(&cp, 0, sizeof(cp));
1134 	cp.handle = htobs(ci->handle);
1135 	cp.reason = HCI_OE_USER_ENDED_CONNECTION;
1136 
1137 	hci_send_cmd(dd, OGF_LINK_CTL, OCF_DISCONNECT,
1138 			DISCONNECT_CP_SIZE, &cp);
1139 
1140 	close(dd);
1141 
1142 fail:
1143 	return FALSE;
1144 }
1145 
disconnect(DBusConnection * conn,DBusMessage * msg,void * user_data)1146 static DBusMessage *disconnect(DBusConnection *conn,
1147 					DBusMessage *msg, void *user_data)
1148 {
1149 	struct device *device = user_data;
1150 	GSList *l;
1151 	bdaddr_t bda;
1152 
1153 	str2ba(device->address, &bda);
1154 	l = g_slist_find_custom(device->adapter->active_conn,
1155 				&bda, active_conn_find_by_bdaddr);
1156 	if (!l)
1157 		return g_dbus_create_error(msg,
1158 				ERROR_INTERFACE ".NotConnected",
1159 				"Device is not connected");
1160 
1161 	g_dbus_emit_signal(conn, device->path,
1162 			DEVICE_INTERFACE, "DisconnectRequested",
1163 			DBUS_TYPE_INVALID);
1164 
1165 	device->disconn_timer = g_timeout_add_seconds(DISCONNECT_TIMER,
1166 						disconnect_timeout, device);
1167 
1168 	return dbus_message_new_method_return(msg);
1169 }
1170 
1171 static GDBusMethodTable device_methods[] = {
1172 	{ "GetProperties",	"",	"a{sv}",	get_properties	},
1173 	{ "SetProperty",	"sv",	"",		set_property	},
1174 	{ "DiscoverServices",	"s",	"a{us}",	discover_services,
1175 						G_DBUS_METHOD_FLAG_ASYNC},
1176 	{ "CancelDiscovery",	"",	"",		cancel_discover	},
1177 	{ "Disconnect",		"",	"",		disconnect	},
1178 	{ }
1179 };
1180 
1181 static GDBusSignalTable device_signals[] = {
1182 	{ "PropertyChanged",		"sv"	},
1183 	{ "DisconnectRequested",	""	},
1184 	{ }
1185 };
1186 
device_create(DBusConnection * conn,struct adapter * adapter,const gchar * address)1187 struct device *device_create(DBusConnection *conn, struct adapter *adapter,
1188 					const gchar *address)
1189 {
1190 	gchar *address_up;
1191 	struct device *device;
1192 
1193 	device = g_try_malloc0(sizeof(struct device));
1194 	if (device == NULL)
1195 		return NULL;
1196 
1197 	address_up = g_ascii_strup(address, -1);
1198 	device->path = g_strdup_printf("/hci%d/dev_%s",
1199 				adapter->dev_id, address_up);
1200 	g_strdelimit(device->path, ":", '_');
1201 	g_free(address_up);
1202 
1203 	debug("Creating device %s", device->path);
1204 
1205 	if (g_dbus_register_interface(conn, device->path, DEVICE_INTERFACE,
1206 				device_methods, device_signals, NULL,
1207 				device, device_free) == FALSE) {
1208 		device_free(device);
1209 		return NULL;
1210 	}
1211 
1212 	device->address = g_strdup(address);
1213 	device->adapter = adapter;
1214 
1215 	device->dev.path = device->path;
1216 	str2ba(device->address, &device->dev.dst);
1217 	str2ba(adapter->address, &device->dev.src);
1218 
1219 	return device;
1220 }
1221 
device_remove(DBusConnection * conn,struct device * device)1222 void device_remove(DBusConnection *conn, struct device *device)
1223 {
1224 	GSList *list;
1225 	struct btd_device_driver *driver;
1226 	gchar *path = g_strdup(device->path);
1227 
1228 	debug("Removing device %s", path);
1229 
1230 	for (list = device->drivers; list; list = list->next) {
1231 		driver = (struct btd_device_driver *) list->data;
1232 
1233 		driver->remove(&device->dev);
1234 	}
1235 
1236 	g_dbus_unregister_interface(conn, path, DEVICE_INTERFACE);
1237 
1238 	g_free(path);
1239 }
1240 
device_address_cmp(struct device * device,const gchar * address)1241 gint device_address_cmp(struct device *device, const gchar *address)
1242 {
1243 	return strcasecmp(device->address, address);
1244 }
1245 
cmp_by_name(const void * data,const void * user_data)1246 static int cmp_by_name(const void *data, const void *user_data)
1247 {
1248 	const struct btd_device_driver *dev_driver = data, *driver = user_data;
1249 
1250 	return (strcmp(dev_driver->name, driver->name));
1251 }
1252 
device_probe_drivers(struct device * device,GSList * uuids)1253 void device_probe_drivers(struct device *device, GSList *uuids)
1254 {
1255 	GSList *list;
1256 	const char **uuid;
1257 	int err;
1258 
1259 	debug("Probe drivers for %s", device->path);
1260 
1261 	for (list = drivers; list; list = list->next) {
1262 		struct btd_device_driver *driver = list->data;
1263 		gboolean do_probe = FALSE;
1264 
1265 		for (uuid = driver->uuids; *uuid; uuid++) {
1266 			GSList *match = g_slist_find_custom(uuids, *uuid,
1267 					(GCompareFunc) strcasecmp);
1268 			if (match) {
1269 				do_probe = TRUE;
1270 				break;
1271 			}
1272 		}
1273 
1274 		if (do_probe == TRUE && !g_slist_find_custom(device->drivers,
1275 					driver, (GCompareFunc) cmp_by_name)) {
1276 
1277 			err = driver->probe(&device->dev);
1278 			if (err < 0) {
1279 				error("probe failed for driver %s",
1280 							driver->name);
1281 				continue;
1282 			}
1283 
1284 			device->drivers = g_slist_append(device->drivers,
1285 								driver);
1286 		}
1287 	}
1288 
1289 	for (list = uuids; list; list = list->next)
1290 		device->uuids = g_slist_insert_sorted(device->uuids,
1291 				list->data, (GCompareFunc) strcmp);
1292 }
1293 
device_remove_drivers(struct device * device,GSList * uuids)1294 void device_remove_drivers(struct device *device, GSList *uuids)
1295 {
1296 	GSList *list;
1297 
1298 	debug("Remove drivers for %s", device->path);
1299 
1300 	for (list = device->drivers; list; list = list->next) {
1301 		struct btd_device_driver *driver = list->data;
1302 		const char **uuid;
1303 
1304 		for (uuid = driver->uuids; *uuid; uuid++) {
1305 			GSList *match = g_slist_find_custom(uuids, *uuid,
1306 					(GCompareFunc) strcasecmp);
1307 
1308 			if (!match)
1309 				continue;
1310 
1311 			driver->remove(&device->dev);
1312 			device->drivers = g_slist_remove(device->drivers,
1313 								driver);
1314 		}
1315 	}
1316 
1317 	for (list = uuids; list; list = list->next)
1318 		device->uuids = g_slist_remove(device->uuids, list->data);
1319 }
1320 
iter_append_record(DBusMessageIter * dict,uint32_t handle,const char * record)1321 static void iter_append_record(DBusMessageIter *dict, uint32_t handle,
1322 							const char *record)
1323 {
1324 	DBusMessageIter entry;
1325 
1326 	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
1327 							NULL, &entry);
1328 
1329 	dbus_message_iter_append_basic(&entry, DBUS_TYPE_UINT32, &handle);
1330 
1331 	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &record);
1332 
1333 	dbus_message_iter_close_container(dict, &entry);
1334 }
1335 
discover_device_reply(struct browse_req * req,sdp_list_t * recs)1336 static void discover_device_reply(struct browse_req *req, sdp_list_t *recs)
1337 {
1338 	DBusMessage *reply;
1339 	DBusMessageIter iter, dict;
1340 	sdp_list_t *seq;
1341 
1342 	reply = dbus_message_new_method_return(req->msg);
1343 	if (!reply)
1344 		return;
1345 
1346 	dbus_message_iter_init_append(reply, &iter);
1347 
1348 	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1349 			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1350 			DBUS_TYPE_UINT32_AS_STRING DBUS_TYPE_STRING_AS_STRING
1351 			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1352 
1353 	for (seq = recs; seq; seq = seq->next) {
1354 		sdp_record_t *rec = (sdp_record_t *) seq->data;
1355 		sdp_buf_t result;
1356 
1357 		if (!rec)
1358 			break;
1359 
1360 		memset(&result, 0, sizeof(sdp_buf_t));
1361 
1362 		convert_sdp_record_to_xml(rec, &result,
1363 				append_and_grow_string);
1364 
1365 		if (result.data) {
1366 			const char *val = (char *) result.data;
1367 			iter_append_record(&dict, rec->handle, val);
1368 			free(result.data);
1369 		}
1370 	}
1371 
1372 	dbus_message_iter_close_container(&iter, &dict);
1373 
1374 	dbus_connection_send(req->conn, reply, NULL);
1375 	dbus_message_unref(reply);
1376 }
1377 
services_changed(struct browse_req * req)1378 static void services_changed(struct browse_req *req)
1379 {
1380 	struct device *device = req->device;
1381 	char **uuids;
1382 	GSList *l;
1383 	int i;
1384 
1385 	uuids = g_new0(char *, g_slist_length(device->uuids) + 1);
1386 	for (i = 0, l = device->uuids; l; l = l->next, i++)
1387 		uuids[i] = l->data;
1388 
1389 	dbus_connection_emit_property_changed(req->conn, device->path,
1390 					DEVICE_INTERFACE, "UUIDs",
1391 					DBUS_TYPE_ARRAY, &uuids);
1392 
1393 	g_free(uuids);
1394 }
1395 
update_services(struct browse_req * req,sdp_list_t * recs)1396 static void update_services(struct browse_req *req, sdp_list_t *recs)
1397 {
1398 	struct device *device = req->device;
1399 	sdp_list_t *seq;
1400 
1401 	for (seq = recs; seq; seq = seq->next) {
1402 		sdp_record_t *rec = (sdp_record_t *) seq->data;
1403 		sdp_list_t *svcclass = NULL;
1404 		gchar *uuid_str;
1405 		GSList *l;
1406 
1407 		if (!rec)
1408 			break;
1409 
1410 		if (sdp_get_service_classes(rec, &svcclass) < 0)
1411 			continue;
1412 
1413 		/* Extract the first element and skip the remainning */
1414 		uuid_str = bt_uuid2string(svcclass->data);
1415 		if (!uuid_str)
1416 			continue;
1417 
1418 		l = g_slist_find_custom(device->uuids, uuid_str,
1419 				(GCompareFunc) strcmp);
1420 		if (!l)
1421 			req->uuids_added = g_slist_append(req->uuids_added,
1422 					uuid_str);
1423 		else {
1424 			req->uuids_removed = g_slist_remove(req->uuids_removed,
1425 					l->data);
1426 			g_free(uuid_str);
1427 		}
1428 
1429 		sdp_list_free(svcclass, free);
1430 	}
1431 }
1432 
store(struct device * device)1433 static void store(struct device *device)
1434 {
1435 	struct adapter *adapter = device->adapter;
1436 	bdaddr_t src, dst;
1437 	char *str;
1438 
1439 	str2ba(adapter->address, &src);
1440 	str2ba(device->address, &dst);
1441 
1442 	if (!device->uuids) {
1443 		write_device_profiles(&src, &dst, "");
1444 		return;
1445 	}
1446 
1447 	str = bt_list2string(device->uuids);
1448 	write_device_profiles(&src, &dst, str);
1449 	g_free(str);
1450 }
1451 
browse_cb(sdp_list_t * recs,int err,gpointer user_data)1452 static void browse_cb(sdp_list_t *recs, int err, gpointer user_data)
1453 {
1454 	struct browse_req *req = user_data;
1455 	struct device *device = req->device;
1456 	struct adapter *adapter = device->adapter;
1457 	bdaddr_t src, dst;
1458 	uuid_t uuid;
1459 	DBusMessage *reply;
1460 
1461 	if (err < 0)
1462 		goto proceed;
1463 
1464 	update_services(req, recs);
1465 
1466 	/* Public browsing successful or Single record requested */
1467 	if (req->browse == FALSE || (!req->search_uuid && recs))
1468 		goto probe;
1469 
1470 	if (uuid_list[++req->search_uuid]) {
1471 		sdp_uuid16_create(&uuid, uuid_list[req->search_uuid]);
1472 		str2ba(adapter->address, &src);
1473 		str2ba(device->address, &dst);
1474 		bt_search_service(&src, &dst, &uuid, browse_cb, user_data, NULL);
1475 		return;
1476 	}
1477 
1478 probe:
1479 
1480 	if (!req->uuids_added && !req->uuids_removed)
1481 		goto proceed;
1482 
1483 	/* Probe matching drivers for services added */
1484 	if (req->uuids_added)
1485 		device_probe_drivers(device, req->uuids_added);
1486 
1487 	/* Remove drivers for services removed */
1488 	if (req->uuids_removed)
1489 		device_remove_drivers(device, req->uuids_removed);
1490 
1491 	/* Store the device's profiles in the filesystem */
1492 	store(device);
1493 
1494 	/* Propagate services changes */
1495 	services_changed(req);
1496 
1497 proceed:
1498 	if (dbus_message_is_method_call(req->msg, DEVICE_INTERFACE,
1499 			"DiscoverServices")) {
1500 		discover_device_reply(req, recs);
1501 		goto cleanup;
1502 	}
1503 
1504 	g_dbus_emit_signal(req->conn, dbus_message_get_path(req->msg),
1505 				ADAPTER_INTERFACE, "DeviceCreated",
1506 				DBUS_TYPE_OBJECT_PATH, &device->path,
1507 				DBUS_TYPE_INVALID);
1508 
1509 	/* Reply create device request */
1510 	reply = dbus_message_new_method_return(req->msg);
1511 	if (!reply)
1512 		goto cleanup;
1513 
1514 	dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &device->path,
1515 							DBUS_TYPE_INVALID);
1516 
1517 	dbus_connection_send(req->conn, reply, NULL);
1518 	dbus_message_unref(reply);
1519 
1520 cleanup:
1521 	device->discov_active = 0;
1522 
1523 	if (device->discov_requestor) {
1524 		g_dbus_remove_watch(req->conn, device->discov_listener);
1525 		device->discov_listener = 0;
1526 		g_free(device->discov_requestor);
1527 		device->discov_requestor = NULL;
1528 	}
1529 
1530 	if (recs != NULL)
1531 		sdp_list_free(recs, (sdp_free_func_t) sdp_record_free);
1532 
1533 	dbus_message_unref(req->msg);
1534 	dbus_connection_unref(req->conn);
1535 	g_slist_free(req->uuids_added);
1536 	g_slist_free(req->uuids_removed);
1537 	g_free(req);
1538 }
1539 
device_browse(struct device * device,DBusConnection * conn,DBusMessage * msg,uuid_t * search)1540 int device_browse(struct device *device, DBusConnection *conn,
1541 			DBusMessage *msg, uuid_t *search)
1542 {
1543 	struct adapter *adapter = device->adapter;
1544 	struct browse_req *req;
1545 	bdaddr_t src, dst;
1546 	uuid_t uuid;
1547 	GSList *l;
1548 
1549 	req = g_new0(struct browse_req, 1);
1550 	req->conn = dbus_connection_ref(conn);
1551 	req->msg = dbus_message_ref(msg);
1552 	req->device = device;
1553 
1554 	for (l = device->uuids; l; l = l->next)
1555 		req->uuids_removed = g_slist_append(req->uuids_removed,
1556 				l->data);
1557 
1558 	str2ba(adapter->address, &src);
1559 	str2ba(device->address, &dst);
1560 
1561 	if (search) {
1562 		memcpy(&uuid, search, sizeof(uuid_t));
1563 		req->browse = FALSE;
1564 	} else {
1565 		sdp_uuid16_create(&uuid, uuid_list[req->search_uuid]);
1566 		req->browse = TRUE;
1567 	}
1568 
1569 	device->discov_active = 1;
1570 	device->discov_requestor = g_strdup(dbus_message_get_sender(msg));
1571 	/* Track the request owner to cancel it
1572 	 * automatically if the owner exits */
1573 	device->discov_listener = g_dbus_add_disconnect_watch(conn,
1574 						dbus_message_get_sender(msg),
1575 						discover_services_req_exit,
1576 						device, NULL);
1577 
1578 	return bt_search_service(&src, &dst, &uuid, browse_cb, req, NULL);
1579 }
1580 
btd_register_device_driver(struct btd_device_driver * driver)1581 int btd_register_device_driver(struct btd_device_driver *driver)
1582 {
1583 	const char **uuid;
1584 
1585 	drivers = g_slist_append(drivers, driver);
1586 
1587 	for (uuid = driver->uuids; *uuid; uuid++) {
1588 		debug("name %s uuid %s", driver->name, *uuid);
1589 	}
1590 
1591 	register_service(driver->name, driver->uuids);
1592 
1593 	return 0;
1594 }
1595 
btd_unregister_device_driver(struct btd_device_driver * driver)1596 void btd_unregister_device_driver(struct btd_device_driver *driver)
1597 {
1598 	unregister_service(driver->name);
1599 
1600 	drivers = g_slist_remove(drivers, driver);
1601 }
1602