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

« back to all changes in this revision

Viewing changes to common/glib-helper.c

ImportĀ upstreamĀ versionĀ 4.81

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) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
6
 
 *
7
 
 *
8
 
 *  This program is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License as published by
10
 
 *  the Free Software Foundation; either version 2 of the License, or
11
 
 *  (at your option) any later version.
12
 
 *
13
 
 *  This program is distributed in the hope that it will be useful,
14
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 *  GNU General Public License for more details.
17
 
 *
18
 
 *  You should have received a copy of the GNU General Public License
19
 
 *  along with this program; if not, write to the Free Software
20
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
 
 *
22
 
 */
23
 
 
24
 
#ifdef HAVE_CONFIG_H
25
 
#include <config.h>
26
 
#endif
27
 
 
28
 
#include <stdlib.h>
29
 
#include <errno.h>
30
 
#include <fcntl.h>
31
 
#include <unistd.h>
32
 
#include <sys/ioctl.h>
33
 
#include <sys/socket.h>
34
 
 
35
 
#include <bluetooth/bluetooth.h>
36
 
#include <bluetooth/hci.h>
37
 
#include <bluetooth/hci_lib.h>
38
 
#include <bluetooth/rfcomm.h>
39
 
#include <bluetooth/l2cap.h>
40
 
#include <bluetooth/sco.h>
41
 
#include <bluetooth/sdp.h>
42
 
#include <bluetooth/sdp_lib.h>
43
 
 
44
 
#include <glib.h>
45
 
 
46
 
#include "glib-helper.h"
47
 
 
48
 
/* Number of seconds to keep a sdp_session_t in the cache */
49
 
#define CACHE_TIMEOUT 2
50
 
 
51
 
struct cached_sdp_session {
52
 
        bdaddr_t src;
53
 
        bdaddr_t dst;
54
 
        sdp_session_t *session;
55
 
        guint timer;
56
 
};
57
 
 
58
 
static GSList *cached_sdp_sessions = NULL;
59
 
 
60
 
struct hci_cmd_data {
61
 
        bt_hci_result_t         cb;
62
 
        uint16_t                handle;
63
 
        uint16_t                ocf;
64
 
        gpointer                caller_data;
65
 
};
66
 
 
67
 
static gboolean cached_session_expired(gpointer user_data)
68
 
{
69
 
        struct cached_sdp_session *cached = user_data;
70
 
 
71
 
        cached_sdp_sessions = g_slist_remove(cached_sdp_sessions, cached);
72
 
 
73
 
        sdp_close(cached->session);
74
 
 
75
 
        g_free(cached);
76
 
 
77
 
        return FALSE;
78
 
}
79
 
 
80
 
static sdp_session_t *get_sdp_session(const bdaddr_t *src, const bdaddr_t *dst)
81
 
{
82
 
        GSList *l;
83
 
 
84
 
        for (l = cached_sdp_sessions; l != NULL; l = l->next) {
85
 
                struct cached_sdp_session *c = l->data;
86
 
                sdp_session_t *session;
87
 
 
88
 
                if (bacmp(&c->src, src) || bacmp(&c->dst, dst))
89
 
                        continue;
90
 
 
91
 
                g_source_remove(c->timer);
92
 
 
93
 
                session = c->session;
94
 
 
95
 
                cached_sdp_sessions = g_slist_remove(cached_sdp_sessions, c);
96
 
                g_free(c);
97
 
 
98
 
                return session;
99
 
        }
100
 
 
101
 
        return sdp_connect(src, dst, SDP_NON_BLOCKING);
102
 
}
103
 
 
104
 
static void cache_sdp_session(bdaddr_t *src, bdaddr_t *dst,
105
 
                                sdp_session_t *session)
106
 
{
107
 
        struct cached_sdp_session *cached;
108
 
 
109
 
        cached = g_new0(struct cached_sdp_session, 1);
110
 
 
111
 
        bacpy(&cached->src, src);
112
 
        bacpy(&cached->dst, dst);
113
 
 
114
 
        cached->session = session;
115
 
 
116
 
        cached_sdp_sessions = g_slist_append(cached_sdp_sessions, cached);
117
 
 
118
 
        cached->timer = g_timeout_add_seconds(CACHE_TIMEOUT,
119
 
                                                cached_session_expired,
120
 
                                                cached);
121
 
}
122
 
 
123
 
