Revert "Fixup: tvb_* -> tvb_captured"
[metze/wireshark/wip.git] / epan / dissectors / packet-bt3ds.c
1 /* packet-bt3ds.c
2  * Routines for Bluetooth 3DS dissection
3  *
4  * Copyright 2013, Michal Labedzki for Tieto Corporation
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <epan/packet.h>
28 #include <epan/prefs.h>
29 #include <epan/expert.h>
30
31 #include "packet-btl2cap.h"
32 #include "packet-btsdp.h"
33
34 static int proto_bt3ds = -1;
35
36 static int hf_message_opcode                                               = -1;
37 static int hf_association_notification                                     = -1;
38 static int hf_user_request_for_battery_level_display                       = -1;
39 static int hf_reserved                                                     = -1;
40 static int hf_battery_level                                                = -1;
41
42 static expert_field ei_message_opcode_reserved                        = EI_INIT;
43 static expert_field ei_reserved                                       = EI_INIT;
44 static expert_field ei_battery_level_reserved                         = EI_INIT;
45 static expert_field ei_unexpected_data                                = EI_INIT;
46
47 static gint ett_bt3ds                                                      = -1;
48
49 static dissector_handle_t b3ds_handle;
50
51 static const value_string message_opcode_vals[] = {
52     { 0x00,   "3DG Connection Announcement" },
53     { 0, NULL }
54 };
55
56 void proto_register_bt3ds(void);
57 void proto_reg_handoff_bt3ds(void);
58
59 static gint
60 dissect_bt3ds(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
61 {
62     proto_item     *main_item;
63     proto_tree     *main_tree;
64     proto_item     *sub_item;
65     gint            offset = 0;
66     guint8          value;
67
68     main_item = proto_tree_add_item(tree, proto_bt3ds, tvb, offset, -1, ENC_NA);
69     main_tree = proto_item_add_subtree(main_item, ett_bt3ds);
70
71     col_set_str(pinfo->cinfo, COL_PROTOCOL, "3DS");
72
73     switch (pinfo->p2p_dir) {
74         case P2P_DIR_SENT:
75             col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
76             break;
77         case P2P_DIR_RECV:
78             col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
79             break;
80         default:
81             col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown direction %d ",
82                 pinfo->p2p_dir);
83             break;
84     }
85
86     sub_item = proto_tree_add_item(main_tree, hf_message_opcode, tvb, offset, 1, ENC_BIG_ENDIAN);
87     value = tvb_get_guint8(tvb, offset);
88     if (value > 0)
89         expert_add_info(pinfo, sub_item, &ei_message_opcode_reserved);
90     offset += 1;
91
92     col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str_const(value, message_opcode_vals, "Unknown"));
93
94     sub_item = proto_tree_add_item(main_tree, hf_reserved, tvb, offset, 1, ENC_BIG_ENDIAN);
95     proto_tree_add_item(main_tree, hf_user_request_for_battery_level_display, tvb, offset, 1, ENC_BIG_ENDIAN);
96     proto_tree_add_item(main_tree, hf_association_notification, tvb, offset, 1, ENC_BIG_ENDIAN);
97     value = tvb_get_guint8(tvb, offset) >> 2;
98     if (value != 0)
99         expert_add_info(pinfo, sub_item, &ei_reserved);
100     offset += 1;
101
102     sub_item = proto_tree_add_item(main_tree, hf_battery_level, tvb, offset, 1, ENC_BIG_ENDIAN);
103     value = tvb_get_guint8(tvb, offset);
104     if (value >= 101 && value <= 254)
105         expert_add_info(pinfo, sub_item, &ei_battery_level_reserved);
106     else if (value == 255)
107         proto_item_append_text(sub_item, "Battery Level Reporting Not Supported");
108
109     offset += 1;
110
111     if (tvb_length_remaining(tvb, offset) > 0) {
112         proto_tree_add_expert(main_tree, pinfo, &ei_unexpected_data, tvb, offset, -1);
113         offset += tvb_length_remaining(tvb, offset);
114     }
115
116     return offset;
117 }
118
119
120 void
121 proto_register_bt3ds(void)
122 {
123     module_t         *module;
124     expert_module_t  *expert_bt3ds;
125
126     static ei_register_info ei[] = {
127         { &ei_message_opcode_reserved,           { "bt3ds.expert.message_opcode.reserved", PI_PROTOCOL, PI_NOTE, "Value is reserved", EXPFILL }},
128         { &ei_reserved,                          { "bt3ds.expert.reserved", PI_PROTOCOL, PI_NOTE, "Value is reserved", EXPFILL }},
129         { &ei_battery_level_reserved,            { "bt3ds.expert.battery_level.reserved", PI_PROTOCOL, PI_NOTE, "Value is reserved", EXPFILL }},
130         { &ei_unexpected_data,                   { "bt3ds.expert.unexpected_data", PI_PROTOCOL, PI_WARN, "Unexpected data", EXPFILL }}
131     };
132
133     static hf_register_info hf[] = {
134         { &hf_message_opcode,
135             { "Message Opcode",                            "bt3ds.message_opcode",
136             FT_UINT8, BASE_HEX, VALS(message_opcode_vals), 0x00,
137             NULL, HFILL }
138         },
139         { &hf_association_notification,
140             { "Association Notification",                  "bt3ds.association_notification",
141             FT_BOOLEAN, 8, NULL, 0x01,
142             NULL, HFILL }
143         },
144         { &hf_user_request_for_battery_level_display,
145             { "User Request for Battery Level Display",    "bt3ds.user_request_for_battery_level_display",
146             FT_BOOLEAN, 8, NULL, 0x02,
147             NULL, HFILL }
148         },
149         { &hf_reserved,
150             { "Reserved",                                  "bt3ds.reserved",
151             FT_UINT8, BASE_HEX, NULL, 0xFC,
152             NULL, HFILL }
153         },
154         { &hf_battery_level,
155             { "Battery Level",                             "bt3ds.battery_level",
156             FT_UINT8, BASE_DEC, NULL, 0x00,
157             "0-100% of current charge level of battery", HFILL }
158         }
159     };
160
161     static gint *ett[] = {
162         &ett_bt3ds
163     };
164
165     proto_bt3ds = proto_register_protocol("Bluetooth 3DS Profile", "BT 3DS", "bt3ds");
166     b3ds_handle = new_register_dissector("bt3ds", dissect_bt3ds, proto_bt3ds);
167
168     proto_register_field_array(proto_bt3ds, hf, array_length(hf));
169     proto_register_subtree_array(ett, array_length(ett));
170
171     expert_bt3ds = expert_register_protocol(proto_bt3ds);
172     expert_register_field_array(expert_bt3ds, ei, array_length(ei));
173
174     module = prefs_register_protocol(proto_bt3ds, NULL);
175     prefs_register_static_text_preference(module, "3ds.version",
176             "Bluetooth Profile 3DS version: 1.0",
177             "Version of profile supported by this dissector.");
178
179 }
180
181 void
182 proto_reg_handoff_bt3ds(void)
183 {
184     dissector_add_uint("btl2cap.service", BTSDP_3D_SYNCHRONIZATION_UUID, b3ds_handle);
185     dissector_add_uint("btl2cap.service", BTSDP_3D_DISPLAY_UUID, b3ds_handle);
186     dissector_add_uint("btl2cap.service", BTSDP_3D_GLASSES_UUID, b3ds_handle);
187
188     dissector_add_uint("btl2cap.psm", BTL2CAP_PSM_3DS, b3ds_handle);
189     dissector_add_handle("btl2cap.cid", b3ds_handle);
190 }
191
192 /*
193  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
194  *
195  * Local variables:
196  * c-basic-offset: 4
197  * tab-width: 8
198  * indent-tabs-mode: nil
199  * End:
200  *
201  * vi: set shiftwidth=4 tabstop=8 expandtab:
202  * :indentSize=4:tabSize=8:noTabs=true:
203  */