~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to src/device.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  BlueZ - Bluetooth protocol stack for Linux
4
4
 *
5
 
 *  Copyright (C) 2006-2007  Nokia Corporation
6
 
 *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
 
5
 *  Copyright (C) 2006-2010  Nokia Corporation
 
6
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7
7
 *
8
8
 *
9
9
 *  This program is free software; you can redistribute it and/or modify
31
31
#include <unistd.h>
32
32
#include <fcntl.h>
33
33
#include <sys/stat.h>
 
34
#include <sys/ioctl.h>
34
35
#include <errno.h>
35
36
 
36
37
#include <bluetooth/bluetooth.h>
37
 
#include <bluetooth/hci.h>
38
 
#include <bluetooth/hci_lib.h>
39
 
#include <bluetooth/l2cap.h>
40
38
#include <bluetooth/sdp.h>
41
39
#include <bluetooth/sdp_lib.h>
42
40
 
44
42
#include <dbus/dbus.h>
45
43
#include <gdbus.h>
46
44
 
47
 
#include "logging.h"
 
45
#include "log.h"
48
46
#include "textfile.h"
49
47
 
50
48
#include "hcid.h"
51
49
#include "adapter.h"
52
50
#include "device.h"
53
51
#include "dbus-common.h"
54
 
#include "dbus-hci.h"
 
52
#include "event.h"
55
53
#include "error.h"
56
54
#include "glib-helper.h"
57
55
#include "agent.h"
59
57
#include "storage.h"
60
58
#include "btio.h"
61
59
 
62
 
#define DEFAULT_XML_BUF_SIZE    1024
63
60
#define DISCONNECT_TIMER        2
64
61
#define DISCOVERY_TIMER         2
65
62
 
 
63
/* When all services should trust a remote device */
 
64
#define GLOBAL_TRUST "[all]"
 
65
 
66
66
struct btd_driver_data {
 
67
        guint id;
67
68
        struct btd_device_driver *driver;
68
69
        void *priv;
69
70
};
70
71
 
 
72
struct btd_disconnect_data {
 
73
        guint id;
 
74
        disconnect_watch watch;
 
75
        void *user_data;
 
76
        GDestroyNotify destroy;
 
77
};
 
78
 
71
79
struct bonding_req {
72
80
        DBusConnection *conn;
73
81
        DBusMessage *msg;
74
82
        GIOChannel *io;
75
 
        guint io_id;
76
83
        guint listener_id;
77
84
        struct btd_device *device;
78
85
};
84
91
        struct btd_device *device;
85
92
};
86
93
 
 
94
struct browse_req {
 
95
        DBusConnection *conn;
 
96
        DBusMessage *msg;
 
97
        struct btd_device *device;
 
98
        GSList *match_uuids;
 
99
        GSList *profiles_added;
 
100
        GSList *profiles_removed;
 
101
        sdp_list_t *records;
 
102
        int search_uuid;
 
103
        int reconnect_attempt;
 
104
        guint listener_id;
 
105
        guint timer;
 
106
};
 
107
 
87
108
struct btd_device {
88
109
        bdaddr_t        bdaddr;
 
110
        gboolean        le;
89
111
        gchar           *path;
90
 
        char            name[248];
 
112
        char            name[MAX_NAME_LENGTH + 1];
 
113
        char            *alias;
91
114
        struct btd_adapter      *adapter;
92
115
        GSList          *uuids;
 
116
        GSList          *services;              /* Primary services path */
93
117
        GSList          *drivers;               /* List of driver_data */
 
118
        GSList          *watches;               /* List of disconnect_data */
94
119
        gboolean        temporary;
95
120
        struct agent    *agent;
96
121
        guint           disconn_timer;
97
 
        int             discov_active;          /* Service discovery active */
98
 
        char            *discov_requestor;      /* discovery requestor unique
99
 
                                                 * name */
100
 
        guint           discov_listener;
101
122
        guint           discov_timer;
 
123
        struct browse_req *browse;              /* service discover request */
102
124
        struct bonding_req *bonding;
103
125
        struct authentication_req *authr;       /* authentication request */
 
126
        GSList          *disconnects;           /* disconnects message */
104
127
 
105
128
        /* For Secure Simple Pairing */
106
129
        uint8_t         cap;
113
136
 
114
137
        sdp_list_t      *tmp_records;
115
138
 
 
139
        gboolean        trusted;
 
140
        gboolean        paired;
 
141
        gboolean        blocked;
116
142
        gboolean        renewed_key;
117
 
};
118
 
 
119
 
struct browse_req {
120
 
        DBusConnection *conn;
121
 
        DBusMessage *msg;
122
 
        struct btd_device *device;
123
 
        GSList *match_uuids;
124
 
        GSList *profiles_added;
125
 
        GSList *profiles_removed;
126
 
        sdp_list_t *records;
127
 
        int search_uuid;
128
 
        int reconnect_attempt;
 
143
 
 
144
        gboolean        authorizing;
 
145
        gint            ref;
 
146
 
 
147
        gboolean        has_debug_key;
 
148
        uint8_t         debug_key[16];
129
149
};
130
150
 
131
151
static uint16_t uuid_list[] = {
137
157
 
138
158
static GSList *device_drivers = NULL;
139
159
 
140
 
static DBusHandlerResult error_connection_attempt_failed(DBusConnection *conn,
141
 
                                                DBusMessage *msg, int err)
142
 
{
143
 
        return error_common_reply(conn, msg,
144
 
                        ERROR_INTERFACE ".ConnectionAttemptFailed",
145
 
                        err > 0 ? strerror(err) : "Connection attempt failed");
146
 
}
147
 
 
148
160
static DBusHandlerResult error_failed(DBusConnection *conn,
149
161
                                        DBusMessage *msg, const char * desc)
150
162
{
167
179
 
168
180
static inline DBusMessage *in_progress(DBusMessage *msg, const char *str)
169
181
{
170
 
        return g_dbus_create_error(msg, ERROR_INTERFACE ".InProgress", str);
 
182
        return g_dbus_create_error(msg, ERROR_INTERFACE ".InProgress",
 
183
                                                                "%s", str);
 
184
}
 
185
 
 
186
static void browse_request_free(struct browse_req *req)
 
187
{
 
188
        if (req->listener_id)
 
189
                g_dbus_remove_watch(req->conn, req->listener_id);
 
190
        if (req->msg)
 
191
                dbus_message_unref(req->msg);
 
192
        if (req->conn)
 
193
                dbus_connection_unref(req->conn);
 
194
        if (req->device)
 
195
                btd_device_unref(req->device);
 
196
        g_slist_foreach(req->profiles_added, (GFunc) g_free, NULL);
 
197
        g_slist_free(req->profiles_added);
 
198
        g_slist_free(req->profiles_removed);
 
199
        if (req->records)
 
200
                sdp_list_free(req->records, (sdp_free_func_t) sdp_record_free);
 
201
        g_free(req);
 
202
}
 
203
 
 
204
static void browse_request_cancel(struct browse_req *req)
 
205
{
 
206
        struct btd_device *device = req->device;
 
207
        struct btd_adapter *adapter = device->adapter;
 
208
        bdaddr_t src;
 
209
 
 
210
        if (device_is_creating(device, NULL))
 
211
                device_set_temporary(device, TRUE);
 
212
 
 
213
        adapter_get_address(adapter, &src);
 
214
 
 
215
        if (device->le == FALSE)
 
216
                bt_cancel_discovery(&src, &device->bdaddr);
 
217
 
 
218
        device->browse = NULL;
 
219
        browse_request_free(req);
171
220
}
172
221
 
173
222
static void device_free(gpointer user_data)
177
226
        struct agent *agent = adapter_get_agent(adapter);
178
227
 
179
228
        if (device->agent)
180
 
                agent_destroy(device->agent, FALSE);
 
229
                agent_free(device->agent);
181
230
 
182
 
        if (agent && agent_is_busy(agent, device))
 
231
        if (agent && (agent_is_busy(agent, device) ||
 
232
                                agent_is_busy(agent, device->authr)))
183
233
                agent_cancel(agent);
184
234
 
 
235
        g_slist_foreach(device->services, (GFunc) g_free, NULL);
 
236
        g_slist_free(device->services);
 
237
 
185
238
        g_slist_foreach(device->uuids, (GFunc) g_free, NULL);
186
239
        g_slist_free(device->uuids);
187
240
 
 
241
        if (device->tmp_records)
 
242
                sdp_list_free(device->tmp_records,
 
243
                                        (sdp_free_func_t) sdp_record_free);
 
244
 
188
245
        if (device->disconn_timer)
189
246
                g_source_remove(device->disconn_timer);
190
247
 
191
248
        if (device->discov_timer)
192
249
                g_source_remove(device->discov_timer);
193
250
 
 
251
        DBG("%p", device);
 
252
 
 
253
        g_free(device->authr);
194
254
        g_free(device->path);
 
255
        g_free(device->alias);
195
256
        g_free(device);
196
257
}
197
258
 
198
259
gboolean device_is_paired(struct btd_device *device)
199
260
{
200
 
        struct btd_adapter *adapter = device->adapter;
201
 
        char filename[PATH_MAX + 1], *str;
202
 
        char srcaddr[18], dstaddr[18];
203
 
        gboolean ret;
204
 
        bdaddr_t src;
205
 
 
206
 
        adapter_get_address(adapter, &src);
207
 
        ba2str(&src, srcaddr);
208
 
        ba2str(&device->bdaddr, dstaddr);
209
 
 
210
 
        create_name(filename, PATH_MAX, STORAGEDIR,
211
 
                        srcaddr, "linkkeys");
212
 
        str = textfile_caseget(filename, dstaddr);
213
 
        ret = str ? TRUE : FALSE;
214
 
        g_free(str);
215
 
 
216
 
        return ret;
 
261
        return device->paired;
 
262
}
 
263
 
 
264
gboolean device_is_trusted(struct btd_device *device)
 
265
{
 
266
        return device->trusted;
217
267
}
218
268
 
219
269
static DBusMessage *get_properties(DBusConnection *conn,
225
275
        DBusMessageIter iter;
226
276
        DBusMessageIter dict;
227
277
        bdaddr_t src;
228
 
        char name[248], srcaddr[18], dstaddr[18];
229
 
        char **uuids;
 
278
        char name[MAX_NAME_LENGTH + 1], srcaddr[18], dstaddr[18];
 
279
        char **str;
230
280
        const char *ptr;
231
281
        dbus_bool_t boolean;
232
282
        uint32_t class;
256
306
        adapter_get_address(adapter, &src);
257
307
        ba2str(&src, srcaddr);
258
308
 
259
 
        if (device->name) {
260
 
                ptr = device->name;
261
 
                dict_append_entry(&dict, "Name", DBUS_TYPE_STRING, &ptr);
262
 
        }
 
309
        ptr = device->name;
 
310
        dict_append_entry(&dict, "Name", DBUS_TYPE_STRING, &ptr);
263
311
 
264
312
        /* Alias (fallback to name or address) */
265
 
        if (read_device_alias(srcaddr, dstaddr, name, sizeof(name)) < 1) {
266
 
                if (!ptr) {
267
 
                        g_strdelimit(dstaddr, ":", '-');
268
 
                        ptr = dstaddr;
269
 
                }
270
 
        } else
271
 
                ptr = name;
 
313
        if (device->alias != NULL)
 
314
                ptr = device->alias;
 
315
        else if (strlen(ptr) == 0) {
 
316
                g_strdelimit(dstaddr, ":", '-');
 
317
                ptr = dstaddr;
 
318
        }
272
319
 
273
 
        if (ptr)
274
 
                dict_append_entry(&dict, "Alias", DBUS_TYPE_STRING, &ptr);
 
320
        dict_append_entry(&dict, "Alias", DBUS_TYPE_STRING, &ptr);
275
321
 
276
322
        /* Class */
277
323
        if (read_remote_class(&src, &device->bdaddr, &class) == 0) {
289
335
        dict_append_entry(&dict, "Paired", DBUS_TYPE_BOOLEAN, &boolean);
290
336
 
291
337
        /* Trusted */
292
 
        boolean = read_trust(&src, dstaddr, GLOBAL_TRUST);
 
338
        boolean = device_is_trusted(device);
293
339
        dict_append_entry(&dict, "Trusted", DBUS_TYPE_BOOLEAN, &boolean);
294
340
 
 
341
        /* Blocked */
 
342
        boolean = device->blocked;
 
343
        dict_append_entry(&dict, "Blocked", DBUS_TYPE_BOOLEAN, &boolean);
 
344
 
295
345
        /* Connected */
296
346
        boolean = (device->handle != 0);
297
347
        dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN,
298
348
                                &boolean);
299
349
 
300
350
        /* UUIDs */
301
 
        uuids = g_new0(char *, g_slist_length(device->uuids) + 1);
 
351
        str = g_new0(char *, g_slist_length(device->uuids) + 1);
302
352
        for (i = 0, l = device->uuids; l; l = l->next, i++)
303
 
                uuids[i] = l->data;
304
 
        dict_append_array(&dict, "UUIDs", DBUS_TYPE_STRING, &uuids, i);
305
 
        g_free(uuids);
 
353
                str[i] = l->data;
 
354
        dict_append_array(&dict, "UUIDs", DBUS_TYPE_STRING, &str, i);
 
355
        g_free(str);
 
356
 
 
357
        /* Services */
 
358
        str = g_new0(char *, g_slist_length(device->services) + 1);
 
359
        for (i = 0, l = device->services; l; l = l->next, i++)
 
360
                str[i] = l->data;
 
361
        dict_append_array(&dict, "Services", DBUS_TYPE_OBJECT_PATH, &str, i);
 
362
        g_free(str);
306
363
 
307
364
        /* Adapter */
308
365
        ptr = adapter_get_path(adapter);
322
379
        bdaddr_t src;
323
380
        int err;
324
381
 
 
382
        /* No change */
 
383
        if ((device->alias == NULL && g_str_equal(alias, "")) ||
 
384
                        g_strcmp0(device->alias, alias) == 0)
 
385
                return dbus_message_new_method_return(msg);
 
386
 
325
387
        adapter_get_address(adapter, &src);
326
388
        ba2str(&src, srcaddr);
327
389
        ba2str(&device->bdaddr, dstaddr);
332
394
        if (err < 0)
333
395
                return g_dbus_create_error(msg,
334
396
                                ERROR_INTERFACE ".Failed",
335
 
                                strerror(-err));
 
397
                                "%s", strerror(-err));
 
398
 
 
399
        g_free(device->alias);
 
400
        device->alias = g_str_equal(alias, "") ? NULL : g_strdup(alias);
336
401
 
337
402
        emit_property_changed(conn, dbus_message_get_path(msg),
338
403
                                DEVICE_INTERFACE, "Alias",
342
407
}
343
408
 
344
409
static DBusMessage *set_trust(DBusConnection *conn, DBusMessage *msg,
345
 
                                        dbus_bool_t value, void *data)
 
410
                                        gboolean value, void *data)
