3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2007 Nokia Corporation
6 * Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/types.h>
38 #include <netinet/in.h>
40 #include <bluetooth/bluetooth.h>
41 #include <bluetooth/sdp.h>
42 #include <bluetooth/sdp_lib.h>
43 #include <bluetooth/l2cap.h>
46 #include <dbus/dbus.h>
49 #include "dbus-service.h"
57 #include "glib-helper.h"
62 #define AVCTP_COMMAND 0
63 #define AVCTP_RESPONSE 1
66 #define AVCTP_PACKET_SINGLE 0
67 #define AVCTP_PACKET_START 1
68 #define AVCTP_PACKET_CONTINUE 2
69 #define AVCTP_PACKET_END 3
72 #define CTYPE_CONTROL 0x0
73 #define CTYPE_STATUS 0x1
74 #define CTYPE_ACCEPTED 0x9
75 #define CTYPE_STABLE 0xC
78 #define OP_UNITINFO 0x30
79 #define OP_SUBUNITINFO 0x31
80 #define OP_PASSTHROUGH 0x7c
82 /* subunits of interest */
83 #define SUBUNIT_PANEL 0x09
85 /* operands in passthrough commands */
87 #define VOLDOWN_OP 0x42
93 #define REWIND_OP 0x48
94 #define FAST_FORWARD_OP 0x49
98 static DBusConnection *connection = NULL;
100 static uint32_t tg_record_id = 0;
102 static uint32_t ct_record_id = 0;
105 static GIOChannel *avctp_server = NULL;
106 static gchar *input_device_name = NULL;
107 static GSList *sessions = NULL;
110 AVCTP_STATE_DISCONNECTED = 0,
111 AVCTP_STATE_CONNECTING,
112 AVCTP_STATE_CONNECTED
115 #if __BYTE_ORDER == __LITTLE_ENDIAN
117 struct avctp_header {
120 uint8_t packet_type:2;
121 uint8_t transaction:4;
123 } __attribute__ ((packed));
125 struct avrcp_header {
128 uint8_t subunit_id:3;
129 uint8_t subunit_type:5;
131 } __attribute__ ((packed));
133 #elif __BYTE_ORDER == __BIG_ENDIAN
135 struct avctp_header {
136 uint8_t transaction:4;
137 uint8_t packet_type:2;
141 } __attribute__ ((packed));
143 struct avrcp_header {
146 uint8_t subunit_type:5;
147 uint8_t subunit_id:3;
149 } __attribute__ ((packed));
152 #error "Unknown byte order"
156 struct audio_device *dev;
173 struct avctp *session;
176 static sdp_record_t *avrcp_ct_record()
178 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
179 uuid_t root_uuid, l2cap, avctp, avrct;
180 sdp_profile_desc_t profile[1];
181 sdp_list_t *aproto, *proto[2];
182 sdp_record_t *record;
183 sdp_data_t *psm, *version, *features;
184 int16_t lp = AVCTP_PSM, ver = 0x0100, feat = 0x000f;
186 record = sdp_record_alloc();
190 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
191 root = sdp_list_append(0, &root_uuid);
192 sdp_set_browse_groups(record, root);
194 /* Service Class ID List */
195 sdp_uuid16_create(&avrct, AV_REMOTE_SVCLASS_ID);
196 svclass_id = sdp_list_append(0, &avrct);
197 sdp_set_service_classes(record, svclass_id);
199 /* Protocol Descriptor List */
200 sdp_uuid16_create(&l2cap, L2CAP_UUID);
201 proto[0] = sdp_list_append(0, &l2cap);
202 psm = sdp_data_alloc(SDP_UINT16, &lp);
203 proto[0] = sdp_list_append(proto[0], psm);
204 apseq = sdp_list_append(0, proto[0]);
206 sdp_uuid16_create(&avctp, AVCTP_UUID);
207 proto[1] = sdp_list_append(0, &avctp);
208 version = sdp_data_alloc(SDP_UINT16, &ver);
209 proto[1] = sdp_list_append(proto[1], version);
210 apseq = sdp_list_append(apseq, proto[1]);
212 aproto = sdp_list_append(0, apseq);
213 sdp_set_access_protos(record, aproto);
215 /* Bluetooth Profile Descriptor List */
216 sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
217 profile[0].version = ver;
218 pfseq = sdp_list_append(0, &profile[0]);
219 sdp_set_profile_descs(record, pfseq);
221 features = sdp_data_alloc(SDP_UINT16, &feat);
222 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
224 sdp_set_info_attr(record, "AVRCP CT", 0, 0);
228 sdp_list_free(proto[0], 0);
229 sdp_list_free(proto[1], 0);
230 sdp_list_free(apseq, 0);
231 sdp_list_free(pfseq, 0);
232 sdp_list_free(aproto, 0);
233 sdp_list_free(root, 0);
234 sdp_list_free(svclass_id, 0);
239 static sdp_record_t *avrcp_tg_record()
241 sdp_list_t *svclass_id, *pfseq, *apseq, *root;
242 uuid_t root_uuid, l2cap, avctp, avrtg;
243 sdp_profile_desc_t profile[1];
244 sdp_list_t *aproto, *proto[2];
245 sdp_record_t *record;
246 sdp_data_t *psm, *version, *features;
247 uint16_t lp = AVCTP_PSM, ver = 0x0100, feat = 0x000f;
249 record = sdp_record_alloc();
253 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
254 root = sdp_list_append(0, &root_uuid);
255 sdp_set_browse_groups(record, root);
257 /* Service Class ID List */
258 sdp_uuid16_create(&avrtg, AV_REMOTE_TARGET_SVCLASS_ID);
259 svclass_id = sdp_list_append(0, &avrtg);
260 sdp_set_service_classes(record, svclass_id);
262 /* Protocol Descriptor List */
263 sdp_uuid16_create(&l2cap, L2CAP_UUID);
264 proto[0] = sdp_list_append(0, &l2cap);
265 psm = sdp_data_alloc(SDP_UINT16, &lp);
266 proto[0] = sdp_list_append(proto[0], psm);
267 apseq = sdp_list_append(0, proto[0]);
269 sdp_uuid16_create(&avctp, AVCTP_UUID);
270 proto[1] = sdp_list_append(0, &avctp);
271 version = sdp_data_alloc(SDP_UINT16, &ver);
272 proto[1] = sdp_list_append(proto[1], version);
273 apseq = sdp_list_append(apseq, proto[1]);
275 aproto = sdp_list_append(0, apseq);
276 sdp_set_access_protos(record, aproto);
278 /* Bluetooth Profile Descriptor List */
279 sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
280 profile[0].version = ver;
281 pfseq = sdp_list_append(0, &profile[0]);
282 sdp_set_profile_descs(record, pfseq);
284 features = sdp_data_alloc(SDP_UINT16, &feat);
285 sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
287 sdp_set_info_attr(record, "AVRCP TG", 0, 0);
291 sdp_list_free(proto[0], 0);
292 sdp_list_free(proto[1], 0);
293 sdp_list_free(apseq, 0);
294 sdp_list_free(aproto, 0);
295 sdp_list_free(pfseq, 0);
296 sdp_list_free(root, 0);
297 sdp_list_free(svclass_id, 0);
302 static struct avctp *find_session(const bdaddr_t *src, const bdaddr_t *dst)
306 for (l = sessions; l != NULL; l = g_slist_next(l)) {
307 struct avctp *s = l->data;
309 if (bacmp(src, &s->src) || bacmp(dst, &s->dst))
318 static struct avctp *avctp_get(const bdaddr_t *src, const bdaddr_t *dst)
320 struct avctp *session;
325 session = find_session(src, dst);
329 session = g_new0(struct avctp, 1);
331 session->uinput = -1;
333 bacpy(&session->src, src);
334 bacpy(&session->dst, dst);
336 sessions = g_slist_append(sessions, session);
341 static int send_event(int fd, uint16_t type, uint16_t code, int32_t value)
343 struct uinput_event event;
345 memset(&event, 0, sizeof(event));
350 return write(fd, &event, sizeof(event));
353 static void send_key(int fd, uint16_t key, int pressed)
358 send_event(fd, EV_KEY, key, pressed);
359 send_event(fd, EV_SYN, SYN_REPORT, 0);
362 static void handle_panel_passthrough(struct avctp *session,
363 const unsigned char *operands,
369 if (operand_count == 0)
372 if (operands[0] & 0x80) {
380 switch (operands[0] & 0x7F) {
382 debug("AVRCP: PLAY %s", status);
383 send_key(session->uinput, KEY_PLAYPAUSE, pressed);
386 debug("AVRCP: STOP %s", status);
387 send_key(session->uinput, KEY_STOP, pressed);
390 debug("AVRCP: PAUSE %s", status);
391 send_key(session->uinput, KEY_PLAYPAUSE, pressed);
394 debug("AVRCP: NEXT %s", status);
395 send_key(session->uinput, KEY_NEXTSONG, pressed);
398 debug("AVRCP: PREV %s", status);
399 send_key(session->uinput, KEY_PREVIOUSSONG, pressed);
402 debug("AVRCP: REWIND %s", status);
403 send_key(session->uinput, KEY_REWIND, pressed);
405 case FAST_FORWARD_OP:
406 debug("AVRCP: FAST FORWARD %s", status);
407 send_key(session->uinput, KEY_FORWARD, pressed);
410 debug("AVRCP: unknown button 0x%02X %s", operands[0] & 0x7F, status);
415 static void avctp_unref(struct avctp *session)
417 sessions = g_slist_remove(sessions, session);
419 if (session->state == AVCTP_STATE_CONNECTED)
420 g_dbus_emit_signal(session->dev->conn,
422 AUDIO_CONTROL_INTERFACE,
425 if (session->sock >= 0)
426 close(session->sock);
428 g_source_remove(session->io);
430 if (session->dev && session->dev->control)
431 session->dev->control->session = NULL;
433 if (session->uinput >= 0) {
434 ioctl(session->uinput, UI_DEV_DESTROY);
435 close(session->uinput);
441 static gboolean session_cb(GIOChannel *chan, GIOCondition cond,
444 struct avctp *session = data;
445 unsigned char buf[1024], *operands;
446 struct avctp_header *avctp;
447 struct avrcp_header *avrcp;
448 int ret, packet_size, operand_count;
450 if (!(cond | G_IO_IN))
453 ret = read(session->sock, buf, sizeof(buf));
457 debug("Got %d bytes of data for AVCTP session %p", ret, session);
459 if (ret < sizeof(struct avctp_header)) {
460 error("Too small AVCTP packet");
466 avctp = (struct avctp_header *) buf;
468 debug("AVCTP transaction %u, packet type %u, C/R %u, IPID %u, "
470 avctp->transaction, avctp->packet_type,
471 avctp->cr, avctp->ipid, ntohs(avctp->pid));
473 ret -= sizeof(struct avctp_header);
474 if (ret < sizeof(struct avrcp_header)) {
475 error("Too small AVRCP packet");
479 avrcp = (struct avrcp_header *) (buf + sizeof(struct avctp_header));
481 ret -= sizeof(struct avrcp_header);
483 operands = buf + sizeof(struct avctp_header) + sizeof(struct avrcp_header);
486 debug("AVRCP %s 0x%01X, subunit_type 0x%02X, subunit_id 0x%01X, "
487 "opcode 0x%02X, %d operands",
488 avctp->cr ? "response" : "command",
489 avrcp->code, avrcp->subunit_type, avrcp->subunit_id,
490 avrcp->opcode, operand_count);
492 if (avctp->packet_type == AVCTP_PACKET_SINGLE &&
493 avctp->cr == AVCTP_COMMAND &&
494 avctp->pid == htons(AV_REMOTE_SVCLASS_ID) &&
495 avrcp->code == CTYPE_CONTROL &&
496 avrcp->subunit_type == SUBUNIT_PANEL &&
497 avrcp->opcode == OP_PASSTHROUGH) {
498 handle_panel_passthrough(session, operands, operand_count);
499 avctp->cr = AVCTP_RESPONSE;
500 avrcp->code = CTYPE_ACCEPTED;
501 ret = write(session->sock, buf, packet_size);
504 if (avctp->packet_type == AVCTP_PACKET_SINGLE &&
505 avctp->cr == AVCTP_COMMAND &&
506 avctp->pid == htons(AV_REMOTE_SVCLASS_ID) &&
507 avrcp->code == CTYPE_STATUS &&
508 (avrcp->opcode == OP_UNITINFO
509 || avrcp->opcode == OP_SUBUNITINFO)) {
510 avctp->cr = AVCTP_RESPONSE;
511 avrcp->code = CTYPE_STABLE;
512 debug("reply to %s", avrcp->opcode == OP_UNITINFO ?
513 "OP_UNITINFO" : "OP_SUBUNITINFO");
514 ret = write(session->sock, buf, packet_size);
520 debug("AVCTP session %p got disconnected", session);
521 avctp_unref(session);
525 static int uinput_create(char *name)
527 struct uinput_dev dev;
530 fd = open("/dev/uinput", O_RDWR);
532 fd = open("/dev/input/uinput", O_RDWR);
534 fd = open("/dev/misc/uinput", O_RDWR);
537 error("Can't open input device: %s (%d)",
544 memset(&dev, 0, sizeof(dev));
546 strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
548 dev.id.bustype = BUS_BLUETOOTH;
549 dev.id.vendor = 0x0000;
550 dev.id.product = 0x0000;
551 dev.id.version = 0x0000;
553 if (write(fd, &dev, sizeof(dev)) < 0) {
555 error("Can't write device information: %s (%d)",
562 ioctl(fd, UI_SET_EVBIT, EV_KEY);
563 ioctl(fd, UI_SET_EVBIT, EV_REL);
564 ioctl(fd, UI_SET_EVBIT, EV_REP);
565 ioctl(fd, UI_SET_EVBIT, EV_SYN);
567 ioctl(fd, UI_SET_KEYBIT, KEY_PLAYPAUSE);
568 ioctl(fd, UI_SET_KEYBIT, KEY_STOP);
569 ioctl(fd, UI_SET_KEYBIT, KEY_NEXTSONG);
570 ioctl(fd, UI_SET_KEYBIT, KEY_PREVIOUSSONG);
571 ioctl(fd, UI_SET_KEYBIT, KEY_REWIND);
572 ioctl(fd, UI_SET_KEYBIT, KEY_FORWARD);
574 if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
576 error("Can't create uinput device: %s (%d)",
586 static void init_uinput(struct avctp *session)
588 char address[18], *name;
591 ba2str(&session->dst, address);
593 /* use device name from config file if specified */
594 name = input_device_name;
596 name = session->dev->name ? session->dev->name : address;
598 session->uinput = uinput_create(name);
599 if (session->uinput < 0)
600 error("AVRCP: failed to init uinput for %s", name);
602 debug("AVRCP: uinput initialized for %s", name);
605 static void avctp_connect_session(struct avctp *session)
609 session->state = AVCTP_STATE_CONNECTED;
610 session->dev = manager_device_connected(&session->dst,
612 session->dev->control->session = session;
614 init_uinput(session);
616 g_dbus_emit_signal(session->dev->conn, session->dev->path,
617 AUDIO_CONTROL_INTERFACE, "Connected",
621 g_source_remove(session->io);
623 io = g_io_channel_unix_new(session->sock);
624 session->io = g_io_add_watch(io,
625 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
626 (GIOFunc) session_cb, session);
627 g_io_channel_unref(io);
630 static void auth_cb(DBusError *derr, void *user_data)
632 struct avctp *session = user_data;
634 if (derr && dbus_error_is_set(derr)) {
635 error("Access denied: %s", derr->message);
636 if (dbus_error_has_name(derr, DBUS_ERROR_NO_REPLY)) {
637 debug("Canceling authorization request");
638 service_cancel_auth(&session->src, &session->dst);
641 avctp_unref(session);
645 avctp_connect_session(session);
648 static void avctp_server_cb(GIOChannel *chan, int err, const bdaddr_t *src,
649 const bdaddr_t *dst, gpointer data)
652 struct l2cap_options l2o;
653 struct avctp *session;
654 GIOCondition flags = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
655 struct audio_device *dev;
659 error("AVCTP server socket: %s (%d)", strerror(-err), -err);
663 session = avctp_get(src, dst);
666 error("Unable to create new AVCTP session");
670 if (session->sock >= 0) {
671 error("Refusing unexpected connect from %s", address);
675 dev = manager_find_device(&session->dst, AUDIO_CONTROL_INTERFACE, FALSE);
678 error("Unable to get audio device object for %s", address);
683 dev->control = control_init(dev);
685 device_remove_control_timer(dev);
687 session->state = AVCTP_STATE_CONNECTING;
688 session->sock = g_io_channel_unix_get_fd(chan);
690 memset(&l2o, 0, sizeof(l2o));
692 if (getsockopt(session->sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &size) < 0) {
694 error("getsockopt(L2CAP_OPTIONS): %s (%d)", strerror(err),
696 avctp_unref(session);
700 session->mtu = l2o.imtu;
702 session->io = g_io_add_watch(chan, flags, (GIOFunc) session_cb,
704 g_io_channel_unref(chan);
706 if (avdtp_is_connected(src, dst))
709 if (service_req_auth(src, dst, AVRCP_TARGET_UUID, auth_cb, session) < 0)
715 avctp_connect_session(session);
720 close(session->sock);
723 static GIOChannel *avctp_server_socket(gboolean master)
728 lm = L2CAP_LM_SECURE;
731 lm |= L2CAP_LM_MASTER;
733 io = bt_l2cap_listen(BDADDR_ANY, AVCTP_PSM, 0, lm, avctp_server_cb,
736 error("Unable to allocate new io channel");
743 static void avctp_connect_cb(GIOChannel *chan, int err, const bdaddr_t *src,
744 const bdaddr_t *dst, gpointer data)
746 struct avctp *session = data;
747 struct l2cap_options l2o;
753 avctp_unref(session);
754 error("AVCTP connect(%s): %s (%d)", address, strerror(-err),
759 ba2str(&session->dst, address);
760 debug("AVCTP: connected to %s", address);
762 g_io_channel_set_close_on_unref(chan, FALSE);
763 sk = g_io_channel_unix_get_fd(chan);
766 memset(&l2o, 0, sizeof(l2o));
768 if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
770 avctp_unref(session);
771 error("getsockopt(L2CAP_OPTIONS): %s (%d)", strerror(err),
776 init_uinput(session);
778 g_dbus_emit_signal(session->dev->conn, session->dev->path,
779 AUDIO_CONTROL_INTERFACE, "Connected",
782 session->state = AVCTP_STATE_CONNECTED;
783 session->mtu = l2o.imtu;
784 session->io = g_io_add_watch(chan,
785 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
786 (GIOFunc) session_cb, session);
789 gboolean avrcp_connect(struct audio_device *dev)
791 struct control *control = dev->control;
792 struct avctp *session;
795 if (control->session)
798 session = avctp_get(&dev->src, &dev->dst);
800 error("Unable to create new AVCTP session");
804 device_remove_control_timer(dev);
807 session->state = AVCTP_STATE_CONNECTING;
809 err = bt_l2cap_connect(&dev->src, &dev->dst, AVCTP_PSM, 0,
810 avctp_connect_cb, session);
812 avctp_unref(session);
813 error("Connect failed. %s(%d)", strerror(-err), -err);
817 control->session = session;
822 void avrcp_disconnect(struct audio_device *dev)
824 struct control *control = dev->control;
825 struct avctp *session = control->session;
830 avctp_unref(session);
831 control->session = NULL;
834 int avrcp_init(DBusConnection *conn, GKeyFile *config)
836 sdp_record_t *record;
837 gboolean tmp, master = TRUE;
844 tmp = g_key_file_get_boolean(config, "General",
847 debug("audio.conf: %s", err->message);
853 input_device_name = g_key_file_get_string(config,
854 "AVRCP", "InputDeviceName", &err);
856 debug("InputDeviceName not specified in audio.conf");
857 input_device_name = NULL;
862 connection = dbus_connection_ref(conn);
864 record = avrcp_tg_record();
866 error("Unable to allocate new service record");
870 if (add_record_to_server(BDADDR_ANY, record) < 0) {
871 error("Unable to register AVRCP target service record");
872 sdp_record_free(record);
875 tg_record_id = record->handle;
878 record = avrcp_ct_record();
880 error("Unable to allocate new service record");
884 if (add_record_to_server(BDADDR_ANY, record) < 0) {
885 error("Unable to register AVRCP controller service record");
886 sdp_record_free(record);
889 ct_record_id = record->handle;
892 avctp_server = avctp_server_socket(master);
899 void avrcp_exit(void)
904 g_io_channel_close(avctp_server);
905 g_io_channel_unref(avctp_server);
909 remove_record_from_server(ct_record_id);
912 remove_record_from_server(tg_record_id);
915 dbus_connection_unref(connection);
919 static DBusMessage *control_is_connected(DBusConnection *conn,
923 struct audio_device *device = data;
924 struct control *control = device->control;
926 dbus_bool_t connected;
928 reply = dbus_message_new_method_return(msg);
932 connected = (control->session != NULL);
934 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected,
940 static GDBusMethodTable control_methods[] = {
941 { "IsConnected", "", "b", control_is_connected },
942 { NULL, NULL, NULL, NULL }
945 static GDBusSignalTable control_signals[] = {
947 { "Disconnected", "" },
951 struct control *control_init(struct audio_device *dev)
953 if (!g_dbus_register_interface(dev->conn, dev->path,
954 AUDIO_CONTROL_INTERFACE,
955 control_methods, control_signals, NULL,
959 return g_new0(struct control, 1);
962 void control_free(struct audio_device *dev)
964 struct control *control = dev->control;
966 if (control->session)
967 avctp_unref(control->session);
973 gboolean control_is_active(struct audio_device *dev)
975 struct control *control = dev->control;
977 if (control->session &&
978 control->session->state != AVCTP_STATE_DISCONNECTED)