aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/ethercat/packet-nv.c
blob: 10e2750fe98e5b4d1a8fa9ac27e0766464204a69 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* packet-nv.c
 * Routines for ethercat packet disassembly
 *
 * Copyright (c) 2007 by Beckhoff Automation GmbH
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Include files */

#include "config.h"

#include <epan/packet.h>

#include "packet-nv.h"

void proto_register_nv(void);
void proto_reg_handoff_nv(void);

/* Define the nv proto */
int proto_nv  = -1;

static int ett_nv = -1;
static int ett_nv_header = -1;
static int ett_nv_var = -1;
static int ett_nv_varheader = -1;

/* static int hf_nv_summary = -1; */
static int hf_nv_header = -1;
static int hf_nv_publisher = -1;
static int hf_nv_count = -1;
static int hf_nv_cycleindex = -1;
static int hf_nv_variable = -1;
static int hf_nv_varheader = -1;
static int hf_nv_id = -1;
static int hf_nv_hash = -1;
static int hf_nv_length = -1;
static int hf_nv_quality = -1;
static int hf_nv_data = -1;

/*nv*/
static void NvSummaryFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
{
   guint32 nvOffset = offset;

   g_snprintf ( szText, nMax, "Network Vars from %d.%d.%d.%d.%d.%d - %d Var(s)",
      tvb_get_guint8(tvb, nvOffset),
      tvb_get_guint8(tvb, nvOffset+1),
      tvb_get_guint8(tvb, nvOffset+2),
      tvb_get_guint8(tvb, nvOffset+3),
      tvb_get_guint8(tvb, nvOffset+4),
      tvb_get_guint8(tvb, nvOffset+5),
      tvb_get_letohs(tvb, nvOffset+6));
}

static void NvPublisherFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
{
   guint32 nvOffset = offset;

   g_snprintf ( szText, nMax, "Publisher %d.%d.%d.%d.%d.%d",
      tvb_get_guint8(tvb, nvOffset),
      tvb_get_guint8(tvb, nvOffset+1),
      tvb_get_guint8(tvb, nvOffset+2),
      tvb_get_guint8(tvb, nvOffset+3),
      tvb_get_guint8(tvb, nvOffset+4),
      tvb_get_guint8(tvb, nvOffset+5));
}

static void NvVarHeaderFormater(tvbuff_t *tvb, gint offset, char *szText, int nMax)
{
   g_snprintf ( szText, nMax, "Variable - Id = %d, Length = %d",
      tvb_get_letohs(tvb, offset),
      tvb_get_letohs(tvb, offset+4));
}

static int dissect_nv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
   proto_item *ti;
   proto_tree *nv_tree, *nv_header_tree, *nv_var_tree,*nv_varheader_tree;
   gint offset = 0;
   char szText[200];
   int nMax = (int)sizeof(szText)-1;

   gint i;

   col_set_str(pinfo->cinfo, COL_PROTOCOL, "TC-NV");

   col_clear(pinfo->cinfo, COL_INFO);

   NvSummaryFormater(tvb, offset, szText, nMax);
   col_append_str(pinfo->cinfo, COL_INFO, szText);

   if (tree)
   {
      guint16 nv_count;

      ti = proto_tree_add_item(tree, proto_nv, tvb, 0, -1, ENC_NA);
      nv_tree = proto_item_add_subtree(ti, ett_nv);
      proto_item_append_text(ti,": %s",szText);

      ti = proto_tree_add_item(nv_tree, hf_nv_header, tvb, offset, NvParserHDR_Len, ENC_NA);

      nv_header_tree = proto_item_add_subtree(ti, ett_nv_header);

      ti= proto_tree_add_item(nv_header_tree, hf_nv_publisher, tvb, offset, (int)sizeof(guint8)*6, ENC_NA);
      NvPublisherFormater(tvb, offset, szText, nMax);
      proto_item_set_text(ti, "%s", szText);
      offset+=((int)sizeof(guint8)*6);

      proto_tree_add_item(nv_header_tree, hf_nv_count, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
      nv_count = tvb_get_letohs(tvb, offset);
      offset+=(int)sizeof(guint16);

      proto_tree_add_item(nv_header_tree, hf_nv_cycleindex, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
      offset = NvParserHDR_Len;

      for ( i=0; i < nv_count; i++ )
      {
         guint16 var_length = tvb_get_letohs(tvb, offset+4);

         ti = proto_tree_add_item(nv_tree, hf_nv_variable, tvb, offset, ETYPE_88A4_NV_DATA_HEADER_Len+var_length, ENC_NA);
         NvVarHeaderFormater(tvb, offset, szText, nMax);
         proto_item_set_text(ti, "%s", szText);

         nv_var_tree = proto_item_add_subtree(ti, ett_nv_var);
         ti = proto_tree_add_item(nv_var_tree, hf_nv_varheader, tvb, offset, ETYPE_88A4_NV_DATA_HEADER_Len, ENC_NA);

         nv_varheader_tree = proto_item_add_subtree(ti, ett_nv_varheader);
         proto_tree_add_item(nv_varheader_tree, hf_nv_id, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
         offset+=(int)sizeof(guint16);

         proto_tree_add_item(nv_varheader_tree, hf_nv_hash, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
         offset+=(int)sizeof(guint16);

         proto_tree_add_item(nv_varheader_tree, hf_nv_length, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
         offset+=(int)sizeof(guint16);

         proto_tree_add_item(nv_varheader_tree, hf_nv_quality, tvb, offset, (int)sizeof(guint16), ENC_LITTLE_ENDIAN);
         offset+=(int)sizeof(guint16);

         proto_tree_add_item(nv_var_tree, hf_nv_data, tvb, offset, var_length, ENC_NA);
         offset+=var_length;
      }
   }
   return tvb_captured_length(tvb);
}

void proto_register_nv(void)
{
   static hf_register_info hf[] =
      {
#if 0
         { &hf_nv_summary, { "Summary of the Nv Packet", "tc_nv.summary",
                             FT_BYTES, BASE_NONE, NULL, 0x0,
                             NULL, HFILL }
         },
#endif
         { &hf_nv_header, { "Header", "tc_nv.header",
                            FT_NONE, BASE_NONE, NULL, 0x0,
                            NULL, HFILL }
         },
         { &hf_nv_publisher, { "Publisher", "tc_nv.publisher",
                               FT_BYTES, BASE_NONE, NULL, 0x0,
                               NULL, HFILL }
         },
         { &hf_nv_count, { "Count", "tc_nv.count",
                           FT_UINT16, BASE_HEX, NULL, 0x0,
                           NULL, HFILL }
         },
         { &hf_nv_cycleindex, { "CycleIndex", "tc_nv.cycleindex",
                                FT_UINT16, BASE_HEX, NULL, 0x0,
                                NULL, HFILL }
         },
         { &hf_nv_variable, { "Variable", "tc_nv.variable",
                              FT_BYTES, BASE_NONE, NULL, 0x0,
                              NULL, HFILL }
         },
         { &hf_nv_varheader, { "VarHeader", "tc_nv.varheader",
                               FT_NONE, BASE_NONE, NULL, 0x0,
                               NULL, HFILL }
         },
         { &hf_nv_id, { "Id", "tc_nv.id",
                        FT_UINT16, BASE_HEX, NULL, 0x0,
                        NULL, HFILL }
         },
         { &hf_nv_hash, { "Hash", "tc_nv.hash",
                          FT_UINT16, BASE_HEX, NULL, 0x0,
                          NULL, HFILL }
         },
         { &hf_nv_length, { "Length", "tc_nv.length",
                            FT_UINT16, BASE_HEX, NULL, 0x0,
                            NULL, HFILL }
         },
         { &hf_nv_quality, { "Quality", "tc_nv.quality",
                             FT_UINT16, BASE_HEX, NULL, 0x0,
                             NULL, HFILL }
         },
         { &hf_nv_data, { "Data", "tc_nv.data",
                          FT_BYTES, BASE_NONE, NULL, 0x0,
                          NULL, HFILL }
         },
      };

   static gint *ett[] =
      {
         &ett_nv,
         &ett_nv_header,
         &ett_nv_var,
         &ett_nv_varheader
      };

   proto_nv = proto_register_protocol("TwinCAT NV",
                                      "TC-NV","tc_nv");
   proto_register_field_array(proto_nv,hf,array_length(hf));
   proto_register_subtree_array(ett,array_length(ett));
}

void proto_reg_handoff_nv(void)
{
   dissector_handle_t nv_handle;

   nv_handle = create_dissector_handle(dissect_nv, proto_nv);
   dissector_add_uint("ecatf.type", 4, nv_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 3
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=3 tabstop=8 expandtab:
 * :indentSize=3:tabSize=8:noTabs=true:
 */