346
411
{
347
412
        struct btd_device *device = data;
348
413
        struct btd_adapter *adapter = device->adapter;
349
414
        char srcaddr[18], dstaddr[18];
350
415
        bdaddr_t src;
 
416
        int err;
 
417
 
 
418
        if (device->trusted == value)
 
419
                return dbus_message_new_method_return(msg);
351
420
 
352
421
        adapter_get_address(adapter, &src);
353
422
        ba2str(&src, srcaddr);
354
423
        ba2str(&device->bdaddr, dstaddr);
355
424
 
356
 
        write_trust(srcaddr, dstaddr, GLOBAL_TRUST, value);
 
425
        err = write_trust(srcaddr, dstaddr, GLOBAL_TRUST, value);
 
426
        if (err < 0)
 
427
                return g_dbus_create_error(msg,
 
428
                                ERROR_INTERFACE ".Failed",
 
429
                                "%s", strerror(-err));
 
430
 
 
431
        device->trusted = value;
357
432
 
358
433
        emit_property_changed(conn, dbus_message_get_path(msg),
359
434
                                DEVICE_INTERFACE, "Trusted",
362
437
        return dbus_message_new_method_return(msg);
363
438
}
364
439
 
 
440
static void driver_remove(struct btd_driver_data *driver_data,
 
441
                                                struct btd_device *device)
 
442
{
 
443
        struct btd_device_driver *driver = driver_data->driver;
 
444
 
 
445
        driver->remove(device);
 
446
 
 
447
        device->drivers = g_slist_remove(device->drivers, driver_data);
 
448
        g_free(driver_data);
 
449
}
 
450
 
 
451
static gboolean do_disconnect(gpointer user_data)
 
452
{
 
453
        struct btd_device *device = user_data;
 
454
 
 
455
        device->disconn_timer = 0;
 
456
 
 
457
        btd_adapter_disconnect_device(device->adapter, device->handle);
 
458
 
 
459
        return FALSE;
 
460
}
 
461
 
 
462
static int device_block(DBusConnection *conn, struct btd_device *device)
 
463
{
 
464
        int err;
 
465
        bdaddr_t src;
 
466
 
 
467
        if (device->blocked)
 
468
                return 0;
 
469
 
 
470
        if (device->handle)
 
471
                do_disconnect(device);
 
472
 
 
473
        g_slist_foreach(device->drivers, (GFunc) driver_remove, device);
 
474
 
 
475
        err = btd_adapter_block_address(device->adapter, &device->bdaddr);
 
476
        if (err < 0)
 
477
                return err;
 
478
 
 
479
        device->blocked = TRUE;
 
480
 
 
481
        adapter_get_address(device->adapter, &src);
 
482
 
 
483
        err = write_blocked(&src, &device->bdaddr, TRUE);
 
484
        if (err < 0)
 
485
                error("write_blocked(): %s (%d)", strerror(-err), -err);
 
486
 
 
487
        device_set_temporary(device, FALSE);
 
488
 
 
489
        emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Blocked",
 
490
                                        DBUS_TYPE_BOOLEAN, &device->blocked);
 
491
 
 
492
        return 0;
 
493
}
 
494
 
 
495
static int device_unblock(DBusConnection *conn, struct btd_device *device,
 
496
                                                        gboolean silent)
 
497
{
 
498
        int err;
 
499
        bdaddr_t src;
 
500
 
 
501
        if (!device->blocked)
 
502
                return 0;
 
503
 
 
504
        err = btd_adapter_unblock_address(device->adapter, &device->bdaddr);
 
505
        if (err < 0)
 
506
                return err;
 
507
 
 
508
        device->blocked = FALSE;
 
509
 
 
510
        adapter_get_address(device->adapter, &src);
 
511
 
 
512
        err = write_blocked(&src, &device->bdaddr, FALSE);
 
513
        if (err < 0)
 
514
                error("write_blocked(): %s (%d)", strerror(-err), -err);
 
515
 
 
516
        if (!silent) {
 
517
                emit_property_changed(conn, device->path,
 
518
                                        DEVICE_INTERFACE, "Blocked",
 
519
                                        DBUS_TYPE_BOOLEAN, &device->blocked);
 
520
                device_probe_drivers(device, device->uuids);
 
521
        }
 
522
 
 
523
        return 0;
 
524
}
 
525
 
 
526
static DBusMessage *set_blocked(DBusConnection *conn, DBusMessage *msg,
 
527
                                                gboolean value, void *data)
 
528
{
 
529
        struct btd_device *device = data;
 
530
        int err;
 
531
 
 
532
        if (value)
 
533
                err = device_block(conn, device);
 
534
        else
 
535
                err = device_unblock(conn, device, FALSE);
 
536
 
 
537
        switch (-err) {
 
538
        case 0:
 
539
                return dbus_message_new_method_return(msg);
 
540
        case EINVAL:
 
541
                return g_dbus_create_error(msg,
 
542
                                        ERROR_INTERFACE ".NotSupported",
 
543
                                        "Kernel lacks blacklist support");
 
544
        default:
 
545
                return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
 
546
                                                "%s", strerror(-err));
 
547
        }
 
548
}
 
549
 
365
550
static inline DBusMessage *invalid_args(DBusMessage *msg)
366
551
{
367
 
        return g_dbus_create_error(msg,
368
 
                        ERROR_INTERFACE ".InvalidArguments",
369
 
                        "Invalid arguments in method call");
 
552
        return g_dbus_create_error(msg, ERROR_INTERFACE ".InvalidArguments",
 
553
                                        "Invalid arguments in method call");
370
554
}
371
555
 
372
556
static DBusMessage *set_property(DBusConnection *conn,
391
575
 
392
576
        if (g_str_equal("Trusted", property)) {
393
577
                dbus_bool_t value;
394
 
 
395
578
                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
396
579
                        return invalid_args(msg);
397
580
                dbus_message_iter_get_basic(&sub, &value);
405
588
                dbus_message_iter_get_basic(&sub, &alias);
406
589
 
407
590
                return set_alias(conn, msg, alias, data);
 
591
        } else if (g_str_equal("Blocked", property)) {
 
592
                dbus_bool_t value;
 
593
 
 
594
                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
 
595
                        return invalid_args(msg);
 
596
 
 
597
                dbus_message_iter_get_basic(&sub, &value);
 
598
 
 
599
                return set_blocked(conn, msg, value, data);
408
600
        }
409
601
 
410
602
        return invalid_args(msg);
412
604
 
413
605
static void discover_services_req_exit(DBusConnection *conn, void *user_data)
414
606
{
415
 
        struct btd_device *device = user_data;
416
 
        struct btd_adapter *adapter = device->adapter;
417
 
        bdaddr_t src;
418
 
 
419
 
        adapter_get_address(adapter, &src);
420
 
 
421
 
        debug("DiscoverDevices requestor exited");
422
 
 
423
 
        bt_cancel_discovery(&src, &device->bdaddr);
 
607
        struct browse_req *req = user_data;
 
608
 
 
609
        DBG("DiscoverServices requestor exited");
 
610
 
 
611
        browse_request_cancel(req);
424
612
}
425
613
 
426
614
static DBusMessage *discover_services(DBusConnection *conn,
430
618
        const char *pattern;
431
619
        int err;
432
620
 
433
 
        if (device->discov_active)
 
621
        if (device->browse)
434
622
                return g_dbus_create_error(msg, ERROR_INTERFACE ".InProgress",
435
623
                                                "Discover in progress");
436
624
 
462
650
                                        "Discovery Failed");
463
651
}
464
652
 
 
653
static const char *browse_request_get_requestor(struct browse_req *req)
 
