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

« back to all changes in this revision

Viewing changes to audio/control.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  BlueZ - Bluetooth protocol stack for Linux
4
4
 *
5
 
 *  Copyright (C) 2006-2007  Nokia Corporation
6
 
 *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
 
5
 *  Copyright (C) 2006-2010  Nokia Corporation
 
6
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7
7
 *
8
8
 *
9
9
 *  This program is free software; you can redistribute it and/or modify
40
40
#include <bluetooth/bluetooth.h>
41
41
#include <bluetooth/sdp.h>
42
42
#include <bluetooth/sdp_lib.h>
43
 
#include <bluetooth/l2cap.h>
44
43
 
45
44
#include <glib.h>
46
45
#include <dbus/dbus.h>
47
46
#include <gdbus.h>
48
47
 
49
 
#include "logging.h"
 
48
#include "log.h"
 
49
#include "error.h"
50
50
#include "uinput.h"
51
51
#include "adapter.h"
 
52
#include "../src/device.h"
52
53
#include "device.h"
53
54
#include "manager.h"
54
55
#include "avdtp.h"
87
88
#define SUBUNIT_PANEL           0x09
88
89
 
89
90
/* operands in passthrough commands */
90
 
#define VOLUP_OP                0x41
91
 
#define VOLDOWN_OP              0x42
 
91
#define VOL_UP_OP               0x41
 
92
#define VOL_DOWN_OP             0x42
92
93
#define MUTE_OP                 0x43
93
 
 
94
94
#define PLAY_OP                 0x44
95
95
#define STOP_OP                 0x45
96
96
#define PAUSE_OP                0x46
 
97
#define RECORD_OP               0x47
97
98
#define REWIND_OP               0x48
98
99
#define FAST_FORWARD_OP         0x49
99
 
#define NEXT_OP                 0x4b
100
 
#define PREV_OP                 0x4c
 
100
#define EJECT_OP                0x4a
 
101
#define FORWARD_OP              0x4b
 
102
#define BACKWARD_OP             0x4c
 
103
 
 
104
#define QUIRK_NO_RELEASE        1 << 0
101
105
 
102
106
static DBusConnection *connection = NULL;
103
107
 
104
108
static GSList *servers = NULL;
105
 
static GSList *sessions = NULL;
106
109
 
107
110
#if __BYTE_ORDER == __LITTLE_ENDIAN
108
111
 
113
116
        uint8_t transaction:4;
114
117
        uint16_t pid;
115
118
} __attribute__ ((packed));
 
119
#define AVCTP_HEADER_LENGTH 3
116
120
 
117
121
struct avrcp_header {
118
122
        uint8_t code:4;
121
125
        uint8_t subunit_type:5;
122
126
        uint8_t opcode;
123
127
} __attribute__ ((packed));
 
128
#define AVRCP_HEADER_LENGTH 3
124
129
 
125
130
#elif __BYTE_ORDER == __BIG_ENDIAN
126
131
 
131
136
        uint8_t ipid:1;
132
137
        uint16_t pid;
133
138
} __attribute__ ((packed));
 
139
#define AVCTP_HEADER_LENGTH 3
134
140
 
135
141
struct avrcp_header {
136
142
        uint8_t _hdr0:4;
139
145
        uint8_t subunit_id:3;
140
146
        uint8_t opcode;
141
147
} __attribute__ ((packed));
 
148
#define AVRCP_HEADER_LENGTH 3
142
149
 
143
150
#else
144
151
#error "Unknown byte order"
157
164
        uint32_t ct_record_id;
158
165
};
159
166
 
160
 
struct avctp {
 
167
struct control {
161
168
        struct audio_device *dev;
162
169
 
163
170
        avctp_state_t state;
164
171
 
165
 
        bdaddr_t src;
166
 
        bdaddr_t dst;
167
 
 
168
172
        int uinput;
169
173
 
170
174
        GIOChannel *io;
171
175
        guint io_id;
172
176
 
173
177
        uint16_t mtu;
 
178
 
 
179
        gboolean target;
 
180
 
 
181
        uint8_t key_quirks[256];
174
182
};
175
183
 
176
 
struct control {
177
 