int set_nonblocking(int fd)
124
 
{
125
 
        long arg;
126
 
 
127
 
        arg = fcntl(fd, F_GETFL);
128
 
        if (arg < 0)
129
 
                return -errno;
130
 
 
131
 
        /* Return if already nonblocking */
132
 
        if (arg & O_NONBLOCK)
133
 
                return 0;
134
 
 
135
 
        arg |= O_NONBLOCK;
136
 
        if (fcntl(fd, F_SETFL, arg) < 0)
137
 
                return -errno;
138
 
 
139
 
        return 0;
140
 
}
141
 
 
142
 
struct search_context {
143
 
        bdaddr_t                src;
144
 
        bdaddr_t                dst;
145
 
        sdp_session_t           *session;
146
 
        bt_callback_t           cb;
147
 
        bt_destroy_t            destroy;
148
 
        gpointer                user_data;
149
 
        uuid_t                  uuid;
150
 
};
151
 
 
152
 
static GSList *context_list = NULL;
153
 
 
154
 
static void search_context_cleanup(struct search_context *ctxt)
155
 
{
156
 
        context_list = g_slist_remove(context_list, ctxt);
157
 
 
158
 
        if (ctxt->destroy)
159
 
                ctxt->destroy(ctxt->user_data);
160
 
 
161
 
        g_free(ctxt);
162
 
}
163
 
 
164
 
static void search_completed_cb(uint8_t type, uint16_t status,
165
 
                        uint8_t *rsp, size_t size, void *user_data)
166
 
{
167
 
        struct search_context *ctxt = user_data;
168
 
        sdp_list_t *recs = NULL;
169
 
        int scanned, seqlen = 0, bytesleft = size;
170
 
        uint8_t dataType;
171
 
        int err = 0;
172
 
 
173
 
        if (status || type != SDP_SVC_SEARCH_ATTR_RSP) {
174
 
                err = -EPROTO;
175
 
                goto done;
176
 
        }
177
 
 
178
 
        scanned = sdp_extract_seqtype(rsp, bytesleft, &dataType, &seqlen);
179
 
        if (!scanned || !seqlen)
180
 
                goto done;
181
 
 
182
 
        rsp += scanned;
183
 
        bytesleft -= scanned;
184
 
        do {
185
 
                sdp_record_t *rec;
186
 
                int recsize;
187
 
 
188
 
                recsize = 0;
189
 
                rec = sdp_extract_pdu(rsp, bytesleft, &recsize);
190
 
                if (!rec)
191
 
                        break;
192
 
 
193
 
                if (!recsize) {
194
 
                        sdp_record_free(rec);
195
 
                        break;
196
 
                }
197
 
 
198
 
                scanned += recsize;
199
 
                rsp += recsize;
200
 
                bytesleft -= recsize;
201
 
 
202
 
                recs = sdp_list_append(recs, rec);
203
 
        } while (scanned < (ssize_t) size && bytesleft > 0);
204
 
 
205
 
done:
206
 
        cache_sdp_session(&ctxt->src, &ctxt->dst, ctxt->session);
207
 
 
208
 
        if (ctxt->cb)
209
 
                ctxt->cb(recs, err, ctxt->user_data);
210
 
 
211
 
        if (recs)
212
 
                sdp_list_free(recs, (sdp_free_func_t) sdp_record_free);
213
 
 
214
 
        search_context_cleanup(ctxt);
215
 
}
216
 
 
217
 
static gboolean search_process_cb(GIOChannel *chan,
218
 
                        GIOCondition cond, void *user_data)
219
 
{
220
 
        struct search_context *ctxt = user_data;
221
 
        int err = 0;
222
 
 
223
 
        if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
224
 
                err = EIO;
225
 
                goto failed;
226
 
        }
227
 
 
228
 
        if (sdp_process(ctxt->session) < 0)
229
 
                goto failed;
230
 
 
231
 
        return TRUE;
232
 
 
233
 