654
{
 
655
        if (!req->msg)
 
656
                return NULL;
 
657
 
 
658
        return dbus_message_get_sender(req->msg);
 
659
}
 
660
 
 
661
static void iter_append_record(DBusMessageIter *dict, uint32_t handle,
 
662
                                                        const char *record)
 
663
{
 
664
        DBusMessageIter entry;
 
665
 
 
666
        dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
 
667
                                                        NULL, &entry);
 
668
 
 
669
        dbus_message_iter_append_basic(&entry, DBUS_TYPE_UINT32, &handle);
 
670
 
 
671
        dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &record);
 
672
 
 
673
        dbus_message_iter_close_container(dict, &entry);
 
674
}
 
675
 
 
676
static void discover_services_reply(struct browse_req *req, int err,
 
677
                                                        sdp_list_t *recs)
 
678
{
 
679
        DBusMessage *reply;
 
680
        DBusMessageIter iter, dict;
 
681
        sdp_list_t *seq;
 
682
 
 
683
        if (err) {
 
684
                const char *err_if;
 
685
 
 
686
                if (err == -EHOSTDOWN)
 
687
                        err_if = ERROR_INTERFACE ".ConnectionAttemptFailed";
 
688
                else
 
689
                        err_if = ERROR_INTERFACE ".Failed";
 
690
 
 
691
                reply = dbus_message_new_error(req->msg, err_if,
 
692
                                                        strerror(-err));
 
693
                g_dbus_send_message(req->conn, reply);
 
694
                return;
 
695
        }
 
696
 
 
697
        reply = dbus_message_new_method_return(req->msg);
 
698
        if (!reply)
 
699
                return;
 
700
 
 
701
        dbus_message_iter_init_append(reply, &iter);
 
702
 
 
703
        dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
 
704
                        DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
 
705
                        DBUS_TYPE_UINT32_AS_STRING DBUS_TYPE_STRING_AS_STRING
 
706
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
707
 
 
708
        for (seq = recs; seq; seq = seq->next) {
 
709
                sdp_record_t *rec = (sdp_record_t *) seq->data;
 
710
                GString *result;
 
711
 
 
712
                if (!rec)
 
713
                        break;
 
714
 
 
715
                result = g_string_new(NULL);
 
716
 
 
717
                convert_sdp_record_to_xml(rec, result,
 
718
                                (void *) g_string_append);
 
719
 
 
720
                if (result->len)
 
721
                        iter_append_record(&dict, rec->handle, result->str);
 
722
 
 
723
                g_string_free(result, TRUE);
 
724
        }
 
725
 
 
726
        dbus_message_iter_close_container(&iter, &dict);
 
727
 
 
728
        g_dbus_send_message(req->conn, reply);
 
729
}
 
730
 
465
731
static DBusMessage *cancel_discover(DBusConnection *conn,
466
732
                                        DBusMessage *msg, void *user_data)
467
733
{
468
734
        struct btd_device *device = user_data;
469
 
        struct btd_adapter *adapter = device->adapter;
470
735
        const char *sender = dbus_message_get_sender(msg);
471
 
        bdaddr_t src;
472
 
 
473
 
        adapter_get_address(adapter, &src);
474
 
 
475
 
        if (!device->discov_active)
 
736
        const char *requestor;
 
737
 
 
738
        if (!device->browse)
476
739
                return g_dbus_create_error(msg,
477
740
                                ERROR_INTERFACE ".Failed",
478
741
                                "No pending discovery");
479
742
 
 
743
        if (!dbus_message_is_method_call(device->browse->msg, DEVICE_INTERFACE,
 
744
                                        "DiscoverServices"))
 
745
                return g_dbus_create_error(msg,
 
746
                                ERROR_INTERFACE ".NotAuthorized",
 
747
                                "Not Authorized");
 
748
 
 
749
        requestor = browse_request_get_requestor(device->browse);
 
750
 
480
751
        /* only the discover requestor can cancel the inquiry process */
481
 
        if (!device->discov_requestor ||
482
 
                                !g_str_equal(device->discov_requestor, sender))
 
752
        if (!requestor || !g_str_equal(requestor, sender))
483
753
                return g_dbus_create_error(msg,
484
754
                                ERROR_INTERFACE ".NotAuthorized",
485
755
                                "Not Authorized");
486
756
 
487
 
        if (bt_cancel_discovery(&src, &device->bdaddr) < 0)
488
 
                return g_dbus_create_error(msg,
489
 
                                ERROR_INTERFACE ".Failed",
490
 
                                "No pending discover");
 
757
        discover_services_reply(device->browse, -ECANCELED, NULL);
 
758
 
 
759
        browse_request_cancel(device->browse);
491
760
 
492
761
        return dbus_message_new_method_return(msg);
493
762
}
494
763
 
495
 
static gboolean disconnect_timeout(gpointer user_data)
496
 
{
497
 
        struct btd_device *device = user_data;
498
 
        disconnect_cp cp;
499
 
        int dd;
500
 
        uint16_t dev_id = adapter_get_dev_id(device->adapter);
501
 
 
502
 
        device->disconn_timer = 0;
503
 
 
504
 
        dd = hci_open_dev(dev_id);
505
 
        if (dd < 0)
506
 
                goto fail;
507
 
 
508
 
        memset(&cp, 0, sizeof(cp));
509
 
        cp.handle = htobs(device->handle);
510
 
        cp.reason = HCI_OE_USER_ENDED_CONNECTION;
511
 
 
512
 
        hci_send_cmd(dd, OGF_LINK_CTL, OCF_DISCONNECT,
513
 
                        DISCONNECT_CP_SIZE, &cp);
514
 
 
515
 
        close(dd);
516
 
 
517
 
fail:
518
 
        return FALSE;
 
764
static void bonding_request_cancel(struct bonding_req *bonding)
 
765
{
 
766
        if (!bonding->io)
 
767
                return;
 
768
 
 
769
        g_io_channel_shutdown(bonding->io, TRUE, NULL);
 
770
        g_io_channel_unref(bonding->io);
 
771
        bonding->io = NULL;
 
772
}
 
773
 
 
774
void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
 
775
{
 
776
        DBusConnection *conn = get_dbus_connection();
 
777
 
 
778
        if (device->bonding)
 
779
                bonding_request_cancel(device->bonding);
 
780
 
 
781
        if (device->browse)
 
782
                browse_request_cancel(device->browse);
 
783
 
 
784
        if (msg)
 
785
                device->disconnects = g_slist_append(device->disconnects,
 
786
                                                dbus_message_ref(msg));
 
787
 
 
788
        if (device->disconn_timer)
 
789
                return;
 
790
 
 
791
        while (device->watches) {
 
792
                struct btd_disconnect_data *data = device->watches->data;
 
793
 
 
794
                if (data->watch)
 
795
                        /* temporary is set if device is going to be removed */
 
796
                        data->watch(device, device->temporary,
 
797
                                                        data->user_data);
 
798
 
 
799
                /* Check if the watch has been removed by callback function */
 
800
                if (!g_slist_find(device->watches, data))
 
801
                        continue;
 
802
 
 
803
                device->watches = g_slist_remove(device->watches, data);
 
804
                g_free(data);
 
805
        }
 
806
 
 
807
        device->disconn_timer = g_timeout_add_seconds(DISCONNECT_TIMER,
 
808
                                                do_disconnect, device);
 
809
 
 
810
        g_dbus_emit_signal(conn, device->path,
 
811
                        DEVICE_INTERFACE, "DisconnectRequested",
 
812
                        DBUS_TYPE_INVALID);
519
813
}
520
814
 
521
815
static DBusMessage *disconnect(DBusConnection *conn, DBusMessage *msg,
528
822
                                ERROR_INTERFACE ".NotConnected",
529
823
                                "Device is not connected");
530
824
 
531
 
        g_dbus_emit_signal(conn, device->path,
532
 
                        DEVICE_INTERFACE, "DisconnectRequested",
533
 
                        DBUS_TYPE_INVALID);
534
 
 
535
 
        device->disconn_timer = g_timeout_add_seconds(DISCONNECT_TIMER,
536
 
                                                disconnect_timeout, device);
537
 
 
538
 
        return dbus_message_new_method_return(msg);
 
825
        device_request_disconnect(device, msg);
 
826
 
 
827
        return NULL;
539
828
}
540
829
 
541
830
static GDBusMethodTable device_methods[] = {
544
833
        { "DiscoverServices",   "s",    "a{us}",        discover_services,
545
834
                                                G_DBUS_METHOD_FLAG_ASYNC},
546
835
        { "CancelDiscovery",    "",     "",             cancel_discover },
547
 
        { "Disconnect",         "",     "",             disconnect      },
 
836
        { "Disconnect",         "",     "",             disconnect,
 
837
                                                G_DBUS_METHOD_FLAG_ASYNC},
548
838
        { }
549
839
};
550
840
 
574
864
 
575
865
                device->secmode3 = FALSE;
576
866
 
577
 
                hcid_dbus_bonding_process_complete(&sba, &device->bdaddr, 0);
 
867
                btd_event_bonding_process_complete(&sba, &device->bdaddr, 0);
578
868
        }
579
869
}
580
870
 
582
872
                                uint16_t handle)
