2 * Routines for I2C captures (using libpcap extensions)
4 * Pigeon Point Systems <www.pigeonpoint.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
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>
21 void proto_register_i2c(void);
22 void proto_reg_handoff_i2c(void);
24 static int proto_i2c = -1;
25 static int proto_i2c_event = -1;
26 static int proto_i2c_data = -1;
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;
34 static gint ett_i2c = -1;
36 static dissector_table_t subdissector_table;
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
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 */
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
64 disconnected from the bus */
65 #define I2C_EVENT_ERR_FAIL (1 << 22) /* Undiagnosed failure */
67 static void i2c_prompt(packet_info *pinfo _U_, gchar* result)
69 g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Interpret I2C messages as");
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)
75 if (pseudo_header->i2c.is_event) {
76 capture_dissector_increment_count(cpinfo, proto_i2c_event);
78 capture_dissector_increment_count(cpinfo, proto_i2c_data);
85 i2c_get_event_desc(guint32 event)
89 switch (event & 0x0000ffff) {
90 case I2C_EVENT_PROM_ON:
91 desc = "Promiscuous mode is enabled";
93 case I2C_EVENT_PROM_OFF:
94 desc = "Promiscuous mode is disabled";
96 case I2C_EVENT_ONLINE_ON:
97 desc = "The I2C controller is operational";
99 case I2C_EVENT_ONLINE_OFF:
100 desc = "The I2C controller is non-operational";
102 case I2C_EVENT_ATTACHED_ON:
103 desc = "The I2C controller is attached to an I2C bus";
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: "
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";
131 case I2C_EVENT_PROM_OVFL_ON:
132 desc = "The incoming promiscuous data buffer has been overrun; "
135 case I2C_EVENT_PROM_OVFL_OFF:
136 desc = "The incoming promiscuous data buffer is available";
138 case I2C_EVENT_OVFL_ON:
139 desc = "The incoming I2C data buffer has been overrun; "
142 case I2C_EVENT_OVFL_OFF:
143 desc = "The incoming I2C data buffer is available";
146 desc = "<unknown state event>";
154 dissect_i2c(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
157 proto_tree *i2c_tree;
162 flags = pinfo->pseudo_header->i2c.flags;
164 bus = pinfo->pseudo_header->i2c.bus;
165 col_add_fstr(pinfo->cinfo, COL_DEF_SRC, "I2C-%d", bus);
167 is_event = pinfo->pseudo_header->i2c.is_event;
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));
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));
184 pinfo->ptype = PT_I2C;
186 ti = proto_tree_add_protocol_format(tree, proto_i2c, tvb, 0, -1,
187 "Inter-Integrated Circuit (%s)",
188 is_event ? "Event" : "Data");
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,
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);
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);
203 if (!dissector_try_payload(subdissector_table, tvb, pinfo, tree))
205 call_data_dissector(tvb, pinfo, tree);
208 return tvb_captured_length(tvb);
212 proto_register_i2c(void)
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 }},
220 static gint *ett[] = {
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));
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);
233 m = prefs_register_protocol(proto_i2c, NULL);
234 prefs_register_obsolete_preference(m, "type");
236 subdissector_table = register_decode_as_next_proto(proto_i2c, "I2C Message", "i2c.message", "I2C messages dissector", i2c_prompt);
240 proto_reg_handoff_i2c(void)
242 dissector_handle_t i2c_handle;
243 capture_dissector_handle_t i2c_cap_handle;
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);
252 * Editor modelines - http://www.wireshark.org/tools/modelines.html
257 * indent-tabs-mode: t
260 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
261 * :indentSize=8:tabSize=8:noTabs=false: