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
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.
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" },
66 void proto_register_pn532_hci(void);
67 void proto_reg_handoff_pn532_hci(void);
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
;
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
);
93 while (tvb_length_remaining(tvb
, length
) >= 2 && tvb_get_ntohs(tvb
, length
) != 0x00FF) {
97 proto_tree_add_item(main_tree
, hf_preamble
, tvb
, offset
, length
, ENC_NA
);
101 proto_tree_add_item(main_tree
, hf_start_code
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
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
);
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
);
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
);
121 proto_tree_add_item(main_tree
, hf_specific_application_level_error_code
, tvb
, offset
, 1, ENC_NA
);
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
);
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
);
133 proto_tree_add_expert(main_tree
, pinfo
, &ei_invalid_length_checksum
, tvb
, 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
);
141 proto_tree_add_item(main_tree
, hf_data_checksum
, tvb
, offset
, 1, ENC_NA
);
142 checksum
= tvb_get_guint8(tvb
, offset
);
144 checksum
+= tvb_get_guint8(tvb
, offset
- length
);
148 proto_tree_add_expert(main_tree
, pinfo
, &ei_invalid_data_checksum
, tvb
, 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
);
158 proto_tree_add_item(main_tree
, hf_length_checksum
, tvb
, offset
, 1, ENC_NA
);
159 checksum
= length
+ tvb_get_guint8(tvb
, offset
);
161 proto_tree_add_expert(main_tree
, pinfo
, &ei_invalid_length_checksum
, tvb
, 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
);
168 proto_tree_add_item(main_tree
, hf_data_checksum
, tvb
, offset
, 1, ENC_NA
);
169 checksum
= tvb_get_guint8(tvb
, offset
);
171 checksum
+= tvb_get_guint8(tvb
, offset
- length
);
175 proto_tree_add_expert(main_tree
, pinfo
, &ei_invalid_data_checksum
, tvb
, offset
, 1);
181 if (tvb_length_remaining(tvb
, offset
) == 1) {
183 } else while (tvb_length_remaining(tvb
, offset
+ length
) >= 2 && tvb_get_ntohs(tvb
, offset
+ length
) != 0x00FF) {
187 proto_tree_add_item(main_tree
, hf_postable
, tvb
, offset
, length
, ENC_NA
);
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
);
200 proto_register_pn532_hci(void)
203 expert_module_t
*expert_module
;
205 static hf_register_info hf
[] = {
207 { "Preamble", "pn532_hci.preamble",
208 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
212 { "Start Code", "pn532_hci.start_code",
213 FT_UINT16
, BASE_HEX
, NULL
, 0x00,
217 { "Packet Code", "pn532_hci.packet_code",
218 FT_UINT16
, BASE_HEX
, VALS(packet_code_vals
), 0x00,
222 { "Length", "pn532_hci.length",
223 FT_UINT8
, BASE_DEC
, NULL
, 0x00,
226 { &hf_extended_length
,
227 { "Extended Length", "pn532_hci.extended_length",
228 FT_UINT16
, BASE_DEC
, NULL
, 0x00,
231 { &hf_length_checksum
,
232 { "Length Checksum", "pn532_hci.length_checksum",
233 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
237 { "Data Checksum", "pn532_hci.data_checksum",
238 FT_UINT8
, BASE_HEX
, NULL
, 0x00,
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,
247 { "Postamble", "pn532_hci.postamble",
248 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
252 { "Ignored", "pn532_hci.ignored",
253 FT_BYTES
, BASE_NONE
, NULL
, 0x00,
258 static gint
*ett
[] = {
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.");
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
298 * indent-tabs-mode: nil
301 * vi: set shiftwidth=4 tabstop=8 expandtab:
302 * :indentSize=4:tabSize=8:noTabs=true: