2 * Routines for Bluetooth SBC dissection
4 * Copyright 2012, Michal Labedzki for Tieto Corporation
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include <epan/packet.h>
16 #include <epan/expert.h>
17 #include <epan/prefs.h>
18 #include <epan/unit_strings.h>
20 #include <wsutil/array.h>
21 #include "packet-btavdtp.h"
23 #define CHANNELS_MONO 0x00
24 #define CHANNELS_DUAL_CHANNEL 0x01
25 #define CHANNELS_JOINT_STEREO 0x03
27 #define FREQUENCY_16000 0x00
28 #define FREQUENCY_32000 0x01
29 #define FREQUENCY_44100 0x02
30 #define FREQUENCY_48000 0x03
32 void proto_register_sbc(void);
36 static int hf_sbc_fragmented
;
37 static int hf_sbc_starting_packet
;
38 static int hf_sbc_last_packet
;
39 static int hf_sbc_rfa
;
40 static int hf_sbc_number_of_frames
;
42 static int hf_sbc_syncword
;
43 static int hf_sbc_sampling_frequency
;
44 static int hf_sbc_blocks
;
45 static int hf_sbc_channel_mode
;
46 static int hf_sbc_allocation_method
;
47 static int hf_sbc_subbands
;
48 static int hf_sbc_bitpool
;
49 static int hf_sbc_crc_check
;
50 static int hf_sbc_expected_data_speed
;
51 static int hf_sbc_frame_duration
;
52 static int hf_sbc_cumulative_frame_duration
;
53 static int hf_sbc_delta_time
;
54 static int hf_sbc_delta_time_from_the_beginning
;
55 static int hf_sbc_cumulative_duration
;
56 static int hf_sbc_avrcp_song_position
;
57 static int hf_sbc_diff
;
59 static int hf_sbc_data
;
62 static int ett_sbc_list
;
64 static expert_field ei_sbc_syncword
;
66 extern value_string_ext media_codec_audio_type_vals_ext
;
68 static const value_string sampling_frequency_vals
[] = {
76 static const value_string blocks_vals
[] = {
84 static const value_string channel_mode_vals
[] = {
86 { 0x01, "Dual Channel"},
88 { 0x03, "Joint Stereo"},
92 static const value_string allocation_method_vals
[] = {
98 static const value_string subbands_vals
[] = {
106 dissect_sbc(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
)
109 proto_tree
*sbc_tree
;
113 uint8_t number_of_frames
;
123 uint8_t sbc_subbands
;
127 int expected_speed_data
;
128 double frame_duration
;
129 double cumulative_frame_duration
= 0;
130 bta2dp_codec_info_t
*info
;
132 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "SBC");
134 info
= (bta2dp_codec_info_t
*) data
;
136 ti
= proto_tree_add_item(tree
, proto_sbc
, tvb
, offset
, -1, ENC_NA
);
137 sbc_tree
= proto_item_add_subtree(ti
, ett_sbc
);
139 proto_tree_add_item(sbc_tree
, hf_sbc_fragmented
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
140 proto_tree_add_item(sbc_tree
, hf_sbc_starting_packet
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
141 proto_tree_add_item(sbc_tree
, hf_sbc_last_packet
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
142 proto_tree_add_item(sbc_tree
, hf_sbc_rfa
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
143 proto_tree_add_item(sbc_tree
, hf_sbc_number_of_frames
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
144 number_of_frames
= tvb_get_uint8(tvb
, offset
) & 0x0F;
147 while (tvb_reported_length_remaining(tvb
, offset
) > 0) {
148 byte
= tvb_get_uint8(tvb
, offset
+ 1);
149 frequency
= (byte
& 0xC0) >> 6;
150 blocks
= (byte
& 0x30) >> 4;
151 channels
= (byte
& 0x0C)>> 2;
152 subbands
= byte
& 0x01;
154 bitpool
= tvb_get_uint8(tvb
, offset
+ 2);
156 if (channels
== CHANNELS_MONO
)
162 case FREQUENCY_16000
:
165 case FREQUENCY_32000
:
168 case FREQUENCY_44100
:
171 case FREQUENCY_48000
:
178 sbc_subbands
= 4 * (subbands
+ 1);
179 sbc_blocks
= 4 * (blocks
+ 1);
181 frame_length
= (4 * sbc_subbands
* sbc_channels
) / 8;
182 if (sbc_channels
== 1 || channels
== CHANNELS_DUAL_CHANNEL
)
183 val
= sbc_blocks
* sbc_channels
* bitpool
;
185 val
= (((channels
== CHANNELS_JOINT_STEREO
) ? 1 : 0) * sbc_subbands
+ sbc_blocks
* bitpool
);
187 frame_length
+= val
/ 8;
191 expected_speed_data
= (frame_length
* frequency
) / (sbc_subbands
* sbc_blocks
);
193 rtree
= proto_tree_add_subtree_format(sbc_tree
, tvb
, offset
, 4 + frame_length
,
194 ett_sbc_list
, NULL
, "Frame: %3u/%3u", counter
, number_of_frames
);
196 pitem
= proto_tree_add_item(rtree
, hf_sbc_syncword
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
197 syncword
= tvb_get_uint8(tvb
, offset
);
198 if (syncword
!= 0x9C) {
199 expert_add_info(pinfo
, pitem
, &ei_sbc_syncword
);
203 proto_tree_add_item(rtree
, hf_sbc_sampling_frequency
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
204 proto_tree_add_item(rtree
, hf_sbc_blocks
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
205 proto_tree_add_item(rtree
, hf_sbc_channel_mode
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
206 proto_tree_add_item(rtree
, hf_sbc_allocation_method
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
207 proto_tree_add_item(rtree
, hf_sbc_subbands
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
210 proto_tree_add_item(rtree
, hf_sbc_bitpool
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
213 proto_tree_add_item(rtree
, hf_sbc_crc_check
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
216 proto_tree_add_item(rtree
, hf_sbc_data
, tvb
, offset
, frame_length
, ENC_NA
);
217 offset
+= frame_length
;
219 /* TODO: expert_info for invalid CRC */
221 pitem
= proto_tree_add_uint(rtree
, hf_sbc_expected_data_speed
, tvb
, offset
, 0, expected_speed_data
/ 1024);
222 proto_item_set_generated(pitem
);
224 frame_duration
= (((double) frame_length
/ (double) expected_speed_data
) * 1000.0);
225 cumulative_frame_duration
+= frame_duration
;
227 pitem
= proto_tree_add_double(rtree
, hf_sbc_frame_duration
, tvb
, offset
, 0, frame_duration
);
228 proto_item_set_generated(pitem
);
233 pitem
= proto_tree_add_double(sbc_tree
, hf_sbc_cumulative_frame_duration
, tvb
, offset
, 0, cumulative_frame_duration
);
234 proto_item_set_generated(pitem
);
236 if (info
&& info
->configuration
&& info
->configuration_length
> 0) {
237 /* TODO: display current codec configuration */
240 if (info
&& info
->previous_media_packet_info
&& info
->current_media_packet_info
) {
243 nstime_delta(&delta
, &pinfo
->abs_ts
, &info
->previous_media_packet_info
->abs_ts
);
244 pitem
= proto_tree_add_double(sbc_tree
, hf_sbc_delta_time
, tvb
, offset
, 0, nstime_to_msec(&delta
));
245 proto_item_set_generated(pitem
);
247 pitem
= proto_tree_add_double(sbc_tree
, hf_sbc_avrcp_song_position
, tvb
, offset
, 0, info
->previous_media_packet_info
->avrcp_song_position
);
248 proto_item_set_generated(pitem
);
250 nstime_delta(&delta
, &pinfo
->abs_ts
, &info
->previous_media_packet_info
->first_abs_ts
);
251 pitem
= proto_tree_add_double(sbc_tree
, hf_sbc_delta_time_from_the_beginning
, tvb
, offset
, 0, nstime_to_msec(&delta
));
252 proto_item_set_generated(pitem
);
254 if (!pinfo
->fd
->visited
) {
255 info
->current_media_packet_info
->cumulative_frame_duration
+= cumulative_frame_duration
;
256 info
->current_media_packet_info
->avrcp_song_position
+= cumulative_frame_duration
;
259 pitem
= proto_tree_add_double(sbc_tree
, hf_sbc_cumulative_duration
, tvb
, offset
, 0, info
->previous_media_packet_info
->cumulative_frame_duration
);
260 proto_item_set_generated(pitem
);
262 pitem
= proto_tree_add_double(sbc_tree
, hf_sbc_diff
, tvb
, offset
, 0, info
->previous_media_packet_info
->cumulative_frame_duration
- nstime_to_msec(&delta
));
263 proto_item_set_generated(pitem
);
266 /* TODO: more precise dissection: blocks, channels, subbands, padding */
268 col_append_fstr(pinfo
->cinfo
, COL_INFO
, " Frames=%u", number_of_frames
);
274 proto_register_sbc(void)
277 expert_module_t
* expert_sbc
;
279 static hf_register_info hf
[] = {
280 { &hf_sbc_fragmented
,
281 { "Fragmented", "sbc.fragmented",
282 FT_BOOLEAN
, 8, NULL
, 0x80,
285 { &hf_sbc_starting_packet
,
286 { "Starting Packet", "sbc.starting_packet",
287 FT_BOOLEAN
, 8, NULL
, 0x40,
290 { &hf_sbc_last_packet
,
291 { "Last Packet", "sbc.last_packet",
292 FT_BOOLEAN
, 8, NULL
, 0x20,
297 FT_BOOLEAN
, 8, NULL
, 0x10,
300 { &hf_sbc_number_of_frames
,
301 { "Number of Frames", "sbc.number_of_frames",
302 FT_UINT8
, BASE_DEC
, NULL
, 0x0F,
306 { "Sync Word", "sbc.syncword",
307 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
310 { &hf_sbc_sampling_frequency
,
311 { "Sampling Frequency", "sbc.sampling_frequency",
312 FT_UINT8
, BASE_HEX
, VALS(sampling_frequency_vals
), 0xC0,
316 { "Blocks", "sbc.blocks",
317 FT_UINT8
, BASE_HEX
, VALS(blocks_vals
), 0x30,
320 { &hf_sbc_channel_mode
,
321 { "Channel Mode", "sbc.channel_mode",
322 FT_UINT8
, BASE_HEX
, VALS(channel_mode_vals
), 0x0C,
325 { &hf_sbc_allocation_method
,
326 { "Allocation Method", "sbc.allocation_method",
327 FT_UINT8
, BASE_HEX
, VALS(allocation_method_vals
), 0x02,
331 { "Subbands", "sbc.subbands",
332 FT_UINT8
, BASE_HEX
, VALS(subbands_vals
), 0x01,
336 { "Bitpool", "sbc.bitpool",
337 FT_UINT8
, BASE_DEC
, NULL
, 0x00,
341 { "CRC Check", "sbc.crc_check",
342 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
345 { &hf_sbc_expected_data_speed
,
346 { "Expected data speed", "sbc.expected_data_speed",
347 FT_UINT32
, BASE_DEC
|BASE_UNIT_STRING
, UNS(&units_kibps
), 0x00,
350 { &hf_sbc_frame_duration
,
351 { "Frame Duration", "sbc.frame_duration",
352 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
355 { &hf_sbc_cumulative_frame_duration
,
356 { "Cumulative Frame Duration", "sbc.cumulative_frame_duration",
357 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
360 { &hf_sbc_delta_time
,
361 { "Delta time", "sbc.delta_time",
362 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
365 { &hf_sbc_delta_time_from_the_beginning
,
366 { "Delta time from the beginning", "sbc.delta_time_from_the_beginning",
367 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
370 { &hf_sbc_cumulative_duration
,
371 { "Cumulative Music Duration", "sbc.cumulative_music_duration",
372 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
375 { &hf_sbc_avrcp_song_position
,
376 { "AVRCP Song Position", "sbc.avrcp_song_position",
377 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
381 { "Diff", "sbc.diff",
382 FT_DOUBLE
, BASE_NONE
|BASE_UNIT_STRING
, UNS(&units_milliseconds
), 0x00,
386 { "Frame Data", "sbc.data",
387 FT_NONE
, BASE_NONE
, NULL
, 0x00,
392 static int *ett
[] = {
397 static ei_register_info ei
[] = {
398 { &ei_sbc_syncword
, { "sbc.syncword.unexpected", PI_PROTOCOL
, PI_WARN
, "Unexpected syncword", EXPFILL
}},
401 proto_sbc
= proto_register_protocol("Bluetooth SBC Codec", "SBC", "sbc");
403 proto_register_field_array(proto_sbc
, hf
, array_length(hf
));
404 proto_register_subtree_array(ett
, array_length(ett
));
405 expert_sbc
= expert_register_protocol(proto_sbc
);
406 expert_register_field_array(expert_sbc
, ei
, array_length(ei
));
408 register_dissector("sbc", dissect_sbc
, proto_sbc
);
410 module
= prefs_register_protocol_subtree("Bluetooth", proto_sbc
, NULL
);
411 prefs_register_static_text_preference(module
, "a2dp.version",
412 "Bluetooth Audio Codec SBC version based on A2DP 1.3",
413 "Version of codec supported by this dissector.");
417 * Editor modelines - https://www.wireshark.org/tools/modelines.html
422 * indent-tabs-mode: nil
425 * vi: set shiftwidth=4 tabstop=8 expandtab:
426 * :indentSize=4:tabSize=8:noTabs=true: