2 * Routines for Bluetooth SBC dissection
4 * Copyright 2012, Michal Labedzki for Tieto Corporation
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/packet.h>
30 #include <epan/expert.h>
31 #include <epan/prefs.h>
33 #define CHANNELS_MONO 0x00
34 #define CHANNELS_JOINT_STEREO 0x03
36 #define FREQUENCY_16000 0x00
37 #define FREQUENCY_32000 0x01
38 #define FREQUENCY_44100 0x02
39 #define FREQUENCY_48000 0x03
41 static int proto_sbc
= -1;
43 static int hf_sbc_fragmented
= -1;
44 static int hf_sbc_starting_packet
= -1;
45 static int hf_sbc_last_packet
= -1;
46 static int hf_sbc_rfa
= -1;
47 static int hf_sbc_number_of_frames
= -1;
49 static int hf_sbc_syncword
= -1;
50 static int hf_sbc_sampling_frequency
= -1;
51 static int hf_sbc_blocks
= -1;
52 static int hf_sbc_channel_mode
= -1;
53 static int hf_sbc_allocation_method
= -1;
54 static int hf_sbc_subbands
= -1;
55 static int hf_sbc_bitpool
= -1;
56 static int hf_sbc_crc_check
= -1;
58 static int hf_sbc_data
= -1;
60 static gint ett_sbc
= -1;
61 static gint ett_sbc_list
= -1;
63 static expert_field ei_sbc_syncword
= EI_INIT
;
65 extern value_string_ext media_codec_audio_type_vals_ext
;
67 static const value_string sampling_frequency_vals
[] = {
75 static const value_string blocks_vals
[] = {
83 static const value_string channel_mode_vals
[] = {
85 { 0x01, "Dual Channel"},
87 { 0x03, "Joint Stereo"},
91 static const value_string allocation_method_vals
[] = {
97 static const value_string subbands_vals
[] = {
105 dissect_sbc(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
)
108 proto_tree
*sbc_tree
;
113 guint8 number_of_frames
;
127 gint expected_speed_data
;
129 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "SBC");
131 ti
= proto_tree_add_item(tree
, proto_sbc
, tvb
, offset
, -1, ENC_NA
);
132 sbc_tree
= proto_item_add_subtree(ti
, ett_sbc
);
134 proto_tree_add_item(sbc_tree
, hf_sbc_fragmented
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
135 proto_tree_add_item(sbc_tree
, hf_sbc_starting_packet
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
136 proto_tree_add_item(sbc_tree
, hf_sbc_last_packet
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
137 proto_tree_add_item(sbc_tree
, hf_sbc_rfa
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
138 proto_tree_add_item(sbc_tree
, hf_sbc_number_of_frames
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
139 number_of_frames
= tvb_get_guint8(tvb
, offset
) & 0x0F;
142 while (tvb_length_remaining(tvb
, offset
) > 0) {
143 byte
= tvb_get_guint8(tvb
, offset
+ 1);
144 frequency
= (byte
& 0xC0) >> 6;
145 blocks
= (byte
& 0x30) >> 4;
146 channels
= (byte
& 0x0C)>> 2;
147 subbands
= byte
& 0x01;
149 bitpool
= tvb_get_guint8(tvb
, offset
+ 2);
151 if (channels
== CHANNELS_MONO
)
157 case FREQUENCY_16000
:
160 case FREQUENCY_32000
:
163 case FREQUENCY_44100
:
166 case FREQUENCY_48000
:
173 sbc_subbands
= 4 * (subbands
+ 1);
174 sbc_blocks
= 4 * (blocks
+ 1);
176 frame_length
= (4 * sbc_subbands
* sbc_channels
) / 8;
177 if (sbc_channels
== 1)
178 val
= sbc_blocks
* sbc_channels
* bitpool
;
180 val
= (((channels
== CHANNELS_JOINT_STEREO
) ? 1 : 0) * sbc_subbands
+ sbc_blocks
* bitpool
);
182 frame_length
+= val
/ 8;
186 expected_speed_data
= (frame_length
* frequency
) / (sbc_subbands
* sbc_blocks
);
188 ritem
= proto_tree_add_text(sbc_tree
, tvb
, offset
, 4 + frame_length
,
189 "Frame: %3u/%3u", counter
, number_of_frames
);
190 rtree
= proto_item_add_subtree(ritem
, ett_sbc_list
);
192 pitem
= proto_tree_add_item(rtree
, hf_sbc_syncword
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
193 syncword
= tvb_get_guint8(tvb
, offset
);
194 if (syncword
!= 0x9C) {
195 expert_add_info(pinfo
, pitem
, &ei_sbc_syncword
);
199 proto_tree_add_item(ritem
, hf_sbc_sampling_frequency
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
200 proto_tree_add_item(ritem
, hf_sbc_blocks
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
201 proto_tree_add_item(ritem
, hf_sbc_channel_mode
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
202 proto_tree_add_item(ritem
, hf_sbc_allocation_method
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
203 proto_tree_add_item(ritem
, hf_sbc_subbands
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
206 proto_tree_add_item(ritem
, hf_sbc_bitpool
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
209 proto_tree_add_item(ritem
, hf_sbc_crc_check
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
212 proto_tree_add_item(ritem
, hf_sbc_data
, tvb
, offset
, frame_length
, ENC_BIG_ENDIAN
);
213 offset
+= frame_length
;
215 /* TODO: expert_info for invalid CRC*/
217 pitem
= proto_tree_add_text(ritem
, tvb
, offset
, 0, "Expected speed data: %u KiB/s", expected_speed_data
/ 1024);
218 PROTO_ITEM_SET_GENERATED(pitem
);
223 /* TODO: more precise dissection: blocks, channels, subbands, padding */
225 col_append_fstr(pinfo
->cinfo
, COL_INFO
, " Frames=%u", number_of_frames
);
231 proto_register_sbc(void)
234 expert_module_t
* expert_sbc
;
236 static hf_register_info hf
[] = {
237 { &hf_sbc_fragmented
,
238 { "Fragmented", "sbc.fragmented",
239 FT_BOOLEAN
, 8, NULL
, 0x80,
242 { &hf_sbc_starting_packet
,
243 { "Starting Packet", "sbc.starting_packet",
244 FT_BOOLEAN
, 8, NULL
, 0x40,
247 { &hf_sbc_last_packet
,
248 { "Last Packet", "sbc.last_packet",
249 FT_BOOLEAN
, 8, NULL
, 0x20,
254 FT_BOOLEAN
, 8, NULL
, 0x10,
257 { &hf_sbc_number_of_frames
,
258 { "Number of Frames", "sbc.number_of_frames",
259 FT_UINT8
, BASE_DEC
, NULL
, 0x0F,
263 { "Sync Word", "sbc.syncword",
264 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
267 { &hf_sbc_sampling_frequency
,
268 { "Sampling Frequency", "sbc.sampling_frequency",
269 FT_UINT8
, BASE_HEX
, VALS(sampling_frequency_vals
), 0xC0,
273 { "Blocks", "sbc.blocks",
274 FT_UINT8
, BASE_HEX
, VALS(blocks_vals
), 0x30,
277 { &hf_sbc_channel_mode
,
278 { "Channel Mode", "sbc.channel_mode",
279 FT_UINT8
, BASE_HEX
, VALS(channel_mode_vals
), 0x0C,
282 { &hf_sbc_allocation_method
,
283 { "Allocation Method", "sbc.allocation_method",
284 FT_UINT8
, BASE_HEX
, VALS(allocation_method_vals
), 0x02,
288 { "Subbands", "sbc.subbands",
289 FT_UINT8
, BASE_HEX
, VALS(subbands_vals
), 0x01,
293 { "Bitpool", "sbc.bitpool",
294 FT_UINT8
, BASE_DEC
, NULL
, 0x00,
298 { "CRC Check", "sbc.crc_check",
299 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
304 { "Data", "sbc.data",
305 FT_NONE
, BASE_NONE
, NULL
, 0x00,
310 static gint
*ett
[] = {
315 static ei_register_info ei
[] = {
316 { &ei_sbc_syncword
, { "sbc.syncword.unexpected", PI_PROTOCOL
, PI_WARN
, "Unexpected syncword", EXPFILL
}},
319 proto_sbc
= proto_register_protocol("Bluetooth SBC Codec", "SBC", "sbc");
321 proto_register_field_array(proto_sbc
, hf
, array_length(hf
));
322 proto_register_subtree_array(ett
, array_length(ett
));
323 expert_sbc
= expert_register_protocol(proto_sbc
);
324 expert_register_field_array(expert_sbc
, ei
, array_length(ei
));
326 new_register_dissector("sbc", dissect_sbc
, proto_sbc
);
328 module
= prefs_register_protocol(proto_sbc
, NULL
);
329 prefs_register_static_text_preference(module
, "a2dp.version",
330 "Bluetooth Audio Codec SBC version based on A2DP 1.3",
331 "Version of codec supported by this dissector.");
335 * Editor modelines - http://www.wireshark.org/tools/modelines.html
340 * indent-tabs-mode: nil
343 * vi: set shiftwidth=4 tabstop=8 expandtab:
344 * :indentSize=4:tabSize=8:noTabs=true: