• 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 #define _GNU_SOURCE
30 #include <stdio.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 
39 #include <bluetooth/bluetooth.h>
40 #include <bluetooth/hci.h>
41 #include <bluetooth/hci_lib.h>
42 #include <bluetooth/sdp.h>
43 
44 #include <glib.h>
45 #include <dbus/dbus.h>
46 #include <gdbus.h>
47 
48 #include "hcid.h"
49 #include "textfile.h"
50 #include "manager.h"
51 #include "adapter.h"
52 #include "device.h"
53 #include "error.h"
54 #include "glib-helper.h"
55 #include "dbus-common.h"
56 #include "dbus-error.h"
57 #include "dbus-service.h"
58 #include "dbus-security.h"
59 #include "agent.h"
60 #include "dbus-hci.h"
61 
62 static DBusConnection *connection = NULL;
63 
bonding_request_free(struct bonding_request_info * bonding)64 void bonding_request_free(struct bonding_request_info *bonding)
65 {
66 	struct device *device;
67 	char address[18];
68 
69 	if (!bonding)
70 		return;
71 
72 	if (bonding->msg)
73 		dbus_message_unref(bonding->msg);
74 
75 	if (bonding->conn)
76 		dbus_connection_unref(bonding->conn);
77 
78 	if (bonding->io)
79 		g_io_channel_unref(bonding->io);
80 
81 	ba2str(&bonding->bdaddr, address);
82 
83 	device = adapter_find_device(bonding->adapter, address);
84 	if (device && device->agent) {
85 		agent_destroy(device->agent, FALSE);
86 		device->agent = NULL;
87 	}
88 
89 	g_free(bonding);
90 }
91 
found_device_cmp(const struct remote_dev_info * d1,const struct remote_dev_info * d2)92 int found_device_cmp(const struct remote_dev_info *d1,
93 			const struct remote_dev_info *d2)
94 {
95 	int ret;
96 
97 	if (bacmp(&d2->bdaddr, BDADDR_ANY)) {
98 		ret = bacmp(&d1->bdaddr, &d2->bdaddr);
99 		if (ret)
100 			return ret;
101 	}
102 
103 	if (d2->name_status != NAME_ANY) {
104 		ret = (d1->name_status - d2->name_status);
105 		if (ret)
106 			return ret;
107 	}
108 
109 	return 0;
110 }
111 
dev_rssi_cmp(struct remote_dev_info * d1,struct remote_dev_info * d2)112 int dev_rssi_cmp(struct remote_dev_info *d1, struct remote_dev_info *d2)
113 {
114 	int rssi1, rssi2;
115 
116 	rssi1 = d1->rssi < 0 ? -d1->rssi : d1->rssi;
117 	rssi2 = d2->rssi < 0 ? -d2->rssi : d2->rssi;
118 
119 	return rssi1 - rssi2;
120 }
121 
found_device_add(GSList ** list,bdaddr_t * bdaddr,int8_t rssi,name_status_t name_status)122 int found_device_add(GSList **list, bdaddr_t *bdaddr, int8_t rssi,
123 			name_status_t name_status)
124 {
125 	struct remote_dev_info *dev, match;
126 	GSList *l;
127 
128 	memset(&match, 0, sizeof(struct remote_dev_info));
129 	bacpy(&match.bdaddr, bdaddr);
130 	match.name_status = NAME_ANY;
131 
132 	/* ignore repeated entries */
133 	l = g_slist_find_custom(*list, &match, (GCompareFunc) found_device_cmp);
134 	if (l) {
135 		/* device found, update the attributes */
136 		dev = l->data;
137 
138 		if (rssi != 0)
139 			dev->rssi = rssi;
140 
141 		 /* Get remote name can be received while inquiring.
142 		  * Keep in mind that multiple inquiry result events can
143 		  * be received from the same remote device.
144 		  */
145 		if (name_status != NAME_NOT_REQUIRED)
146 			dev->name_status = name_status;
147 
148 		*list = g_slist_sort(*list, (GCompareFunc) dev_rssi_cmp);
149 
150 		return -EALREADY;
151 	}
152 
153 	dev = g_new0(struct remote_dev_info, 1);
154 
155 	bacpy(&dev->bdaddr, bdaddr);
156 	dev->rssi = rssi;
157 	dev->name_status = name_status;
158 
159 	*list = g_slist_insert_sorted(*list, dev, (GCompareFunc) dev_rssi_cmp);
160 
161 	return 0;
162 }
163 
found_device_remove(GSList ** list,bdaddr_t * bdaddr)164 static int found_device_remove(GSList **list, bdaddr_t *bdaddr)
165 {
166 	struct remote_dev_info *dev, match;
167 	GSList *l;
168 
169 	memset(&match, 0, sizeof(struct remote_dev_info));
170 	bacpy(&match.bdaddr, bdaddr);
171 
172 	l = g_slist_find_custom(*list, &match, (GCompareFunc) found_device_cmp);
173 	if (!l)
174 		return -1;
175 
176 	dev = l->data;
177 	*list = g_slist_remove(*list, dev);
178 	g_free(dev);
179 
180 	return 0;
181 }
182 
active_conn_find_by_bdaddr(const void * data,const void * user_data)183 int active_conn_find_by_bdaddr(const void *data, const void *user_data)
184 {
185 	const struct active_conn_info *con = data;
186 	const bdaddr_t *bdaddr = user_data;
187 
188 	return bacmp(&con->bdaddr, bdaddr);
189 }
190 
active_conn_find_by_handle(const void * data,const void * user_data)191 static int active_conn_find_by_handle(const void *data, const void *user_data)
192 {
193 	const struct active_conn_info *dev = data;
194 	const uint16_t *handle = user_data;
195 
196 	if (dev->handle == *handle)
197 		return 0;
198 
199 	return -1;
200 }
201 
active_conn_append(GSList ** list,bdaddr_t * bdaddr,uint16_t handle)202 static int active_conn_append(GSList **list, bdaddr_t *bdaddr,
203 				uint16_t handle)
204 {
205 	struct active_conn_info *dev;
206 
207 	dev = g_new0(struct active_conn_info, 1);
208 
209 	bacpy(&dev->bdaddr, bdaddr);
210 	dev->handle = handle;
211 
212 	*list = g_slist_append(*list, dev);
213 	return 0;
214 }
215 
new_authentication_return(DBusMessage * msg,uint8_t status)216 DBusMessage *new_authentication_return(DBusMessage *msg, uint8_t status)
217 {
218 	switch (status) {
219 	case 0x00: /* success */
220 		return dbus_message_new_method_return(msg);
221 
222 	case 0x04: /* page timeout */
223 	case 0x08: /* connection timeout */
224 	case 0x10: /* connection accept timeout */
225 	case 0x22: /* LMP response timeout */
226 	case 0x28: /* instant passed - is this a timeout? */
227 		return dbus_message_new_error(msg,
228 					ERROR_INTERFACE ".AuthenticationTimeout",
229 					"Authentication Timeout");
230 	case 0x17: /* too frequent pairing attempts */
231 		return dbus_message_new_error(msg,
232 					ERROR_INTERFACE ".RepeatedAttempts",
233 					"Repeated Attempts");
234 
235 	case 0x06:
236 	case 0x18: /* pairing not allowed (e.g. gw rejected attempt) */
237 		return dbus_message_new_error(msg,
238 					ERROR_INTERFACE ".AuthenticationRejected",
239 					"Authentication Rejected");
240 
241 	case 0x07: /* memory capacity */
242 	case 0x09: /* connection limit */
243 	case 0x0a: /* synchronous connection limit */
244 	case 0x0d: /* limited resources */
245 	case 0x14: /* terminated due to low resources */
246 		return dbus_message_new_error(msg,
247 					ERROR_INTERFACE ".AuthenticationCanceled",
248 					"Authentication Canceled");
249 
250 	case 0x05: /* authentication failure */
251 	case 0x0E: /* rejected due to security reasons - is this auth failure? */
252 	case 0x25: /* encryption mode not acceptable - is this auth failure? */
253 	case 0x26: /* link key cannot be changed - is this auth failure? */
254 	case 0x29: /* pairing with unit key unsupported - is this auth failure? */
255 	case 0x2f: /* insufficient security - is this auth failure? */
256 	default:
257 		return dbus_message_new_error(msg,
258 					ERROR_INTERFACE ".AuthenticationFailed",
259 					"Authentication Failed");
260 	}
261 }
262 
send_adapter_signal(DBusConnection * conn,int devid,const char * name,int first,...)263 static dbus_bool_t send_adapter_signal(DBusConnection *conn, int devid,
264 					const char *name, int first, ...)
265 {
266 	va_list var_args;
267 	dbus_bool_t ret;
268 	char path[MAX_PATH_LENGTH];
269 
270 	snprintf(path, sizeof(path)-1, "%s/hci%d", BASE_PATH, devid);
271 
272 	va_start(var_args, first);
273 	ret = g_dbus_emit_signal_valist(conn, path, ADAPTER_INTERFACE,
274 							name, first, var_args);
275 	va_end(var_args);
276 
277 	return ret;
278 }
279 
adapter_mode_changed(struct adapter * adapter,uint8_t scan_enable)280 static void adapter_mode_changed(struct adapter *adapter, uint8_t scan_enable)
281 {
282 	const char *mode;
283 
284 	adapter->scan_enable = scan_enable;
285 
286 	switch (scan_enable) {
287 	case SCAN_DISABLED:
288 		mode = "off";
289 		adapter->mode = MODE_OFF;
290 		break;
291 	case SCAN_PAGE:
292 		mode = "connectable";
293 		adapter->mode = MODE_CONNECTABLE;
294 		break;
295 	case (SCAN_PAGE | SCAN_INQUIRY):
296 
297 		if (adapter->discov_timeout != 0)
298 			adapter->timeout_id = g_timeout_add(adapter->discov_timeout * 1000,
299 					discov_timeout_handler, adapter);
300 
301 		if (adapter->mode == MODE_LIMITED) {
302 			mode = "limited";
303 		} else {
304 			adapter->mode = MODE_DISCOVERABLE;
305 			mode = "discoverable";
306 		}
307 		break;
308 	case SCAN_INQUIRY:
309 		/* Address the scenario where another app changed the scan mode */
310 		if (adapter->discov_timeout != 0)
311 			adapter->timeout_id = g_timeout_add(adapter->discov_timeout * 1000,
312 					discov_timeout_handler, adapter);
313 		/* ignore, this event should not be sent*/
314 	default:
315 		/* ignore, reserved */
316 		return;
317 	}
318 
319 	g_dbus_emit_signal(connection, adapter->path, ADAPTER_INTERFACE,
320 					"ModeChanged",
321 					DBUS_TYPE_STRING, &mode,
322 					DBUS_TYPE_INVALID);
323 
324 	if (hcid_dbus_use_experimental()) {
325 		const char *ptr = adapter->path + ADAPTER_PATH_INDEX;
326 		dbus_connection_emit_property_changed(connection, ptr,
327 						ADAPTER_INTERFACE, "Mode",
328 						DBUS_TYPE_STRING, &mode);
329 	}
330 }
331 
332 /*
333  * HCI D-Bus services
334  */
reply_pending_requests(const char * path,struct adapter * adapter)335 static void reply_pending_requests(const char *path, struct adapter *adapter)
336 {
337 	if (!path || !adapter)
338 		return;
339 
340 	/* pending bonding */
341 	if (adapter->bonding) {
342 		error_authentication_canceled(connection, adapter->bonding->msg);
343 
344 		remove_pending_device(adapter);
345 
346 		g_dbus_remove_watch(adapter->bonding->conn,
347 					adapter->bonding->listener_id);
348 
349 		if (adapter->bonding->io_id)
350 			g_source_remove(adapter->bonding->io_id);
351 		g_io_channel_close(adapter->bonding->io);
352 		bonding_request_free(adapter->bonding);
353 		adapter->bonding = NULL;
354 	}
355 
356 	/* If there is a pending reply for discovery cancel */
357 	if (adapter->discovery_cancel) {
358 		DBusMessage *reply;
359 		reply = dbus_message_new_method_return(adapter->discovery_cancel);
360 		dbus_connection_send(connection, reply, NULL);
361 		dbus_message_unref(reply);
362 		dbus_message_unref(adapter->discovery_cancel);
363 		adapter->discovery_cancel = NULL;
364 	}
365 
366 	if (adapter->discov_active) {
367 		/* Send discovery completed signal if there isn't name
368 		 * to resolve */
369 		if (hcid_dbus_use_experimental()) {
370 			const char *ptr = path + ADAPTER_PATH_INDEX;
371 
372 			g_dbus_emit_signal(connection, ptr,
373 						ADAPTER_INTERFACE,
374 						"DiscoveryCompleted",
375 						DBUS_TYPE_INVALID);
376 
377 		}
378 
379 		g_dbus_emit_signal(connection, path,
380 						ADAPTER_INTERFACE,
381 						"DiscoveryCompleted",
382 						DBUS_TYPE_INVALID);
383 
384 		/* Cancel inquiry initiated by D-Bus client */
385 		if (adapter->discov_requestor)
386 			cancel_discovery(adapter);
387 	}
388 
389 	if (adapter->pdiscov_active) {
390 		/* Send periodic discovery stopped signal exit or stop
391 		 * the device */
392 		g_dbus_emit_signal(connection, path,
393 						ADAPTER_INTERFACE,
394 						"PeriodicDiscoveryStopped",
395 						DBUS_TYPE_INVALID);
396 
397 		/* Stop periodic inquiry initiated by D-Bus client */
398 		if (adapter->pdiscov_requestor)
399 			cancel_periodic_discovery(adapter);
400 	}
401 }
402 
do_unregister(gpointer data,gpointer user_data)403 static void do_unregister(gpointer data, gpointer user_data)
404 {
405 	DBusConnection *conn = user_data;
406 	struct device *device = data;
407 
408 	device_remove(conn, device);
409 }
410 
unregister_adapter_path(const char * path)411 int unregister_adapter_path(const char *path)
412 {
413 	struct adapter *adapter;
414 
415 	info("Unregister path: %s", path);
416 
417 	__remove_servers(path);
418 
419 	adapter = manager_find_adapter_by_path(path);
420 	if (!adapter)
421 		goto unreg;
422 
423 	/* check pending requests */
424 	reply_pending_requests(path, adapter);
425 
426 	cancel_passkey_agent_requests(adapter->passkey_agents, path, NULL);
427 
428 	release_passkey_agents(adapter, NULL);
429 
430 	if (adapter->agent) {
431 		agent_destroy(adapter->agent, FALSE);
432 		adapter->agent = NULL;
433 	}
434 
435 	if (adapter->discov_requestor) {
436 		g_dbus_remove_watch(connection, adapter->discov_listener);
437 		adapter->discov_listener = 0;
438 		g_free(adapter->discov_requestor);
439 		adapter->discov_requestor = NULL;
440 	}
441 
442 	if (adapter->pdiscov_requestor) {
443 		g_dbus_remove_watch(connection, adapter->pdiscov_listener);
444 		adapter->pdiscov_listener = 0;
445 		g_free(adapter->pdiscov_requestor);
446 		adapter->pdiscov_requestor = NULL;
447 	}
448 
449 	if (adapter->found_devices) {
450 		g_slist_foreach(adapter->found_devices,
451 				(GFunc) g_free, NULL);
452 		g_slist_free(adapter->found_devices);
453 		adapter->found_devices = NULL;
454 	}
455 
456 	if (adapter->oor_devices) {
457 		g_slist_foreach(adapter->oor_devices,
458 				(GFunc) free, NULL);
459 		g_slist_free(adapter->oor_devices);
460 		adapter->oor_devices = NULL;
461 	}
462 
463 	if (adapter->auth_reqs) {
464 		g_slist_foreach(adapter->auth_reqs,
465 				(GFunc) g_free, NULL);
466 		g_slist_free(adapter->auth_reqs);
467 		adapter->auth_reqs = NULL;
468 	}
469 
470 	if (adapter->active_conn) {
471 		g_slist_foreach(adapter->active_conn,
472 				(GFunc) free, NULL);
473 		g_slist_free(adapter->active_conn);
474 		adapter->active_conn = NULL;
475 	}
476 
477 	/* Check if there is a pending RemoteDeviceDisconnect request */
478 	if (adapter->pending_dc) {
479 		error_no_such_adapter(adapter->pending_dc->conn,
480 				      adapter->pending_dc->msg);
481 		g_source_remove(adapter->pending_dc->timeout_id);
482 		dc_pending_timeout_cleanup(adapter);
483 	}
484 
485 	if (adapter->devices) {
486 		g_slist_foreach(adapter->devices, do_unregister,
487 							connection);
488 		g_slist_free(adapter->devices);
489 	}
490 
491 	manager_remove_adapter(adapter);
492 
493 	g_free(adapter->path);
494 	g_free(adapter);
495 
496 unreg:
497 	if (!adapter_cleanup(connection, path)) {
498 		error("Failed to unregister adapter interface on %s object",
499 			path);
500 		return -1;
501 	}
502 
503 	if (!security_cleanup(connection, path)) {
504 		error("Failed to unregister security interface on %s object",
505 			path);
506 		return -1;
507 	}
508 
509 	if (hcid_dbus_use_experimental()) {
510 		const char *ptr = path + ADAPTER_PATH_INDEX;
511 
512 		adapter_cleanup(connection, ptr);
513 	}
514 
515 	return 0;
516 }
517 
518 /*****************************************************************
519  *
520  *  Section reserved to HCI commands confirmation handling and low
521  *  level events(eg: device attached/dettached.
522  *
523  *****************************************************************/
524 
hcid_dbus_register_device(uint16_t id)525 int hcid_dbus_register_device(uint16_t id)
526 {
527 	char path[MAX_PATH_LENGTH];
528 	char *ptr = path + ADAPTER_PATH_INDEX;
529 	struct adapter *adapter;
530 
531 	snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, id);
532 
533 	adapter = g_try_new0(struct adapter, 1);
534 	if (!adapter) {
535 		error("Failed to alloc memory to D-Bus path register data (%s)",
536 				path);
537 		return -1;
538 	}
539 
540 	adapter->dev_id = id;
541 	adapter->pdiscov_resolve_names = 1;
542 
543 	if (!adapter_init(connection, path, adapter)) {
544 		error("Adapter interface init failed on path %s", path);
545 		g_free(adapter);
546 		return -1;
547 	}
548 
549 	adapter->path = g_strdup(path);
550 
551 	if (!security_init(connection, path)) {
552 		error("Security interface init failed on path %s", path);
553 		goto failed;
554 	}
555 
556 	__probe_servers(path);
557 
558 	manager_add_adapter(adapter);
559 
560 	return 0;
561 
562 failed:
563 	if (hcid_dbus_use_experimental())
564 		g_dbus_unregister_interface(connection, ptr, ADAPTER_INTERFACE);
565 
566 	g_dbus_unregister_interface(connection, path, ADAPTER_INTERFACE);
567 
568 	g_free(adapter->path);
569 	g_free(adapter);
570 
571 	return -1;
572 }
573 
hcid_dbus_unregister_device(uint16_t id)574 int hcid_dbus_unregister_device(uint16_t id)
575 {
576 	char path[MAX_PATH_LENGTH];
577 
578 	snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, id);
579 
580 	return unregister_adapter_path(path);
581 }
582 
create_stored_device_from_profiles(char * key,char * value,void * user_data)583 static void create_stored_device_from_profiles(char *key, char *value,
584 						void *user_data)
585 {
586 	struct adapter *adapter = user_data;
587 	GSList *uuids = bt_string2list(value);
588 	struct device *device;
589 
590 	device = device_create(connection, adapter, key);
591 	if (device) {
592 		device->temporary = FALSE;
593 		adapter->devices = g_slist_append(adapter->devices, device);
594 		device_probe_drivers(device, uuids);
595 		g_slist_free(uuids);
596 	}
597 }
598 
create_stored_device_from_linkkeys(char * key,char * value,void * user_data)599 static void create_stored_device_from_linkkeys(char *key, char *value,
600 						void *user_data)
601 {
602 	struct adapter *adapter = user_data;
603 	struct device *device;
604 
605 	if (g_slist_find_custom(adapter->devices,
606 				key, (GCompareFunc) device_address_cmp))
607 		return;
608 
609 	device = device_create(connection, adapter, key);
610 	if (device) {
611 		device->temporary = FALSE;
612 		adapter->devices = g_slist_append(adapter->devices, device);
613 	}
614 }
615 
register_devices(bdaddr_t * src,struct adapter * adapter)616 static void register_devices(bdaddr_t *src, struct adapter *adapter)
617 {
618 	char filename[PATH_MAX + 1];
619 	char addr[18];
620 
621 	ba2str(src, addr);
622 
623 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "profiles");
624 	textfile_foreach(filename, create_stored_device_from_profiles, adapter);
625 
626 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "linkkeys");
627 	textfile_foreach(filename, create_stored_device_from_linkkeys, adapter);
628 }
629 
hcid_dbus_start_device(uint16_t id)630 int hcid_dbus_start_device(uint16_t id)
631 {
632 	char path[MAX_PATH_LENGTH];
633 	char *ptr = path + ADAPTER_PATH_INDEX;
634 	struct hci_dev_info di;
635 	struct adapter* adapter;
636 	struct hci_conn_list_req *cl = NULL;
637 	struct hci_conn_info *ci;
638 	const char *mode;
639 	int i, err, dd = -1, ret = -1;
640 
641 	snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, id);
642 
643 	if (hci_devinfo(id, &di) < 0) {
644 		error("Getting device info failed: hci%d", id);
645 		return -1;
646 	}
647 
648 	if (hci_test_bit(HCI_RAW, &di.flags))
649 		return -1;
650 
651 	adapter = manager_find_adapter_by_path(path);
652 	if (!adapter) {
653 		error("Getting %s path data failed!", path);
654 		return -1;
655 	}
656 
657 	if (hci_test_bit(HCI_INQUIRY, &di.flags))
658 		adapter->discov_active = 1;
659 	else
660 		adapter->discov_active = 0;
661 
662 	adapter->up = 1;
663 	adapter->discov_timeout = get_discoverable_timeout(id);
664 	adapter->discov_type = DISCOVER_TYPE_NONE;
665 
666 	dd = hci_open_dev(id);
667 	if (dd < 0)
668 		goto failed;
669 
670 	adapter->scan_enable = get_startup_scan(id);
671 	hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE,
672 					1, &adapter->scan_enable);
673 	/*
674 	 * Get the adapter Bluetooth address
675 	 */
676 	err = get_device_address(adapter->dev_id, adapter->address,
677 					sizeof(adapter->address));
678 	if (err < 0)
679 		goto failed;
680 
681 	err = get_device_class(adapter->dev_id, adapter->class);
682 	if (err < 0)
683 		goto failed;
684 
685 	adapter->mode = get_startup_mode(id);
686 	if (adapter->mode == MODE_LIMITED)
687 		set_limited_discoverable(dd, adapter->class, TRUE);
688 
689 	/*
690 	 * retrieve the active connections: address the scenario where
691 	 * the are active connections before the daemon've started
692 	 */
693 
694 	cl = g_malloc0(10 * sizeof(*ci) + sizeof(*cl));
695 
696 	cl->dev_id = id;
697 	cl->conn_num = 10;
698 	ci = cl->conn_info;
699 
700 	if (ioctl(dd, HCIGETCONNLIST, cl) < 0)
701 		goto failed;
702 
703 	for (i = 0; i < cl->conn_num; i++, ci++)
704 		active_conn_append(&adapter->active_conn,
705 					&ci->bdaddr, ci->handle);
706 
707 	ret = 0;
708 
709 	mode = mode2str(adapter->mode);
710 	g_dbus_emit_signal(connection, path, ADAPTER_INTERFACE,
711 					"ModeChanged",
712 					DBUS_TYPE_STRING, &mode,
713 					DBUS_TYPE_INVALID);
714 
715 	if (hcid_dbus_use_experimental()) {
716 		dbus_connection_emit_property_changed(connection, ptr,
717 						ADAPTER_INTERFACE, "Mode",
718 						DBUS_TYPE_STRING, &mode);
719 	}
720 
721 	if (manager_get_default_adapter() < 0)
722 		manager_set_default_adapter(id);
723 
724 	if (hcid_dbus_use_experimental())
725 		register_devices(&di.bdaddr, adapter);
726 
727 failed:
728 	if (dd >= 0)
729 		hci_close_dev(dd);
730 
731 	g_free(cl);
732 
733 	return ret;
734 }
735 
send_dc_signal(struct active_conn_info * dev,const char * path)736 static void send_dc_signal(struct active_conn_info *dev, const char *path)
737 {
738 	char addr[18];
739 	const char *paddr = addr;
740 
741 	ba2str(&dev->bdaddr, addr);
742 
743 	g_dbus_emit_signal(connection, path, ADAPTER_INTERFACE,
744 					"RemoteDeviceDisconnected",
745 					DBUS_TYPE_STRING, &paddr,
746 					DBUS_TYPE_INVALID);
747 }
748 
hcid_dbus_stop_device(uint16_t id)749 int hcid_dbus_stop_device(uint16_t id)
750 {
751 	char path[MAX_PATH_LENGTH];
752 	struct adapter *adapter;
753 	const char *mode = "off";
754 
755 	snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, id);
756 
757 	adapter = manager_find_adapter_by_path(path);
758 	if (!adapter) {
759 		error("Getting %s path data failed!", path);
760 		return -1;
761 	}
762 
763 	/* cancel pending timeout */
764 	if (adapter->timeout_id) {
765 		g_source_remove(adapter->timeout_id);
766 		adapter->timeout_id = 0;
767 	}
768 
769 	/* check pending requests */
770 	reply_pending_requests(path, adapter);
771 
772 	cancel_passkey_agent_requests(adapter->passkey_agents, path, NULL);
773 
774 	release_passkey_agents(adapter, NULL);
775 
776 	if (adapter->discov_requestor) {
777 		g_dbus_remove_watch(connection, adapter->discov_listener);
778 		adapter->discov_listener = 0;
779 		g_free(adapter->discov_requestor);
780 		adapter->discov_requestor = NULL;
781 	}
782 
783 	if (adapter->pdiscov_requestor) {
784 		g_dbus_remove_watch(connection, adapter->pdiscov_listener);
785 		adapter->pdiscov_listener = 0;
786 		g_free(adapter->pdiscov_requestor);
787 		adapter->pdiscov_requestor = NULL;
788 	}
789 
790 	if (adapter->found_devices) {
791 		g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
792 		g_slist_free(adapter->found_devices);
793 		adapter->found_devices = NULL;
794 	}
795 
796 	if (adapter->oor_devices) {
797 		g_slist_foreach(adapter->oor_devices, (GFunc) free, NULL);
798 		g_slist_free(adapter->oor_devices);
799 		adapter->oor_devices = NULL;
800 	}
801 
802 	if (adapter->auth_reqs) {
803 		g_slist_foreach(adapter->auth_reqs, (GFunc) g_free, NULL);
804 		g_slist_free(adapter->auth_reqs);
805 		adapter->auth_reqs = NULL;
806 	}
807 
808 	if (adapter->active_conn) {
809 		g_slist_foreach(adapter->active_conn, (GFunc) send_dc_signal, path);
810 		g_slist_foreach(adapter->active_conn, (GFunc) g_free, NULL);
811 		g_slist_free(adapter->active_conn);
812 		adapter->active_conn = NULL;
813 	}
814 
815 	send_adapter_signal(connection, adapter->dev_id, "ModeChanged",
816 				DBUS_TYPE_STRING, &mode,
817 				DBUS_TYPE_INVALID);
818 
819 	if (hcid_dbus_use_experimental()) {
820 		const char *ptr = path + ADAPTER_PATH_INDEX;
821 		dbus_connection_emit_property_changed(connection, ptr,
822 						ADAPTER_INTERFACE, "Mode",
823 						DBUS_TYPE_STRING, &mode);
824 	}
825 
826 	adapter->up = 0;
827 	adapter->scan_enable = SCAN_DISABLED;
828 	adapter->mode = MODE_OFF;
829 	adapter->discov_active = 0;
830 	adapter->pdiscov_active = 0;
831 	adapter->pinq_idle = 0;
832 	adapter->discov_type = DISCOVER_TYPE_NONE;
833 
834 	return 0;
835 }
836 
pincode_cb(struct agent * agent,DBusError * err,const char * pincode,struct device * device)837 static void pincode_cb(struct agent *agent, DBusError *err, const char *pincode,
838 			struct device *device)
839 {
840 	struct adapter *adapter = device->adapter;
841 	pin_code_reply_cp pr;
842 	bdaddr_t sba, dba;
843 	size_t len;
844 	int dev;
845 	struct pending_auth_info *auth;
846 
847 	/* No need to reply anything if the authentication already failed */
848 	if (adapter->bonding && adapter->bonding->hci_status)
849 		return;
850 
851 	dev = hci_open_dev(adapter->dev_id);
852 	if (dev < 0) {
853 		error("hci_open_dev(%d): %s (%d)", adapter->dev_id,
854 				strerror(errno), errno);
855 		return;
856 	}
857 
858 	str2ba(adapter->address, &sba);
859 	str2ba(device->address, &dba);
860 
861 	auth = adapter_find_auth_request(adapter, &dba);
862 
863 	if (err) {
864 		hci_send_cmd(dev, OGF_LINK_CTL,
865 				OCF_PIN_CODE_NEG_REPLY, 6, &dba);
866 		goto done;
867 	}
868 
869 	len = strlen(pincode);
870 
871 	set_pin_length(&sba, len);
872 
873 	memset(&pr, 0, sizeof(pr));
874 	bacpy(&pr.bdaddr, &dba);
875 	memcpy(pr.pin_code, pincode, len);
876 	pr.pin_len = len;
877 	hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_REPLY, PIN_CODE_REPLY_CP_SIZE, &pr);
878 
879 done:
880 	if (auth) {
881 		auth->replied = TRUE;
882 		auth->agent = NULL;
883 	}
884 	hci_close_dev(dev);
885 }
886 
hcid_dbus_request_pin(int dev,bdaddr_t * sba,struct hci_conn_info * ci)887 int hcid_dbus_request_pin(int dev, bdaddr_t *sba, struct hci_conn_info *ci)
888 {
889 	char addr[18];
890 	struct adapter *adapter;
891 	struct device *device;
892 	struct agent *agent;
893 	int ret;
894 
895 	adapter = manager_find_adapter(sba);
896 	if (!adapter) {
897 		error("No matching adapter found");
898 		return -1;
899 	}
900 
901 	if (!hcid_dbus_use_experimental())
902 		goto old_fallback;
903 
904 	ba2str(&ci->bdaddr, addr);
905 
906 	device = adapter_find_device(adapter, addr);
907 	agent = device && device->agent ? device->agent : adapter->agent;
908 	if (!agent)
909 		goto old_fallback;
910 
911 	if (!device) {
912 		device = adapter_create_device(connection, adapter, addr);
913 		if (!device)
914 			return -ENODEV;
915 	}
916 
917 	ret = agent_request_pincode(agent, device,
918 					(agent_pincode_cb) pincode_cb,
919 					device);
920 	if (ret == 0) {
921 		struct pending_auth_info *auth;
922 		auth = adapter_new_auth_request(adapter, &ci->bdaddr,
923 						AUTH_TYPE_PINCODE);
924 		auth->agent = agent;
925 	}
926 
927 
928 	return ret;
929 
930 old_fallback:
931 	ret = handle_passkey_request_old(connection, dev, adapter, sba,
932 						&ci->bdaddr);
933 	if (ret == 0)
934 		adapter_new_auth_request(adapter, &ci->bdaddr,
935 						AUTH_TYPE_PINCODE);
936 	return ret;
937 }
938 
confirm_cb(struct agent * agent,DBusError * err,void * user_data)939 static void confirm_cb(struct agent *agent, DBusError *err, void *user_data)
940 {
941 	struct device *device = user_data;
942 	struct adapter *adapter = device->adapter;
943 	user_confirm_reply_cp cp;
944 	int dd;
945 	struct pending_auth_info *auth;
946 
947 	/* No need to reply anything if the authentication already failed */
948 	if (adapter->bonding && adapter->bonding->hci_status)
949 		return;
950 
951 	dd = hci_open_dev(adapter->dev_id);
952 	if (dd < 0) {
953 		error("Unable to open hci%d", adapter->dev_id);
954 		return;
955 	}
956 
957 	memset(&cp, 0, sizeof(cp));
958 	str2ba(device->address, &cp.bdaddr);
959 
960 	auth = adapter_find_auth_request(adapter, &cp.bdaddr);
961 
962 	if (err)
963 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,
964 					USER_CONFIRM_REPLY_CP_SIZE, &cp);
965 	else
966 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_REPLY,
967 					USER_CONFIRM_REPLY_CP_SIZE, &cp);
968 
969 	if (auth) {
970 		auth->replied = TRUE;
971 		auth->agent = FALSE;
972 	}
973 
974 	hci_close_dev(dd);
975 }
976 
passkey_cb(struct agent * agent,DBusError * err,uint32_t passkey,void * user_data)977 static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
978 			void *user_data)
979 {
980 	struct device *device = user_data;
981 	struct adapter *adapter = device->adapter;
982 	user_passkey_reply_cp cp;
983 	bdaddr_t dba;
984 	int dd;
985 	struct pending_auth_info *auth;
986 
987 	/* No need to reply anything if the authentication already failed */
988 	if (adapter->bonding && adapter->bonding->hci_status)
989 		return;
990 
991 	dd = hci_open_dev(adapter->dev_id);
992 	if (dd < 0) {
993 		error("Unable to open hci%d", adapter->dev_id);
994 		return;
995 	}
996 
997 	str2ba(device->address, &dba);
998 
999 	memset(&cp, 0, sizeof(cp));
1000 	bacpy(&cp.bdaddr, &dba);
1001 	cp.passkey = passkey;
1002 
1003 	auth = adapter_find_auth_request(adapter, &dba);
1004 
1005 	if (err)
1006 		hci_send_cmd(dd, OGF_LINK_CTL,
1007 				OCF_USER_PASSKEY_NEG_REPLY, 6, &dba);
1008 	else
1009 		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_PASSKEY_REPLY,
1010 					USER_PASSKEY_REPLY_CP_SIZE, &cp);
1011 
1012 	if (auth) {
1013 		auth->replied = TRUE;
1014 		auth->agent = NULL;
1015 	}
1016 
1017 	hci_close_dev(dd);
1018 }
1019 
get_auth_requirements(bdaddr_t * local,bdaddr_t * remote,uint8_t * auth)1020 static int get_auth_requirements(bdaddr_t *local, bdaddr_t *remote,
1021 							uint8_t *auth)
1022 {
1023 	struct hci_auth_info_req req;
1024 	char addr[18];
1025 	int err, dd, dev_id;
1026 
1027 	ba2str(local, addr);
1028 
1029 	dev_id = hci_devid(addr);
1030 	if (dev_id < 0)
1031 		return dev_id;
1032 
1033 	dd = hci_open_dev(dev_id);
1034 	if (dd < 0)
1035 		return dd;
1036 
1037 	memset(&req, 0, sizeof(req));
1038 	bacpy(&req.bdaddr, remote);
1039 
1040 	err = ioctl(dd, HCIGETAUTHINFO, (unsigned long) &req);
1041 	if (err < 0) {
1042 		debug("HCIGETAUTHINFO failed: %s (%d)",
1043 					strerror(errno), errno);
1044 		hci_close_dev(dd);
1045 		return err;
1046 	}
1047 
1048 	hci_close_dev(dd);
1049 
1050 	if (auth)
1051 		*auth = req.type;
1052 
1053 	return 0;
1054 }
1055 
hcid_dbus_user_confirm(bdaddr_t * sba,bdaddr_t * dba,uint32_t passkey)1056 int hcid_dbus_user_confirm(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
1057 {
1058 	struct adapter *adapter;
1059 	struct device *device;
1060 	struct agent *agent;
1061 	char addr[18];
1062 	uint8_t type;
1063 	struct pending_auth_info *auth;
1064 
1065 	adapter = manager_find_adapter(sba);
1066 	if (!adapter) {
1067 		error("No matching adapter found");
1068 		return -1;
1069 	}
1070 
1071 	if (get_auth_requirements(sba, dba, &type) < 0) {
1072 		int dd;
1073 
1074 		dd = hci_open_dev(adapter->dev_id);
1075 		if (dd < 0) {
1076 			error("Unable to open hci%d", adapter->dev_id);
1077 			return -1;
1078 		}
1079 
1080 		hci_send_cmd(dd, OGF_LINK_CTL,
1081 					OCF_USER_CONFIRM_NEG_REPLY, 6, dba);
1082 
1083 		hci_close_dev(dd);
1084 
1085 		return 0;
1086 	}
1087 
1088 	ba2str(dba, addr);
1089 
1090 	device = adapter_get_device(connection, adapter, addr);
1091 	if (!device) {
1092 		error("Device creation failed");
1093 		return -1;
1094 	}
1095 
1096 	/* If no MITM protection required, auto-accept */
1097 	if (!(device->auth & 0x01) && !(type & 0x01)) {
1098 		int dd;
1099 
1100 		dd = hci_open_dev(adapter->dev_id);
1101 		if (dd < 0) {
1102 			error("Unable to open hci%d", adapter->dev_id);
1103 			return -1;
1104 		}
1105 
1106 		hci_send_cmd(dd, OGF_LINK_CTL,
1107 					OCF_USER_CONFIRM_REPLY, 6, dba);
1108 
1109 		hci_close_dev(dd);
1110 
1111 		return 0;
1112 	}
1113 
1114 	if (device->agent)
1115 		agent = device->agent;
1116 	else
1117 		agent = adapter->agent;
1118 
1119 	if (!agent) {
1120 		error("No agent available for user confirm request");
1121 		return -1;
1122 	}
1123 
1124 	if (agent_request_confirmation(agent, device, passkey,
1125 						confirm_cb, device) < 0) {
1126 		error("Requesting passkey failed");
1127 		return -1;
1128 	}
1129 
1130 	auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_CONFIRM);
1131 	auth->agent = agent;
1132 
1133 	return 0;
1134 }
1135 
hcid_dbus_user_passkey(bdaddr_t * sba,bdaddr_t * dba)1136 int hcid_dbus_user_passkey(bdaddr_t *sba, bdaddr_t *dba)
1137 {
1138 	struct adapter *adapter;
1139 	struct device *device;
1140 	struct agent *agent;
1141 	char addr[18];
1142 	struct pending_auth_info *auth;
1143 
1144 	adapter = manager_find_adapter(sba);
1145 	if (!adapter) {
1146 		error("No matching adapter found");
1147 		return -1;
1148 	}
1149 
1150 	ba2str(dba, addr);
1151 
1152 	device = adapter_get_device(connection, adapter, addr);
1153 	if (device && device->agent)
1154 		agent = device->agent;
1155 	else
1156 		agent = adapter->agent;
1157 
1158 	if (!agent) {
1159 		error("No agent available for user confirm request");
1160 		return -1;
1161 	}
1162 
1163 	if (agent_request_passkey(agent, device, passkey_cb, device) < 0) {
1164 		error("Requesting passkey failed");
1165 		return -1;
1166 	}
1167 
1168 	auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_PASSKEY);
1169 	auth->agent = agent;
1170 
1171 	return 0;
1172 }
1173 
hcid_dbus_user_notify(bdaddr_t * sba,bdaddr_t * dba,uint32_t passkey)1174 int hcid_dbus_user_notify(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
1175 {
1176 	struct adapter *adapter;
1177 	struct device *device;
1178 	struct agent *agent;
1179 	char addr[18];
1180 	struct pending_auth_info *auth;
1181 
1182 	adapter = manager_find_adapter(sba);
1183 	if (!adapter) {
1184 		error("No matching adapter found");
1185 		return -1;
1186 	}
1187 
1188 	ba2str(dba, addr);
1189 
1190 	device = adapter_get_device(connection, adapter, addr);
1191 	if (device && device->agent)
1192 		agent = device->agent;
1193 	else
1194 		agent = adapter->agent;
1195 
1196 	if (!agent) {
1197 		error("No agent available for user confirm request");
1198 		return -1;
1199 	}
1200 
1201 	if (agent_display_passkey(agent, device, passkey) < 0) {
1202 		error("Displaying passkey failed");
1203 		return -1;
1204 	}
1205 
1206 	auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_NOTIFY);
1207 	auth->agent = agent;
1208 
1209 	return 0;
1210 }
1211 
hcid_dbus_bonding_process_complete(bdaddr_t * local,bdaddr_t * peer,uint8_t status)1212 void hcid_dbus_bonding_process_complete(bdaddr_t *local, bdaddr_t *peer,
1213 					uint8_t status)
1214 {
1215 	struct adapter *adapter;
1216 	char peer_addr[18];
1217 	const char *paddr = peer_addr;
1218 	DBusMessage *reply;
1219 	struct device *device;
1220 	struct bonding_request_info *bonding;
1221 	gboolean paired = TRUE;
1222 	struct pending_auth_info *auth;
1223 
1224 	debug("hcid_dbus_bonding_process_complete: status=%02x", status);
1225 
1226 	ba2str(peer, peer_addr);
1227 
1228 	adapter = manager_find_adapter(local);
1229 	if (!adapter) {
1230 		error("Unable to find matching adapter");
1231 		return;
1232 	}
1233 
1234 	if (status) {
1235 		if (adapter->bonding)
1236 			adapter->bonding->hci_status = status;
1237 		cancel_passkey_agent_requests(adapter->passkey_agents,
1238 						adapter->path, peer);
1239 	}
1240 
1241 	auth = adapter_find_auth_request(adapter, peer);
1242 	if (!auth) {
1243 		debug("hcid_dbus_bonding_process_complete: no pending auth request");
1244 		goto proceed;
1245 	}
1246 
1247 	if (auth->agent)
1248 		agent_cancel(auth->agent);
1249 
1250 	adapter_remove_auth_request(adapter, peer);
1251 
1252 	if (status)
1253 		goto proceed;
1254 
1255 	send_adapter_signal(connection, adapter->dev_id, "BondingCreated",
1256 				DBUS_TYPE_STRING, &paddr, DBUS_TYPE_INVALID);
1257 
1258 	device = adapter_get_device(connection, adapter, paddr);
1259 	if (device) {
1260 		char *ptr = adapter->path + ADAPTER_PATH_INDEX;
1261 
1262 		debug("hcid_dbus_bonding_process_complete: removing temporary flag");
1263 
1264 		device->temporary = FALSE;
1265 
1266 		g_dbus_emit_signal(connection, ptr,
1267 					ADAPTER_INTERFACE,
1268 					"DeviceCreated",
1269 					DBUS_TYPE_OBJECT_PATH,
1270 					&device->path,
1271 					DBUS_TYPE_INVALID);
1272 
1273 		dbus_connection_emit_property_changed(connection, device->path,
1274 					DEVICE_INTERFACE, "Paired",
1275 					DBUS_TYPE_BOOLEAN, &paired);
1276 	}
1277 
1278 proceed:
1279 
1280 	release_passkey_agents(adapter, peer);
1281 
1282 	bonding = adapter->bonding;
1283 	if (!bonding || bacmp(&bonding->bdaddr, peer))
1284 		return; /* skip: no bonding req pending */
1285 
1286 	if (bonding->cancel) {
1287 		/* reply authentication canceled */
1288 		error_authentication_canceled(connection, bonding->msg);
1289 		goto cleanup;
1290 	}
1291 
1292 	/* reply authentication success or an error */
1293 	if (dbus_message_is_method_call(bonding->msg, ADAPTER_INTERFACE,
1294 					"CreateBonding")) {
1295 		reply = new_authentication_return(bonding->msg, status);
1296 		dbus_connection_send(connection, reply, NULL);
1297 		dbus_message_unref(reply);
1298 	} else if ((device = adapter_find_device(adapter, paddr))) {
1299 		if (status) {
1300 			reply = new_authentication_return(bonding->msg, status);
1301 			dbus_connection_send(connection, reply, NULL);
1302 			dbus_message_unref(reply);
1303 		} else {
1304 			device->temporary = FALSE;
1305 			device_browse(device, bonding->conn,
1306 					bonding->msg, NULL);
1307 		}
1308 	}
1309 
1310 cleanup:
1311 	g_dbus_remove_watch(connection, adapter->bonding->listener_id);
1312 
1313 	if (adapter->bonding->io_id)
1314 		g_source_remove(adapter->bonding->io_id);
1315 	g_io_channel_close(adapter->bonding->io);
1316 	bonding_request_free(adapter->bonding);
1317 	adapter->bonding = NULL;
1318 }
1319 
hcid_dbus_inquiry_start(bdaddr_t * local)1320 void hcid_dbus_inquiry_start(bdaddr_t *local)
1321 {
1322 	struct adapter *adapter;
1323 
1324 	adapter = manager_find_adapter(local);
1325 	if (!adapter) {
1326 		error("Unable to find matching adapter");
1327 		return;
1328 	}
1329 
1330 	adapter->discov_active = 1;
1331 	/*
1332 	 * Cancel pending remote name request and clean the device list
1333 	 * when inquiry is supported in periodic inquiry idle state.
1334 	 */
1335 	if (adapter->pdiscov_active)
1336 		pending_remote_name_cancel(adapter);
1337 
1338 	/* Disable name resolution for non D-Bus clients */
1339 	if (!adapter->discov_requestor)
1340 		adapter->discov_type &= ~RESOLVE_NAME;
1341 
1342 	if (hcid_dbus_use_experimental())
1343 		dbus_connection_emit_property_changed(connection,
1344 				adapter->path + ADAPTER_PATH_INDEX,
1345 				ADAPTER_INTERFACE, "PeriodicDiscovery",
1346 				DBUS_TYPE_BOOLEAN, &adapter->discov_active);
1347 
1348 	send_adapter_signal(connection, adapter->dev_id, "DiscoveryStarted",
1349 				DBUS_TYPE_INVALID);
1350 
1351 	if (hcid_dbus_use_experimental())
1352 		g_dbus_emit_signal(connection,
1353 						adapter->path + ADAPTER_PATH_INDEX,
1354 						ADAPTER_INTERFACE,
1355 						"DiscoveryStarted",
1356 						DBUS_TYPE_INVALID);
1357 }
1358 
found_device_req_name(struct adapter * adapter)1359 int found_device_req_name(struct adapter *adapter)
1360 {
1361 	struct hci_request rq;
1362 	evt_cmd_status rp;
1363 	remote_name_req_cp cp;
1364 	struct remote_dev_info match;
1365 	GSList *l;
1366 	int dd, req_sent = 0;
1367 
1368 	/* get the next remote address */
1369 	if (!adapter->found_devices)
1370 		return -ENODATA;
1371 
1372 	memset(&match, 0, sizeof(struct remote_dev_info));
1373 	bacpy(&match.bdaddr, BDADDR_ANY);
1374 	match.name_status = NAME_REQUIRED;
1375 
1376 	l = g_slist_find_custom(adapter->found_devices, &match,
1377 					(GCompareFunc) found_device_cmp);
1378 	if (!l)
1379 		return -ENODATA;
1380 
1381 	dd = hci_open_dev(adapter->dev_id);
1382 	if (dd < 0)
1383 		return -errno;
1384 
1385 	memset(&rq, 0, sizeof(rq));
1386 	rq.ogf    = OGF_LINK_CTL;
1387 	rq.ocf    = OCF_REMOTE_NAME_REQ;
1388 	rq.cparam = &cp;
1389 	rq.clen   = REMOTE_NAME_REQ_CP_SIZE;
1390 	rq.rparam = &rp;
1391 	rq.rlen   = EVT_CMD_STATUS_SIZE;
1392 	rq.event  = EVT_CMD_STATUS;
1393 
1394 	/* send at least one request or return failed if the list is empty */
1395 	do {
1396 		struct remote_dev_info *dev = l->data;
1397 		char peer_addr[18];
1398 		const char *signal = NULL, *paddr = peer_addr;
1399 
1400 		 /* flag to indicate the current remote name requested */
1401 		dev->name_status = NAME_REQUESTED;
1402 
1403 		memset(&rp, 0, sizeof(rp));
1404 		memset(&cp, 0, sizeof(cp));
1405 		bacpy(&cp.bdaddr, &dev->bdaddr);
1406 		cp.pscan_rep_mode = 0x02;
1407 
1408 		ba2str(&dev->bdaddr, peer_addr);
1409 
1410 		if (hci_send_req(dd, &rq, 500) < 0) {
1411 			error("Unable to send the HCI remote name request: %s (%d)",
1412 						strerror(errno), errno);
1413 			signal = "RemoteNameFailed";
1414 		}
1415 
1416 		if (rp.status) {
1417 			error("Remote name request failed with status 0x%02x",
1418 					rp.status);
1419 			signal = "RemoteNameFailed";
1420 		}
1421 
1422 		if (!signal) {
1423 			req_sent = 1;
1424 			/* if we are in discovery, inform application of getting name */
1425 			if (adapter->discov_type & (STD_INQUIRY | PERIODIC_INQUIRY))
1426 				signal = "RemoteNameRequested";
1427 		}
1428 
1429 		if (signal)
1430 			send_adapter_signal(connection, adapter->dev_id, signal,
1431 						DBUS_TYPE_STRING, &paddr,
1432 						DBUS_TYPE_INVALID);
1433 
1434 		if (req_sent)
1435 			break;
1436 
1437 		/* if failed, request the next element */
1438 		/* remove the element from the list */
1439 		adapter->found_devices = g_slist_remove(adapter->found_devices, dev);
1440 		g_free(dev);
1441 
1442 		/* get the next element */
1443 		l = g_slist_find_custom(adapter->found_devices, &match,
1444 					(GCompareFunc) found_device_cmp);
1445 
1446 	} while (l);
1447 
1448 	hci_close_dev(dd);
1449 
1450 	if (!req_sent)
1451 		return -ENODATA;
1452 
1453 	return 0;
1454 }
1455 
send_out_of_range(const char * path,GSList * l)1456 static void send_out_of_range(const char *path, GSList *l)
1457 {
1458 	while (l) {
1459 		const char *peer_addr = l->data;
1460 
1461 		g_dbus_emit_signal(connection, path,
1462 						ADAPTER_INTERFACE,
1463 						"RemoteDeviceDisappeared",
1464 						DBUS_TYPE_STRING, &peer_addr,
1465 						DBUS_TYPE_INVALID);
1466 
1467 		if (hcid_dbus_use_experimental()) {
1468 			const char *ptr = path + ADAPTER_PATH_INDEX;
1469 			g_dbus_emit_signal(connection, ptr,
1470 						ADAPTER_INTERFACE,
1471 						"DeviceDisappeared",
1472 						DBUS_TYPE_STRING,
1473 						&peer_addr,
1474 						DBUS_TYPE_INVALID);
1475 		}
1476 
1477 		l = l->next;
1478 	}
1479 }
1480 
hcid_dbus_inquiry_complete(bdaddr_t * local)1481 void hcid_dbus_inquiry_complete(bdaddr_t *local)
1482 {
1483 	struct adapter *adapter;
1484 	struct remote_dev_info *dev;
1485 	bdaddr_t tmp;
1486 
1487 	adapter = manager_find_adapter(local);
1488 	if (!adapter) {
1489 		error("Unable to find matching adapter");
1490 		return;
1491 	}
1492 
1493 	/* Out of range verification */
1494 	if (adapter->pdiscov_active && !adapter->discov_active) {
1495 		GSList *l;
1496 
1497 		send_out_of_range(adapter->path, adapter->oor_devices);
1498 
1499 		g_slist_foreach(adapter->oor_devices, (GFunc) free, NULL);
1500 		g_slist_free(adapter->oor_devices);
1501 		adapter->oor_devices = NULL;
1502 
1503 		l = adapter->found_devices;
1504 		while (l) {
1505 			dev = l->data;
1506 			baswap(&tmp, &dev->bdaddr);
1507 			adapter->oor_devices = g_slist_append(adapter->oor_devices,
1508 								batostr(&tmp));
1509 			l = l->next;
1510 		}
1511 	}
1512 
1513 	adapter->pinq_idle = 1;
1514 
1515 	/*
1516 	 * Enable resolution again: standard inquiry can be
1517 	 * received in the periodic inquiry idle state.
1518 	 */
1519 	if (adapter->pdiscov_requestor && adapter->pdiscov_resolve_names)
1520 		adapter->discov_type |= RESOLVE_NAME;
1521 
1522 	/*
1523 	 * The following scenarios can happen:
1524 	 * 1. standard inquiry: always send discovery completed signal
1525 	 * 2. standard inquiry + name resolving: send discovery completed
1526 	 *    after name resolving
1527 	 * 3. periodic inquiry: skip discovery completed signal
1528 	 * 4. periodic inquiry + standard inquiry: always send discovery
1529 	 *    completed signal
1530 	 *
1531 	 * Keep in mind that non D-Bus requests can arrive.
1532 	 */
1533 
1534 	if (!found_device_req_name(adapter))
1535 		return;		/* skip - there is name to resolve */
1536 
1537 	if (adapter->discov_active) {
1538 		if (hcid_dbus_use_experimental()) {
1539 			const char *ptr = adapter->path + ADAPTER_PATH_INDEX;
1540 			g_dbus_emit_signal(connection, ptr,
1541 						ADAPTER_INTERFACE,
1542 						"DiscoveryCompleted",
1543 						DBUS_TYPE_INVALID);
1544 
1545 		}
1546 
1547 		g_dbus_emit_signal(connection, adapter->path,
1548 						ADAPTER_INTERFACE,
1549 						"DiscoveryCompleted",
1550 						DBUS_TYPE_INVALID);
1551 		adapter->discov_active = 0;
1552 	}
1553 
1554 	/* free discovered devices list */
1555 	g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
1556 	g_slist_free(adapter->found_devices);
1557 	adapter->found_devices = NULL;
1558 
1559 	if (adapter->discov_requestor) {
1560 		g_dbus_remove_watch(connection, adapter->discov_listener);
1561 		adapter->discov_listener = 0;
1562 		g_free(adapter->discov_requestor);
1563 		adapter->discov_requestor = NULL;
1564 
1565 		/* If there is a pending reply for discovery cancel */
1566 		if (adapter->discovery_cancel) {
1567 			DBusMessage *reply;
1568 			reply = dbus_message_new_method_return(adapter->discovery_cancel);
1569 			dbus_connection_send(connection, reply, NULL);
1570 			dbus_message_unref(reply);
1571 			dbus_message_unref(adapter->discovery_cancel);
1572 			adapter->discovery_cancel = NULL;
1573 		}
1574 
1575 		/* reset the discover type for standard inquiry only */
1576 		adapter->discov_type &= ~STD_INQUIRY;
1577 	}
1578 }
1579 
hcid_dbus_periodic_inquiry_start(bdaddr_t * local,uint8_t status)1580 void hcid_dbus_periodic_inquiry_start(bdaddr_t *local, uint8_t status)
1581 {
1582 	struct adapter *adapter;
1583 
1584 	/* Don't send the signal if the cmd failed */
1585 	if (status)
1586 		return;
1587 
1588 	adapter = manager_find_adapter(local);
1589 	if (!adapter) {
1590 		error("No matching adapter found");
1591 		return;
1592 	}
1593 
1594 	adapter->pdiscov_active = 1;
1595 
1596 	/* Disable name resolution for non D-Bus clients */
1597 	if (!adapter->pdiscov_requestor)
1598 		adapter->discov_type &= ~RESOLVE_NAME;
1599 
1600 	if (hcid_dbus_use_experimental())
1601 		dbus_connection_emit_property_changed(connection,
1602 						adapter->path + ADAPTER_PATH_INDEX,
1603 						ADAPTER_INTERFACE,
1604 						"PeriodicDiscovery",
1605 						DBUS_TYPE_BOOLEAN,
1606 						&adapter->pdiscov_active);
1607 
1608 	g_dbus_emit_signal(connection, adapter->path, ADAPTER_INTERFACE,
1609 					"PeriodicDiscoveryStarted",
1610 					DBUS_TYPE_INVALID);
1611 }
1612 
hcid_dbus_periodic_inquiry_exit(bdaddr_t * local,uint8_t status)1613 void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status)
1614 {
1615 	struct adapter *adapter;
1616 	char *ptr;
1617 
1618 	/* Don't send the signal if the cmd failed */
1619 	if (status)
1620 		return;
1621 
1622 	adapter = manager_find_adapter(local);
1623 	if (!adapter) {
1624 		error("No matching adapter found");
1625 		return;
1626 	}
1627 
1628 	ptr = adapter->path + ADAPTER_PATH_INDEX;
1629 
1630 	/* reset the discover type to be able to handle D-Bus and non D-Bus
1631 	 * requests */
1632 	adapter->pdiscov_active = 0;
1633 	adapter->discov_type &= ~(PERIODIC_INQUIRY | RESOLVE_NAME);
1634 
1635 	/* free discovered devices list */
1636 	g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
1637 	g_slist_free(adapter->found_devices);
1638 	adapter->found_devices = NULL;
1639 
1640 	/* free out of range devices list */
1641 	g_slist_foreach(adapter->oor_devices, (GFunc) free, NULL);
1642 	g_slist_free(adapter->oor_devices);
1643 	adapter->oor_devices = NULL;
1644 
1645 	if (adapter->pdiscov_requestor) {
1646 		g_dbus_remove_watch(connection, adapter->pdiscov_listener);
1647 		adapter->pdiscov_listener = 0;
1648 		g_free(adapter->pdiscov_requestor);
1649 		adapter->pdiscov_requestor = NULL;
1650 	}
1651 
1652 	 /* workaround: inquiry completed is not sent when exiting from
1653 	  * periodic inquiry */
1654 	if (adapter->discov_active) {
1655 		if (hcid_dbus_use_experimental())
1656 			g_dbus_emit_signal(connection, ptr,
1657 					ADAPTER_INTERFACE,
1658 					"DiscoveryCompleted",
1659 					DBUS_TYPE_INVALID);
1660 
1661 		g_dbus_emit_signal(connection, adapter->path,
1662 						ADAPTER_INTERFACE,
1663 						"DiscoveryCompleted",
1664 						DBUS_TYPE_INVALID);
1665 		adapter->discov_active = 0;
1666 	}
1667 
1668 	/* Send discovery completed signal if there isn't name to resolve */
1669 	g_dbus_emit_signal(connection, adapter->path,
1670 					ADAPTER_INTERFACE,
1671 					"PeriodicDiscoveryStopped",
1672 					DBUS_TYPE_INVALID);
1673 
1674 	if (hcid_dbus_use_experimental())
1675 		dbus_connection_emit_property_changed(connection, ptr,
1676 						ADAPTER_INTERFACE,
1677 						"PeriodicDiscovery",
1678 						DBUS_TYPE_BOOLEAN,
1679 						&adapter->discov_active);
1680 }
1681 
extract_eir_name(uint8_t * data,uint8_t * type)1682 static char *extract_eir_name(uint8_t *data, uint8_t *type)
1683 {
1684 	if (!data || !type)
1685 		return NULL;
1686 
1687 	if (data[0] == 0)
1688 		return NULL;
1689 
1690 	*type = data[1];
1691 
1692 	switch (*type) {
1693 	case 0x08:
1694 	case 0x09:
1695 		return strndup((char *) (data + 2), data[0] - 1);
1696 	}
1697 
1698 	return NULL;
1699 }
1700 
append_dict_valist(DBusMessageIter * iter,const char * first_key,va_list var_args)1701 static void append_dict_valist(DBusMessageIter *iter,
1702 					const char *first_key,
1703 					va_list var_args)
1704 {
1705 	DBusMessageIter dict;
1706 	const char *key;
1707 	int type;
1708 	void *val;
1709 
1710 	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1711 			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1712 			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1713 			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1714 
1715 	key = first_key;
1716 	while (key) {
1717 		type = va_arg(var_args, int);
1718 		val = va_arg(var_args, void *);
1719 		dbus_message_iter_append_dict_entry(&dict, key, type, val);
1720 		key = va_arg(var_args, char *);
1721 	}
1722 
1723 	dbus_message_iter_close_container(iter, &dict);
1724 }
1725 
emit_device_found(const char * path,const char * address,const char * first_key,...)1726 static void emit_device_found(const char *path, const char *address,
1727 				const char *first_key, ...)
1728 {
1729 	DBusMessage *signal;
1730 	DBusMessageIter iter;
1731 	va_list var_args;
1732 
1733 	signal = dbus_message_new_signal(path, ADAPTER_INTERFACE,
1734 					"DeviceFound");
1735 	if (!signal) {
1736 		error("Unable to allocate new %s.DeviceFound signal",
1737 				ADAPTER_INTERFACE);
1738 		return;
1739 	}
1740 	dbus_message_iter_init_append(signal, &iter);
1741 	dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &address);
1742 
1743 	va_start(var_args, first_key);
1744 	append_dict_valist(&iter, first_key, var_args);
1745 	va_end(var_args);
1746 
1747 	dbus_connection_send(connection, signal, NULL);
1748 
1749 	dbus_message_unref(signal);
1750 }
1751 
hcid_dbus_inquiry_result(bdaddr_t * local,bdaddr_t * peer,uint32_t class,int8_t rssi,uint8_t * data)1752 void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class,
1753 				int8_t rssi, uint8_t *data)
1754 {
1755 	char filename[PATH_MAX + 1];
1756 	struct adapter *adapter;
1757 	GSList *l;
1758 	char local_addr[18], peer_addr[18], *name, *tmp_name;
1759 	const char *paddr = peer_addr;
1760 	struct remote_dev_info match;
1761 	dbus_int16_t tmp_rssi = rssi;
1762 	uint8_t name_type = 0x00;
1763 	name_status_t name_status;
1764 
1765 	ba2str(local, local_addr);
1766 	ba2str(peer, peer_addr);
1767 
1768 	adapter = manager_find_adapter(local);
1769 	if (!adapter) {
1770 		error("No matching adapter found");
1771 		return;
1772 	}
1773 
1774 	write_remote_class(local, peer, class);
1775 
1776 	if (data)
1777 		write_remote_eir(local, peer, data);
1778 
1779 	/*
1780 	 * workaround to identify situation when the daemon started and
1781 	 * a standard inquiry or periodic inquiry was already running
1782 	 */
1783 	if (!adapter->discov_active && !adapter->pdiscov_active)
1784 		adapter->pdiscov_active = 1;
1785 
1786 	/* reset the idle flag when the inquiry complete event arrives */
1787 	if (adapter->pdiscov_active) {
1788 		adapter->pinq_idle = 0;
1789 
1790 		/* Out of range list update */
1791 		l = g_slist_find_custom(adapter->oor_devices, peer_addr,
1792 				(GCompareFunc) strcmp);
1793 		if (l) {
1794 			char *dev = l->data;
1795 			adapter->oor_devices = g_slist_remove(adapter->oor_devices,
1796 								dev);
1797 			g_free(dev);
1798 		}
1799 	}
1800 
1801 	/* send the device found signal */
1802 	g_dbus_emit_signal(connection, adapter->path,
1803 					ADAPTER_INTERFACE,
1804 					"RemoteDeviceFound",
1805 					DBUS_TYPE_STRING, &paddr,
1806 					DBUS_TYPE_UINT32, &class,
1807 					DBUS_TYPE_INT16, &tmp_rssi,
1808 					DBUS_TYPE_INVALID);
1809 
1810 	memset(&match, 0, sizeof(struct remote_dev_info));
1811 	bacpy(&match.bdaddr, peer);
1812 	match.name_status = NAME_SENT;
1813 	/* if found: don't send the name again */
1814 	l = g_slist_find_custom(adapter->found_devices, &match,
1815 			(GCompareFunc) found_device_cmp);
1816 	if (l)
1817 		return;
1818 
1819 	/* the inquiry result can be triggered by NON D-Bus client */
1820 	if (adapter->discov_type & RESOLVE_NAME)
1821 		name_status = NAME_REQUIRED;
1822 	else
1823 		name_status = NAME_NOT_REQUIRED;
1824 
1825 	create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "names");
1826 	name = textfile_get(filename, peer_addr);
1827 
1828 	tmp_name = extract_eir_name(data, &name_type);
1829 	if (tmp_name) {
1830 		if (name_type == 0x09) {
1831 			write_device_name(local, peer, tmp_name);
1832 			name_status = NAME_NOT_REQUIRED;
1833 
1834 			if (name)
1835 				g_free(name);
1836 
1837 			name = tmp_name;
1838 		} else {
1839 			if (name)
1840 				free(tmp_name);
1841 			else
1842 				name = tmp_name;
1843 		}
1844 	}
1845 
1846 	if (name) {
1847 		g_dbus_emit_signal(connection, adapter->path,
1848 						ADAPTER_INTERFACE,
1849 						"RemoteNameUpdated",
1850 						DBUS_TYPE_STRING, &paddr,
1851 						DBUS_TYPE_STRING, &name,
1852 						DBUS_TYPE_INVALID);
1853 		if (name_type != 0x08)
1854 			name_status = NAME_SENT;
1855 
1856 		if (hcid_dbus_use_experimental()) {
1857 			emit_device_found(adapter->path + ADAPTER_PATH_INDEX,
1858 					paddr,
1859 					"Address", DBUS_TYPE_STRING, &paddr,
1860 					"Class", DBUS_TYPE_UINT32, &class,
1861 					"RSSI", DBUS_TYPE_INT16, &tmp_rssi,
1862 					"Name", DBUS_TYPE_STRING, &name,
1863 					NULL);
1864 		}
1865 
1866 		g_free(name);
1867 	} else if (hcid_dbus_use_experimental()) {
1868 		emit_device_found(adapter->path + ADAPTER_PATH_INDEX,
1869 				paddr,
1870 				"Address", DBUS_TYPE_STRING, &paddr,
1871 				"Class", DBUS_TYPE_UINT32, &class,
1872 				"RSSI", DBUS_TYPE_INT16, &tmp_rssi,
1873 				NULL);
1874 	}
1875 
1876 	/* add in the list to track name sent/pending */
1877 	found_device_add(&adapter->found_devices, peer, rssi, name_status);
1878 }
1879 
hcid_dbus_remote_class(bdaddr_t * local,bdaddr_t * peer,uint32_t class)1880 void hcid_dbus_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
1881 {
1882 	char peer_addr[18];
1883 	const char *paddr = peer_addr;
1884 	uint32_t old_class = 0;
1885 	struct adapter *adapter;
1886 
1887 	read_remote_class(local, peer, &old_class);
1888 
1889 	if (old_class == class)
1890 		return;
1891 
1892 	adapter = manager_find_adapter(local);
1893 	if (!adapter) {
1894 		error("No matching adapter found");
1895 		return;
1896 	}
1897 
1898 	ba2str(peer, peer_addr);
1899 
1900 	send_adapter_signal(connection, adapter->dev_id,
1901 				"RemoteClassUpdated",
1902 				DBUS_TYPE_STRING, &paddr,
1903 				DBUS_TYPE_UINT32, &class,
1904 				DBUS_TYPE_INVALID);
1905 
1906 	if (hcid_dbus_use_experimental()) {
1907 		GSList *l;
1908 		struct device *device;
1909 
1910 		l = g_slist_find_custom(adapter->devices, paddr,
1911 				(GCompareFunc) device_address_cmp);
1912 		if (!l)
1913 			return;
1914 
1915 		device = l->data;
1916 		dbus_connection_emit_property_changed(connection,
1917 					device->path, DEVICE_INTERFACE,
1918 					"Class", DBUS_TYPE_UINT32, &class);
1919 	}
1920 }
1921 
hcid_dbus_remote_name(bdaddr_t * local,bdaddr_t * peer,uint8_t status,char * name)1922 void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status,
1923 				char *name)
1924 {
1925 	struct adapter *adapter;
1926 	char peer_addr[18];
1927 	const char *paddr = peer_addr;
1928 
1929 	adapter = manager_find_adapter(local);
1930 	if (!adapter) {
1931 		error("No matching adapter found");
1932 		return;
1933 	}
1934 
1935 	ba2str(peer, peer_addr);
1936 
1937 	if (status)
1938 		g_dbus_emit_signal(connection, adapter->path,
1939 						ADAPTER_INTERFACE,
1940 						"RemoteNameFailed",
1941 						DBUS_TYPE_STRING, &paddr,
1942 						DBUS_TYPE_INVALID);
1943 	else {
1944 		g_dbus_emit_signal(connection, adapter->path,
1945 						ADAPTER_INTERFACE,
1946 						"RemoteNameUpdated",
1947 						DBUS_TYPE_STRING, &paddr,
1948 						DBUS_TYPE_STRING, &name,
1949 						DBUS_TYPE_INVALID);
1950 
1951 		if (hcid_dbus_use_experimental()) {
1952 			struct device *device;
1953 
1954 			device = adapter_find_device(adapter, paddr);
1955 			if (device) {
1956 				dbus_connection_emit_property_changed(connection,
1957 						device->path, DEVICE_INTERFACE,
1958 						"Name", DBUS_TYPE_STRING, &name);
1959 			}
1960 		}
1961 	}
1962 
1963 	/* remove from remote name request list */
1964 	found_device_remove(&adapter->found_devices, peer);
1965 
1966 	/* check if there is more devices to request names */
1967 	if (!found_device_req_name(adapter))
1968 		return; /* skip if a new request has been sent */
1969 
1970 	/* free discovered devices list */
1971 	g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
1972 	g_slist_free(adapter->found_devices);
1973 	adapter->found_devices = NULL;
1974 
1975 	/* The discovery completed signal must be sent only for discover
1976 	 * devices request WITH name resolving */
1977 	if (adapter->discov_requestor) {
1978 		g_dbus_remove_watch(connection, adapter->discov_listener);
1979 		adapter->discov_listener = 0;
1980 		g_free(adapter->discov_requestor);
1981 		adapter->discov_requestor = NULL;
1982 
1983 		/* If there is a pending reply for discovery cancel */
1984 		if (adapter->discovery_cancel) {
1985 			DBusMessage *reply;
1986 			reply = dbus_message_new_method_return(adapter->discovery_cancel);
1987 			dbus_connection_send(connection, reply, NULL);
1988 			dbus_message_unref(reply);
1989 			dbus_message_unref(adapter->discovery_cancel);
1990 			adapter->discovery_cancel = NULL;
1991 		}
1992 
1993 		/* Disable name resolution for non D-Bus clients */
1994 		if (!adapter->pdiscov_requestor)
1995 			adapter->discov_type &= ~RESOLVE_NAME;
1996 	}
1997 
1998 	if (adapter->discov_active) {
1999 		if (hcid_dbus_use_experimental())
2000 			g_dbus_emit_signal(connection,
2001 					adapter->path + ADAPTER_PATH_INDEX,
2002 					ADAPTER_INTERFACE,
2003 					"DiscoveryCompleted",
2004 					DBUS_TYPE_INVALID);
2005 
2006 		g_dbus_emit_signal(connection, adapter->path,
2007 						ADAPTER_INTERFACE,
2008 						"DiscoveryCompleted",
2009 						DBUS_TYPE_INVALID);
2010 		adapter->discov_active = 0;
2011 	}
2012 }
2013 
hcid_dbus_conn_complete(bdaddr_t * local,uint8_t status,uint16_t handle,bdaddr_t * peer)2014 void hcid_dbus_conn_complete(bdaddr_t *local, uint8_t status, uint16_t handle,
2015 				bdaddr_t *peer)
2016 {
2017 	char peer_addr[18];
2018 	const char *paddr = peer_addr;
2019 	struct adapter *adapter;
2020 
2021 	adapter = manager_find_adapter(local);
2022 	if (!adapter) {
2023 		error("No matching adapter found");
2024 		return;
2025 	}
2026 
2027 	ba2str(peer, peer_addr);
2028 
2029 	if (status) {
2030 		struct pending_auth_info *auth;
2031 
2032 		cancel_passkey_agent_requests(adapter->passkey_agents,
2033 						adapter->path, peer);
2034 		release_passkey_agents(adapter, peer);
2035 
2036 		auth = adapter_find_auth_request(adapter, peer);
2037 		if (auth && auth->agent)
2038 			agent_cancel(auth->agent);
2039 
2040 		adapter_remove_auth_request(adapter, peer);
2041 
2042 		if (adapter->bonding)
2043 			adapter->bonding->hci_status = status;
2044 	} else {
2045 		/* Send the remote device connected signal */
2046 		g_dbus_emit_signal(connection, adapter->path,
2047 						ADAPTER_INTERFACE,
2048 						"RemoteDeviceConnected",
2049 						DBUS_TYPE_STRING, &paddr,
2050 						DBUS_TYPE_INVALID);
2051 
2052 		if (hcid_dbus_use_experimental()) {
2053 			struct device *device;
2054 			gboolean connected = TRUE;
2055 
2056 			device = adapter_find_device(adapter, paddr);
2057 			if (device) {
2058 				dbus_connection_emit_property_changed(connection,
2059 					device->path, DEVICE_INTERFACE,
2060 					"Connected", DBUS_TYPE_BOOLEAN,
2061 					&connected);
2062 			}
2063 		}
2064 
2065 		/* add in the active connetions list */
2066 		active_conn_append(&adapter->active_conn, peer, handle);
2067 	}
2068 }
2069 
hcid_dbus_disconn_complete(bdaddr_t * local,uint8_t status,uint16_t handle,uint8_t reason)2070 void hcid_dbus_disconn_complete(bdaddr_t *local, uint8_t status,
2071 				uint16_t handle, uint8_t reason)
2072 {
2073 	DBusMessage *reply;
2074 	char peer_addr[18];
2075 	const char *paddr = peer_addr;
2076 	struct adapter *adapter;
2077 	struct device *device;
2078 	struct active_conn_info *dev;
2079 	GSList *l;
2080 	gboolean connected = FALSE;
2081 	struct pending_auth_info *auth;
2082 
2083 	if (status) {
2084 		error("Disconnection failed: 0x%02x", status);
2085 		return;
2086 	}
2087 
2088 	adapter = manager_find_adapter(local);
2089 	if (!adapter) {
2090 		error("No matching adapter found");
2091 		return;
2092 	}
2093 
2094 	l = g_slist_find_custom(adapter->active_conn, &handle,
2095 				active_conn_find_by_handle);
2096 
2097 	if (!l)
2098 		return;
2099 
2100 	dev = l->data;
2101 
2102 	ba2str(&dev->bdaddr, peer_addr);
2103 
2104 	/* clean pending HCI cmds */
2105 	hci_req_queue_remove(adapter->dev_id, &dev->bdaddr);
2106 
2107 	/* Cancel D-Bus/non D-Bus requests */
2108 	cancel_passkey_agent_requests(adapter->passkey_agents, adapter->path,
2109 					&dev->bdaddr);
2110 	release_passkey_agents(adapter, &dev->bdaddr);
2111 
2112 	auth = adapter_find_auth_request(adapter, &dev->bdaddr);
2113 	if (auth && auth->agent)
2114 		agent_cancel(auth->agent);
2115 
2116 	adapter_remove_auth_request(adapter, &dev->bdaddr);
2117 
2118 	/* Check if there is a pending CreateBonding request */
2119 	if (adapter->bonding && (bacmp(&adapter->bonding->bdaddr, &dev->bdaddr) == 0)) {
2120 		if (adapter->bonding->cancel) {
2121 			/* reply authentication canceled */
2122 			error_authentication_canceled(connection,
2123 							adapter->bonding->msg);
2124 		} else {
2125 			reply = new_authentication_return(adapter->bonding->msg,
2126 							HCI_AUTHENTICATION_FAILURE);
2127 			dbus_connection_send(connection, reply, NULL);
2128 			dbus_message_unref(reply);
2129 		}
2130 
2131 		g_dbus_remove_watch(adapter->bonding->conn,
2132 					adapter->bonding->listener_id);
2133 
2134 		if (adapter->bonding->io_id)
2135 			g_source_remove(adapter->bonding->io_id);
2136 		g_io_channel_close(adapter->bonding->io);
2137 		bonding_request_free(adapter->bonding);
2138 		adapter->bonding = NULL;
2139 	}
2140 
2141 	/* Check if there is a pending RemoteDeviceDisconnect request */
2142 	if (adapter->pending_dc) {
2143 		reply = dbus_message_new_method_return(adapter->pending_dc->msg);
2144 		if (reply) {
2145 			dbus_connection_send(connection, reply, NULL);
2146 			dbus_message_unref(reply);
2147 		} else
2148 			error("Failed to allocate disconnect reply");
2149 
2150 		g_source_remove(adapter->pending_dc->timeout_id);
2151 		dc_pending_timeout_cleanup(adapter);
2152 	}
2153 
2154 	/* Send the remote device disconnected signal */
2155 	g_dbus_emit_signal(connection, adapter->path,
2156 					ADAPTER_INTERFACE,
2157 					"RemoteDeviceDisconnected",
2158 					DBUS_TYPE_STRING, &paddr,
2159 					DBUS_TYPE_INVALID);
2160 
2161 	adapter->active_conn = g_slist_remove(adapter->active_conn, dev);
2162 	g_free(dev);
2163 
2164 	device = adapter_find_device(adapter, paddr);
2165 	if (device) {
2166 		dbus_connection_emit_property_changed(connection,
2167 					device->path, DEVICE_INTERFACE,
2168 					"Connected", DBUS_TYPE_BOOLEAN,
2169 					&connected);
2170 		if (device->temporary) {
2171 			debug("Removing temporary device %s", device->address);
2172 			adapter_remove_device(connection, adapter, device);
2173 		}
2174 	}
2175 }
2176 
set_limited_discoverable(int dd,const uint8_t * cls,gboolean limited)2177 int set_limited_discoverable(int dd, const uint8_t *cls, gboolean limited)
2178 {
2179 	uint32_t dev_class;
2180 	int err;
2181 	int num = (limited ? 2 : 1);
2182 	uint8_t lap[] = { 0x33, 0x8b, 0x9e, 0x00, 0x8b, 0x9e };
2183 	/*
2184 	 * 1: giac
2185 	 * 2: giac + liac
2186 	 */
2187 	if (hci_write_current_iac_lap(dd, num, lap, 1000) < 0) {
2188 		err = errno;
2189 		error("Can't write current IAC LAP: %s(%d)",
2190 				strerror(err), err);
2191 		return -err;
2192 	}
2193 
2194 	if (limited) {
2195 		if (cls[1] & 0x20)
2196 			return 0; /* Already limited */
2197 
2198 		dev_class = (cls[2] << 16) | ((cls[1] | 0x20) << 8) | cls[0];
2199 	} else {
2200 		if (!(cls[1] & 0x20))
2201 			return 0; /* Already clear */
2202 
2203 		dev_class = (cls[2] << 16) | ((cls[1] & 0xdf) << 8) | cls[0];
2204 	}
2205 
2206 	if (hci_write_class_of_dev(dd, dev_class, 1000) < 0) {
2207 		err = errno;
2208 		error("Can't write class of device: %s (%d)",
2209 							strerror(err), err);
2210 		return -err;
2211 	}
2212 
2213 	return 0;
2214 }
2215 
set_service_classes(int dd,const uint8_t * cls,uint8_t value)2216 int set_service_classes(int dd, const uint8_t *cls, uint8_t value)
2217 {
2218 	uint32_t dev_class;
2219 	int err;
2220 
2221 	if (cls[2] == value)
2222 		return 0; /* Already set */
2223 
2224 	dev_class = (value << 16) | (cls[1] << 8) | cls[0];
2225 
2226 	if (hci_write_class_of_dev(dd, dev_class, 1000) < 0) {
2227 		err = errno;
2228 		error("Can't write class of device: %s (%d)",
2229 							strerror(err), err);
2230 		return -err;
2231 	}
2232 
2233 	return 0;
2234 }
2235 
discov_timeout_handler(void * data)2236 gboolean discov_timeout_handler(void *data)
2237 {
2238 	struct adapter *adapter = data;
2239 	struct hci_request rq;
2240 	int dd;
2241 	uint8_t scan_enable = adapter->scan_enable;
2242 	uint8_t status = 0;
2243 	gboolean retval = TRUE;
2244 
2245 	scan_enable &= ~SCAN_INQUIRY;
2246 
2247 	dd = hci_open_dev(adapter->dev_id);
2248 	if (dd < 0) {
2249 		error("HCI device open failed: hci%d", adapter->dev_id);
2250 		return TRUE;
2251 	}
2252 
2253 	memset(&rq, 0, sizeof(rq));
2254 	rq.ogf    = OGF_HOST_CTL;
2255 	rq.ocf    = OCF_WRITE_SCAN_ENABLE;
2256 	rq.cparam = &scan_enable;
2257 	rq.clen   = sizeof(scan_enable);
2258 	rq.rparam = &status;
2259 	rq.rlen   = sizeof(status);
2260 	rq.event  = EVT_CMD_COMPLETE;
2261 
2262 	if (hci_send_req(dd, &rq, 1000) < 0) {
2263 		error("Sending write scan enable command to hci%d failed: %s (%d)",
2264 				adapter->dev_id, strerror(errno), errno);
2265 		goto failed;
2266 	}
2267 	if (status) {
2268 		error("Setting scan enable failed with status 0x%02x", status);
2269 		goto failed;
2270 	}
2271 
2272 	set_limited_discoverable(dd, adapter->class, FALSE);
2273 
2274 	adapter->timeout_id = 0;
2275 	retval = FALSE;
2276 
2277 failed:
2278 	if (dd >= 0)
2279 		hci_close_dev(dd);
2280 
2281 	return retval;
2282 }
2283 
2284 /* Section reserved to device HCI callbacks */
2285 
hcid_dbus_setname_complete(bdaddr_t * local)2286 void hcid_dbus_setname_complete(bdaddr_t *local)
2287 {
2288 	int id, dd = -1;
2289 	read_local_name_rp rp;
2290 	struct hci_request rq;
2291 	const char *pname = (char *) rp.name;
2292 	char local_addr[18], name[249];
2293 
2294 	ba2str(local, local_addr);
2295 
2296 	id = hci_devid(local_addr);
2297 	if (id < 0) {
2298 		error("No matching device id for %s", local_addr);
2299 		return;
2300 	}
2301 
2302 	dd = hci_open_dev(id);
2303 	if (dd < 0) {
2304 		error("HCI device open failed: hci%d", id);
2305 		memset(&rp, 0, sizeof(rp));
2306 	} else {
2307 		memset(&rq, 0, sizeof(rq));
2308 		rq.ogf    = OGF_HOST_CTL;
2309 		rq.ocf    = OCF_READ_LOCAL_NAME;
2310 		rq.rparam = &rp;
2311 		rq.rlen   = READ_LOCAL_NAME_RP_SIZE;
2312 		rq.event  = EVT_CMD_COMPLETE;
2313 
2314 		if (hci_send_req(dd, &rq, 1000) < 0) {
2315 			error("Sending getting name command failed: %s (%d)",
2316 						strerror(errno), errno);
2317 			rp.name[0] = '\0';
2318 		} else if (rp.status) {
2319 			error("Getting name failed with status 0x%02x",
2320 					rp.status);
2321 			rp.name[0] = '\0';
2322 		}
2323 		hci_close_dev(dd);
2324 	}
2325 
2326 	strncpy(name, pname, sizeof(name) - 1);
2327 	name[248] = '\0';
2328 	pname = name;
2329 
2330 	send_adapter_signal(connection, id, "NameChanged",
2331 				DBUS_TYPE_STRING, &pname, DBUS_TYPE_INVALID);
2332 }
2333 
hcid_dbus_setscan_enable_complete(bdaddr_t * local)2334 void hcid_dbus_setscan_enable_complete(bdaddr_t *local)
2335 {
2336 	struct adapter *adapter;
2337 	read_scan_enable_rp rp;
2338 	struct hci_request rq;
2339 	int dd = -1;
2340 
2341 	adapter = manager_find_adapter(local);
2342 	if (!adapter) {
2343 		error("No matching adapter found");
2344 		return;
2345 	}
2346 
2347 	dd = hci_open_dev(adapter->dev_id);
2348 	if (dd < 0) {
2349 		error("HCI device open failed: hci%d", adapter->dev_id);
2350 		return;
2351 	}
2352 
2353 	memset(&rq, 0, sizeof(rq));
2354 	rq.ogf    = OGF_HOST_CTL;
2355 	rq.ocf    = OCF_READ_SCAN_ENABLE;
2356 	rq.rparam = &rp;
2357 	rq.rlen   = READ_SCAN_ENABLE_RP_SIZE;
2358 	rq.event  = EVT_CMD_COMPLETE;
2359 
2360 	if (hci_send_req(dd, &rq, 1000) < 0) {
2361 		error("Sending read scan enable command failed: %s (%d)",
2362 				strerror(errno), errno);
2363 		goto failed;
2364 	}
2365 
2366 	if (rp.status) {
2367 		error("Getting scan enable failed with status 0x%02x",
2368 				rp.status);
2369 		goto failed;
2370 	}
2371 
2372 	if (adapter->timeout_id) {
2373 		g_source_remove(adapter->timeout_id);
2374 		adapter->timeout_id = 0;
2375 	}
2376 
2377 	if (adapter->scan_enable != rp.enable)
2378 		adapter_mode_changed(adapter, rp.enable);
2379 
2380 failed:
2381 	if (dd >= 0)
2382 		hci_close_dev(dd);
2383 }
2384 
hcid_dbus_write_class_complete(bdaddr_t * local)2385 void hcid_dbus_write_class_complete(bdaddr_t *local)
2386 {
2387 	struct adapter *adapter;
2388 	int dd;
2389 	uint8_t cls[3];
2390 
2391 	adapter = manager_find_adapter(local);
2392 	if (!adapter) {
2393 		error("No matching adapter found");
2394 		return;
2395 	}
2396 
2397 	dd = hci_open_dev(adapter->dev_id);
2398 	if (dd < 0) {
2399 		error("HCI device open failed: hci%d", adapter->dev_id);
2400 		return;
2401 	}
2402 
2403 	if (hci_read_class_of_dev(dd, cls, 1000) < 0) {
2404 		error("Can't read class of device on hci%d: %s (%d)",
2405 			adapter->dev_id, strerror(errno), errno);
2406 		hci_close_dev(dd);
2407 		return;
2408 	}
2409 
2410 	write_local_class(local, cls);
2411 	set_device_class(adapter->dev_id, cls);
2412 	memcpy(adapter->class, cls, 3);
2413 
2414 	hci_close_dev(dd);
2415 }
2416 
hcid_dbus_write_simple_pairing_mode_complete(bdaddr_t * local)2417 void hcid_dbus_write_simple_pairing_mode_complete(bdaddr_t *local)
2418 {
2419 	char addr[18];
2420 	int dev_id, dd;
2421 	uint8_t mode;
2422 
2423 	ba2str(local, addr);
2424 
2425 	dev_id = hci_devid(addr);
2426 	if (dev_id < 0) {
2427 		error("No matching device id for %s", addr);
2428 		return;
2429 	}
2430 
2431 	dd = hci_open_dev(dev_id);
2432 	if (dd < 0) {
2433 		error("HCI device open failed: hci%d", dev_id);
2434 		return;
2435 	}
2436 
2437 	if (hci_read_simple_pairing_mode(dd, &mode, 1000) < 0) {
2438 		error("Can't read class of device on hci%d: %s(%d)",
2439 					dev_id, strerror(errno), errno);
2440 		hci_close_dev(dd);
2441 		return;
2442 	}
2443 
2444 	set_simple_pairing_mode(dev_id, mode);
2445 
2446 	hci_close_dev(dd);
2447 }
2448 
hcid_dbus_get_io_cap(bdaddr_t * local,bdaddr_t * remote,uint8_t * cap,uint8_t * auth)2449 int hcid_dbus_get_io_cap(bdaddr_t *local, bdaddr_t *remote,
2450 						uint8_t *cap, uint8_t *auth)
2451 {
2452 	struct adapter *adapter;
2453 	struct device *device;
2454 	struct agent *agent;
2455 	char addr[18];
2456 
2457 	adapter = manager_find_adapter(local);
2458 	if (!adapter) {
2459 		error("No matching adapter found");
2460 		return -1;
2461 	}
2462 
2463 	if (get_auth_requirements(local, remote, auth) < 0)
2464 		return -1;
2465 
2466 	ba2str(remote, addr);
2467 
2468 	device = adapter_find_device(adapter, addr);
2469 	if (device && device->agent) {
2470 		agent = device->agent;
2471 		*auth = 0x03;
2472 	} else
2473 		agent = adapter->agent;
2474 
2475 	if (!agent) {
2476 		if (!(*auth & 0x01)) {
2477 			/* No input, no output */
2478 			*cap = 0x03;
2479 			return 0;
2480 		}
2481 		error("No agent available for IO capability");
2482 		return -1;
2483 	}
2484 
2485 	*cap = agent_get_io_capability(agent);
2486 
2487 	return 0;
2488 }
2489 
hcid_dbus_set_io_cap(bdaddr_t * local,bdaddr_t * remote,uint8_t cap,uint8_t auth)2490 int hcid_dbus_set_io_cap(bdaddr_t *local, bdaddr_t *remote,
2491                                                 uint8_t cap, uint8_t auth)
2492 {
2493 	struct adapter *adapter;
2494 	struct device *device;
2495 	char addr[18];
2496 
2497 	adapter = manager_find_adapter(local);
2498 	if (!adapter) {
2499 		error("No matching adapter found");
2500 		return -1;
2501 	}
2502 
2503 	ba2str(remote, addr);
2504 
2505 	device = adapter_get_device(connection, adapter, addr);
2506 	if (device) {
2507 		device->cap = cap;
2508 		device->auth = auth;
2509 	}
2510 
2511 	return 0;
2512 }
2513 
inquiry_cancel(int dd,int to)2514 static int inquiry_cancel(int dd, int to)
2515 {
2516 	struct hci_request rq;
2517 	uint8_t status;
2518 
2519 	memset(&rq, 0, sizeof(rq));
2520 	rq.ogf    = OGF_LINK_CTL;
2521 	rq.ocf    = OCF_INQUIRY_CANCEL;
2522 	rq.rparam = &status;
2523 	rq.rlen   = sizeof(status);
2524 	rq.event = EVT_CMD_COMPLETE;
2525 
2526 	if (hci_send_req(dd, &rq, to) < 0)
2527 		return -1;
2528 
2529 	if (status) {
2530 		errno = bt_error(status);
2531 		return -1;
2532 	}
2533 
2534 	return 0;
2535 }
2536 
remote_name_cancel(int dd,bdaddr_t * dba,int to)2537 static int remote_name_cancel(int dd, bdaddr_t *dba, int to)
2538 {
2539 	remote_name_req_cancel_cp cp;
2540 	struct hci_request rq;
2541 	uint8_t status;
2542 
2543 	memset(&rq, 0, sizeof(rq));
2544 	memset(&cp, 0, sizeof(cp));
2545 
2546 	bacpy(&cp.bdaddr, dba);
2547 
2548 	rq.ogf    = OGF_LINK_CTL;
2549 	rq.ocf    = OCF_REMOTE_NAME_REQ_CANCEL;
2550 	rq.cparam = &cp;
2551 	rq.clen   = REMOTE_NAME_REQ_CANCEL_CP_SIZE;
2552 	rq.rparam = &status;
2553 	rq.rlen = sizeof(status);
2554 	rq.event = EVT_CMD_COMPLETE;
2555 
2556 	if (hci_send_req(dd, &rq, to) < 0)
2557 		return -1;
2558 
2559 	if (status) {
2560 		errno = bt_error(status);
2561 		return -1;
2562 	}
2563 
2564 	return 0;
2565 }
2566 
cancel_discovery(struct adapter * adapter)2567 int cancel_discovery(struct adapter *adapter)
2568 {
2569 	struct remote_dev_info *dev, match;
2570 	GSList *l;
2571 	int dd, err = 0;
2572 
2573 	if (!adapter->discov_active)
2574 		goto cleanup;
2575 
2576 	dd = hci_open_dev(adapter->dev_id);
2577 	if (dd < 0) {
2578 		err = -ENODEV;
2579 		goto cleanup;
2580 	}
2581 
2582 	/*
2583 	 * If there is a pending read remote name request means
2584 	 * that the inquiry complete event was already received
2585 	 */
2586 	memset(&match, 0, sizeof(struct remote_dev_info));
2587 	bacpy(&match.bdaddr, BDADDR_ANY);
2588 	match.name_status = NAME_REQUESTED;
2589 
2590 	l = g_slist_find_custom(adapter->found_devices, &match,
2591 				(GCompareFunc) found_device_cmp);
2592 	if (l) {
2593 		dev = l->data;
2594 		if (remote_name_cancel(dd, &dev->bdaddr, 1000) < 0) {
2595 			error("Read remote name cancel failed: %s, (%d)",
2596 					strerror(errno), errno);
2597 			err = -errno;
2598 		}
2599 	} else {
2600 		if (inquiry_cancel(dd, 1000) < 0) {
2601 			error("Inquiry cancel failed:%s (%d)",
2602 					strerror(errno), errno);
2603 			err = -errno;
2604 		}
2605 	}
2606 
2607 	hci_close_dev(dd);
2608 
2609 cleanup:
2610 	/*
2611 	 * Reset discov_requestor and discover_state in the remote name
2612 	 * request event handler or in the inquiry complete handler.
2613 	 */
2614 	g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
2615 	g_slist_free(adapter->found_devices);
2616 	adapter->found_devices = NULL;
2617 
2618 	/* Disable name resolution for non D-Bus clients */
2619 	if (!adapter->pdiscov_requestor)
2620 		adapter->discov_type &= ~RESOLVE_NAME;
2621 
2622 	return err;
2623 }
2624 
periodic_inquiry_exit(int dd,int to)2625 static int periodic_inquiry_exit(int dd, int to)
2626 {
2627 	struct hci_request rq;
2628 	uint8_t status;
2629 
2630 	memset(&rq, 0, sizeof(rq));
2631 	rq.ogf    = OGF_LINK_CTL;
2632 	rq.ocf    = OCF_EXIT_PERIODIC_INQUIRY;
2633 	rq.rparam = &status;
2634 	rq.rlen   = sizeof(status);
2635 	rq.event = EVT_CMD_COMPLETE;
2636 
2637 	if (hci_send_req(dd, &rq, to) < 0)
2638 		return -1;
2639 
2640 	if (status) {
2641 		errno = status;
2642 		return -1;
2643 	}
2644 
2645 	return 0;
2646 }
2647 
cancel_periodic_discovery(struct adapter * adapter)2648 int cancel_periodic_discovery(struct adapter *adapter)
2649 {
2650 	struct remote_dev_info *dev, match;
2651 	GSList *l;
2652 	int dd, err = 0;
2653 
2654 	if (!adapter->pdiscov_active)
2655 		goto cleanup;
2656 
2657 	dd = hci_open_dev(adapter->dev_id);
2658 	if (dd < 0) {
2659 		err = -ENODEV;
2660 		goto cleanup;
2661 	}
2662 	/* find the pending remote name request */
2663 	memset(&match, 0, sizeof(struct remote_dev_info));
2664 	bacpy(&match.bdaddr, BDADDR_ANY);
2665 	match.name_status = NAME_REQUESTED;
2666 
2667 	l = g_slist_find_custom(adapter->found_devices, &match,
2668 			(GCompareFunc) found_device_cmp);
2669 	if (l) {
2670 		dev = l->data;
2671 		if (remote_name_cancel(dd, &dev->bdaddr, 1000) < 0) {
2672 			error("Read remote name cancel failed: %s, (%d)",
2673 					strerror(errno), errno);
2674 			err = -errno;
2675 		}
2676 	}
2677 
2678 	/* ovewrite err if necessary: stop periodic inquiry has higher
2679 	 * priority */
2680 	if (periodic_inquiry_exit(dd, 1000) < 0) {
2681 		error("Periodic Inquiry exit failed:%s (%d)",
2682 				strerror(errno), errno);
2683 		err = -errno;
2684 	}
2685 
2686 	hci_close_dev(dd);
2687 
2688 cleanup:
2689 	/*
2690 	 * Reset pdiscov_requestor and pdiscov_active is done when the
2691 	 * cmd complete event for exit periodic inquiry mode cmd arrives.
2692 	 */
2693 	g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
2694 	g_slist_free(adapter->found_devices);
2695 	adapter->found_devices = NULL;
2696 
2697 	return err;
2698 }
2699 
2700 /* Most of the functions in this module require easy access to a connection so
2701  * we keep it global here and provide these access functions the other (few)
2702  * modules that require access to it */
2703 
set_dbus_connection(DBusConnection * conn)2704 void set_dbus_connection(DBusConnection *conn)
2705 {
2706 	connection = conn;
2707 }
2708 
get_dbus_connection(void)2709 DBusConnection *get_dbus_connection(void)
2710 {
2711 	return connection;
2712 }
2713