GSM A DTAP: add UMTS EVS to supported codecs list IE
[metze/wireshark/wip.git] / epan / dissectors / packet-i2c.c
1 /* packet-i2c.c
2  * Routines for I2C captures (using libpcap extensions)
3  *
4  * Pigeon Point Systems <www.pigeonpoint.com>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include "config.h"
14
15 #include <epan/packet.h>
16 #include <epan/capture_dissectors.h>
17 #include <epan/prefs.h>
18 #include <epan/decode_as.h>
19 #include <wiretap/wtap.h>
20
21 void proto_register_i2c(void);
22 void proto_reg_handoff_i2c(void);
23
24 static int proto_i2c = -1;
25 static int proto_i2c_event = -1;
26 static int proto_i2c_data = -1;
27
28
29 static int hf_i2c_bus = -1;
30 static int hf_i2c_event = -1;
31 static int hf_i2c_flags = -1;
32 static int hf_i2c_addr = -1;
33
34 static gint ett_i2c = -1;
35
36 static dissector_table_t subdissector_table;
37
38 /* I2C packet flags. */
39 #define I2C_FLAG_RD                     0x00000001
40 #define I2C_FLAG_TEN                    0x00000010
41 #define I2C_FLAG_REV_DIR_ADDR           0x00002000
42 #define I2C_FLAG_NOSTART                0x00004000
43
44 /* I2C events.  */
45 #define I2C_EVENT_PROM_ON               (1 << 0)        /* Promiscuous mode: on      */
46 #define I2C_EVENT_PROM_OFF              (1 << 1)        /* Promiscuous mode: off     */
47 #define I2C_EVENT_ONLINE_ON             (1 << 2)        /* Online state: on          */
48 #define I2C_EVENT_ONLINE_OFF            (1 << 3)        /* Online state: off         */
49 #define I2C_EVENT_ATTACHED_ON           (1 << 4)        /* Attached state: on        */
50 #define I2C_EVENT_ATTACHED_OFF          (1 << 5)        /* Attached state: off       */
51 #define I2C_EVENT_PROM_OVFL_ON          (1 << 6)        /* Prom. queue overflow: on  */
52 #define I2C_EVENT_PROM_OVFL_OFF         (1 << 7)        /* Prom. queue overflow: off */
53 #define I2C_EVENT_OVFL_ON               (1 << 8)        /* Queue overflow: on        */
54 #define I2C_EVENT_OVFL_OFF              (1 << 9)        /* Queue overflow: off       */
55
56 /* I2C errors.  */
57 #define I2C_EVENT_ERR_DATA_LO           (1 << 16)       /* Unable to drive data LO   */
58 #define I2C_EVENT_ERR_DATA_HI           (1 << 17)       /* Unable to drive data HI   */
59 #define I2C_EVENT_ERR_CLOCK_LO          (1 << 18)       /* Unable to drive clock LO  */
60 #define I2C_EVENT_ERR_CLOCK_HI          (1 << 19)       /* Unable to drive clock HI  */
61 #define I2C_EVENT_ERR_CLOCK_TO          (1 << 20)       /* Clock low timeout         */
62 #define I2C_EVENT_ERR_PHYS              (1 << 21)       /* The I2C bus controller
63                                                            has been physically
64                                                            disconnected from the bus */
65 #define I2C_EVENT_ERR_FAIL              (1 << 22)       /* Undiagnosed failure       */
66
67 static void i2c_prompt(packet_info *pinfo _U_, gchar* result)
68 {
69         g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Interpret I2C messages as");
70 }
71
72 static gboolean
73 capture_i2c(const guchar *pd _U_, int offset _U_, int len _U_, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
74 {
75         if (pseudo_header->i2c.is_event) {
76                 capture_dissector_increment_count(cpinfo, proto_i2c_event);
77         } else {
78                 capture_dissector_increment_count(cpinfo, proto_i2c_data);
79         }
80
81         return TRUE;
82 }
83
84 static const char *
85 i2c_get_event_desc(guint32 event)
86 {
87         const char *desc;
88
89         switch (event & 0x0000ffff) {
90                 case I2C_EVENT_PROM_ON:
91                         desc = "Promiscuous mode is enabled";
92                         break;
93                 case I2C_EVENT_PROM_OFF:
94                         desc = "Promiscuous mode is disabled";
95                         break;
96                 case I2C_EVENT_ONLINE_ON:
97                         desc = "The I2C controller is operational";
98                         break;
99                 case I2C_EVENT_ONLINE_OFF:
100                         desc = "The I2C controller is non-operational";
101                         break;
102                 case I2C_EVENT_ATTACHED_ON:
103                         desc = "The I2C controller is attached to an I2C bus";
104                         break;
105                 case I2C_EVENT_ATTACHED_OFF:
106                         desc = "The I2C controller is detached from an I2C bus";
107                         if (event & I2C_EVENT_ERR_DATA_LO) {
108                                 desc = "The I2C controller is detached from an I2C bus: "
109                                        "unable to drive data LO";
110                         } else if (event & I2C_EVENT_ERR_DATA_HI) {
111                                 desc = "The I2C controller is detached from an I2C bus: "
112                                        "unable to drive data HI";
113                         } else if (event & I2C_EVENT_ERR_CLOCK_LO) {
114                                 desc = "The I2C controller is detached from an I2C bus: "
115                                        "unable to drive clock LO";
116                         } else if (event & I2C_EVENT_ERR_CLOCK_HI) {
117                                 desc = "The I2C controller is detached from an I2C bus: "
118                                        "unable to drive clock HI";
119                         } else if (event & I2C_EVENT_ERR_CLOCK_TO) {
120                                 desc = "The I2C controller is detached from an I2C bus: "
121                                        "clock low timeout";
122                         } else if (event & I2C_EVENT_ERR_PHYS) {
123                                 desc = "The I2C controller is detached from an I2C bus: "
124                                        "the I2C bus controller has been physically "
125                                        "disconnected from the bus";
126                         } else if (event & I2C_EVENT_ERR_FAIL) {
127                                 desc = "The I2C controller is detached from an I2C bus: "
128                                        "undiagnosed failure";
129                         }
130                         break;
131                 case I2C_EVENT_PROM_OVFL_ON:
132                         desc = "The incoming promiscuous data buffer has been overrun; "
133                                "some data is lost";
134                         break;
135                 case I2C_EVENT_PROM_OVFL_OFF:
136                         desc = "The incoming promiscuous data buffer is available";
137                         break;
138                 case I2C_EVENT_OVFL_ON:
139                         desc = "The incoming I2C data buffer has been overrun; "
140                                "some data is lost";
141                         break;
142                 case I2C_EVENT_OVFL_OFF:
143                         desc = "The incoming I2C data buffer is available";
144                         break;
145                 default:
146                         desc = "<unknown state event>";
147                         break;
148         }
149
150         return desc;
151 }
152
153 static int
154 dissect_i2c(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
155 {
156         proto_item *ti;
157         proto_tree *i2c_tree;
158         guint8      is_event;
159         guint8      bus, addr;
160         guint32     flags;
161
162         flags = pinfo->pseudo_header->i2c.flags;
163
164         bus = pinfo->pseudo_header->i2c.bus;
165         col_add_fstr(pinfo->cinfo, COL_DEF_SRC, "I2C-%d", bus);
166
167         is_event = pinfo->pseudo_header->i2c.is_event;
168         if (is_event) {
169                 addr = 0;
170                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "I2C Event");
171                 col_add_fstr(pinfo->cinfo, COL_DEF_DST, "----");
172                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
173                                 i2c_get_event_desc(flags));
174         } else {
175                 /* Report 7-bit hardware address */
176                 addr = tvb_get_guint8(tvb, 0) >> 1;
177                 col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "I2C %s",
178                                 (flags & I2C_FLAG_RD) ? "Read" : "Write");
179                 col_add_fstr(pinfo->cinfo, COL_DEF_DST, "0x%02x", addr);
180                 col_add_fstr(pinfo->cinfo, COL_INFO, "I2C %s, %d bytes",
181                                         (flags & I2C_FLAG_RD) ? "Read" : "Write", tvb_captured_length(tvb));
182         }
183
184         pinfo->ptype = PT_I2C;
185
186         ti = proto_tree_add_protocol_format(tree, proto_i2c, tvb, 0, -1,
187                         "Inter-Integrated Circuit (%s)",
188                         is_event ? "Event" : "Data");
189
190         i2c_tree = proto_item_add_subtree(ti, ett_i2c);
191         proto_tree_add_uint_format(i2c_tree, hf_i2c_bus, tvb, 0, 0, bus,
192                         "Bus: I2C-%d", bus);
193
194         if (is_event) {
195                 proto_tree_add_uint_format_value(i2c_tree, hf_i2c_event, tvb, 0, 0,
196                                 flags, "%s (0x%08x)",
197                                 i2c_get_event_desc(flags), flags);
198         } else {
199                 proto_tree_add_uint_format_value(i2c_tree, hf_i2c_addr, tvb, 0, 1,
200                                 addr, "0x%02x%s", addr, addr ? "" : " (General Call)");
201                 proto_tree_add_uint(i2c_tree, hf_i2c_flags, tvb, 0, 0, flags);
202
203                 if (!dissector_try_payload(subdissector_table, tvb, pinfo, tree))
204                 {
205                         call_data_dissector(tvb, pinfo, tree);
206                 }
207         }
208         return tvb_captured_length(tvb);
209 }
210
211 void
212 proto_register_i2c(void)
213 {
214         static hf_register_info hf[] = {
215                 { &hf_i2c_bus,   { "Bus ID", "i2c.bus", FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL }},
216                 { &hf_i2c_addr,  { "Target address", "i2c.addr", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
217                 { &hf_i2c_event, { "Event", "i2c.event", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL }},
218                 { &hf_i2c_flags, { "Flags", "i2c.flags", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL }},
219         };
220         static gint *ett[] = {
221                 &ett_i2c
222         };
223         module_t *m;
224
225         proto_i2c = proto_register_protocol("Inter-Integrated Circuit", "I2C", "i2c");
226         proto_register_field_array(proto_i2c, hf, array_length(hf));
227         proto_register_subtree_array(ett, array_length(ett));
228
229         /* Placeholders for capture statistics */
230         proto_i2c_event = proto_register_protocol_in_name_only("I2C Events", "I2C Events", "i2c_event", proto_i2c, FT_PROTOCOL);
231         proto_i2c_data = proto_register_protocol_in_name_only("I2C Data", "I2C Data", "i2c_data", proto_i2c, FT_PROTOCOL);
232
233         m = prefs_register_protocol(proto_i2c, NULL);
234         prefs_register_obsolete_preference(m, "type");
235
236         subdissector_table = register_decode_as_next_proto(proto_i2c, "I2C Message", "i2c.message", "I2C messages dissector", i2c_prompt);
237 }
238
239 void
240 proto_reg_handoff_i2c(void)
241 {
242         dissector_handle_t i2c_handle;
243         capture_dissector_handle_t i2c_cap_handle;
244
245         i2c_handle = create_dissector_handle(dissect_i2c, proto_i2c);
246         dissector_add_uint("wtap_encap", WTAP_ENCAP_I2C, i2c_handle);
247         i2c_cap_handle = create_capture_dissector_handle(capture_i2c, proto_i2c);
248         capture_dissector_add_uint("wtap_encap", WTAP_ENCAP_I2C, i2c_cap_handle);
249 }
250
251 /*
252  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
253  *
254  * Local variables:
255  * c-basic-offset: 8
256  * tab-width: 8
257  * indent-tabs-mode: t
258  * End:
259  *
260  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
261  * :indentSize=8:tabSize=8:noTabs=false:
262  */