583
873
{
584
874
        if (device->handle) {
585
 
                error("%s: Unable to add connection %u, %u already exist)",
586
 
                        device->path, handle, device->handle);
 
875
                char addr[18];
 
876
                ba2str(&device->bdaddr, addr);
 
877
                error("%s: Unable to add connection %u, %u already exist",
 
878
                        addr, handle, device->handle);
587
879
                return;
588
880
        }
589
881
 
595
887
void device_remove_connection(struct btd_device *device, DBusConnection *conn,
596
888
                                uint16_t handle)
597
889
{
598
 
        if (device->handle != handle) {
599
 
                error("%s: Unable to remove connection %u, handle mismatch (%u)",
600
 
                        device->path, handle, device->handle);
 
890
        if (handle && device->handle != handle) {
 
891
                char addr[18];
 
892
                ba2str(&device->bdaddr, addr);
 
893
                error("%s: connection handle mismatch %u != %u",
 
894
                                                addr, handle, device->handle);
601
895
                return;
602
896
        }
603
897
 
604
898
        device->handle = 0;
605
899
 
 
900
        if (device->disconn_timer > 0) {
 
901
                g_source_remove(device->disconn_timer);
 
902
                device->disconn_timer = 0;
 
903
        }
 
904
 
 
905
        while (device->disconnects) {
 
906
                DBusMessage *msg = device->disconnects->data;
 
907
 
 
908
                g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
 
909
                device->disconnects = g_slist_remove(device->disconnects, msg);
 
910
        }
 
911
 
606
912
        device_set_connected(device, conn, FALSE);
607
913
}
608
914
 
611
917
        return (handle == device->handle);
612
918
}
613
919
 
 
920
guint device_add_disconnect_watch(struct btd_device *device,
 
921
                                disconnect_watch watch, void *user_data,
 
922
                                GDestroyNotify destroy)
 
923
{
 
924
        struct btd_disconnect_data *data;
 
925
        static guint id = 0;
 
926
 
 
927
        data = g_new0(struct btd_disconnect_data, 1);
 
928
        data->id = ++id;
 
929
        data->watch = watch;
 
930
        data->user_data = user_data;
 
931
        data->destroy = destroy;
 
932
 
 
933
        device->watches = g_slist_append(device->watches, data);
 
934
 
 
935
        return data->id;
 
936
}
 
937
 
 
938
void device_remove_disconnect_watch(struct btd_device *device, guint id)
 
939
{
 
940
        GSList *l;
 
941
 
 
942
        for (l = device->watches; l; l = l->next) {
 
943
                struct btd_disconnect_data *data = l->data;
 
944
 
 
945
                if (data->id == id) {
 
946
                        device->watches = g_slist_remove(device->watches,
 
947
                                                        data);
 
948
                        if (data->destroy)
 
949
                                data->destroy(data->user_data);
 
950
                        g_free(data);
 
951
                        return;
 
952
                }
 
953
        }
 
954
}
 
955
 
 
956
gboolean device_get_secmode3_conn(struct btd_device *device)
 
957
{
 
958
        return device->secmode3;
 
959
}
 
960
 
614
961
void device_set_secmode3_conn(struct btd_device *device, gboolean enable)
615
962
{
616
963
        device->secmode3 = enable;
618
965
 
619
966
struct btd_device *device_create(DBusConnection *conn,
620
967
                                        struct btd_adapter *adapter,
621
 
                                        const gchar *address)
 
968
                                        const gchar *address, gboolean le)
622
969
{
623
970
        gchar *address_up;
624
971
        struct btd_device *device;
625
972
        const gchar *adapter_path = adapter_get_path(adapter);
626
973
        bdaddr_t src;
627
 
        char srcaddr[18];
 
974
        char srcaddr[18], alias[MAX_NAME_LENGTH + 1];
628
975
 
629
976
        device = g_try_malloc0(sizeof(struct btd_device));
630
977
        if (device == NULL)
635
982
        g_strdelimit(device->path, ":", '_');
636
983
        g_free(address_up);
637
984
 
638
 
        debug("Creating device %s", device->path);
 
985
        DBG("Creating device %s", device->path);
639
986
 
640
987
        if (g_dbus_register_interface(conn, device->path, DEVICE_INTERFACE,
641
988
                                device_methods, device_signals, NULL,
646
993
 
647
994
        str2ba(address, &device->bdaddr);
648
995
        device->adapter = adapter;
 
996
        device->le = le;
649
997
        adapter_get_address(adapter, &src);
650
998
        ba2str(&src, srcaddr);
651
999
        read_device_name(srcaddr, address, device->name);
 
1000
        if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
 
1001
                device->alias = g_strdup(alias);
 
1002
        device->trusted = read_trust(&src, address, GLOBAL_TRUST);
 
1003
 
 
1004
        if (read_blocked(&src, &device->bdaddr))
 
1005
                device_block(conn, device);
652
1006
 
653
1007
        device->auth = 0xff;
654
1008
 
655
 
        return device;
 
1009
        if (read_link_key(&src, &device->bdaddr, NULL, NULL) == 0)
 
1010
                device->paired = TRUE;
 
1011
 
 
1012
        return btd_device_ref(device);
656
1013
}
657
1014
 
658
1015
void device_set_name(struct btd_device *device, const char *name)
659
1016
{
660
1017
        DBusConnection *conn = get_dbus_connection();
661
 
        char alias[248];
662
 
        char srcaddr[18], dstaddr[18];
663
 
        bdaddr_t src;
664
1018
 
665
 
        if (strncmp(name, device->name, 248) == 0)
 
1019
        if (strncmp(name, device->name, MAX_NAME_LENGTH) == 0)
666
1020
                return;
667
1021
 
668
 
        strncpy(device->name, name, 248);
 
1022
        strncpy(device->name, name, MAX_NAME_LENGTH);
669
1023
 
670
1024
        emit_property_changed(conn, device->path,
671
1025
                                DEVICE_INTERFACE, "Name",
672
1026
                                DBUS_TYPE_STRING, &name);
673
1027
 
674
 
        adapter_get_address(device->adapter, &src);
675
 
        ba2str(&src, srcaddr);
676
 
        ba2str(&device->bdaddr, dstaddr);
677
 
 
678
 
        if (read_device_alias(srcaddr, dstaddr, alias, sizeof(alias)) == 0)
 
1028
        if (device->alias != NULL)
679
1029
                return;
680
1030
 
681
1031
        emit_property_changed(conn, device->path,
683
1033
                                DBUS_TYPE_STRING, &name);
684
1034
}
685
1035
 
686
 
static void device_remove_bonding(struct btd_device *device,
687
 
                                                        DBusConnection *conn)
 
1036
void device_get_name(struct btd_device *device, char *name, size_t len)
 
1037
{
 
1038
        strncpy(name, device->name, len);
 
1039
}
 
1040
 
 
1041
void device_remove_bonding(struct btd_device *device)
688
1042
{
689
1043
        char filename[PATH_MAX + 1];
690
 
        char *str, srcaddr[18], dstaddr[18];
691
 
        int dd, dev_id;
 
1044
        char srcaddr[18], dstaddr[18];
692
1045
        bdaddr_t bdaddr;
693
 
        gboolean paired;
694
1046
 
695
1047
        adapter_get_address(device->adapter, &bdaddr);
696
1048
        ba2str(&bdaddr, srcaddr);
697
1049
        ba2str(&device->bdaddr, dstaddr);
698
1050
 
699
 
        dev_id = adapter_get_dev_id(device->adapter);
700
 
 
701
 
        dd = hci_open_dev(dev_id);
702
 
        if (dd < 0)
703
 
                return;
704
 
 
705
1051
        create_name(filename, PATH_MAX, STORAGEDIR, srcaddr,
706
1052
                        "linkkeys");
707
1053
 
708
 
        /* textfile_del doesn't return an error when the key is not found */
709
 
        str = textfile_caseget(filename, dstaddr);
710
 
        paired = str ? TRUE : FALSE;
711
 
        g_free(str);
712
 
 
713
 
        if (!paired)
714
 
                return;
715
 
 
716
1054
        /* Delete the link key from storage */
717
1055
        textfile_casedel(filename, dstaddr);
718
1056
 
719
 
        /* Delete the link key from the Bluetooth chip */
720
 
        hci_delete_stored_link_key(dd, &device->bdaddr, 0, HCI_REQ_TIMEOUT);
721
 
 
722
 
        /* Send the HCI disconnect command */
723
 
        if (device->handle) {
724
 
                int err = hci_disconnect(dd, htobs(device->handle),
725
 
                                        HCI_OE_USER_ENDED_CONNECTION,
726
 
                                        HCI_REQ_TIMEOUT);
727
 
                if (err < 0)
728
 
                        error("Disconnect: %s (%d)", strerror(-err), -err);
729
 
        }
730
 
 
731
 
        hci_close_dev(dd);
732
 
 
733
 
        paired = FALSE;
734
 
        emit_property_changed(conn, device->path, DEVICE_INTERFACE,
735
 
                                "Paired", DBUS_TYPE_BOOLEAN, &paired);
 
1057
        btd_adapter_remove_bonding(device->adapter, &device->bdaddr);
736
1058
}
737
1059
 
738
 
static void device_remove_stored(struct btd_device *device,
739
 
                                        DBusConnection *conn)
 
1060
static void device_remove_stored(struct btd_device *device)
740
1061
{
741
1062
        bdaddr_t src;
742
1063
        char addr[18];
 
1064
        DBusConnection *conn = get_dbus_connection();
743
1065
 
744
1066
        adapter_get_address(device->adapter, &src);
745
1067
        ba2str(&device->bdaddr, addr);
746
 
        device_remove_bonding(device, conn);
 
1068
 
 
1069
        if (device->paired)
 
1070
                device_remove_bonding(device);
747
1071
        delete_entry(&src, "profiles", addr);
748
1072
        delete_entry(&src, "trusts", addr);
 
1073
        delete_all_records(&src, &device->bdaddr);
 
1074
        delete_device_service(&src, &device->bdaddr);
 
1075
 
 
1076
        if (device->blocked)
 
1077
                device_unblock(conn, device, TRUE);
749
1078
}
750
1079
 
751
 
void device_remove(struct btd_device *device, DBusConnection *conn,
752
 
                                                gboolean remove_stored)
 
1080
void device_remove(struct btd_device *device, gboolean remove_stored)
753
1081
{
754
 
        GSList *list;
755
 
        struct btd_device_driver *driver;
756
 
        gchar *path = g_strdup(device->path);
757
 
 
758
 
        debug("Removing device %s", path);
 
1082
 
 
1083
        DBG("Removing device %s", device->path);
 
1084
 
 
1085
        if (device->agent)
 
1086
                agent_free(device->agent);
759
1087
 
760
1088
        if (device->bonding)
761
1089
                device_cancel_bonding(device, HCI_OE_USER_ENDED_CONNECTION);
762
1090
 
763
 
        if (!device->temporary && remove_stored)
764
 
                device_remove_stored(device, conn);
765
 
 
766
 
        for (list = device->drivers; list; list = list->next) {
767
 
                struct btd_driver_data *driver_data = list->data;
768
 
                driver = driver_data->driver;
769
 
 
770
 
                driver->remove(device);
771
 
                g_free(driver_data);
772
 
        }
773
 
 
774
 
 
775
 
        g_dbus_unregister_interface(conn, path, DEVICE_INTERFACE);
776
 
 
777
 
        g_free(path);
 
1091
        if (device->browse)
 
1092
                browse_request_cancel(device->browse);
 
1093
 
 
1094
        if (device->handle)
 
1095
                do_disconnect(device);
 
1096
 
 
1097
        if (remove_stored)
 
1098
                device_remove_stored(device);
 
1099
 
 
1100
        g_slist_foreach(device->drivers, (GFunc) driver_remove, device);
 
1101
        g_slist_free(device->drivers);
 
1102
        device->drivers = NULL;
 
1103
 
 
1104
        btd_device_unref(device);
778
1105
}
779
1106
 
780
1107
gint device_address_cmp(struct btd_device *device, const gchar *address)
865
1192
void device_probe_drivers(struct btd_device *device, GSList *profiles)
866
1193
{
867
1194
        GSList *list;
 
1195
        char addr[18];
868
1196
        int err;
869
1197
 
870
 
        debug("Probe drivers for %s", device->path);
 
1198
        ba2str(&device->bdaddr, addr);
 
1199
 
 
1200
        if (device->blocked) {
 
1201
                DBG("Skipping drivers for blocked device %s", addr);
 
1202
                goto add_uuids;
 
1203
        }
 
1204
 
 
1205
        DBG("Probing drivers for %s", addr);
871
1206
 
872
1207
        for (list = device_drivers; list; list = list->next) {
873
1208
                struct btd_device_driver *driver = list->data;
883
1218
 
884
1219
                err = driver->probe(device, probe_uuids);
885
1220
                if (err < 0) {
886
 
                        error("probe failed with driver %s for device %s",
887
 
                                        driver->name, device->path);
888
 
 
 
1221
                        error("%s driver probe failed for device %s",
 
1222
                                                        driver->name, addr);
889
1223
                        g_free(driver_data);
890
1224
                        g_slist_free(probe_uuids);
891
1225
                        continue;
896
1230
                g_slist_free(probe_uuids);
897
1231
        }
898
1232
 
 
1233
add_uuids:
899
1234
        for (list = profiles; list; list = list->next) {
900
1235
                GSList *l = g_slist_find_custom(device->uuids, list->data,
901
1236
                                                (GCompareFunc) strcasecmp);
906
1241
                                                g_strdup(list->data),
907
1242
                                                (GCompareFunc) strcasecmp);
908
1243
        }
909
 
 
910
 
        if (device->tmp_records) {
911
 
                sdp_list_free(device->tmp_records,
912
 
                                (sdp_free_func_t) sdp_record_free);
913
 
                device->tmp_records = NULL;
914
 
        }
915
1244
}
916
1245
 
917
1246
static void device_remove_drivers(struct btd_device *device, GSList *uuids)
928
1257
 
929
1258
        records = read_records(&src, &device->bdaddr);
930
1259
 
931
 
        debug("Remove drivers for %s", device->path);
 
1260
        DBG("Removing drivers for %s", dstaddr);
932
1261
 
933
1262
        for (list = device->drivers; list; list = next) {
934
1263
                struct btd_driver_data *driver_data = list->data;
938
1267
                next = list->next;
939
1268
 
940
1269
                for (uuid = driver->uuids; *uuid; uuid++) {
941
 
                        sdp_record_t *rec;
942
 
 
943
1270
                        if (!g_slist_find_custom(uuids, *uuid,
944
 
                                        (GCompareFunc) strcasecmp))
 
1271
                                                (GCompareFunc) strcasecmp))
945
1272
                                continue;
946
1273
 
947
 
                        debug("UUID %s was removed from device %s",
 
1274
                        DBG("UUID %s was removed from device %s",
948
1275
                                                        *uuid, dstaddr);
949
1276
 
950
1277
                        driver->remove(device);
952
1279
                                                                driver_data);
953
1280
                        g_free(driver_data);
954
1281
 
955
 
                        rec = find_record_in_list(records, *uuid);
956
 
                        if (!rec)
957
 
                                break;
958
 
 
959
 
                        delete_record(srcaddr, dstaddr, rec->handle);
960
 
 
961
 
                        records = sdp_list_remove(records, rec);
962
 
                        sdp_record_free(rec);
963
 
 
964
1282
                        break;
965
1283
                }
966
1284
        }
967
1285
 
 
1286
        for (list = uuids; list; list = list->next) {
 
1287
                sdp_record_t *rec;
 
1288
 
 
1289
                device->uuids = g_slist_remove(device->uuids, list->data);
 
1290
 
 
1291
                rec = find_record_in_list(records, list->data);
 
1292
                if (!rec)
 
1293
                        continue;
 
1294
 
 
1295
                delete_record(srcaddr, dstaddr, rec->handle);
 
1296
 
 
1297
                records = sdp_list_remove(records, rec);
 
1298
                sdp_record_free(rec);
 
1299
 
 
1300
        }
 
1301
 
968
1302
        if (records)
969
1303
                sdp_list_free(records, (sdp_free_func_t) sdp_record_free);
970
 
 
971
 
        for (list = uuids; list; list = list->next)
972
 
                device->uuids = g_slist_remove(device->uuids, list->data);
973
 
}
974
 
 
975
 
