Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-btsap.c
blob3272d51d840d8418b656bf30ccebd9d52ee7f4ca
1 /* packet-btsap.c
2 * Routines for Bluetooth SAP 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/prefs.h>
17 #include <epan/expert.h>
18 #include "packet-btsdp.h"
20 enum {
21 TOP_DISSECT_OFF = 0,
22 TOP_DISSECT_INTERNAL = 1,
23 TOP_DISSECT_TOP = 2
26 enum {
27 PARAMETER_MAX_MSG_SIZE = 0x00,
28 PARAMETER_CONNECTION_STATUS = 0x01,
29 PARAMETER_RESULT_CODE = 0x02,
30 PARAMETER_DISCONNECTION_TYPE = 0x03,
31 PARAMETER_COMMAND_APDU = 0x04,
32 PARAMETER_RESPONSE_APDU = 0x05,
33 PARAMETER_ATR = 0x06,
34 PARAMETER_CARD_READER_STATUS = 0x07,
35 PARAMETER_STATUS_CHANGE = 0x08,
36 PARAMETER_TRANSPORT_PROTOCOL = 0x09,
37 PARAMETER_COMMAND_APDU_7816 = 0x10
40 static int proto_btsap;
41 static int hf_btsap_header_msg_id;
42 static int hf_btsap_header_number_of_parameters;
43 static int hf_btsap_header_reserved;
44 static int hf_btsap_parameter;
45 static int hf_btsap_parameter_id;
46 static int hf_btsap_parameter_reserved;
47 static int hf_btsap_parameter_length;
48 static int hf_btsap_parameter_padding;
49 static int hf_btsap_parameter_max_msg_size;
50 static int hf_btsap_parameter_connection_status;
51 static int hf_btsap_parameter_result_code;
52 static int hf_btsap_parameter_disconnection_type;
53 static int hf_btsap_parameter_status_change;
54 static int hf_btsap_parameter_transport_protocol;
55 static int hf_btsap_parameter_card_reader_status_card_reader_identity;
56 static int hf_btsap_parameter_card_reader_status_card_reader_removable;
57 static int hf_btsap_parameter_card_reader_status_card_reader_present;
58 static int hf_btsap_parameter_card_reader_status_card_reader_present_lower;
59 static int hf_btsap_parameter_card_reader_status_card_present;
60 static int hf_btsap_parameter_card_reader_status_card_powered;
62 static int hf_btsap_data;
64 static int ett_btsap;
65 static int ett_btsap_parameter;
67 static expert_field ei_btsap_parameter_error;
68 static expert_field ei_unexpected_data;
70 static int top_dissect = TOP_DISSECT_INTERNAL;
72 static dissector_handle_t btsap_handle;
73 static dissector_handle_t gsm_sim_cmd_handle;
74 static dissector_handle_t gsm_sim_resp_handle;
75 static dissector_handle_t iso7816_atr_handle;
77 static const value_string msg_id_vals[] = {
78 { 0x00, "CONNECT_REQ" },
79 { 0x01, "CONNECT_RESP" },
80 { 0x02, "DISCONNECT_REQ" },
81 { 0x03, "DISCONNECT_RESP" },
82 { 0x04, "DISCONNECT_IND" },
83 { 0x05, "TRANSFER_APDU_REQ" },
84 { 0x06, "TRANSFER_APDU_RESP" },
85 { 0x07, "TRANSFER_ATR_REQ" },
86 { 0x08, "TRANSFER_ATR_RESP" },
87 { 0x09, "POWER_SIM_OFF_REQ" },
88 { 0x0A, "POWER_SIM_OFF_RESP" },
89 { 0x0B, "POWER_SIM_ON_REQ" },
90 { 0x0C, "POWER_SIM_ON_RESP" },
91 { 0x0D, "RESET_SIM_REQ" },
92 { 0x0E, "RESET_SIM_RESP" },
93 { 0x0F, "TRANSFER_CARD_READER_STATUS_REQ" },
94 { 0x10, "TRANSFER_CARD_READER_STATUS_RESP" },
95 { 0x11, "STATUS_IND" },
96 { 0x12, "ERROR_RESP" },
97 { 0x13, "SET_TRANSPORT_PROTOCOL_REQ" },
98 { 0x14, "SET_TRANSPORT_PROTOCOL_RESP" },
99 { 0, NULL }
102 static const value_string parameter_id_vals[] = {
103 { 0x00, "MaxMsgSize" },
104 { 0x01, "ConnectionStatus" },
105 { 0x02, "ResultCode" },
106 { 0x03, "DisconnectionType" },
107 { 0x04, "CommandAPDU" },
108 { 0x05, "ResponseAPDU" },
109 { 0x06, "ATR" },
110 { 0x07, "CardReaderStatus" },
111 { 0x08, "StatusChange" },
112 { 0x09, "TransportProtocol" },
113 { 0x10, "CommandAPDU7816" },
114 { 0, NULL }
117 static const value_string connection_status_vals[] = {
118 { 0x00, "OK, Server can fulfill requirements" },
119 { 0x01, "Error, Server unable to establish connection" },
120 { 0x02, "Error, Server does not support maximum message size" },
121 { 0x03, "Error, maximum message size by Client is too small" },
122 { 0x04, "OK, ongoing call" },
123 { 0, NULL }
126 static const value_string result_code_vals[] = {
127 { 0x00, "OK, request processed correctly" },
128 { 0x01, "Error, no reason defined" },
129 { 0x02, "Error, card not accessible" },
130 { 0x03, "Error, card (already) powered off" },
131 { 0x04, "Error, card removed" },
132 { 0x05, "Error, card already powered on" },
133 { 0x06, "Error, data no available" },
134 { 0x07, "Error, not supported" },
135 { 0, NULL }
138 static const value_string disconnection_type_vals[] = {
139 { 0x00, "Graceful" },
140 { 0x01, "Immediate" },
141 { 0, NULL }
144 static const value_string status_change_vals[] = {
145 { 0x00, "Unknown Error" },
146 { 0x01, "Card Reset" },
147 { 0x02, "Card Not Accessible" },
148 { 0x03, "Card Removed" },
149 { 0x04, "Card Inserted" },
150 { 0x05, "Card Recovered" },
151 { 0, NULL }
154 static const enum_val_t pref_top_dissect[] = {
155 { "off", "off", TOP_DISSECT_OFF },
156 { "internal", "Put higher dissectors under this one", TOP_DISSECT_INTERNAL },
157 { "top", "On top", TOP_DISSECT_TOP },
158 { NULL, NULL, 0 }
161 void proto_register_btsap(void);
162 void proto_reg_handoff_btsap(void);
164 static int
165 dissect_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *top_tree,
166 proto_tree *tree, int offset, uint8_t *parameter, int *parameter_offset)
168 proto_item *parameter_item;
169 proto_item *pitem;
170 proto_tree *ptree;
171 tvbuff_t *next_tvb;
172 unsigned parameter_id;
173 unsigned parameter_length;
174 unsigned parameter_padding_length;
175 unsigned padding_length;
176 unsigned length;
177 uint16_t max_msg_size;
178 uint8_t connection_status;
179 uint8_t result_code;
180 uint8_t disconnection_type;
181 uint8_t status_change;
182 uint8_t transport_protocol;
184 parameter_id = tvb_get_uint8(tvb, offset);
185 parameter_length = tvb_get_ntohs(tvb, offset + 2);
186 parameter_padding_length = parameter_length % 4;
187 if (parameter_padding_length > 0)
188 parameter_padding_length = 4 - parameter_padding_length;
190 parameter_item = proto_tree_add_none_format(tree, hf_btsap_parameter, tvb, offset,
191 2 + 2 + parameter_length + parameter_padding_length, "Parameter: %s: ",
192 val_to_str_const(parameter_id, parameter_id_vals, "Unknown ParameterID"));
193 ptree = proto_item_add_subtree(parameter_item, ett_btsap_parameter);
195 proto_tree_add_item(ptree, hf_btsap_parameter_id, tvb, offset, 1, ENC_BIG_ENDIAN);
197 col_append_fstr(pinfo->cinfo, COL_INFO, " - %s", val_to_str_const(parameter_id, parameter_id_vals, "Unknown ParameterID"));
198 offset += 1;
200 proto_tree_add_item(ptree, hf_btsap_parameter_reserved, tvb, offset, 1, ENC_BIG_ENDIAN);
201 offset += 1;
203 pitem = proto_tree_add_item(ptree, hf_btsap_parameter_length, tvb, offset, 2, ENC_BIG_ENDIAN);
205 proto_item_append_text(pitem, " (in 4 bytes sections, padding length: %u)", parameter_padding_length);
206 offset += 2;
208 switch(parameter_id) {
209 case 0x00: /* MaxMsgSize */
210 proto_tree_add_item(ptree, hf_btsap_parameter_max_msg_size, tvb, offset, 2, ENC_BIG_ENDIAN);
211 max_msg_size = tvb_get_ntohs(tvb, offset);
212 proto_item_append_text(parameter_item, "%u", max_msg_size);
213 col_append_fstr(pinfo->cinfo, COL_INFO, ": %u", max_msg_size);
214 length = 2;
215 padding_length = 2;
216 break;
217 case 0x01: /* ConnectionStatus */
218 proto_tree_add_item(ptree, hf_btsap_parameter_connection_status, tvb, offset, 1, ENC_BIG_ENDIAN);
219 connection_status = tvb_get_uint8(tvb, offset);
220 proto_item_append_text(parameter_item, "%s", val_to_str_const(connection_status, connection_status_vals, "Unknown"));
221 col_append_fstr(pinfo->cinfo, COL_INFO, ": %s", val_to_str_const(connection_status, connection_status_vals, "Unknown"));
222 length = 1;
223 padding_length = 3;
224 break;
225 case 0x02: /* ResultCode */
226 proto_tree_add_item(ptree, hf_btsap_parameter_result_code, tvb, offset, 1, ENC_BIG_ENDIAN);
227 result_code = tvb_get_uint8(tvb, offset);
228 proto_item_append_text(parameter_item, "%s", val_to_str_const(result_code, result_code_vals, "Unknown"));
229 col_append_fstr(pinfo->cinfo, COL_INFO, ": %s", val_to_str_const(result_code, result_code_vals, "Unknown"));
230 length = 1;
231 padding_length = 3;
232 break;
233 case 0x03: /* DisconnectionType */
234 proto_tree_add_item(ptree, hf_btsap_parameter_disconnection_type, tvb, offset, 1, ENC_BIG_ENDIAN);
235 disconnection_type = tvb_get_uint8(tvb, offset);
236 proto_item_append_text(parameter_item, "%s", val_to_str_const(disconnection_type, disconnection_type_vals, "Unknown"));
237 col_append_fstr(pinfo->cinfo, COL_INFO, ": %s", val_to_str_const(disconnection_type, disconnection_type_vals, "Unknown"));
238 length = 1;
239 padding_length = 3;
240 break;
241 case 0x04: /* CommandAPDU */
242 /* GSM 11.11 */
243 if (gsm_sim_cmd_handle && top_dissect != TOP_DISSECT_OFF) {
244 next_tvb = tvb_new_subset_length(tvb, offset, parameter_length);
245 col_append_str(pinfo->cinfo, COL_INFO, ": ");
247 if (top_dissect == TOP_DISSECT_INTERNAL) {
248 call_dissector(gsm_sim_cmd_handle, next_tvb, pinfo, ptree);
249 } else {
250 col_clear(pinfo->cinfo, COL_INFO);
251 call_dissector(gsm_sim_cmd_handle, next_tvb, pinfo, top_tree);
253 } else {
254 proto_tree_add_item(ptree, hf_btsap_data, tvb, offset, parameter_length, ENC_NA);
257 length = parameter_length;
258 padding_length = parameter_padding_length;
259 break;
260 case 0x05: /* ResponseAPDU */
261 /* GSM 11.11 or ISO/IEC 7816-4; depend of TRANSFER_APDU_REQ */
262 if (gsm_sim_resp_handle && top_dissect != TOP_DISSECT_OFF) {
263 next_tvb = tvb_new_subset_length(tvb, offset, parameter_length);
264 col_append_str(pinfo->cinfo, COL_INFO, ": ");
266 if (top_dissect == TOP_DISSECT_INTERNAL) {
267 call_dissector(gsm_sim_resp_handle, next_tvb, pinfo, ptree);
268 } else {
269 col_clear(pinfo->cinfo, COL_INFO);
270 call_dissector(gsm_sim_resp_handle, next_tvb, pinfo, top_tree);
272 } else {
273 proto_tree_add_item(ptree, hf_btsap_data, tvb, offset, parameter_length, ENC_NA);
276 length = parameter_length;
277 padding_length = parameter_padding_length;
278 break;
279 case 0x06: /* ATR */
280 /* ISO/IEC 7816-3 */
281 if (iso7816_atr_handle && top_dissect != TOP_DISSECT_OFF) {
282 next_tvb = tvb_new_subset_length(tvb, offset, parameter_length);
283 col_append_str(pinfo->cinfo, COL_INFO, ": ");
285 if (top_dissect == TOP_DISSECT_INTERNAL) {
286 call_dissector(iso7816_atr_handle, next_tvb, pinfo, ptree);
287 } else {
288 col_clear(pinfo->cinfo, COL_INFO);
289 call_dissector(iso7816_atr_handle, next_tvb, pinfo, top_tree);
291 } else {
292 proto_tree_add_item(ptree, hf_btsap_data, tvb, offset, parameter_length, ENC_NA);
295 length = parameter_length;
296 padding_length = parameter_padding_length;
297 break;
298 case 0x07: /* CardReaderStatus */
299 /* 3GPP TS 11.14 */
300 proto_tree_add_item(ptree, hf_btsap_parameter_card_reader_status_card_powered, tvb, offset, 1, ENC_BIG_ENDIAN);
301 proto_tree_add_item(ptree, hf_btsap_parameter_card_reader_status_card_present, tvb, offset, 1, ENC_BIG_ENDIAN);
302 proto_tree_add_item(ptree, hf_btsap_parameter_card_reader_status_card_reader_present_lower, tvb, offset, 1, ENC_BIG_ENDIAN);
303 proto_tree_add_item(ptree, hf_btsap_parameter_card_reader_status_card_reader_present, tvb, offset, 1, ENC_BIG_ENDIAN);
304 proto_tree_add_item(ptree, hf_btsap_parameter_card_reader_status_card_reader_removable, tvb, offset, 1, ENC_BIG_ENDIAN);
305 proto_tree_add_item(ptree, hf_btsap_parameter_card_reader_status_card_reader_identity, tvb, offset, 1, ENC_BIG_ENDIAN);
306 length = 1;
307 padding_length = 3;
308 break;
309 case 0x08: /* StatusChange */
310 proto_tree_add_item(ptree, hf_btsap_parameter_status_change, tvb, offset, 1, ENC_BIG_ENDIAN);
311 status_change = tvb_get_uint8(tvb, offset);
312 proto_item_append_text(parameter_item, "%s", val_to_str_const(status_change, status_change_vals, "Unknown"));
313 col_append_fstr(pinfo->cinfo, COL_INFO, ": %s", val_to_str_const(status_change, status_change_vals, "Unknown"));
314 length = 1;
315 padding_length = 3;
316 break;
317 case 0x09: /* TransportProtocol */
318 proto_tree_add_item(ptree, hf_btsap_parameter_transport_protocol, tvb, offset, 1, ENC_BIG_ENDIAN);
319 transport_protocol = tvb_get_uint8(tvb, offset);
320 proto_item_append_text(parameter_item, "%u", transport_protocol);
321 col_append_fstr(pinfo->cinfo, COL_INFO, ": %u", transport_protocol);
322 length = 1;
323 padding_length = 3;
324 break;
325 case 0x10: /* CommandAPDU7816 */
326 /* ISO/IEC 7816-4 */
327 if (gsm_sim_cmd_handle && top_dissect != TOP_DISSECT_OFF) {
328 next_tvb = tvb_new_subset_length(tvb, offset, parameter_length);
329 col_append_str(pinfo->cinfo, COL_INFO, ": ");
331 if (top_dissect == TOP_DISSECT_INTERNAL) {
332 call_dissector(gsm_sim_cmd_handle, next_tvb, pinfo, ptree);
333 } else {
334 col_clear(pinfo->cinfo, COL_INFO);
335 call_dissector(gsm_sim_cmd_handle, next_tvb, pinfo, top_tree);
337 } else {
338 proto_tree_add_item(ptree, hf_btsap_data, tvb, offset, parameter_length, ENC_NA);
341 length = parameter_length;
342 padding_length = parameter_padding_length;
343 break;
344 default:
345 proto_tree_add_item(ptree, hf_btsap_data, tvb, offset, parameter_length, ENC_NA);
346 length = parameter_length;
347 padding_length = parameter_padding_length;
350 *parameter = parameter_id;
351 *parameter_offset = offset;
353 if (length != parameter_length || padding_length != parameter_padding_length) {
354 /* Malformed frame */
355 expert_add_info_format(pinfo, pitem, &ei_btsap_parameter_error,
356 "Parameter Length does not meet content length");
359 offset += parameter_length;
361 if (parameter_padding_length > 0) {
362 pitem = proto_tree_add_item(ptree, hf_btsap_parameter_padding, tvb, offset, parameter_padding_length, ENC_NA);
363 proto_item_append_text(pitem, " (length %d)", parameter_padding_length);
364 offset += parameter_padding_length;
367 return offset;
370 static int
371 dissect_btsap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
373 proto_item *ti;
374 proto_tree *btsap_tree;
375 unsigned offset = 0;
376 unsigned msg_id;
377 unsigned number_of_parameters;
378 uint8_t *parameters;
379 int *parameter_offsets;
380 unsigned parameters_check = 0;
381 unsigned required_parameters = 0;
382 unsigned i_parameter;
383 unsigned i_next_parameter;
385 ti = proto_tree_add_item(tree, proto_btsap, tvb, offset, -1, ENC_NA);
386 btsap_tree = proto_item_add_subtree(ti, ett_btsap);
388 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SAP");
390 switch (pinfo->p2p_dir) {
391 case P2P_DIR_SENT:
392 col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
393 break;
394 case P2P_DIR_RECV:
395 col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
396 break;
397 default:
398 col_set_str(pinfo->cinfo, COL_INFO, "UnknownDirection ");
399 break;
402 proto_tree_add_item(btsap_tree, hf_btsap_header_msg_id, tvb, offset, 1, ENC_BIG_ENDIAN);
403 msg_id = tvb_get_uint8(tvb, offset);
404 col_append_str(pinfo->cinfo, COL_INFO, val_to_str_const(msg_id, msg_id_vals, "Unknown MsgID"));
405 offset += 1;
407 proto_tree_add_item(btsap_tree, hf_btsap_header_number_of_parameters, tvb, offset, 1, ENC_BIG_ENDIAN);
408 number_of_parameters = tvb_get_uint8(tvb, offset);
409 offset += 1;
411 proto_tree_add_item(btsap_tree, hf_btsap_header_reserved, tvb, offset, 2, ENC_BIG_ENDIAN);
412 offset += 2;
414 parameters = (uint8_t *) wmem_alloc(pinfo->pool, number_of_parameters * sizeof(uint8_t));
415 parameter_offsets = (int *) wmem_alloc0(pinfo->pool, number_of_parameters * sizeof(unsigned));
417 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
418 offset = dissect_parameter(tvb, pinfo, tree, btsap_tree, offset, &parameters[i_parameter], &parameter_offsets[i_parameter]);
421 /* detect invalid data */
422 switch(msg_id) {
423 case 0x02: /* DISCONNECT_REQ */
424 case 0x03: /* DISCONNECT_RESP */
425 case 0x07: /* TRANSFER_ATR_REQ */
426 case 0x09: /* POWER_SIM_OFF_REQ */
427 case 0x0B: /* POWER_SIM_ON_REQ */
428 case 0x0D: /* RESET_SIM_REQ */
429 case 0x0F: /* TRANSFER_CARD_READER_STATUS_REQ */
430 case 0x12: /* ERROR_RESP */
431 required_parameters = 0;
432 break;
433 case 0x0A: /* POWER_SIM_OFF_RESP */
434 case 0x0C: /* POWER_SIM_ON_RESP */
435 case 0x0E: /* RESET_SIM_RESP */
436 case 0x14: /* SET_TRANSPORT_PROTOCOL_RESP */
437 /* Parameters: 1 - ResultCode */
438 required_parameters = 1;
439 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
440 if (parameters[i_parameter] == PARAMETER_RESULT_CODE) ++parameters_check;
442 break;
443 case 0x00: /* CONNECT_REQ */
444 /* 1 - MaxMsgSize */
445 required_parameters = 1;
446 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
447 if (parameters[i_parameter] == PARAMETER_MAX_MSG_SIZE) ++parameters_check;
449 break;
450 case 0x01: /* CONNECT_RESP */
451 /* Parameters: 1..2 - ConnectionStatus, MaxMsgSize (if error cannot fulfill) */
452 required_parameters = 1;
453 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
454 if (parameters[i_parameter] == PARAMETER_CONNECTION_STATUS) {
455 if (tvb_get_uint8(tvb, parameter_offsets[i_parameter]) != 0x00) {
456 for (i_next_parameter = 0; i_next_parameter < number_of_parameters; ++i_next_parameter) {
457 if (parameters[i_next_parameter] == PARAMETER_MAX_MSG_SIZE) {
458 ++parameters_check;
459 required_parameters = 2;
463 ++parameters_check;
466 break;
467 case 0x04: /* DISCONNECT_IND */
468 /* Parameters: 1 - DisconnectionType */
469 required_parameters = 1;
470 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
471 if (parameters[i_parameter] == PARAMETER_DISCONNECTION_TYPE) ++parameters_check;
473 break;
474 case 0x05: /* TRANSFER_APDU_REQ */
475 /* Parameters: 1 - CommandAPU or CommandAPU7816 */
476 required_parameters = 1;
477 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
478 if (parameters[i_parameter] == PARAMETER_COMMAND_APDU ||
479 parameters[i_parameter] == PARAMETER_COMMAND_APDU_7816)
480 ++parameters_check;
482 break;
483 case 0x06: /* TRANSFER_APDU_RESP */
484 /* Parameters: 1..2 - ResultCode, ResponseAPDU (if status ok) */
485 required_parameters = 1;
486 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
487 if (parameters[i_parameter] == PARAMETER_RESULT_CODE) {
488 if (tvb_get_uint8(tvb, parameter_offsets[i_parameter]) == 0x00) {
489 for (i_next_parameter = 0; i_next_parameter < number_of_parameters; ++i_next_parameter) {
490 if (parameters[i_next_parameter] == PARAMETER_RESPONSE_APDU) {
491 ++parameters_check;
492 required_parameters = 2;
496 ++parameters_check;
499 break;
500 case 0x08: /* TRANSFER_ATR_RESP */
501 /* Parameters: 1..2 - ResultCode, ATR (if status ok) */
502 required_parameters = 1;
503 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
504 if (parameters[i_parameter] == PARAMETER_RESULT_CODE) {
505 if (tvb_get_uint8(tvb, parameter_offsets[i_parameter]) == 0x00) {
506 for (i_next_parameter = 0; i_next_parameter < number_of_parameters; ++i_next_parameter) {
507 if (parameters[i_next_parameter] == PARAMETER_ATR) {
508 ++parameters_check;
509 required_parameters = 2;
513 ++parameters_check;
516 break;
517 case 0x10: /* TRANSFER_CARD_READER_STATUS_RESP */
518 /* Parameters: 1..2 - ResultCode, CardReaderStatus (if status ok) */
519 required_parameters = 1;
520 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
521 if (parameters[i_parameter] == PARAMETER_RESULT_CODE) {
522 if (tvb_get_uint8(tvb, parameter_offsets[i_parameter]) == 0x00) {
523 for (i_next_parameter = 0; i_next_parameter < number_of_parameters; ++i_next_parameter) {
524 if (parameters[i_next_parameter] == PARAMETER_CARD_READER_STATUS) {
525 ++parameters_check;
526 required_parameters = 2;
530 ++parameters_check;
533 break;
534 case 0x11: /* STATUS_IND */
535 /* Parameters: 1 - StatusChange */
536 required_parameters = 1;
537 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
538 if (parameters[i_parameter] == PARAMETER_STATUS_CHANGE) ++parameters_check;
540 break;
541 case 0x13: /* SET_TRANSPORT_PROTOCOL_REQ */
542 /* Parameters: 1 - TransportProtocol */
543 required_parameters = 1;
544 for (i_parameter = 0; i_parameter < number_of_parameters; ++i_parameter) {
545 if (parameters[i_parameter] == PARAMETER_TRANSPORT_PROTOCOL) ++parameters_check;
547 break;
550 if (parameters_check < required_parameters) {
551 proto_tree_add_expert_format(tree, pinfo, &ei_btsap_parameter_error,
552 tvb, offset, 0, "There are no required parameters");
553 } else if (parameters_check > required_parameters) {
554 proto_tree_add_expert_format(tree, pinfo, &ei_btsap_parameter_error,
555 tvb, offset, 0, "Invalid parameters");
557 if (number_of_parameters < required_parameters) {
558 proto_tree_add_expert_format(tree, pinfo, &ei_btsap_parameter_error,
559 tvb, offset, 0, "Too few parameters");
560 } else if (number_of_parameters > required_parameters) {
561 proto_tree_add_expert_format(tree, pinfo, &ei_btsap_parameter_error,
562 tvb, offset, 0, "Too many parameters");
565 if (tvb_reported_length(tvb) > offset)
566 proto_tree_add_expert(tree, pinfo, &ei_unexpected_data, tvb, offset, tvb_reported_length_remaining(tvb, offset));
568 return offset;
572 void
573 proto_register_btsap(void)
575 module_t *module;
576 expert_module_t *expert_btsap;
578 static hf_register_info hf[] = {
579 { &hf_btsap_header_msg_id,
580 { "MsgID", "btsap.msg_id",
581 FT_UINT8, BASE_HEX, VALS(msg_id_vals), 0x00,
582 NULL, HFILL }
584 { &hf_btsap_header_number_of_parameters,
585 { "Number of Parameters", "btsap.number_of_parameters",
586 FT_UINT8, BASE_HEX, NULL, 0x00,
587 NULL, HFILL }
589 { &hf_btsap_header_reserved,
590 { "reserved", "btsap.reserved",
591 FT_UINT16, BASE_HEX, NULL, 0x00,
592 NULL, HFILL }
594 { &hf_btsap_parameter,
595 { "Parameter", "btsap.parameter",
596 FT_NONE, BASE_NONE, NULL, 0x00,
597 NULL, HFILL }
599 { &hf_btsap_parameter_id,
600 { "Parameter ID", "btsap.parameter_id",
601 FT_UINT8, BASE_HEX, VALS(parameter_id_vals), 0x00,
602 NULL, HFILL }
604 { &hf_btsap_parameter_reserved,
605 { "reserved", "btsap.parameter.reserved",
606 FT_UINT8, BASE_HEX, NULL, 0x00,
607 NULL, HFILL }
609 { &hf_btsap_parameter_length,
610 { "Parameter Length", "btsap.parameter.length",
611 FT_UINT16, BASE_DEC, NULL, 0x00,
612 NULL, HFILL }
614 { &hf_btsap_parameter_padding,
615 { "Parameter Padding", "btsap.parameter.padding",
616 FT_NONE, BASE_NONE, NULL, 0x00,
617 NULL, HFILL }
619 { &hf_btsap_parameter_max_msg_size,
620 { "Max Msg Size", "btsap.parameter.max_msg_size",
621 FT_UINT16, BASE_DEC, NULL, 0x00,
622 NULL, HFILL }
624 { &hf_btsap_parameter_connection_status,
625 { "Connection Status", "btsap.parameter.connection_status",
626 FT_UINT8, BASE_HEX, VALS(connection_status_vals), 0x00,
627 NULL, HFILL }
629 { &hf_btsap_parameter_result_code,
630 { "Result Code", "btsap.parameter.result_code",
631 FT_UINT8, BASE_HEX, VALS(result_code_vals), 0x00,
632 NULL, HFILL }
634 { &hf_btsap_parameter_disconnection_type,
635 { "Disconnection Type", "btsap.parameter.disconnection_type",
636 FT_UINT8, BASE_HEX, VALS(disconnection_type_vals), 0x00,
637 NULL, HFILL }
639 { &hf_btsap_parameter_card_reader_status_card_reader_identity,
640 { "Identify of Card Reader", "btsap.parameter.card_reader_status.card_reader_identity",
641 FT_UINT8, BASE_HEX, NULL, 0x03,
642 NULL, HFILL }
644 { &hf_btsap_parameter_card_reader_status_card_reader_removable,
645 { "Card Reader is Removable", "btsap.parameter.card_reader_status.card_reader_removable",
646 FT_BOOLEAN, 8, NULL, 0x08,
647 NULL, HFILL }
649 { &hf_btsap_parameter_card_reader_status_card_reader_present,
650 { "Card Reader is Present", "btsap.parameter.card_reader_status.card_reader_present",
651 FT_BOOLEAN, 8, NULL, 0x10,
652 NULL, HFILL }
654 { &hf_btsap_parameter_card_reader_status_card_reader_present_lower,
655 { "Card Reader Present is ID-1 Size","btsap.parameter.card_reader_status.card_reader_present_lower",
656 FT_BOOLEAN, 8, NULL, 0x20,
657 NULL, HFILL }
659 { &hf_btsap_parameter_card_reader_status_card_present,
660 { "Card is Present in Reader", "btsap.parameter.card_reader_status.card_present",
661 FT_BOOLEAN, 8, NULL, 0x40,
662 NULL, HFILL }
664 { &hf_btsap_parameter_card_reader_status_card_powered,
665 { "Card in Reader is Powered", "btsap.parameter.card_reader_status.card_powered",
666 FT_BOOLEAN, 8, NULL, 0x80,
667 NULL, HFILL }
669 { &hf_btsap_parameter_status_change,
670 { "Status Change", "btsap.parameter.status_change",
671 FT_UINT8, BASE_HEX, VALS(status_change_vals), 0x00,
672 NULL, HFILL }
674 { &hf_btsap_parameter_transport_protocol,
675 { "Transport Protocol", "btsap.parameter.transport_protocol",
676 FT_UINT8, BASE_HEX, NULL, 0x00,
677 NULL, HFILL }
680 { &hf_btsap_data,
681 { "Data", "btsap.data",
682 FT_NONE, BASE_NONE, NULL, 0x0,
683 NULL, HFILL }
688 static int *ett[] = {
689 &ett_btsap,
690 &ett_btsap_parameter
693 static ei_register_info ei[] = {
694 { &ei_btsap_parameter_error, { "btsap.parameter_error", PI_PROTOCOL, PI_WARN, "Parameter error", EXPFILL }},
695 { &ei_unexpected_data, { "btsap.unexpected_data", PI_PROTOCOL, PI_WARN, "Unexpected_data", EXPFILL }},
698 proto_btsap = proto_register_protocol("Bluetooth SAP Profile", "BT SAP", "btsap");
699 btsap_handle = register_dissector("btsap", dissect_btsap, proto_btsap);
701 proto_register_field_array(proto_btsap, hf, array_length(hf));
702 proto_register_subtree_array(ett, array_length(ett));
703 expert_btsap = expert_register_protocol(proto_btsap);
704 expert_register_field_array(expert_btsap, ei, array_length(ei));
706 module = prefs_register_protocol_subtree("Bluetooth", proto_btsap, NULL);
707 prefs_register_static_text_preference(module, "sap.version",
708 "Bluetooth Profile SAP version: 1.1",
709 "Version of protocol supported by this dissector.");
711 prefs_register_enum_preference(module, "sap.top_dissect",
712 "Dissecting the top protocols", "Dissecting the top protocols",
713 &top_dissect, pref_top_dissect, false);
717 void
718 proto_reg_handoff_btsap(void)
720 gsm_sim_cmd_handle = find_dissector_add_dependency("gsm_sim.command", proto_btsap);
721 gsm_sim_resp_handle = find_dissector_add_dependency("gsm_sim.response", proto_btsap);
722 iso7816_atr_handle = find_dissector_add_dependency("iso7816.atr", proto_btsap);
724 dissector_add_string("bluetooth.uuid", "112d", btsap_handle);
726 dissector_add_for_decode_as("btrfcomm.dlci", btsap_handle);
730 * Editor modelines - https://www.wireshark.org/tools/modelines.html
732 * Local variables:
733 * c-basic-offset: 4
734 * tab-width: 8
735 * indent-tabs-mode: nil
736 * End:
738 * vi: set shiftwidth=4 tabstop=8 expandtab:
739 * :indentSize=4:tabSize=8:noTabs=true: