2 * Routines for ethercat packet disassembly
6 * Copyright (c) 2007 by Beckhoff Automation GmbH
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <epan/packet.h>
35 #include "packet-nv.h"
37 /* Define the nv proto */
40 static int ett_nv = -1;
41 static int ett_nv_header = -1;
42 static int ett_nv_var = -1;
43 static int ett_nv_varheader = -1;
45 /* static int hf_nv_summary = -1; */
46 static int hf_nv_header = -1;
47 static int hf_nv_publisher = -1;
48 static int hf_nv_count = -1;
49 static int hf_nv_cycleindex = -1;
50 static int hf_nv_variable = -1;
51 static int hf_nv_varheader = -1;
52 static int hf_nv_id = -1;
53 static int hf_nv_hash = -1;
54 static int hf_nv_length = -1;
55 static int hf_nv_quality = -1;
56 static int hf_nv_data = -1;
59 static void NvSummaryFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
61 guint32 nvOffset = offset;
63 g_snprintf ( szText, nMax, "Network Vars from %d.%d.%d.%d.%d.%d - %d Var(s)",
64 tvb_get_guint8(tvb, nvOffset),
65 tvb_get_guint8(tvb, nvOffset+1),
66 tvb_get_guint8(tvb, nvOffset+2),
67 tvb_get_guint8(tvb, nvOffset+3),
68 tvb_get_guint8(tvb, nvOffset+4),
69 tvb_get_guint8(tvb, nvOffset+5),
70 tvb_get_letohs(tvb, nvOffset+6));
73 static void NvPublisherFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
75 guint32 nvOffset = offset;
77 g_snprintf ( szText, nMax, "Publisher %d.%d.%d.%d.%d.%d",
78 tvb_get_guint8(tvb, nvOffset),
79 tvb_get_guint8(tvb, nvOffset+1),
80 tvb_get_guint8(tvb, nvOffset+2),
81 tvb_get_guint8(tvb, nvOffset+3),
82 tvb_get_guint8(tvb, nvOffset+4),
83 tvb_get_guint8(tvb, nvOffset+5));
86 static void NvVarHeaderFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
88 g_snprintf ( szText, nMax, "Variable - Id = %d, Length = %d",
89 tvb_get_letohs(tvb, offset),
90 tvb_get_letohs(tvb, offset+4));
93 static void dissect_nv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
96 proto_tree *nv_tree, *nv_header_tree, *nv_var_tree,*nv_varheader_tree;
99 int nMax = (int)sizeof(szText)-1;
103 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TC-NV");
105 col_clear(pinfo->cinfo, COL_INFO);
107 NvSummaryFormater(tvb, offset, szText, nMax);
108 col_append_str(pinfo->cinfo, COL_INFO, szText);
114 ti = proto_tree_add_item(tree, proto_nv, tvb, 0, -1, ENC_NA);
115 nv_tree = proto_item_add_subtree(ti, ett_nv);
116 proto_item_append_text(ti,": %s",szText);
118 ti = proto_tree_add_item(nv_tree, hf_nv_header, tvb, offset, NvParserHDR_Len, ENC_NA);
120 nv_header_tree = proto_item_add_subtree(ti, ett_nv_header);
122 ti= proto_tree_add_item(nv_header_tree, hf_nv_publisher, tvb, offset, (int)sizeof(guint8)*6, ENC_NA);
123 NvPublisherFormater(tvb, offset, szText, nMax);
124 proto_item_set_text(ti, "%s", szText);
125 offset+=((int)sizeof(guint8)*6);
127 proto_tree_add_item(nv_header_tree, hf_nv_count, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
128 nv_count = tvb_get_letohs(tvb, offset);
129 offset+=(int)sizeof(guint16);
131 proto_tree_add_item(nv_header_tree, hf_nv_cycleindex, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
132 offset = NvParserHDR_Len;
134 for ( i=0; i < nv_count; i++ )
136 guint16 var_length = tvb_get_letohs(tvb, offset+4);
138 ti = proto_tree_add_item(nv_tree, hf_nv_variable, tvb, offset, ETYPE_88A4_NV_DATA_HEADER_Len+var_length, ENC_NA);
139 NvVarHeaderFormater(tvb, offset, szText, nMax);
140 proto_item_set_text(ti, "%s", szText);
142 nv_var_tree = proto_item_add_subtree(ti, ett_nv_var);
143 ti = proto_tree_add_item(nv_var_tree, hf_nv_varheader, tvb, offset, ETYPE_88A4_NV_DATA_HEADER_Len, ENC_NA);
145 nv_varheader_tree = proto_item_add_subtree(ti, ett_nv_varheader);
146 proto_tree_add_item(nv_varheader_tree, hf_nv_id, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
147 offset+=(int)sizeof(guint16);
149 proto_tree_add_item(nv_varheader_tree, hf_nv_hash, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
150 offset+=(int)sizeof(guint16);
152 proto_tree_add_item(nv_varheader_tree, hf_nv_length, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
153 offset+=(int)sizeof(guint16);
155 proto_tree_add_item(nv_varheader_tree, hf_nv_quality, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
156 offset+=(int)sizeof(guint16);
158 proto_tree_add_item(nv_var_tree, hf_nv_data, tvb, offset, var_length, ENC_NA);
164 void proto_register_nv(void)
166 static hf_register_info hf[] =
170 { "Summary of the Nv Packet", "tc_nv.summary",
171 FT_BYTES, BASE_NONE, NULL, 0x0,
175 { &hf_nv_header, { "Header", "tc_nv.header",
176 FT_NONE, BASE_NONE, NULL, 0x0,
179 { &hf_nv_publisher, { "Publisher", "tc_nv.publisher",
180 FT_BYTES, BASE_NONE, NULL, 0x0,
183 { &hf_nv_count, { "Count", "tc_nv.count",
184 FT_UINT16, BASE_HEX, NULL, 0x0,
187 { &hf_nv_cycleindex, { "CycleIndex", "tc_nv.cycleindex",
188 FT_UINT16, BASE_HEX, NULL, 0x0,
191 { &hf_nv_variable, { "Variable", "tc_nv.variable",
192 FT_BYTES, BASE_NONE, NULL, 0x0,
195 { &hf_nv_varheader, { "VarHeader", "tc_nv.varheader",
196 FT_NONE, BASE_NONE, NULL, 0x0,
199 { &hf_nv_id, { "Id", "tc_nv.id",
200 FT_UINT16, BASE_HEX, NULL, 0x0,
203 { &hf_nv_hash, { "Hash", "tc_nv.hash",
204 FT_UINT16, BASE_HEX, NULL, 0x0,
207 { &hf_nv_length, { "Length", "tc_nv.length",
208 FT_UINT16, BASE_HEX, NULL, 0x0,
211 { &hf_nv_quality, { "Quality", "tc_nv.quality",
212 FT_UINT16, BASE_HEX, NULL, 0x0,
215 { &hf_nv_data, { "Data", "tc_nv.data",
216 FT_BYTES, BASE_NONE, NULL, 0x0,
229 proto_nv = proto_register_protocol("TwinCAT NV",
231 proto_register_field_array(proto_nv,hf,array_length(hf));
232 proto_register_subtree_array(ett,array_length(ett));
235 void proto_reg_handoff_nv(void)
237 dissector_handle_t nv_handle;
239 nv_handle = create_dissector_handle(dissect_nv, proto_nv);
240 dissector_add_uint("ecatf.type", 4, nv_handle);