2007-10-22 22:11:04 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2007-10-24 01:17:47 +08:00
|
|
|
* Copyright (C) 2006-2007 Nokia Corporation
|
2009-01-02 02:33:20 +08:00
|
|
|
* Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
|
2007-10-24 01:17:47 +08:00
|
|
|
*
|
2007-10-22 22:11:04 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <signal.h>
|
2007-10-25 18:38:47 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2007-10-22 22:11:04 +08:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/sdp.h>
|
|
|
|
#include <bluetooth/sdp_lib.h>
|
|
|
|
#include <bluetooth/l2cap.h>
|
|
|
|
|
2008-05-09 04:23:45 +08:00
|
|
|
#include <glib.h>
|
|
|
|
#include <dbus/dbus.h>
|
2008-05-09 06:19:14 +08:00
|
|
|
#include <gdbus.h>
|
2008-05-09 04:23:45 +08:00
|
|
|
|
2007-10-22 22:11:04 +08:00
|
|
|
#include "logging.h"
|
2009-03-03 07:51:40 +08:00
|
|
|
#include "error.h"
|
2007-10-25 18:38:47 +08:00
|
|
|
#include "uinput.h"
|
2008-08-19 06:20:42 +08:00
|
|
|
#include "adapter.h"
|
2007-10-22 22:11:04 +08:00
|
|
|
#include "device.h"
|
|
|
|
#include "manager.h"
|
|
|
|
#include "avdtp.h"
|
|
|
|
#include "control.h"
|
2008-03-28 07:07:19 +08:00
|
|
|
#include "sdpd.h"
|
2008-05-09 02:47:21 +08:00
|
|
|
#include "glib-helper.h"
|
2009-02-19 06:17:54 +08:00
|
|
|
#include "btio.h"
|
2008-10-03 03:48:17 +08:00
|
|
|
#include "dbus-common.h"
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
#define AVCTP_PSM 23
|
|
|
|
|
2007-10-23 16:56:06 +08:00
|
|
|
/* Message types */
|
|
|
|
#define AVCTP_COMMAND 0
|
|
|
|
#define AVCTP_RESPONSE 1
|
|
|
|
|
|
|
|
/* Packet types */
|
2007-10-25 01:25:56 +08:00
|
|
|
#define AVCTP_PACKET_SINGLE 0
|
|
|
|
#define AVCTP_PACKET_START 1
|
|
|
|
#define AVCTP_PACKET_CONTINUE 2
|
|
|
|
#define AVCTP_PACKET_END 3
|
2007-10-23 16:56:06 +08:00
|
|
|
|
|
|
|
/* ctype entries */
|
|
|
|
#define CTYPE_CONTROL 0x0
|
|
|
|
#define CTYPE_STATUS 0x1
|
2009-03-28 03:57:45 +08:00
|
|
|
#define CTYPE_NOT_IMPLEMENTED 0x8
|
2007-10-23 16:56:06 +08:00
|
|
|
#define CTYPE_ACCEPTED 0x9
|
2009-03-28 03:57:45 +08:00
|
|
|
#define CTYPE_REJECTED 0xA
|
2007-10-23 16:56:06 +08:00
|
|
|
#define CTYPE_STABLE 0xC
|
|
|
|
|
|
|
|
/* opcodes */
|
|
|
|
#define OP_UNITINFO 0x30
|
|
|
|
#define OP_SUBUNITINFO 0x31
|
|
|
|
#define OP_PASSTHROUGH 0x7c
|
|
|
|
|
|
|
|
/* subunits of interest */
|
|
|
|
#define SUBUNIT_PANEL 0x09
|
|
|
|
|
|
|
|
/* operands in passthrough commands */
|
2009-03-03 06:31:32 +08:00
|
|
|
#define VOL_UP_OP 0x41
|
|
|
|
#define VOL_DOWN_OP 0x42
|
2007-10-23 16:56:06 +08:00
|
|
|
#define MUTE_OP 0x43
|
|
|
|
#define PLAY_OP 0x44
|
|
|
|
#define STOP_OP 0x45
|
|
|
|
#define PAUSE_OP 0x46
|
2009-03-03 06:31:32 +08:00
|
|
|
#define RECORD_OP 0x47
|
2007-10-25 18:38:47 +08:00
|
|
|
#define REWIND_OP 0x48
|
|
|
|
#define FAST_FORWARD_OP 0x49
|
2009-03-03 06:31:32 +08:00
|
|
|
#define EJECT_OP 0x4a
|
|
|
|
#define FORWARD_OP 0x4b
|
|
|
|
#define BACKWARD_OP 0x4c
|
2007-10-23 16:56:06 +08:00
|
|
|
|
2007-10-22 22:11:04 +08:00
|
|
|
static DBusConnection *connection = NULL;
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
static GSList *servers = NULL;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
|
|
|
struct avctp_header {
|
|
|
|
uint8_t ipid:1;
|
|
|
|
uint8_t cr:1;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint16_t pid;
|
|
|
|
} __attribute__ ((packed));
|
2009-03-03 07:51:40 +08:00
|
|
|
#define AVCTP_HEADER_LENGTH 3
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
struct avrcp_header {
|
|
|
|
uint8_t code:4;
|
|
|
|
uint8_t _hdr0:4;
|
|
|
|
uint8_t subunit_id:3;
|
|
|
|
uint8_t subunit_type:5;
|
|
|
|
uint8_t opcode;
|
|
|
|
} __attribute__ ((packed));
|
2009-03-03 07:51:40 +08:00
|
|
|
#define AVRCP_HEADER_LENGTH 3
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
|
|
|
struct avctp_header {
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t cr:1;
|
|
|
|
uint8_t ipid:1;
|
|
|
|
uint16_t pid;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct avrcp_header {
|
|
|
|
uint8_t _hdr0:4;
|
|
|
|
uint8_t code:4;
|
|
|
|
uint8_t subunit_type:5;
|
|
|
|
uint8_t subunit_id:3;
|
|
|
|
uint8_t opcode;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "Unknown byte order"
|
|
|
|
#endif
|
|
|
|
|
2009-03-27 22:40:36 +08:00
|
|
|
struct avctp_state_callback {
|
|
|
|
avctp_state_cb cb;
|
|
|
|
void *user_data;
|
|
|
|
unsigned int id;
|
|
|
|
};
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
struct avctp_server {
|
|
|
|
bdaddr_t src;
|
|
|
|
GIOChannel *io;
|
|
|
|
uint32_t tg_record_id;
|
|
|
|
uint32_t ct_record_id;
|
|
|
|
};
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
struct control {
|
2008-05-29 16:05:16 +08:00
|
|
|
struct audio_device *dev;
|
2007-10-23 15:54:36 +08:00
|
|
|
|
|
|
|
avctp_state_t state;
|
|
|
|
|
2007-10-25 18:38:47 +08:00
|
|
|
int uinput;
|
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
GIOChannel *io;
|
|
|
|
guint io_id;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
uint16_t mtu;
|
|
|
|
};
|
|
|
|
|
2009-03-27 22:40:36 +08:00
|
|
|
static GSList *avctp_callbacks = NULL;
|
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
static sdp_record_t *avrcp_ct_record()
|
2007-10-22 22:11:04 +08:00
|
|
|
{
|
|
|
|
sdp_list_t *svclass_id, *pfseq, *apseq, *root;
|
|
|
|
uuid_t root_uuid, l2cap, avctp, avrct;
|
|
|
|
sdp_profile_desc_t profile[1];
|
|
|
|
sdp_list_t *aproto, *proto[2];
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_record_t *record;
|
2007-10-22 22:11:04 +08:00
|
|
|
sdp_data_t *psm, *version, *features;
|
2009-02-03 04:26:09 +08:00
|
|
|
uint16_t lp = AVCTP_PSM, ver = 0x0100, feat = 0x000f;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
record = sdp_record_alloc();
|
|
|
|
if (!record)
|
|
|
|
return NULL;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
|
|
|
|
root = sdp_list_append(0, &root_uuid);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_browse_groups(record, root);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
/* Service Class ID List */
|
|
|
|
sdp_uuid16_create(&avrct, AV_REMOTE_SVCLASS_ID);
|
|
|
|
svclass_id = sdp_list_append(0, &avrct);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_service_classes(record, svclass_id);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
/* Protocol Descriptor List */
|
|
|
|
sdp_uuid16_create(&l2cap, L2CAP_UUID);
|
|
|
|
proto[0] = sdp_list_append(0, &l2cap);
|
|
|
|
psm = sdp_data_alloc(SDP_UINT16, &lp);
|
|
|
|
proto[0] = sdp_list_append(proto[0], psm);
|
|
|
|
apseq = sdp_list_append(0, proto[0]);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&avctp, AVCTP_UUID);
|
|
|
|
proto[1] = sdp_list_append(0, &avctp);
|
|
|
|
version = sdp_data_alloc(SDP_UINT16, &ver);
|
|
|
|
proto[1] = sdp_list_append(proto[1], version);
|
|
|
|
apseq = sdp_list_append(apseq, proto[1]);
|
|
|
|
|
|
|
|
aproto = sdp_list_append(0, apseq);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_access_protos(record, aproto);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
/* Bluetooth Profile Descriptor List */
|
|
|
|
sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
|
|
|
|
profile[0].version = ver;
|
|
|
|
pfseq = sdp_list_append(0, &profile[0]);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_profile_descs(record, pfseq);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
features = sdp_data_alloc(SDP_UINT16, &feat);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_info_attr(record, "AVRCP CT", 0, 0);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
free(psm);
|
|
|
|
free(version);
|
|
|
|
sdp_list_free(proto[0], 0);
|
|
|
|
sdp_list_free(proto[1], 0);
|
|
|
|
sdp_list_free(apseq, 0);
|
|
|
|
sdp_list_free(pfseq, 0);
|
|
|
|
sdp_list_free(aproto, 0);
|
|
|
|
sdp_list_free(root, 0);
|
|
|
|
sdp_list_free(svclass_id, 0);
|
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
return record;
|
2007-10-22 22:11:04 +08:00
|
|
|
}
|
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
static sdp_record_t *avrcp_tg_record()
|
2007-10-22 22:11:04 +08:00
|
|
|
{
|
|
|
|
sdp_list_t *svclass_id, *pfseq, *apseq, *root;
|
|
|
|
uuid_t root_uuid, l2cap, avctp, avrtg;
|
|
|
|
sdp_profile_desc_t profile[1];
|
|
|
|
sdp_list_t *aproto, *proto[2];
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_record_t *record;
|
2007-10-22 22:11:04 +08:00
|
|
|
sdp_data_t *psm, *version, *features;
|
2009-02-03 04:26:09 +08:00
|
|
|
uint16_t lp = AVCTP_PSM, ver = 0x0100, feat = 0x000f;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
record = sdp_record_alloc();
|
|
|
|
if (!record)
|
|
|
|
return NULL;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
|
|
|
|
root = sdp_list_append(0, &root_uuid);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_browse_groups(record, root);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
/* Service Class ID List */
|
|
|
|
sdp_uuid16_create(&avrtg, AV_REMOTE_TARGET_SVCLASS_ID);
|
|
|
|
svclass_id = sdp_list_append(0, &avrtg);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_service_classes(record, svclass_id);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
/* Protocol Descriptor List */
|
|
|
|
sdp_uuid16_create(&l2cap, L2CAP_UUID);
|
|
|
|
proto[0] = sdp_list_append(0, &l2cap);
|
|
|
|
psm = sdp_data_alloc(SDP_UINT16, &lp);
|
|
|
|
proto[0] = sdp_list_append(proto[0], psm);
|
|
|
|
apseq = sdp_list_append(0, proto[0]);
|
|
|
|
|
|
|
|
sdp_uuid16_create(&avctp, AVCTP_UUID);
|
|
|
|
proto[1] = sdp_list_append(0, &avctp);
|
|
|
|
version = sdp_data_alloc(SDP_UINT16, &ver);
|
|
|
|
proto[1] = sdp_list_append(proto[1], version);
|
|
|
|
apseq = sdp_list_append(apseq, proto[1]);
|
|
|
|
|
|
|
|
aproto = sdp_list_append(0, apseq);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_access_protos(record, aproto);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
/* Bluetooth Profile Descriptor List */
|
|
|
|
sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
|
|
|
|
profile[0].version = ver;
|
|
|
|
pfseq = sdp_list_append(0, &profile[0]);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_profile_descs(record, pfseq);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
features = sdp_data_alloc(SDP_UINT16, &feat);
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_set_info_attr(record, "AVRCP TG", 0, 0);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
free(psm);
|
|
|
|
free(version);
|
|
|
|
sdp_list_free(proto[0], 0);
|
|
|
|
sdp_list_free(proto[1], 0);
|
|
|
|
sdp_list_free(apseq, 0);
|
|
|
|
sdp_list_free(aproto, 0);
|
|
|
|
sdp_list_free(pfseq, 0);
|
|
|
|
sdp_list_free(root, 0);
|
|
|
|
sdp_list_free(svclass_id, 0);
|
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
return record;
|
2007-10-22 22:11:04 +08:00
|
|
|
}
|
|
|
|
|
2007-10-25 18:38:47 +08:00
|
|
|
static int send_event(int fd, uint16_t type, uint16_t code, int32_t value)
|
|
|
|
{
|
|
|
|
struct uinput_event event;
|
|
|
|
|
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
event.type = type;
|
|
|
|
event.code = code;
|
|
|
|
event.value = value;
|
|
|
|
|
|
|
|
return write(fd, &event, sizeof(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_key(int fd, uint16_t key, int pressed)
|
|
|
|
{
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
send_event(fd, EV_KEY, key, pressed);
|
|
|
|
send_event(fd, EV_SYN, SYN_REPORT, 0);
|
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
static void handle_panel_passthrough(struct control *control,
|
2007-10-25 18:38:47 +08:00
|
|
|
const unsigned char *operands,
|
|
|
|
int operand_count)
|
2007-10-23 16:56:06 +08:00
|
|
|
{
|
2007-10-25 01:25:56 +08:00
|
|
|
const char *status;
|
2007-10-25 18:38:47 +08:00
|
|
|
int pressed;
|
2007-10-25 01:25:56 +08:00
|
|
|
|
2007-10-23 16:56:06 +08:00
|
|
|
if (operand_count == 0)
|
|
|
|
return;
|
|
|
|
|
2007-10-25 18:38:47 +08:00
|
|
|
if (operands[0] & 0x80) {
|
2007-10-25 01:25:56 +08:00
|
|
|
status = "released";
|
2007-10-25 18:38:47 +08:00
|
|
|
pressed = 0;
|
|
|
|
} else {
|
2007-10-25 01:25:56 +08:00
|
|
|
status = "pressed";
|
2007-10-25 18:38:47 +08:00
|
|
|
pressed = 1;
|
|
|
|
}
|
2007-10-25 01:25:56 +08:00
|
|
|
|
|
|
|
switch (operands[0] & 0x7F) {
|
2007-10-23 16:56:06 +08:00
|
|
|
case PLAY_OP:
|
2007-10-25 01:25:56 +08:00
|
|
|
debug("AVRCP: PLAY %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_PLAYPAUSE, pressed);
|
2007-10-23 16:56:06 +08:00
|
|
|
break;
|
|
|
|
case STOP_OP:
|
2007-10-25 01:25:56 +08:00
|
|
|
debug("AVRCP: STOP %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_STOP, pressed);
|
2007-10-23 16:56:06 +08:00
|
|
|
break;
|
|
|
|
case PAUSE_OP:
|
2007-10-25 01:25:56 +08:00
|
|
|
debug("AVRCP: PAUSE %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_PLAYPAUSE, pressed);
|
2007-10-23 16:56:06 +08:00
|
|
|
break;
|
2009-03-03 06:31:32 +08:00
|
|
|
case FORWARD_OP:
|
|
|
|
debug("AVRCP: FORWARD %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_NEXTSONG, pressed);
|
2007-10-23 16:56:06 +08:00
|
|
|
break;
|
2009-03-03 06:31:32 +08:00
|
|
|
case BACKWARD_OP:
|
|
|
|
debug("AVRCP: BACKWARD %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_PREVIOUSSONG, pressed);
|
2007-10-25 18:38:47 +08:00
|
|
|
break;
|
|
|
|
case REWIND_OP:
|
|
|
|
debug("AVRCP: REWIND %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_REWIND, pressed);
|
2007-10-25 18:38:47 +08:00
|
|
|
break;
|
|
|
|
case FAST_FORWARD_OP:
|
|
|
|
debug("AVRCP: FAST FORWARD %s", status);
|
2009-04-23 04:00:01 +08:00
|
|
|
send_key(control->uinput, KEY_FORWARD, pressed);
|
2007-10-23 16:56:06 +08:00
|
|
|
break;
|
|
|
|
default:
|
2007-10-25 01:25:56 +08:00
|
|
|
debug("AVRCP: unknown button 0x%02X %s", operands[0] & 0x7F, status);
|
2007-10-23 16:56:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
static void avctp_disconnected(struct audio_device *dev)
|
2008-05-27 22:02:41 +08:00
|
|
|
{
|
2009-04-23 04:00:01 +08:00
|
|
|
struct control *control = dev->control;
|
2008-05-27 22:02:41 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (!control)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (control->io) {
|
|
|
|
g_io_channel_shutdown(control->io, TRUE, NULL);
|
|
|
|
g_io_channel_unref(control->io);
|
|
|
|
control->io = NULL;
|
2008-10-03 03:52:31 +08:00
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->io_id) {
|
|
|
|
g_source_remove(control->io_id);
|
|
|
|
control->io_id = 0;
|
2009-02-20 04:22:20 +08:00
|
|
|
}
|
2009-02-24 21:22:38 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->uinput >= 0) {
|
|
|
|
ioctl(control->uinput, UI_DEV_DESTROY);
|
|
|
|
close(control->uinput);
|
|
|
|
control->uinput = -1;
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 22:02:41 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
static void avctp_set_state(struct control *control, avctp_state_t new_state)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
struct audio_device *dev = control->dev;
|
|
|
|
avdtp_session_state_t old_state = control->state;
|
|
|
|
gboolean value;
|
2008-05-27 22:02:41 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
switch (new_state) {
|
|
|
|
case AVCTP_STATE_DISCONNECTED:
|
|
|
|
avctp_disconnected(control->dev);
|
|
|
|
|
|
|
|
if (old_state != AVCTP_STATE_CONNECTED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
value = FALSE;
|
|
|
|
g_dbus_emit_signal(dev->conn, dev->path,
|
|
|
|
AUDIO_CONTROL_INTERFACE,
|
|
|
|
"Disconnected", DBUS_TYPE_INVALID);
|
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_CONTROL_INTERFACE, "Connected",
|
|
|
|
DBUS_TYPE_BOOLEAN, &value);
|
|
|
|
break;
|
|
|
|
case AVCTP_STATE_CONNECTING:
|
|
|
|
break;
|
|
|
|
case AVCTP_STATE_CONNECTED:
|
|
|
|
value = TRUE;
|
|
|
|
g_dbus_emit_signal(control->dev->conn, control->dev->path,
|
|
|
|
AUDIO_CONTROL_INTERFACE, "Connected",
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
emit_property_changed(control->dev->conn, control->dev->path,
|
|
|
|
AUDIO_CONTROL_INTERFACE, "Connected",
|
|
|
|
DBUS_TYPE_BOOLEAN, &value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Invalid AVCTP state %d", new_state);
|
|
|
|
return;
|
2008-05-27 22:02:41 +08:00
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
control->state = new_state;
|
|
|
|
|
|
|
|
for (l = avctp_callbacks; l != NULL; l = l->next) {
|
|
|
|
struct avctp_state_callback *cb = l->data;
|
|
|
|
cb->cb(control->dev, old_state, new_state, cb->user_data);
|
|
|
|
}
|
2008-05-27 22:02:41 +08:00
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
static gboolean control_cb(GIOChannel *chan, GIOCondition cond,
|
2007-10-22 22:11:04 +08:00
|
|
|
gpointer data)
|
|
|
|
{
|
2009-04-23 04:00:01 +08:00
|
|
|
struct control *control = data;
|
2007-10-23 16:56:06 +08:00
|
|
|
unsigned char buf[1024], *operands;
|
2007-10-22 22:11:04 +08:00
|
|
|
struct avctp_header *avctp;
|
|
|
|
struct avrcp_header *avrcp;
|
2009-02-20 04:22:20 +08:00
|
|
|
int ret, packet_size, operand_count, sock;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
if (!(cond | G_IO_IN))
|
|
|
|
goto failed;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
sock = g_io_channel_unix_get_fd(control->io);
|
2009-02-20 04:22:20 +08:00
|
|
|
|
|
|
|
ret = read(sock, buf, sizeof(buf));
|
2007-10-22 22:11:04 +08:00
|
|
|
if (ret <= 0)
|
|
|
|
goto failed;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
debug("Got %d bytes of data for AVCTP session %p", ret, control);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2009-01-30 07:24:59 +08:00
|
|
|
if ((unsigned int) ret < sizeof(struct avctp_header)) {
|
2007-10-22 22:11:04 +08:00
|
|
|
error("Too small AVCTP packet");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2007-10-23 16:56:06 +08:00
|
|
|
packet_size = ret;
|
|
|
|
|
2007-10-22 22:11:04 +08:00
|
|
|
avctp = (struct avctp_header *) buf;
|
|
|
|
|
|
|
|
debug("AVCTP transaction %u, packet type %u, C/R %u, IPID %u, "
|
|
|
|
"PID 0x%04X",
|
|
|
|
avctp->transaction, avctp->packet_type,
|
|
|
|
avctp->cr, avctp->ipid, ntohs(avctp->pid));
|
|
|
|
|
|
|
|
ret -= sizeof(struct avctp_header);
|
2009-01-30 07:24:59 +08:00
|
|
|
if ((unsigned int) ret < sizeof(struct avrcp_header)) {
|
2007-10-22 22:11:04 +08:00
|
|
|
error("Too small AVRCP packet");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
avrcp = (struct avrcp_header *) (buf + sizeof(struct avctp_header));
|
|
|
|
|
2007-10-23 16:56:06 +08:00
|
|
|
ret -= sizeof(struct avrcp_header);
|
|
|
|
|
|
|
|
operands = buf + sizeof(struct avctp_header) + sizeof(struct avrcp_header);
|
|
|
|
operand_count = ret;
|
|
|
|
|
2007-10-22 22:11:04 +08:00
|
|
|
debug("AVRCP %s 0x%01X, subunit_type 0x%02X, subunit_id 0x%01X, "
|
2007-10-22 22:59:22 +08:00
|
|
|
"opcode 0x%02X, %d operands",
|
|
|
|
avctp->cr ? "response" : "command",
|
2007-10-22 22:11:04 +08:00
|
|
|
avrcp->code, avrcp->subunit_type, avrcp->subunit_id,
|
2007-10-23 16:56:06 +08:00
|
|
|
avrcp->opcode, operand_count);
|
|
|
|
|
2009-03-28 03:57:45 +08:00
|
|
|
if (avctp->packet_type != AVCTP_PACKET_SINGLE) {
|
|
|
|
avctp->cr = AVCTP_RESPONSE;
|
|
|
|
avrcp->code = CTYPE_NOT_IMPLEMENTED;
|
|
|
|
} else if (avctp->pid != htons(AV_REMOTE_SVCLASS_ID)) {
|
|
|
|
avctp->ipid = 1;
|
|
|
|
avctp->cr = AVCTP_RESPONSE;
|
|
|
|
avrcp->code = CTYPE_REJECTED;
|
|
|
|
} else if (avctp->cr == AVCTP_COMMAND &&
|
2007-10-23 16:56:06 +08:00
|
|
|
avrcp->code == CTYPE_CONTROL &&
|
|
|
|
avrcp->subunit_type == SUBUNIT_PANEL &&
|
|
|
|
avrcp->opcode == OP_PASSTHROUGH) {
|
2009-04-23 04:00:01 +08:00
|
|
|
handle_panel_passthrough(control, operands, operand_count);
|
2007-10-23 16:56:06 +08:00
|
|
|
avctp->cr = AVCTP_RESPONSE;
|
|
|
|
avrcp->code = CTYPE_ACCEPTED;
|
2009-03-28 03:57:45 +08:00
|
|
|
} else if (avctp->cr == AVCTP_COMMAND &&
|
2008-01-16 22:44:20 +08:00
|
|
|
avrcp->code == CTYPE_STATUS &&
|
|
|
|
(avrcp->opcode == OP_UNITINFO
|
|
|
|
|| avrcp->opcode == OP_SUBUNITINFO)) {
|
|
|
|
avctp->cr = AVCTP_RESPONSE;
|
|
|
|
avrcp->code = CTYPE_STABLE;
|
|
|
|
debug("reply to %s", avrcp->opcode == OP_UNITINFO ?
|
|
|
|
"OP_UNITINFO" : "OP_SUBUNITINFO");
|
2009-03-28 03:57:45 +08:00
|
|
|
} else {
|
|
|
|
avctp->cr = AVCTP_RESPONSE;
|
|
|
|
avrcp->code = CTYPE_REJECTED;
|
2008-01-16 22:44:20 +08:00
|
|
|
}
|
2009-03-28 03:57:45 +08:00
|
|
|
ret = write(sock, buf, packet_size);
|
2008-01-16 22:44:20 +08:00
|
|
|
|
2007-10-22 22:11:04 +08:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
2009-04-23 04:00:01 +08:00
|
|
|
debug("AVCTP session %p got disconnected", control);
|
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2007-10-22 22:11:04 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-05-27 22:02:41 +08:00
|
|
|
|
|
|
|
static int uinput_create(char *name)
|
|
|
|
{
|
|
|
|
struct uinput_dev dev;
|
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
fd = open("/dev/uinput", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fd = open("/dev/input/uinput", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fd = open("/dev/misc/uinput", O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
err = errno;
|
|
|
|
error("Can't open input device: %s (%d)",
|
|
|
|
strerror(err), err);
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&dev, 0, sizeof(dev));
|
|
|
|
if (name)
|
|
|
|
strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
|
|
|
|
|
|
|
|
dev.id.bustype = BUS_BLUETOOTH;
|
|
|
|
dev.id.vendor = 0x0000;
|
|
|
|
dev.id.product = 0x0000;
|
|
|
|
dev.id.version = 0x0000;
|
|
|
|
|
|
|
|
if (write(fd, &dev, sizeof(dev)) < 0) {
|
|
|
|
err = errno;
|
|
|
|
error("Can't write device information: %s (%d)",
|
|
|
|
strerror(err), err);
|
|
|
|
close(fd);
|
|
|
|
errno = err;
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_KEY);
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_REL);
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_REP);
|
|
|
|
ioctl(fd, UI_SET_EVBIT, EV_SYN);
|
|
|
|
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, KEY_PLAYPAUSE);
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, KEY_STOP);
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, KEY_NEXTSONG);
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, KEY_PREVIOUSSONG);
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, KEY_REWIND);
|
|
|
|
ioctl(fd, UI_SET_KEYBIT, KEY_FORWARD);
|
|
|
|
|
|
|
|
if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
|
|
|
|
err = errno;
|
|
|
|
error("Can't create uinput device: %s (%d)",
|
|
|
|
strerror(err), err);
|
|
|
|
close(fd);
|
|
|
|
errno = err;
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
static void init_uinput(struct control *control)
|
2008-05-27 22:02:41 +08:00
|
|
|
{
|
2008-07-29 04:15:27 +08:00
|
|
|
char address[18];
|
2008-05-27 22:02:41 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
ba2str(&control->dev->dst, address);
|
2008-05-27 22:02:41 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
control->uinput = uinput_create(address);
|
|
|
|
if (control->uinput < 0)
|
2008-07-29 04:15:27 +08:00
|
|
|
error("AVRCP: failed to init uinput for %s", address);
|
2008-05-27 22:02:41 +08:00
|
|
|
else
|
2008-07-29 04:15:27 +08:00
|
|
|
debug("AVRCP: uinput initialized for %s", address);
|
2008-05-27 22:02:41 +08:00
|
|
|
}
|
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
static void avctp_connect_cb(GIOChannel *chan, GError *err, gpointer data)
|
2007-10-22 22:11:04 +08:00
|
|
|
{
|
2009-02-20 04:22:20 +08:00
|
|
|
struct control *control = data;
|
|
|
|
char address[18];
|
|
|
|
uint16_t imtu;
|
|
|
|
GError *gerr = NULL;
|
2008-10-08 17:21:17 +08:00
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
if (err) {
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2009-02-20 04:22:20 +08:00
|
|
|
error("%s", err->message);
|
|
|
|
return;
|
|
|
|
}
|
2008-07-29 04:15:27 +08:00
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
bt_io_get(chan, BT_IO_L2CAP, &gerr,
|
|
|
|
BT_IO_OPT_DEST, &address,
|
|
|
|
BT_IO_OPT_IMTU, &imtu,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (gerr) {
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2009-02-20 04:22:20 +08:00
|
|
|
error("%s", gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("AVCTP: connected to %s", address);
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (!control->io)
|
|
|
|
control->io = g_io_channel_ref(chan);
|
2008-06-18 03:37:36 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
init_uinput(control);
|
2007-10-23 15:54:36 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_CONNECTED);
|
|
|
|
control->mtu = imtu;
|
|
|
|
control->io_id = g_io_add_watch(chan,
|
2009-02-20 04:22:20 +08:00
|
|
|
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
|
2009-04-23 04:00:01 +08:00
|
|
|
(GIOFunc) control_cb, control);
|
2007-10-22 22:11:04 +08:00
|
|
|
}
|
|
|
|
|
2008-06-18 03:37:36 +08:00
|
|
|
static void auth_cb(DBusError *derr, void *user_data)
|
2008-05-05 22:46:39 +08:00
|
|
|
{
|
2009-02-20 04:22:20 +08:00
|
|
|
struct control *control = user_data;
|
|
|
|
GError *err = NULL;
|
2008-05-05 22:46:39 +08:00
|
|
|
|
2008-06-18 03:37:36 +08:00
|
|
|
if (derr && dbus_error_is_set(derr)) {
|
|
|
|
error("Access denied: %s", derr->message);
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2008-06-18 03:37:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (!bt_io_accept(control->io, avctp_connect_cb, control,
|
2009-02-20 04:22:20 +08:00
|
|
|
NULL, &err)) {
|
|
|
|
error("bt_io_accept: %s", err->message);
|
|
|
|
g_error_free(err);
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2009-02-20 04:22:20 +08:00
|
|
|
}
|
2008-05-05 22:46:39 +08:00
|
|
|
}
|
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
static void avctp_confirm_cb(GIOChannel *chan, gpointer data)
|
2007-10-22 22:11:04 +08:00
|
|
|
{
|
2009-04-23 04:00:01 +08:00
|
|
|
struct control *control = NULL;
|
2008-10-08 20:44:54 +08:00
|
|
|
struct audio_device *dev;
|
2007-10-22 22:11:04 +08:00
|
|
|
char address[18];
|
2009-02-19 06:17:54 +08:00
|
|
|
bdaddr_t src, dst;
|
2009-02-20 04:22:20 +08:00
|
|
|
GError *err = NULL;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
bt_io_get(chan, BT_IO_L2CAP, &err,
|
2009-02-19 06:17:54 +08:00
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &src,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &dst,
|
|
|
|
BT_IO_OPT_DEST, address,
|
|
|
|
BT_IO_OPT_INVALID);
|
2009-02-20 04:22:20 +08:00
|
|
|
if (err) {
|
|
|
|
error("%s", err->message);
|
|
|
|
g_error_free(err);
|
2009-02-20 02:58:15 +08:00
|
|
|
g_io_channel_shutdown(chan, TRUE, NULL);
|
2009-02-19 06:17:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-12 01:01:00 +08:00
|
|
|
dev = manager_get_device(&src, &dst, TRUE);
|
2008-10-08 20:44:54 +08:00
|
|
|
if (!dev) {
|
|
|
|
error("Unable to get audio device object for %s", address);
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev->control)
|
|
|
|
dev->control = control_init(dev);
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
control = dev->control;
|
2009-02-20 04:22:20 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->io) {
|
|
|
|
error("Refusing unexpected connect from %s", address);
|
|
|
|
goto drop;
|
|
|
|
}
|
2009-02-20 04:22:20 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_CONNECTING);
|
|
|
|
control->io = g_io_channel_ref(chan);
|
2008-06-18 03:37:36 +08:00
|
|
|
|
2009-02-25 21:48:39 +08:00
|
|
|
if (avdtp_is_connected(&src, &dst)) {
|
|
|
|
if (!bt_io_accept(chan, avctp_connect_cb, dev->control,
|
|
|
|
NULL, NULL))
|
|
|
|
goto drop;
|
|
|
|
} else if (btd_request_authorization(&src, &dst,
|
|
|
|
AVRCP_TARGET_UUID, auth_cb, dev->control) < 0)
|
2008-10-08 17:21:17 +08:00
|
|
|
goto drop;
|
2007-10-23 15:54:36 +08:00
|
|
|
|
2008-05-27 22:02:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
drop:
|
2009-04-23 04:00:01 +08:00
|
|
|
if (!control || !control->io)
|
2009-02-25 21:48:39 +08:00
|
|
|
g_io_channel_shutdown(chan, TRUE, NULL);
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control)
|
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2008-05-27 22:02:41 +08:00
|
|
|
}
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
static GIOChannel *avctp_server_socket(const bdaddr_t *src, gboolean master)
|
2008-05-27 22:02:41 +08:00
|
|
|
{
|
2009-02-19 06:17:54 +08:00
|
|
|
GError *err = NULL;
|
2008-05-27 22:02:41 +08:00
|
|
|
GIOChannel *io;
|
|
|
|
|
2009-02-20 04:22:20 +08:00
|
|
|
io = bt_io_listen(BT_IO_L2CAP, NULL, avctp_confirm_cb, NULL,
|
2009-02-19 06:17:54 +08:00
|
|
|
NULL, &err,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, src,
|
|
|
|
BT_IO_OPT_PSM, AVCTP_PSM,
|
|
|
|
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
|
|
|
|
BT_IO_OPT_MASTER, master,
|
|
|
|
BT_IO_OPT_INVALID);
|
2008-05-27 22:02:41 +08:00
|
|
|
if (!io) {
|
2009-02-19 06:17:54 +08:00
|
|
|
error("%s", err->message);
|
|
|
|
g_error_free(err);
|
2008-05-27 22:02:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return io;
|
2007-10-22 22:11:04 +08:00
|
|
|
}
|
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
gboolean avrcp_connect(struct audio_device *dev)
|
2007-10-23 15:54:36 +08:00
|
|
|
{
|
|
|
|
struct control *control = dev->control;
|
2009-02-19 06:17:54 +08:00
|
|
|
GError *err = NULL;
|
|
|
|
GIOChannel *io;
|
2007-10-23 15:54:36 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->state > AVCTP_STATE_DISCONNECTED)
|
2007-10-23 15:54:36 +08:00
|
|
|
return TRUE;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_CONNECTING);
|
2007-10-23 15:54:36 +08:00
|
|
|
|
2009-02-19 06:17:54 +08:00
|
|
|
io = bt_io_connect(BT_IO_L2CAP, avctp_connect_cb, control, NULL, &err,
|
|
|
|
BT_IO_OPT_SOURCE_BDADDR, &dev->src,
|
|
|
|
BT_IO_OPT_DEST_BDADDR, &dev->dst,
|
|
|
|
BT_IO_OPT_PSM, AVCTP_PSM,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (err) {
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2009-02-19 06:17:54 +08:00
|
|
|
error("%s", err->message);
|
|
|
|
g_error_free(err);
|
2008-05-09 21:18:47 +08:00
|
|
|
return FALSE;
|
2007-10-23 15:54:36 +08:00
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
control->io = io;
|
2007-10-23 15:54:36 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
void avrcp_disconnect(struct audio_device *dev)
|
2007-10-23 15:54:36 +08:00
|
|
|
{
|
|
|
|
struct control *control = dev->control;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (!(control && control->io))
|
2007-10-23 15:54:36 +08:00
|
|
|
return;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
avctp_set_state(control, AVCTP_STATE_DISCONNECTED);
|
2007-10-23 15:54:36 +08:00
|
|
|
}
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
int avrcp_register(DBusConnection *conn, const bdaddr_t *src, GKeyFile *config)
|
2007-10-22 22:11:04 +08:00
|
|
|
{
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_record_t *record;
|
2008-03-06 01:43:54 +08:00
|
|
|
gboolean tmp, master = TRUE;
|
|
|
|
GError *err = NULL;
|
2008-09-09 05:20:29 +08:00
|
|
|
struct avctp_server *server;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-05-15 10:51:55 +08:00
|
|
|
if (config) {
|
|
|
|
tmp = g_key_file_get_boolean(config, "General",
|
|
|
|
"Master", &err);
|
|
|
|
if (err) {
|
|
|
|
debug("audio.conf: %s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
} else
|
|
|
|
master = tmp;
|
|
|
|
}
|
2008-03-06 01:43:54 +08:00
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
server = g_new0(struct avctp_server, 1);
|
|
|
|
if (!server)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (!connection)
|
|
|
|
connection = dbus_connection_ref(conn);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
record = avrcp_tg_record();
|
|
|
|
if (!record) {
|
2007-10-22 22:11:04 +08:00
|
|
|
error("Unable to allocate new service record");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
if (add_record_to_server(src, record) < 0) {
|
2007-10-22 22:11:04 +08:00
|
|
|
error("Unable to register AVRCP target service record");
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_record_free(record);
|
2007-10-22 22:11:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2008-09-09 05:20:29 +08:00
|
|
|
server->tg_record_id = record->handle;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-03-28 07:07:19 +08:00
|
|
|
record = avrcp_ct_record();
|
|
|
|
if (!record) {
|
2007-10-22 22:11:04 +08:00
|
|
|
error("Unable to allocate new service record");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
if (add_record_to_server(src, record) < 0) {
|
2007-10-22 22:11:04 +08:00
|
|
|
error("Unable to register AVRCP controller service record");
|
2008-03-28 07:07:19 +08:00
|
|
|
sdp_record_free(record);
|
2007-10-22 22:11:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2008-09-09 05:20:29 +08:00
|
|
|
server->ct_record_id = record->handle;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
server->io = avctp_server_socket(src, master);
|
|
|
|
if (!server->io) {
|
|
|
|
remove_record_from_server(server->ct_record_id);
|
|
|
|
remove_record_from_server(server->tg_record_id);
|
|
|
|
g_free(server);
|
2007-10-22 22:11:04 +08:00
|
|
|
return -1;
|
2008-09-09 05:20:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bacpy(&server->src, src);
|
|
|
|
|
|
|
|
servers = g_slist_append(servers, server);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
static struct avctp_server *find_server(GSList *list, const bdaddr_t *src)
|
2007-10-22 22:11:04 +08:00
|
|
|
{
|
2008-09-09 05:20:29 +08:00
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
for (l = list; l; l = l->next) {
|
|
|
|
struct avctp_server *server = l->data;
|
|
|
|
|
|
|
|
if (bacmp(&server->src, src) == 0)
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void avrcp_unregister(const bdaddr_t *src)
|
|
|
|
{
|
|
|
|
struct avctp_server *server;
|
|
|
|
|
|
|
|
server = find_server(servers, src);
|
|
|
|
if (!server)
|
2007-10-22 22:11:04 +08:00
|
|
|
return;
|
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
servers = g_slist_remove(servers, server);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2008-09-09 05:20:29 +08:00
|
|
|
remove_record_from_server(server->ct_record_id);
|
|
|
|
remove_record_from_server(server->tg_record_id);
|
2007-10-22 22:11:04 +08:00
|
|
|
|
2009-02-20 02:58:15 +08:00
|
|
|
g_io_channel_shutdown(server->io, TRUE, NULL);
|
2008-09-09 05:20:29 +08:00
|
|
|
g_io_channel_unref(server->io);
|
|
|
|
g_free(server);
|
|
|
|
|
|
|
|
if (servers)
|
|
|
|
return;
|
2007-10-22 22:11:04 +08:00
|
|
|
|
|
|
|
dbus_connection_unref(connection);
|
|
|
|
connection = NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static DBusMessage *control_is_connected(DBusConnection *conn,
|
2007-10-23 15:54:36 +08:00
|
|
|
DBusMessage *msg,
|
|
|
|
void *data)
|
|
|
|
{
|
2008-05-29 16:05:16 +08:00
|
|
|
struct audio_device *device = data;
|
2007-10-23 15:54:36 +08:00
|
|
|
struct control *control = device->control;
|
|
|
|
DBusMessage *reply;
|
|
|
|
dbus_bool_t connected;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
2008-06-02 22:34:58 +08:00
|
|
|
return NULL;
|
2007-10-23 15:54:36 +08:00
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
connected = (control->state == AVCTP_STATE_CONNECTED);
|
2007-10-23 15:54:36 +08:00
|
|
|
|
|
|
|
dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
return reply;
|
2007-10-23 15:54:36 +08:00
|
|
|
}
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
static int avctp_send_passthrough(struct control *control, uint8_t op)
|
2009-03-03 07:51:40 +08:00
|
|
|
{
|
|
|
|
unsigned char buf[AVCTP_HEADER_LENGTH + AVRCP_HEADER_LENGTH + 2];
|
|
|
|
struct avctp_header *avctp = (void *) buf;
|
|
|
|
struct avrcp_header *avrcp = (void *) &buf[AVCTP_HEADER_LENGTH];
|
|
|
|
uint8_t *operands = &buf[AVCTP_HEADER_LENGTH + AVRCP_HEADER_LENGTH];
|
2009-04-23 04:00:01 +08:00
|
|
|
int err, sk = g_io_channel_unix_get_fd(control->io);
|
2009-03-03 07:51:40 +08:00
|
|
|
static uint8_t transaction = 0;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
avctp->transaction = transaction++;
|
|
|
|
avctp->packet_type = AVCTP_PACKET_SINGLE;
|
|
|
|
avctp->cr = AVCTP_COMMAND;
|
|
|
|
avctp->pid = htons(AV_REMOTE_SVCLASS_ID);
|
|
|
|
|
|
|
|
avrcp->code = CTYPE_CONTROL;
|
|
|
|
avrcp->subunit_type = SUBUNIT_PANEL;
|
|
|
|
avrcp->opcode = OP_PASSTHROUGH;
|
|
|
|
|
|
|
|
operands[0] = op & 0x7f;
|
|
|
|
operands[1] = 0;
|
|
|
|
|
|
|
|
err = write(sk, buf, sizeof(buf));
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Button release */
|
|
|
|
avctp->transaction = transaction++;
|
|
|
|
operands[0] &= 0x80;
|
|
|
|
|
|
|
|
return write(sk, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *volume_up(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
|
|
|
struct control *control = device->control;
|
|
|
|
DBusMessage *reply;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
|
|
|
return NULL;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->state != AVCTP_STATE_CONNECTED)
|
2009-03-03 07:51:40 +08:00
|
|
|
return g_dbus_create_error(msg,
|
|
|
|
ERROR_INTERFACE ".NotConnected",
|
|
|
|
"Device not Connected");
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
err = avctp_send_passthrough(control, VOL_UP_OP);
|
2009-03-03 07:51:40 +08:00
|
|
|
if (err < 0)
|
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
strerror(-err));
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *volume_down(DBusConnection *conn, DBusMessage *msg,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
|
|
|
struct control *control = device->control;
|
|
|
|
DBusMessage *reply;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
|
|
|
return NULL;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->state != AVCTP_STATE_CONNECTED)
|
2009-03-03 07:51:40 +08:00
|
|
|
return g_dbus_create_error(msg,
|
|
|
|
ERROR_INTERFACE ".NotConnected",
|
|
|
|
"Device not Connected");
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
err = avctp_send_passthrough(control, VOL_DOWN_OP);
|
2009-03-03 07:51:40 +08:00
|
|
|
if (err < 0)
|
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
strerror(-err));
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
2008-10-03 03:48:17 +08:00
|
|
|
static DBusMessage *control_get_properties(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
|
|
|
DBusMessage *reply;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
DBusMessageIter dict;
|
|
|
|
gboolean value;
|
|
|
|
|
2008-10-03 14:29:51 +08:00
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
|
|
|
return NULL;
|
|
|
|
|
2008-10-03 03:48:17 +08:00
|
|
|
dbus_message_iter_init_append(reply, &iter);
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
|
|
|
|
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
|
|
|
|
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
|
|
|
|
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
|
|
|
|
|
|
|
|
/* Connected */
|
2009-04-23 04:00:01 +08:00
|
|
|
value = (device->control->state == AVCTP_STATE_CONNECTED);
|
2008-10-17 02:23:43 +08:00
|
|
|
dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &value);
|
2008-10-03 03:48:17 +08:00
|
|
|
|
|
|
|
dbus_message_iter_close_container(&iter, &dict);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static GDBusMethodTable control_methods[] = {
|
2008-10-03 06:16:39 +08:00
|
|
|
{ "IsConnected", "", "b", control_is_connected,
|
|
|
|
G_DBUS_METHOD_FLAG_DEPRECATED },
|
2008-10-03 03:48:17 +08:00
|
|
|
{ "GetProperties", "", "a{sv}",control_get_properties },
|
2009-03-03 07:51:40 +08:00
|
|
|
{ "VolumeUp", "", "", volume_up },
|
|
|
|
{ "VolumeDown", "", "", volume_down },
|
2007-10-23 15:54:36 +08:00
|
|
|
{ NULL, NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static GDBusSignalTable control_signals[] = {
|
2008-10-03 06:16:39 +08:00
|
|
|
{ "Connected", "", G_DBUS_SIGNAL_FLAG_DEPRECATED},
|
|
|
|
{ "Disconnected", "", G_DBUS_SIGNAL_FLAG_DEPRECATED},
|
2008-10-03 03:48:17 +08:00
|
|
|
{ "PropertyChanged", "sv" },
|
2007-10-23 15:54:36 +08:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-07-31 04:57:50 +08:00
|
|
|
static void path_unregister(void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = data;
|
2009-04-23 04:00:01 +08:00
|
|
|
struct control *control = dev->control;
|
2008-07-31 04:57:50 +08:00
|
|
|
|
2009-03-24 18:31:10 +08:00
|
|
|
debug("Unregistered interface %s on path %s",
|
2008-07-31 04:57:50 +08:00
|
|
|
AUDIO_CONTROL_INTERFACE, dev->path);
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control->state != AVCTP_STATE_DISCONNECTED)
|
|
|
|
avctp_disconnected(dev);
|
|
|
|
|
|
|
|
g_free(control);
|
|
|
|
dev->control = NULL;
|
2008-07-31 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void control_unregister(struct audio_device *dev)
|
|
|
|
{
|
|
|
|
g_dbus_unregister_interface(dev->conn, dev->path,
|
|
|
|
AUDIO_CONTROL_INTERFACE);
|
|
|
|
}
|
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
struct control *control_init(struct audio_device *dev)
|
2007-10-23 15:54:36 +08:00
|
|
|
{
|
2009-04-23 04:00:01 +08:00
|
|
|
struct control *control;
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
if (!g_dbus_register_interface(dev->conn, dev->path,
|
|
|
|
AUDIO_CONTROL_INTERFACE,
|
|
|
|
control_methods, control_signals, NULL,
|
2008-07-31 04:57:50 +08:00
|
|
|
dev, path_unregister))
|
2007-10-23 15:54:36 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-03-24 18:31:10 +08:00
|
|
|
debug("Registered interface %s on path %s",
|
2008-07-29 04:15:27 +08:00
|
|
|
AUDIO_CONTROL_INTERFACE, dev->path);
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
control = g_new0(struct control, 1);
|
|
|
|
control->dev = dev;
|
|
|
|
control->state = AVCTP_STATE_DISCONNECTED;
|
|
|
|
control->uinput = -1;
|
|
|
|
|
|
|
|
return control;
|
2007-10-23 15:54:36 +08:00
|
|
|
}
|
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
gboolean control_is_active(struct audio_device *dev)
|
2007-10-23 15:54:36 +08:00
|
|
|
{
|
|
|
|
struct control *control = dev->control;
|
|
|
|
|
2009-04-23 04:00:01 +08:00
|
|
|
if (control && control->state != AVCTP_STATE_DISCONNECTED)
|
2007-10-23 15:54:36 +08:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-03-27 22:40:36 +08:00
|
|
|
|
|
|
|
unsigned int avctp_add_state_cb(avctp_state_cb cb, void *user_data)
|
|
|
|
{
|
|
|
|
struct avctp_state_callback *state_cb;
|
|
|
|
static unsigned int id = 0;
|
|
|
|
|
|
|
|
state_cb = g_new(struct avctp_state_callback, 1);
|
|
|
|
state_cb->cb = cb;
|
|
|
|
state_cb->user_data = user_data;
|
|
|
|
state_cb->id = ++id;
|
|
|
|
|
2009-03-27 22:57:00 +08:00
|
|
|
avctp_callbacks = g_slist_append(avctp_callbacks, state_cb);
|
2009-03-27 22:40:36 +08:00
|
|
|
|
|
|
|
return state_cb->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean avctp_remove_state_cb(unsigned int id)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
for (l = avctp_callbacks; l != NULL; l = l->next) {
|
|
|
|
struct avctp_state_callback *cb = l->data;
|
|
|
|
if (cb && cb->id == id) {
|
|
|
|
avctp_callbacks = g_slist_remove(avctp_callbacks, cb);
|
|
|
|
g_free(cb);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|