summaryrefslogtreecommitdiff
path: root/monitor/a2dp.c
blob: 902823863efdece56023a98fc88423d70db5104b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2015  Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lib/bluetooth.h"

#include "src/shared/util.h"
#include "bt.h"
#include "packet.h"
#include "display.h"
#include "l2cap.h"
#include "a2dp.h"

#define BASE_INDENT	4

/* Codec Types */
#define A2DP_CODEC_SBC		0x00
#define A2DP_CODEC_MPEG12	0x01
#define A2DP_CODEC_MPEG24	0x02
#define A2DP_CODEC_ATRAC	0x04
#define A2DP_CODEC_VENDOR	0xff

struct bit_desc {
	uint8_t bit_num;
	const char *str;
};

static const struct bit_desc sbc_frequency_table[] = {
	{  7, "16000" },
	{  6, "32000" },
	{  5, "44100" },
	{  4, "48000" },
	{ }
};

static const struct bit_desc sbc_channel_mode_table[] = {
	{  3, "Mono" },
	{  2, "Dual Channel" },
	{  1, "Stereo" },
	{  0, "Joint Stereo" },
	{ }
};

static const struct bit_desc sbc_blocklen_table[] = {
	{  7, "4" },
	{  6, "8" },
	{  5, "12" },
	{  4, "16" },
	{ }
};

static const struct bit_desc sbc_subbands_table[] = {
	{  3, "4" },
	{  2, "8" },
	{ }
};

static const struct bit_desc sbc_allocation_table[] = {
	{  1, "SNR" },
	{  0, "Loudness" },
	{ }
};

static const struct bit_desc mpeg12_layer_table[] = {
	{  7, "Layer I (mp1)" },
	{  6, "Layer II (mp2)" },
	{  5, "Layer III (mp3)" },
	{ }
};

static const struct bit_desc mpeg12_channel_mode_table[] = {
	{  3, "Mono" },
	{  2, "Dual Channel" },
	{  1, "Stereo" },
	{  0, "Joint Stereo" },
	{ }
};

static const struct bit_desc mpeg12_frequency_table[] = {
	{  5, "16000" },
	{  4, "22050" },
	{  3, "24000" },
	{  2, "32000" },
	{  1, "44100" },
	{  0, "48000" },
	{ }
};

static const struct bit_desc mpeg12_bitrate_table[] = {
	{ 14, "1110" },
	{ 13, "1101" },
	{ 12, "1100" },
	{ 11, "1011" },
	{ 10, "1010" },
	{  9, "1001" },
	{  8, "1000" },
	{  7, "0111" },
	{  6, "0110" },
	{  5, "0101" },
	{  4, "0100" },
	{  3, "0011" },
	{  2, "0010" },
	{  1, "0001" },
	{  0, "0000" },
	{ }
};

static void print_value_bits(uint8_t indent, uint32_t value,
						const struct bit_desc *table)
{
	int i;

	for (i = 0; table[i].str; i++) {
		if (value & (1 << table[i].bit_num))
			print_field("%*c%s", indent + 2, ' ', table[i].str);
	}
}

static const char *find_value_bit(uint32_t value,
						const struct bit_desc *table)
{
	int i;

	for (i = 0; table[i].str; i++) {
		if (value & (1 << table[i].bit_num))
			return table[i].str;
	}

	return "Unknown";
}

static bool codec_sbc_cap(uint8_t losc, struct l2cap_frame *frame)
{
	uint8_t cap = 0;

	if (losc != 4)
		return false;

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cFrequency: 0x%02x", BASE_INDENT, ' ', cap & 0xf0);
	print_value_bits(BASE_INDENT, cap & 0xf0, sbc_frequency_table);

	print_field("%*cChannel Mode: 0x%02x", BASE_INDENT, ' ', cap & 0x0f);
	print_value_bits(BASE_INDENT, cap & 0x0f, sbc_channel_mode_table);

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cBlock Length: 0x%02x", BASE_INDENT, ' ', cap & 0xf0);
	print_value_bits(BASE_INDENT, cap & 0xf0, sbc_blocklen_table);

	print_field("%*cSubbands: 0x%02x", BASE_INDENT, ' ', cap & 0x0c);
	print_value_bits(BASE_INDENT, cap & 0x0c, sbc_subbands_table);

	print_field("%*cAllocation Method: 0x%02x", BASE_INDENT, ' ',
								cap & 0x03);
	print_value_bits(BASE_INDENT, cap & 0x03, sbc_allocation_table);

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cMinimum Bitpool: %d", BASE_INDENT, ' ', cap);

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cMaximum Bitpool: %d", BASE_INDENT, ' ', cap);

	return true;
}

