HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / dissectors / packet-sbc.c
blob091d099f65dcff28edad5ff9ddcbe8f2af732702
1 /* packet-sbc.c
2 * Routines for Bluetooth SBC dissection
4 * Copyright 2012, Michal Labedzki for Tieto Corporation
6 * $Id$
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.
27 #include "config.h"
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[] = {
68 { 0x00, "16 kHz"},
69 { 0x01, "32 kHz"},
70 { 0x02, "44.1 kHz"},
71 { 0x03, "48 kHz"},
72 { 0, NULL }
75 static const value_string blocks_vals[] = {
76 { 0x00, "4"},
77 { 0x01, "8"},
78 { 0x02, "12"},
79 { 0x03, "16"},
80 { 0, NULL }
83 static const value_string channel_mode_vals[] = {
84 { 0x00, "Mono"},
85 { 0x01, "Dual Channel"},
86 { 0x02, "Stereo"},
87 { 0x03, "Joint Stereo"},
88 { 0, NULL }
91 static const value_string allocation_method_vals[] = {
92 { 0x00, "Loudness"},
93 { 0x01, "SNR"},
94 { 0, NULL }
97 static const value_string subbands_vals[] = {
98 { 0x00, "4"},
99 { 0x01, "8"},
100 { 0, NULL }
104 static gint
105 dissect_sbc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
107 proto_item *ti;
108 proto_tree *sbc_tree;
109 proto_item *pitem;
110 proto_item *ritem;
111 proto_tree *rtree;
112 gint offset = 0;
113 guint8 number_of_frames;
114 guint8 syncword;
115 guint8 byte;
116 guint8 blocks;
117 guint8 channels;
118 guint8 subbands;
119 guint8 bitpool;
120 guint frequency;
121 guint8 sbc_blocks;
122 gint sbc_channels;
123 guint8 sbc_subbands;
124 gint val;
125 gint counter = 1;
126 gint frame_length;
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;
140 offset += 1;
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)
152 sbc_channels = 1;
153 else
154 sbc_channels = 2;
156 switch (frequency) {
157 case FREQUENCY_16000:
158 frequency = 16000;
159 break;
160 case FREQUENCY_32000:
161 frequency = 32000;
162 break;
163 case FREQUENCY_44100:
164 frequency = 44100;
165 break;
166 case FREQUENCY_48000:
167 frequency = 48000;
168 break;
169 default:
170 frequency = 0;
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;
179 else
180 val = (((channels == CHANNELS_JOINT_STEREO) ? 1 : 0) * sbc_subbands + sbc_blocks * bitpool);
182 frame_length += val / 8;
183 if (val % 8)
184 frame_length += 1;
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);
197 offset += 1;
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);
204 offset += 1;
206 proto_tree_add_item(ritem, hf_sbc_bitpool, tvb, offset, 1, ENC_BIG_ENDIAN);
207 offset += 1;
209 proto_tree_add_item(ritem, hf_sbc_crc_check, tvb, offset, 1, ENC_BIG_ENDIAN);
210 offset += 1;
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);
220 counter += 1;
223 /* TODO: more precise dissection: blocks, channels, subbands, padding */
225 col_append_fstr(pinfo->cinfo, COL_INFO, " Frames=%u", number_of_frames);
227 return offset;
230 void
231 proto_register_sbc(void)
233 module_t *module;
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,
240 NULL, HFILL }
242 { &hf_sbc_starting_packet,
243 { "Starting Packet", "sbc.starting_packet",
244 FT_BOOLEAN, 8, NULL, 0x40,
245 NULL, HFILL }
247 { &hf_sbc_last_packet,
248 { "Last Packet", "sbc.last_packet",
249 FT_BOOLEAN, 8, NULL, 0x20,
250 NULL, HFILL }
252 { &hf_sbc_rfa,
253 { "RFA", "sbc.rfa",
254 FT_BOOLEAN, 8, NULL, 0x10,
255 NULL, HFILL }
257 { &hf_sbc_number_of_frames,
258 { "Number of Frames", "sbc.number_of_frames",
259 FT_UINT8, BASE_DEC, NULL, 0x0F,
260 NULL, HFILL }
262 { &hf_sbc_syncword,
263 { "Sync Word", "sbc.syncword",
264 FT_UINT8, BASE_HEX, NULL, 0x00,
265 NULL, HFILL }
267 { &hf_sbc_sampling_frequency,
268 { "Sampling Frequency", "sbc.sampling_frequency",
269 FT_UINT8, BASE_HEX, VALS(sampling_frequency_vals), 0xC0,
270 NULL, HFILL }
272 { &hf_sbc_blocks,
273 { "Blocks", "sbc.blocks",
274 FT_UINT8, BASE_HEX, VALS(blocks_vals), 0x30,
275 NULL, HFILL }
277 { &hf_sbc_channel_mode,
278 { "Channel Mode", "sbc.channel_mode",
279 FT_UINT8, BASE_HEX, VALS(channel_mode_vals), 0x0C,
280 NULL, HFILL }
282 { &hf_sbc_allocation_method,
283 { "Allocation Method", "sbc.allocation_method",
284 FT_UINT8, BASE_HEX, VALS(allocation_method_vals), 0x02,
285 NULL, HFILL }
287 { &hf_sbc_subbands,
288 { "Subbands", "sbc.subbands",
289 FT_UINT8, BASE_HEX, VALS(subbands_vals), 0x01,
290 NULL, HFILL }
292 { &hf_sbc_bitpool,
293 { "Bitpool", "sbc.bitpool",
294 FT_UINT8, BASE_DEC, NULL, 0x00,
295 NULL, HFILL }
297 { &hf_sbc_crc_check,
298 { "CRC Check", "sbc.crc_check",
299 FT_UINT8, BASE_HEX, NULL, 0x00,
300 NULL, HFILL }
303 { &hf_sbc_data,
304 { "Data", "sbc.data",
305 FT_NONE, BASE_NONE, NULL, 0x00,
306 NULL, HFILL }
310 static gint *ett[] = {
311 &ett_sbc,
312 &ett_sbc_list,
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
337 * Local variables:
338 * c-basic-offset: 4
339 * tab-width: 8
340 * indent-tabs-mode: nil
341 * End:
343 * vi: set shiftwidth=4 tabstop=8 expandtab:
344 * :indentSize=4:tabSize=8:noTabs=true: