3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2015 Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "lib/bluetooth.h"
34 #include "src/shared/util.h"
44 #define A2DP_CODEC_SBC 0x00
45 #define A2DP_CODEC_MPEG12 0x01
46 #define A2DP_CODEC_MPEG24 0x02
47 #define A2DP_CODEC_ATRAC 0x04
48 #define A2DP_CODEC_VENDOR 0xff
50 /* Vendor Specific A2DP Codecs */
51 #define APTX_VENDOR_ID 0x0000004f
52 #define APTX_CODEC_ID 0x0001
53 #define LDAC_VENDOR_ID 0x0000012d
54 #define LDAC_CODEC_ID 0x00aa
61 static const struct bit_desc sbc_frequency_table[] = {
69 static const struct bit_desc sbc_channel_mode_table[] = {
71 { 2, "Dual Channel" },
73 { 0, "Joint Stereo" },
77 static const struct bit_desc sbc_blocklen_table[] = {
85 static const struct bit_desc sbc_subbands_table[] = {
91 static const struct bit_desc sbc_allocation_table[] = {
97 static const struct bit_desc mpeg12_layer_table[] = {
98 { 7, "Layer I (mp1)" },
99 { 6, "Layer II (mp2)" },
100 { 5, "Layer III (mp3)" },
104 static const struct bit_desc mpeg12_channel_mode_table[] = {
106 { 2, "Dual Channel" },
108 { 0, "Joint Stereo" },
112 static const struct bit_desc mpeg12_frequency_table[] = {
122 static const struct bit_desc mpeg12_bitrate_table[] = {
141 static const struct bit_desc aac_object_type_table[] = {
142 { 7, "MPEG-2 AAC LC" },
143 { 6, "MPEG-4 AAC LC" },
144 { 5, "MPEG-4 AAC LTP" },
145 { 4, "MPEG-4 AAC scalable" },
153 static const struct bit_desc aac_frequency_table[] = {
169 static const struct bit_desc aac_channels_table[] = {
175 static const struct bit_desc aptx_frequency_table[] = {
183 static const struct bit_desc aptx_channel_mode_table[] = {
189 static void print_value_bits(uint8_t indent, uint32_t value,
190 const struct bit_desc *table)
194 for (i = 0; table[i].str; i++) {
195 if (value & (1 << table[i].bit_num))
196 print_field("%*c%s", indent + 2, ' ', table[i].str);
200 static const char *find_value_bit(uint32_t value,
201 const struct bit_desc *table)
205 for (i = 0; table[i].str; i++) {
206 if (value & (1 << table[i].bit_num))
213 static const char *vndcodec2str(uint32_t vendor_id, uint16_t codec_id)
215 if (vendor_id == APTX_VENDOR_ID && codec_id == APTX_CODEC_ID)
217 else if (vendor_id == LDAC_VENDOR_ID && codec_id == LDAC_CODEC_ID)
223 static bool codec_sbc_cap(uint8_t losc, struct l2cap_frame *frame)
230 l2cap_frame_get_u8(frame, &cap);
232 print_field("%*cFrequency: 0x%02x", BASE_INDENT, ' ', cap & 0xf0);
233 print_value_bits(BASE_INDENT, cap & 0xf0, sbc_frequency_table);
235 print_field("%*cChannel Mode: 0x%02x", BASE_INDENT, ' ', cap & 0x0f);
236 print_value_bits(BASE_INDENT, cap & 0x0f, sbc_channel_mode_table);
238 l2cap_frame_get_u8(frame, &cap);
240 print_field("%*cBlock Length: 0x%02x", BASE_INDENT, ' ', cap & 0xf0);
241 print_value_bits(BASE_INDENT, cap & 0xf0, sbc_blocklen_table);
243 print_field("%*cSubbands: 0x%02x", BASE_INDENT, ' ', cap & 0x0c);
244 print_value_bits(BASE_INDENT, cap & 0x0c, sbc_subbands_table);
246 print_field("%*cAllocation Method: 0x%02x", BASE_INDENT, ' ',
248 print_value_bits(BASE_INDENT, cap & 0x03, sbc_allocation_table);
250 l2cap_frame_get_u8(frame, &cap);
252 print_field("%*cMinimum Bitpool: %d", BASE_INDENT, ' ', cap);
254 l2cap_frame_get_u8(frame, &cap);
256 print_field("%*cMaximum Bitpool: %d", BASE_INDENT, ' ', cap);
261 static bool codec_sbc_cfg(uint8_t losc, struct l2cap_frame *frame)
268 l2cap_frame_get_u8(frame, &cap);
270 print_field("%*cFrequency: %s (0x%02x)", BASE_INDENT, ' ',
271 find_value_bit(cap & 0xf0, sbc_frequency_table),
274 print_field("%*cChannel Mode: %s (0x%02x)", BASE_INDENT, ' ',
275 find_value_bit(cap & 0x0f, sbc_channel_mode_table),
278 l2cap_frame_get_u8(frame, &cap);
280 print_field("%*cBlock Length: %s (0x%02x)", BASE_INDENT, ' ',
281 find_value_bit(cap & 0xf0, sbc_blocklen_table),
284 print_field("%*cSubbands: %s (0x%02x)", BASE_INDENT, ' ',
285 find_value_bit(cap & 0x0c, sbc_subbands_table),
288 print_field("%*cAllocation Method: %s (0x%02x)", BASE_INDENT, ' ',
289 find_value_bit(cap & 0x03, sbc_allocation_table),
292 l2cap_frame_get_u8(frame, &cap);
294 print_field("%*cMinimum Bitpool: %d", BASE_INDENT, ' ', cap);
296 l2cap_frame_get_u8(frame, &cap);
298 print_field("%*cMaximum Bitpool: %d", BASE_INDENT, ' ', cap);
303 static bool codec_mpeg12_cap(uint8_t losc, struct l2cap_frame *frame)
315 l2cap_frame_get_be16(frame, &cap);
317 layer = (cap >> 8) & 0xe0;
319 chan = (cap >> 8) & 0x0f;
323 l2cap_frame_get_be16(frame, &cap);
326 bitrate = cap & 0x7fff;
328 print_field("%*cLayer: 0x%02x", BASE_INDENT, ' ', layer);
329 print_value_bits(BASE_INDENT, layer, mpeg12_layer_table);
331 print_field("%*cCRC: %s", BASE_INDENT, ' ', crc ? "Yes" : "No");
333 print_field("%*cChannel Mode: 0x%02x", BASE_INDENT, ' ', chan);
334 print_value_bits(BASE_INDENT, chan, mpeg12_channel_mode_table);
336 print_field("%*cMedia Payload Format: %s", BASE_INDENT, ' ',
337 mpf ? "RFC-2250 RFC-3119" : "RFC-2250");
339 print_field("%*cFrequency: 0x%02x", BASE_INDENT, ' ', freq);
340 print_value_bits(BASE_INDENT, freq, mpeg12_frequency_table);
343 print_field("%*cBitrate Index: 0x%04x", BASE_INDENT, ' ',
345 print_value_bits(BASE_INDENT, freq, mpeg12_bitrate_table);
348 print_field("%*cVBR: %s", BASE_INDENT, ' ', vbr ? "Yes" : "No");
353 static bool codec_mpeg12_cfg(uint8_t losc, struct l2cap_frame *frame)
365 l2cap_frame_get_be16(frame, &cap);
367 layer = (cap >> 8) & 0xe0;
369 chan = (cap >> 8) & 0x0f;
373 l2cap_frame_get_be16(frame, &cap);
376 bitrate = cap & 0x7fff;
378 print_field("%*cLayer: %s (0x%02x)", BASE_INDENT, ' ',
379 find_value_bit(layer, mpeg12_layer_table),
382 print_field("%*cCRC: %s", BASE_INDENT, ' ', crc ? "Yes" : "No");
384 print_field("%*cChannel Mode: %s (0x%02x)", BASE_INDENT, ' ',
385 find_value_bit(chan, mpeg12_channel_mode_table),
388 print_field("%*cMedia Payload Format: %s", BASE_INDENT, ' ',
389 mpf ? "RFC-2250 RFC-3119" : "RFC-2250");
391 print_field("%*cFrequency: %s (0x%02x)", BASE_INDENT, ' ',
392 find_value_bit(freq, mpeg12_frequency_table),
396 print_field("%*cBitrate Index: %s (0x%04x)", BASE_INDENT, ' ',
397 find_value_bit(freq, mpeg12_bitrate_table),
400 print_field("%*cVBR: %s", BASE_INDENT, ' ', vbr ? "Yes" : "No");
405 static bool codec_aac_cap(uint8_t losc, struct l2cap_frame *frame)
417 l2cap_frame_get_be16(frame, &cap);
422 l2cap_frame_get_be16(frame, &cap);
424 freq |= (cap >> 8) & 0xf0;
425 chan = (cap >> 8) & 0x0c;
426 bitrate = (cap << 16) & 0x7f0000;
429 l2cap_frame_get_be16(frame, &cap);
433 print_field("%*cObject Type: 0x%02x", BASE_INDENT, ' ', type);
434 print_value_bits(BASE_INDENT, type, aac_object_type_table);
436 print_field("%*cFrequency: 0x%02x", BASE_INDENT, ' ', freq);
437 print_value_bits(BASE_INDENT, freq, aac_frequency_table);
439 print_field("%*cChannels: 0x%02x", BASE_INDENT, ' ', chan);
440 print_value_bits(BASE_INDENT, chan, aac_channels_table);
442 print_field("%*cBitrate: %ubps", BASE_INDENT, ' ', bitrate);
443 print_field("%*cVBR: %s", BASE_INDENT, ' ', vbr ? "Yes" : "No");
448 static bool codec_aac_cfg(uint8_t losc, struct l2cap_frame *frame)
460 l2cap_frame_get_be16(frame, &cap);
465 l2cap_frame_get_be16(frame, &cap);
467 freq |= (cap >> 8) & 0xf0;
468 chan = (cap >> 8) & 0x0c;
469 bitrate = (cap << 16) & 0x7f0000;
472 l2cap_frame_get_be16(frame, &cap);
476 print_field("%*cObject Type: %s (0x%02x)", BASE_INDENT, ' ',
477 find_value_bit(type, aac_object_type_table), type);
479 print_field("%*cFrequency: %s (0x%02x)", BASE_INDENT, ' ',
480 find_value_bit(freq, aac_frequency_table), freq);
482 print_field("%*cChannels: %s (0x%02x)", BASE_INDENT, ' ',
483 find_value_bit(chan, aac_channels_table), chan);
485 print_field("%*cBitrate: %ubps", BASE_INDENT, ' ', bitrate);
486 print_field("%*cVBR: %s", BASE_INDENT, ' ', vbr ? "Yes" : "No");
491 static bool codec_vendor_aptx_cap(uint8_t losc, struct l2cap_frame *frame)
498 l2cap_frame_get_u8(frame, &cap);
500 print_field("%*cFrequency: 0x%02x", BASE_INDENT + 2, ' ', cap & 0xf0);
501 print_value_bits(BASE_INDENT + 2, cap & 0xf0, aptx_frequency_table);
503 print_field("%*cChannel Mode: 0x%02x", BASE_INDENT + 2, ' ',
505 print_value_bits(BASE_INDENT + 2, cap & 0x0f, aptx_channel_mode_table);
510 static bool codec_vendor_ldac(uint8_t losc, struct l2cap_frame *frame)
517 l2cap_frame_get_le16(frame, &cap);
519 print_field("%*cUnknown: 0x%04x", BASE_INDENT + 2, ' ', cap);
524 static bool codec_vendor_cap(uint8_t losc, struct l2cap_frame *frame)
526 uint32_t vendor_id = 0;
527 uint16_t codec_id = 0;
532 l2cap_frame_get_le32(frame, &vendor_id);
533 l2cap_frame_get_le16(frame, &codec_id);
537 print_field("%*cVendor ID: %s (0x%08x)", BASE_INDENT, ' ',
538 bt_compidtostr(vendor_id), vendor_id);
540 print_field("%*cVendor Specific Codec ID: %s (0x%04x)", BASE_INDENT,
541 ' ', vndcodec2str(vendor_id, codec_id), codec_id);
543 if (vendor_id == APTX_VENDOR_ID && codec_id == APTX_CODEC_ID)
544 return codec_vendor_aptx_cap(losc, frame);
545 else if (vendor_id == LDAC_VENDOR_ID && codec_id == LDAC_CODEC_ID)
546 return codec_vendor_ldac(losc, frame);
548 packet_hexdump(frame->data, losc);
549 l2cap_frame_pull(frame, frame, losc);
554 static bool codec_vendor_aptx_cfg(uint8_t losc, struct l2cap_frame *frame)
561 l2cap_frame_get_u8(frame, &cap);
563 print_field("%*cFrequency: %s (0x%02x)", BASE_INDENT + 2, ' ',
564 find_value_bit(cap & 0xf0, aptx_frequency_table),
567 print_field("%*cChannel Mode: %s (0x%02x)", BASE_INDENT + 2, ' ',
568 find_value_bit(cap & 0x0f, aptx_channel_mode_table),
574 static bool codec_vendor_cfg(uint8_t losc, struct l2cap_frame *frame)
576 uint32_t vendor_id = 0;
577 uint16_t codec_id = 0;
582 l2cap_frame_get_le32(frame, &vendor_id);
583 l2cap_frame_get_le16(frame, &codec_id);
587 print_field("%*cVendor ID: %s (0x%08x)", BASE_INDENT, ' ',
588 bt_compidtostr(vendor_id), vendor_id);
590 print_field("%*cVendor Specific Codec ID: %s (0x%04x)", BASE_INDENT,
591 ' ', vndcodec2str(vendor_id, codec_id), codec_id);
593 if (vendor_id == APTX_VENDOR_ID && codec_id == APTX_CODEC_ID)
594 return codec_vendor_aptx_cfg(losc, frame);
595 else if (vendor_id == LDAC_VENDOR_ID && codec_id == LDAC_CODEC_ID)
596 return codec_vendor_ldac(losc, frame);
598 packet_hexdump(frame->data, losc);
599 l2cap_frame_pull(frame, frame, losc);
604 bool a2dp_codec_cap(uint8_t codec, uint8_t losc, struct l2cap_frame *frame)
608 return codec_sbc_cap(losc, frame);
609 case A2DP_CODEC_MPEG12:
610 return codec_mpeg12_cap(losc, frame);
611 case A2DP_CODEC_MPEG24:
612 return codec_aac_cap(losc, frame);
613 case A2DP_CODEC_VENDOR:
614 return codec_vendor_cap(losc, frame);
616 packet_hexdump(frame->data, losc);
617 l2cap_frame_pull(frame, frame, losc);
622 bool a2dp_codec_cfg(uint8_t codec, uint8_t losc, struct l2cap_frame *frame)
626 return codec_sbc_cfg(losc, frame);
627 case A2DP_CODEC_MPEG12:
628 return codec_mpeg12_cfg(losc, frame);
629 case A2DP_CODEC_MPEG24:
630 return codec_aac_cfg(losc, frame);
631 case A2DP_CODEC_VENDOR:
632 return codec_vendor_cfg(losc, frame);
634 packet_hexdump(frame->data, losc);
635 l2cap_frame_pull(frame, frame, losc);