static void iter_append_record(DBusMessageIter *dict, uint32_t handle,
976
 
                                                        const char *record)
977
 
{
978
 
        DBusMessageIter entry;
979
 
 
980
 
        dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
981
 
                                                        NULL, &entry);
982
 
 
983
 
        dbus_message_iter_append_basic(&entry, DBUS_TYPE_UINT32, &handle);
984
 
 
985
 
        dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &record);
986
 
 
987
 
        dbus_message_iter_close_container(dict, &entry);
988
 
}
989
 
 
990
 
static void discover_services_reply(struct browse_req *req, int err,
991
 
                                                        sdp_list_t *recs)
992
 
{
993
 
        DBusMessage *reply;
994
 
        DBusMessageIter iter, dict;
995
 
        sdp_list_t *seq;
996
 
 
997
 
        if (err) {
998
 
                const char *err_if;
999
 
 
1000
 
                if (err == -EHOSTDOWN)
1001
 
                        err_if = ERROR_INTERFACE ".ConnectionAttemptFailed";
1002
 
                else
1003
 
                        err_if = ERROR_INTERFACE ".Failed";
1004
 
 
1005
 
                reply = dbus_message_new_error(req->msg, err_if,
1006
 
                                                        strerror(-err));
1007
 
                g_dbus_send_message(req->conn, reply);
1008
 
                return;
1009
 
        }
1010
 
 
1011
 
        reply = dbus_message_new_method_return(req->msg);
1012
 
        if (!reply)
1013
 
                return;
1014
 
 
1015
 
        dbus_message_iter_init_append(reply, &iter);
1016
 
 
1017
 
        dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1018
 
                        DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1019
 
                        DBUS_TYPE_UINT32_AS_STRING DBUS_TYPE_STRING_AS_STRING
1020
 
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1021
 
 
1022
 
        for (seq = recs; seq; seq = seq->next) {
1023
 
                sdp_record_t *rec = (sdp_record_t *) seq->data;
1024
 
                GString *result;
1025
 
 
1026
 
                if (!rec)
1027
 
                        break;
1028
 
 
1029
 
                result = g_string_new(NULL);
1030
 
 
1031
 
                convert_sdp_record_to_xml(rec, result,
1032
 
                                (void *) g_string_append);
1033
 
 
1034
 
                if (result->len)
1035
 
                        iter_append_record(&dict, rec->handle, result->str);
1036
 
 
1037
 
                g_string_free(result, TRUE);
1038
 
        }
1039
 
 
1040
 
        dbus_message_iter_close_container(&iter, &dict);
1041
 
 
1042
 
        g_dbus_send_message(req->conn, reply);
1043
1304
}
1044
1305
 
1045
1306
static void services_changed(struct btd_device *device)
1054
1315
                uuids[i] = l->data;
1055
1316
 
1056
1317
        emit_array_property_changed(conn, device->path, DEVICE_INTERFACE,
1057
 
                                        "UUIDs", DBUS_TYPE_STRING, &uuids);
 
1318
                                        "UUIDs", DBUS_TYPE_STRING, &uuids, i);
1058
1319
 
1059
1320
        g_free(uuids);
1060
1321
}
1091
1352
                if (sdp_get_service_classes(rec, &svcclass) < 0)
1092
1353
                        continue;
1093
1354
 
 
1355
                /* Check for empty service classes list */
 
1356
                if (svcclass == NULL) {
 
1357
                        DBG("Skipping record with no service classes");
 
1358
                        continue;
 
1359
                }
 
1360
 
1094
1361
                /* Extract the first element and skip the remainning */
1095
1362
                profile_uuid = bt_uuid2string(svcclass->data);
1096
1363
                if (!profile_uuid) {
1167
1434
        g_free(str);
1168
1435
}
1169
1436
 
1170
 
static void browse_req_free(struct browse_req *req)
 
1437
static void create_device_reply(struct btd_device *device, struct browse_req *req)
1171
1438
{
1172
 
        struct btd_device *device = req->device;
1173
 
 
1174
 
        device->discov_active = 0;
1175
 
 
1176
 
        if (device->discov_requestor) {
1177
 
                g_dbus_remove_watch(req->conn, device->discov_listener);
1178
 
                device->discov_listener = 0;
1179
 
                g_free(device->discov_requestor);
1180
 
                device->discov_requestor = NULL;
1181
 
        }
1182
 
 
1183
 
        if (req->msg)
1184
 
                dbus_message_unref(req->msg);
1185
 
        if (req->conn)
1186
 
                dbus_connection_unref(req->conn);
1187
 
        g_slist_foreach(req->profiles_added, (GFunc) g_free, NULL);
1188
 
        g_slist_free(req->profiles_added);
1189
 
        g_slist_free(req->profiles_removed);
1190
 
        if (req->records)
1191
 
                sdp_list_free(req->records, (sdp_free_func_t) sdp_record_free);
1192
 
        g_free(req);
 
1439
        DBusMessage *reply;
 
1440
 
 
1441
        reply = dbus_message_new_method_return(req->msg);
 
1442
        if (!reply)
 
1443
                return;
 
1444
 
 
1445
        dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &device->path,
 
1446
                                        DBUS_TYPE_INVALID);
 
1447
 
 
1448
        g_dbus_send_message(req->conn, reply);
1193
1449
}
1194
1450
 
1195
1451
static void search_cb(sdp_list_t *recs, int err, gpointer user_data)
1196
1452
{
1197
1453
        struct browse_req *req = user_data;
1198
1454
        struct btd_device *device = req->device;
1199
 
        DBusMessage *reply;
 
1455
        char addr[18];
 
1456
 
 
1457
        ba2str(&device->bdaddr, addr);
1200
1458
 
1201
1459
        if (err < 0) {
1202
1460
                error("%s: error updating services: %s (%d)",
1203
 
                                device->path, strerror(-err), -err);
1204
 
                goto proceed;
 
1461
                                addr, strerror(-err), -err);
 
1462
                goto send_reply;
1205
1463
        }
1206
1464
 
1207
1465
        update_services(req, recs);
1208
1466
 
 
1467
        if (device->tmp_records)
 
1468
                sdp_list_free(device->tmp_records,
 
1469
                                        (sdp_free_func_t) sdp_record_free);
 
1470
 
 
1471
        device->tmp_records = req->records;
 
1472
        req->records = NULL;
 
1473
 
1209
1474
        if (!req->profiles_added && !req->profiles_removed) {
1210
 
                debug("%s: No service update", device->path);
1211
 
                goto proceed;
1212
 
        }
1213
 
 
1214
 
        if (device->tmp_records && req->records) {
1215
 
                sdp_list_free(device->tmp_records,
1216
 
                                        (sdp_free_func_t) sdp_record_free);
1217
 
                device->tmp_records = req->records;
1218
 
                req->records = NULL;
 
1475
                DBG("%s: No service update", addr);
 
1476
                goto send_reply;
1219
1477
        }
1220
1478
 
1221
1479
        /* Probe matching drivers for services added */
1229
1487
        /* Propagate services changes */
1230
1488
        services_changed(req->device);
1231
1489
 
1232
 
proceed:
1233
 
        /* Store the device's profiles in the filesystem */
1234
 
        store_profiles(device);
1235
 
 
 
1490
send_reply:
1236
1491
        if (!req->msg)
1237
1492
                goto cleanup;
1238
1493
 
1239
1494
        if (dbus_message_is_method_call(req->msg, DEVICE_INTERFACE,
1240
 
                                        "DiscoverServices")) {
1241
 
                discover_services_reply(req, err, req->records);
1242
 
                goto cleanup;
 
1495
                                        "DiscoverServices"))
 
1496
                discover_services_reply(req, err, device->tmp_records);
 
1497
        else if (dbus_message_is_method_call(req->msg, ADAPTER_INTERFACE,
 
1498
                                                "CreatePairedDevice"))
 
1499
                create_device_reply(device, req);
 
1500
        else if (dbus_message_is_method_call(req->msg, ADAPTER_INTERFACE,
 
1501
                                                "CreateDevice")) {
 
1502
                if (err < 0) {
 
1503
                        error_failed_errno(req->conn, req->msg, -err);
 
1504
                        goto cleanup;
 
1505
                }
 
1506
 
 
1507
                create_device_reply(device, req);
 
1508
                device_set_temporary(device, FALSE);
1243
1509
        }
1244
1510
 
1245
 
        /* Reply create device request */
1246
 
        reply = dbus_message_new_method_return(req->msg);
1247
 
        if (!reply)
1248
 
                goto cleanup;
1249
 
 
1250
 
        dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &device->path,
1251
 
                                                        DBUS_TYPE_INVALID);
1252
 
 
1253
 
        g_dbus_send_message(req->conn, reply);
1254
 
 
1255
1511
cleanup:
1256
 
        browse_req_free(req);
 
1512
        if (!device->temporary)
 
1513
                store_profiles(device);
 
1514
        device->browse = NULL;
 
1515
        browse_request_free(req);
1257
1516
}
1258
1517
 
1259
1518
static void browse_cb(sdp_list_t *recs, int err, gpointer user_data)
1306
1565
                                                l->data);
1307
1566
}
1308
1567
 
 
1568
static void primary_cb(GSList *services, int err, gpointer user_data)
 
1569
{
 
1570
        struct browse_req *req = user_data;
 
1571
        struct btd_device *device = req->device;
 
1572
 
 
1573
        if (err) {
 
1574
                error_failed_errno(req->conn, req->msg, -err);
 
1575
                goto done;
 
1576
        }
 
1577
 
 
1578
        services_changed(req->device);
 
1579
        device_set_temporary(req->device, FALSE);
 
1580
        device_probe_drivers(req->device, services);
 
1581
 
 
1582
        create_device_reply(req->device, req);
 
1583
 
 
1584
done:
 
1585
        device->browse = NULL;
 
1586
        browse_request_free(req);
 
1587
}
 
1588
 
 
1589
static struct browse_req *browse_primary(struct btd_device *device, int *err)
 
1590
{
 
1591
        struct btd_adapter *adapter = device->adapter;
 
1592
        struct browse_req *req;
 
1593
        bdaddr_t src;
 
1594
        int ret;
 
1595
 
 
1596
        req = g_new0(struct browse_req, 1);
 
1597
        req->device = btd_device_ref(device);
 
1598
 
 
1599
        adapter_get_address(adapter, &src);
 
1600
 
 
1601
        ret = bt_discover_primary(&src, &device->bdaddr, -1, primary_cb, req,
 
1602
                                                                        NULL);
 
1603
 
 
1604
        if (ret < 0) {
 
1605
                browse_request_free(req);
 
1606
                if (err)
 
1607
                        *err = ret;
 
1608
 
 
1609
                return NULL;
 
1610
        }
 
1611
 
 
1612
        return req;
 
1613
}
 
1614
 
 
1615
static struct browse_req *browse_sdp(struct btd_device *device, uuid_t *search,
 
1616
                                                gboolean reverse, int *err)
 
1617
{
 
1618
        struct btd_adapter *adapter = device->adapter;
 
1619
        struct browse_req *req;
 
1620
        bt_callback_t cb;
 
1621
        bdaddr_t src;
 
1622
        uuid_t uuid;
 
1623
        int ret;
 
1624
 
 
1625
        adapter_get_address(adapter, &src);
 
1626
 
 
1627
        req = g_new0(struct browse_req, 1);
 
1628
        req->device = btd_device_ref(device);
 
1629
        if (search) {
 
1630
                memcpy(&uuid, search, sizeof(uuid_t));
 
1631
                cb = search_cb;
 
1632
        } else {
 
1633
                sdp_uuid16_create(&uuid, uuid_list[req->search_uuid++]);
 
1634
                init_browse(req, reverse);
 
1635
                cb = browse_cb;
 
1636
        }
 
1637
 
 
1638
        ret = bt_search_service(&src, &device->bdaddr, &uuid, cb, req, NULL);
 
1639
        if (ret < 0) {
 
1640
                browse_request_free(req);
 
1641
                if (err)
 
1642
                        *err = ret;
 
1643
 
 
1644
                return NULL;
 
1645
        }
 
1646
 
 
1647
        return req;
 
1648
}
 
1649
 
1309
1650
int device_browse(struct btd_device *device, DBusConnection *conn,
1310
1651
                        DBusMessage *msg, uuid_t *search, gboolean reverse)
1311
1652
{
1312
 
        struct btd_adapter *adapter = device->adapter;
1313
1653
        struct browse_req *req;
1314
 
        bdaddr_t src;
1315
 
        uuid_t uuid;
1316
 
        bt_callback_t cb;
1317
 
        int err;
 
1654
        int err = 0;
1318
1655
 
1319
 
        if (device->discov_active)
 
1656
        if (device->browse)
1320
1657
                return -EBUSY;
1321
1658
 
1322
 
        adapter_get_address(adapter, &src);
 
1659
        if (device->le)
 
1660
                req = browse_primary(device, &err);
 
1661
        else
 
1662
                req = browse_sdp(device, search, reverse, &err);
1323
1663
 
1324
 
        req = g_new0(struct browse_req, 1);
 
1664
        if (req == NULL)
 
1665
                return err;
1325
1666
 
1326
1667
        if (conn == NULL)
1327
1668
                conn = get_dbus_connection();
1328
1669
 
1329
1670
        req->conn = dbus_connection_ref(conn);
1330
 
        req->device = device;
1331
 
 
1332
 
        if (search) {
1333
 
                memcpy(&uuid, search, sizeof(uuid_t));
1334
 
                cb = search_cb;
1335
 
        } else {
1336
 
                sdp_uuid16_create(&uuid, uuid_list[req->search_uuid++]);
1337
 
                init_browse(req, reverse);
1338
 
                cb = browse_cb;
1339
 
        }
1340
 
 
1341
 
        device->discov_active = 1;
 
1671
        device->browse = req;
1342
1672
 
1343
1673
        if (msg) {
1344
1674
                const char *sender = dbus_message_get_sender(msg);
1345
1675
 
1346
1676
                req->msg = dbus_message_ref(msg);
1347
 
                device->discov_requestor = g_strdup(sender);
1348
1677
                /* Track the request owner to cancel it
1349
1678
                 * automatically if the owner exits */
1350
 
                device->discov_listener = g_dbus_add_disconnect_watch(conn,
 
1679
                req->listener_id = g_dbus_add_disconnect_watch(conn,
1351
1680
                                                sender,
1352
1681
                                                discover_services_req_exit,
1353
 
                                                device, NULL);
 
1682
                                                req, NULL);
1354
1683
        }
1355
1684
 
1356
 
        err = bt_search_service(&src, &device->bdaddr,
1357
 
                                &uuid, cb, req, NULL);
1358
 
        if (err < 0)
1359
 
                browse_req_free(req);
1360
 
 
1361
1685
        return err;
1362
1686
}
1363
1687
 
1387
1711
        if (!device)
1388
1712
                return NULL;
1389
1713
 
1390
 
        return  device->agent;
1391
 
}
1392
 
 
1393
 
void device_set_agent(struct btd_device *device, struct agent *agent)
1394
 
{
1395
 
        if (!device)
1396
 
                return;
1397
 
 
1398
 
        device->agent = agent;
 
1714
        if (device->agent)
 
1715
                return device->agent;
 
1716
 
 
1717
        return adapter_get_agent(device->adapter);
1399
1718
}
1400
1719
 
1401
1720
gboolean device_is_busy(struct btd_device *device)
1402
1721
{
1403
 
        return device->discov_active ? TRUE : FALSE;
 
1722
        return device->browse ? TRUE : FALSE;
1404
1723
}
1405
1724
 
1406
1725
gboolean device_is_temporary(struct btd_device *device)
1453
1772
        return FALSE;
1454
1773
}
1455
1774
 
1456
 
DBusMessage *new_authentication_return(DBusMessage *msg, uint8_t status)
 
1775
static DBusMessage *new_authentication_return(DBusMessage *msg, int status)
1457
1776
{
1458
1777
        switch (status) {
1459
1778
        case 0x00: /* success */
1490
1809
        case 0x0d: /* limited resources */
1491
1810
        case 0x13: /* user ended the connection */
1492
1811
        case 0x14: /* terminated due to low resources */
 
1812
        case 0x16: /* connection terminated */
1493
1813
                return dbus_message_new_error(msg,
1494
1814
                                        ERROR_INTERFACE ".AuthenticationCanceled",
1495
1815
                                        "Authentication Canceled");
1507
1827
        }
1508
1828
}
1509
1829
 
1510
 
static void bonding_request_free(struct bonding_req *bonding, gboolean close)
 
1830
static void bonding_request_free(struct bonding_req *bonding)
1511
1831
{
1512
1832
        struct btd_device *device;
1513
1833
 
1523
1843
        if (bonding->conn)
1524
1844
                dbus_connection_unref(bonding->conn);
1525
1845
 
1526
 
        if (bonding->io_id)
1527
 
                g_source_remove(bonding->io_id);
1528
 
 
1529
 
        if (bonding->io) {
1530
 
                if (close)
1531
 
                        g_io_channel_shutdown(bonding->io, TRUE, NULL);
 
1846
        if (bonding->io)
1532
1847
                g_io_channel_unref(bonding->io);
1533
 
        }
1534
1848
 
1535
1849
        device = bonding->device;
 
1850
        g_free(bonding);
1536
1851
 
1537
 
        if (device && device->agent) {
1538
 
                agent_destroy(device->agent, FALSE);
1539
 
                device->agent = NULL;
1540
 
        }
 
1852
        if (!device)
 
1853
                return;
1541
1854
 
1542
1855
        device->bonding = NULL;
1543
 
        g_free(bonding);
 
1856
 
 
1857
        adapter_resume_discovery(device->adapter);
 
1858
 
 
1859
        if (!device->agent)
 
1860
                return;
 
1861
 
 
1862
        agent_cancel(device->agent);
 
1863
        agent_free(device->agent);
 
1864
        device->agent = NULL;
1544
1865
}
1545
1866
 
1546
 
static void device_set_paired(struct btd_device *device, gboolean value)
 
1867
void device_set_paired(struct btd_device *device, gboolean value)
1547
1868
{
1548
1869
        DBusConnection *conn = get_dbus_connection();
1549
1870
 
 
1871
        if (device->paired == value)
 
1872
                return;
 
1873
 
 
1874
        device->paired = value;
 
1875
 
1550
1876
        emit_property_changed(conn, device->path, DEVICE_INTERFACE, "Paired",
1551
1877
                                DBUS_TYPE_BOOLEAN, &value);
1552
1878
}
1555
1881
{
1556
1882
        struct btd_device *device = user_data;
1557
1883
 
1558
 
        device_set_agent(device, NULL);
 
1884
        device->agent = NULL;
1559
1885
 
1560
1886
        if (device->authr)
1561
1887
                device->authr->agent = NULL;
1570
1896
        struct bonding_req *bonding;
1571
1897
        const char *name = dbus_message_get_sender(msg);
1572
1898
        struct agent *agent;
 
1899
        char addr[18];
1573
1900
 
1574
 
        debug("%s: requesting bonding", device->path);
 
1901
        ba2str(&device->bdaddr, addr);
 
1902
        DBG("Requesting bonding for %s", addr);
1575
1903
 
1576
1904
        if (!agent_path)
1577
1905
                goto proceed;
1587
1915
 
1588
1916
        device->agent = agent;
1589
1917
 
1590
 
        debug("Temporary agent registered for %s at %s:%s",
1591
 
                        device->path, name, agent_path);
 
1918
        DBG("Temporary agent registered for %s at %s:%s",
 
1919
                        addr, name, agent_path);
1592
1920
 
1593
1921
proceed:
1594
1922
        bonding = g_new0(struct bonding_req, 1);
1596
1924
        bonding->conn = dbus_connection_ref(conn);
1597
1925
        bonding->msg = dbus_message_ref(msg);
1598
1926
 
 
1927
        adapter_suspend_discovery(device->adapter);
 
1928
 
1599
1929
        return bonding;
1600
1930
}
1601
1931
 