static bool codec_sbc_cfg(uint8_t losc, struct l2cap_frame *frame)
{
	uint8_t cap = 0;

	if (losc != 4)
		return false;

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cFrequency: %s (0x%02x)", BASE_INDENT, ' ',
			find_value_bit(cap & 0xf0, sbc_frequency_table),
			cap & 0xf0);

	print_field("%*cChannel Mode: %s (0x%02x)", BASE_INDENT, ' ',
			find_value_bit(cap & 0x0f, sbc_channel_mode_table),
			cap & 0x0f);

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cBlock Length: %s (0x%02x)", BASE_INDENT, ' ',
			find_value_bit(cap & 0xf0, sbc_blocklen_table),
			cap & 0xf0);

	print_field("%*cSubbands: %s (0x%02x)", BASE_INDENT, ' ',
			find_value_bit(cap & 0x0c, sbc_subbands_table),
			cap & 0x0c);

	print_field("%*cAllocation Method: %s (0x%02x)", BASE_INDENT, ' ',
			find_value_bit(cap & 0x03, sbc_allocation_table),
			cap & 0x03);

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cMinimum Bitpool: %d", BASE_INDENT, ' ', cap);

	l2cap_frame_get_u8(frame, &cap);

	print_field("%*cMaximum Bitpool: %d", BASE_INDENT, ' ', cap);

	return true;
}

static bool codec_mpeg12_cap(uint8_t losc, struct l2cap_frame *frame)
{
	uint16_t cap = 0;
	uint8_t layer;
	uint8_t chan;
	uint8_t freq;
	uint16_t bitrate;
	bool crc, mpf, vbr;

	if (losc != 4)
		return false;

	l2cap_frame_get_be16(frame, &cap);

	layer = (cap >> 8) & 0xe0;
	crc = cap & 0x1000;
	chan = (cap >> 8) & 0x0f;
	mpf = cap & 0x0040;
	freq = cap & 0x003f;

	l2cap_frame_get_be16(frame, &cap);

	vbr = cap & 0x8000;
	bitrate = cap & 0x7fff;

	print_field("%*cLayer: 0x%02x", BASE_INDENT, ' ', layer);
	print_value_bits(BASE_INDENT, layer, mpeg12_layer_table);

	print_field("%*cCRC: %s", BASE_INDENT, ' ', crc ? "Yes" : "No");

	print_field("%*cChannel Mode: 0x%02x", BASE_INDENT, ' ', chan);
	print_value_bits(BASE_INDENT, chan, mpeg12_channel_mode_table);

	print_field("%*cMedia Payload Format: %s", BASE_INDENT, ' ',
					mpf ? "RFC-2250 RFC-3119" : "RFC-2250");

	print_field("%*cFrequency: 0x%02x", BASE_INDENT, ' ', freq);
	print_value_bits(BASE_INDENT, freq, mpeg12_frequency_table);

	if (!vbr) {
		print_field("%*cBitrate Index: 0x%04x", BASE_INDENT, ' ',
								bitrate);
		print_value_bits(BASE_INDENT, freq, mpeg12_bitrate_table);
	}

	print_field("%*cVBR: %s", BASE_INDENT, ' ', vbr ? "Yes" : "No");

	return true;
}

static bool codec_mpeg12_cfg(uint8_t losc, struct l2cap_frame *frame)
{
	uint16_t cap = 0;
	uint8_t layer;
	uint8_t chan;
	uint8_t freq;
	uint16_t bitrate;
	bool crc, mpf, vbr;

	if (losc != 4)
		return false;

	l2cap_frame_get_be16(frame, &cap);

	layer = (cap >> 8) & 0xe0;
	crc = cap & 0x1000;
	chan = (cap >> 8) & 0x0f;
	mpf = cap & 0x0040;
	freq = cap & 0x003f;

	l2cap_frame_get_be16(frame, &cap);

	vbr = cap & 0x8000;
	bitrate = cap & 0x7fff;

	print_field("%*cLayer: %s (0x%02x)", BASE_INDENT, ' ',
				find_value_bit(layer, mpeg12_layer_table),
				layer);

	print_field("%*cCRC: %s", BASE_INDENT, ' ', crc ? "Yes" : "No");

	print_field("%*cChannel Mode: %s (0x%02x)", BASE_INDENT, ' ',
				find_value_bit(chan, mpeg12_channel_mode_table),
				chan);

	print_field("%*cMedia Payload Format: %s", BASE_INDENT, ' ',
					mpf ? "RFC-2250 RFC-3119" : "RFC-2250");

	print_field("%*cFrequency: %s (0x%02x)", BASE_INDENT, ' ',
				find_value_bit(freq, mpeg12_frequency_table),
				freq);

	if (!vbr)
		print_field("%*cBitrate Index: %s (0x%04x)", BASE_INDENT, ' ',
				find_value_bit(freq, mpeg12_bitrate_table),
				bitrate);

	print_field("%*cVBR: %s", BASE_INDENT, ' ', vbr ? "Yes" : "No");

	return true;
}

bool a2dp_codec_cap(uint8_t codec, uint8_t losc, struct l2cap_frame *frame)
{
	switch (codec) {
	case A2DP_CODEC_SBC:
		return codec_sbc_cap(losc, frame);
	case A2DP_CODEC_MPEG12:
		return codec_mpeg12_cap(losc, frame);
	default:
		packet_hexdump(frame->data, losc);
		l2cap_frame_pull(frame, frame, losc);
		return true;
	}
}

bool a2dp_codec_cfg(uint8_t codec, uint8_t losc, struct l2cap_frame *frame)
{
	switch (codec) {
	case A2DP_CODEC_SBC:
		return codec_sbc_cfg(losc, frame);
	case A2DP_CODEC_MPEG12:
		return codec_mpeg12_cfg(losc, frame);
	default:
		packet_hexdump(frame->data, losc);
		l2cap_frame_pull(frame, frame, losc);
		return true;
	}
}