epan/dissectors/pidl/ C99 drsuapi
[wireshark-sm.git] / epan / dissectors / packet-btmcap.c
blob002121c6d06403cb72ef0295822e5e1e743ddaaf
1 /* packet-btmcap.c
2 * Routines for Bluetooth MCAP dissection
3 * https://www.bluetooth.org/Technical/Specifications/adopted.htm
5 * Copyright 2013, Michal Labedzki for Tieto Corporation
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "config.h"
16 #include <epan/packet.h>
17 #include <epan/prefs.h>
18 #include <epan/expert.h>
19 #include <epan/unit_strings.h>
21 static int proto_btmcap;
23 static int hf_btmcap_op_code;
24 static int hf_btmcap_response_code;
25 static int hf_btmcap_mdl_id;
26 static int hf_btmcap_mdep_id;
27 static int hf_btmcap_response_parameters;
28 static int hf_btmcap_configuration;
29 static int hf_btmcap_timestamp_required_accuracy;
30 static int hf_btmcap_timestamp_update_information;
31 static int hf_btmcap_bluetooth_clock_sync_time;
32 static int hf_btmcap_timestamp_sync_time;
33 static int hf_btmcap_timestamp_sample_accuracy;
34 static int hf_btmcap_bluetooth_clock_access_resolution;
35 static int hf_btmcap_sync_lead_time;
36 static int hf_btmcap_timestamp_native_resolution;
37 static int hf_btmcap_timestamp_native_accuracy;
38 static int hf_btmcap_data;
40 static int ett_btmcap;
42 static expert_field ei_btmcap_mdl_id_ffff;
43 static expert_field ei_btmcap_response_parameters_bad;
44 static expert_field ei_btmcap_unexpected_data;
46 static dissector_handle_t btmcap_handle;
48 static const value_string op_code_vals[] = {
49 { 0x00, "ERROR_RSP" },
50 { 0x01, "MD_CREATE_MDL_REQ" },
51 { 0x02, "MD_CREATE_MDL_RSP" },
52 { 0x03, "MD_RECONNECT_MDL_REQ" },
53 { 0x04, "MD_RECONNECT_MDL_RSP" },
54 { 0x05, "MD_ABORT_MDL_REQ" },
55 { 0x06, "MD_ABORT_MDL_RSP" },
56 { 0x07, "MD_DELETE_MDL_REQ" },
57 { 0x08, "MD_DELETE_MDL_RSP" },
58 { 0x11, "MD_SYNC_CAP_REQ" },
59 { 0x12, "MD_SYNC_CAP_RSP" },
60 { 0x13, "MD_SYNC_SET_REQ" },
61 { 0x14, "MD_SYNC_SET_RSP" },
62 { 0x15, "MD_SYNC_INFO_IND" },
63 { 0x16, "Reserved as pseudoresponse" },
64 { 0, NULL }
67 static const value_string response_code_vals[] = {
68 { 0x00, "Success" },
69 { 0x01, "Invalid Op Code" },
70 { 0x02, "Invalid Parameter Value" },
71 { 0x03, "Invalid MDEP" },
72 { 0x04, "MDEP Busy" },
73 { 0x05, "Invalid MDL" },
74 { 0x06, "MDL Busy" },
75 { 0x07, "Invalid Operation" },
76 { 0x08, "Resource Unavailable" },
77 { 0x09, "Unspecified Error" },
78 { 0x0A, "Request Not Supported" },
79 { 0x0B, "Configuration Rejected" },
80 { 0, NULL }
84 void proto_register_btmcap(void);
85 void proto_reg_handoff_btmcap(void);
87 static int
88 dissect_btmcap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
90 proto_item *main_item;
91 proto_tree *main_tree;
92 proto_item *pitem;
93 int offset = 0;
94 uint32_t op_code;
95 uint32_t response_code;
96 uint32_t mdl_id;
97 uint32_t mdep_id;
98 uint32_t bluetooth_clock_sync_time;
99 uint64_t timestamp_sync_time;
101 main_item = proto_tree_add_item(tree, proto_btmcap, tvb, offset, tvb_captured_length(tvb), ENC_NA);
102 main_tree = proto_item_add_subtree(main_item, ett_btmcap);
104 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MCAP");
106 switch (pinfo->p2p_dir) {
107 case P2P_DIR_SENT:
108 col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
109 break;
110 case P2P_DIR_RECV:
111 col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
112 break;
113 default:
114 col_set_str(pinfo->cinfo, COL_INFO, "UnknownDirection ");
115 break;
118 pitem = proto_tree_add_item(main_tree, hf_btmcap_op_code, tvb, offset, 1, ENC_BIG_ENDIAN);
119 op_code = tvb_get_uint8(tvb, offset);
120 offset += 1;
122 col_append_str(pinfo->cinfo, COL_INFO, val_to_str_const(op_code, op_code_vals, "Unknown Op Code"));
123 if (op_code >= 0x11 && op_code <= 0x20) {
124 proto_item_append_text(pitem, " (Clock Sync)");
125 col_append_str(pinfo->cinfo, COL_INFO, " (Clock Sync)");
126 } else {
127 proto_item_append_text(pitem, " (Standard)");
128 col_append_str(pinfo->cinfo, COL_INFO, " (Standard)");
131 if (op_code & 0x01) {
132 /* isRequest */
133 switch(op_code) {
134 case 0x01: /* MD_CREATE_MDL_REQ */
135 case 0x03: /* MD_RECONNECT_MDL_REQ */
136 case 0x05: /* MD_ABORT_MDL_REQ */
137 case 0x07: /* MD_DELETE_MDL_REQ */
138 pitem = proto_tree_add_item(main_tree, hf_btmcap_mdl_id, tvb, offset, 2, ENC_BIG_ENDIAN);
139 mdl_id = tvb_get_ntohs(tvb, offset);
140 offset += 2;
142 col_append_fstr(pinfo->cinfo, COL_INFO, " - MDL ID: %u", mdl_id);
143 if (mdl_id == 0xFFFF) {
144 proto_item_append_text(pitem, " (Indicates all MDLs)");
145 col_append_str(pinfo->cinfo, COL_INFO, " (Indicates all MDLs)");
146 } else if (mdl_id >= 0x0001 && mdl_id <= 0xFEFF) {
147 proto_item_append_text(pitem, " (Dynamic Range)");
148 col_append_str(pinfo->cinfo, COL_INFO, " (Dynamic Range)");
149 } else if (mdl_id == 0x0000) {
150 proto_item_append_text(pitem, " (Reserved)");
151 col_append_str(pinfo->cinfo, COL_INFO, " (Reserved)");
154 if (op_code != 0x07 && mdl_id == 0xFFFF) {
155 expert_add_info(pinfo, pitem, &ei_btmcap_mdl_id_ffff);
158 if (op_code == 0x01) {
159 /* only MD_CREATE_MDL_REQ */
160 pitem = proto_tree_add_item(main_tree, hf_btmcap_mdep_id, tvb, offset, 1, ENC_BIG_ENDIAN);
161 mdep_id = tvb_get_uint8(tvb, offset);
162 offset += 1;
164 if (mdep_id <= 0x7F) {
165 proto_item_append_text(pitem, " (Available for use)");
166 } else {
167 proto_item_append_text(pitem, " (Reserved)");
170 proto_tree_add_item(main_tree, hf_btmcap_configuration, tvb, offset, 1, ENC_BIG_ENDIAN);
171 offset += 1;
173 break;
174 case 0x11: /* MD_SYNC_CAP_REQ */
175 proto_tree_add_item(main_tree, hf_btmcap_timestamp_required_accuracy, tvb, offset, 2, ENC_BIG_ENDIAN);
176 offset += 2;
177 break;
178 case 0x13: /* MD_SYNC_SET_REQ */
179 proto_tree_add_item(main_tree, hf_btmcap_timestamp_update_information, tvb, offset, 1, ENC_BIG_ENDIAN);
180 offset += 1;
182 pitem = proto_tree_add_item(main_tree, hf_btmcap_bluetooth_clock_sync_time, tvb, offset, 4, ENC_BIG_ENDIAN);
183 bluetooth_clock_sync_time = tvb_get_ntohl(tvb, offset);
184 if (bluetooth_clock_sync_time == 0xFFFFFFFF)
185 proto_item_append_text(pitem, " (Instant Synchronization)");
186 else
187 proto_item_append_text(pitem, " (Baseband Half-Slot Instant)");
188 offset += 4;
190 pitem = proto_tree_add_item(main_tree, hf_btmcap_timestamp_sync_time, tvb, offset, 8, ENC_BIG_ENDIAN);
191 timestamp_sync_time = tvb_get_ntoh64(tvb, offset);
192 if (timestamp_sync_time == UINT64_C(0xFFFFFFFFFFFFFFFF))
193 proto_item_append_text(pitem, " (No Time Synchronization)");
194 else
195 proto_item_append_text(pitem, " (Time-Stamp Clock Instant)");
196 offset += 8;
197 break;
198 case 0x15: /* MD_SYNC_INFO_IND */
199 pitem = proto_tree_add_item(main_tree, hf_btmcap_bluetooth_clock_sync_time, tvb, offset, 4, ENC_BIG_ENDIAN);
200 proto_item_append_text(pitem, " (Baseband Half-Slot Instant)");
201 offset += 4;
203 pitem = proto_tree_add_item(main_tree, hf_btmcap_timestamp_sync_time, tvb, offset, 8, ENC_BIG_ENDIAN);
204 proto_item_append_text(pitem, " (Time-Stamp Clock Instant)");
205 offset += 8;
207 proto_tree_add_item(main_tree, hf_btmcap_timestamp_sample_accuracy, tvb, offset, 2, ENC_BIG_ENDIAN);
208 offset += 2;
209 break;
211 } else {
212 /* isResponse */
214 proto_tree_add_item(main_tree, hf_btmcap_response_code, tvb, offset, 1, ENC_BIG_ENDIAN);
215 response_code = tvb_get_uint8(tvb, offset);
216 offset += 1;
218 col_append_fstr(pinfo->cinfo, COL_INFO, " - %s", val_to_str_const(response_code, response_code_vals, "Unknown ResponseCode"));
220 if (op_code >= 0x11 && op_code <= 0x20) {
221 /* Clock Sync */
222 switch(op_code) {
223 case 0x12: /* MD_SYNC_CAP_RSP */
224 pitem = proto_tree_add_item(main_tree, hf_btmcap_bluetooth_clock_access_resolution, tvb, offset, 1, ENC_BIG_ENDIAN);
225 proto_item_append_text(pitem, " (Baseband half-slots)");
226 offset += 1;
228 proto_tree_add_item(main_tree, hf_btmcap_sync_lead_time, tvb, offset, 2, ENC_BIG_ENDIAN);
229 offset += 2;
231 proto_tree_add_item(main_tree, hf_btmcap_timestamp_native_resolution, tvb, offset, 2, ENC_BIG_ENDIAN);
232 offset += 2;
234 proto_tree_add_item(main_tree, hf_btmcap_timestamp_native_accuracy, tvb, offset, 2, ENC_BIG_ENDIAN);
235 offset += 2;
236 break;
237 case 0x14: /* MD_SYNC_SET_RSP */
238 pitem = proto_tree_add_item(main_tree, hf_btmcap_bluetooth_clock_sync_time, tvb, offset, 4, ENC_BIG_ENDIAN);
239 bluetooth_clock_sync_time = tvb_get_ntohl(tvb, offset);
240 if (bluetooth_clock_sync_time == 0xFFFFFFFF)
241 proto_item_append_text(pitem, " (Instant Synchronization)");
242 else
243 proto_item_append_text(pitem, " (Baseband Half-Slot Instant)");
244 offset += 4;
246 pitem = proto_tree_add_item(main_tree, hf_btmcap_timestamp_sync_time, tvb, offset, 8, ENC_BIG_ENDIAN);
247 timestamp_sync_time = tvb_get_ntoh64(tvb, offset);
248 if (timestamp_sync_time == UINT64_C(0xFFFFFFFFFFFFFFFF))
249 proto_item_append_text(pitem, " (No Time Synchronization)");
250 else
251 proto_item_append_text(pitem, " (Time-Stamp Clock Instant)");
252 offset += 8;
254 proto_tree_add_item(main_tree, hf_btmcap_timestamp_sample_accuracy, tvb, offset, 2, ENC_BIG_ENDIAN);
255 offset += 2;
256 break;
258 } else {
259 /* Standard Op Code */
260 pitem = proto_tree_add_item(main_tree, hf_btmcap_mdl_id, tvb, offset, 2, ENC_BIG_ENDIAN);
261 mdl_id = tvb_get_ntohs(tvb, offset);
262 offset += 2;
264 col_append_fstr(pinfo->cinfo, COL_INFO, " - %u", mdl_id);
265 if (mdl_id == 0xFFFF) {
266 proto_item_append_text(pitem, " (Indicates all MDLs)");
267 col_append_str(pinfo->cinfo, COL_INFO, " (Indicates all MDLs)");
268 } else if (mdl_id >= 0x0001 && mdl_id <= 0xFEFF) {
269 proto_item_append_text(pitem, " (Dynamic Range)");
270 col_append_str(pinfo->cinfo, COL_INFO, " (Dynamic Range)");
271 } else if (mdl_id == 0x0000) {
272 proto_item_append_text(pitem, " (Reserved)");
273 col_append_str(pinfo->cinfo, COL_INFO, " (Reserved)");
276 if ((op_code == 0x03 || op_code == 0x05 || op_code == 0x07) && tvb_reported_length_remaining(tvb, offset)) {
277 expert_add_info_format(pinfo, pitem, &ei_btmcap_response_parameters_bad,
278 "The Response Parameters for MD_RECONNECT_MDL_RSP shall have length zero.");
279 } else if (tvb_reported_length_remaining(tvb, offset)) {
280 pitem = proto_tree_add_item(main_tree, hf_btmcap_response_parameters, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_NA);
281 if (response_code != 0x00) {
282 expert_add_info_format(pinfo, pitem, &ei_btmcap_response_parameters_bad,
283 "When the Response Code is not Success, the Response Parameters shall have length zero.");
285 offset += tvb_reported_length_remaining(tvb, offset);
290 if (tvb_reported_length_remaining(tvb, offset)) {
291 pitem = proto_tree_add_item(main_tree, hf_btmcap_data, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_NA);
292 expert_add_info(pinfo, pitem, &ei_btmcap_unexpected_data);
293 offset = tvb_reported_length(tvb);
296 return offset;
300 void
301 proto_register_btmcap(void)
303 module_t *module;
304 expert_module_t *expert_btmcap;
306 static hf_register_info hf[] = {
307 { &hf_btmcap_op_code,
308 { "Op Code", "btmcap.op_code",
309 FT_UINT8, BASE_HEX, VALS(op_code_vals), 0x0,
310 NULL, HFILL }
312 { &hf_btmcap_response_code,
313 { "Response Code", "btmcap.response_code",
314 FT_UINT8, BASE_HEX, VALS(response_code_vals), 0x0,
315 NULL, HFILL }
317 { &hf_btmcap_mdl_id,
318 { "MDL ID", "btmcap.mdl_id",
319 FT_UINT16, BASE_HEX, NULL, 0x0,
320 NULL, HFILL }
322 { &hf_btmcap_mdep_id,
323 { "MDEP ID", "btmcap.mdep_id",
324 FT_UINT8, BASE_HEX, NULL, 0x0,
325 NULL, HFILL }
327 { &hf_btmcap_configuration,
328 { "Configuration", "btmcap.configuration",
329 FT_UINT8, BASE_HEX, NULL, 0x0,
330 NULL, HFILL }
332 { &hf_btmcap_timestamp_required_accuracy,
333 { "Timestamp Required Accuracy", "btmcap.timestamp_required_accuracy",
334 FT_UINT16, BASE_DEC|BASE_UNIT_STRING, UNS(&units_ppm), 0x00,
335 NULL, HFILL }
337 { &hf_btmcap_timestamp_update_information,
338 { "Timestamp Update Information", "btmcap.timestamp_update_information",
339 FT_UINT8, BASE_DEC, NULL, 0x00,
340 NULL, HFILL }
342 { &hf_btmcap_bluetooth_clock_sync_time,
343 { "Bluetooth Clock Sync Time", "btmcap.bluetooth_clock_sync_time",
344 FT_UINT32, BASE_DEC, NULL, 0x00,
345 NULL, HFILL }
347 { &hf_btmcap_timestamp_sync_time,
348 { "Timestamp Sync Time", "btmcap.timestamp_sync_time",
349 FT_UINT64, BASE_DEC, NULL, 0x00,
350 NULL, HFILL }
352 { &hf_btmcap_timestamp_sample_accuracy,
353 { "Timestamp Sample Accuracy", "btmcap.timestamp_sample_accuracy",
354 FT_UINT16, BASE_DEC|BASE_UNIT_STRING, UNS(&units_microseconds), 0x00,
355 NULL, HFILL }
357 { &hf_btmcap_bluetooth_clock_access_resolution,
358 { "Bluetooth Clock Access Resolution","btmcap.bluetooth_clock_access_resolution",
359 FT_UINT8, BASE_DEC, NULL, 0x00,
360 NULL, HFILL }
362 { &hf_btmcap_sync_lead_time,
363 { "Sync Lead Time", "btmcap.sync_lead_time",
364 FT_UINT16, BASE_DEC|BASE_UNIT_STRING, UNS(&units_milliseconds), 0x00,
365 NULL, HFILL }
367 { &hf_btmcap_timestamp_native_resolution,
368 { "Timestamp Native Resolution", "btmcap.timestamp_native_resolution",
369 FT_UINT16, BASE_DEC|BASE_UNIT_STRING, UNS(&units_microseconds), 0x00,
370 NULL, HFILL }
372 { &hf_btmcap_timestamp_native_accuracy,
373 { "Timestamp Native Accuracy", "btmcap.timestamp_native_accuracy",
374 FT_UINT16, BASE_DEC|BASE_UNIT_STRING, UNS(&units_ppm), 0x00,
375 NULL, HFILL }
377 { &hf_btmcap_response_parameters,
378 { "Response Parameters", "btmcap.response_parameters",
379 FT_BYTES, BASE_NONE, NULL, 0x00,
380 NULL, HFILL }
383 { &hf_btmcap_data,
384 { "Data", "btmcap.data",
385 FT_NONE, BASE_NONE, NULL, 0x00,
386 NULL, HFILL }
391 static int *ett[] = {
392 &ett_btmcap
395 static ei_register_info ei[] = {
396 { &ei_btmcap_mdl_id_ffff, { "btmcap.mdl_id.ffff", PI_PROTOCOL, PI_WARN, "The value 0xFFFF is not a valid MDL ID for this request and shall not be used.", EXPFILL }},
397 { &ei_btmcap_response_parameters_bad, { "btmcap.response_parameters.bad", PI_PROTOCOL, PI_WARN, "Response parameters bad", EXPFILL }},
398 { &ei_btmcap_unexpected_data, { "btmcap.unexpected_data", PI_PROTOCOL, PI_WARN, "Unexpected data", EXPFILL }},
401 proto_btmcap = proto_register_protocol("Bluetooth MCAP Protocol", "BT MCAP", "btmcap");
402 btmcap_handle = register_dissector("btmcap", dissect_btmcap, proto_btmcap);
404 proto_register_field_array(proto_btmcap, hf, array_length(hf));
405 proto_register_subtree_array(ett, array_length(ett));
406 expert_btmcap = expert_register_protocol(proto_btmcap);
407 expert_register_field_array(expert_btmcap, ei, array_length(ei));
409 module = prefs_register_protocol_subtree("Bluetooth", proto_btmcap, NULL);
410 prefs_register_static_text_preference(module, "mcap.version",
411 "Bluetooth Protocol MCAP version: 1.0",
412 "Version of protocol supported by this dissector.");
416 void
417 proto_reg_handoff_btmcap(void)
419 dissector_add_string("bluetooth.uuid", "1e", btmcap_handle);
420 dissector_add_string("bluetooth.uuid", "1f", btmcap_handle);
421 dissector_add_string("bluetooth.uuid", "1400", btmcap_handle);
422 dissector_add_string("bluetooth.uuid", "1401", btmcap_handle);
423 dissector_add_string("bluetooth.uuid", "1402", btmcap_handle);
425 /* dynamic PSM */
426 dissector_add_for_decode_as("btl2cap.psm", btmcap_handle);
427 dissector_add_for_decode_as("btl2cap.cid", btmcap_handle);
431 * Editor modelines - https://www.wireshark.org/tools/modelines.html
433 * Local variables:
434 * c-basic-offset: 4
435 * tab-width: 8
436 * indent-tabs-mode: nil
437 * End:
439 * vi: set shiftwidth=4 tabstop=8 expandtab:
440 * :indentSize=4:tabSize=8:noTabs=true: