3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 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
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
42 #include <bluetooth/bluetooth.h>
43 #include <bluetooth/hci.h>
44 #include <bluetooth/hci_lib.h>
45 #include <bluetooth/l2cap.h>
50 #define AVDTP_DISCOVER 0x01
51 #define AVDTP_GET_CAPABILITIES 0x02
53 #define AVDTP_PKT_TYPE_SINGLE 0x00
55 #define AVDTP_MSG_TYPE_COMMAND 0x00
57 /* SEP capability categories */
58 #define AVDTP_MEDIA_TRANSPORT 0x01
59 #define AVDTP_REPORTING 0x02
60 #define AVDTP_RECOVERY 0x03
61 #define AVDTP_CONTENT_PROTECTION 0x04
62 #define AVDTP_HEADER_COMPRESSION 0x05
63 #define AVDTP_MULTIPLEXING 0x06
64 #define AVDTP_MEDIA_CODEC 0x07
66 /* SEP types definitions */
67 #define AVDTP_SEP_TYPE_SOURCE 0x00
68 #define AVDTP_SEP_TYPE_SINK 0x01
70 /* Media types definitions */
71 #define AVDTP_MEDIA_TYPE_AUDIO 0x00
72 #define AVDTP_MEDIA_TYPE_VIDEO 0x01
73 #define AVDTP_MEDIA_TYPE_MULTIMEDIA 0x02
75 #define A2DP_CODEC_SBC 0x00
76 #define A2DP_CODEC_MPEG12 0x01
77 #define A2DP_CODEC_MPEG24 0x02
78 #define A2DP_CODEC_ATRAC 0x03
80 #define SBC_SAMPLING_FREQ_16000 (1 << 3)
81 #define SBC_SAMPLING_FREQ_32000 (1 << 2)
82 #define SBC_SAMPLING_FREQ_44100 (1 << 1)
83 #define SBC_SAMPLING_FREQ_48000 (1 << 0)
85 #define SBC_CHANNEL_MODE_MONO (1 << 3)
86 #define SBC_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
87 #define SBC_CHANNEL_MODE_STEREO (1 << 1)
88 #define SBC_CHANNEL_MODE_JOINT_STEREO (1 << 0)
90 #define SBC_BLOCK_LENGTH_4 (1 << 3)
91 #define SBC_BLOCK_LENGTH_8 (1 << 2)
92 #define SBC_BLOCK_LENGTH_12 (1 << 1)
93 #define SBC_BLOCK_LENGTH_16 (1 << 0)
95 #define SBC_SUBBANDS_4 (1 << 1)
96 #define SBC_SUBBANDS_8 (1 << 0)
98 #define SBC_ALLOCATION_SNR (1 << 1)
99 #define SBC_ALLOCATION_LOUDNESS (1 << 0)
101 #define MPEG_CHANNEL_MODE_MONO (1 << 3)
102 #define MPEG_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
103 #define MPEG_CHANNEL_MODE_STEREO (1 << 1)
104 #define MPEG_CHANNEL_MODE_JOINT_STEREO (1 << 0)
106 #define MPEG_LAYER_MP1 (1 << 2)
107 #define MPEG_LAYER_MP2 (1 << 1)
108 #define MPEG_LAYER_MP3 (1 << 0)
110 #define MPEG_SAMPLING_FREQ_16000 (1 << 5)
111 #define MPEG_SAMPLING_FREQ_22050 (1 << 4)
112 #define MPEG_SAMPLING_FREQ_24000 (1 << 3)
113 #define MPEG_SAMPLING_FREQ_32000 (1 << 2)
114 #define MPEG_SAMPLING_FREQ_44100 (1 << 1)
115 #define MPEG_SAMPLING_FREQ_48000 (1 << 0)
117 #define MPEG_BIT_RATE_VBR 0x8000
118 #define MPEG_BIT_RATE_320000 0x4000
119 #define MPEG_BIT_RATE_256000 0x2000
120 #define MPEG_BIT_RATE_224000 0x1000
121 #define MPEG_BIT_RATE_192000 0x0800
122 #define MPEG_BIT_RATE_160000 0x0400
123 #define MPEG_BIT_RATE_128000 0x0200
124 #define MPEG_BIT_RATE_112000 0x0100
125 #define MPEG_BIT_RATE_96000 0x0080
126 #define MPEG_BIT_RATE_80000 0x0040
127 #define MPEG_BIT_RATE_64000 0x0020
128 #define MPEG_BIT_RATE_56000 0x0010
129 #define MPEG_BIT_RATE_48000 0x0008
130 #define MPEG_BIT_RATE_40000 0x0004
131 #define MPEG_BIT_RATE_32000 0x0002
132 #define MPEG_BIT_RATE_FREE 0x0001
134 struct avdtp_service_capability {
138 } __attribute__ ((packed));
140 #if __BYTE_ORDER == __LITTLE_ENDIAN
142 struct avdtp_header {
143 uint8_t message_type:2;
144 uint8_t packet_type:2;
145 uint8_t transaction:4;
148 } __attribute__ ((packed));
156 uint8_t media_type:4;
157 } __attribute__ ((packed));
160 struct avdtp_header header;
163 } __attribute__ ((packed));
165 struct avdtp_media_codec_capability {
167 uint8_t media_type:4;
168 uint8_t media_codec_type;
170 } __attribute__ ((packed));
172 struct sbc_codec_cap {
173 struct avdtp_media_codec_capability cap;
174 uint8_t channel_mode:4;
176 uint8_t allocation_method:2;
178 uint8_t block_length:4;
181 } __attribute__ ((packed));
183 struct mpeg_codec_cap {
184 struct avdtp_media_codec_capability cap;
185 uint8_t channel_mode:4;
192 } __attribute__ ((packed));
194 #elif __BYTE_ORDER == __BIG_ENDIAN
196 struct avdtp_header {
197 uint8_t transaction:4;
198 uint8_t packet_type:2;
199 uint8_t message_type:2;
202 } __attribute__ ((packed));
208 uint8_t media_type:4;
211 } __attribute__ ((packed));
214 struct avdtp_header header;
217 } __attribute__ ((packed));
219 struct avdtp_media_codec_capability {
220 uint8_t media_type:4;
222 uint8_t media_codec_type;
224 } __attribute__ ((packed));
226 struct sbc_codec_cap {
227 struct avdtp_media_codec_capability cap;
229 uint8_t channel_mode:4;
230 uint8_t block_length:4;
232 uint8_t allocation_method:2;
235 } __attribute__ ((packed));
237 struct mpeg_codec_cap {
238 struct avdtp_media_codec_capability cap;
241 uint8_t channel_mode:4;
246 } __attribute__ ((packed));
249 #error "Unknown byte order"
252 struct discover_resp {
253 struct avdtp_header header;
254 struct seid_info seps[0];
255 } __attribute__ ((packed));
258 struct avdtp_header header;
260 } __attribute__ ((packed));
263 static void print_mpeg12(struct mpeg_codec_cap *mpeg)
265 printf("\tMedia Codec: MPEG12\n\t\tChannel Modes: ");
267 if (mpeg->channel_mode & MPEG_CHANNEL_MODE_MONO)
269 if (mpeg->channel_mode & MPEG_CHANNEL_MODE_DUAL_CHANNEL)
270 printf("DualChannel ");
271 if (mpeg->channel_mode & MPEG_CHANNEL_MODE_STEREO)
273 if (mpeg->channel_mode & MPEG_CHANNEL_MODE_JOINT_STEREO)
274 printf("JointStereo");
276 printf("\n\t\tFrequencies: ");
277 if (mpeg->frequency & MPEG_SAMPLING_FREQ_16000)
279 if (mpeg->frequency & MPEG_SAMPLING_FREQ_22050)
281 if (mpeg->frequency & MPEG_SAMPLING_FREQ_24000)
283 if (mpeg->frequency & MPEG_SAMPLING_FREQ_32000)
285 if (mpeg->frequency & MPEG_SAMPLING_FREQ_44100)
287 if (mpeg->frequency & MPEG_SAMPLING_FREQ_48000)
290 printf("\n\t\tCRC: %s", mpeg->crc ? "Yes" : "No");
292 printf("\n\t\tLayer: ");
293 if (mpeg->layer & MPEG_LAYER_MP1)
295 if (mpeg->layer & MPEG_LAYER_MP2)
297 if (mpeg->layer & MPEG_LAYER_MP3)
300 printf("\n\t\tBit Rate: ");
301 if (mpeg->bitrate & MPEG_BIT_RATE_FREE)
302 printf("Free format");
304 if (mpeg->bitrate & MPEG_BIT_RATE_32000)
306 if (mpeg->bitrate & MPEG_BIT_RATE_40000)
308 if (mpeg->bitrate & MPEG_BIT_RATE_48000)
310 if (mpeg->bitrate & MPEG_BIT_RATE_56000)
312 if (mpeg->bitrate & MPEG_BIT_RATE_64000)
314 if (mpeg->bitrate & MPEG_BIT_RATE_80000)
316 if (mpeg->bitrate & MPEG_BIT_RATE_96000)
318 if (mpeg->bitrate & MPEG_BIT_RATE_112000)
320 if (mpeg->bitrate & MPEG_BIT_RATE_128000)
322 if (mpeg->bitrate & MPEG_BIT_RATE_160000)
324 if (mpeg->bitrate & MPEG_BIT_RATE_192000)
326 if (mpeg->bitrate & MPEG_BIT_RATE_224000)
328 if (mpeg->bitrate & MPEG_BIT_RATE_256000)
330 if (mpeg->bitrate & MPEG_BIT_RATE_320000)
334 printf("\n\t\tVBR: %s", mpeg->bitrate & MPEG_BIT_RATE_VBR ? "Yes" :
337 printf("\n\t\tPayload Format: ");
339 printf("RFC-2250 RFC-3119\n");
341 printf("RFC-2250\n");
344 static void print_sbc(struct sbc_codec_cap *sbc)
346 printf("\tMedia Codec: SBC\n\t\tChannel Modes: ");
348 if (sbc->channel_mode & SBC_CHANNEL_MODE_MONO)
350 if (sbc->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
351 printf("DualChannel ");
352 if (sbc->channel_mode & SBC_CHANNEL_MODE_STEREO)
354 if (sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
355 printf("JointStereo");
357 printf("\n\t\tFrequencies: ");
358 if (sbc->frequency & SBC_SAMPLING_FREQ_16000)
360 if (sbc->frequency & SBC_SAMPLING_FREQ_32000)
362 if (sbc->frequency & SBC_SAMPLING_FREQ_44100)
364 if (sbc->frequency & SBC_SAMPLING_FREQ_48000)
367 printf("\n\t\tSubbands: ");
368 if (sbc->allocation_method & SBC_SUBBANDS_4)
370 if (sbc->allocation_method & SBC_SUBBANDS_8)
373 printf("\n\t\tBlocks: ");
374 if (sbc->block_length & SBC_BLOCK_LENGTH_4)
376 if (sbc->block_length & SBC_BLOCK_LENGTH_8)
378 if (sbc->block_length & SBC_BLOCK_LENGTH_12)
380 if (sbc->block_length & SBC_BLOCK_LENGTH_16)
383 printf("\n\t\tBitpool Range: %d-%d\n",
384 sbc->min_bitpool, sbc->max_bitpool);
387 static void print_media_codec(struct avdtp_media_codec_capability *cap)
389 switch (cap->media_codec_type) {
391 print_sbc((void *) cap);
393 case A2DP_CODEC_MPEG12:
394 print_mpeg12((void *) cap);
397 printf("\tMedia Codec: Unknown\n");
401 static void print_caps(void *data, int size)
405 for (processed = 0; processed + 2 < size;) {
406 struct avdtp_service_capability *cap;
410 if (processed + 2 + cap->length > size) {
411 printf("Invalid capability data in getcap resp\n");
415 switch (cap->category) {
416 case AVDTP_MEDIA_TRANSPORT:
417 case AVDTP_REPORTING:
419 case AVDTP_CONTENT_PROTECTION:
420 case AVDTP_MULTIPLEXING:
421 /* FIXME: Add proper functions */
423 case AVDTP_MEDIA_CODEC:
424 print_media_codec((void *) cap->data);
428 processed += 2 + cap->length;
429 data += 2 + cap->length;
433 static void init_request(struct avdtp_header *header, int request_id)
435 static int transaction = 0;
437 header->packet_type = AVDTP_PKT_TYPE_SINGLE;
438 header->message_type = AVDTP_MSG_TYPE_COMMAND;
439 header->transaction = transaction;
440 header->signal_id = request_id;
445 transaction = (transaction + 1) % 16;
448 static ssize_t avdtp_send(int sk, void *data, int len)
452 ret = send(sk, data, len, 0);
460 printf("Unable to send message: %s (%zd)\n",
461 strerror(-ret), -ret);
468 static ssize_t avdtp_receive(int sk, void *data, int len)
472 ret = recv(sk, data, len, 0);
475 printf("Unable to receive message: %s (%d)\n",
476 strerror(errno), errno);
483 static ssize_t avdtp_get_caps(int sk, int seid)
487 struct getcap_resp *caps = (void *) buffer;
490 memset(&req, 0, sizeof(req));
491 init_request(&req.header, AVDTP_GET_CAPABILITIES);
494 ret = avdtp_send(sk, &req, sizeof(req));
498 memset(&buffer, 0, sizeof(buffer));
499 ret = avdtp_receive(sk, caps, sizeof(buffer));
503 if ((size_t) ret < (sizeof(struct getcap_resp) + 4 +
504 sizeof(struct avdtp_media_codec_capability))) {
505 printf("Invalid capabilities\n");
509 print_caps(caps, ret);
514 static ssize_t avdtp_discover(int sk)
516 struct avdtp_header req;
518 struct discover_resp *discover = (void *) buffer;
522 memset(&req, 0, sizeof(req));
523 init_request(&req, AVDTP_DISCOVER);
525 ret = avdtp_send(sk, &req, sizeof(req));
529 memset(&buffer, 0, sizeof(buffer));
530 ret = avdtp_receive(sk, discover, sizeof(buffer));
534 seps = (ret - sizeof(struct avdtp_header)) / sizeof(struct seid_info);
535 for (i = 0; i < seps; i++) {
536 const char *type, *media;
538 switch (discover->seps[i].type) {
539 case AVDTP_SEP_TYPE_SOURCE:
542 case AVDTP_SEP_TYPE_SINK:
549 switch (discover->seps[i].media_type) {
550 case AVDTP_MEDIA_TYPE_AUDIO:
553 case AVDTP_MEDIA_TYPE_VIDEO:
556 case AVDTP_MEDIA_TYPE_MULTIMEDIA:
557 media = "Multimedia";
563 printf("Stream End-Point #%d: %s %s %s\n",
564 discover->seps[i].seid, media, type,
565 discover->seps[i].inuse ? "*" : "");
567 avdtp_get_caps(sk, discover->seps[i].seid);
573 static int l2cap_connect(bdaddr_t *src, bdaddr_t *dst)
575 struct sockaddr_l2 l2a;
578 memset(&l2a, 0, sizeof(l2a));
579 l2a.l2_family = AF_BLUETOOTH;
580 bacpy(&l2a.l2_bdaddr, src);
582 sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
584 printf("Cannot create L2CAP socket. %s(%d)\n", strerror(errno),
589 if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a)) < 0) {
590 printf("Bind failed. %s (%d)\n", strerror(errno), errno);
594 memset(&l2a, 0, sizeof(l2a));
595 l2a.l2_family = AF_BLUETOOTH;
596 bacpy(&l2a.l2_bdaddr, dst);
597 l2a.l2_psm = htobs(AVDTP_PSM);
599 if (connect(sk, (struct sockaddr *) &l2a, sizeof(l2a)) < 0) {
600 printf("Connect failed. %s(%d)\n", strerror(errno), errno);
607 static void usage(void)
609 printf("avinfo - Audio/Video Info Tool ver %s\n", VERSION);
611 "\tavinfo [options] <remote address>\n");
613 "\t-h\t\tDisplay help\n"
614 "\t-i\t\tSpecify source interface\n");
617 static struct option main_options[] = {
618 { "help", 0, 0, 'h' },
619 { "device", 1, 0, 'i' },
623 int main(int argc, char *argv[])
633 bacpy(&src, BDADDR_ANY);
634 dev_id = hci_get_route(&src);
635 if ((dev_id < 0) || (hci_devba(dev_id, &src) < 0)) {
636 printf("Cannot find any local adapter\n");
640 while ((opt = getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
643 if (!strncmp(optarg, "hci", 3))
644 hci_devba(atoi(optarg + 3), &src);
646 str2ba(optarg, &src);
656 printf("Connecting ... \n");
658 if (bachk(argv[optind]) < 0) {
659 printf("Invalid argument\n");
663 str2ba(argv[optind], &dst);
664 sk = l2cap_connect(&src, &dst);
668 if (avdtp_discover(sk) < 0)