1602
 
static gboolean bonding_io_cb(GIOChannel *io, GIOCondition cond,
1603
 
                                                        gpointer user_data)
1604
 
{
1605
 
        struct btd_device *device = user_data;
1606
 
 
1607
 
        if (!device->bonding)
1608
 
                return FALSE;
1609
 
 
1610
 
        error_connection_attempt_failed(device->bonding->conn,
1611
 
                                        device->bonding->msg, ENETDOWN);
1612
 
 
1613
 
        return FALSE;
1614
 
}
1615
 
 
1616
1932
static void bonding_connect_cb(GIOChannel *io, GError *err, gpointer user_data)
1617
1933
{
1618
1934
        struct btd_device *device = user_data;
1619
 
        struct hci_request rq;
1620
 
        auth_requested_cp cp;
1621
 
        evt_cmd_status rp;
1622
 
        int dd;
1623
1935
        uint16_t handle;
1624
1936
 
1625
1937
        if (!device->bonding) {
1628
1940
                return;
1629
1941
        }
1630
1942
 
1631
 
        if (err) {
1632
 
                error("%s", err->message);
1633
 
                error_connection_attempt_failed(device->bonding->conn,
1634
 
                                                device->bonding->msg,
1635
 
                                                ENETDOWN);
1636
 
                goto cleanup;
1637
 
        }
 
1943
        if (err)
 
1944
                /* Wait proper error to be propagated by bonding complete */
 
1945
                return;
1638
1946
 
1639
1947
        if (!bt_io_get(io, BT_IO_L2RAW, &err,
1640
1948
                        BT_IO_OPT_HANDLE, &handle,
1641
1949
                        BT_IO_OPT_INVALID)) {
1642
1950
                error("Unable to get connection handle: %s", err->message);
1643
 
                error_connection_attempt_failed(device->bonding->conn,
1644
 
                                                device->bonding->msg,
1645
 
                                                ENETDOWN);
1646
1951
                g_error_free(err);
1647
1952
                goto failed;
1648
1953
        }
1649
1954
 
1650
 
        dd = hci_open_dev(adapter_get_dev_id(device->adapter));
1651
 
        if (dd < 0) {
1652
 
                DBusMessage *reply = no_such_adapter(device->bonding->msg);
1653
 
                g_dbus_send_message(device->bonding->conn, reply);
1654
 
                goto failed;
1655
 
        }
1656
 
 
1657
 
        memset(&rp, 0, sizeof(rp));
1658
 
 
1659
 
        memset(&cp, 0, sizeof(cp));
1660
 
        cp.handle = htobs(handle);
1661
 
 
1662
 
        memset(&rq, 0, sizeof(rq));
1663
 
        rq.ogf    = OGF_LINK_CTL;
1664
 
        rq.ocf    = OCF_AUTH_REQUESTED;
1665
 
        rq.cparam = &cp;
1666
 
        rq.clen   = AUTH_REQUESTED_CP_SIZE;
1667
 
        rq.rparam = &rp;
1668
 
        rq.rlen   = EVT_CMD_STATUS_SIZE;
1669
 
        rq.event  = EVT_CMD_STATUS;
1670
 
 
1671
 
        if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {
1672
 
                error("Unable to send HCI request: %s (%d)",
1673
 
                                        strerror(errno), errno);
1674
 
                error_failed_errno(device->bonding->conn, device->bonding->msg,
1675
 
                                errno);
1676
 
                hci_close_dev(dd);
1677
 
                goto failed;
1678
 
        }
1679
 
 
1680
 
        if (rp.status) {
1681
 
                error("HCI_Authentication_Requested failed with status 0x%02x",
1682
 
                                rp.status);
1683
 
                error_failed_errno(device->bonding->conn, device->bonding->msg,
1684
 
                                bt_error(rp.status));
1685
 
                hci_close_dev(dd);
1686
 
                goto failed;
1687
 
        }
1688
 
 
1689
 
        hci_close_dev(dd);
1690
 
 
1691
 
        device->bonding->io_id = g_io_add_watch(io,
1692
 
                                        G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1693
 
                                        bonding_io_cb, device);
 
1955
        if (btd_adapter_request_authentication(device->adapter, handle) < 0)
 
1956
                goto failed;
1694
1957
 
1695
1958
        return;
1696
1959
 
1697
1960
failed:
1698
1961
        g_io_channel_shutdown(io, TRUE, NULL);
1699
 
 
1700
 
cleanup:
1701
 
        device->bonding->io_id = 0;
1702
 
        bonding_request_free(device->bonding, FALSE);
 
1962
        device_cancel_bonding(device, HCI_UNSPECIFIED_ERROR);
1703
1963
}
1704
1964
 
1705
1965
static void create_bond_req_exit(DBusConnection *conn, void *user_data)
1706
1966
{
1707
1967
        struct btd_device *device = user_data;
 
1968
        char addr[18];
1708
1969
 
1709
 
        debug("%s: requestor exited before bonding was completed", device->path);
 
1970
        ba2str(&device->bdaddr, addr);
 
1971
        DBG("%s: requestor exited before bonding was completed", addr);
1710
1972
 
1711
1973
        if (device->authr)
1712
1974
                device_cancel_authentication(device, FALSE);
1713
1975
 
1714
1976
        if (device->bonding) {
1715
1977
                device->bonding->listener_id = 0;
1716
 
                bonding_request_free(device->bonding, TRUE);
 
1978
                device_request_disconnect(device, NULL);
1717
1979
        }
1718
1980
}
1719
1981
 
1730
1992
        bdaddr_t src;
1731
1993
        GError *err = NULL;
1732
1994
        GIOChannel *io;
 
1995
        BtIOSecLevel sec_level;
1733
1996
 
1734
1997
        adapter_get_address(adapter, &src);
1735
1998
        ba2str(&src, srcaddr);
1750
2013
                                "Bonding already exists");
1751
2014
        }
1752
2015
 
 
2016
        /* If our IO capability is NoInputNoOutput use medium security
 
2017
         * level (i.e. don't require MITM protection) else use high
 
2018
         * security level */
 
2019
        if (capability == 0x03)
 
2020
                sec_level = BT_IO_SEC_MEDIUM;
 
2021
        else
 
2022
                sec_level = BT_IO_SEC_HIGH;
1753
2023
 
1754
2024
        io = bt_io_connect(BT_IO_L2RAW, bonding_connect_cb, device,
1755
2025
                                NULL, &err,
1756
2026
                                BT_IO_OPT_SOURCE_BDADDR, &src,
1757
2027
                                BT_IO_OPT_DEST_BDADDR, &device->bdaddr,
1758
 
                                BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_HIGH,
 
2028
                                BT_IO_OPT_SEC_LEVEL, sec_level,
1759
2029
                                BT_IO_OPT_INVALID);
1760
2030
        if (io == NULL) {
1761
2031
                DBusMessage *reply;
1762
2032
                reply = g_dbus_create_error(msg,
1763
2033
                                ERROR_INTERFACE ".ConnectionAttemptFailed",
1764
 
                                err->message);
 
2034
                                "%s", err->message);
1765
2035
                error("bt_io_connect: %s", err->message);
1766
2036
                g_error_free(err);
1767
2037
                return reply;
1803
2073
        if (auth && auth->type == AUTH_TYPE_NOTIFY && auth->agent)
1804
2074
                agent_cancel(auth->agent);
1805
2075
 
1806
 
        if (status)
1807
 
                goto failed;
 
2076
        if (status) {
 
2077
                device_cancel_authentication(device, TRUE);
 
2078
                device_cancel_bonding(device, status);
 
2079
                return;
 
2080
        }
1808
2081
 
1809
2082
        device->auth = 0xff;
1810
 
        device->temporary = FALSE;
1811
2083
 
1812
2084
        g_free(device->authr);
1813
2085
        device->authr = NULL;
1815
2087
        if (device->renewed_key)
1816
2088
                return;
1817
2089
 
 
2090
        device_set_temporary(device, FALSE);
 
2091
 
1818
2092
        /* If we were initiators start service discovery immediately.
1819
2093
         * However if the other end was the initator wait a few seconds
1820
2094
         * before SDP. This is due to potential IOP issues if the other
1829
2103
 
1830
2104
                device_browse(device, bonding->conn, bonding->msg,
1831
2105
                                NULL, FALSE);
1832
 
        } else if (!device->discov_active && !device->discov_timer &&
1833
 
                        main_opts.reverse_sdp) {
1834
 
                /* If we are not initiators and there is no currently active
1835
 
                 * discovery or discovery timer, set the discovery timer */
1836
 
                debug("setting timer for reverse service discovery");
1837
 
                device->discov_timer = g_timeout_add_seconds(DISCOVERY_TIMER,
1838
 
                                                start_discovery,
1839
 
                                                device);
 
2106
 
 
2107
                bonding_request_free(bonding);
 
2108
        } else {
 
2109
                if (!device->browse && !device->discov_timer &&
 
2110
                                main_opts.reverse_sdp) {
 
2111
                        /* If we are not initiators and there is no currently
 
2112
                         * active discovery or discovery timer, set discovery
 
2113
                         * timer */
 
2114
                        DBG("setting timer for reverse service discovery");
 
2115
                        device->discov_timer = g_timeout_add_seconds(
 
2116
                                                        DISCOVERY_TIMER,
 
2117
                                                        start_discovery,
 
2118
                                                        device);
 
2119
                }
1840
2120
        }
1841
2121
 
1842
2122
        device_set_paired(device, TRUE);
1843
 
 
1844
 
        bonding_request_free(bonding, TRUE);
1845
 
 
1846
 
        return;
1847
 
 
1848
 
failed:
1849
 
        device_cancel_bonding(device, status);
 
2123
}
 
2124
 
 
2125
gboolean device_is_creating(struct btd_device *device, const char *sender)
 
2126
{
 
2127
        DBusMessage *msg;
 
2128
 
 
2129
        if (device->bonding && device->bonding->msg)
 
2130
                msg = device->bonding->msg;
 
2131
        else if (device->browse && device->browse->msg)
 
2132
                msg = device->browse->msg;
 
2133
        else
 
2134
                return FALSE;
 
2135
 
 
2136
        if (!dbus_message_is_method_call(msg, ADAPTER_INTERFACE,
 
2137
                                                "CreatePairedDevice") &&
 
2138
                        !dbus_message_is_method_call(msg, ADAPTER_INTERFACE,
 
2139
                                                        "CreateDevice"))
 
2140
                return FALSE;
 
2141
 
 
2142
        if (sender == NULL)
 
2143
                return TRUE;
 
2144
 
 
2145
        return g_str_equal(sender, dbus_message_get_sender(msg));
1850
2146
}
1851
2147
 
