Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-sbc.c
blobf1b27b073a3d90560c0b5736366dd885a9b26137
1 /* packet-sbc.c
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
13 #include "config.h"
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);
34 static int proto_sbc;
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;
61 static int ett_sbc;
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[] = {
69 { 0x00, "16 kHz"},
70 { 0x01, "32 kHz"},
71 { 0x02, "44.1 kHz"},
72 { 0x03, "48 kHz"},
73 { 0, NULL }
76 static const value_string blocks_vals[] = {
77 { 0x00, "4"},
78 { 0x01, "8"},
79 { 0x02, "12"},
80 { 0x03, "16"},
81 { 0, NULL }
84 static const value_string channel_mode_vals[] = {
85 { 0x00, "Mono"},
86 { 0x01, "Dual Channel"},
87 { 0x02, "Stereo"},
88 { 0x03, "Joint Stereo"},
89 { 0, NULL }
92 static const value_string allocation_method_vals[] = {
93 { 0x00, "Loudness"},
94 { 0x01, "SNR"},
95 { 0, NULL }
98 static const value_string subbands_vals[] = {
99 { 0x00, "4"},
100 { 0x01, "8"},
101 { 0, NULL }
105 static int
106 dissect_sbc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
108 proto_item *ti;
109 proto_tree *sbc_tree;
110 proto_item *pitem;
111 proto_tree *rtree;
112 int offset = 0;
113 uint8_t number_of_frames;
114 uint8_t syncword;
115 uint8_t byte;
116 uint8_t blocks;
117 uint8_t channels;
118 uint8_t subbands;
119 uint8_t bitpool;
120 unsigned frequency;
121 uint8_t sbc_blocks;
122 int sbc_channels;
123 uint8_t sbc_subbands;
124 int val;
125 int counter = 1;
126 int frame_length;
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;
145 offset += 1;
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)
157 sbc_channels = 1;
158 else
159 sbc_channels = 2;
161 switch (frequency) {
162 case FREQUENCY_16000:
163 frequency = 16000;
164 break;
165 case FREQUENCY_32000:
166 frequency = 32000;
167 break;
168 case FREQUENCY_44100:
169 frequency = 44100;
170 break;
171 case FREQUENCY_48000:
172 frequency = 48000;
173 break;
174 default:
175 frequency = 0;
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;
184 else
185 val = (((channels == CHANNELS_JOINT_STEREO) ? 1 : 0) * sbc_subbands + sbc_blocks * bitpool);
187 frame_length += val / 8;
188 if (val % 8)
189 frame_length += 1;
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);
201 offset += 1;
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);
208 offset += 1;
210 proto_tree_add_item(rtree, hf_sbc_bitpool, tvb, offset, 1, ENC_BIG_ENDIAN);
211 offset += 1;
213 proto_tree_add_item(rtree, hf_sbc_crc_check, tvb, offset, 1, ENC_BIG_ENDIAN);
214 offset += 1;
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);
230 counter += 1;
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) {
241 nstime_t delta;
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);
270 return offset;
273 void
274 proto_register_sbc(void)
276 module_t *module;
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,
283 NULL, HFILL }
285 { &hf_sbc_starting_packet,
286 { "Starting Packet", "sbc.starting_packet",
287 FT_BOOLEAN, 8, NULL, 0x40,
288 NULL, HFILL }
290 { &hf_sbc_last_packet,
291 { "Last Packet", "sbc.last_packet",
292 FT_BOOLEAN, 8, NULL, 0x20,
293 NULL, HFILL }
295 { &hf_sbc_rfa,
296 { "RFA", "sbc.rfa",
297 FT_BOOLEAN, 8, NULL, 0x10,
298 NULL, HFILL }
300 { &hf_sbc_number_of_frames,
301 { "Number of Frames", "sbc.number_of_frames",
302 FT_UINT8, BASE_DEC, NULL, 0x0F,
303 NULL, HFILL }
305 { &hf_sbc_syncword,
306 { "Sync Word", "sbc.syncword",
307 FT_UINT8, BASE_HEX, NULL, 0x00,
308 NULL, HFILL }
310 { &hf_sbc_sampling_frequency,
311 { "Sampling Frequency", "sbc.sampling_frequency",
312 FT_UINT8, BASE_HEX, VALS(sampling_frequency_vals), 0xC0,
313 NULL, HFILL }
315 { &hf_sbc_blocks,
316 { "Blocks", "sbc.blocks",
317 FT_UINT8, BASE_HEX, VALS(blocks_vals), 0x30,
318 NULL, HFILL }
320 { &hf_sbc_channel_mode,
321 { "Channel Mode", "sbc.channel_mode",
322 FT_UINT8, BASE_HEX, VALS(channel_mode_vals), 0x0C,
323 NULL, HFILL }
325 { &hf_sbc_allocation_method,
326 { "Allocation Method", "sbc.allocation_method",
327 FT_UINT8, BASE_HEX, VALS(allocation_method_vals), 0x02,
328 NULL, HFILL }
330 { &hf_sbc_subbands,
331 { "Subbands", "sbc.subbands",
332 FT_UINT8, BASE_HEX, VALS(subbands_vals), 0x01,
333 NULL, HFILL }
335 { &hf_sbc_bitpool,
336 { "Bitpool", "sbc.bitpool",
337 FT_UINT8, BASE_DEC, NULL, 0x00,
338 NULL, HFILL }
340 { &hf_sbc_crc_check,
341 { "CRC Check", "sbc.crc_check",
342 FT_UINT8, BASE_HEX, NULL, 0x00,
343 NULL, HFILL }
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,
348 NULL, HFILL }
350 { &hf_sbc_frame_duration,
351 { "Frame Duration", "sbc.frame_duration",
352 FT_DOUBLE, BASE_NONE|BASE_UNIT_STRING, UNS(&units_milliseconds), 0x00,
353 NULL, HFILL }
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,
358 NULL, HFILL }
360 { &hf_sbc_delta_time,
361 { "Delta time", "sbc.delta_time",
362 FT_DOUBLE, BASE_NONE|BASE_UNIT_STRING, UNS(&units_milliseconds), 0x00,
363 NULL, HFILL }
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,
368 NULL, HFILL }
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,
373 NULL, HFILL }
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,
378 NULL, HFILL }
380 { &hf_sbc_diff,
381 { "Diff", "sbc.diff",
382 FT_DOUBLE, BASE_NONE|BASE_UNIT_STRING, UNS(&units_milliseconds), 0x00,
383 NULL, HFILL }
385 { &hf_sbc_data,
386 { "Frame Data", "sbc.data",
387 FT_NONE, BASE_NONE, NULL, 0x00,
388 NULL, HFILL }
392 static int *ett[] = {
393 &ett_sbc,
394 &ett_sbc_list,
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
419 * Local variables:
420 * c-basic-offset: 4
421 * tab-width: 8
422 * indent-tabs-mode: nil
423 * End:
425 * vi: set shiftwidth=4 tabstop=8 expandtab:
426 * :indentSize=4:tabSize=8:noTabs=true: