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

« back to all changes in this revision

Viewing changes to src/dbus-hci.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-10-07 12:10:29 UTC
  • Revision ID: james.westby@ubuntu.com-20081007121029-4gup4fmmh2vfo5nh
Tags: upstream-4.12
ImportĀ upstreamĀ versionĀ 4.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2006-2007  Nokia Corporation
 
6
 *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
 
7
 *
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 *
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <config.h>
 
27
#endif
 
28
 
 
29
#define _GNU_SOURCE
 
30
#include <stdio.h>
 
31
#include <errno.h>
 
32
#include <unistd.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <sys/param.h>
 
36
#include <sys/ioctl.h>
 
37
#include <sys/socket.h>
 
38
 
 
39
#include <bluetooth/bluetooth.h>
 
40
#include <bluetooth/hci.h>
 
41
#include <bluetooth/hci_lib.h>
 
42
#include <bluetooth/sdp.h>
 
43
 
 
44
#include <glib.h>
 
45
#include <dbus/dbus.h>
 
46
#include <gdbus.h>
 
47
 
 
48
#include "logging.h"
 
49
#include "textfile.h"
 
50
 
 
51
#include "hcid.h"
 
52
#include "manager.h"
 
53
#include "adapter.h"
 
54
#include "device.h"
 
55
#include "error.h"
 
56
#include "glib-helper.h"
 
57
#include "dbus-common.h"
 
58
#include "agent.h"
 
59
#include "storage.h"
 
60
 
 
61
static DBusConnection *connection = NULL;
 
62
 
 
63
const char *class_to_icon(uint32_t class)
 
64
{
 
65
        switch ((class & 0x1f00) >> 8) {
 
66
        case 0x01:
 
67
                return "computer";
 
68
        case 0x02:
 
69
                switch ((class & 0xfc) >> 2) {
 
70
                case 0x01:
 
71
                case 0x02:
 
72
                case 0x03:
 
73
                case 0x05:
 
74
                        return "phone";
 
75
                case 0x04:
 
76
                        return "modem";
 
77
                }
 
78
                break;
 
79
        case 0x03:
 
80
                return "network-wireless";
 
81
        case 0x04:
 
82
                switch ((class & 0xfc) >> 2) {
 
83
                case 0x01:
 
84
                case 0x02:
 
85
                        return "audio-card";    /* Headset */
 
86
                case 0x06:
 
87
                        return "audio-card";    /* Headphone */
 
88
                }
 
89
                break;
 
90
        case 0x05:
 
91
                switch ((class & 0xc0) >> 6) {
 
92
                case 0x00:
 
93
                        switch ((class & 0x1e) >> 2) {
 
94
                        case 0x01:
 
95
                        case 0x02:
 
96
                                return "input-gaming";
 
97
                        }
 
98
                        break;
 
99
                case 0x01:
 
100
                        return "input-keyboard";
 
101
                case 0x02:
 
102
                        switch ((class & 0x1e) >> 2) {
 
103
                        case 0x05:
 
104
                                return "input-tablet";
 
105
                        default:
 
106
                                return "input-mouse";
 
107
                        }
 
108
                }
 
109
                break;
 
110
        case 0x06:
 
111
                if (class & 0x80)
 
112
                        return "printer";
 
113
                if (class & 0x20)
 
114
                        return "camera-photo";
 
115
                break;
 
116
        }
 
117
 
 
118
        return NULL;
 
119
}
 
120
 
 
121
DBusMessage *new_authentication_return(DBusMessage *msg, uint8_t status)
 
122
{
 
123
        switch (status) {
 
124
        case 0x00: /* success */
 
125
                return dbus_message_new_method_return(msg);
 
126
 
 
127
        case 0x04: /* page timeout */
 
128
        case 0x08: /* connection timeout */
 
129
        case 0x10: /* connection accept timeout */
 
130
        case 0x22: /* LMP response timeout */
 
131
        case 0x28: /* instant passed - is this a timeout? */
 
132
                return dbus_message_new_error(msg,
 
133
                                        ERROR_INTERFACE ".AuthenticationTimeout",
 
134
                                        "Authentication Timeout");
 
135
        case 0x17: /* too frequent pairing attempts */
 
136
                return dbus_message_new_error(msg,
 
137
                                        ERROR_INTERFACE ".RepeatedAttempts",
 
138
                                        "Repeated Attempts");
 
139
 
 
140
        case 0x06:
 
141
        case 0x18: /* pairing not allowed (e.g. gw rejected attempt) */
 
142
                return dbus_message_new_error(msg,
 
143
                                        ERROR_INTERFACE ".AuthenticationRejected",
 
144
                                        "Authentication Rejected");
 
145
 
 
146
        case 0x07: /* memory capacity */
 
147
        case 0x09: /* connection limit */
 
148
        case 0x0a: /* synchronous connection limit */
 
149
        case 0x0d: /* limited resources */
 
150
        case 0x13: /* user ended the connection */
 
151
        case 0x14: /* terminated due to low resources */
 
152
                return dbus_message_new_error(msg,
 
153
                                        ERROR_INTERFACE ".AuthenticationCanceled",
 
154
                                        "Authentication Canceled");
 
155
 
 
156
        case 0x05: /* authentication failure */
 
157
        case 0x0E: /* rejected due to security reasons - is this auth failure? */
 
158
        case 0x25: /* encryption mode not acceptable - is this auth failure? */
 
159
        case 0x26: /* link key cannot be changed - is this auth failure? */
 
160
        case 0x29: /* pairing with unit key unsupported - is this auth failure? */
 
161
        case 0x2f: /* insufficient security - is this auth failure? */
 
162
        default:
 
163
                return dbus_message_new_error(msg,
 
164
                                        ERROR_INTERFACE ".AuthenticationFailed",
 
165
                                        "Authentication Failed");
 
166
        }
 
167
}
 
168
 
 
169
/*****************************************************************
 
170
 *
 
171
 *  Section reserved to HCI commands confirmation handling and low
 
172
 *  level events(eg: device attached/dettached.
 
173
 *
 
174
 *****************************************************************/
 
175
 
 
176
static void pincode_cb(struct agent *agent, DBusError *err, const char *pincode,
 
177
                        struct btd_device *device)
 
178
{
 
179
        struct btd_adapter *adapter = device_get_adapter(device);
 
180
        pin_code_reply_cp pr;
 
181
        bdaddr_t sba, dba;
 
182
        size_t len;
 
183
        int dev;
 
184
        struct pending_auth_info *auth;
 
185
        uint16_t dev_id = adapter_get_dev_id(adapter);
 
186
        struct bonding_request_info *bonding = adapter_get_bonding_info(adapter);
 
187
 
 
188
        /* No need to reply anything if the authentication already failed */
 
189
        if (bonding && bonding->hci_status)
 
190
                return;
 
191
 
 
192
        dev = hci_open_dev(dev_id);
 
193
        if (dev < 0) {
 
194
                error("hci_open_dev(%d): %s (%d)", dev_id,
 
195
                                strerror(errno), errno);
 
196
                return;
 
197
        }
 
198
 
 
199
        adapter_get_address(adapter, &sba);
 
200
        device_get_address(device, &dba);
 
201
 
 
202
        auth = adapter_find_auth_request(adapter, &dba);
 
203
 
 
204
        if (err) {
 
205
                hci_send_cmd(dev, OGF_LINK_CTL,
 
206
                                OCF_PIN_CODE_NEG_REPLY, 6, &dba);
 
207
                goto done;
 
208
        }
 
209
 
 
210
        len = strlen(pincode);
 
211
 
 
212
        set_pin_length(&sba, len);
 
213
 
 
214
        memset(&pr, 0, sizeof(pr));
 
215
        bacpy(&pr.bdaddr, &dba);
 
216
        memcpy(pr.pin_code, pincode, len);
 
217
        pr.pin_len = len;
 
218
        hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_REPLY, PIN_CODE_REPLY_CP_SIZE, &pr);
 
219
 
 
220
done:
 
221
        if (auth) {
 
222
                auth->replied = TRUE;
 
223
                auth->agent = NULL;
 
224
        }
 
225
        hci_close_dev(dev);
 
226
}
 
227
 
 
228
int hcid_dbus_request_pin(int dev, bdaddr_t *sba, struct hci_conn_info *ci)
 
229
{
 
230
        char addr[18];
 
231
        struct btd_adapter *adapter;
 
232
        struct btd_device *device;
 
233
        struct agent *agent = NULL;
 
234
        int ret;
 
235
 
 
236
        adapter = manager_find_adapter(sba);
 
237
        if (!adapter) {
 
238
                error("No matching adapter found");
 
239
                return -1;
 
240
        }
 
241
 
 
242
        ba2str(&ci->bdaddr, addr);
 
243
 
 
244
        device = adapter_find_device(adapter, addr);
 
245
 
 
246
        if (device)
 
247
                agent = device_get_agent(device);
 
248
 
 
249
        if (!agent)
 
250
                agent = adapter_get_agent(adapter);
 
251
 
 
252
        if (!agent)
 
253
                return -EPERM;
 
254
 
 
255
        if (!device) {
 
256
                device = adapter_create_device(connection, adapter, addr);
 
257
                if (!device)
 
258
                        return -ENODEV;
 
259
        }
 
260
 
 
261
        ret = agent_request_pincode(agent, device,
 
262
                                        (agent_pincode_cb) pincode_cb,
 
263
                                        device);
 
264
        if (ret == 0) {
 
265
                struct pending_auth_info *auth;
 
266
                auth = adapter_new_auth_request(adapter, &ci->bdaddr,
 
267
                                                AUTH_TYPE_PINCODE);
 
268
                auth->agent = agent;
 
269
        }
 
270
 
 
271
        return ret;
 
272
}
 
273
 
 
274
static void confirm_cb(struct agent *agent, DBusError *err, void *user_data)
 
275
{
 
276
        struct btd_device *device = user_data;
 
277
        struct btd_adapter *adapter = device_get_adapter(device);
 
278
        user_confirm_reply_cp cp;
 
279
        int dd;
 
280
        struct pending_auth_info *auth;
 
281
        uint16_t dev_id = adapter_get_dev_id(adapter);
 
282
        struct bonding_request_info *bonding = adapter_get_bonding_info(adapter);
 
283
 
 
284
        /* No need to reply anything if the authentication already failed */
 
285
        if (bonding && bonding->hci_status)
 
286
                return;
 
287
 
 
288
        dd = hci_open_dev(dev_id);
 
289
        if (dd < 0) {
 
290
                error("Unable to open hci%d", dev_id);
 
291
                return;
 
292
        }
 
293
 
 
294
        memset(&cp, 0, sizeof(cp));
 
295
        device_get_address(device, &cp.bdaddr);
 
296
 
 
297
        auth = adapter_find_auth_request(adapter, &cp.bdaddr);
 
298
 
 
299
        if (err)
 
300
                hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,
 
301
                                        USER_CONFIRM_REPLY_CP_SIZE, &cp);
 
302
        else
 
303
                hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_REPLY,
 
304
                                        USER_CONFIRM_REPLY_CP_SIZE, &cp);
 
305
 
 
306
        if (auth) {
 
307
                auth->replied = TRUE;
 
308
                auth->agent = FALSE;
 
309
        }
 
310
 
 
311
        hci_close_dev(dd);
 
312
}
 
313
 
 
314
static void passkey_cb(struct agent *agent, DBusError *err, uint32_t passkey,
 
315
                        void *user_data)
 
316
{
 
317
        struct btd_device *device = user_data;
 
318
        struct btd_adapter *adapter = device_get_adapter(device);
 
319
        user_passkey_reply_cp cp;
 
320
        bdaddr_t dba;
 
321
        int dd;
 
322
        struct pending_auth_info *auth;
 
323
        uint16_t dev_id = adapter_get_dev_id(adapter);
 
324
        struct bonding_request_info *bonding = adapter_get_bonding_info(adapter);
 
325
 
 
326
        /* No need to reply anything if the authentication already failed */
 
327
        if (bonding && bonding->hci_status)
 
328
                return;
 
329
 
 
330
        dd = hci_open_dev(dev_id);
 
331
        if (dd < 0) {
 
332
                error("Unable to open hci%d", dev_id);
 
333
                return;
 
334
        }
 
335
 
 
336
        device_get_address(device, &dba);
 
337
 
 
338
        memset(&cp, 0, sizeof(cp));
 
339
        bacpy(&cp.bdaddr, &dba);
 
340
        cp.passkey = passkey;
 
341
 
 
342
        auth = adapter_find_auth_request(adapter, &dba);
 
343
 
 
344
        if (err)
 
345
                hci_send_cmd(dd, OGF_LINK_CTL,
 
346
                                OCF_USER_PASSKEY_NEG_REPLY, 6, &dba);
 
347
        else
 
348
                hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_PASSKEY_REPLY,
 
349
                                        USER_PASSKEY_REPLY_CP_SIZE, &cp);
 
350
 
 
351
        if (auth) {
 
352
                auth->replied = TRUE;
 
353
                auth->agent = NULL;
 
354
        }
 
355
 
 
356
        hci_close_dev(dd);
 
357
}
 
358
 
 
359
static int get_auth_requirements(bdaddr_t *local, bdaddr_t *remote,
 
360
                                                        uint8_t *auth)
 
361
{
 
362
        struct hci_auth_info_req req;
 
363
        char addr[18];
 
364
        int err, dd, dev_id;
 
365
 
 
366
        ba2str(local, addr);
 
367
 
 
368
        dev_id = hci_devid(addr);
 
369
        if (dev_id < 0)
 
370
                return dev_id;
 
371
 
 
372
        dd = hci_open_dev(dev_id);
 
373
        if (dd < 0)
 
374
                return dd;
 
375
 
 
376
        memset(&req, 0, sizeof(req));
 
377
        bacpy(&req.bdaddr, remote);
 
378
 
 
379
        err = ioctl(dd, HCIGETAUTHINFO, (unsigned long) &req);
 
380
        if (err < 0) {
 
381
                debug("HCIGETAUTHINFO failed: %s (%d)",
 
382
                                        strerror(errno), errno);
 
383
                hci_close_dev(dd);
 
384
                return err;
 
385
        }
 
386
 
 
387
        hci_close_dev(dd);
 
388
 
 
389
        if (auth)
 
390
                *auth = req.type;
 
391
 
 
392
        return 0;
 
393
}
 
394
 
 
395
int hcid_dbus_user_confirm(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
 
396
{
 
397
        struct btd_adapter *adapter;
 
398
        struct btd_device *device;
 
399
        struct agent *agent;
 
400
        char addr[18];
 
401
        uint8_t type;
 
402
        struct pending_auth_info *auth;
 
403
        uint16_t dev_id;
 
404
 
 
405
        adapter = manager_find_adapter(sba);
 
406
        if (!adapter) {
 
407
                error("No matching adapter found");
 
408
                return -1;
 
409
        }
 
410
 
 
411
        dev_id = adapter_get_dev_id(adapter);
 
412
 
 
413
        if (get_auth_requirements(sba, dba, &type) < 0) {
 
414
                int dd;
 
415
 
 
416
                dd = hci_open_dev(dev_id);
 
417
                if (dd < 0) {
 
418
                        error("Unable to open hci%d", dev_id);
 
419
                        return -1;
 
420
                }
 
421
 
 
422
                hci_send_cmd(dd, OGF_LINK_CTL,
 
423
                                        OCF_USER_CONFIRM_NEG_REPLY, 6, dba);
 
424
 
 
425
                hci_close_dev(dd);
 
426
 
 
427
                return 0;
 
428
        }
 
429
 
 
430
        ba2str(dba, addr);
 
431
 
 
432
        device = adapter_get_device(connection, adapter, addr);
 
433
        if (!device) {
 
434
                error("Device creation failed");
 
435
                return -1;
 
436
        }
 
437
 
 
438
        /* If no MITM protection required, auto-accept */
 
439
        if (!(device_get_auth(device) & 0x01) && !(type & 0x01)) {
 
440
                int dd;
 
441
 
 
442
                dd = hci_open_dev(dev_id);
 
443
                if (dd < 0) {
 
444
                        error("Unable to open hci%d", dev_id);
 
445
                        return -1;
 
446
                }
 
447
 
 
448
                hci_send_cmd(dd, OGF_LINK_CTL,
 
449
                                        OCF_USER_CONFIRM_REPLY, 6, dba);
 
450
 
 
451
                hci_close_dev(dd);
 
452
 
 
453
                return 0;
 
454
        }
 
455
 
 
456
        agent = device_get_agent(device);
 
457
 
 
458
        if (!agent)
 
459
                agent = adapter_get_agent(adapter);
 
460
 
 
461
        if (!agent) {
 
462
                error("No agent available for user confirm request");
 
463
                return -1;
 
464
        }
 
465
 
 
466
        if (agent_request_confirmation(agent, device, passkey,
 
467
                                                confirm_cb, device) < 0) {
 
468
                error("Requesting passkey failed");
 
469
                return -1;
 
470
        }
 
471
 
 
472
        auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_CONFIRM);
 
473
        auth->agent = agent;
 
474
 
 
475
        return 0;
 
476
}
 
