Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-rfid-pn532-hci.c
blob41f18e7750fa809d0dd35f1505a919380eee8486
1 /* packet-pn532_hci.c
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
15 #include "config.h"
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;
25 static int hf_length;
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" },
49 { 0, NULL }
52 void proto_register_pn532_hci(void);
53 void proto_reg_handoff_pn532_hci(void);
55 static int
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;
60 int offset = 0;
61 tvbuff_t *next_tvb;
62 uint16_t packet_code;
63 uint16_t length;
64 uint8_t checksum;
65 urb_info_t *urb;
67 /* Reject the packet if data is NULL */
68 if (data == NULL)
69 return 0;
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);
81 length = 0;
82 while (tvb_captured_length_remaining(tvb, length) >= 2 && tvb_get_ntohs(tvb, length) != 0x00FF) {
83 length += 1;
85 if (length) {
86 proto_tree_add_item(main_tree, hf_preamble, tvb, offset, length, ENC_NA);
87 offset += length;
90 proto_tree_add_item(main_tree, hf_start_code, tvb, offset, 2, ENC_BIG_ENDIAN);
91 offset += 2;
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);
98 offset += 2;
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);
103 offset += 2;
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);
108 offset += 2;
110 proto_tree_add_item(main_tree, hf_specific_application_level_error_code, tvb, offset, 1, ENC_BIG_ENDIAN);
111 offset += 1;
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);
117 offset += 2;
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);
122 offset += 1;
124 next_tvb = tvb_new_subset_length(tvb, offset, length);
125 call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, urb);
126 offset += length;
128 checksum = tvb_get_uint8(tvb, offset);
129 while (length) {
130 checksum += tvb_get_uint8(tvb, offset - length);
131 length -= 1;
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);
135 offset += 1;
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);
141 offset += 1;
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);
146 offset += 1;
148 next_tvb = tvb_new_subset_length(tvb, offset, length);
149 call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, urb);
150 offset += length;
152 proto_tree_add_item(main_tree, hf_data_checksum, tvb, offset, 1, ENC_BIG_ENDIAN);
153 checksum = tvb_get_uint8(tvb, offset);
154 while (length) {
155 checksum += tvb_get_uint8(tvb, offset - length);
156 length -= 1;
158 if (checksum != 0) {
159 proto_tree_add_expert(main_tree, pinfo, &ei_invalid_data_checksum, tvb, offset, 1);
161 offset += 1;
164 length = 0;
165 if (tvb_captured_length_remaining(tvb, offset) == 1) {
166 length = 1;
167 } else while (tvb_captured_length_remaining(tvb, offset + length) >= 2 && tvb_get_ntohs(tvb, offset + length) != 0x00FF) {
168 length += 1;
170 if (length) {
171 proto_tree_add_item(main_tree, hf_postable, tvb, offset, length, ENC_NA);
172 offset += length;
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);
180 return offset;
183 void
184 proto_register_pn532_hci(void)
186 module_t *module;
187 expert_module_t *expert_module;
189 static hf_register_info hf[] = {
190 { &hf_preamble,
191 { "Preamble", "pn532_hci.preamble",
192 FT_BYTES, BASE_NONE, NULL, 0x00,
193 NULL, HFILL }
195 { &hf_start_code,
196 { "Start Code", "pn532_hci.start_code",
197 FT_UINT16, BASE_HEX, NULL, 0x00,
198 NULL, HFILL }
200 { &hf_packet_code,
201 { "Packet Code", "pn532_hci.packet_code",
202 FT_UINT16, BASE_HEX, VALS(packet_code_vals), 0x00,
203 NULL, HFILL }
205 { &hf_length,
206 { "Length", "pn532_hci.length",
207 FT_UINT8, BASE_DEC, NULL, 0x00,
208 NULL, HFILL }
210 { &hf_extended_length,
211 { "Extended Length", "pn532_hci.extended_length",
212 FT_UINT16, BASE_DEC, NULL, 0x00,
213 NULL, HFILL }
215 { &hf_length_checksum,
216 { "Length Checksum", "pn532_hci.length_checksum",
217 FT_UINT8, BASE_HEX, NULL, 0x00,
218 NULL, HFILL }
220 { &hf_length_checksum_status,
221 { "Length Checksum Status", "pn532_hci.length_checksum.status",
222 FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x00,
223 NULL, HFILL }
225 { &hf_data_checksum,
226 { "Data Checksum", "pn532_hci.data_checksum",
227 FT_UINT8, BASE_HEX, NULL, 0x00,
228 NULL, HFILL }
230 { &hf_data_checksum_status,
231 { "Data Checksum Status", "pn532_hci.data_checksum.status",
232 FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x00,
233 NULL, HFILL }
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,
238 NULL, HFILL }
240 { &hf_postable,
241 { "Postamble", "pn532_hci.postamble",
242 FT_BYTES, BASE_NONE, NULL, 0x00,
243 NULL, HFILL }
245 { &hf_ignored,
246 { "Ignored", "pn532_hci.ignored",
247 FT_BYTES, BASE_NONE, NULL, 0x00,
248 NULL, HFILL }
252 static int *ett[] = {
253 &ett_pn532_hci
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.");
275 void
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
289 * Local variables:
290 * c-basic-offset: 4
291 * tab-width: 8
292 * indent-tabs-mode: nil
293 * End:
295 * vi: set shiftwidth=4 tabstop=8 expandtab:
296 * :indentSize=4:tabSize=8:noTabs=true: