HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / dissectors / packet-rfid-pn532-hci.c
blobd1218291f3e0f6300a1fc6b5b5057a2984273a5a
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 * $Id$
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See thehf_class
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "config.h"
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/expert.h>
34 #include <epan/wmem/wmem.h>
36 #include "packet-usb.h"
38 static int proto_pn532_hci = -1;
39 static int hf_preamble = -1;
40 static int hf_start_code = -1;
41 static int hf_length = -1;
42 static int hf_length_checksum = -1;
43 static int hf_extended_length = -1;
44 static int hf_packet_code = -1;
45 static int hf_postable = -1;
46 static int hf_specific_application_level_error_code = -1;
47 static int hf_data_checksum = -1;
48 static int hf_ignored = -1;
50 static gint ett_pn532_hci = -1;
52 static expert_field ei_invalid_length_checksum = EI_INIT;
53 static expert_field ei_invalid_data_checksum = EI_INIT;
55 static dissector_handle_t pn532_handle;
56 static dissector_handle_t pn532_hci_handle;
58 static const value_string packet_code_vals[] = {
59 { 0x00FF, "ACK Frame" },
60 { 0x01FF, "Error Frame" },
61 { 0xFF00, "NACK Frame" },
62 { 0xFFFF, "Extended Information Frame" },
63 { 0, NULL }
66 void proto_register_pn532_hci(void);
67 void proto_reg_handoff_pn532_hci(void);
69 static gint
70 dissect_pn532_hci(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
72 proto_item *main_item;
73 proto_tree *main_tree;
74 gint offset = 0;
75 tvbuff_t *next_tvb;
76 guint16 packet_code;
77 guint16 length;
78 guint8 checksum;
79 usb_conv_info_t *usb_conv_info = (usb_conv_info_t *)data;
81 DISSECTOR_ASSERT(usb_conv_info);
83 length = tvb_length_remaining(tvb, offset);
84 if (length < 6) return offset;
86 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PN532_HCI");
87 col_clear(pinfo->cinfo, COL_INFO);
89 main_item = proto_tree_add_item(tree, proto_pn532_hci, tvb, offset, -1, ENC_NA);
90 main_tree = proto_item_add_subtree(main_item, ett_pn532_hci);
92 length = 0;
93 while (tvb_length_remaining(tvb, length) >= 2 && tvb_get_ntohs(tvb, length) != 0x00FF) {
94 length += 1;
96 if (length) {
97 proto_tree_add_item(main_tree, hf_preamble, tvb, offset, length, ENC_NA);
98 offset += length;
101 proto_tree_add_item(main_tree, hf_start_code, tvb, offset, 2, ENC_BIG_ENDIAN);
102 offset += 2;
104 packet_code = tvb_get_ntohs(tvb, offset);
105 if (packet_code == 0x00FF) { /* ACK Frame */
106 col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));
108 proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
109 offset += 2;
110 } else if (packet_code == 0xFF00) { /* NACK Frame */
111 col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));
113 proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
114 offset += 2;
115 } else if (packet_code == 0x01FF) { /* Error Frame */
116 col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));
118 proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
119 offset += 2;
121 proto_tree_add_item(main_tree, hf_specific_application_level_error_code, tvb, offset, 1, ENC_NA);
122 offset += 1;
123 } else if (packet_code == 0xFFFF) { /* Extended Information Frame */
124 col_set_str(pinfo->cinfo, COL_INFO, "Extended Information Frame");
126 proto_tree_add_item(main_tree, hf_extended_length, tvb, offset, 2, ENC_BIG_ENDIAN);
127 length = tvb_get_ntohs(tvb, offset);
128 offset += 2;
130 proto_tree_add_item(main_tree, hf_length_checksum, tvb, offset, 1, ENC_NA);
131 checksum = (length >> 8) + (length & 0xFF) + tvb_get_guint8(tvb, offset);
132 if (checksum != 0) {
133 proto_tree_add_expert(main_tree, pinfo, &ei_invalid_length_checksum, tvb, offset, 1);
135 offset += 1;
137 next_tvb = tvb_new_subset(tvb, offset, length, length);
138 call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, usb_conv_info);
139 offset += length;
141 proto_tree_add_item(main_tree, hf_data_checksum, tvb, offset, 1, ENC_NA);
142 checksum = tvb_get_guint8(tvb, offset);
143 while (length) {
144 checksum += tvb_get_guint8(tvb, offset - length);
145 length -= 1;
147 if (checksum != 0) {
148 proto_tree_add_expert(main_tree, pinfo, &ei_invalid_data_checksum, tvb, offset, 1);
150 offset += 1;
151 } else { /* Normal Information Frame */
152 col_set_str(pinfo->cinfo, COL_INFO, "Normal Information Frame");
154 proto_tree_add_item(main_tree, hf_length, tvb, offset, 1, ENC_NA);
155 length = tvb_get_guint8(tvb, offset);
156 offset += 1;
158 proto_tree_add_item(main_tree, hf_length_checksum, tvb, offset, 1, ENC_NA);
159 checksum = length + tvb_get_guint8(tvb, offset);
160 if (checksum != 0)
161 proto_tree_add_expert(main_tree, pinfo, &ei_invalid_length_checksum, tvb, offset, 1);
162 offset += 1;
164 next_tvb = tvb_new_subset(tvb, offset, length, length);
165 call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, usb_conv_info);
166 offset += length;
168 proto_tree_add_item(main_tree, hf_data_checksum, tvb, offset, 1, ENC_NA);
169 checksum = tvb_get_guint8(tvb, offset);
170 while (length) {
171 checksum += tvb_get_guint8(tvb, offset - length);
172 length -= 1;
174 if (checksum != 0) {
175 proto_tree_add_expert(main_tree, pinfo, &ei_invalid_data_checksum, tvb, offset, 1);
177 offset += 1;
180 length = 0;
181 if (tvb_length_remaining(tvb, offset) == 1) {
182 length = 1;
183 } else while (tvb_length_remaining(tvb, offset + length) >= 2 && tvb_get_ntohs(tvb, offset + length) != 0x00FF) {
184 length += 1;
186 if (length) {
187 proto_tree_add_item(main_tree, hf_postable, tvb, offset, length, ENC_NA);
188 offset += length;
191 if (tvb_length_remaining(tvb, offset)) {
192 proto_tree_add_item(main_tree, hf_ignored, tvb, offset, tvb_length_remaining(tvb, offset), ENC_NA);
193 offset += tvb_length_remaining(tvb, offset);
196 return offset;
199 void
200 proto_register_pn532_hci(void)
202 module_t *module;
203 expert_module_t *expert_module;
205 static hf_register_info hf[] = {
206 { &hf_preamble,
207 { "Preamble", "pn532_hci.preamble",
208 FT_BYTES, BASE_NONE, NULL, 0x00,
209 NULL, HFILL }
211 { &hf_start_code,
212 { "Start Code", "pn532_hci.start_code",
213 FT_UINT16, BASE_HEX, NULL, 0x00,
214 NULL, HFILL }
216 { &hf_packet_code,
217 { "Packet Code", "pn532_hci.packet_code",
218 FT_UINT16, BASE_HEX, VALS(packet_code_vals), 0x00,
219 NULL, HFILL }
221 { &hf_length,
222 { "Length", "pn532_hci.length",
223 FT_UINT8, BASE_DEC, NULL, 0x00,
224 NULL, HFILL }
226 { &hf_extended_length,
227 { "Extended Length", "pn532_hci.extended_length",
228 FT_UINT16, BASE_DEC, NULL, 0x00,
229 NULL, HFILL }
231 { &hf_length_checksum,
232 { "Length Checksum", "pn532_hci.length_checksum",
233 FT_UINT8, BASE_HEX, NULL, 0x00,
234 NULL, HFILL }
236 { &hf_data_checksum,
237 { "Data Checksum", "pn532_hci.data_checksum",
238 FT_UINT8, BASE_HEX, NULL, 0x00,
239 NULL, HFILL }
241 { &hf_specific_application_level_error_code,
242 { "Specific Application Level Error Code","pn532_hci.specific_application_level_error_code",
243 FT_UINT8, BASE_HEX, NULL, 0x00,
244 NULL, HFILL }
246 { &hf_postable,
247 { "Postamble", "pn532_hci.postamble",
248 FT_BYTES, BASE_NONE, NULL, 0x00,
249 NULL, HFILL }
251 { &hf_ignored,
252 { "Ignored", "pn532_hci.ignored",
253 FT_BYTES, BASE_NONE, NULL, 0x00,
254 NULL, HFILL }
258 static gint *ett[] = {
259 &ett_pn532_hci
262 static ei_register_info ei[] = {
263 { &ei_invalid_length_checksum, { "pn532_hci.expert.invalid_length_checksum", PI_PROTOCOL, PI_WARN, "Invalid Length Checksum", EXPFILL }},
264 { &ei_invalid_data_checksum, { "pn532_hci.expert.invalid_data_checksum", PI_PROTOCOL, PI_WARN, "Invalid Data Checksum", EXPFILL }},
267 proto_pn532_hci = proto_register_protocol("NXP PN532 HCI", "PN532_HCI", "pn532_hci");
268 new_register_dissector("pn532_hci", dissect_pn532_hci, proto_pn532_hci);
270 proto_register_field_array(proto_pn532_hci, hf, array_length(hf));
271 proto_register_subtree_array(ett, array_length(ett));
272 expert_module = expert_register_protocol(proto_pn532_hci);
273 expert_register_field_array(expert_module, ei, array_length(ei));
275 module = prefs_register_protocol(proto_pn532_hci, NULL);
276 prefs_register_static_text_preference(module, "version",
277 "PN532 HCI protocol version is based on: \"UM0701-02; PN532 User Manual\"",
278 "Version of protocol supported by this dissector.");
281 void
282 proto_reg_handoff_pn532_hci(void)
284 pn532_handle = find_dissector("pn532");
285 pn532_hci_handle = find_dissector("pn532_hci");
287 dissector_add_uint("usb.product", (0x04e6 << 16) | 0x5591, pn532_hci_handle);
289 dissector_add_handle("usb.device", pn532_hci_handle);
293 * Editor modelines - http://www.wireshark.org/tools/modelines.html
295 * Local variables:
296 * c-basic-offset: 4
297 * tab-width: 8
298 * indent-tabs-mode: nil
299 * End:
301 * vi: set shiftwidth=4 tabstop=8 expandtab:
302 * :indentSize=4:tabSize=8:noTabs=true: