#include <prefs.h> not required
[obnox/wireshark/wip.git] / plugins / sercosiii / packet-sercosiii_1v1_mdt.c
1 /* packet-sercosiii_1v1_mdt.c
2  * Routines for SERCOS III dissection
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <glib.h>
34
35 #include <epan/packet.h>
36
37 #include "packet-sercosiii.h"
38
39 static gint hf_siii_mdt_version = -1;
40 static gint hf_siii_mdt_version_initprocvers = -1;
41 static gint hf_siii_mdt_version_num_mdt_at_cp1_2 = -1;
42 static gint hf_siii_mdt_version_revision = -1;
43
44 static const value_string siii_mdt_version_num_mdtat_cp1_2_text[]=
45 {
46   {0x00, "2 MDTs/ATs in CP1/2"},
47   {0x01, "4 MDTs/ATs in CP1/2"},
48   {0, NULL}
49 };
50
51 static const value_string siii_mdt_version_initprocvers_text[]=
52 {
53   {0x00, "No remote address allocation"},
54   {0x01, "Remote address allocation"},
55   {0, NULL}
56 };
57
58 static gint ett_siii_mdt = -1;
59 static gint ett_siii_mdt_svc = -1;
60 static gint ett_siii_mdt_devctrls = -1;
61 static gint ett_siii_mdt_version = -1;
62 static gint ett_siii_mdt_svc_channel[MAX_SERCOS_DEVICES] = {-1};
63 static gint ett_siii_mdt_dev_control[MAX_SERCOS_DEVICES] = {-1};
64
65 void dissect_siii_mdt_cp0(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
66 {
67   proto_item* ti;
68   proto_tree* subtree;
69   ti = proto_tree_add_item(tree, hf_siii_mdt_version, tvb, 0, 4, TRUE);
70   subtree = proto_item_add_subtree(ti, ett_siii_mdt_version);
71
72   proto_tree_add_item(subtree, hf_siii_mdt_version_num_mdt_at_cp1_2, tvb, 0, 4, TRUE);
73   proto_tree_add_item(subtree, hf_siii_mdt_version_initprocvers, tvb, 0, 4, TRUE);
74   proto_tree_add_item(subtree, hf_siii_mdt_version_revision, tvb, 0, 4, TRUE);
75
76 }
77
78 void dissect_siii_mdt_cp1_2(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint telno)
79 {
80   guint devstart = telno * 128; /* MDT0: slaves 0-127; MDT1: slaves 128-255; ... */
81   tvbuff_t* tvb_n;
82
83   guint idx;
84
85   proto_item* ti;
86   proto_tree* subtree;
87   proto_tree* subtree_svc;
88   proto_tree* subtree_devctrl;
89
90   ti = proto_tree_add_text(tree, tvb, 0, 128 * 6, "Service Channels");
91   subtree_svc = proto_item_add_subtree(ti, ett_siii_mdt_svc);
92
93   ti = proto_tree_add_text(tree, tvb, 128 * 6, 512, "Device Control");
94   subtree_devctrl = proto_item_add_subtree(ti, ett_siii_mdt_svc);
95
96   for(idx = 0; idx < 128; ++idx) /* each MDT of CP1/2 has data for 128 different slaves */
97   {
98     tvb_n = tvb_new_subset(tvb, 6 * idx, 6, 6); /* subset for service channel data */
99
100     ti = proto_tree_add_text(subtree_svc, tvb_n, 0, 6, "Device %u", idx + devstart);
101     subtree = proto_item_add_subtree(ti, ett_siii_mdt_svc_channel[idx]);
102     dissect_siii_mdt_svc(tvb_n, pinfo, subtree, idx + devstart);
103
104     tvb_n = tvb_new_subset(tvb, 128 * 6 + 4 * idx, 2, 2); /* subset for device control information */
105
106     ti = proto_tree_add_text(subtree_devctrl, tvb_n, 0, 2, "Device %u", idx + devstart);
107     subtree = proto_item_add_subtree(ti, ett_siii_mdt_dev_control[idx]);
108
109     dissect_siii_mdt_devctrl(tvb_n, pinfo, subtree);
110   }
111 }
112
113 void dissect_siii_mdt_cp3_4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint telno)
114 {
115   guint devstart _U_ = telno * 128;
116
117   proto_item* ti;
118   proto_tree* subtree_svc;
119   proto_tree* subtree_devctrl;
120
121   if(0 == telno) /* dissect hotplug field in MDT0 only */
122     dissect_siii_mdt_hp(tvb, pinfo, tree);
123
124   /* offsets of service channel, device status and connections are unknown
125    * this data could be extracted from svc communication during CP2
126    */
127   ti = proto_tree_add_text(tree, tvb, 0, 0, "Service Channels");
128   subtree_svc = proto_item_add_subtree(ti, ett_siii_mdt_svc);
129
130   ti = proto_tree_add_text(tree, tvb, 0, 0, "Device Controls");
131   subtree_devctrl = proto_item_add_subtree(ti, ett_siii_mdt_svc);
132 }
133
134
135 void dissect_siii_mdt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
136 {
137   proto_item* ti;
138   proto_tree* subtree;
139   tvbuff_t* tvb_n;
140
141   guint t_phase;
142   guint telno;
143
144   if(check_col(pinfo->cinfo, COL_PROTOCOL))
145     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIII MDT");
146
147   t_phase = (tvb_get_guint8(tvb, 1)&0x8F); /* read communication phase out of SERCOS III header */
148   telno = (tvb_get_guint8(tvb, 0) & 0xF); /* read number of MDT out of SERCOS III header */
149
150   if(check_col(pinfo->cinfo, COL_INFO))
151   {
152       if(t_phase & 0x80) /* communication phase switching in progress */
153       {
154         col_append_fstr(pinfo->cinfo, COL_INFO, " Phase=CP?s -> CP%u",
155               (t_phase&0x0f));
156       }
157       else /* communication as usual */
158       {
159         col_append_fstr(pinfo->cinfo, COL_INFO, " Phase=CP%u",
160               (t_phase&0x0f));
161       }
162   }
163
164   ti = proto_tree_add_text(tree, tvb, 0, -1, "MDT%u", telno);
165   subtree = proto_item_add_subtree(ti, ett_siii_mdt);
166
167   dissect_siii_mst(tvb, pinfo, subtree); /* dissect SERCOS III header */
168
169   switch(t_phase) /* call the MDT dissector depending on the current communication phase */
170   {
171   case COMMUNICATION_PHASE_0: /* CP0 */
172     tvb_n = tvb_new_subset(tvb, 6, 40, 40);
173     dissect_siii_mdt_cp0(tvb_n, pinfo, subtree);
174   break;
175
176   case COMMUNICATION_PHASE_1: /* CP1 */
177   case COMMUNICATION_PHASE_2: /* CP2 */
178     tvb_n = tvb_new_subset(tvb, 6, 1280, 1280);
179     dissect_siii_mdt_cp1_2(tvb_n, pinfo, subtree, telno);
180   break;
181
182   case COMMUNICATION_PHASE_3: /* CP3 */
183   case COMMUNICATION_PHASE_4: /* CP4 */
184     tvb_n = tvb_new_subset(tvb, 6, -1, -1);
185     dissect_siii_mdt_cp3_4(tvb_n, pinfo, subtree, telno);
186   break;
187
188   default:
189     proto_tree_add_text(tree, tvb, 6, -1, "CP is unknown");
190   }
191 }
192
193 void dissect_siii_mdt_init(gint proto_siii)
194 {
195   gint idx;
196
197   /* Setup list of header fields  See Section 1.6.1 for details*/
198   static hf_register_info hf_siii_header[] = {
199     { &hf_siii_mdt_version,
200       { "Communication Version", "siii.mdt.version",
201       FT_UINT32, BASE_HEX, NULL, 0,
202       NULL, HFILL }
203     },
204     { &hf_siii_mdt_version_revision,
205       { "Revision Number", "siii.mdt.version.revision",
206       FT_UINT32, BASE_HEX, NULL, 0x7F,
207       NULL, HFILL }
208     },
209     { &hf_siii_mdt_version_num_mdt_at_cp1_2,
210       { "Number of MDTs and ATS in CP1 and CP2", "siii.mdt.version.num_mdt_at_cp1_2",
211       FT_UINT32, BASE_HEX, VALS(siii_mdt_version_num_mdtat_cp1_2_text), 0x30000,
212       NULL, HFILL }
213     },
214     { &hf_siii_mdt_version_initprocvers,
215       { "Initialization Procedure Version Number", "siii.mdt.version.initprocvers",
216       FT_UINT32, BASE_HEX, VALS(siii_mdt_version_initprocvers_text), 0xFF00,
217       NULL, HFILL }
218     }
219   };
220   /* Setup protocol subtree array */
221   static gint *ett[] = {
222     &ett_siii_mdt,
223     &ett_siii_mdt_version,
224     &ett_siii_mdt_svc,
225     &ett_siii_mdt_devctrls
226   };
227
228   static gint* etts[MAX_SERCOS_DEVICES];
229
230   for(idx = 0; idx < MAX_SERCOS_DEVICES; ++idx)
231   {
232     etts[idx] = &ett_siii_mdt_svc_channel[idx];
233   }
234   proto_register_subtree_array(etts, array_length(etts));
235
236   for(idx = 0; idx < MAX_SERCOS_DEVICES; ++idx)
237   {
238     etts[idx] = &ett_siii_mdt_dev_control[idx];
239   }
240   proto_register_subtree_array(etts, array_length(etts));
241
242   proto_register_field_array(proto_siii, hf_siii_header, array_length(hf_siii_header));
243   proto_register_subtree_array(ett, array_length(ett));
244 }