        struct avctp *session;
 
184
static struct {
 
185
        const char *name;
 
186
        uint8_t avrcp;
 
187
        uint16_t uinput;
 
188
} key_map[] = {
 
189
        { "PLAY",               PLAY_OP,                KEY_PLAYCD },
 
190
        { "STOP",               STOP_OP,                KEY_STOPCD },
 
191
        { "PAUSE",              PAUSE_OP,               KEY_PAUSECD },
 
192
        { "FORWARD",            FORWARD_OP,             KEY_NEXTSONG },
 
193
        { "BACKWARD",           BACKWARD_OP,            KEY_PREVIOUSSONG },
 
194
        { "REWIND",             REWIND_OP,              KEY_REWIND },
 
195
        { "FAST FORWARD",       FAST_FORWARD_OP,        KEY_FASTFORWARD },
 
196
        { NULL }
178
197
};
179
198
 
180
199
static GSList *avctp_callbacks = NULL;
181
200
 
 
201
static void auth_cb(DBusError *derr, void *user_data);
 
202
 
182
203
static sdp_record_t *avrcp_ct_record()
183
204
{
184
205
        sdp_list_t *svclass_id, *pfseq, *apseq, *root;
187
208
        sdp_list_t *aproto, *proto[2];
188
209
        sdp_record_t *record;
189
210
        sdp_data_t *psm, *version, *features;
190
 
        uint16_t lp = AVCTP_PSM, ver = 0x0100, feat = 0x000f;
 
211
        uint16_t lp = AVCTP_PSM;
 
212
        uint16_t avrcp_ver = 0x0100, avctp_ver = 0x0103, feat = 0x000f;
191
213
 
192
214
        record = sdp_record_alloc();
193
215
        if (!record)
211
233
 
212
234
        sdp_uuid16_create(&avctp, AVCTP_UUID);
213
235
        proto[1] = sdp_list_append(0, &avctp);
214
 
        version = sdp_data_alloc(SDP_UINT16, &ver);
 
236
        version = sdp_data_alloc(SDP_UINT16, &avctp_ver);
215
237
        proto[1] = sdp_list_append(proto[1], version);
216
238
        apseq = sdp_list_append(apseq, proto[1]);
217
239
 
220
242
 
221
243
        /* Bluetooth Profile Descriptor List */
222
244
        sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
223
 
        profile[0].version = ver;
 
245
        profile[0].version = avrcp_ver;
224
246
        pfseq = sdp_list_append(0, &profile[0]);
225
247
        sdp_set_profile_descs(record, pfseq);
226
248
 
250
272
        sdp_list_t *aproto, *proto[2];
251
273
        sdp_record_t *record;
252
274
        sdp_data_t *psm, *version, *features;
253
 
        uint16_t lp = AVCTP_PSM, ver = 0x0100, feat = 0x000f;
 
275
        uint16_t lp = AVCTP_PSM;
 
276
        uint16_t avrcp_ver = 0x0100, avctp_ver = 0x0103, feat = 0x000f;
254
277
 
255
278
        record = sdp_record_alloc();
256
279
        if (!record)
274
297
 
275
298
        sdp_uuid16_create(&avctp, AVCTP_UUID);
276
299
        proto[1] = sdp_list_append(0, &avctp);
277
 
        version = sdp_data_alloc(SDP_UINT16, &ver);
 
300
        version = sdp_data_alloc(SDP_UINT16, &avctp_ver);
278
301
        proto[1] = sdp_list_append(proto[1], version);
279
302
        apseq = sdp_list_append(apseq, proto[1]);
280
303
 
283
306
 
284
307
        /* Bluetooth Profile Descriptor List */
285
308
        sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
286
 
        profile[0].version = ver;
 
309
        profile[0].version = avrcp_ver;
287
310
        pfseq = sdp_list_append(0, &profile[0]);
288
311
        sdp_set_profile_descs(record, pfseq);
289
312
 
305
328
        return record;
306
329
}
307
330
 
308
 
static struct avctp *find_session(const bdaddr_t *src, const bdaddr_t *dst)
309
 
{
310
 
        GSList *l;
311
 
 
312
 
        for (l = sessions; l != NULL; l = g_slist_next(l)) {
313
 
                struct avctp *s = l->data;
314
 
 
315
 
                if (bacmp(src, &s->src) || bacmp(dst, &s->dst))
316
 
                        continue;
317
 
 
318
 
                return s;
319
 
        }
320
 
 
321
 
        return NULL;
322
 
}
323
 
 
324
 
static struct avctp *avctp_get(const bdaddr_t *src, const bdaddr_t *dst)
325
 
{
326
 
        struct avctp *session;
327
 
 
328
 
        assert(src != NULL);
329
 
        assert(dst != NULL);
330
 
 
331
 
        session = find_session(src, dst);
332
 
        if (session)
333
 
                return session;
334
 
 
335
 
        session = g_new0(struct avctp, 1);
336
 
 
337
 
        session->state = AVCTP_STATE_DISCONNECTED;
338
 
        session->uinput = -1;
339
 
        bacpy(&session->src, src);
340
 
        bacpy(&session->dst, dst);
341
 
 
342
 
        sessions = g_slist_append(sessions, session);
343
 
 
344
 
        return session;
345
 
}
346
 
 
347
331
static int send_event(int fd, uint16_t type, uint16_t code, int32_t value)
348
332
{
349
333
        struct uinput_event event;
365
349
        send_event(fd, EV_SYN, SYN_REPORT, 0);
366
350
}
367
351
 
368
 
static void handle_panel_passthrough(struct avctp *session,
 
352
static void handle_panel_passthrough(struct control *control,
369
353
                                        const unsigned char *operands,
370
354
                                        int operand_count)
371
355
{
372
356
        const char *status;
373
 
        int pressed;
 
357
        int pressed, i;
374
358
 
375
359
        if (operand_count == 0)
376
360
                return;
383
367
                pressed = 1;
384
368
        }
385
369
 
386
 
        switch (operands[0] & 0x7F) {
387
 
        case PLAY_OP:
388
 
                debug("AVRCP: PLAY %s", status);
389
 
                send_key(session->uinput, KEY_PLAYPAUSE, pressed);
390
 
                break;
391
 
        case STOP_OP:
392
 
                debug("AVRCP: STOP %s", status);
393
 
                send_key(session->uinput, KEY_STOP, pressed);
394
 
                break;
395
 
        case PAUSE_OP:
396
 
                debug("AVRCP: PAUSE %s", status);
397
 
                send_key(session->uinput, KEY_PLAYPAUSE, pressed);
398
 
                break;
399
 
        case NEXT_OP:
400
 
                debug("AVRCP: NEXT %s", status);
401
 
                send_key(session->uinput, KEY_NEXTSONG, pressed);
402
 
                break;
403
 
        case PREV_OP:
404
 
                debug("AVRCP: PREV %s", status);
405
 
                send_key(session->uinput, KEY_PREVIOUSSONG, pressed);
406
 
                break;
407
 
        case REWIND_OP:
408
 
                debug("AVRCP: REWIND %s", status);
409
 
                send_key(session->uinput, KEY_REWIND, pressed);
410
 
                break;
411
 
        case FAST_FORWARD_OP:
412
 
                debug("AVRCP: FAST FORWARD %s", status);
413
 
                send_key(session->uinput, KEY_FORWARD, pressed);
414
 
                break;
415
 
        default:
416
 
                debug("AVRCP: unknown button 0x%02X %s", operands[0] & 0x7F, status);
417
 
                break;
418
 
        }
419
 
}
420
 
 
421
 
static void avctp_unref(struct avctp *session)
422
 
{
423
 
        sessions = g_slist_remove(sessions, session);
424
 
 
425
 
        if (session->state == AVCTP_STATE_CONNECTED) {
426
 
                gboolean value = FALSE;
427
 
                g_dbus_emit_signal(session->dev->conn, session->dev->path,
 
370
        for (i = 0; key_map[i].name != NULL; i++) {
 
371
                uint8_t key_quirks;
 
372
 
 
373
                if ((operands[0] & 0x7F) != key_map[i].avrcp)
 
374
                        continue;
 
375
 
 
376
                DBG("AVRCP: %s %s", key_map[i].name, status);
 
377
 
 
378
                key_quirks = control->key_quirks[key_map[i].avrcp];
 
379
 
 
380
                if (key_quirks & QUIRK_NO_RELEASE) {
 
381
                        if (!pressed) {
 
382
                                DBG("AVRCP: Ignoring release");
 
383
                                break;
 
384
                        }
 
385
 
 
386
                        DBG("AVRCP: treating key press as press + release");
 
387
                        send_key(control->uinput, key_map[i].uinput, 1);
 
388
                        send_key(control->uinput, key_map[i].uinput, 0);
 
389
                        break;
 
390
                }
 
391
 
 
392
                send_key(control->uinput, key_map[i].uinput, pressed);
 
393
                break;
 
394
        }
 
395
 
 
396
        if (key_map[i].name == NULL)
 
397
                DBG("AVRCP: unknown button 0x%02X %s",
 
398
                                                operands[0] & 0x7F, status);
 
399
}
 
400
 
 
401
static void avctp_disconnected(struct audio_device *dev)
 
402
{
 
403
        struct control *control = dev->control;
 
404
 
 
405
        if (!control)
 
406
                return;
 
407
 
 
408
        if (control->io) {
 
409
                g_io_channel_shutdown(control->io, TRUE, NULL);
 
410
                g_io_channel_unref(control->io);
 
411
                control->io = NULL;
 
412
        }
 
413
 
 
414
        if (control->io_id) {
 
415
                g_source_remove(control->io_id);
 
416
                control->io_id = 0;
 
417
 
 
418
                if (control->state == AVCTP_STATE_CONNECTING)
 
419
                        audio_device_cancel_authorization(dev, auth_cb,
 
420
                                                                control);
 
421
        }
 
422
 
 
423
        if (control->uinput >= 0) {
 
424
                char address[18];
 
425
 
 
426
                ba2str(&dev->dst, address);
 
427
                DBG("AVRCP: closing uinput for %s", address);
 
428
 
 
429
                ioctl(control->uinput, UI_DEV_DESTROY);
 
430
                close(control->uinput);
 
431
                control->uinput = -1;
 
432
        }
 
433
}
 
434
 
 
435
static void avctp_set_state(struct control *control, avctp_state_t new_state)
 
436
{
 
437
        GSList *l;
 
438
        struct audio_device *dev = control->dev;
 
439
        avctp_state_t old_state = control->state;
 
440
        gboolean value;
 
441
 
 
442
        switch (new_state) {
 
443
        case AVCTP_STATE_DISCONNECTED:
 
444
                DBG("AVCTP Disconnected");
 
445
 
 
446
                avctp_disconnected(control->dev);
 
447
 
 
448
                if (old_state != AVCTP_STATE_CONNECTED)
 
449
                        break;
 
450
 
 
451
                value = FALSE;
 
452
                g_dbus_emit_signal(dev->conn, dev->path,
428
453
                                        AUDIO_CONTROL_INTERFACE,
429
454
                                        "Disconnected", DBUS_TYPE_INVALID);
430
 
                emit_property_changed(session->dev->conn, session->dev->path,
 
455
                emit_property_changed(dev->conn, dev->path,
431
456
                                        AUDIO_CONTROL_INTERFACE, "Connected",
432
457
                                        DBUS_TYPE_BOOLEAN, &value);
433
 
        }
434
 
 
435
 
        if (session->io) {
436
 
                g_io_channel_shutdown(session->io, TRUE, NULL);
437
 
                g_io_channel_unref(session->io);
438
 
        }
439
 
 
440
 
        if (session->io_id)
441
 
                g_source_remove(session->io_id);
442
 
 
443
 
        if (session->dev && session->dev->control)
444
 
                session->dev->control->session = NULL;
445
 
 
446
 
        if (session->uinput >= 0) {
447
 
                ioctl(session->uinput, UI_DEV_DESTROY);
448
 
                close(session->uinput);
449
 
        }
450
 
 
451
 
        g_free(session);
 
458
 
 
459
                if (!audio_device_is_active(dev, NULL))
 
460
                        audio_device_set_authorized(dev, FALSE);
 
461
 
 
462
                break;
 
463
        case AVCTP_STATE_CONNECTING:
 
464
                DBG("AVCTP Connecting");
 
465
                break;
 
466
        case AVCTP_STATE_CONNECTED:
 
467
                DBG("AVCTP Connected");
 
468
                value = TRUE;
 
469
                g_dbus_emit_signal(control->dev->conn, control->dev->path,
 
470
                                AUDIO_CONTROL_INTERFACE, "Connected",
 
471
                                DBUS_TYPE_INVALID);
 
472
                emit_property_changed(control->dev->conn, control->dev->path,
 
473
                                AUDIO_CONTROL_INTERFACE, "Connected",
 
474
                                DBUS_TYPE_BOOLEAN, &value);
 
475
                break;
 
476
        default:
 
477
                error("Invalid AVCTP state %d", new_state);
 
478
                return;
 
479
        }
 
480
 
 
481
        control->state = new_state;
 
482
 
 
483
        for (l = avctp_callbacks; l != NULL; l = l->next) {
 
484
                struct avctp_state_callback *cb = l->data;
 
485
                cb->cb(control->dev, old_state, new_state, cb->user_data);
 
486
        }
452
487
}
453
488
 
454
 
static gboolean session_cb(GIOChannel *chan, GIOCondition cond,
 
489
static gboolean control_cb(GIOChannel *chan, GIOCondition cond,
455
490
                                gpointer data)
456
491
{
457
 
        struct avctp *session = data;
 
492
        struct control *control = data;
458
493
        unsigned char buf[1024], *operands;
459
494
        struct avctp_header *avctp;
460
495
        struct avrcp_header *avrcp;
461
496
        int ret, packet_size, operand_count, sock;
462
497
 
463
 
        if (!(cond | G_IO_IN))
 
498
        if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
464
499
                goto failed;
465
500
 
466
 
        sock = g_io_channel_unix_get_fd(session->io);
 
501
        sock = g_io_channel_unix_get_fd(control->io);
467
502
 
468
503
        ret = read(sock, buf, sizeof(buf));
469
504
        if (ret <= 0)
470
505
                goto failed;
471
506
 
472
 
        debug("Got %d bytes of data for AVCTP session %p", ret, session);
 
507
        DBG("Got %d bytes of data for AVCTP session %p", ret, control);
473
508
 
474
509
        if ((unsigned int) ret < sizeof(struct avctp_header)) {
475
510
                error("Too small AVCTP packet");
480
515
 
481
516
        avctp = (struct avctp_header *) buf;
482
517
 
483
 
        debug("AVCTP transaction %u, packet type %u, C/R %u, IPID %u, "
 
518
        DBG("AVCTP transaction %u, packet type %u, C/R %u, IPID %u, "
484
519
                        "PID 0x%04X",
485
520
                        avctp->transaction, avctp->packet_type,
486
521
                        avctp->cr, avctp->ipid, ntohs(avctp->pid));
498
533
        operands = buf + sizeof(struct avctp_header) + sizeof(struct avrcp_header);
499
534
        operand_count = ret;
500
535
 
501
 
        debug("AVRCP %s 0x%01X, subunit_type 0x%02X, subunit_id 0x%01X, "
 
536
        DBG("AVRCP %s 0x%01X, subunit_type 0x%02X, subunit_id 0x%01X, "
502
537
                        "opcode 0x%02X, %d operands",
503
538
                        avctp->cr ? "response" : "command",
504
539
                        avrcp->code, avrcp->subunit_type, avrcp->subunit_id,
515
550
                        avrcp->code == CTYPE_CONTROL &&
516
551
                        avrcp->subunit_type == SUBUNIT_PANEL &&
517
552
                        avrcp->opcode == OP_PASSTHROUGH) {
518
 
                handle_panel_passthrough(session, operands, operand_count);
 
553
                handle_panel_passthrough(control, operands, operand_count);
519
554
                avctp->cr = AVCTP_RESPONSE;
520
555
                avrcp->code = CTYPE_ACCEPTED;
521
556
        } else if (avctp->cr == AVCTP_COMMAND &&
524
559
                        || avrcp->opcode == OP_SUBUNITINFO)) {
525
560
                avctp->cr = AVCTP_RESPONSE;
526
561
                avrcp->code = CTYPE_STABLE;
527
 
                debug("reply to %s", avrcp->opcode == OP_UNITINFO ?
 
562
                /* The first operand should be 0x07 for the UNITINFO response.
 
563
                 * Neither AVRCP (section 22.1, page 117) nor AVC Digital
 
564
                 * Interface Command Set (section 9.2.1, page 45) specs
 
565
                 * explain this value but both use it */
 
566
                if (operand_count >= 1 && avrcp->opcode == OP_UNITINFO)
 
567
                        operands[0] = 0x07;
 
568
                if (operand_count >= 2)
 
569
                        operands[1] = SUBUNIT_PANEL << 3;
 
570
                DBG("reply to %s", avrcp->opcode == OP_UNITINFO ?
528
571
                                "OP_UNITINFO" : "OP_SUBUNITINFO");
529
572
        } else {
530
573
                avctp->cr = AVCTP_RESPONSE;
535
578
        return TRUE;
536
579
 
537
580
failed:
538
 
        debug("AVCTP session %p got disconnected", session);
539
 
        avctp_unref(session);
 
581
        DBG("AVCTP session %p got disconnected", control);
 
582
        avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
540
583
        return FALSE;
541
584
}
542
585
 
543
586
static int uinput_create(char *name)
544
587
{
545
588
        struct uinput_dev dev;
546
 
        int fd, err;
 
589
        int fd, err, i;
547
590
 
548
591
        fd = open("/dev/uinput", O_RDWR);
549
592
        if (fd < 0) {
561
604
 
562
605
        memset(&dev, 0, sizeof(dev));
563
606
        if (name)
564
 
                strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
 
607
                strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE - 1);
565
608
 
566
609
        dev.id.bustype = BUS_BLUETOOTH;
567
610
        dev.id.vendor  = 0x0000;
582
625
        ioctl(fd, UI_SET_EVBIT, EV_REP);
583
626
        ioctl(fd, UI_SET_EVBIT, EV_SYN);
584
627
 
585
 
        ioctl(fd, UI_SET_KEYBIT, KEY_PLAYPAUSE);
586
 
        ioctl(fd, UI_SET_KEYBIT, KEY_STOP);
587
 
        ioctl(fd, UI_SET_KEYBIT, KEY_NEXTSONG);
588
 
        ioctl(fd, UI_SET_KEYBIT, KEY_PREVIOUSSONG);
589
 
        ioctl(fd, UI_SET_KEYBIT, KEY_REWIND);
590
 
        ioctl(fd, UI_SET_KEYBIT, KEY_FORWARD);
 
628
        for (i = 0; key_map[i].name != NULL; i++)
 
629
                ioctl(fd, UI_SET_KEYBIT, key_map[i].uinput);
591
630
 
592
631
        if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
593
632
                err = errno;
601
640
        return fd;
602
641
}
603
642
 
604
 
static void init_uinput(struct avctp *session)
 
643
static void init_uinput(struct control *control)
605
644
{
606
 
        char address[18];
607
 
 
608
 
        ba2str(&session->dst, address);
609
 
 
610
 
        session->uinput = uinput_create(address);
611
 
        if (session->uinput < 0)
 
645
        struct audio_device *dev = control->dev;
 
646
        char address[18], name[248 + 1];
 
647
 
 
648
        device_get_name(dev->btd_dev, name, sizeof(name));
 
649
        if (g_str_equal(name, "Nokia CK-20W")) {
 
650
                control->key_quirks[FORWARD_OP] |= QUIRK_NO_RELEASE;
 
651
                control->key_quirks[BACKWARD_OP] |= QUIRK_NO_RELEASE;
 
652
                control->key_quirks[PLAY_OP] |= QUIRK_NO_RELEASE;
 
653
                control->key_quirks[PAUSE_OP] |= QUIRK_NO_RELEASE;
 
654
        }
 
655
 
 
656
        ba2str(&dev->dst, address);
 
657
 
 
658
        control->uinput = uinput_create(address);
 
659
        if (control->uinput < 0)
612
660
                error("AVRCP: failed to init uinput for %s", address);
613
661
        else
614
 
                debug("AVRCP: uinput initialized for %s", address);
615
 
}
616
 
 
617
 
static void avctp_set_state(struct avctp *session, avctp_state_t new_state)
618
 
{
619
 
        GSList *l;
620
 
        avdtp_session_state_t old_state = session->state;
621
 
 
622
 
        session->state = new_state;
623
 
 
624
 
        for (l = avctp_callbacks; l != NULL; l = l->next) {
625
 
                struct avctp_state_callback *cb = l->data;
626
 
                cb->cb(session->dev, old_state, new_state, cb->user_data);
627
 
        }
 
662
                DBG("AVRCP: uinput initialized for %s", address);
628
663
}
629
664
 
630
665
static void avctp_connect_cb(GIOChannel *chan, GError *err, gpointer data)
631
666
{
632
667
        struct control *control = data;
633
 
        struct avctp *session = control->session;
634
668
        char address[18];
635
669
        uint16_t imtu;
636
670
        GError *gerr = NULL;
637
 
        gboolean value;
638
 
 
639
 
        if (!session) {
640
 
                debug("avctp_connect_cb: session removed while connecting");
641
 
                if (!err)
642
 
                        g_io_channel_shutdown(chan, TRUE, NULL);
643
 
                return;
644
 
        }
645
671
 
646
672
        if (err) {
647
 
                avctp_unref(session);
 
673
                avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
648
674
                error("%s", err->message);
649
675
                return;
650
676
        }
654
680
                        BT_IO_OPT_IMTU, &imtu,
655
681
                        BT_IO_OPT_INVALID);
656
682
        if (gerr) {
657
 
                avctp_unref(session);
 
683
                avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
658
684
                error("%s", gerr->message);
659
685
                g_error_free(gerr);
660
 
                g_io_channel_shutdown(chan, TRUE, NULL);
661
686
                return;
662
687
        }
663
688
 
664
 
        debug("AVCTP: connected to %s", address);
665
 
 
666
 
        if (!session->io)
667
 
                session->io = g_io_channel_ref(chan);
668
 
 
669
 
        init_uinput(session);
670
 
 
671
 
        value = TRUE;
672
 
        g_dbus_emit_signal(session->dev->conn, session->dev->path,
673
 
                                AUDIO_CONTROL_INTERFACE, "Connected",
674
 
                                DBUS_TYPE_INVALID);
675
 
        emit_property_changed(session->dev->conn, session->dev->path,
676
 
                                AUDIO_CONTROL_INTERFACE, "Connected",
677
 
                                DBUS_TYPE_BOOLEAN, &value);
678
 
 
679
 
        avctp_set_state(session, AVCTP_STATE_CONNECTED);
680
 
        session->mtu = imtu;
681
 
        session->io_id = g_io_add_watch(chan,
 
689
        DBG("AVCTP: connected to %s", address);
 
690
 
 
691
        if (!control->io)
 
692
                control->io = g_io_channel_ref(chan);
 
693
 
 
694
        init_uinput(control);
 
695
 
 
696
        avctp_set_state(control, AVCTP_STATE_CONNECTED);
 
697
        control->mtu = imtu;
 
698
        control->io_id = g_io_add_watch(chan,
682
699
                                G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
683
 
                                (GIOFunc) session_cb, session);
 
700
                                (GIOFunc) control_cb, control);
684
701
}
685
702
 
686
703
static void auth_cb(DBusError *derr, void *user_data)
687
704
{
688
705
        struct control *control = user_data;
689
 
        struct avctp *session = control->session;
690
706
        GError *err = NULL;
691
707
 
 
708
        if (control->io_id) {
 
709
                g_source_remove(control->io_id);
 
710
                control->io_id = 0;
 
711
        }
 
712
 
692
713
        if (derr && dbus_error_is_set(derr)) {
693
714
                error("Access denied: %s", derr->message);
694
 
                avctp_unref(session);
 
715
                avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
695
716
                return;
696
717
        }
697
718
 
698
 
        if (!bt_io_accept(session->io, avctp_connect_cb, control,
 
719
        if (!bt_io_accept(control->io, avctp_connect_cb, control,
699
720
                                                                NULL, &err)) {
700
721
                error("bt_io_accept: %s", err->message);
701
722
                g_error_free(err);
702
 
                avctp_unref(session);
 
723
                avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
703
724
        }
704
725
}
705
726
 
706
727
static void avctp_confirm_cb(GIOChannel *chan, gpointer data)
707
728
{
708
 
        struct avctp *session;
 
729
        struct control *control = NULL;
709
730
        struct audio_device *dev;
710
731
        char address[18];
711
732
        bdaddr_t src, dst;
723
744
                return;
724
745
        }
725
746
 
726
 
        session = avctp_get(&src, &dst);
727
 
 
728
 
        if (!session) {
729
 
                error("Unable to create new AVCTP session");
730
 
                goto drop;
731
 
        }
732
 
 
733
 
        if (session->io) {
734
 
                error("Refusing unexpected connect from %s", address);
735
 
                goto drop;
736
 
        }
737
 
 
738
 
        dev = manager_get_device(&src, &dst);
 
747
        dev = manager_get_device(&src, &dst, TRUE);
739
748
        if (!dev) {
740
749
                error("Unable to get audio device object for %s", address);
741
750
                goto drop;
742
751
        }
743
752
 
744
 
        if (!dev->control)
745
 
                dev->control = control_init(dev);
746
 
 
747
 
        if (!dev->control->session)
748
 
                dev->control->session = session;
749
 
 
750
 
        if (!session->dev)
751
 
                session->dev = dev;
752
 
 
753
 
        avctp_set_state(session, AVCTP_STATE_CONNECTING);
754
 
        session->io = g_io_channel_ref(chan);
755
 
 
756
 
        if (avdtp_is_connected(&src, &dst)) {
757
 
                if (!bt_io_accept(chan, avctp_connect_cb, dev->control,
758
 
                                                                NULL, NULL))
 
753
        if (!dev->control) {
 
754
                btd_device_add_uuid(dev->btd_dev, AVRCP_REMOTE_UUID);
 
755
                if (!dev->control)
759
756
                        goto drop;
760
 
        } else if (btd_request_authorization(&src, &dst,
761
 
                                AVRCP_TARGET_UUID, auth_cb, dev->control) < 0)
762
 
                goto drop;
763
 
 
 
757
        }
 
758
 
 
759
        control = dev->control;
 
760
 
 
761
        if (control->io) {
 
762
                error("Refusing unexpected connect from %s", address);
 
763
                goto drop;
 
764
        }
 
765
 
 
766
        avctp_set_state(control, AVCTP_STATE_CONNECTING);
 
767
        control->io = g_io_channel_ref(chan);
 
768
 
 
769
        if (audio_device_request_authorization(dev, AVRCP_TARGET_UUID,
 
770
                                                auth_cb, dev->control) < 0)
 
771
                goto drop;
 
772
 
 
773
        control->io_id = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
 
774
                                                        control_cb, control);
764
775
        return;
765
776
 
766
777
drop:
767
 
        if (!session->io)
 
778
        if (!control || !control->io)
768
779
                g_io_channel_shutdown(chan, TRUE, NULL);
769
 
        avctp_unref(session);
 
780
        if (control)
 
781
                avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
770
782
}
771
783
 
772
784
static GIOChannel *avctp_server_socket(const bdaddr_t *src, gboolean master)
792
804
gboolean avrcp_connect(struct audio_device *dev)
793
805
{
794
806
        struct control *control = dev->control;
795
 
        struct avctp *session;
796
807
        GError *err = NULL;
797
808
        GIOChannel *io;
798
809
 
799
 
        if (control->session)
 
810
        if (control->state > AVCTP_STATE_DISCONNECTED)
800
811
                return TRUE;
801
812
 
802
 
        session = avctp_get(&dev->src, &dev->dst);
803
 
        if (!session) {
804
 
                error("Unable to create new AVCTP session");
805
 
                return FALSE;
806
 
        }
807
 
 
808
 
        session->dev = dev;
809
 
        avctp_set_state(session, AVCTP_STATE_CONNECTING);
 
813
        avctp_set_state(control, AVCTP_STATE_CONNECTING);
810
814
 
811
815
        io = bt_io_connect(BT_IO_L2CAP, avctp_connect_cb, control, NULL, &err,
812
816
                                BT_IO_OPT_SOURCE_BDADDR, &dev->src,
814
818
                                BT_IO_OPT_PSM, AVCTP_PSM,
815
819
                                BT_IO_OPT_INVALID);
816
820
        if (err) {
817
 
                avctp_unref(session);
 
821
                avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
818
822
                error("%s", err->message);
819
823
                g_error_free(err);
820
824
                return FALSE;
821
825
        }
822
826
 
823
 
        g_io_channel_unref(io);
824
 
 
825
 
        control->session = session;
 
827
        control->io = io;
826
828
 
827
829
        return TRUE;
828
830
}
830
832
void avrcp_disconnect(struct audio_device *dev)
831
833
{
832
834
        struct control *control = dev->control;
833
 
        struct avctp *session = control->session;
834
835
 
835
 
        if (!session)
 
836
        if (!(control && control->io))
836
837
                return;
837
838
 
838
 
        avctp_unref(session);
839
 
        control->session = NULL;
 
839
        avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
840
840
}
841
841
 
842
842
int avrcp_register(DBusConnection *conn, const bdaddr_t *src, GKeyFile *config)
850
850
                tmp = g_key_file_get_boolean(config, "General",
851
851
                                                        "Master", &err);
852
852
                if (err) {
853
 
                        debug("audio.conf: %s", err->message);
 
853
                        DBG("audio.conf: %s", err->message);
854
854
                        g_error_free(err);
855
855
                } else
856
856
                        master = tmp;
866
866
        record = avrcp_tg_record();
867
867
        if (!record) {
868
868
                error("Unable to allocate new service record");
 
869
                g_free(server);
869
870
                return -1;
870
871
        }
871
872
 
872
873
        if (add_record_to_server(src, record) < 0) {
873
874
                error("Unable to register AVRCP target service record");
 
875
                g_free(server);
874
876
                sdp_record_free(record);
875
877
                return -1;
876
878
        }
879
881
        record = avrcp_ct_record();
880
882
        if (!record) {
881
883
                error("Unable to allocate new service record");
 
884
                g_free(server);
882
885
                return -1;
883
886
        }
884
887
 
885
888
        if (add_record_to_server(src, record) < 0) {
886
889
                error("Unable to register AVRCP controller service record");
887
890
                sdp_record_free(record);
 
891
                g_free(server);
888
892
                return -1;
889
893
        }
890
894
        server->ct_record_id = record->handle;
955
959
        if (!reply)
956
960
                return NULL;
957
961
 
958
 
        connected = (control->session != NULL);
 
962
        connected = (control->state == AVCTP_STATE_CONNECTED);
959
963
 
960
964
        dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected,
961
965
                                        DBUS_TYPE_INVALID);
963
967
        return reply;
964
968
}
965
969
 
 
970
static int avctp_send_passthrough(struct control *control, uint8_t op)
 
971
{
 
972
        unsigned char buf[AVCTP_HEADER_LENGTH + AVRCP_HEADER_LENGTH + 2];
 
973
        struct avctp_header *avctp = (void *) buf;
 
974
        struct avrcp_header *avrcp = (void *) &buf[AVCTP_HEADER_LENGTH];
 
975
        uint8_t *operands = &buf[AVCTP_HEADER_LENGTH + AVRCP_HEADER_LENGTH];
 
976
        int err, sk = g_io_channel_unix_get_fd(control->io);
 
977
        static uint8_t transaction = 0;
 
978
 
 
979
        memset(buf, 0, sizeof(buf));
 
980
 
 
981
        avctp->transaction = transaction++;
 
982
        avctp->packet_type = AVCTP_PACKET_SINGLE;
 
983
        avctp->cr = AVCTP_COMMAND;
 
984
        avctp->pid = htons(AV_REMOTE_SVCLASS_ID);
 
985
 
 
986
        avrcp->code = CTYPE_CONTROL;
 
987
        avrcp->subunit_type = SUBUNIT_PANEL;
 
988
        avrcp->opcode = OP_PASSTHROUGH;
 
989
 
 
990
        operands[0] = op & 0x7f;
 
991
        operands[1] = 0;
 
992
 
 
993
        err = write(sk, buf, sizeof(buf));
 
994
        if (err < 0)
 
995
                return err;
 
996
 
 
997
        /* Button release */
 
998
        avctp->transaction = transaction++;
 
999
        operands[0] |= 0x80;
 
1000
 
 
1001
        return write(sk, buf, sizeof(buf));
 
1002
}
 
1003
 
 
1004
static DBusMessage *volume_up(DBusConnection *conn, DBusMessage *msg,
 
1005
                                                                void *data)
 
1006
{
 
1007
        struct audio_device *device = data;
 
1008
        struct control *control = device->control;
 
1009
        DBusMessage *reply;
 
1010
        int err;
 
1011
 
 
1012
        reply = dbus_message_new_method_return(msg);
 
1013
        if (!reply)
 
1014
                return NULL;
 
1015
 
 
1016
        if (control->state != AVCTP_STATE_CONNECTED)
 
1017
                return g_dbus_create_error(msg,
 
1018
                                        ERROR_INTERFACE ".NotConnected",
 
1019
                                        "Device not Connected");
 
1020
 
 
1021
        if (!control->target)
 
1022
                return g_dbus_create_error(msg,
 
1023
                                        ERROR_INTERFACE ".NotSupported",
 
1024
                                        "AVRCP Target role not supported");
 
1025
 
 
1026
        err = avctp_send_passthrough(control, VOL_UP_OP);
 
1027
        if (err < 0)
 
1028
                return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
 
1029
                                                        "%s", strerror(-err));
 
1030
 
 
1031
        return dbus_message_new_method_return(msg);
 
1032
}
 
1033
 
 
1034
static DBusMessage *volume_down(DBusConnection *conn, DBusMessage *msg,
 
1035
                                                                void *data)
 
1036
{
 
1037
        struct audio_device *device = data;
 
1038
        struct control *control = device->control;
 
1039
        DBusMessage *reply;
 
1040
        int err;
 
1041
 
 
1042
        reply = dbus_message_new_method_return(msg);
 
1043
        if (!reply)
 
1044
                return NULL;
 
1045
 
 
1046
        if (control->state != AVCTP_STATE_CONNECTED)
 
1047
                return g_dbus_create_error(msg,
 
1048
                                        ERROR_INTERFACE ".NotConnected",
 
1049
                                        "Device not Connected");
 
1050
 
 
1051
        if (!control->target)
 
1052
                return g_dbus_create_error(msg,
 
1053
                                        ERROR_INTERFACE ".NotSupported",
 
1054
                                        "AVRCP Target role not supported");
 
1055
 
 
1056
        err = avctp_send_passthrough(control, VOL_DOWN_OP);
 
1057
        if (err < 0)
 
1058
                return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
 
1059
                                                        "%s", strerror(-err));
 
1060
 
 
1061
        return dbus_message_new_method_return(msg);
 
1062
}
 
1063
 
966
1064
static DBusMessage *control_get_properties(DBusConnection *conn,
967
1065
                                        DBusMessage *msg, void *data)
968
1066
{
984
1082
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
985
1083
 
986
1084
        /* Connected */
987
 
        value = (device->control->session != NULL);
 
1085
        value = (device->control->state == AVCTP_STATE_CONNECTED);
988
1086
        dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &value);
989
1087
 
990
1088
        dbus_message_iter_close_container(&iter, &dict);
996
1094
        { "IsConnected",        "",     "b",    control_is_connected,
997
1095
                                                G_DBUS_METHOD_FLAG_DEPRECATED },
998
1096
        { "GetProperties",      "",     "a{sv}",control_get_properties },
 
1097
        { "VolumeUp",           "",     "",     volume_up },
 
1098
        { "VolumeDown",         "",     "",     volume_down },
999
1099
        { NULL, NULL, NULL, NULL }
1000
1100
};
1001
1101
 
1006
1106
        { NULL, NULL }
1007
1107
};
1008
1108
 
1009
 
static void control_free(struct audio_device *dev)
 
1109
static void path_unregister(void *data)
1010
1110
{
 
1111
        struct audio_device *dev = data;
1011
1112
        struct control *control = dev->control;
1012
1113
 
1013
 
        if (control->session)
1014
 
                avctp_unref(control->session);
 
1114
        DBG("Unregistered interface %s on path %s",
 
1115
                AUDIO_CONTROL_INTERFACE, dev->path);
 
1116
 
 
1117
        if (control->state != AVCTP_STATE_DISCONNECTED)
 
1118
                avctp_disconnected(dev);
1015
1119
 
1016
1120
        g_free(control);
1017
1121
        dev->control = NULL;
1018
1122
}
1019
1123
 
1020
 
static void path_unregister(void *data)
1021
 
{
1022
 
        struct audio_device *dev = data;
1023
 
 
1024
 
        debug("Unregistered interface %s on path %s",
1025
 
                AUDIO_CONTROL_INTERFACE, dev->path);
1026
 
 
1027
 
        control_free(dev);
1028
 
}
1029
 
 
1030
1124
void control_unregister(struct audio_device *dev)
1031
1125
{
1032
1126
        g_dbus_unregister_interface(dev->conn, dev->path,
1033
1127
                AUDIO_CONTROL_INTERFACE);
1034
1128
}
1035
1129
 
1036
 
struct control *control_init(struct audio_device *dev)
1037
 
{
 
1130
void control_update(struct audio_device *dev, uint16_t uuid16)
 
1131
{
 
1132
        struct control *control = dev->control;
 
1133
 
 
1134
        if (uuid16 == AV_REMOTE_TARGET_SVCLASS_ID)
 
1135
                control->target = TRUE;
 
1136
}
 
1137
 
 
1138
struct control *control_init(struct audio_device *dev, uint16_t uuid16)
 
1139
{
 
1140
        struct control *control;
 
1141
 
1038
1142
        if (!g_dbus_register_interface(dev->conn, dev->path,
1039
1143
                                        AUDIO_CONTROL_INTERFACE,
1040
1144
                                        control_methods, control_signals, NULL,
1041
1145
                                        dev, path_unregister))
1042
1146
                return NULL;
1043
1147
 
1044
 
        debug("Registered interface %s on path %s",
 
1148
        DBG("Registered interface %s on path %s",
1045
1149
                AUDIO_CONTROL_INTERFACE, dev->path);
1046
1150
 
1047
 
        return g_new0(struct control, 1);
 
1151
        control = g_new0(struct control, 1);
 
1152
        control->dev = dev;
 
1153
        control->state = AVCTP_STATE_DISCONNECTED;
 
1154
        control->uinput = -1;
 
1155
 
 
1156
        if (uuid16 == AV_REMOTE_TARGET_SVCLASS_ID)
 
1157
                control->target = TRUE;
 
1158
 
 
1159
        return control;
1048
1160
}
1049
1161
 
1050
1162
gboolean control_is_active(struct audio_device *dev)
1051
1163
{
1052
1164
        struct control *control = dev->control;
1053
1165
 
1054
 
        if (control->session &&
1055
 
                        control->session->state != AVCTP_STATE_DISCONNECTED)
 
1166
        if (control && control->state != AVCTP_STATE_DISCONNECTED)
1056
1167
                return TRUE;
1057
1168
 
1058
1169
        return FALSE;