Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-hci_h4.c
1 /* packet-hci_h4.c
2  * Routines for the Bluetooth HCI H4 dissection
3  *
4  * Copyright 2002, Christoph Scholz <scholz@cs.uni-bonn.de>
5  *  From: http://affix.sourceforge.net/archive/ethereal_affix-3.patch
6  *
7  * Refactored for wireshark checkin
8  *   Ronnie Sahlberg 2006
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
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.
18  *
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 the
22  * GNU General Public License for more details.
23  *
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.
27  */
28
29 #include "config.h"
30
31 #include <epan/packet.h>
32 #include <wiretap/wtap.h>
33 #include <epan/wmem/wmem.h>
34 #include <wiretap/wtap.h>
35
36 #include "packet-bluetooth-hci.h"
37
38 static int proto_hci_h4 = -1;
39 static int hf_hci_h4_type = -1;
40 static int hf_hci_h4_direction = -1;
41
42 static gint ett_hci_h4 = -1;
43
44 static dissector_handle_t hci_h4_handle;
45
46 static dissector_table_t hci_h4_table;
47 static dissector_handle_t data_handle;
48
49 static wmem_tree_t *chandle_to_bdaddr_table = NULL;
50 static wmem_tree_t *bdaddr_to_name_table    = NULL;
51 static wmem_tree_t *localhost_name          = NULL;
52 static wmem_tree_t *localhost_bdaddr        = NULL;
53
54 static const value_string hci_h4_type_vals[] = {
55     {HCI_H4_TYPE_CMD, "HCI Command"},
56     {HCI_H4_TYPE_ACL, "ACL Data"},
57     {HCI_H4_TYPE_SCO, "SCO Data"},
58     {HCI_H4_TYPE_EVT, "HCI Event"},
59     {0, NULL }
60 };
61 static const value_string hci_h4_direction_vals[] = {
62     {P2P_DIR_SENT,        "Sent"},
63     {P2P_DIR_RECV,        "Rcvd"},
64     {P2P_DIR_UNKNOWN,     "Unspecified"},
65     {0, NULL}
66 };
67
68 void proto_register_hci_h4(void);
69 void proto_reg_handoff_hci_h4(void);
70
71 static gint
72 dissect_hci_h4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
73 {
74     guint8      type;
75     tvbuff_t    *next_tvb;
76     proto_item  *ti = NULL;
77     proto_tree  *hci_h4_tree = NULL;
78     hci_data_t  *hci_data;
79
80     col_set_str(pinfo->cinfo, COL_PROTOCOL, "HCI H4");
81     switch (pinfo->p2p_dir) {
82
83     case P2P_DIR_SENT:
84         col_add_fstr(pinfo->cinfo, COL_INFO, "Sent ");
85         break;
86
87     case P2P_DIR_RECV:
88         col_add_fstr(pinfo->cinfo, COL_INFO, "Rcvd ");
89         break;
90
91     case P2P_DIR_UNKNOWN:
92         break;
93
94     default:
95         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown direction %d ",
96                 pinfo->p2p_dir);
97         break;
98     }
99
100     type = tvb_get_guint8(tvb, 0);
101
102     if (tree) {
103         ti = proto_tree_add_item(tree, proto_hci_h4, tvb, 0, 1, ENC_NA);
104         hci_h4_tree = proto_item_add_subtree(ti, ett_hci_h4);
105     }
106
107     hci_data = (hci_data_t *) wmem_new(wmem_packet_scope(), hci_data_t);
108     if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
109         hci_data->interface_id = pinfo->phdr->interface_id;
110     else
111         hci_data->interface_id = HCI_INTERFACE_DEFAULT;
112     hci_data->adapter_id = HCI_ADAPTER_DEFAULT;
113     hci_data->chandle_to_bdaddr_table = chandle_to_bdaddr_table;
114     hci_data->bdaddr_to_name_table = bdaddr_to_name_table;
115     hci_data->localhost_bdaddr = localhost_bdaddr;
116     hci_data->localhost_name = localhost_name;
117
118     pinfo->ptype = PT_BLUETOOTH;
119
120     ti = proto_tree_add_uint(hci_h4_tree, hf_hci_h4_direction, tvb, 0, 0, pinfo->p2p_dir);
121     PROTO_ITEM_SET_GENERATED(ti);
122
123     proto_tree_add_item(hci_h4_tree, hf_hci_h4_type,
124         tvb, 0, 1, ENC_LITTLE_ENDIAN);
125     col_append_fstr(pinfo->cinfo, COL_INFO, "%s",
126             val_to_str(type, hci_h4_type_vals, "Unknown HCI packet type 0x%02x"));
127
128     next_tvb = tvb_new_subset_remaining(tvb, 1);
129     if (!dissector_try_uint_new(hci_h4_table, type, next_tvb, pinfo, tree, TRUE, hci_data)) {
130         call_dissector(data_handle, next_tvb, pinfo, tree);
131     }
132
133     return tvb_length(tvb);
134 }
135
136
137 void
138 proto_register_hci_h4(void)
139 {
140     static hf_register_info hf[] = {
141         { &hf_hci_h4_type,
142             { "HCI Packet Type",           "hci_h4.type",
143             FT_UINT8, BASE_HEX, VALS(hci_h4_type_vals), 0x0,
144             NULL, HFILL }
145         },
146         { &hf_hci_h4_direction,
147             { "Direction",                 "hci_h4.direction",
148             FT_UINT8, BASE_HEX, VALS(hci_h4_direction_vals), 0x0,
149             "HCI Packet Direction Sent/Rcvd", HFILL }
150         }
151     };
152
153     static gint *ett[] = {
154         &ett_hci_h4,
155     };
156
157     proto_hci_h4 = proto_register_protocol("Bluetooth HCI H4",
158             "HCI_H4", "hci_h4");
159
160     hci_h4_handle = new_register_dissector("hci_h4", dissect_hci_h4, proto_hci_h4);
161
162     proto_register_field_array(proto_hci_h4, hf, array_length(hf));
163     proto_register_subtree_array(ett, array_length(ett));
164
165     hci_h4_table = register_dissector_table("hci_h4.type",
166             "HCI H4 pdu type", FT_UINT8, BASE_HEX);
167
168     chandle_to_bdaddr_table = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope()); /* adapter, chandle: bdaddr */
169     bdaddr_to_name_table = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope()); /* bdaddr: name */
170     localhost_bdaddr = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope()); /* adapter, frame: bdaddr */
171     localhost_name = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope()); /* adapter, frame: name */
172 }
173
174 void
175 proto_reg_handoff_hci_h4(void)
176 {
177     data_handle = find_dissector("data");
178
179     dissector_add_uint("wtap_encap", WTAP_ENCAP_BLUETOOTH_H4, hci_h4_handle);
180     dissector_add_uint("wtap_encap", WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR, hci_h4_handle);
181 }
182
183 /*
184  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
185  *
186  * Local variables:
187  * c-basic-offset: 4
188  * tab-width: 8
189  * indent-tabs-mode: nil
190  * End:
191  *
192  * vi: set shiftwidth=4 tabstop=8 expandtab:
193  * :indentSize=4:tabSize=8:noTabs=true:
194  */