1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2006-2010  Nokia Corporation
6 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 *  This program is free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2 of the License, or
12 *  (at your option) any later version.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 */
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29#define _GNU_SOURCE
30#include <stdio.h>
31#include <ctype.h>
32#include <errno.h>
33#include <unistd.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/param.h>
37#include <sys/ioctl.h>
38#include <sys/socket.h>
39
40#include <bluetooth/bluetooth.h>
41#include <bluetooth/sdp.h>
42
43#include <glib.h>
44#include <dbus/dbus.h>
45#include <gdbus.h>
46
47#include "log.h"
48#include "textfile.h"
49
50#include "hcid.h"
51#include "adapter.h"
52#include "manager.h"
53#include "device.h"
54#include "error.h"
55#include "glib-helper.h"
56#include "dbus-common.h"
57#include "agent.h"
58#include "storage.h"
59#include "event.h"
60#include "sdpd.h"
61#include "eir.h"
62
63static gboolean get_adapter_and_device(bdaddr_t *src, bdaddr_t *dst,
64					struct btd_adapter **adapter,
65					struct btd_device **device,
66					gboolean create)
67{
68	DBusConnection *conn = get_dbus_connection();
69	char peer_addr[18];
70
71	*adapter = manager_find_adapter(src);
72	if (!*adapter) {
73		error("Unable to find matching adapter");
74		return FALSE;
75	}
76
77	ba2str(dst, peer_addr);
78
79	if (create)
80		*device = adapter_get_device(conn, *adapter, peer_addr);
81	else
82		*device = adapter_find_device(*adapter, peer_addr);
83
84	if (create && !*device) {
85		error("Unable to get device object!");
86		return FALSE;
87	}
88
89	return TRUE;
90}
91
92/*****************************************************************
93 *
94 *  Section reserved to HCI commands confirmation handling and low
95 *  level events(eg: device attached/dettached.
96 *
97 *****************************************************************/
98
99static size_t decode_hex(const char *pin, char *out)
100{
101	size_t i;
102
103	for (i = 0; i < 16 && pin[i * 2] && pin[i * 2 + 1]; i++)
104		sscanf(&pin[i * 2], "%02hhX", &out[i]);
105
106	return i;
107}
108
109static size_t decode_pin(const char *pin, char *out)
110{
111	size_t len;
112
113	if (!pin)
114		return 0;
115
116	if (pin[0] == '$') {
117		len = decode_hex(&pin[1], out);
118	} else {
119		len = strnlen(pin, 16);
120		memcpy(out, pin, len);
121	}
122
123	return len;
124}
125
126static void pincode_cb(struct agent *agent, DBusError *derr,
127				const char *pincode, struct btd_device *device)
128{
129	struct btd_adapter *adapter = device_get_adapter(device);
130	bdaddr_t dba;
131	int err;
132	size_t len;
133	char rawpin[16];
134
135	device_get_address(device, &dba);
136
137	len = decode_pin(pincode, rawpin);
138	if (derr || !len) {
139		err = btd_adapter_pincode_reply(adapter, &dba, NULL, 0);
140		if (err < 0)
141			goto fail;
142		return;
143	}
144
145	err = btd_adapter_pincode_reply(adapter, &dba, rawpin, len);
146	if (err < 0)
147		goto fail;
148
149	return;
150
151fail:
152	error("Sending PIN code reply failed: %s (%d)", strerror(-err), -err);
153}
154
155int btd_event_request_pin(bdaddr_t *sba, bdaddr_t *dba)
156{
157	struct btd_adapter *adapter;
158	struct btd_device *device;
159	char pin[17];
160	int pinlen;
161
162	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
163		return -ENODEV;
164
165	memset(pin, 0, sizeof(pin));
166	pinlen = read_pin_code(sba, dba, pin);
167	if (pinlen > 0) {
168		btd_adapter_pincode_reply(adapter, dba, pin, pinlen);
169		return 0;
170	}
171
172	return device_request_authentication(device, AUTH_TYPE_PINCODE, 0,
173								pincode_cb);
174}
175
176static int confirm_reply(struct btd_adapter *adapter,
177				struct btd_device *device, gboolean success)
178{
179	bdaddr_t bdaddr;
180
181	device_get_address(device, &bdaddr);
182
183	return btd_adapter_confirm_reply(adapter, &bdaddr, success);
184}
185
186static void confirm_cb(struct agent *agent, DBusError *err, void *user_data)
187{
188	struct btd_device *device = user_data;
189	struct btd_adapter *adapter = device_get_adapter(device);
190	gboolean success = (err == NULL) ? TRUE : FALSE;
191
192	confirm_reply(adapter, device, success);
193}
194
195static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
196			void *user_data)
197{
198	struct btd_device *device = user_data;
199	struct btd_adapter *adapter = device_get_adapter(device);
200	bdaddr_t bdaddr;
201
202	device_get_address(device, &bdaddr);
203
204	if (err)
205		passkey = INVALID_PASSKEY;
206
207	btd_adapter_passkey_reply(adapter, &bdaddr, passkey);
208}
209
210int btd_event_user_confirm(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
211{
212	struct btd_adapter *adapter;
213	struct btd_device *device;
214
215	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
216		return -ENODEV;
217
218	return device_request_authentication(device, AUTH_TYPE_CONFIRM,
219							passkey, confirm_cb);
220}
221
222int btd_event_user_consent(bdaddr_t *sba, bdaddr_t *dba)
223{
224	struct btd_adapter *adapter;
225	struct btd_device *device;
226
227	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
228		return -ENODEV;
229
230	return device_request_authentication(device, AUTH_TYPE_PAIRING_CONSENT,
231						0, confirm_cb);
232}
233
234int btd_event_user_passkey(bdaddr_t *sba, bdaddr_t *dba)
235{
236	struct btd_adapter *adapter;
237	struct btd_device *device;
238
239	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
240		return -ENODEV;
241
242	return device_request_authentication(device, AUTH_TYPE_PASSKEY, 0,
243								passkey_cb);
244}
245
246int btd_event_user_notify(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
247{
248	struct btd_adapter *adapter;
249	struct btd_device *device;
250
251	if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
252		return -ENODEV;
253
254	return device_request_authentication(device, AUTH_TYPE_NOTIFY,
255								passkey, NULL);
256}
257
258void btd_event_bonding_complete(bdaddr_t *local, bdaddr_t *peer,
259							uint8_t status)
260{
261	struct btd_adapter *adapter;
262	struct btd_device *device;
263	gboolean create;
264
265	DBG("status 0x%02x", status);
266
267	create = status ? FALSE : TRUE;
268
269	if (!get_adapter_and_device(local, peer, &adapter, &device, create))
270		return;
271
272	if (device)
273		device_bonding_complete(device, status);
274}
275
276void btd_event_simple_pairing_complete(bdaddr_t *local, bdaddr_t *peer,
277								uint8_t status)
278{
279	struct btd_adapter *adapter;
280	struct btd_device *device;
281	gboolean create;
282
283	DBG("status=%02x", status);
284
285	create = status ? FALSE : TRUE;
286
287	if (!get_adapter_and_device(local, peer, &adapter, &device, create))
288		return;
289
290	if (!device)
291		return;
292
293	device_simple_pairing_complete(device, status);
294}
295
296static void update_lastseen(bdaddr_t *sba, bdaddr_t *dba)
297{
298	time_t t;
299	struct tm *tm;
300
301	t = time(NULL);
302	tm = gmtime(&t);
303
304	write_lastseen_info(sba, dba, tm);
305}
306
307static void update_lastused(bdaddr_t *sba, bdaddr_t *dba)
308{
309	time_t t;
310	struct tm *tm;
311
312	t = time(NULL);
313	tm = gmtime(&t);
314
315	write_lastused_info(sba, dba, tm);
316}
317
318void btd_event_device_found(bdaddr_t *local, bdaddr_t *peer, uint32_t class,
319				int8_t rssi, uint8_t *data)
320{
321	struct btd_adapter *adapter;
322
323	adapter = manager_find_adapter(local);
324	if (!adapter) {
325		error("No matching adapter found");
326		return;
327	}
328
329	update_lastseen(local, peer);
330	write_remote_class(local, peer, class);
331
332	if (data)
333		write_remote_eir(local, peer, data);
334
335	adapter_update_found_devices(adapter, peer, class, rssi, data);
336}
337
338void btd_event_set_legacy_pairing(bdaddr_t *local, bdaddr_t *peer,
339							gboolean legacy)
340{
341	struct btd_adapter *adapter;
342	struct remote_dev_info *dev, match;
343
344	adapter = manager_find_adapter(local);
345	if (!adapter) {
346		error("No matching adapter found");
347		return;
348	}
349
350	memset(&match, 0, sizeof(struct remote_dev_info));
351	bacpy(&match.bdaddr, peer);
352	match.name_status = NAME_ANY;
353
354	dev = adapter_search_found_devices(adapter, &match);
355	if (dev)
356		dev->legacy = legacy;
357}
358
359void btd_event_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
360{
361	struct btd_adapter *adapter;
362	struct btd_device *device;
363	uint32_t old_class = 0;
364
365	read_remote_class(local, peer, &old_class);
366
367	if (old_class == class)
368		return;
369
370	write_remote_class(local, peer, class);
371
372	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
373		return;
374
375	if (!device)
376		return;
377
378	device_set_class(device, class);
379}
380
381void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status,
382				char *name)
383{
384	struct btd_adapter *adapter;
385	char srcaddr[18], dstaddr[18];
386	struct btd_device *device;
387	struct remote_dev_info match, *dev_info;
388
389	if (status == 0) {
390		if (!g_utf8_validate(name, -1, NULL)) {
391			int i;
392
393			/* Assume ASCII, and replace all non-ASCII with
394			 * spaces */
395			for (i = 0; name[i] != '\0'; i++) {
396				if (!isascii(name[i]))
397					name[i] = ' ';
398			}
399			/* Remove leading and trailing whitespace characters */
400			g_strstrip(name);
401		}
402
403		write_device_name(local, peer, name);
404	}
405
406	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
407		return;
408
409	ba2str(local, srcaddr);
410	ba2str(peer, dstaddr);
411
412	if (status != 0)
413		goto proceed;
414
415	bacpy(&match.bdaddr, peer);
416	match.name_status = NAME_ANY;
417
418	dev_info = adapter_search_found_devices(adapter, &match);
419	if (dev_info) {
420		g_free(dev_info->name);
421		dev_info->name = g_strdup(name);
422		adapter_emit_device_found(adapter, dev_info);
423	}
424
425	if (device)
426		device_set_name(device, name);
427
428proceed:
429	/* remove from remote name request list */
430	adapter_remove_found_device(adapter, peer);
431
432	/* check if there is more devices to request names */
433	if (adapter_resolve_names(adapter) == 0)
434		return;
435
436	adapter_set_state(adapter, STATE_IDLE);
437}
438
439int btd_event_link_key_notify(bdaddr_t *local, bdaddr_t *peer,
440				uint8_t *key, uint8_t key_type,
441				uint8_t pin_length)
442{
443	struct btd_adapter *adapter;
444	struct btd_device *device;
445	int ret;
446
447	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
448		return -ENODEV;
449
450	DBG("storing link key of type 0x%02x", key_type);
451
452	ret = write_link_key(local, peer, key, key_type, pin_length);
453
454	if (ret == 0) {
455		device_set_bonded(device, TRUE);
456
457		if (device_is_temporary(device))
458			device_set_temporary(device, FALSE);
459	}
460
461	return ret;
462}
463
464void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer)
465{
466	struct btd_adapter *adapter;
467	struct btd_device *device;
468
469	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
470		return;
471
472	update_lastused(local, peer);
473
474	adapter_add_connection(adapter, device);
475}
476
477void btd_event_conn_failed(bdaddr_t *local, bdaddr_t *peer, uint8_t status)
478{
479	struct btd_adapter *adapter;
480	struct btd_device *device;
481	DBusConnection *conn = get_dbus_connection();
482
483	DBG("status 0x%02x", status);
484
485	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
486		return;
487
488	if (!device)
489		return;
490
491	if (device_is_temporary(device))
492		adapter_remove_device(conn, adapter, device, TRUE);
493}
494
495void btd_event_disconn_complete(bdaddr_t *local, bdaddr_t *peer)
496{
497	struct btd_adapter *adapter;
498	struct btd_device *device;
499
500	DBG("");
501
502	if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
503		return;
504
505	if (!device)
506		return;
507
508	adapter_remove_connection(adapter, device);
509}
510
511/* Section reserved to device HCI callbacks */
512
513void btd_event_returned_link_key(bdaddr_t *local, bdaddr_t *peer)
514{
515	struct btd_adapter *adapter;
516	struct btd_device *device;
517
518	if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
519		return;
520
521	device_set_paired(device, TRUE);
522}
523