failed:
234
 
        if (err) {
235
 
                sdp_close(ctxt->session);
236
 
 
237
 
                if (ctxt->cb)
238
 
                        ctxt->cb(NULL, err, ctxt->user_data);
239
 
 
240
 
                search_context_cleanup(ctxt);
241
 
        }
242
 
 
243
 
        return FALSE;
244
 
}
245
 
 
246
 
static gboolean connect_watch(GIOChannel *chan, GIOCondition cond, gpointer user_data)
247
 
{
248
 
        struct search_context *ctxt = user_data;
249
 
        sdp_list_t *search, *attrids;
250
 
        uint32_t range = 0x0000ffff;
251
 
        socklen_t len;
252
 
        int sk, err = 0;
253
 
 
254
 
        sk = g_io_channel_unix_get_fd(chan);
255
 
 
256
 
        len = sizeof(err);
257
 
        if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
258
 
                err = errno;
259
 
                goto failed;
260
 
        }
261
 
 
262
 
        if (err != 0)
263
 
                goto failed;
264
 
 
265
 
        if (sdp_set_notify(ctxt->session, search_completed_cb, ctxt) < 0) {
266
 
                err = EIO;
267
 
                goto failed;
268
 
        }
269
 
 
270
 
        search = sdp_list_append(NULL, &ctxt->uuid);
271
 
        attrids = sdp_list_append(NULL, &range);
272
 
        if (sdp_service_search_attr_async(ctxt->session,
273
 
                                search, SDP_ATTR_REQ_RANGE, attrids) < 0) {
274
 
                sdp_list_free(attrids, NULL);
275
 
                sdp_list_free(search, NULL);
276
 
                err = EIO;
277
 
                goto failed;
278
 
        }
279
 
 
280
 
        sdp_list_free(attrids, NULL);
281
 
        sdp_list_free(search, NULL);
282
 
 
283
 
        /* Set callback responsible for update the internal SDP transaction */
284
 
        g_io_add_watch(chan, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
285
 
                        search_process_cb, ctxt);
286
 
        return FALSE;
287
 
 
288
 
failed:
289
 
        sdp_close(ctxt->session);
290
 
 
291
 
        if (ctxt->cb)
292
 
                ctxt->cb(NULL, -err, ctxt->user_data);
293
 
 
294
 
        search_context_cleanup(ctxt);
295
 
 
296
 
        return FALSE;
297
 
}
298
 
 
299
 
static int create_search_context(struct search_context **ctxt,
300
 
                                const bdaddr_t *src, const bdaddr_t *dst,
301
 
                                uuid_t *uuid)
302
 
{
303
 
        sdp_session_t *s;
304
 
        GIOChannel *chan;
305
 
 
306
 
        if (!ctxt)
307
 
                return -EINVAL;
308
 
 
309
 
        s = get_sdp_session(src, dst);
310
 
        if (!s)
311
 
                return -errno;
312
 
 
313
 
        *ctxt = g_try_malloc0(sizeof(struct search_context));
314
 
        if (!*ctxt) {
315
 
                sdp_close(s);
316
 
                return -ENOMEM;
317
 
        }
318
 
 
319
 
        bacpy(&(*ctxt)->src, src);
320
 
        bacpy(&(*ctxt)->dst, dst);
321
 
        (*ctxt)->session = s;
322
 
        (*ctxt)->uuid = *uuid;
323
 
 
324
 
        chan = g_io_channel_unix_new(sdp_get_socket(s));
325
 
        g_io_add_watch(chan, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
326
 
                        connect_watch, *ctxt);
327
 
        g_io_channel_unref(chan);
328
 
 
329
 
        return 0;
330
 
}
331
 
 
332
 
int bt_search_service(const bdaddr_t *src, const bdaddr_t *dst,
333
 
                        uuid_t *uuid, bt_callback_t cb, void *user_data,
334
 
                        bt_destroy_t destroy)
335
 
{
336
 
        struct search_context *ctxt = NULL;
337
 
        int err;
338
 
 
339
 
        if (!cb)
340
 
                return -EINVAL;
341
 
 
342
 
        err = create_search_context(&ctxt, src, dst, uuid);
343
 
        if (err < 0)
344
 
                return err;
345
 
 
346
 
        ctxt->cb        = cb;
347
 
        ctxt->destroy   = destroy;
348
 
        ctxt->user_data = user_data;
349
 
 
350
 
        context_list = g_slist_append(context_list, ctxt);
351
 
 
352
 
        return 0;
353
 
}
354
 
 
355
 
