Coverage Report

Created: 2026-06-30 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-rfid-pn532-hci.c
Line
Count
Source
1
/* packet-rfid-pn532-hci.c
2
 * Routines for NXP PN532 HCI Protocol
3
 *
4
 * http://www.nxp.com/documents/user_manual/141520.pdf
5
 *
6
 * Copyright 2013, Michal Labedzki for Tieto Corporation
7
 *
8
 * Wireshark - Network traffic analyzer
9
 * By Gerald Combs <gerald@wireshark.org>
10
 * Copyright 1998 Gerald Combs
11
 *
12
 * SPDX-License-Identifier: GPL-2.0-or-later
13
 */
14
15
#include "config.h"
16
17
#include <epan/packet.h>
18
#include <epan/prefs.h>
19
#include <epan/expert.h>
20
#include "packet-usb.h"
21
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;
35
36
static int ett_pn532_hci;
37
38
static expert_field ei_invalid_length_checksum;
39
static expert_field ei_invalid_data_checksum;
40
41
static dissector_handle_t  pn532_handle;
42
static dissector_handle_t  pn532_hci_handle;
43
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 }
50
};
51
52
void proto_register_pn532_hci(void);
53
void proto_reg_handoff_pn532_hci(void);
54
55
static int
56
dissect_pn532_hci(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
57
0
{
58
0
    proto_item      *main_item;
59
0
    proto_tree      *main_tree;
60
0
    int              offset = 0;
61
0
    tvbuff_t        *next_tvb;
62
0
    uint16_t         packet_code;
63
0
    uint16_t         length;
64
0
    uint8_t          checksum;
65
0
    urb_info_t      *urb;
66
67
    /* Reject the packet if data is NULL */
68
0
    if (data == NULL)
69
0
        return 0;
70
0
    urb = (urb_info_t *)data;
71
72
0
    length = tvb_captured_length_remaining(tvb, offset);
73
0
    if (length < 6) return offset;
74
75
0
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "PN532_HCI");
76
0
    col_clear(pinfo->cinfo, COL_INFO);
77
78
0
    main_item = proto_tree_add_item(tree, proto_pn532_hci, tvb, offset, -1, ENC_NA);
79
0
    main_tree = proto_item_add_subtree(main_item, ett_pn532_hci);
80
81
0
    length = 0;
82
0
    while (tvb_captured_length_remaining(tvb, length) >= 2 && tvb_get_ntohs(tvb, length) != 0x00FF) {
83
0
        length += 1;
84
0
    }
85
0
    if (length) {
86
0
        proto_tree_add_item(main_tree, hf_preamble, tvb, offset, length, ENC_NA);
87
0
        offset += length;
88
0
    }
89
90
0
    proto_tree_add_item(main_tree, hf_start_code, tvb, offset, 2, ENC_BIG_ENDIAN);
91
0
    offset += 2;
92
93
0
    packet_code = tvb_get_ntohs(tvb, offset);
94
0
    if (packet_code == 0x00FF) { /* ACK Frame */
95
0
        col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));
96
97
0
        proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
98
0
        offset += 2;
99
0
    } else if (packet_code == 0xFF00) { /* NACK Frame */
100
0
        col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));
101
102
0
        proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
103
0
        offset += 2;
104
0
    } else if (packet_code == 0x01FF) { /* Error Frame */
105
0
        col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));
106
107
0
        proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
108
0
        offset += 2;
109
110
0
        proto_tree_add_item(main_tree, hf_specific_application_level_error_code, tvb, offset, 1, ENC_BIG_ENDIAN);
111
0
        offset += 1;
112
0
    } else if (packet_code == 0xFFFF) { /* Extended Information Frame */
113
0
        col_set_str(pinfo->cinfo, COL_INFO, "Extended Information Frame");
114
115
0
        proto_tree_add_item_ret_uint16(main_tree, hf_extended_length, tvb, offset, 2, ENC_BIG_ENDIAN, &length);
116
0
        offset += 2;
117
118
0
        checksum = (length >> 8) + (length & 0xFF) + tvb_get_uint8(tvb, offset);
119
0
        proto_tree_add_checksum(main_tree, tvb, offset, hf_length_checksum, hf_length_checksum_status, &ei_invalid_length_checksum,
120
0
                            pinfo, checksum, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
121
0
        offset += 1;
122
123
0
        next_tvb = tvb_new_subset_length(tvb, offset, length);
124
0
        call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, urb);
125
0
        offset += length;
126
127
0
        checksum = tvb_get_uint8(tvb, offset);
128
0
        while (length) {
129
0
            checksum += tvb_get_uint8(tvb, offset - length);
130
0
            length -= 1;
131
0
        }
132
0
        proto_tree_add_checksum(main_tree, tvb, offset, hf_data_checksum, hf_data_checksum_status, &ei_invalid_data_checksum, pinfo, 0,
133
0
                            ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
134
0
        offset += 1;
135
0
    } else { /* Normal Information Frame */
136
0
        col_set_str(pinfo->cinfo, COL_INFO, "Normal Information Frame");
137
138
0
        proto_tree_add_item_ret_uint16(main_tree, hf_length, tvb, offset, 1, ENC_BIG_ENDIAN, &length);
139
0
        offset += 1;
140
141
0
        checksum = length + tvb_get_uint8(tvb, offset);
142
0
        proto_tree_add_checksum(main_tree, tvb, offset, hf_length_checksum, hf_length_checksum_status, &ei_invalid_length_checksum,
143
0
                            pinfo, checksum, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
144
0
        offset += 1;
145
146
0
        next_tvb = tvb_new_subset_length(tvb, offset, length);
147
0
        call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, urb);
148
0
        offset += length;
149
150
0
        proto_tree_add_item_ret_uint8(main_tree, hf_data_checksum, tvb, offset, 1, ENC_BIG_ENDIAN, &checksum);
151
0
        while (length) {
152
0
            checksum += tvb_get_uint8(tvb, offset - length);
153
0
            length -= 1;
154
0
        }
155
0
        if (checksum != 0) {
156
0
            proto_tree_add_expert(main_tree, pinfo, &ei_invalid_data_checksum, tvb, offset, 1);
157
0
        }
158
0
        offset += 1;
159
0
    }
160
161
0
    length = 0;
162
0
    if (tvb_captured_length_remaining(tvb, offset) == 1) {
163
0
        length = 1;
164
0
    } else while (tvb_captured_length_remaining(tvb, offset + length) >= 2 && tvb_get_ntohs(tvb, offset + length) != 0x00FF) {
165
0
        length += 1;
166
0
    }
167
0
    if (length) {
168
0
        proto_tree_add_item(main_tree, hf_postable, tvb, offset, length, ENC_NA);
169
0
        offset += length;
170
0
    }
171
172
0
    if (tvb_captured_length_remaining(tvb, offset)) {
173
0
        proto_tree_add_item(main_tree, hf_ignored, tvb, offset, tvb_captured_length_remaining(tvb, offset), ENC_NA);
174
0
        offset += tvb_captured_length_remaining(tvb, offset);
175
0
    }
176
177
0
    return offset;
178
0
}
179
180
void
181
proto_register_pn532_hci(void)
182
14
{
183
14
    module_t         *module;
184
14
    expert_module_t  *expert_module;
185
186
14
    static hf_register_info hf[] = {
187
14
        { &hf_preamble,
188
14
            { "Preamble",                        "pn532_hci.preamble",
189
14
            FT_BYTES, BASE_NONE, NULL, 0x00,
190
14
            NULL, HFILL }
191
14
        },
192
14
        { &hf_start_code,
193
14
            { "Start Code",                      "pn532_hci.start_code",
194
14
            FT_UINT16, BASE_HEX, NULL, 0x00,
195
14
            NULL, HFILL }
196
14
        },
197
14
        { &hf_packet_code,
198
14
            { "Packet Code",                     "pn532_hci.packet_code",
199
14
            FT_UINT16, BASE_HEX, VALS(packet_code_vals), 0x00,
200
14
            NULL, HFILL }
201
14
        },
202
14
        { &hf_length,
203
14
            { "Length",                          "pn532_hci.length",
204
14
            FT_UINT8, BASE_DEC, NULL, 0x00,
205
14
            NULL, HFILL }
206
14
        },
207
14
        { &hf_extended_length,
208
14
            { "Extended Length",                 "pn532_hci.extended_length",
209
14
            FT_UINT16, BASE_DEC, NULL, 0x00,
210
14
            NULL, HFILL }
211
14
        },
212
14
        { &hf_length_checksum,
213
14
            { "Length Checksum",                 "pn532_hci.length_checksum",
214
14
            FT_UINT8, BASE_HEX, NULL, 0x00,
215
14
            NULL, HFILL }
216
14
        },
217
14
        { &hf_length_checksum_status,
218
14
            { "Length Checksum Status",            "pn532_hci.length_checksum.status",
219
14
            FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x00,
220
14
            NULL, HFILL }
221
14
        },
222
14
        { &hf_data_checksum,
223
14
            { "Data Checksum",                   "pn532_hci.data_checksum",
224
14
            FT_UINT8, BASE_HEX, NULL, 0x00,
225
14
            NULL, HFILL }
226
14
        },
227
14
        { &hf_data_checksum_status,
228
14
            { "Data Checksum Status",            "pn532_hci.data_checksum.status",
229
14
            FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x00,
230
14
            NULL, HFILL }
231
14
        },
232
14
        { &hf_specific_application_level_error_code,
233
14
            { "Specific Application Level Error Code","pn532_hci.specific_application_level_error_code",
234
14
            FT_UINT8, BASE_HEX, NULL, 0x00,
235
14
            NULL, HFILL }
236
14
        },
237
14
        { &hf_postable,
238
14
            { "Postamble",                       "pn532_hci.postamble",
239
14
            FT_BYTES, BASE_NONE, NULL, 0x00,
240
14
            NULL, HFILL }
241
14
        },
242
14
        { &hf_ignored,
243
14
            { "Ignored",                         "pn532_hci.ignored",
244
14
            FT_BYTES, BASE_NONE, NULL, 0x00,
245
14
            NULL, HFILL }
246
14
        }
247
14
    };
248
249
14
    static int *ett[] = {
250
14
        &ett_pn532_hci
251
14
    };
252
253
14
    static ei_register_info ei[] = {
254
14
        { &ei_invalid_length_checksum, { "pn532_hci.expert.invalid_length_checksum", PI_PROTOCOL, PI_WARN, "Invalid Length Checksum", EXPFILL }},
255
14
        { &ei_invalid_data_checksum,   { "pn532_hci.expert.invalid_data_checksum", PI_PROTOCOL, PI_WARN, "Invalid Data Checksum", EXPFILL }},
256
14
    };
257
258
14
    proto_pn532_hci = proto_register_protocol("NXP PN532 HCI", "PN532_HCI", "pn532_hci");
259
14
    pn532_hci_handle = register_dissector("pn532_hci", dissect_pn532_hci, proto_pn532_hci);
260
261
14
    proto_register_field_array(proto_pn532_hci, hf, array_length(hf));
262
14
    proto_register_subtree_array(ett, array_length(ett));
263
14
    expert_module = expert_register_protocol(proto_pn532_hci);
264
14
    expert_register_field_array(expert_module, ei, array_length(ei));
265
266
14
    module = prefs_register_protocol(proto_pn532_hci, NULL);
267
14
    prefs_register_static_text_preference(module, "version",
268
14
            "PN532 HCI protocol version is based on: \"UM0701-02; PN532 User Manual\"",
269
14
            "Version of protocol supported by this dissector.");
270
14
}
271
272
void
273
proto_reg_handoff_pn532_hci(void)
274
14
{
275
14
    pn532_handle = find_dissector_add_dependency("pn532", proto_pn532_hci);
276
277
14
    dissector_add_uint("usb.product", (0x04e6 << 16) | 0x5591, pn532_hci_handle);
278
279
14
    dissector_add_for_decode_as("usb.device", pn532_hci_handle);
280
14
    dissector_add_for_decode_as("usb.protocol", pn532_hci_handle);
281
14
}
282
283
/*
284
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
285
 *
286
 * Local variables:
287
 * c-basic-offset: 4
288
 * tab-width: 8
289
 * indent-tabs-mode: nil
290
 * End:
291
 *
292
 * vi: set shiftwidth=4 tabstop=8 expandtab:
293
 * :indentSize=4:tabSize=8:noTabs=true:
294
 */