2008-08-12 04:44:35 +00:00
|
|
|
/* packet-i2c.c
|
|
|
|
* Routines for I2C captures (using libpcap extensions)
|
|
|
|
*
|
|
|
|
* Pigeon Point Systems <www.pigeonpoint.com>
|
|
|
|
*
|
2009-04-08 13:07:21 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2008-08-12 04:44:35 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-12 11:23:27 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2008-08-12 04:44:35 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 02:03:38 +00:00
|
|
|
#include "config.h"
|
2008-08-12 04:44:35 +00:00
|
|
|
|
|
|
|
#include <epan/packet.h>
|
2015-12-13 04:38:21 +00:00
|
|
|
#include <epan/capture_dissectors.h>
|
2008-08-12 04:44:35 +00:00
|
|
|
#include <epan/prefs.h>
|
2015-04-03 00:16:54 +00:00
|
|
|
#include <epan/decode_as.h>
|
2013-12-21 17:23:17 +00:00
|
|
|
#include <wiretap/wtap.h>
|
2008-08-12 04:44:35 +00:00
|
|
|
|
2013-12-14 12:45:57 +00:00
|
|
|
void proto_register_i2c(void);
|
|
|
|
void proto_reg_handoff_i2c(void);
|
|
|
|
|
2008-08-12 04:44:35 +00:00
|
|
|
static int proto_i2c = -1;
|
2015-12-19 15:11:40 +00:00
|
|
|
static int proto_i2c_event = -1;
|
|
|
|
static int proto_i2c_data = -1;
|
|
|
|
|
2008-08-12 04:44:35 +00:00
|
|
|
|
|
|
|
static int hf_i2c_bus = -1;
|
|
|
|
static int hf_i2c_event = -1;
|
|
|
|
static int hf_i2c_flags = -1;
|
|
|
|
static int hf_i2c_addr = -1;
|
|
|
|
|
|
|
|
static gint ett_i2c = -1;
|
|
|
|
|
2015-04-03 00:16:54 +00:00
|
|
|
static dissector_table_t subdissector_table;
|
2008-08-12 04:44:35 +00:00
|
|
|
|
|
|
|
/* I2C packet flags. */
|
|
|
|
#define I2C_FLAG_RD 0x00000001
|
|
|
|
#define I2C_FLAG_TEN 0x00000010
|
|
|
|
#define I2C_FLAG_REV_DIR_ADDR 0x00002000
|
|
|
|
#define I2C_FLAG_NOSTART 0x00004000
|
|
|
|
|
|
|
|
/* I2C events. */
|
|
|
|
#define I2C_EVENT_PROM_ON (1 << 0) /* Promiscuous mode: on */
|
|
|
|
#define I2C_EVENT_PROM_OFF (1 << 1) /* Promiscuous mode: off */
|
|
|
|
#define I2C_EVENT_ONLINE_ON (1 << 2) /* Online state: on */
|
|
|
|
#define I2C_EVENT_ONLINE_OFF (1 << 3) /* Online state: off */
|
|
|
|
#define I2C_EVENT_ATTACHED_ON (1 << 4) /* Attached state: on */
|
|
|
|
#define I2C_EVENT_ATTACHED_OFF (1 << 5) /* Attached state: off */
|
|
|
|
#define I2C_EVENT_PROM_OVFL_ON (1 << 6) /* Prom. queue overflow: on */
|
|
|
|
#define I2C_EVENT_PROM_OVFL_OFF (1 << 7) /* Prom. queue overflow: off */
|
|
|
|
#define I2C_EVENT_OVFL_ON (1 << 8) /* Queue overflow: on */
|
|
|
|
#define I2C_EVENT_OVFL_OFF (1 << 9) /* Queue overflow: off */
|
|
|
|
|
|
|
|
/* I2C errors. */
|
|
|
|
#define I2C_EVENT_ERR_DATA_LO (1 << 16) /* Unable to drive data LO */
|
|
|
|
#define I2C_EVENT_ERR_DATA_HI (1 << 17) /* Unable to drive data HI */
|
|
|
|
#define I2C_EVENT_ERR_CLOCK_LO (1 << 18) /* Unable to drive clock LO */
|
|
|
|
#define I2C_EVENT_ERR_CLOCK_HI (1 << 19) /* Unable to drive clock HI */
|
|
|
|
#define I2C_EVENT_ERR_CLOCK_TO (1 << 20) /* Clock low timeout */
|
|
|
|
#define I2C_EVENT_ERR_PHYS (1 << 21) /* The I2C bus controller
|
|
|
|
has been physically
|
|
|
|
disconnected from the bus */
|
|
|
|
#define I2C_EVENT_ERR_FAIL (1 << 22) /* Undiagnosed failure */
|
|
|
|
|
2015-04-03 00:16:54 +00:00
|
|
|
static void i2c_prompt(packet_info *pinfo _U_, gchar* result)
|
|
|
|
{
|
|
|
|
g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Interpret I2C messages as");
|
|
|
|
}
|
|
|
|
|
2015-12-13 21:54:16 +00:00
|
|
|
static gboolean
|
2015-12-15 00:25:31 +00:00
|
|
|
capture_i2c(const guchar *pd _U_, int offset _U_, int len _U_, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
|
2008-08-12 04:44:35 +00:00
|
|
|
{
|
|
|
|
if (pseudo_header->i2c.is_event) {
|
2015-12-19 15:11:40 +00:00
|
|
|
capture_dissector_increment_count(cpinfo, proto_i2c_event);
|
2008-08-12 04:44:35 +00:00
|
|
|
} else {
|
2015-12-19 15:11:40 +00:00
|
|
|
capture_dissector_increment_count(cpinfo, proto_i2c_data);
|
2008-08-12 04:44:35 +00:00
|
|
|
}
|
2015-12-13 21:54:16 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2008-08-12 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2014-12-04 21:49:42 +00:00
|
|
|
i2c_get_event_desc(guint32 event)
|
2008-08-12 04:44:35 +00:00
|
|
|
{
|
|
|
|
const char *desc;
|
|
|
|
|
|
|
|
switch (event & 0x0000ffff) {
|
|
|
|
case I2C_EVENT_PROM_ON:
|
|
|
|
desc = "Promiscuous mode is enabled";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_PROM_OFF:
|
|
|
|
desc = "Promiscuous mode is disabled";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_ONLINE_ON:
|
|
|
|
desc = "The I2C controller is operational";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_ONLINE_OFF:
|
|
|
|
desc = "The I2C controller is non-operational";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_ATTACHED_ON:
|
|
|
|
desc = "The I2C controller is attached to an I2C bus";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_ATTACHED_OFF:
|
|
|
|
desc = "The I2C controller is detached from an I2C bus";
|
|
|
|
if (event & I2C_EVENT_ERR_DATA_LO) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"unable to drive data LO";
|
|
|
|
} else if (event & I2C_EVENT_ERR_DATA_HI) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"unable to drive data HI";
|
|
|
|
} else if (event & I2C_EVENT_ERR_CLOCK_LO) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"unable to drive clock LO";
|
|
|
|
} else if (event & I2C_EVENT_ERR_CLOCK_HI) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"unable to drive clock HI";
|
|
|
|
} else if (event & I2C_EVENT_ERR_CLOCK_TO) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"clock low timeout";
|
|
|
|
} else if (event & I2C_EVENT_ERR_PHYS) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"the I2C bus controller has been physically "
|
|
|
|
"disconnected from the bus";
|
|
|
|
} else if (event & I2C_EVENT_ERR_FAIL) {
|
|
|
|
desc = "The I2C controller is detached from an I2C bus: "
|
|
|
|
"undiagnosed failure";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_PROM_OVFL_ON:
|
|
|
|
desc = "The incoming promiscuous data buffer has been overrun; "
|
|
|
|
"some data is lost";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_PROM_OVFL_OFF:
|
|
|
|
desc = "The incoming promiscuous data buffer is available";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_OVFL_ON:
|
|
|
|
desc = "The incoming I2C data buffer has been overrun; "
|
|
|
|
"some data is lost";
|
|
|
|
break;
|
|
|
|
case I2C_EVENT_OVFL_OFF:
|
|
|
|
desc = "The incoming I2C data buffer is available";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
desc = "<unknown state event>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2015-11-15 21:51:45 +00:00
|
|
|
static int
|
|
|
|
dissect_i2c(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
|
2008-08-12 04:44:35 +00:00
|
|
|
{
|
|
|
|
proto_item *ti;
|
2014-12-04 21:38:16 +00:00
|
|
|
proto_tree *i2c_tree;
|
2014-12-04 21:49:42 +00:00
|
|
|
guint8 is_event;
|
|
|
|
guint8 bus, addr;
|
|
|
|
guint32 flags;
|
2008-08-12 04:44:35 +00:00
|
|
|
|
|
|
|
flags = pinfo->pseudo_header->i2c.flags;
|
2014-12-04 21:38:16 +00:00
|
|
|
|
2008-08-12 04:44:35 +00:00
|
|
|
bus = pinfo->pseudo_header->i2c.bus;
|
2014-12-04 21:38:16 +00:00
|
|
|
col_add_fstr(pinfo->cinfo, COL_DEF_SRC, "I2C-%d", bus);
|
|
|
|
|
|
|
|
is_event = pinfo->pseudo_header->i2c.is_event;
|
2008-08-12 04:44:35 +00:00
|
|
|
if (is_event) {
|
|
|
|
addr = 0;
|
2014-12-04 21:38:16 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_PROTOCOL, "I2C Event");
|
|
|
|
col_add_fstr(pinfo->cinfo, COL_DEF_DST, "----");
|
|
|
|
col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
|
|
|
|
i2c_get_event_desc(flags));
|
2008-08-12 04:44:35 +00:00
|
|
|
} else {
|
|
|
|
/* Report 7-bit hardware address */
|
|
|
|
addr = tvb_get_guint8(tvb, 0) >> 1;
|
2013-06-14 01:02:11 +00:00
|
|
|
col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "I2C %s",
|
|
|
|
(flags & I2C_FLAG_RD) ? "Read" : "Write");
|
|
|
|
col_add_fstr(pinfo->cinfo, COL_DEF_DST, "0x%02x", addr);
|
|
|
|
col_add_fstr(pinfo->cinfo, COL_INFO, "I2C %s, %d bytes",
|
2014-12-04 21:25:39 +00:00
|
|
|
(flags & I2C_FLAG_RD) ? "Read" : "Write", tvb_captured_length(tvb));
|
2014-12-04 21:38:16 +00:00
|
|
|
}
|
2008-08-12 04:44:35 +00:00
|
|
|
|
2014-12-04 21:38:16 +00:00
|
|
|
pinfo->ptype = PT_I2C;
|
2013-10-13 19:56:52 +00:00
|
|
|
|
2014-12-04 21:38:16 +00:00
|
|
|
ti = proto_tree_add_protocol_format(tree, proto_i2c, tvb, 0, -1,
|
|
|
|
"Inter-Integrated Circuit (%s)",
|
|
|
|
is_event ? "Event" : "Data");
|
2008-08-12 04:44:35 +00:00
|
|
|
|
2014-12-04 21:38:16 +00:00
|
|
|
i2c_tree = proto_item_add_subtree(ti, ett_i2c);
|
|
|
|
proto_tree_add_uint_format(i2c_tree, hf_i2c_bus, tvb, 0, 0, bus,
|
|
|
|
"Bus: I2C-%d", bus);
|
|
|
|
|
|
|
|
if (is_event) {
|
|
|
|
proto_tree_add_uint_format_value(i2c_tree, hf_i2c_event, tvb, 0, 0,
|
|
|
|
flags, "%s (0x%08x)",
|
|
|
|
i2c_get_event_desc(flags), flags);
|
|
|
|
} else {
|
|
|
|
proto_tree_add_uint_format_value(i2c_tree, hf_i2c_addr, tvb, 0, 1,
|
|
|
|
addr, "0x%02x%s", addr, addr ? "" : " (General Call)");
|
2015-04-03 00:16:54 +00:00
|
|
|
proto_tree_add_uint(i2c_tree, hf_i2c_flags, tvb, 0, 0, flags);
|
2008-08-12 04:44:35 +00:00
|
|
|
|
2017-07-09 17:47:59 +00:00
|
|
|
if (!dissector_try_payload(subdissector_table, tvb, pinfo, tree))
|
2015-04-03 00:16:54 +00:00
|
|
|
{
|
2016-03-20 00:33:14 +00:00
|
|
|
call_data_dissector(tvb, pinfo, tree);
|
2008-08-12 04:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-15 21:51:45 +00:00
|
|
|
return tvb_captured_length(tvb);
|
2008-08-12 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_register_i2c(void)
|
|
|
|
{
|
|
|
|
static hf_register_info hf[] = {
|
2014-10-04 15:28:35 +00:00
|
|
|
{ &hf_i2c_bus, { "Bus ID", "i2c.bus", FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL }},
|
|
|
|
{ &hf_i2c_addr, { "Target address", "i2c.addr", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
|
From Kovarththanan Rajaratnam via bug 3548:
(1) Trailing/leading spaces are removed from 'name's/'blurb's
(2) Duplicate 'blurb's are replaced with NULL
(3) Empty ("") 'blurb's are replaced with NULL
(4) BASE_NONE, NULL, 0x0 are used for 'display', 'strings' and 'bitmask' fields
for FT_NONE, FT_BYTES, FT_IPv4, FT_IPv6, FT_ABSOLUTE_TIME, FT_RELATIVE_TIME,
FT_PROTOCOL, FT_STRING and FT_STRINGZ field types
(5) Only allow non-zero value for 'display' if 'bitmask' is non-zero
svn path=/trunk/; revision=28770
2009-06-18 21:30:42 +00:00
|
|
|
{ &hf_i2c_event, { "Event", "i2c.event", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL }},
|
|
|
|
{ &hf_i2c_flags, { "Flags", "i2c.flags", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL }},
|
2008-08-12 04:44:35 +00:00
|
|
|
};
|
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_i2c
|
|
|
|
};
|
|
|
|
module_t *m;
|
|
|
|
|
|
|
|
proto_i2c = proto_register_protocol("Inter-Integrated Circuit", "I2C", "i2c");
|
|
|
|
proto_register_field_array(proto_i2c, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
|
|
|
|
2017-01-01 14:40:58 +00:00
|
|
|
/* Placeholders for capture statistics */
|
|
|
|
proto_i2c_event = proto_register_protocol_in_name_only("I2C Events", "I2C Events", "i2c_event", proto_i2c, FT_PROTOCOL);
|
|
|
|
proto_i2c_data = proto_register_protocol_in_name_only("I2C Data", "I2C Data", "i2c_data", proto_i2c, FT_PROTOCOL);
|
|
|
|
|
2008-08-12 04:44:35 +00:00
|
|
|
m = prefs_register_protocol(proto_i2c, NULL);
|
2015-04-03 00:16:54 +00:00
|
|
|
prefs_register_obsolete_preference(m, "type");
|
|
|
|
|
2018-02-13 18:05:31 +00:00
|
|
|
subdissector_table = register_decode_as_next_proto(proto_i2c, "I2C Message", "i2c.message", "I2C messages dissector", i2c_prompt);
|
2008-08-12 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_reg_handoff_i2c(void)
|
|
|
|
{
|
2008-09-26 22:07:45 +00:00
|
|
|
dissector_handle_t i2c_handle;
|
2016-10-26 16:07:47 +00:00
|
|
|
capture_dissector_handle_t i2c_cap_handle;
|
2008-09-26 22:07:45 +00:00
|
|
|
|
2015-12-09 03:49:44 +00:00
|
|
|
i2c_handle = create_dissector_handle(dissect_i2c, proto_i2c);
|
2010-12-20 05:35:29 +00:00
|
|
|
dissector_add_uint("wtap_encap", WTAP_ENCAP_I2C, i2c_handle);
|
2016-10-26 16:07:47 +00:00
|
|
|
i2c_cap_handle = create_capture_dissector_handle(capture_i2c, proto_i2c);
|
|
|
|
capture_dissector_add_uint("wtap_encap", WTAP_ENCAP_I2C, i2c_cap_handle);
|
2008-08-12 04:44:35 +00:00
|
|
|
}
|
2014-10-04 15:28:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|