int bt_discover_services(const bdaddr_t *src, const bdaddr_t *dst,
356
 
                bt_callback_t cb, void *user_data, bt_destroy_t destroy)
357
 
{
358
 
        uuid_t uuid;
359
 
 
360
 
        sdp_uuid16_create(&uuid, PUBLIC_BROWSE_GROUP);
361
 
 
362
 
        return bt_search_service(src, dst, &uuid, cb, user_data, destroy);
363
 
}
364
 
 
365
 
static int find_by_bdaddr(const void *data, const void *user_data)
366
 
{
367
 
        const struct search_context *ctxt = data, *search = user_data;
368
 
 
369
 
        return (bacmp(&ctxt->dst, &search->dst) &&
370
 
                                        bacmp(&ctxt->src, &search->src));
371
 
}
372
 
 
373
 
int bt_cancel_discovery(const bdaddr_t *src, const bdaddr_t *dst)
374
 
{
375
 
        struct search_context search, *ctxt;
376
 
        GSList *match;
377
 
 
378
 
        memset(&search, 0, sizeof(search));
379
 
        bacpy(&search.src, src);
380
 
        bacpy(&search.dst, dst);
381
 
 
382
 
        /* Ongoing SDP Discovery */
383
 
        match = g_slist_find_custom(context_list, &search, find_by_bdaddr);
384
 
        if (!match)
385
 
                return -ENODATA;
386
 
 
387
 
        ctxt = match->data;
388
 
        if (!ctxt->session)
389
 
                return -ENOTCONN;
390
 
 
391
 
        close(ctxt->session->sock);
392
 
        return 0;
393
 
}
394
 
 
395
 
char *bt_uuid2string(uuid_t *uuid)
396
 
{
397
 
        gchar *str;
398
 
        uuid_t uuid128;
399
 
        unsigned int data0;
400
 
        unsigned short data1;
401
 
        unsigned short data2;
402
 
        unsigned short data3;
403
 
        unsigned int data4;
404
 
        unsigned short data5;
405
 
 
406
 
        if (!uuid)
407
 
                return NULL;
408
 
 
409
 
        switch (uuid->type) {
410
 
        case SDP_UUID16:
411
 
                sdp_uuid16_to_uuid128(&uuid128, uuid);
412
 
                break;
413
 
        case SDP_UUID32:
414
 
                sdp_uuid32_to_uuid128(&uuid128, uuid);
415
 
                break;
416
 
        case SDP_UUID128:
417
 
                memcpy(&uuid128, uuid, sizeof(uuid_t));
418
 
                break;
419
 
        default:
420
 
                /* Type of UUID unknown */
421
 
                return NULL;
422
 
        }
423
 
 
424
 
        memcpy(&data0, &uuid128.value.uuid128.data[0], 4);
425
 
        memcpy(&data1, &uuid128.value.uuid128.data[4], 2);
426
 
        memcpy(&data2, &uuid128.value.uuid128.data[6], 2);
427
 
        memcpy(&data3, &uuid128.value.uuid128.data[8], 2);
428
 
        memcpy(&data4, &uuid128.value.uuid128.data[10], 4);
429
 
        memcpy(&data5, &uuid128.value.uuid128.data[14], 2);
430
 
 
431
 
        str = g_try_malloc0(MAX_LEN_UUID_STR);
432
 
        if (!str)
433
 
                return NULL;
434
 
 
435
 
        sprintf(str, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
436
 
                        g_ntohl(data0), g_ntohs(data1),
437
 
                        g_ntohs(data2), g_ntohs(data3),
438
 
                        g_ntohl(data4), g_ntohs(data5));
439
 
 
440
 
        return str;
441
 
}
442
 
 
443
 
static struct {
444
 
        const char      *name;
445
 
        uint16_t        class;
446
 
} bt_services[] = {
447
 
        { "vcp",        VIDEO_CONF_SVCLASS_ID           },
448
 
        { "pbap",       PBAP_SVCLASS_ID                 },
449
 
        { "sap",        SAP_SVCLASS_ID                  },
450
 
        { "ftp",        OBEX_FILETRANS_SVCLASS_ID       },
451
 
        { "bpp",        BASIC_PRINTING_SVCLASS_ID       },
452
 
        { "bip",        IMAGING_SVCLASS_ID              },
453
 
        { "synch",      IRMC_SYNC_SVCLASS_ID            },
454
 
        { "dun",        DIALUP_NET_SVCLASS_ID           },
455
 
        { "opp",        OBEX_OBJPUSH_SVCLASS_ID         },
456
 
        { "fax",        FAX_SVCLASS_ID                  },
457
 
        { "spp",        SERIAL_PORT_SVCLASS_ID          },
458
 
        { "hsp",        HEADSET_SVCLASS_ID              },
459
 
        { "hfp",        HANDSFREE_SVCLASS_ID            },
460
 
        { }
461
 
};
462
 
 
463
 
uint16_t bt_name2class(const char *pattern)
464
 
{
465
 
        int i;
466
 
 
467
 
        for (i = 0; bt_services[i].name; i++) {
468
 
                if (strcasecmp(bt_services[i].name, pattern) == 0)
469
 
                        return bt_services[i].class;
470
 
        }
471
 
 
472
 
        return 0;
473
 
}
474
 
 
475
 
static inline gboolean is_uuid128(const char *string)
476
 
{
477
 
        return (strlen(string) == 36 &&
478
 
                        string[8] == '-' &&
479
 
                        string[13] == '-' &&
480
 
                        string[18] == '-' &&
481
 
                        string[23] == '-');
482
 
}
483
 
 
484
 
char *bt_name2string(const char *pattern)
485
 
{
486
 
        uuid_t uuid;
487
 
        uint16_t uuid16;
488
 
        int i;
489
 
 
490
 
        /* UUID 128 string format */
491
 
        if (is_uuid128(pattern))
492
 
                return g_strdup(pattern);
493
 
 
494
 
        /* Friendly service name format */
495
 
        uuid16 = bt_name2class(pattern);
496
 
        if (uuid16)
497
 
                goto proceed;
498
 
 
499
 
        /* HEX format */
500
 
        uuid16 = strtol(pattern, NULL, 16);
501
 
        for (i = 0; bt_services[i].class; i++) {
502
 
                if (bt_services[i].class == uuid16)
503
 
                        goto proceed;
504
 
        }
505
 
 
506
 
        return NULL;
507
 
 
508
 
proceed:
509
 
        sdp_uuid16_create(&uuid, uuid16);
510
 
 
511
 
        return bt_uuid2string(&uuid);
512
 
}
513
 
 
514
 
int bt_string2uuid(uuid_t *uuid, const char *string)
515
 
{
516
 
        uint32_t data0, data4;
517
 
        uint16_t data1, data2, data3, data5;
518
 
 
519
 
        if (is_uuid128(string) &&
520
 
                        sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
521
 
                                &data0, &data1, &data2, &data3, &data4, &data5) == 6) {
522
 
                uint8_t val[16];
523
 
 
524
 
                data0 = g_htonl(data0);
525
 
                data1 = g_htons(data1);
526
 
                data2 = g_htons(data2);
527
 
                data3 = g_htons(data3);
528
 
                data4 = g_htonl(data4);
529
 
                data5 = g_htons(data5);
530
 
 
531
 
                memcpy(&val[0], &data0, 4);
532
 
                memcpy(&val[4], &data1, 2);
533
 
                memcpy(&val[6], &data2, 2);
534
 
                memcpy(&val[8], &data3, 2);
535
 
                memcpy(&val[10], &data4, 4);
536
 
                memcpy(&val[14], &data5, 2);
537
 
 
538
 
                sdp_uuid128_create(uuid, val);
539
 
 
540
 
                return 0;
541
 
        } else {
542
 
                uint16_t class = bt_name2class(string);
543
 
                if (class) {
544
 
                        sdp_uuid16_create(uuid, class);
545
 
                        return 0;
546
 
                }
547
 
        }
548
 
 
549
 
        return -1;
550
 
}
551
 
 
552
 
gchar *bt_list2string(GSList *list)
553
 
{
554
 
        GSList *l;
555
 
        gchar *str, *tmp;
556
 
 
557
 
        if (!list)
558
 
                return NULL;
559
 
 
560
 
        str = g_strdup((const gchar *) list->data);
561
 
 
562
 
        for (l = list->next; l; l = l->next) {
563
 
                tmp = g_strconcat(str, " " , (const gchar *) l->data, NULL);
564
 
                g_free(str);
565
 
                str = tmp;
566
 
        }
567
 
 
568
 
        return str;
569
 
}
570
 
 
571
 
GSList *bt_string2list(const gchar *str)
572
 
{
573
 
        GSList *l = NULL;
574
 
        gchar **uuids;
575
 
        int i = 0;
576
 
 
577
 
        if (!str)
578
 
                return NULL;
579
 
 
580
 
        /* FIXME: eglib doesn't support g_strsplit */
581
 
        uuids = g_strsplit(str, " ", 0);
582
 
        if (!uuids)
583
 
                return NULL;
584
 
 
585
 
        while (uuids[i]) {
586
 
                l = g_slist_append(l, uuids[i]);
587
 
                i++;
588
 
        }
589
 
 
590
 
        g_free(uuids);
591
 
 
592
 
        return l;
593
 
}
594
 
 
595
 
static gboolean hci_event_watch(GIOChannel *io,
596
 
                        GIOCondition cond, gpointer user_data)
597
 
{
598
 
        unsigned char buf[HCI_MAX_EVENT_SIZE], *body;
599
 
        struct hci_cmd_data *cmd = user_data;
600
 
        evt_cmd_status *evt_status;
601
 
        evt_auth_complete *evt_auth;
602
 
        evt_encrypt_change *evt_enc;
603
 
        hci_event_hdr *hdr;
604
 
        set_conn_encrypt_cp cp;
605
 
        int dd;
606
 
        uint16_t ocf;
607
 
        uint8_t status = HCI_OE_POWER_OFF;
608
 
 
609
 
        if (cond & G_IO_NVAL) {
610
 
                cmd->cb(status, cmd->caller_data);
611
 
                return FALSE;
612
 
        }
613
 
 
614
 
        if (cond & (G_IO_ERR | G_IO_HUP))
615
 
                goto failed;
616
 
 
617
 
        dd = g_io_channel_unix_get_fd(io);
618
 
 
619
 
        if (read(dd, buf, sizeof(buf)) < 0)
620
 
                goto failed;
621
 
 
622
 
        hdr = (hci_event_hdr *) (buf + 1);
623
 
        body = buf + (1 + HCI_EVENT_HDR_SIZE);
624
 
 
625
 
        switch (hdr->evt) {
626
 
        case EVT_CMD_STATUS:
627
 
                evt_status = (evt_cmd_status *) body;
628
 
                ocf = cmd_opcode_ocf(evt_status->opcode);
629
 
                if (ocf != cmd->ocf)
630
 
                        return TRUE;
631
 
                switch (ocf) {
632
 
                case OCF_AUTH_REQUESTED:
633
 
                case OCF_SET_CONN_ENCRYPT:
634
 
                        if (evt_status->status != 0) {
635
 
                                /* Baseband rejected command */
636
 
                                status = evt_status->status;
637
 
                                goto failed;
638
 
                        }
639
 
                        break;
640
 
                default:
641
 
                        return TRUE;
642
 
                }
643
 
                /* Wait for the next event */
644
 
                return TRUE;
645
 
        case EVT_AUTH_COMPLETE:
646
 
                evt_auth = (evt_auth_complete *) body;
647
 
                if (evt_auth->handle != cmd->handle) {
648
 
                        /* Skipping */
649
 
                        return TRUE;
650
 
                }
651
 
 
652
 
                if (evt_auth->status != 0x00) {
653
 
                        status = evt_auth->status;
654
 
                        /* Abort encryption */
655
 
                        goto failed;
656
 
                }
657
 
 
658
 
                memset(&cp, 0, sizeof(cp));
659
 
                cp.handle  = cmd->handle;
660
 
                cp.encrypt = 1;
661
 
 
662
 
                cmd->ocf = OCF_SET_CONN_ENCRYPT;
663
 
 
664
 
                if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
665
 
                                        SET_CONN_ENCRYPT_CP_SIZE, &cp) < 0) {
666
 
                        status = HCI_COMMAND_DISALLOWED;
667
 
                        goto failed;
668
 
                }
669
 
                /* Wait for encrypt change event */
670
 
                return TRUE;
671
 
        case EVT_ENCRYPT_CHANGE:
672
 
                evt_enc = (evt_encrypt_change *) body;
673
 
                if (evt_enc->handle != cmd->handle)
674
 
                        return TRUE;
675
 
 
676
 
                /* Procedure finished: reporting status */
677
 
                status = evt_enc->status;
678
 
                break;
679
 
        default:
680
 
                /* Skipping */
681
 
                return TRUE;
682
 
        }
683
 
 
684
 
failed:
685
 
        cmd->cb(status, cmd->caller_data);
686
 
        g_io_channel_shutdown(io, TRUE, NULL);
687
 
 
688
 
        return FALSE;
689
 
}
690
 
 
691
 
int bt_acl_encrypt(const bdaddr_t *src, const bdaddr_t *dst,
692
 
                        bt_hci_result_t cb, gpointer user_data)
693
 
{
694
 
        GIOChannel *io;
695
 
        struct hci_cmd_data *cmd;
696
 
        struct hci_conn_info_req *cr;
697
 
        auth_requested_cp cp;
698
 
        struct hci_filter nf;
699
 
        int dd, dev_id, err;
700
 
        char src_addr[18];
701
 
        uint32_t link_mode;
702
 
        uint16_t handle;
703
 
 
704
 
        ba2str(src, src_addr);
705
 
        dev_id = hci_devid(src_addr);
706
 
        if (dev_id < 0)
707
 
                return -errno;
708
 
 
709
 
        dd = hci_open_dev(dev_id);
710
 
        if (dd < 0)
711
 
                return -errno;
712
 
 
713
 
        cr = g_malloc0(sizeof(*cr) + sizeof(struct hci_conn_info));
714
 
        cr->type = ACL_LINK;
715
 
        bacpy(&cr->bdaddr, dst);
716
 
 
717
 
        err = ioctl(dd, HCIGETCONNINFO, cr);
718
 
        link_mode = cr->conn_info->link_mode;
719
 
        handle = cr->conn_info->handle;
720
 
        g_free(cr);
721
 
 
722
 
        if (err < 0) {
723
 
                err = errno;
724
 
                goto failed;
725
 
        }
726
 
 
727
 
        if (link_mode & HCI_LM_ENCRYPT) {
728
 
                /* Already encrypted */
729
 
                err = EALREADY;
730
 
                goto failed;
731
 
        }
732
 
 
733
 
        memset(&cp, 0, sizeof(cp));
734
 
        cp.handle = htobs(handle);
735
 
 
736
 
        if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
737
 
                                AUTH_REQUESTED_CP_SIZE, &cp) < 0) {
738
 
                err = errno;
739
 
                goto failed;
740
 
        }
741
 
 
742
 
        cmd = g_new0(struct hci_cmd_data, 1);
743
 
        cmd->handle = handle;
744
 
        cmd->ocf = OCF_AUTH_REQUESTED;
745
 
        cmd->cb = cb;
746
 
        cmd->caller_data = user_data;
747
 
 
748
 
        hci_filter_clear(&nf);
749
 
        hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
750
 
        hci_filter_set_event(EVT_CMD_STATUS, &nf);
751
 
        hci_filter_set_event(EVT_AUTH_COMPLETE, &nf);
752
 
        hci_filter_set_event(EVT_ENCRYPT_CHANGE, &nf);
753
 
 
754
 
        if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
755
 
                err = errno;
756
 
                goto failed;
757
 
        }
758
 
 
759
 
        io = g_io_channel_unix_new(dd);
760
 
        g_io_channel_set_close_on_unref(io, FALSE);
761
 
        g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
762
 
                        G_IO_HUP | G_IO_ERR | G_IO_NVAL | G_IO_IN,
763
 
                        hci_event_watch, cmd, g_free);
764
 
        g_io_channel_unref(io);
765
 
 
766
 
        return 0;
767
 
 
768
 
failed:
769
 
        close(dd);
770
 
 
771
 
        return -err;
772
 
}