aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-hilscher.c
blob: cac5999e72e992b8fe8a61ee55140630bbead9b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* packet-hilscher.c
 * Dissector for Hilscher analyzer protocols.
 * Copyright 2008, Hilscher GmbH, Holger Pfrommer hpfrommer[AT]hilscher.com
 *
 * $Id$
 *
 * This is a new dissector plugin for Hilscher analyzer frames.
 * These frames are generated by Hilscher analyzer products and are identified via
 * their unique source MAC address (this is a reserved MAC from Hilscher-range and
 * will never be used by another network device). Most likely these frames are
 * only generated on a virtual network interface or the generating device is
 * attached directly via patch cable to a real network interface, but not routed
 * through a network. The Ethernet-header (destination MAC, source MAC and
 * Length/Type) is not displayed in the protocol tree for these frames as this is
 * overhead-information which has no practical use in this case.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald[AT]wireshark.org>
 * Copyright 1999 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>
#include <epan/packet.h>
#include <epan/prefs.h>


static int  proto_hilscher              = -1;
static module_t *hilscher_module;

/*  Ethernet heuristic dissectors (such as this one) get called for
 *  every Ethernet frame Wireshark handles.  In order to not impose that
 *  performance penalty on everyone this dissector disables itself by
 *  default.
 *
 *  This is done separately from the disabled protocols list mainly so
 *  we can disable it by default.  XXX Maybe there's a better way.
 */
static gboolean  hilscher_enable_dissector = FALSE;

void proto_reg_handoff_hilscher(void);

static gint ett_information_type        = -1;
static gint ett_gpio_number             = -1;
static gint ett_gpio_edge               = -1;

static int  hf_information_type         = -1;
static int  hf_gpio_number              = -1;
static int  hf_gpio_edge                = -1;

#define  INFO_TYPE_OFFSET    14

static const value_string information_type[] = {
	{ 0x0, "netANALYZER GPIO event" },
	{ 0,   NULL }
};

static const value_string gpio_number[] = {
	{ 0x0, "GPIO 0" },
	{ 0x1, "GPIO 1" },
	{ 0x2, "GPIO 2" },
	{ 0x3, "GPIO 3" },
	{ 0,   NULL }
};

static const value_string gpio_edge[] = {
	{ 0x0, "rising edge" },
	{ 0x1, "falling edge" },
	{ 0,   NULL }
};

/* netANAYLZER dissector */
static void
dissect_hilscher_netanalyzer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset)
{
	proto_item	*ti        = NULL;
	guint		gpio_num;
	guint		gpio_edgex;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "netANALYZER");

	if (tree)
		ti = proto_tree_add_item(tree, hf_information_type, tvb, offset, 1, FALSE);

	/* GPIO NUMBER */
	offset++;
	if (tree)
		ti = proto_tree_add_item (tree, hf_gpio_number, tvb, offset, 1, FALSE);
	gpio_num = (tvb_get_guint8(tvb, offset) & 0x03);

	/* GPIO EDGE */
	offset++;
	if (tree)
		ti = proto_tree_add_item (tree, hf_gpio_edge, tvb, offset, 1, FALSE);
	gpio_edgex = (tvb_get_guint8(tvb, offset) & 0x01);

	if (gpio_edgex == 0x00)
		col_add_fstr(pinfo->cinfo, COL_INFO, "netANALYZER event on GPIO %d (rising edge)", gpio_num);
	else
		col_add_fstr(pinfo->cinfo, COL_INFO, "netANALYZER event on GPIO %d (falling edge)", gpio_num);
}


/* General Hilscher analyzer dissector */
static gboolean
dissect_hilscher_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	guint       info_type;
	gint        offset;

 	/* Check that there's enough data */
	if (tvb_length(tvb) < 14)
		return FALSE;

	/* check for Hilscher frame, this has a unique source MAC from Hilscher range and ethertype 0x88ff
	   First 14 bytes must be: xx xx xx xx xx xx 00 02 a2 ff ff ff 88 ff */
	if ((tvb_get_guint8(tvb, 6) == 0x00) &&
	    (tvb_get_guint8(tvb, 7) == 0x02) &&
	    (tvb_get_guint8(tvb, 8) == 0xa2) &&
	    (tvb_get_guint8(tvb, 9) == 0xff) &&
	    (tvb_get_guint8(tvb, 10) == 0xff) &&
	    (tvb_get_guint8(tvb, 11) == 0xff) &&
	    (tvb_get_guint8(tvb, 12) == 0x88) &&
	    (tvb_get_guint8(tvb, 13) == 0xff) )
	{

		/* determine type of analyzer */
		offset = INFO_TYPE_OFFSET;
		info_type = tvb_get_guint8(tvb, offset);

		switch (info_type)
		{
		/* this is a netANALYZER frame */
		case 0x00:
			dissect_hilscher_netanalyzer(tvb, pinfo, tree, offset);
			break;

		/* this is no Hilscher analyzer frame */
		default:
			return FALSE;
			break;
		}

	}
	else
	{
		/* this is no Hilscher analyzer frame */
		return FALSE;
	}

	  return TRUE;
}



void proto_register_hilscher(void)
{
    static hf_register_info hf[] = {
	{ &hf_information_type,
	  { "Hilscher information block type", "hilscher.information_type",
	    FT_UINT8, BASE_HEX, VALS(information_type), 0x0, NULL, HFILL }
	},
	{ &hf_gpio_number,
	  { "Event on", "hilscher.net_analyzer.gpio_number", FT_UINT8,
	    BASE_HEX, VALS(gpio_number), 0x0, NULL, HFILL }
	},
	{ &hf_gpio_edge,
		{ "Event type", "hilscher.net_analyzer.gpio_edge", FT_UINT8,
		  BASE_HEX, VALS(gpio_edge), 0x0, NULL, HFILL }
	},
    };

    static gint *ett[] = {
	&ett_information_type,
	&ett_gpio_number,
	&ett_gpio_edge,
    };

    proto_hilscher = proto_register_protocol ("Hilscher analyzer dissector",	/* name */
                                              "Hilscher",		/* short name */
                                              "hilscher");		/* abbrev */

    hilscher_module = prefs_register_protocol(proto_hilscher, proto_reg_handoff_hilscher);

    prefs_register_bool_preference(hilscher_module, "enable", "Enable dissector",
				   "Enable this dissector (default is false)",
				   &hilscher_enable_dissector);

    proto_register_field_array(proto_hilscher, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

}

void proto_reg_handoff_hilscher(void)
{
    static gboolean prefs_initialized = FALSE;

    if (!prefs_initialized) {
	/* add heuristic dissector */
	heur_dissector_add("eth", dissect_hilscher_heur, proto_hilscher);
        prefs_initialized = TRUE;
    }

    proto_set_decoding(proto_hilscher, hilscher_enable_dissector);
}