477
 
 
478
int hcid_dbus_user_passkey(bdaddr_t *sba, bdaddr_t *dba)
 
479
{
 
480
        struct btd_adapter *adapter;
 
481
        struct btd_device *device;
 
482
        struct agent *agent = NULL;
 
483
        char addr[18];
 
484
        struct pending_auth_info *auth;
 
485
 
 
486
        adapter = manager_find_adapter(sba);
 
487
        if (!adapter) {
 
488
                error("No matching adapter found");
 
489
                return -1;
 
490
        }
 
491
 
 
492
        ba2str(dba, addr);
 
493
 
 
494
        device = adapter_get_device(connection, adapter, addr);
 
495
 
 
496
        if (device)
 
497
                agent = device_get_agent(device);
 
498
 
 
499
        if (!agent)
 
500
                agent = adapter_get_agent(adapter);
 
501
 
 
502
        if (!agent) {
 
503
                error("No agent available for user confirm request");
 
504
                return -1;
 
505
        }
 
506
 
 
507
        if (agent_request_passkey(agent, device, passkey_cb, device) < 0) {
 
508
                error("Requesting passkey failed");
 
509
                return -1;
 
510
        }
 
511
 
 
512
        auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_PASSKEY);
 
513
        auth->agent = agent;
 
514
 
 
515
        return 0;
 
516
}
 
517
 
 
518
int hcid_dbus_user_notify(bdaddr_t *sba, bdaddr_t *dba, uint32_t passkey)
 
519
{
 
520
        struct btd_adapter *adapter;
 
521
        struct btd_device *device;
 
522
        struct agent *agent = NULL;
 
523
        char addr[18];
 
524
        struct pending_auth_info *auth;
 
525
 
 
526
        adapter = manager_find_adapter(sba);
 
527
        if (!adapter) {
 
528
                error("No matching adapter found");
 
529
                return -1;
 
530
        }
 
531
 
 
532
        ba2str(dba, addr);
 
533
 
 
534
        device = adapter_get_device(connection, adapter, addr);
 
535
        if (device)
 
536
                agent = device_get_agent(device);
 
537
 
 
538
        if (!agent)
 
539
                agent = adapter_get_agent(adapter);
 
540
 
 
541
        if (!agent) {
 
542
                error("No agent available for user confirm request");
 
543
                return -1;
 
544
        }
 
545
 
 
546
        if (agent_display_passkey(agent, device, passkey) < 0) {
 
547
                error("Displaying passkey failed");
 
548
                return -1;
 
549
        }
 
550
 
 
551
        auth = adapter_new_auth_request(adapter, dba, AUTH_TYPE_NOTIFY);
 
552
        auth->agent = agent;
 
553
 
 
554
        return 0;
 
555
}
 
556
 
 
557
void hcid_dbus_bonding_process_complete(bdaddr_t *local, bdaddr_t *peer,
 
558
                                        uint8_t status)
 
559
{
 
560
        struct btd_adapter *adapter;
 
561
        char peer_addr[18];
 
562
        DBusMessage *reply;
 
563
        struct btd_device *device;
 
564
        struct bonding_request_info *bonding;
 
565
        struct pending_auth_info *auth;
 
566
 
 
567
        debug("hcid_dbus_bonding_process_complete: status=%02x", status);
 
568
 
 
569
        ba2str(peer, peer_addr);
 
570
 
 
571
        adapter = manager_find_adapter(local);
 
572
        if (!adapter) {
 
573
                error("Unable to find matching adapter");
 
574
                return;
 
575
        }
 
576
 
 
577
        bonding = adapter_get_bonding_info(adapter);
 
578
 
 
579
        if (bonding && bacmp(&bonding->bdaddr, peer))
 
580
                bonding = NULL;
 
581
 
 
582
        if (status == 0) {
 
583
                device = adapter_get_device(connection, adapter, peer_addr);
 
584
                if (!device) {
 
585
                        /* This should really only happen if we run out of
 
586
                         * memory */
 
587
                        error("Unable to get device object!");
 
588
                        status = HCI_REJECTED_LIMITED_RESOURCES;
 
589
                }
 
590
        }
 
591
 
 
592
        if (status && bonding)
 
593
                bonding->hci_status = status;
 
594
 
 
595
        auth = adapter_find_auth_request(adapter, peer);
 
596
        if (!auth) {
 
597
                /* This means that there was no pending PIN or SSP token request
 
598
                 * from the controller, i.e. this is not a new pairing */
 
599
                debug("hcid_dbus_bonding_process_complete: no pending auth request");
 
600
                goto proceed;
 
601
        }
 
602
 
 
603
        if (auth->agent)
 
604
                agent_cancel(auth->agent);
 
605
 
 
606
        adapter_remove_auth_request(adapter, peer);
 
607
 
 
608
        /* If this is a new pairing send the appropriate signal for it
 
609
         * and proceed with service discovery */
 
610
        if (status == 0) {
 
611
                if (device_set_paired(connection, device, bonding) && bonding)
 
612
                        adapter_free_bonding_request(adapter);
 
613
                return;
 
614
        }
 
615
 
 
616
proceed:
 
617
        if (!bonding)
 
618
                return; /* skip: no bonding req pending */
 
619
 
 
620
        if (bonding->cancel)
 
621
                reply = new_authentication_return(bonding->msg,
 
622
                                        HCI_OE_USER_ENDED_CONNECTION);
 
623
        else
 
624
                reply = new_authentication_return(bonding->msg, status);
 
625
 
 
626
        g_dbus_send_message(connection, reply);
 
627
 
 
628
        adapter_free_bonding_request(adapter);
 
629
}
 
630
 
 
631
void hcid_dbus_inquiry_start(bdaddr_t *local)
 
632
{
 
633
        struct btd_adapter *adapter;
 
634
        int state;
 
635
 
 
636
        adapter = manager_find_adapter(local);
 
637
        if (!adapter) {
 
638
                error("Unable to find matching adapter");
 
639
                return;
 
640
        }
 
641
 
 
642
        state = adapter_get_state(adapter);
 
643
        state |= STD_INQUIRY;
 
644
        adapter_set_state(adapter, state);
 
645
        /*
 
646
         * Cancel pending remote name request and clean the device list
 
647
         * when inquiry is supported in periodic inquiry idle state.
 
648
         */
 
649
        if (adapter_get_state(adapter) & PERIODIC_INQUIRY)
 
650
                pending_remote_name_cancel(adapter);
 
651
 
 
652
        /* Disable name resolution for non D-Bus clients */
 
653
        if (!adapter_has_discov_sessions(adapter)) {
 
654
                state = adapter_get_state(adapter);
 
655
                state &= ~RESOLVE_NAME;
 
656
                adapter_set_state(adapter, state);
 
657
        }
 
658
}
 
659
 
 
660
static int found_device_req_name(struct btd_adapter *adapter)
 
661
{
 
662
        struct hci_request rq;
 
663
        evt_cmd_status rp;
 
664
        remote_name_req_cp cp;
 
665
        struct remote_dev_info *dev, match;
 
666
        int dd, req_sent = 0;
 
667
        uint16_t dev_id = adapter_get_dev_id(adapter);
 
668
 
 
669
        memset(&match, 0, sizeof(struct remote_dev_info));
 
670
        bacpy(&match.bdaddr, BDADDR_ANY);
 
671
        match.name_status = NAME_REQUIRED;
 
672
 
 
673
        dev = adapter_search_found_devices(adapter, &match);
 
674
        if (!dev)
 
675
                return -ENODATA;
 
676
 
 
677
        dd = hci_open_dev(dev_id);
 
678
        if (dd < 0)
 
679
                return -errno;
 
680
 
 
681
        memset(&rq, 0, sizeof(rq));
 
682
        rq.ogf    = OGF_LINK_CTL;
 
683
        rq.ocf    = OCF_REMOTE_NAME_REQ;
 
684
        rq.cparam = &cp;
 
685
        rq.clen   = REMOTE_NAME_REQ_CP_SIZE;
 
686
        rq.rparam = &rp;
 
687
        rq.rlen   = EVT_CMD_STATUS_SIZE;
 
688
        rq.event  = EVT_CMD_STATUS;
 
689
 
 
690
        /* send at least one request or return failed if the list is empty */
 
691
        do {
 
692
                /* flag to indicate the current remote name requested */
 
693
                dev->name_status = NAME_REQUESTED;
 
694
 
 
695
                memset(&rp, 0, sizeof(rp));
 
696
                memset(&cp, 0, sizeof(cp));
 
697
                bacpy(&cp.bdaddr, &dev->bdaddr);
 
698
                cp.pscan_rep_mode = 0x02;
 
699
 
 
700
                if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0)
 
701
                        error("Unable to send the HCI remote name request: %s (%d)",
 
702
                                                strerror(errno), errno);
 
703
 
 
704
                if (!rp.status) {
 
705
                        req_sent = 1;
 
706
                        break;
 
707
                }
 
708
 
 
709
                error("Remote name request failed with status 0x%02x",
 
710
                                                                rp.status);
 
711
 
 
712
                /* if failed, request the next element */
 
713
                /* remove the element from the list */
 
714
                adapter_remove_found_device(adapter, &dev->bdaddr);
 
715
 
 
716
                /* get the next element */
 
717
                dev = adapter_search_found_devices(adapter, &match);
 
718
        } while (dev);
 
719
 
 
720
        hci_close_dev(dd);
 
721
 
 
722
        if (!req_sent)
 
723
                return -ENODATA;
 
724
 
 
725
        return 0;
 
726
}
 
727
 
 
728
void hcid_dbus_inquiry_complete(bdaddr_t *local)
 
729
{
 
730
        struct btd_adapter *adapter;
 
731
        const gchar *path;
 
732
        int state;
 
733
 
 
734
        adapter = manager_find_adapter(local);
 
735
        if (!adapter) {
 
736
                error("Unable to find matching adapter");
 
737
                return;
 
738
        }
 
739
 
 
740
        path = adapter_get_path(adapter);
 
741
 
 
742
        /* Out of range verification */
 
743
        if ((adapter_get_state(adapter) & PERIODIC_INQUIRY) &&
 
744
                                !(adapter_get_state(adapter) & STD_INQUIRY))
 
745
                adapter_update_oor_devices(adapter);
 
746
 
 
747
        /*
 
748
         * The following scenarios can happen:
 
749
         * 1. standard inquiry: always send discovery completed signal
 
750
         * 2. standard inquiry + name resolving: send discovery completed
 
751
         *    after name resolving
 
752
         * 3. periodic inquiry: skip discovery completed signal
 
753
         * 4. periodic inquiry + standard inquiry: always send discovery
 
754
         *    completed signal
 
755
         *
 
756
         * Keep in mind that non D-Bus requests can arrive.
 
757
         */
 
758
        if (found_device_req_name(adapter) == 0)
 
759
                return;
 
760
 
 
761
        /* reset the discover type to be able to handle D-Bus and non D-Bus
 
762
         * requests */
 
763
        state = adapter_get_state(adapter);
 
764
        state &= ~STD_INQUIRY;
 
765
        state &= ~PERIODIC_INQUIRY;
 
766
        adapter_set_state(adapter, state);
 
767
}
 
768
 
 
769
void hcid_dbus_periodic_inquiry_start(bdaddr_t *local, uint8_t status)
 
770
{
 
771
        struct btd_adapter *adapter;
 
772
        int state;
 
773
 
 
774
        /* Don't send the signal if the cmd failed */
 
775
        if (status)
 
776
                return;
 
777
 
 
778
        adapter = manager_find_adapter(local);
 
779
        if (!adapter) {
 
780
                error("No matching adapter found");
 
781
                return;
 
782
        }
 
783
 
 
784
        state = adapter_get_state(adapter);
 
785
        state |= PERIODIC_INQUIRY;
 
786
        adapter_set_state(adapter, state);
 
787
}
 
788
 
 
789
void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status)
 
790
{
 
791
        struct btd_adapter *adapter;
 
792
        int state;
 
793
 
 
794
        /* Don't send the signal if the cmd failed */
 
795
        if (status)
 
796
                return;
 
797
 
 
798
        adapter = manager_find_adapter(local);
 
799
        if (!adapter) {
 
800
                error("No matching adapter found");
 
801
                return;
 
802
        }
 
803
 
 
804
        /* reset the discover type to be able to handle D-Bus and non D-Bus
 
805
         * requests */
 
806
        state = adapter_get_state(adapter);
 
807
        state &= ~PERIODIC_INQUIRY;
 
808
        adapter_set_state(adapter, state);
 
809
}
 
810
 
 
811
static char *extract_eir_name(uint8_t *data, uint8_t *type)
 
812
{
 
813
        if (!data || !type)
 
814
                return NULL;
 
815
 
 
816
        if (data[0] == 0)
 
817
                return NULL;
 
818
 
 
819
        *type = data[1];
 
820
 
 
821
        switch (*type) {
 
822
        case 0x08:
 
823
        case 0x09:
 
824
                return strndup((char *) (data + 2), data[0] - 1);
 
825
        }
 
826
 
 
827
        return NULL;
 
828
}
 
829
 
 
830
static void append_dict_valist(DBusMessageIter *iter,
 
831
                                        const char *first_key,
 
832
                                        va_list var_args)
 
833
{
 
834
        DBusMessageIter dict;
 
835
        const char *key;
 
836
        int type;
 
837
        void *val;
 
838
 
 
839
        dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
 
840
                        DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
 
841
                        DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
 
842
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
843
 
 
844
        key = first_key;
 
845
        while (key) {
 
846
                type = va_arg(var_args, int);
 
847
                val = va_arg(var_args, void *);
 
848
                dbus_message_iter_append_dict_entry(&dict, key, type, val);
 
849
                key = va_arg(var_args, char *);
 
850
        }
 
851
 
 
852
        dbus_message_iter_close_container(iter, &dict);
 
853
}
 
854
 
 
855
static void emit_device_found(const char *path, const char *address,
 
856
                                const char *first_key, ...)
 
857
{
 
858
        DBusMessage *signal;
 
859
        DBusMessageIter iter;
 
860
        va_list var_args;
 
861
 
 
862
        signal = dbus_message_new_signal(path, ADAPTER_INTERFACE,
 
863
                                        "DeviceFound");
 
864
        if (!signal) {
 
865
                error("Unable to allocate new %s.DeviceFound signal",
 
866
                                ADAPTER_INTERFACE);
 
867
                return;
 
868
        }
 
869
        dbus_message_iter_init_append(signal, &iter);
 
870
        dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &address);
 
871
 
 
872
        va_start(var_args, first_key);
 
873
        append_dict_valist(&iter, first_key, var_args);
 
874
        va_end(var_args);
 
875
 
 
876
        dbus_connection_send(connection, signal, NULL);
 
877
 
 
878
        dbus_message_unref(signal);
 
879
}
 
880
 
 
881
void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class,
 
882
                                int8_t rssi, uint8_t *data)
 
883
{
 
884
        char filename[PATH_MAX + 1];
 
885
        struct btd_adapter *adapter;
 
886
        char local_addr[18], peer_addr[18], *alias, *name, *tmp_name;
 
887
        const char *path, *icon, *paddr = peer_addr;
 
888
        struct remote_dev_info *dev, match;
 
889
        dbus_int16_t tmp_rssi = rssi;
 
890
        uint8_t name_type = 0x00;
 
891
        name_status_t name_status;
 
892
        int state;
 
893
 
 
894
        ba2str(local, local_addr);
 
895
        ba2str(peer, peer_addr);
 
896
 
 
897
        adapter = manager_find_adapter(local);
 
898
        if (!adapter) {
 
899
                error("No matching adapter found");
 
900
                return;
 
901
        }
 
902
 
 
903
        write_remote_class(local, peer, class);
 
904
 
 
905
        if (data)
 
906
                write_remote_eir(local, peer, data);
 
907
 
 
908
        /*
 
909
         * workaround to identify situation when the daemon started and
 
910
         * a standard inquiry or periodic inquiry was already running
 
911
         */
 
912
        if (!(adapter_get_state(adapter) & STD_INQUIRY) &&
 
913
                        !(adapter_get_state(adapter) & PERIODIC_INQUIRY)) {
 
914
                state = adapter_get_state(adapter);
 
915
                state |= PERIODIC_INQUIRY;
 
916
                adapter_set_state(adapter, state);
 
917
        }
 
918
        /* Out of range list update */
 
919
        if (adapter_get_state(adapter) & PERIODIC_INQUIRY)
 
920
                adapter_remove_oor_device(adapter, peer_addr);
 
921
 
 
922
        memset(&match, 0, sizeof(struct remote_dev_info));
 
923
        bacpy(&match.bdaddr, peer);
 
924
        match.name_status = NAME_SENT;
 
925
        /* if found: don't send the name again */
 
926
        dev = adapter_search_found_devices(adapter, &match);
 
927
        if (dev)
 
928
                return;
 
929
 
 
930
        /* the inquiry result can be triggered by NON D-Bus client */
 
931
        if (adapter_get_state(adapter) & RESOLVE_NAME)
 
932
                name_status = NAME_REQUIRED;
 
933
        else
 
934
                name_status = NAME_NOT_REQUIRED;
 
935
 
 
936
        create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "aliases");
 
937
        alias = textfile_get(filename, peer_addr);
 
938
 
 
939
        create_name(filename, PATH_MAX, STORAGEDIR, local_addr, "names");
 
940
        name = textfile_get(filename, peer_addr);
 
941
 
 
942
        if (!alias) {
 
943
                if (!name) {
 
944
                        alias = g_strdup(peer_addr);
 
945
                        g_strdelimit(alias, ":", '-');
 
946
                } else
 
947
                        alias = g_strdup(name);
 
948
        }
 
949
 
 
950
        tmp_name = extract_eir_name(data, &name_type);
 
951
        if (tmp_name) {
 
952
                if (name_type == 0x09) {
 
953
                        write_device_name(local, peer, tmp_name);
 
954
                        name_status = NAME_NOT_REQUIRED;
 
955
 
 
956
                        if (name)
 
957
                                g_free(name);
 
958
 
 
959
                        name = tmp_name;
 
960
                } else {
 
961
                        if (name)
 
962
                                free(tmp_name);
 
963
                        else
 
964
                                name = tmp_name;
 
965
                }
 
966
        }
 
967
 
 
968
        path = adapter_get_path(adapter);
 
969
        icon = class_to_icon(class);
 
970
 
 
971
        if (name) {
 
972
                if (name_type != 0x08)
 
973
                        name_status = NAME_SENT;
 
974
 
 
975
                emit_device_found(path, paddr,
 
976
                                "Address", DBUS_TYPE_STRING, &paddr,
 
977
                                "Class", DBUS_TYPE_UINT32, &class,
 
978
                                "Icon", DBUS_TYPE_STRING, &icon,
 
979
                                "RSSI", DBUS_TYPE_INT16, &tmp_rssi,
 
980
                                "Name", DBUS_TYPE_STRING, &name,
 
981
                                "Alias", DBUS_TYPE_STRING, &alias, NULL);
 
982
 
 
983
                g_free(name);
 
984
        } else {
 
985
                emit_device_found(path, paddr,
 
986
                                "Address", DBUS_TYPE_STRING, &paddr,
 
987
                                "Class", DBUS_TYPE_UINT32, &class,
 
988
                                "Icon", DBUS_TYPE_STRING, &icon,
 
989
                                "RSSI", DBUS_TYPE_INT16, &tmp_rssi,
 
990
                                "Alias", DBUS_TYPE_STRING, &alias, NULL);
 
991
        }
 
992
 
 
993
        g_free(alias);
 
994
 
 
995
        /* add in the list to track name sent/pending */
 
996
        adapter_add_found_device(adapter, peer, rssi, name_status);
 
997
}
 
998
 
 
999
void hcid_dbus_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
 
1000
{
 
1001
        char peer_addr[18];
 
1002
        const char *paddr = peer_addr;
 
1003
        uint32_t old_class = 0;
 
1004
        struct btd_adapter *adapter;
 
1005
        struct btd_device *device;
 
1006
        const gchar *dev_path;
 
1007
 
 
1008
        read_remote_class(local, peer, &old_class);
 
1009
 
 
1010
        if (old_class == class)
 
1011
                return;
 
1012
 
 
1013
        adapter = manager_find_adapter(local);
 
1014
        if (!adapter) {
 
1015
                error("No matching adapter found");
 
1016
                return;
 
1017
        }
 
1018
 
 
1019
        ba2str(peer, peer_addr);
 
1020
        device = adapter_find_device(adapter, paddr);
 
1021
 
 
1022
        if (!device)
 
1023
                return;
 
1024
 
 
1025
        dev_path = device_get_path(device);
 
1026
 
 
1027
        dbus_connection_emit_property_changed(connection, dev_path,
 
1028
                                DEVICE_INTERFACE, "Class",
 
1029
                                DBUS_TYPE_UINT32, &class);
 
1030
}
 
1031
 
 
1032
void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status,
 
1033
                                char *name)
 
1034
{
 
1035
        struct btd_adapter *adapter;
 
1036
        char srcaddr[18], dstaddr[18];
 
1037
        const gchar *dev_path;
 
1038
        int state;
 
1039
 
 
1040
        adapter = manager_find_adapter(local);
 
1041
        if (!adapter) {
 
1042
                error("No matching adapter found");
 
1043
                return;
 
1044
        }
 
1045
 
 
1046
        ba2str(local, srcaddr);
 
1047
        ba2str(peer, dstaddr);
 
1048
 
 
1049
        if (!status) {
 
1050
                struct btd_device *device;
 
1051
 
 
1052
                device = adapter_find_device(adapter, dstaddr);
 
1053
                if (device) {
 
1054
                        char alias[248];
 
1055
 
 
1056
                        dev_path = device_get_path(device);
 
1057
 
 
1058
                        dbus_connection_emit_property_changed(connection,
 
1059
                                                dev_path, DEVICE_INTERFACE,
 
1060
                                                "Name", DBUS_TYPE_STRING, &name);
 
1061
 
 
1062
                        if (read_device_alias(srcaddr, dstaddr,
 
1063
                                                alias, sizeof(alias)) < 1) {
 
1064
 
 
1065
                                dbus_connection_emit_property_changed(connection,
 
1066
                                                dev_path, DEVICE_INTERFACE,
 
1067
                                                "Alias", DBUS_TYPE_STRING, &name);
 
1068
                        }
 
1069
                }
 
1070
        }
 
1071
 
 
1072
        /* remove from remote name request list */
 
1073
        adapter_remove_found_device(adapter, peer);
 
1074
 
 
1075
        /* check if there is more devices to request names */
 
1076
        if (found_device_req_name(adapter) == 0)
 
1077
                return;
 
1078
 
 
1079
        state = adapter_get_state(adapter);
 
1080
        state &= ~PERIODIC_INQUIRY;
 
1081
        state &= ~STD_INQUIRY;
 
1082
        adapter_set_state(adapter, state);
 
1083
}
 
1084
 
 
1085
void hcid_dbus_link_key_notify(bdaddr_t *local, bdaddr_t *peer)
 
1086
{
 
1087
        char peer_addr[18];
 
1088
        struct btd_device *device;
 
1089
        struct btd_adapter *adapter;
 
1090
        struct bonding_request_info *bonding;
 
1091
 
 
1092
        adapter = manager_find_adapter(local);
 
1093
        if (!adapter) {
 
1094
                error("No matching adapter found");
 
1095
                return;
 
1096
        }
 
1097
 
 
1098
        ba2str(peer, peer_addr);
 
1099
 
 
1100
        device = adapter_find_device(adapter, peer_addr);
 
1101
 
 
1102
        bonding = adapter_get_bonding_info(adapter);
 
1103
 
 
1104
        if (!device_get_connected(device))
 
1105
                device_set_secmode3_conn(device, TRUE);
 
1106
        else if (!bonding)
 
1107
                hcid_dbus_bonding_process_complete(local, peer, 0);
 
1108
}
 
1109
 
 
1110
void hcid_dbus_conn_complete(bdaddr_t *local, uint8_t status, uint16_t handle,
 
1111
                                bdaddr_t *peer)
 
1112
{
 
1113
        char peer_addr[18];
 
1114
        struct btd_adapter *adapter;
 
1115
        struct bonding_request_info *bonding;
 
1116
        struct btd_device *device;
 
1117
 
 
1118
        adapter = manager_find_adapter(local);
 
1119
        if (!adapter) {
 
1120
                error("No matching adapter found");
 
1121
                return;
 
1122
        }
 
1123
 
 
1124
        ba2str(peer, peer_addr);
 
1125
 
 
1126
        device = adapter_get_device(connection, adapter, peer_addr);
 
1127
 
 
1128
        if (status) {
 
1129
                struct pending_auth_info *auth;
 
1130
 
 
1131
                auth = adapter_find_auth_request(adapter, peer);
 
1132
                if (auth && auth->agent)
 
1133
                        agent_cancel(auth->agent);
 
1134
 
 
1135
                adapter_remove_auth_request(adapter, peer);
 
1136
 
 
1137
                if (device)
 
1138
                        device_set_secmode3_conn(device, FALSE);
 
1139
 
 
1140
                bonding = adapter_get_bonding_info(adapter);
 
1141
                if (bonding)
 
1142
                        bonding->hci_status = status;
 
1143
        } else {
 
1144
                if (device)
 
1145
                        device_set_connected(connection, device, TRUE);
 
1146
 
 
1147
                /* add in the active connetions list */
 
1148
                adapter_add_active_conn(adapter, peer, handle);
 
1149
        }
 
1150
}
 
1151
 
 
1152
void hcid_dbus_disconn_complete(bdaddr_t *local, uint8_t status,
 
1153
                                uint16_t handle, uint8_t reason)
 
1154
{
 
1155
        DBusMessage *reply;
 
1156
        char peer_addr[18];
 
1157
        struct btd_adapter *adapter;
 
1158
        struct btd_device *device;
 
1159
        struct active_conn_info *dev;
 
1160
        struct pending_auth_info *auth;
 
1161
        uint16_t dev_id;
 
1162
        struct bonding_request_info *bonding;
 
1163
 
 
1164
        if (status) {
 
1165
                error("Disconnection failed: 0x%02x", status);
 
1166
                return;
 
1167
        }
 
1168
 
 
1169
        adapter = manager_find_adapter(local);
 
1170
        if (!adapter) {
 
1171
                error("No matching adapter found");
 
1172
                return;
 
1173
        }
 
1174
 
 
1175
        dev = adapter_search_active_conn_by_handle(adapter, handle);
 
1176
        if (!dev) {
 
1177
                error("No matching connection for handle %u", handle);
 
1178
                return;
 
1179
        }
 
1180
 
 
1181
        ba2str(&dev->bdaddr, peer_addr);
 
1182
 
 
1183
        dev_id = adapter_get_dev_id(adapter);
 
1184
 
 
1185
        /* clean pending HCI cmds */
 
1186
        hci_req_queue_remove(dev_id, &dev->bdaddr);
 
1187
 
 
1188
        /* Cancel D-Bus/non D-Bus requests */
 
1189
        auth = adapter_find_auth_request(adapter, &dev->bdaddr);
 
1190
        if (auth && auth->agent)
 
1191
                agent_cancel(auth->agent);
 
1192
 
 
1193
        adapter_remove_auth_request(adapter, &dev->bdaddr);
 
1194
 
 
1195
        bonding = adapter_get_bonding_info(adapter);
 
1196
        /* Check if there is a pending Bonding request */
 
1197
        if (bonding && (bacmp(&bonding->bdaddr, &dev->bdaddr) == 0)) {
 
1198
                if (bonding->cancel) {
 
1199
                        /* reply authentication canceled */
 
1200
                        reply = new_authentication_return(bonding->msg,
 
1201
                                                        HCI_OE_USER_ENDED_CONNECTION);
 
1202
                        g_dbus_send_message(connection, reply);
 
1203
                } else {
 
1204
                        reply = new_authentication_return(bonding->msg,
 
1205
                                                        HCI_AUTHENTICATION_FAILURE);
 
1206
                        dbus_connection_send(connection, reply, NULL);
 
1207
                        dbus_message_unref(reply);
 
1208
                }
 
1209
                adapter_free_bonding_request(adapter);
 
1210
        }
 
1211
 
 
1212
        adapter_remove_active_conn(adapter, dev);
 
1213
 
 
1214
        device = adapter_find_device(adapter, peer_addr);
 
1215
        if (device) {
 
1216
                device_set_connected(connection, device, FALSE);
 
1217
 
 
1218
                if (device_is_temporary(device)) {
 
1219
                        debug("Removing temporary device %s", peer_addr);
 
1220
                        adapter_remove_device(connection, adapter, device);
 
1221
                }
 
1222
        }
 
1223
}
 
1224
 
 
1225
int set_limited_discoverable(int dd, const uint8_t *cls, gboolean limited)
 
1226
{
 
1227
        uint32_t dev_class;
 
1228
        int num = (limited ? 2 : 1);
 
1229
        uint8_t lap[] = { 0x33, 0x8b, 0x9e, 0x00, 0x8b, 0x9e };
 
1230
        /*
 
1231
         * 1: giac
 
1232
         * 2: giac + liac
 
1233
         */
 
1234
        if (hci_write_current_iac_lap(dd, num, lap, HCI_REQ_TIMEOUT) < 0) {
 
1235
                int err = errno;
 
1236
                error("Can't write current IAC LAP: %s(%d)",
 
1237
                                strerror(err), err);
 
1238
                return -err;
 
1239
        }
 
1240
 
 
1241
        if (limited) {
 
1242
                if (cls[1] & 0x20)
 
1243
                        return 0; /* Already limited */
 
1244
 
 
1245
                dev_class = (cls[2] << 16) | ((cls[1] | 0x20) << 8) | cls[0];
 
1246
        } else {
 
1247
                if (!(cls[1] & 0x20))
 
1248
                        return 0; /* Already clear */
 
1249
 
 
1250
                dev_class = (cls[2] << 16) | ((cls[1] & 0xdf) << 8) | cls[0];
 
1251
        }
 
1252
 
 
1253
        if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
 
1254
                int err = errno;
 
1255
                error("Can't write class of device: %s (%d)",
 
1256
                                                        strerror(err), err);
 
1257
                return -err;
 
1258
        }
 
1259
 
 
1260
        return 0;
 
1261
}
 
1262
 
 
1263
int set_service_classes(int dd, const uint8_t *cls, uint8_t value)
 
1264
{
 
1265
        uint32_t dev_class;
 
1266
 
 
1267
        if (cls[2] == value)
 
1268
                return 0; /* Already set */
 
1269
 
 
1270
        dev_class = (value << 16) | (cls[1] << 8) | cls[0];
 
1271
 
 
1272
        if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
 
1273
                int err = errno;
 
1274
                error("Can't write class of device: %s (%d)",
 
1275
                                                        strerror(err), err);
 
1276
                return -err;
 
1277
        }
 
1278
 
 
1279
        return 0;
 
1280
}
 
1281
 
 
1282
int set_major_class(int dd, const uint8_t *cls, uint8_t major)
 
1283
{
 
1284
        uint32_t dev_class;
 
1285
 
 
1286
        dev_class = (cls[2] << 16) | ((cls[1] & 0x20) << 8) |
 
1287
                                                ((major & 0xdf) << 8) | 0x00;
 
1288
 
 
1289
        if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
 
1290
                int err = errno;
 
1291
                error("Can't write class of device: %s (%d)",
 
1292
                                                        strerror(err), err);
 
1293
                return -err;
 
1294
        }
 
1295
 
 
1296
        return 0;
 
1297
}
 
1298
 
 
1299
int set_minor_class(int dd, const uint8_t *cls, uint8_t minor)
 
1300
{
 
1301
        uint32_t dev_class;
 
1302
 
 
1303
        dev_class = (cls[2] << 16) | (cls[1] << 8) | minor;
 
1304
 
 
1305
        if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
 
1306
                int err = errno;
 
1307
                error("Can't write class of device: %s (%d)",
 
1308
                                                        strerror(err), err);
 
1309
                return -err;
 
1310
        }
 
1311
 
 
1312
        return 0;
 
1313
}
 
1314
 
 
1315
int set_major_and_minor_class(int dd, const uint8_t *cls,
 
1316
                                                uint8_t major, uint8_t minor)
 
1317
{
 
1318
        uint32_t dev_class;
 
1319
 
 
1320
        dev_class = (cls[2] << 16) | ((cls[1] & 0x20) << 8) |
 
1321
                                                ((major & 0xdf) << 8) | minor;
 
1322
 
 
1323
        if (hci_write_class_of_dev(dd, dev_class, HCI_REQ_TIMEOUT) < 0) {
 
1324
                int err = errno;
 
1325
                error("Can't write class of device: %s (%d)",
 
1326
                                                        strerror(err), err);
 
1327
                return -err;
 
1328
        }
 
1329
 
 
1330
        return 0;
 
1331
}
 
1332
 
 
1333
/* Section reserved to device HCI callbacks */
 
1334
 
 
1335
void hcid_dbus_setname_complete(bdaddr_t *local)
 
1336
{
 
1337
        int id, dd = -1;
 
1338
        read_local_name_rp rp;
 
1339
        struct hci_request rq;
 
1340
        const char *pname = (char *) rp.name;
 
1341
        char local_addr[18], name[249];
 
1342
 
 
1343
        ba2str(local, local_addr);
 
1344
 
 
1345
        id = hci_devid(local_addr);
 
1346
        if (id < 0) {
 
1347
                error("No matching device id for %s", local_addr);
 
1348
                return;
 
1349
        }
 
1350
 
 
1351
        dd = hci_open_dev(id);
 
1352
        if (dd < 0) {
 
1353
                error("HCI device open failed: hci%d", id);
 
1354
                memset(&rp, 0, sizeof(rp));
 
1355
        } else {
 
1356
                memset(&rq, 0, sizeof(rq));
 
1357
                rq.ogf    = OGF_HOST_CTL;
 
1358
                rq.ocf    = OCF_READ_LOCAL_NAME;
 
1359
                rq.rparam = &rp;
 
1360
                rq.rlen   = READ_LOCAL_NAME_RP_SIZE;
 
1361
                rq.event  = EVT_CMD_COMPLETE;
 
1362
 
 
1363
                if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {
 
1364
                        error("Sending getting name command failed: %s (%d)",
 
1365
                                                strerror(errno), errno);
 
1366
                        rp.name[0] = '\0';
 
1367
                } else if (rp.status) {
 
1368
                        error("Getting name failed with status 0x%02x",
 
1369
                                        rp.status);
 
1370
                        rp.name[0] = '\0';
 
1371
                }
 
1372
                hci_close_dev(dd);
 
1373
        }
 
1374
 
 
1375
        strncpy(name, pname, sizeof(name) - 1);
 
1376
        name[248] = '\0';
 
1377
        pname = name;
 
1378
}
 
1379
 
 
1380
void hcid_dbus_setscan_enable_complete(bdaddr_t *local)
 
1381
{
 
1382
        struct btd_adapter *adapter;
 
1383
        read_scan_enable_rp rp;
 
1384
        struct hci_request rq;
 
1385
        int dd = -1;
 
1386
        uint16_t dev_id;
 
1387
 
 
1388
        adapter = manager_find_adapter(local);
 
1389
        if (!adapter) {
 
1390
                error("No matching adapter found");
 
1391
                return;
 
1392
        }
 
1393
 
 
1394
        dev_id = adapter_get_dev_id(adapter);
 
1395
 
 
1396
        dd = hci_open_dev(dev_id);
 
1397
        if (dd < 0) {
 
1398
                error("HCI device open failed: hci%d", dev_id);
 
1399
                return;
 
1400
        }
 
1401
 
 
1402
        memset(&rq, 0, sizeof(rq));
 
1403
        rq.ogf    = OGF_HOST_CTL;
 
1404
        rq.ocf    = OCF_READ_SCAN_ENABLE;
 
1405
        rq.rparam = &rp;
 
1406
        rq.rlen   = READ_SCAN_ENABLE_RP_SIZE;
 
1407
        rq.event  = EVT_CMD_COMPLETE;
 
1408
 
 
1409
        if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {
 
1410
                error("Sending read scan enable command failed: %s (%d)",
 
1411
                                strerror(errno), errno);
 
1412
                goto failed;
 
1413
        }
 
1414
 
 
1415
        if (rp.status) {
 
1416
                error("Getting scan enable failed with status 0x%02x",
 
1417
                                rp.status);
 
1418
                goto failed;
 
1419
        }
 
1420
 
 
1421
        adapter_remove_discov_timeout(adapter);
 
1422
 
 
1423
        if (adapter_get_scan_mode(adapter) != rp.enable)
 
1424
                adapter_mode_changed(adapter, rp.enable);
 
1425
 
 
1426
failed:
 
1427
        if (dd >= 0)
 
1428
                hci_close_dev(dd);
 
1429
}
 
1430
 
 
1431
void hcid_dbus_write_class_complete(bdaddr_t *local)
 
1432
{
 
1433
        struct btd_adapter *adapter;
 
1434
        int dd;
 
1435
        uint8_t cls[3];
 
1436
        uint16_t dev_id;
 
1437
 
 
1438
        adapter = manager_find_adapter(local);
 
1439
        if (!adapter) {
 
1440
                error("No matching adapter found");
 
1441
                return;
 
1442
        }
 
1443
 
 
1444
        dev_id = adapter_get_dev_id(adapter);
 
1445
 
 
1446
        dd = hci_open_dev(dev_id);
 
1447
        if (dd < 0) {
 
1448
                error("HCI device open failed: hci%d", dev_id);
 
1449
                return;
 
1450
        }
 
1451
 
 
1452
        if (hci_read_class_of_dev(dd, cls, HCI_REQ_TIMEOUT) < 0) {
 
1453
                error("Can't read class of device on hci%d: %s (%d)",
 
1454
                        dev_id, strerror(errno), errno);
 
1455
                hci_close_dev(dd);
 
1456
                return;
 
1457
        }
 
1458
 
 
1459
        adapter_set_class(adapter, cls);
 
1460
        write_local_class(local, cls);
 
1461
 
 
1462
        hci_close_dev(dd);
 
1463
}
 
1464
 
 
1465
void hcid_dbus_write_simple_pairing_mode_complete(bdaddr_t *local)
 
1466
{
 
1467
        struct btd_adapter *adapter;
 
1468
        int dd;
 
1469
        uint8_t mode;
 
1470
        uint16_t dev_id;
 
1471
        const gchar *path;
 
1472
 
 
1473
        adapter = manager_find_adapter(local);
 
1474
        if (!adapter) {
 
1475
                error("No matching adapter found");
 
1476
                return;
 
1477
        }
 
1478
 
 
1479
        dev_id = adapter_get_dev_id(adapter);
 
1480
        path = adapter_get_path(adapter);
 
1481
 
 
1482
        dd = hci_open_dev(dev_id);
 
1483
        if (dd < 0) {
 
1484
                error("HCI adapter open failed: %s", path);
 
1485
                return;
 
1486
        }
 
1487
 
 
1488
        if (hci_read_simple_pairing_mode(dd, &mode,
 
1489
                                                HCI_REQ_TIMEOUT) < 0) {
 
1490
                error("Can't read class of adapter on %s: %s(%d)",
 
1491
                                        path, strerror(errno), errno);
 
1492
                hci_close_dev(dd);
 
1493
                return;
 
1494
        }
 
1495
 
 
1496
        adapter_update_ssp_mode(adapter, dd, mode);
 
1497
 
 
1498
        hci_close_dev(dd);
 
1499
}
 
1500
 
 
1501
int hcid_dbus_get_io_cap(bdaddr_t *local, bdaddr_t *remote,
 
1502
                                                uint8_t *cap, uint8_t *auth)
 
1503
{
 
1504
        struct btd_adapter *adapter;
 
1505
        struct btd_device *device;
 
1506
        struct agent *agent = NULL;
 
1507
        char addr[18];
 
1508
 
 
1509
        adapter = manager_find_adapter(local);
 
1510
        if (!adapter) {
 
1511
                error("No matching adapter found");
 
1512
                return -1;
 
1513
        }
 
1514
 
 
1515
        if (get_auth_requirements(local, remote, auth) < 0)
 
1516
                return -1;
 
1517
 
 
1518
        ba2str(remote, addr);
 
1519
 
 
1520
        /* For CreatePairedDevice use dedicated bonding */
 
1521
        device = adapter_find_device(adapter, addr);
 
1522
        if (device) {
 
1523
                agent = device_get_agent(device);
 
1524
                if (agent)
 
1525
                        *auth = 0x03;
 
1526
        }
 
1527
        if (!agent)
 
1528
                agent = adapter_get_agent(adapter);
 
1529
 
 
1530
        if (!agent) {
 
1531
                /* This is the non bondable mode case */
 
1532
                if (device && device_get_auth(device) > 0x01) {
 
1533
                        debug("Bonding request, but no agent present");
 
1534
                        return -1;
 
1535
                }
 
1536
 
 
1537
                /* No agent available, and no bonding case */
 
1538
                if (*auth < 0x02) {
 
1539
                        debug("Allowing no bonding without agent");
 
1540
                        /* No input, no output */
 
1541
                        *cap = 0x03;
 
1542
                        return 0;
 
1543
                }
 
1544
 
 
1545
                error("No agent available for IO capability");
 
1546
                return -1;
 
1547
        }
 
1548
 
 
1549
        if (device && *auth < 0x02) {
 
1550
                /* If remote requests dedicated bonding follow that lead */
 
1551
                if (device_get_auth(device) == 0x02 ||
 
1552
                                device_get_auth(device) == 0x03)
 
1553
                        *auth = 0x02;
 
1554
        }
 
1555
 
 
1556
        *cap = agent_get_io_capability(agent);
 
1557
 
 
1558
        return 0;
 
1559
}
 
