Update plugins/ with recent change.
[metze/wireshark/wip.git] / plugins / ethercat / packet-nv.c
1 /* packet-nv.c
2  * Routines for ethercat packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2007 by Beckhoff Automation GmbH
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /* Include files */
28
29 #include "config.h"
30
31 #include <glib.h>
32
33 #include <epan/packet.h>
34
35 #include "packet-nv.h"
36
37 /* Define the nv proto */
38 int proto_nv  = -1;
39
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;
44
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;
57
58 /*nv*/
59 static void NvSummaryFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
60 {
61    guint32 nvOffset = offset;
62
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));
71 }
72
73 static void NvPublisherFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
74 {
75    guint32 nvOffset = offset;
76
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));
84 }
85
86 static void NvVarHeaderFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
87 {
88    g_snprintf ( szText, nMax, "Variable - Id = %d, Length = %d",
89       tvb_get_letohs(tvb, offset),
90       tvb_get_letohs(tvb, offset+4));
91 }
92
93 static void dissect_nv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
94 {
95    proto_item *ti;
96    proto_tree *nv_tree, *nv_header_tree, *nv_var_tree,*nv_varheader_tree;
97    gint offset = 0;
98    char szText[200];
99    int nMax = (int)sizeof(szText)-1;
100
101    gint i;
102
103    col_set_str(pinfo->cinfo, COL_PROTOCOL, "TC-NV");
104
105    col_clear(pinfo->cinfo, COL_INFO);
106
107    NvSummaryFormater(tvb, offset, szText, nMax);
108    col_append_str(pinfo->cinfo, COL_INFO, szText);
109
110    if (tree)
111    {
112       guint16 nv_count;
113
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);
117
118       ti = proto_tree_add_item(nv_tree, hf_nv_header, tvb, offset, NvParserHDR_Len, ENC_NA);
119
120       nv_header_tree = proto_item_add_subtree(ti, ett_nv_header);
121
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);
126
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);
130
131       proto_tree_add_item(nv_header_tree, hf_nv_cycleindex, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
132       offset = NvParserHDR_Len;
133
134       for ( i=0; i < nv_count; i++ )
135       {
136          guint16 var_length = tvb_get_letohs(tvb, offset+4);
137
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);
141
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);
144
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);
148
149          proto_tree_add_item(nv_varheader_tree, hf_nv_hash, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
150          offset+=(int)sizeof(guint16);
151
152          proto_tree_add_item(nv_varheader_tree, hf_nv_length, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
153          offset+=(int)sizeof(guint16);
154
155          proto_tree_add_item(nv_varheader_tree, hf_nv_quality, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
156          offset+=(int)sizeof(guint16);
157
158          proto_tree_add_item(nv_var_tree, hf_nv_data, tvb, offset, var_length, ENC_NA);
159          offset+=var_length;
160       }
161    }
162 }
163
164 void proto_register_nv(void)
165 {
166    static hf_register_info hf[] =
167    {
168 #if 0
169       { &hf_nv_summary,
170       { "Summary of the Nv Packet", "tc_nv.summary",
171       FT_BYTES, BASE_NONE, NULL, 0x0,
172       NULL, HFILL }
173       },
174 #endif
175       { &hf_nv_header, { "Header", "tc_nv.header",
176       FT_NONE, BASE_NONE, NULL, 0x0,
177       NULL, HFILL }
178       },
179       { &hf_nv_publisher, { "Publisher", "tc_nv.publisher",
180       FT_BYTES, BASE_NONE, NULL, 0x0,
181       NULL, HFILL }
182       },
183       { &hf_nv_count, { "Count", "tc_nv.count",
184       FT_UINT16, BASE_HEX, NULL, 0x0,
185       NULL, HFILL }
186       },
187       { &hf_nv_cycleindex, { "CycleIndex", "tc_nv.cycleindex",
188       FT_UINT16, BASE_HEX, NULL, 0x0,
189       NULL, HFILL }
190       },
191       { &hf_nv_variable, { "Variable", "tc_nv.variable",
192       FT_BYTES, BASE_NONE, NULL, 0x0,
193       NULL, HFILL }
194       },
195       { &hf_nv_varheader, { "VarHeader", "tc_nv.varheader",
196       FT_NONE, BASE_NONE, NULL, 0x0,
197       NULL, HFILL }
198       },
199       { &hf_nv_id, { "Id", "tc_nv.id",
200       FT_UINT16, BASE_HEX, NULL, 0x0,
201       NULL, HFILL }
202       },
203       { &hf_nv_hash, { "Hash", "tc_nv.hash",
204       FT_UINT16, BASE_HEX, NULL, 0x0,
205       NULL, HFILL }
206       },
207       { &hf_nv_length, { "Length", "tc_nv.length",
208       FT_UINT16, BASE_HEX, NULL, 0x0,
209       NULL, HFILL }
210       },
211       { &hf_nv_quality, { "Quality", "tc_nv.quality",
212       FT_UINT16, BASE_HEX, NULL, 0x0,
213       NULL, HFILL }
214       },
215       { &hf_nv_data, { "Data", "tc_nv.data",
216       FT_BYTES, BASE_NONE, NULL, 0x0,
217       NULL, HFILL }
218       },
219    };
220
221    static gint *ett[] =
222    {
223       &ett_nv,
224       &ett_nv_header,
225       &ett_nv_var,
226       &ett_nv_varheader
227    };
228
229    proto_nv = proto_register_protocol("TwinCAT NV",
230       "TC-NV","tc_nv");
231    proto_register_field_array(proto_nv,hf,array_length(hf));
232    proto_register_subtree_array(ett,array_length(ett));
233 }
234
235 void proto_reg_handoff_nv(void)
236 {
237    dissector_handle_t nv_handle;
238
239    nv_handle = create_dissector_handle(dissect_nv, proto_nv);
240    dissector_add_uint("ecatf.type", 4, nv_handle);
241 }