3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2006-2010 Nokia Corporation
6
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
37
#include <bluetooth/bluetooth.h>
40
#include <dbus/dbus.h>
48
#include "dbus-common.h"
53
static gboolean get_adapter_and_device(bdaddr_t *src, bdaddr_t *dst,
54
struct btd_adapter **adapter,
55
struct btd_device **device,
58
DBusConnection *conn = get_dbus_connection();
61
*adapter = manager_find_adapter(src);
63
error("Unable to find matching adapter");
67
ba2str(dst, peer_addr);
70
*device = adapter_get_device(conn, *adapter, peer_addr);
72
*device = adapter_find_device(*adapter, peer_addr);
74
if (create && !*device) {
75
error("Unable to get device object!");
82
/*****************************************************************
84
* Section reserved to HCI commands confirmation handling and low
85
* level events(eg: device attached/dettached.
87
*****************************************************************/
89
static void pincode_cb(struct agent *agent, DBusError *derr,
90
const char *pincode, struct btd_device *device)
92
struct btd_adapter *adapter = device_get_adapter(device);
96
device_get_address(device, &dba, NULL);
99
err = btd_adapter_pincode_reply(adapter, &dba, NULL, 0);
105
err = btd_adapter_pincode_reply(adapter, &dba, pincode,
106
pincode ? strlen(pincode) : 0);
113
error("Sending PIN code reply failed: %s (%d)", strerror(-err), -err);
116
int btd_event_request_pin(bdaddr_t *sba, bdaddr_t *dba, gboolean secure)
118
struct btd_adapter *adapter;
119
struct btd_device *device;
122
gboolean display = FALSE;
124
if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
127
memset(pin, 0, sizeof(pin));
128
pinlen = btd_adapter_get_pin(adapter, device, pin, &display);
129
if (pinlen > 0 && (!secure || pinlen == 16)) {
130
if (display && device_is_bonding(device, NULL))
131
return device_request_authentication(device,
132
AUTH_TYPE_NOTIFY_PINCODE, pin,
135
btd_adapter_pincode_reply(adapter, dba, pin, pinlen);
139
return device_request_authentication(device, AUTH_TYPE_PINCODE, NULL,
143
static int confirm_reply(struct btd_adapter *adapter,
144
struct btd_device *device, gboolean success)
149
device_get_address(device, &bdaddr, &bdaddr_type);
151
return btd_adapter_confirm_reply(adapter, &bdaddr, bdaddr_type,
155
static void confirm_cb(struct agent *agent, DBusError *err, void *user_data)
157
struct btd_device *device = user_data;
158
struct btd_adapter *adapter = device_get_adapter(device);
159
gboolean success = (err == NULL) ? TRUE : FALSE;
161
confirm_reply(adapter, device, success);
164
static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
167
struct btd_device *device = user_data;
168
struct btd_adapter *adapter = device_get_adapter(device);
172
device_get_address(device, &bdaddr, &bdaddr_type);
175
passkey = INVALID_PASSKEY;
177
btd_adapter_passkey_reply(adapter, &bdaddr, bdaddr_type, passkey);
180
int btd_event_user_confirm(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
182
struct btd_adapter *adapter;
183
struct btd_device *device;
185
if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
188
return device_request_authentication(device, AUTH_TYPE_CONFIRM,
189
&passkey, FALSE, confirm_cb);
192
int btd_event_user_passkey(bdaddr_t *sba, bdaddr_t *dba)
194
struct btd_adapter *adapter;
195
struct btd_device *device;
197
if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
200
return device_request_authentication(device, AUTH_TYPE_PASSKEY, NULL,
204
int btd_event_user_notify(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
206
struct btd_adapter *adapter;
207
struct btd_device *device;
209
if (!get_adapter_and_device(sba, dba, &adapter, &device, TRUE))
212
return device_request_authentication(device, AUTH_TYPE_NOTIFY_PASSKEY,
213
&passkey, FALSE, NULL);
216
void btd_event_simple_pairing_complete(bdaddr_t *local, bdaddr_t *peer,
219
struct btd_adapter *adapter;
220
struct btd_device *device;
223
DBG("status=%02x", status);
225
create = status ? FALSE : TRUE;
227
if (!get_adapter_and_device(local, peer, &adapter, &device, create))
233
device_simple_pairing_complete(device, status);
236
static void update_lastseen(bdaddr_t *sba, bdaddr_t *dba)
244
write_lastseen_info(sba, dba, tm);
247
static void update_lastused(bdaddr_t *sba, bdaddr_t *dba)
255
write_lastused_info(sba, dba, tm);
258
void btd_event_device_found(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
259
int8_t rssi, uint8_t confirm_name,
260
uint8_t *data, uint8_t data_len)
262
struct btd_adapter *adapter;
264
adapter = manager_find_adapter(local);
266
error("No matching adapter found");
270
update_lastseen(local, peer);
273
write_remote_eir(local, peer, data, data_len);
275
adapter_update_found_devices(adapter, peer, bdaddr_type, rssi,
276
confirm_name, data, data_len);
279
void btd_event_set_legacy_pairing(bdaddr_t *local, bdaddr_t *peer,
282
struct btd_adapter *adapter;
283
struct remote_dev_info *dev;
285
adapter = manager_find_adapter(local);
287
error("No matching adapter found");
291
dev = adapter_search_found_devices(adapter, peer);
293
dev->legacy = legacy;
296
void btd_event_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
298
struct btd_adapter *adapter;
299
struct btd_device *device;
300
uint32_t old_class = 0;
302
read_remote_class(local, peer, &old_class);
304
if (old_class == class)
307
write_remote_class(local, peer, class);
309
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
315
device_set_class(device, class);
318
void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
320
struct btd_adapter *adapter;
321
struct btd_device *device;
322
struct remote_dev_info *dev_info;
324
if (!g_utf8_validate(name, -1, NULL)) {
327
/* Assume ASCII, and replace all non-ASCII with spaces */
328
for (i = 0; name[i] != '\0'; i++) {
329
if (!isascii(name[i]))
332
/* Remove leading and trailing whitespace characters */
336
write_device_name(local, peer, name);
338
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
341
dev_info = adapter_search_found_devices(adapter, peer);
343
g_free(dev_info->name);
344
dev_info->name = g_strdup(name);
345
adapter_emit_device_found(adapter, dev_info);
349
device_set_name(device, name);
352
static char *buf2str(uint8_t *data, int datalen)
357
buf = g_try_new0(char, (datalen * 2) + 1);
361
for (i = 0; i < datalen; i++)
362
sprintf(buf + (i * 2), "%2.2x", data[i]);
367
static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
368
uint8_t bdaddr_type, unsigned char *key,
369
uint8_t master, uint8_t authenticated,
370
uint8_t enc_size, uint16_t ediv, uint8_t rand[8])
376
val = buf2str(key, 16);
380
newkey = g_string_new(val);
383
g_string_append_printf(newkey, " %d %d %d %d ", authenticated, master,
386
str = buf2str(rand, 8);
388
g_string_free(newkey, TRUE);
392
newkey = g_string_append(newkey, str);
395
err = write_longtermkeys(local, peer, bdaddr_type, newkey->str);
397
g_string_free(newkey, TRUE);
402
int btd_event_link_key_notify(bdaddr_t *local, bdaddr_t *peer,
403
uint8_t *key, uint8_t key_type,
406
struct btd_adapter *adapter;
407
struct btd_device *device;
410
if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
413
DBG("storing link key of type 0x%02x", key_type);
415
ret = write_link_key(local, peer, key, key_type, pin_length);
418
device_set_bonded(device, TRUE);
420
if (device_is_temporary(device))
421
device_set_temporary(device, FALSE);
427
int btd_event_ltk_notify(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
428
uint8_t *key, uint8_t master,
429
uint8_t authenticated, uint8_t enc_size,
430
uint16_t ediv, uint8_t rand[8])
432
struct btd_adapter *adapter;
433
struct btd_device *device;
436
if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
439
ret = store_longtermkey(local, peer, bdaddr_type, key, master,
440
authenticated, enc_size, ediv, rand);
442
device_set_bonded(device, TRUE);
444
if (device_is_temporary(device))
445
device_set_temporary(device, FALSE);
451
void btd_event_conn_complete(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
452
char *name, uint8_t *dev_class)
454
struct btd_adapter *adapter;
455
struct btd_device *device;
457
if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
460
update_lastused(local, peer);
462
if (dev_class != NULL) {
463
uint32_t class = dev_class[0] | (dev_class[1] << 8) |
464
(dev_class[2] << 16);
467
write_remote_class(local, peer, class);
470
device_set_addr_type(device, bdaddr_type);
472
adapter_add_connection(adapter, device);
475
btd_event_remote_name(local, peer, name);
478
void btd_event_conn_failed(bdaddr_t *local, bdaddr_t *peer, uint8_t status)
480
struct btd_adapter *adapter;
481
struct btd_device *device;
482
DBusConnection *conn = get_dbus_connection();
484
DBG("status 0x%02x", status);
486
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
492
if (device_is_bonding(device, NULL))
493
device_cancel_bonding(device, status);
495
if (device_is_temporary(device))
496
adapter_remove_device(conn, adapter, device, TRUE);
499
void btd_event_disconn_complete(bdaddr_t *local, bdaddr_t *peer)
501
struct btd_adapter *adapter;
502
struct btd_device *device;
506
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
512
adapter_remove_connection(adapter, device);
515
void btd_event_device_blocked(bdaddr_t *local, bdaddr_t *peer)
517
struct btd_adapter *adapter;
518
struct btd_device *device;
520
DBusConnection *conn = get_dbus_connection();
522
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
526
device_block(conn, device, TRUE);
529
void btd_event_device_unblocked(bdaddr_t *local, bdaddr_t *peer)
531
struct btd_adapter *adapter;
532
struct btd_device *device;
534
DBusConnection *conn = get_dbus_connection();
536
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
540
device_unblock(conn, device, FALSE, TRUE);
543
void btd_event_device_unpaired(bdaddr_t *local, bdaddr_t *peer)
545
struct btd_adapter *adapter;
546
struct btd_device *device;
547
DBusConnection *conn = get_dbus_connection();
549
if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
552
device_set_temporary(device, TRUE);
554
if (device_is_connected(device))
555
device_request_disconnect(device, NULL);
557
adapter_remove_device(conn, adapter, device, TRUE);
560
/* Section reserved to device HCI callbacks */
562
void btd_event_returned_link_key(bdaddr_t *local, bdaddr_t *peer)
564
struct btd_adapter *adapter;
565
struct btd_device *device;
567
if (!get_adapter_and_device(local, peer, &adapter, &device, TRUE))
570
device_set_paired(device, TRUE);