1560
 
 
1561
int hcid_dbus_set_io_cap(bdaddr_t *local, bdaddr_t *remote,
 
1562
                                                uint8_t cap, uint8_t auth)
 
1563
{
 
1564
        struct btd_adapter *adapter;
 
1565
        struct btd_device *device;
 
1566
        char addr[18];
 
1567
 
 
1568
        adapter = manager_find_adapter(local);
 
1569
        if (!adapter) {
 
1570
                error("No matching adapter found");
 
1571
                return -1;
 
1572
        }
 
1573
 
 
1574
        ba2str(remote, addr);
 
1575
 
 
1576
        device = adapter_get_device(connection, adapter, addr);
 
1577
        if (device) {
 
1578
                device_set_cap(device, cap);
 
1579
                device_set_auth(device, auth);
 
1580
        }
 
1581
 
 
1582
        return 0;
 
1583
}
 
1584
 
 
1585
static int inquiry_cancel(int dd, int to)
 
1586
{
 
1587
        struct hci_request rq;
 
1588
        uint8_t status;
 
1589
 
 
1590
        memset(&rq, 0, sizeof(rq));
 
1591
        rq.ogf    = OGF_LINK_CTL;
 
1592
        rq.ocf    = OCF_INQUIRY_CANCEL;
 
1593
        rq.rparam = &status;
 
1594
        rq.rlen   = sizeof(status);
 
1595
        rq.event = EVT_CMD_COMPLETE;
 
1596
 
 
1597
        if (hci_send_req(dd, &rq, to) < 0)
 
1598
                return -1;
 
1599
 
 
1600
        if (status) {
 
1601
                errno = bt_error(status);
 
1602
                return -1;
 
1603
        }
 
1604
 
 
1605
        return 0;
 
1606
}
 
1607
 
 
1608
static int remote_name_cancel(int dd, bdaddr_t *dba, int to)
 
1609
{
 
1610
        remote_name_req_cancel_cp cp;
 
1611
        struct hci_request rq;
 
1612
        uint8_t status;
 
1613
 
 
1614
        memset(&rq, 0, sizeof(rq));
 
1615
        memset(&cp, 0, sizeof(cp));
 
1616
 
 
1617
        bacpy(&cp.bdaddr, dba);
 
1618
 
 
1619
        rq.ogf    = OGF_LINK_CTL;
 
1620
        rq.ocf    = OCF_REMOTE_NAME_REQ_CANCEL;
 
1621
        rq.cparam = &cp;
 
1622
        rq.clen   = REMOTE_NAME_REQ_CANCEL_CP_SIZE;
 
1623
        rq.rparam = &status;
 
1624
        rq.rlen = sizeof(status);
 
1625
        rq.event = EVT_CMD_COMPLETE;
 
1626
 
 
1627
        if (hci_send_req(dd, &rq, to) < 0)
 
1628
                return -1;
 
1629
 
 
1630
        if (status) {
 
1631
                errno = bt_error(status);
 
1632
                return -1;
 
1633
        }
 
1634
 
 
1635
        return 0;
 
1636
}
 
1637
 
 
1638
int cancel_discovery(struct btd_adapter *adapter)
 
1639
{
 
1640
        struct remote_dev_info *dev, match;
 
1641
        int dd, err = 0;
 
1642
        uint16_t dev_id = adapter_get_dev_id(adapter);
 
1643
 
 
1644
        dd = hci_open_dev(dev_id);
 
1645
        if (dd < 0)
 
1646
                return -ENODEV;
 
1647
 
 
1648
        /*
 
1649
         * If there is a pending read remote name request means
 
1650
         * that the inquiry complete event was already received
 
1651
         */
 
1652
        memset(&match, 0, sizeof(struct remote_dev_info));
 
1653
        bacpy(&match.bdaddr, BDADDR_ANY);
 
1654
        match.name_status = NAME_REQUESTED;
 
1655
 
 
1656
        dev = adapter_search_found_devices(adapter, &match);
 
1657
        if (dev) {
 
1658
                if (remote_name_cancel(dd, &dev->bdaddr, HCI_REQ_TIMEOUT) < 0) {
 
1659
                        error("Read remote name cancel failed: %s, (%d)",
 
1660
                                        strerror(errno), errno);
 
1661
                        err = -errno;
 
1662
                }
 
1663
        } else {
 
1664
                if (inquiry_cancel(dd, HCI_REQ_TIMEOUT) < 0) {
 
1665
                        error("Inquiry cancel failed:%s (%d)",
 
1666
                                        strerror(errno), errno);
 
1667
                        err = -errno;
 
1668
                }
 
1669
        }
 
1670
 
 
1671
        hci_close_dev(dd);
 
1672
 
 
1673
        return err;
 
1674
}
 
1675
 
 
1676
static int periodic_inquiry_exit(int dd, int to)
 
1677
{
 
1678
        struct hci_request rq;
 
1679
        uint8_t status;
 
1680
 
 
1681
        memset(&rq, 0, sizeof(rq));
 
1682
        rq.ogf    = OGF_LINK_CTL;
 
1683
        rq.ocf    = OCF_EXIT_PERIODIC_INQUIRY;
 
1684
        rq.rparam = &status;
 
1685
        rq.rlen   = sizeof(status);
 
1686
        rq.event = EVT_CMD_COMPLETE;
 
1687
 
 
1688
        if (hci_send_req(dd, &rq, to) < 0)
 
1689
                return -1;
 
1690
 
 
1691
        if (status) {
 
1692
                errno = status;
 
1693
                return -1;
 
1694
        }
 
1695
 
 
1696
        return 0;
 
1697
}
 
1698
 
 
1699
int cancel_periodic_discovery(struct btd_adapter *adapter)
 
1700
{
 
1701
        struct remote_dev_info *dev, match;
 
1702
        int dd, err = 0;
 
1703
        uint16_t dev_id = adapter_get_dev_id(adapter);
 
1704
 
 
1705
        dd = hci_open_dev(dev_id);
 
1706
        if (dd < 0)
 
1707
                return -ENODEV;
 
1708
 
 
1709
        /* find the pending remote name request */
 
1710
        memset(&match, 0, sizeof(struct remote_dev_info));
 
1711
        bacpy(&match.bdaddr, BDADDR_ANY);
 
1712
        match.name_status = NAME_REQUESTED;
 
1713
 
 
1714
        dev = adapter_search_found_devices(adapter, &match);
 
1715
        if (dev) {
 
1716
                if (remote_name_cancel(dd, &dev->bdaddr, HCI_REQ_TIMEOUT) < 0) {
 
1717
                        error("Read remote name cancel failed: %s, (%d)",
 
1718
                                        strerror(errno), errno);
 
1719
                        err = -errno;
 
1720
                }
 
1721
        }
 
1722
 
 
1723
        /* ovewrite err if necessary: stop periodic inquiry has higher
 
1724
         * priority */
 
1725
        if (periodic_inquiry_exit(dd, HCI_REQ_TIMEOUT) < 0) {
 
1726
                error("Periodic Inquiry exit failed:%s (%d)",
 
1727
                                strerror(errno), errno);
 
1728
                err = -errno;
 
1729
        }
 
1730
 
 
1731
        hci_close_dev(dd);
 
1732
 
 
1733
        return err;
 
1734
}
 
1735
 
 
1736
/* Most of the functions in this module require easy access to a connection so
 
1737
 * we keep it global here and provide these access functions the other (few)
 
1738
 * modules that require access to it */
 
1739
 
 
1740
void set_dbus_connection(DBusConnection *conn)
 
1741
{
 
1742
        connection = conn;
 
1743
}
 
1744
 
 
1745
DBusConnection *get_dbus_connection(void)
 
1746
{
 
1747
        return connection;
 
1748
}