2009-02-27 15:20:53 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2009 Marcel Holtmann <marcel@holtmann.org>
|
2009-09-03 16:49:11 +08:00
|
|
|
* Copyright (C) 2009 Nokia Corporation
|
2009-02-27 15:20:53 +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
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/hci_lib.h>
|
|
|
|
#include <bluetooth/l2cap.h>
|
|
|
|
|
|
|
|
#define AVDTP_PKT_TYPE_SINGLE 0x00
|
2009-09-03 01:04:44 +08:00
|
|
|
#define AVDTP_PKT_TYPE_START 0x01
|
|
|
|
#define AVDTP_PKT_TYPE_CONTINUE 0x02
|
|
|
|
#define AVDTP_PKT_TYPE_END 0x03
|
2009-09-01 03:01:56 +08:00
|
|
|
|
|
|
|
#define AVDTP_MSG_TYPE_COMMAND 0x00
|
2009-09-03 21:21:57 +08:00
|
|
|
#define AVDTP_MSG_TYPE_GEN_REJECT 0x01
|
2009-09-01 03:01:56 +08:00
|
|
|
#define AVDTP_MSG_TYPE_ACCEPT 0x02
|
|
|
|
#define AVDTP_MSG_TYPE_REJECT 0x03
|
|
|
|
|
|
|
|
#define AVDTP_DISCOVER 0x01
|
|
|
|
#define AVDTP_GET_CAPABILITIES 0x02
|
|
|
|
#define AVDTP_SET_CONFIGURATION 0x03
|
|
|
|
#define AVDTP_GET_CONFIGURATION 0x04
|
2009-09-02 21:53:11 +08:00
|
|
|
#define AVDTP_RECONFIGURE 0x05
|
|
|
|
#define AVDTP_OPEN 0x06
|
2009-09-02 22:09:19 +08:00
|
|
|
#define AVDTP_START 0x07
|
2009-09-02 22:20:31 +08:00
|
|
|
#define AVDTP_CLOSE 0x08
|
2009-09-02 22:30:08 +08:00
|
|
|
#define AVDTP_SUSPEND 0x09
|
2009-09-03 23:23:03 +08:00
|
|
|
#define AVDTP_ABORT 0x0A
|
2009-09-01 03:01:56 +08:00
|
|
|
|
|
|
|
#define AVDTP_SEP_TYPE_SOURCE 0x00
|
|
|
|
#define AVDTP_SEP_TYPE_SINK 0x01
|
|
|
|
|
|
|
|
#define AVDTP_MEDIA_TYPE_AUDIO 0x00
|
|
|
|
#define AVDTP_MEDIA_TYPE_VIDEO 0x01
|
|
|
|
#define AVDTP_MEDIA_TYPE_MULTIMEDIA 0x02
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
|
|
|
|
struct avdtp_header {
|
|
|
|
uint8_t message_type:2;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint8_t signal_id:6;
|
|
|
|
uint8_t rfa0:2;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct seid_info {
|
|
|
|
uint8_t rfa0:1;
|
|
|
|
uint8_t inuse:1;
|
|
|
|
uint8_t seid:6;
|
|
|
|
uint8_t rfa2:3;
|
|
|
|
uint8_t type:1;
|
|
|
|
uint8_t media_type:4;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2009-09-03 01:04:44 +08:00
|
|
|
struct avdtp_start_header {
|
|
|
|
uint8_t message_type:2;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint8_t no_of_packets;
|
|
|
|
uint8_t signal_id:6;
|
|
|
|
uint8_t rfa0:2;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct avdtp_continue_header {
|
|
|
|
uint8_t message_type:2;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t transaction:4;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
|
|
|
struct avdtp_header {
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t message_type:2;
|
|
|
|
uint8_t rfa0:2;
|
|
|
|
uint8_t signal_id:6;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct seid_info {
|
|
|
|
uint8_t seid:6;
|
|
|
|
uint8_t inuse:1;
|
|
|
|
uint8_t rfa0:1;
|
|
|
|
uint8_t media_type:4;
|
|
|
|
uint8_t type:1;
|
|
|
|
uint8_t rfa2:3;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2009-09-03 01:04:44 +08:00
|
|
|
struct avdtp_start_header {
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t message_type:2;
|
|
|
|
uint8_t no_of_packets;
|
|
|
|
uint8_t rfa0:2;
|
|
|
|
uint8_t signal_id:6;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct avdtp_continue_header {
|
|
|
|
uint8_t transaction:4;
|
|
|
|
uint8_t packet_type:2;
|
|
|
|
uint8_t message_type:2;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
#else
|
|
|
|
#error "Unknown byte order"
|
|
|
|
#endif
|
|
|
|
|
2009-09-02 21:17:02 +08:00
|
|
|
static const unsigned char media_transport[] = {
|
|
|
|
0x01, /* Media transport category */
|
|
|
|
0x00,
|
|
|
|
0x07, /* Media codec category */
|
|
|
|
0x06,
|
|
|
|
0x00, /* Media type audio */
|
|
|
|
0x00, /* Codec SBC */
|
|
|
|
0x22, /* 44.1 kHz, stereo */
|
|
|
|
0x15, /* 16 blocks, 8 subbands */
|
|
|
|
0x02,
|
|
|
|
0x33,
|
|
|
|
};
|
|
|
|
|
2009-09-02 21:53:11 +08:00
|
|
|
static int media_sock = -1;
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
static void dump_header(struct avdtp_header *hdr)
|
|
|
|
{
|
|
|
|
printf("TL %d PT %d MT %d SI %d\n", hdr->transaction,
|
|
|
|
hdr->packet_type, hdr->message_type, hdr->signal_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_buffer(const unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
printf("%02x ", buf[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
static void process_avdtp(int srv_sk, int sk, unsigned char reject,
|
2009-09-03 01:04:44 +08:00
|
|
|
int fragment)
|
2009-09-01 03:01:56 +08:00
|
|
|
{
|
|
|
|
unsigned char buf[672];
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct avdtp_header *hdr = (void *) buf;
|
|
|
|
|
|
|
|
len = read(sk, buf, sizeof(buf));
|
|
|
|
if (len <= 0) {
|
|
|
|
perror("Read failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_buffer(buf, len);
|
|
|
|
dump_header(hdr);
|
|
|
|
|
|
|
|
if (hdr->packet_type != AVDTP_PKT_TYPE_SINGLE) {
|
|
|
|
fprintf(stderr, "Only single packets are supported\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr->message_type != AVDTP_MSG_TYPE_COMMAND) {
|
|
|
|
fprintf(stderr, "Ignoring non-command messages\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (hdr->signal_id) {
|
|
|
|
case AVDTP_DISCOVER:
|
|
|
|
if (reject == AVDTP_DISCOVER) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[2] = 0x29; /* Unsupported configuration */
|
|
|
|
printf("Rejecting discover command\n");
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
} else {
|
|
|
|
struct seid_info *sei = (void *) (buf + 2);
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
buf[2] = 0x00;
|
|
|
|
buf[3] = 0x00;
|
|
|
|
sei->seid = 0x01;
|
|
|
|
sei->type = AVDTP_SEP_TYPE_SINK;
|
|
|
|
sei->media_type = AVDTP_MEDIA_TYPE_AUDIO;
|
|
|
|
printf("Accepting discover command\n");
|
|
|
|
len = write(sk, buf, 4);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AVDTP_GET_CAPABILITIES:
|
|
|
|
if (reject == AVDTP_GET_CAPABILITIES) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[2] = 0x29; /* Unsupported configuration */
|
|
|
|
printf("Rejecting get capabilties command\n");
|
|
|
|
len = write(sk, buf, 3);
|
2009-09-03 01:04:44 +08:00
|
|
|
} else if (fragment) {
|
|
|
|
struct avdtp_start_header *start = (void *) buf;
|
|
|
|
|
|
|
|
printf("Sending fragmented reply to getcap\n");
|
|
|
|
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
|
|
|
|
/* Start packet */
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_START;
|
|
|
|
start->signal_id = AVDTP_GET_CAPABILITIES;
|
|
|
|
start->no_of_packets = 3;
|
|
|
|
memcpy(&buf[3], media_transport,
|
|
|
|
sizeof(media_transport));
|
|
|
|
len = write(sk, buf,
|
|
|
|
3 + sizeof(media_transport));
|
|
|
|
|
|
|
|
/* Continue packet */
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_CONTINUE;
|
|
|
|
memcpy(&buf[1], media_transport,
|
|
|
|
sizeof(media_transport));
|
|
|
|
len = write(sk, buf,
|
|
|
|
1 + sizeof(media_transport));
|
|
|
|
|
|
|
|
/* End packet */
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_END;
|
|
|
|
memcpy(&buf[1], media_transport,
|
|
|
|
sizeof(media_transport));
|
|
|
|
len = write(sk, buf,
|
|
|
|
1 + sizeof(media_transport));
|
2009-09-01 03:01:56 +08:00
|
|
|
} else {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
2009-09-02 21:17:02 +08:00
|
|
|
memcpy(&buf[2], media_transport,
|
|
|
|
sizeof(media_transport));
|
2009-09-01 03:01:56 +08:00
|
|
|
printf("Accepting get capabilities command\n");
|
2009-09-02 21:17:02 +08:00
|
|
|
len = write(sk, buf,
|
|
|
|
2 + sizeof(media_transport));
|
2009-09-01 03:01:56 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-02 23:58:24 +08:00
|
|
|
case AVDTP_SET_CONFIGURATION:
|
|
|
|
if (reject == AVDTP_SET_CONFIGURATION) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[2] = buf[4];
|
|
|
|
buf[3] = 0x13; /* SEP In Use */
|
|
|
|
printf("Rejecting set configuration command\n");
|
|
|
|
len = write(sk, buf, 4);
|
|
|
|
} else {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting set configuration command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-03 00:03:37 +08:00
|
|
|
case AVDTP_GET_CONFIGURATION:
|
|
|
|
if (reject == AVDTP_GET_CONFIGURATION) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[2] = 0x12; /* Bad ACP SEID */
|
|
|
|
printf("Rejecting get configuration command\n");
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
} else {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting get configuration command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-03 00:18:28 +08:00
|
|
|
case AVDTP_OPEN:
|
|
|
|
if (reject == AVDTP_OPEN) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[2] = 0x31; /* Bad State */
|
|
|
|
printf("Rejecting open command\n");
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_l2 addr;
|
|
|
|
socklen_t optlen;
|
|
|
|
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting open command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
optlen = sizeof(addr);
|
|
|
|
|
|
|
|
media_sock = accept(srv_sk,
|
|
|
|
(struct sockaddr *) &addr,
|
|
|
|
&optlen);
|
|
|
|
if (media_sock < 0) {
|
|
|
|
perror("Accept failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-03 00:23:24 +08:00
|
|
|
case AVDTP_START:
|
2009-09-03 23:23:03 +08:00
|
|
|
if (reject == AVDTP_ABORT)
|
|
|
|
printf("Ignoring start to cause abort");
|
|
|
|
else if (reject == AVDTP_START) {
|
2009-09-03 00:23:24 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[3] = 0x31; /* Bad State */
|
|
|
|
printf("Rejecting start command\n");
|
|
|
|
len = write(sk, buf, 4);
|
|
|
|
} else {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting start command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-03 00:26:07 +08:00
|
|
|
case AVDTP_CLOSE:
|
|
|
|
if (reject == AVDTP_CLOSE) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[2] = 0x31; /* Bad State */
|
|
|
|
printf("Rejecting close command\n");
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
} else {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting close command\n");
|
|
|
|
len = write(sk, buf, 2);
|
2009-09-03 00:31:08 +08:00
|
|
|
if (media_sock >= 0) {
|
|
|
|
close(media_sock);
|
|
|
|
media_sock = -1;
|
|
|
|
}
|
2009-09-03 00:26:07 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-03 00:30:02 +08:00
|
|
|
case AVDTP_SUSPEND:
|
|
|
|
if (reject == AVDTP_SUSPEND) {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_REJECT;
|
|
|
|
buf[3] = 0x31; /* Bad State */
|
|
|
|
printf("Rejecting suspend command\n");
|
|
|
|
len = write(sk, buf, 4);
|
|
|
|
} else {
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting suspend command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-03 23:23:03 +08:00
|
|
|
case AVDTP_ABORT:
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_ACCEPT;
|
|
|
|
printf("Accepting abort command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
if (media_sock >= 0) {
|
|
|
|
close(media_sock);
|
|
|
|
media_sock = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
default:
|
|
|
|
buf[1] = 0x00;
|
|
|
|
printf("Unknown command\n");
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
static void process_avctp(int sk, int reject)
|
|
|
|
{
|
|
|
|
unsigned char buf[672];
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
len = read(sk, buf, sizeof(buf));
|
|
|
|
if (len <= 0) {
|
|
|
|
perror("Read failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-03 03:17:02 +08:00
|
|
|
static int set_minimum_mtu(int sk)
|
|
|
|
{
|
|
|
|
struct l2cap_options l2o;
|
|
|
|
socklen_t optlen;
|
|
|
|
|
|
|
|
memset(&l2o, 0, sizeof(l2o));
|
|
|
|
optlen = sizeof(l2o);
|
|
|
|
|
|
|
|
if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &optlen) < 0) {
|
|
|
|
perror("getsockopt");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
l2o.imtu = 48;
|
|
|
|
l2o.omtu = 48;
|
|
|
|
|
|
|
|
if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0) {
|
|
|
|
perror("setsockopt");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-03 01:04:44 +08:00
|
|
|
static void do_listen(const bdaddr_t *src, unsigned char reject, int fragment)
|
2009-09-01 03:01:56 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_l2 addr;
|
|
|
|
socklen_t optlen;
|
|
|
|
int sk, nsk;
|
|
|
|
|
|
|
|
sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
|
|
|
|
if (sk < 0) {
|
|
|
|
perror("Can't create socket");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.l2_family = AF_BLUETOOTH;
|
|
|
|
bacpy(&addr.l2_bdaddr, src);
|
|
|
|
addr.l2_psm = htobs(25);
|
|
|
|
|
|
|
|
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
perror("Can't bind socket");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2009-09-03 03:17:02 +08:00
|
|
|
if (fragment)
|
|
|
|
set_minimum_mtu(sk);
|
2009-09-03 01:04:44 +08:00
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
if (listen(sk, 10)) {
|
|
|
|
perror("Can't listen on the socket");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
optlen = sizeof(addr);
|
|
|
|
|
|
|
|
nsk = accept(sk, (struct sockaddr *) &addr, &optlen);
|
|
|
|
if (nsk < 0) {
|
|
|
|
perror("Accept failed");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
process_avdtp(sk, nsk, reject, fragment);
|
2009-09-03 00:18:28 +08:00
|
|
|
|
|
|
|
if (media_sock >= 0) {
|
|
|
|
close(media_sock);
|
|
|
|
media_sock = -1;
|
|
|
|
}
|
2009-09-01 03:01:56 +08:00
|
|
|
|
|
|
|
close(nsk);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
close(sk);
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
static int do_connect(const bdaddr_t *src, const bdaddr_t *dst, int avctp,
|
|
|
|
int fragment)
|
2009-09-02 20:07:01 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_l2 addr;
|
|
|
|
int sk, err;
|
|
|
|
|
|
|
|
sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
|
|
|
|
if (sk < 0) {
|
|
|
|
perror("Can't create socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.l2_family = AF_BLUETOOTH;
|
|
|
|
bacpy(&addr.l2_bdaddr, src);
|
|
|
|
|
|
|
|
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
perror("Can't bind socket");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2009-09-03 03:17:02 +08:00
|
|
|
if (fragment)
|
|
|
|
set_minimum_mtu(sk);
|
|
|
|
|
2009-09-02 20:07:01 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.l2_family = AF_BLUETOOTH;
|
|
|
|
bacpy(&addr.l2_bdaddr, dst);
|
2009-09-04 02:46:18 +08:00
|
|
|
addr.l2_psm = htobs(avctp ? 23 : 25);
|
2009-09-02 20:07:01 +08:00
|
|
|
|
|
|
|
err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
|
|
|
|
if (err < 0) {
|
|
|
|
perror("Unable to connect");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sk;
|
|
|
|
|
|
|
|
error:
|
|
|
|
close(sk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
static void do_avdtp_send(int sk, const bdaddr_t *src, const bdaddr_t *dst,
|
2009-09-02 21:53:11 +08:00
|
|
|
unsigned char cmd, int invalid, int preconf)
|
2009-09-02 20:07:01 +08:00
|
|
|
{
|
|
|
|
unsigned char buf[672];
|
|
|
|
struct avdtp_header *hdr = (void *) buf;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AVDTP_DISCOVER:
|
2009-09-02 20:16:57 +08:00
|
|
|
if (invalid)
|
|
|
|
hdr->message_type = 0x01;
|
|
|
|
else
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
2009-09-02 20:07:01 +08:00
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_DISCOVER;
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
break;
|
2009-09-02 20:43:19 +08:00
|
|
|
|
2009-09-02 20:32:51 +08:00
|
|
|
case AVDTP_GET_CAPABILITIES:
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_GET_CAPABILITIES;
|
|
|
|
buf[2] = 1 << 2; /* SEID 1 */
|
|
|
|
len = write(sk, buf, invalid ? 2 : 3);
|
|
|
|
break;
|
2009-09-02 20:43:19 +08:00
|
|
|
|
|
|
|
case AVDTP_SET_CONFIGURATION:
|
2009-09-02 23:39:53 +08:00
|
|
|
if (preconf)
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, cmd, 0, 0);
|
2009-09-02 20:43:19 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_SET_CONFIGURATION;
|
|
|
|
buf[2] = 1 << 2; /* ACP SEID */
|
|
|
|
buf[3] = 1 << 2; /* INT SEID */
|
2009-09-02 21:17:02 +08:00
|
|
|
memcpy(&buf[4], media_transport, sizeof(media_transport));
|
2009-09-02 23:39:53 +08:00
|
|
|
if (invalid)
|
|
|
|
buf[5] = 0x01; /* LOSC != 0 */
|
2009-09-02 21:17:02 +08:00
|
|
|
len = write(sk, buf, 4 + sizeof(media_transport));
|
2009-09-02 20:55:34 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVDTP_GET_CONFIGURATION:
|
2009-09-02 21:07:39 +08:00
|
|
|
if (preconf)
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_SET_CONFIGURATION, 0, 0);
|
2009-09-02 20:55:34 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_GET_CONFIGURATION;
|
|
|
|
if (invalid)
|
|
|
|
buf[2] = 13 << 2; /* Invalid ACP SEID */
|
|
|
|
else
|
|
|
|
buf[2] = 1 << 2; /* Valid ACP SEID */
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
break;
|
2009-09-02 21:53:11 +08:00
|
|
|
|
|
|
|
case AVDTP_OPEN:
|
|
|
|
if (preconf)
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_SET_CONFIGURATION, 0, 0);
|
2009-09-02 21:53:11 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_OPEN;
|
|
|
|
buf[2] = 1 << 2; /* ACP SEID */
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
break;
|
2009-09-02 22:09:19 +08:00
|
|
|
|
|
|
|
case AVDTP_START:
|
|
|
|
if (preconf)
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_SET_CONFIGURATION, 0, 0);
|
2009-09-02 22:09:19 +08:00
|
|
|
if (!invalid)
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_OPEN, 0, 0);
|
2009-09-02 22:09:19 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_START;
|
|
|
|
buf[2] = 1 << 2; /* ACP SEID */
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
break;
|
2009-09-02 22:20:31 +08:00
|
|
|
|
|
|
|
case AVDTP_CLOSE:
|
|
|
|
if (preconf) {
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_SET_CONFIGURATION, 0, 0);
|
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_OPEN, 0, 0);
|
2009-09-02 22:20:31 +08:00
|
|
|
}
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_CLOSE;
|
|
|
|
if (invalid)
|
|
|
|
buf[2] = 13 << 2; /* Invalid ACP SEID */
|
|
|
|
else
|
|
|
|
buf[2] = 1 << 2; /* Valid ACP SEID */
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
break;
|
2009-09-02 22:30:08 +08:00
|
|
|
|
|
|
|
case AVDTP_SUSPEND:
|
|
|
|
if (invalid)
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_OPEN, 0, preconf);
|
2009-09-02 22:30:08 +08:00
|
|
|
else
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_START, 0, preconf);
|
2009-09-02 22:30:08 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_SUSPEND;
|
|
|
|
buf[2] = 1 << 2; /* ACP SEID */
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
break;
|
2009-09-02 22:44:40 +08:00
|
|
|
|
2009-09-03 23:23:03 +08:00
|
|
|
case AVDTP_ABORT:
|
2009-09-04 02:46:18 +08:00
|
|
|
do_avdtp_send(sk, src, dst, AVDTP_OPEN, 0, 1);
|
2009-09-03 23:23:03 +08:00
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = AVDTP_ABORT;
|
|
|
|
buf[2] = 1 << 2; /* ACP SEID */
|
|
|
|
len = write(sk, buf, 3);
|
|
|
|
break;
|
|
|
|
|
2009-09-02 22:44:40 +08:00
|
|
|
default:
|
|
|
|
hdr->message_type = AVDTP_MSG_TYPE_COMMAND;
|
|
|
|
hdr->packet_type = AVDTP_PKT_TYPE_SINGLE;
|
|
|
|
hdr->signal_id = cmd;
|
|
|
|
len = write(sk, buf, 2);
|
|
|
|
break;
|
2009-09-02 20:07:01 +08:00
|
|
|
}
|
|
|
|
|
2009-09-03 21:21:57 +08:00
|
|
|
do {
|
|
|
|
len = read(sk, buf, sizeof(buf));
|
2009-09-02 20:07:01 +08:00
|
|
|
|
2009-09-03 21:21:57 +08:00
|
|
|
dump_buffer(buf, len);
|
|
|
|
dump_header(hdr);
|
|
|
|
} while (len < 2 || (hdr->message_type != AVDTP_MSG_TYPE_ACCEPT &&
|
|
|
|
hdr->message_type != AVDTP_MSG_TYPE_REJECT &&
|
|
|
|
hdr->message_type != AVDTP_MSG_TYPE_GEN_REJECT));
|
2009-09-02 21:53:11 +08:00
|
|
|
|
2009-09-02 22:02:46 +08:00
|
|
|
if (cmd == AVDTP_OPEN && len >= 2 &&
|
|
|
|
hdr->message_type == AVDTP_MSG_TYPE_ACCEPT)
|
2009-09-04 02:46:18 +08:00
|
|
|
media_sock = do_connect(src, dst, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_avctp_send(int sk, int invalid)
|
|
|
|
{
|
2009-09-02 20:07:01 +08:00
|
|
|
}
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
static void usage()
|
|
|
|
{
|
|
|
|
printf("avtest - Audio/Video testing ver %s\n", VERSION);
|
|
|
|
printf("Usage:\n"
|
2009-09-02 20:07:01 +08:00
|
|
|
"\tavtest [options] [remote address]\n");
|
2009-09-01 03:01:56 +08:00
|
|
|
printf("Options:\n"
|
2009-09-04 02:01:17 +08:00
|
|
|
"\t--device <hcidev> HCI device\n"
|
|
|
|
"\t--reject <command> Reject command\n"
|
|
|
|
"\t--send <command> Send command\n"
|
|
|
|
"\t--preconf Configure stream before actual command\n"
|
|
|
|
"\t--fragment Use minimum MTU and fragmented messages\n"
|
|
|
|
"\t--invalid <command> Send invalid command\n");
|
2009-09-01 03:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct option main_options[] = {
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "device", 1, 0, 'i' },
|
|
|
|
{ "reject", 1, 0, 'r' },
|
2009-09-02 20:07:01 +08:00
|
|
|
{ "send", 1, 0, 's' },
|
|
|
|
{ "invalid", 1, 0, 'f' },
|
2009-09-02 21:07:39 +08:00
|
|
|
{ "preconf", 0, 0, 'c' },
|
2009-09-03 01:04:44 +08:00
|
|
|
{ "fragment", 0, 0, 'F' },
|
2009-09-04 02:46:18 +08:00
|
|
|
{ "avctp", 0, 0, 'C' },
|
2009-09-01 03:01:56 +08:00
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2009-09-02 20:07:01 +08:00
|
|
|
static unsigned char parse_cmd(const char *arg)
|
|
|
|
{
|
|
|
|
if (!strncmp(arg, "discov", 6))
|
|
|
|
return AVDTP_DISCOVER;
|
|
|
|
else if (!strncmp(arg, "capa", 4))
|
|
|
|
return AVDTP_GET_CAPABILITIES;
|
|
|
|
else if (!strncmp(arg, "getcapa", 7))
|
|
|
|
return AVDTP_GET_CAPABILITIES;
|
|
|
|
else if (!strncmp(arg, "setconf", 7))
|
|
|
|
return AVDTP_SET_CONFIGURATION;
|
|
|
|
else if (!strncmp(arg, "getconf", 7))
|
|
|
|
return AVDTP_GET_CONFIGURATION;
|
2009-09-02 21:53:11 +08:00
|
|
|
else if (!strncmp(arg, "open", 4))
|
|
|
|
return AVDTP_OPEN;
|
2009-09-02 22:10:01 +08:00
|
|
|
else if (!strncmp(arg, "start", 5))
|
2009-09-02 22:09:19 +08:00
|
|
|
return AVDTP_START;
|
2009-09-02 22:20:31 +08:00
|
|
|
else if (!strncmp(arg, "close", 5))
|
|
|
|
return AVDTP_CLOSE;
|
2009-09-02 22:30:08 +08:00
|
|
|
else if (!strncmp(arg, "suspend", 7))
|
|
|
|
return AVDTP_SUSPEND;
|
2009-09-03 23:23:03 +08:00
|
|
|
else if (!strncmp(arg, "abort", 7))
|
|
|
|
return AVDTP_ABORT;
|
2009-09-02 20:07:01 +08:00
|
|
|
else
|
|
|
|
return atoi(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
2009-09-02 20:16:57 +08:00
|
|
|
MODE_NONE, MODE_REJECT, MODE_SEND,
|
2009-09-02 20:07:01 +08:00
|
|
|
};
|
|
|
|
|
2009-02-27 15:20:53 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2009-09-02 20:07:01 +08:00
|
|
|
unsigned char cmd = 0x00;
|
2009-09-01 03:01:56 +08:00
|
|
|
bdaddr_t src, dst;
|
2009-09-03 01:04:44 +08:00
|
|
|
int opt, mode = MODE_NONE, sk, invalid = 0, preconf = 0, fragment = 0;
|
2009-09-04 02:46:18 +08:00
|
|
|
int avctp = 0;
|
2009-09-01 03:01:56 +08:00
|
|
|
|
|
|
|
bacpy(&src, BDADDR_ANY);
|
|
|
|
bacpy(&dst, BDADDR_ANY);
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, "+i:r:s:f:hcFC",
|
2009-09-01 03:01:56 +08:00
|
|
|
main_options, NULL)) != EOF) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'i':
|
|
|
|
if (!strncmp(optarg, "hci", 3))
|
|
|
|
hci_devba(atoi(optarg + 3), &src);
|
|
|
|
else
|
|
|
|
str2ba(optarg, &src);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2009-09-02 20:07:01 +08:00
|
|
|
mode = MODE_REJECT;
|
|
|
|
cmd = parse_cmd(optarg);
|
|
|
|
break;
|
|
|
|
|
2009-09-02 20:16:57 +08:00
|
|
|
case 'f':
|
|
|
|
invalid = 1;
|
|
|
|
/* Intentionally missing break */
|
|
|
|
|
2009-09-02 20:07:01 +08:00
|
|
|
case 's':
|
|
|
|
mode = MODE_SEND;
|
|
|
|
cmd = parse_cmd(optarg);
|
|
|
|
break;
|
|
|
|
|
2009-09-02 21:07:39 +08:00
|
|
|
case 'c':
|
|
|
|
preconf = 1;
|
|
|
|
break;
|
|
|
|
|
2009-09-03 01:04:44 +08:00
|
|
|
case 'F':
|
|
|
|
fragment = 1;
|
|
|
|
break;
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
case 'C':
|
|
|
|
avctp = 1;
|
|
|
|
break;
|
|
|
|
|
2009-09-01 03:01:56 +08:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-02 20:07:01 +08:00
|
|
|
if (argv[optind])
|
|
|
|
str2ba(argv[optind], &dst);
|
|
|
|
|
2009-09-04 02:46:18 +08:00
|
|
|
if (avctp) {
|
|
|
|
avctp = mode;
|
|
|
|
mode = MODE_SEND;
|
|
|
|
}
|
|
|
|
|
2009-09-02 20:07:01 +08:00
|
|
|
switch (mode) {
|
|
|
|
case MODE_REJECT:
|
2009-09-03 01:04:44 +08:00
|
|
|
do_listen(&src, cmd, fragment);
|
2009-09-02 20:07:01 +08:00
|
|
|
break;
|
|
|
|
case MODE_SEND:
|
2009-09-04 02:46:18 +08:00
|
|
|
sk = do_connect(&src, &dst, avctp, fragment);
|
2009-09-02 20:07:01 +08:00
|
|
|
if (sk < 0)
|
|
|
|
exit(1);
|
2009-09-04 02:46:18 +08:00
|
|
|
if (avctp) {
|
|
|
|
if (avctp == MODE_SEND)
|
|
|
|
do_avctp_send(sk, invalid);
|
|
|
|
else
|
|
|
|
process_avctp(sk, cmd);
|
|
|
|
} else
|
|
|
|
do_avdtp_send(sk, &src, &dst, cmd, invalid, preconf);
|
2009-09-02 21:53:11 +08:00
|
|
|
if (media_sock >= 0)
|
|
|
|
close(media_sock);
|
2009-09-02 20:07:01 +08:00
|
|
|
close(sk);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "No operating mode specified!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-09-01 03:01:56 +08:00
|
|
|
|
2009-02-27 15:20:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|