1852
2148
gboolean device_is_bonding(struct btd_device *device, const char *sender)
1866
2162
{
1867
2163
        struct bonding_req *bonding = device->bonding;
1868
2164
        DBusMessage *reply;
 
2165
        char addr[18];
1869
2166
 
1870
2167
        if (!bonding)
1871
2168
                return;
1872
2169
 
1873
 
        debug("%s: canceling bonding request", device->path);
 
2170
        ba2str(&device->bdaddr, addr);
 
2171
        DBG("Canceling bonding request for %s", addr);
1874
2172
 
1875
2173
        if (device->authr)
1876
2174
                device_cancel_authentication(device, FALSE);
1878
2176
        reply = new_authentication_return(bonding->msg, status);
1879
2177
        g_dbus_send_message(bonding->conn, reply);
1880
2178
 
1881
 
        bonding_request_free(bonding, TRUE);
 
2179
        bonding_request_cancel(bonding);
 
2180
        bonding_request_free(bonding);
1882
2181
}
1883
2182
 
1884
 
static void pincode_cb(struct agent *agent, DBusError *err, const char *pincode,
1885
 
                        void *data)
 
2183
static void pincode_cb(struct agent *agent, DBusError *err,
 
2184
                                        const char *pincode, void *data)
1886
2185
{
1887
2186
        struct authentication_req *auth = data;
1888
2187
        struct btd_device *device = auth->device;
1889
2188
 
1890
2189
        /* No need to reply anything if the authentication already failed */
1891
 
        if (!auth->cb)
 
2190
        if (auth->cb == NULL)
1892
2191
                return;
1893
2192
 
1894
2193
        ((agent_pincode_cb) auth->cb)(agent, err, pincode, device);
1895
2194
 
1896
 
        auth->cb = NULL;
 
2195
        device->authr->cb = NULL;
 
2196
        device->authr->agent = NULL;
1897
2197
}
1898
2198
 
1899
2199
static void confirm_cb(struct agent *agent, DBusError *err, void *data)
1902
2202
        struct btd_device *device = auth->device;
1903
2203
 
1904
2204
        /* No need to reply anything if the authentication already failed */
1905
 
        if (!auth->cb)
 
2205
        if (auth->cb == NULL)
1906
2206
                return;
1907
2207
 
1908
2208
        ((agent_cb) auth->cb)(agent, err, device);
1909
2209
 
1910
 
        auth->cb = NULL;
 
2210
        device->authr->cb = NULL;
 
2211
        device->authr->agent = NULL;
1911
2212
}
1912
2213
 
1913
 
static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
1914
 
                        void *data)
 
2214
static void passkey_cb(struct agent *agent, DBusError *err,
 
2215
                                                uint32_t passkey, void *data)
1915
2216
{
1916
2217
        struct authentication_req *auth = data;
1917
2218
        struct btd_device *device = auth->device;
1918
2219
 
1919
2220
        /* No need to reply anything if the authentication already failed */
1920
 
        if (!auth->cb)
 
2221
        if (auth->cb == NULL)
1921
2222
                return;
1922
2223
 
1923
2224
        ((agent_passkey_cb) auth->cb)(agent, err, passkey, device);
1924
2225
 
1925
 
        auth->cb = NULL;
 
2226
        device->authr->cb = NULL;
 
2227
        device->authr->agent = NULL;
1926
2228
}
1927
2229
 
1928
2230
int device_request_authentication(struct btd_device *device, auth_type_t type,
1929
 
                                uint32_t passkey, void *cb)
 
2231
                                                uint32_t passkey, void *cb)
1930
2232
{
1931
2233
        struct authentication_req *auth;
1932
2234
        struct agent *agent;
1933
 
        int ret;
1934
 
 
1935
 
        debug("%s: requesting agent authentication", device->path);
1936
 
 
1937
 
        agent = device->agent;
1938
 
 
1939
 
        if (!agent)
1940
 
                agent = adapter_get_agent(device->adapter);
1941
 
 
 
2235
        char addr[18];
 
2236
        int err;
 
2237
 
 
2238
        ba2str(&device->bdaddr, addr);
 
2239
        DBG("Requesting agent authentication for %s", addr);
 
2240
 
 
2241
        if (device->authr) {
 
2242
                error("Authentication already requested for %s", addr);
 
2243
                return -EALREADY;
 
2244
        }
 
2245
 
 
2246
        agent = device_get_agent(device);
1942
2247
        if (!agent) {
1943
 
                error("No agent available for %u request", type);
 
2248
                error("No agent available for request type %d", type);
1944
2249
                return -EPERM;
1945
2250
        }
1946
2251
 
1953
2258
 
1954
2259
        switch (type) {
1955
2260
        case AUTH_TYPE_PINCODE:
1956
 
                ret = agent_request_pincode(agent, device, pincode_cb,
1957
 
                                        auth);
 
2261
                err = agent_request_pincode(agent, device, pincode_cb,
 
2262
                                                                auth, NULL);
1958
2263
                break;
1959
2264
        case AUTH_TYPE_PASSKEY:
1960
 
                ret = agent_request_passkey(agent, device, passkey_cb,
1961
 
                                        auth);
 
2265
                err = agent_request_passkey(agent, device, passkey_cb,
 
2266
                                                                auth, NULL);
1962
2267
                break;
1963
2268
        case AUTH_TYPE_CONFIRM:
1964
 
                ret = agent_request_confirmation(agent, device, passkey,
1965
 
                                        confirm_cb, auth);
 
2269
                err = agent_request_confirmation(agent, device, passkey,
 
2270
                                                confirm_cb, auth, NULL);
1966
2271
                break;
1967
2272
        case AUTH_TYPE_NOTIFY:
1968
 
                ret = agent_display_passkey(agent, device, passkey);
 
2273
                err = agent_display_passkey(agent, device, passkey);
1969
2274
                break;
1970
2275
        case AUTH_TYPE_AUTO:
1971
 
                ret = 0;
 
2276
                err = 0;
1972
2277
                break;
1973
2278
        default:
1974
 
                ret = -EINVAL;
 
2279
                err = -EINVAL;
1975
2280
        }
1976
2281
 
1977
 
        if (ret < 0) {
 
2282
        if (err < 0) {
1978
2283
                error("Failed requesting authentication");
1979
2284
                g_free(auth);
1980
2285
                device->authr = NULL;
1981
2286
        }
1982
2287
 
1983
 
        return ret;
 
2288
        return err;
1984
2289
}
1985
2290
 
1986
2291
static void cancel_authentication(struct authentication_req *auth)
1987
2292
{
1988
 
        struct btd_device *device = auth->device;
1989
 
        struct agent *agent = auth->agent;
 
2293
        struct btd_device *device;
 
2294
        struct agent *agent;
1990
2295
        DBusError err;
1991
2296
 
1992
 
        if (!auth->cb)
 
2297
        if (!auth || !auth->cb)
1993
2298
                return;
1994
2299
 
 
2300
        device = auth->device;
 
2301
        agent = auth->agent;
 
2302
 
1995
2303
        dbus_error_init(&err);
1996
2304
        dbus_set_error_const(&err, "org.bluez.Error.Canceled", NULL);
1997
2305
 
2018
2326
void device_cancel_authentication(struct btd_device *device, gboolean aborted)
2019
2327
{
2020
2328
        struct authentication_req *auth = device->authr;
 
2329
        char addr[18];
2021
2330
 
2022
2331
        if (!auth)
2023
2332
                return;
2024
2333
 
2025
 
        debug("%s: canceling authentication request", device->path);
 
2334
        ba2str(&device->bdaddr, addr);
 
2335
        DBG("Canceling authentication request for %s", addr);
2026
2336
 
2027
2337
        if (auth->agent)
2028
2338
                agent_cancel(auth->agent);
2039
2349
        return (device->authr != NULL);
2040
2350
}
2041
2351
 
 
2352
gboolean device_is_authorizing(struct btd_device *device)
 
2353
{
 
2354
        return device->authorizing;
 
2355
}
 
2356
 
 
2357
void device_set_authorizing(struct btd_device *device, gboolean auth)
 
2358
{
 
2359
        device->authorizing = auth;
 
2360
}
 
2361
 
2042
2362
void device_set_renewed_key(struct btd_device *device, gboolean renewed)
2043
2363
{
2044
2364
        device->renewed_key = renewed;
2045
2365
}
2046
2366
 
 
2367
void device_add_service(struct btd_device *device, const char *path)
 
2368
{
 
2369
        if (g_slist_find_custom(device->services, path, (GCompareFunc) strcmp))
 
2370
                return;
 
2371
 
 
2372
        device->services = g_slist_append(device->services, g_strdup(path));
 
2373
}
 
2374
 
2047
2375
void btd_device_add_uuid(struct btd_device *device, const char *uuid)
2048
2376
{
2049
2377
        GSList *uuid_list;
2066
2394
}
2067
2395
 
2068
2396
const sdp_record_t *btd_device_get_record(struct btd_device *device,
2069
 
                                                const char *uuid)
 
2397
                                                        const char *uuid)
2070
2398
{
2071
2399
        bdaddr_t src;
2072
2400
 
2073
 
        if (device->tmp_records)
2074
 
                return find_record_in_list(device->tmp_records, uuid);
 
2401
        if (device->tmp_records) {
 
2402
                const sdp_record_t *record;
 
2403
 
 
2404
                record = find_record_in_list(device->tmp_records, uuid);
 
2405
                if (record != NULL)
 
2406
                        return record;
 
2407
        }
2075
2408
 
2076
2409
        adapter_get_address(device->adapter, &src);
2077
2410
 
2082
2415
        return find_record_in_list(device->tmp_records, uuid);
2083
2416
}
2084
2417
 
 
2418
gboolean device_set_debug_key(struct btd_device *device, uint8_t *key)
 
2419
{
 
2420
        if (key == NULL) {
 
2421
                device->has_debug_key = FALSE;
 
2422
                return TRUE;
 
2423
        }
 
2424
 
 
2425
        memcpy(device->debug_key, key, 16);
 
2426
        device->has_debug_key = TRUE;
 
2427
 
 
2428
        return TRUE;
 
2429
}
 
2430
 
 
2431
gboolean device_get_debug_key(struct btd_device *device, uint8_t *key)
 
2432
{
 
2433
        if (!device->has_debug_key)
 
2434
                return FALSE;
 
2435
 
 
2436
        if (key != NULL)
 
2437
                memcpy(key, device->debug_key, 16);
 
2438
 
 
2439
        return TRUE;
 
2440
}
 
2441
 
2085
2442
int btd_register_device_driver(struct btd_device_driver *driver)
2086
2443
{
2087
2444
        device_drivers = g_slist_append(device_drivers, driver);
2093
2450
{
2094
2451
        device_drivers = g_slist_remove(device_drivers, driver);
2095
2452
}
 
2453
 
 
2454
struct btd_device *btd_device_ref(struct btd_device *device)
 
2455
{
 
2456
        device->ref++;
 
2457
 
 
2458
        DBG("%p: ref=%d", device, device->ref);
 
2459
 
 
2460
        return device;
 
2461
}
 
2462
 
 
2463
void btd_device_unref(struct btd_device *device)
 
2464
{
 
2465
        DBusConnection *conn = get_dbus_connection();
 
2466
        gchar *path;
 
2467
 
 
2468
        device->ref--;
 
2469
 
 
2470
        DBG("%p: ref=%d", device, device->ref);
 
2471
 
 
2472
        if (device->ref > 0)
 
2473
                return;
 
2474
 
 
2475
        path = g_strdup(device->path);
 
2476
 
 
2477
        g_dbus_unregister_interface(conn, path, DEVICE_INTERFACE);
 
2478
 
 
2479
        g_free(path);
 
2480
}