2 * Routines for NXP PN532 HCI Protocol
4 * http://www.nxp.com/documents/user_manual/141520.pdf
6 * Copyright 2013, Michal Labedzki for Tieto Corporation
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * SPDX-License-Identifier: GPL-2.0-or-later
17 #include <epan/packet.h>
18 #include <epan/prefs.h>
19 #include <epan/expert.h>
20 #include "packet-usb.h"
22 static int proto_pn532_hci
;
23 static int hf_preamble
;
24 static int hf_start_code
;
26 static int hf_length_checksum
;
27 static int hf_length_checksum_status
;
28 static int hf_extended_length
;
29 static int hf_packet_code
;
30 static int hf_postable
;
31 static int hf_specific_application_level_error_code
;
32 static int hf_data_checksum
;
33 static int hf_data_checksum_status
;
34 static int hf_ignored
;
36 static int ett_pn532_hci
;
38 static expert_field ei_invalid_length_checksum
;
39 static expert_field ei_invalid_data_checksum
;
41 static dissector_handle_t pn532_handle
;
42 static dissector_handle_t pn532_hci_handle
;
44 static const value_string packet_code_vals
[] = {
45 { 0x00FF, "ACK Frame" },
46 { 0x01FF, "Error Frame" },
47 { 0xFF00, "NACK Frame" },
48 { 0xFFFF, "Extended Information Frame" },
52 void proto_register_pn532_hci(void);
53 void proto_reg_handoff_pn532_hci(void);
56 dissect_pn532_hci(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
)
58 proto_item
*main_item
;
59 proto_tree
*main_tree
;
67 /* Reject the packet if data is NULL */
70 urb
= (urb_info_t
*)data
;
72 length
= tvb_captured_length_remaining(tvb
, offset
);
73 if (length
< 6) return offset
;
75 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "PN532_HCI");
76 col_clear(pinfo
->cinfo
, COL_INFO
);
78 main_item
= proto_tree_add_item(tree
, proto_pn532_hci
, tvb
, offset
, -1, ENC_NA
);
79 main_tree
= proto_item_add_subtree(main_item
, ett_pn532_hci
);
82 while (tvb_captured_length_remaining(tvb
, length
) >= 2 && tvb_get_ntohs(tvb
, length
) != 0x00FF) {
86 proto_tree_add_item(main_tree
, hf_preamble
, tvb
, offset
, length
, ENC_NA
);
90 proto_tree_add_item(main_tree
, hf_start_code
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
93 packet_code
= tvb_get_ntohs(tvb
, offset
);
94 if (packet_code
== 0x00FF) { /* ACK Frame */
95 col_set_str(pinfo
->cinfo
, COL_INFO
, val_to_str_const(packet_code
, packet_code_vals
, "Unknown frame"));
97 proto_tree_add_item(main_tree
, hf_packet_code
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
99 } else if (packet_code
== 0xFF00) { /* NACK Frame */
100 col_set_str(pinfo
->cinfo
, COL_INFO
, val_to_str_const(packet_code
, packet_code_vals
, "Unknown frame"));
102 proto_tree_add_item(main_tree
, hf_packet_code
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
104 } else if (packet_code
== 0x01FF) { /* Error Frame */
105 col_set_str(pinfo
->cinfo
, COL_INFO
, val_to_str_const(packet_code
, packet_code_vals
, "Unknown frame"));
107 proto_tree_add_item(main_tree
, hf_packet_code
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
110 proto_tree_add_item(main_tree
, hf_specific_application_level_error_code
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
112 } else if (packet_code
== 0xFFFF) { /* Extended Information Frame */
113 col_set_str(pinfo
->cinfo
, COL_INFO
, "Extended Information Frame");
115 proto_tree_add_item(main_tree
, hf_extended_length
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
116 length
= tvb_get_ntohs(tvb
, offset
);
119 checksum
= (length
>> 8) + (length
& 0xFF) + tvb_get_uint8(tvb
, offset
);
120 proto_tree_add_checksum(main_tree
, tvb
, offset
, hf_length_checksum
, hf_length_checksum_status
, &ei_invalid_length_checksum
,
121 pinfo
, checksum
, ENC_BIG_ENDIAN
, PROTO_CHECKSUM_VERIFY
|PROTO_CHECKSUM_ZERO
);
124 next_tvb
= tvb_new_subset_length(tvb
, offset
, length
);
125 call_dissector_with_data(pn532_handle
, next_tvb
, pinfo
, tree
, urb
);
128 checksum
= tvb_get_uint8(tvb
, offset
);
130 checksum
+= tvb_get_uint8(tvb
, offset
- length
);
133 proto_tree_add_checksum(main_tree
, tvb
, offset
, hf_data_checksum
, hf_data_checksum_status
, &ei_invalid_data_checksum
, pinfo
, 0,
134 ENC_BIG_ENDIAN
, PROTO_CHECKSUM_VERIFY
|PROTO_CHECKSUM_ZERO
);
136 } else { /* Normal Information Frame */
137 col_set_str(pinfo
->cinfo
, COL_INFO
, "Normal Information Frame");
139 proto_tree_add_item(main_tree
, hf_length
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
140 length
= tvb_get_uint8(tvb
, offset
);
143 checksum
= length
+ tvb_get_uint8(tvb
, offset
);
144 proto_tree_add_checksum(main_tree
, tvb
, offset
, hf_length_checksum
, hf_length_checksum_status
, &ei_invalid_length_checksum
,
145 pinfo
, checksum
, ENC_BIG_ENDIAN
, PROTO_CHECKSUM_VERIFY
|PROTO_CHECKSUM_ZERO
);
148 next_tvb
= tvb_new_subset_length(tvb
, offset
, length
);
149 call_dissector_with_data(pn532_handle
, next_tvb
, pinfo
, tree
, urb
);
152 proto_tree_add_item(main_tree
, hf_data_checksum
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
153 checksum
= tvb_get_uint8(tvb
, offset
);
155 checksum
+= tvb_get_uint8(tvb
, offset
- length
);
159 proto_tree_add_expert(main_tree
, pinfo
, &ei_invalid_data_checksum
, tvb
, offset
, 1);
165 if (tvb_captured_length_remaining(tvb
, offset
) == 1) {
167 } else while (tvb_captured_length_remaining(tvb
, offset
+ length
) >= 2 && tvb_get_ntohs(tvb
, offset
+ length
) != 0x00FF) {
171 proto_tree_add_item(main_tree
, hf_postable
, tvb
, offset
, length
, ENC_NA
);
175 if (tvb_captured_length_remaining(tvb
, offset
)) {
176 proto_tree_add_item(main_tree
, hf_ignored
, tvb
, offset
, tvb_captured_length_remaining(tvb
, offset
), ENC_NA
);
177 offset
+= tvb_captured_length_remaining(tvb
, offset
);
184 proto_register_pn532_hci(void)
187 expert_module_t
*expert_module
;
189 static hf_register_info hf
[] = {
191 { "Preamble", "pn532_hci.preamble",
192 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
196 { "Start Code", "pn532_hci.start_code",
197 FT_UINT16
, BASE_HEX
, NULL
, 0x00,
201 { "Packet Code", "pn532_hci.packet_code",
202 FT_UINT16
, BASE_HEX
, VALS(packet_code_vals
), 0x00,
206 { "Length", "pn532_hci.length",
207 FT_UINT8
, BASE_DEC
, NULL
, 0x00,
210 { &hf_extended_length
,
211 { "Extended Length", "pn532_hci.extended_length",
212 FT_UINT16
, BASE_DEC
, NULL
, 0x00,
215 { &hf_length_checksum
,
216 { "Length Checksum", "pn532_hci.length_checksum",
217 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
220 { &hf_length_checksum_status
,
221 { "Length Checksum Status", "pn532_hci.length_checksum.status",
222 FT_UINT8
, BASE_NONE
, VALS(proto_checksum_vals
), 0x00,
226 { "Data Checksum", "pn532_hci.data_checksum",
227 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
230 { &hf_data_checksum_status
,
231 { "Data Checksum Status", "pn532_hci.data_checksum.status",
232 FT_UINT8
, BASE_NONE
, VALS(proto_checksum_vals
), 0x00,
235 { &hf_specific_application_level_error_code
,
236 { "Specific Application Level Error Code","pn532_hci.specific_application_level_error_code",
237 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
241 { "Postamble", "pn532_hci.postamble",
242 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
246 { "Ignored", "pn532_hci.ignored",
247 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
252 static int *ett
[] = {
256 static ei_register_info ei
[] = {
257 { &ei_invalid_length_checksum
, { "pn532_hci.expert.invalid_length_checksum", PI_PROTOCOL
, PI_WARN
, "Invalid Length Checksum", EXPFILL
}},
258 { &ei_invalid_data_checksum
, { "pn532_hci.expert.invalid_data_checksum", PI_PROTOCOL
, PI_WARN
, "Invalid Data Checksum", EXPFILL
}},
261 proto_pn532_hci
= proto_register_protocol("NXP PN532 HCI", "PN532_HCI", "pn532_hci");
262 pn532_hci_handle
= register_dissector("pn532_hci", dissect_pn532_hci
, proto_pn532_hci
);
264 proto_register_field_array(proto_pn532_hci
, hf
, array_length(hf
));
265 proto_register_subtree_array(ett
, array_length(ett
));
266 expert_module
= expert_register_protocol(proto_pn532_hci
);
267 expert_register_field_array(expert_module
, ei
, array_length(ei
));
269 module
= prefs_register_protocol(proto_pn532_hci
, NULL
);
270 prefs_register_static_text_preference(module
, "version",
271 "PN532 HCI protocol version is based on: \"UM0701-02; PN532 User Manual\"",
272 "Version of protocol supported by this dissector.");
276 proto_reg_handoff_pn532_hci(void)
278 pn532_handle
= find_dissector_add_dependency("pn532", proto_pn532_hci
);
280 dissector_add_uint("usb.product", (0x04e6 << 16) | 0x5591, pn532_hci_handle
);
282 dissector_add_for_decode_as("usb.device", pn532_hci_handle
);
283 dissector_add_for_decode_as("usb.protocol", pn532_hci_handle
);
287 * Editor modelines - https://www.wireshark.org/tools/modelines.html
292 * indent-tabs-mode: nil
295 * vi: set shiftwidth=4 tabstop=8 expandtab:
296 * :indentSize=4:tabSize=8:noTabs=true: