eaf28f76bb95f40052a2f328ac04841c9c415730
[metze/wireshark/wip.git] / plugins / sercosiii / packet-sercosiii_1v1_at.c
1 /* packet-sercosiii_1v1_at.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 ett_siii_at = -1;
40 static gint ett_siii_at_svc = -1;
41 static gint ett_siii_at_devstats = -1;
42
43 static gint ett_siii_at_svc_channel[MAX_SERCOS_DEVICES];
44 static gint ett_siii_at_dev_status[MAX_SERCOS_DEVICES];
45
46 void dissect_siii_at_cp0(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
47 {
48   guint16 seqcnt; /* sequence counter */
49   guint16 tfield; /* topology field for sercos addresses */
50   guint16 i;
51   char devices[]="Recognized Devices"; /* fixme: it would be nice to have this as subtree */
52   static char outbuf[200];
53
54   proto_tree_add_text(tree, tvb, 0, 1024, "%s", devices);
55
56   /* check sequence count field */
57   seqcnt = tvb_get_letohs(tvb, 0);
58   g_snprintf(outbuf, sizeof(outbuf), "Number of Devices: %u", (0x1FF & seqcnt)-1);
59   proto_tree_add_text(tree, tvb, 0, 2, "%s", outbuf);
60
61   /* check SERCOS address of each topology field */
62   for(i=1;i < MAX_SERCOS_DEVICES; ++i)
63   {
64     tfield = tvb_get_letohs(tvb, i*2);
65
66     if(tfield == 0)
67     {
68       g_snprintf(outbuf, sizeof(outbuf), "Device Address %u: No SERCOS Address", i);
69     }
70     else if(tfield == 0xFFFF)
71     {
72       g_snprintf(outbuf, sizeof(outbuf), "Device Address %u: No Device", i);
73     }
74     else
75     {
76       g_snprintf(outbuf, sizeof(outbuf), "Device Address %u: %u", i, tfield);
77     }
78     proto_tree_add_text(tree, tvb, i*2, 2, "%s", outbuf);
79   }
80 }
81
82 void dissect_siii_at_cp1_2(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint telno)
83 {
84   guint devstart = telno * 128; /* AT0: slaves 0-127; AT1: slaves 128-255; ... */
85   tvbuff_t* tvb_n;
86
87   guint idx;
88
89   proto_item* ti; /* temporary item */
90   proto_tree* subtree;
91   proto_tree* subtree_svc;
92   proto_tree* subtree_devstat;
93
94   ti = proto_tree_add_text(tree, tvb, 0, 128 * 6, "Service Channel");
95   subtree_svc = proto_item_add_subtree(ti, ett_siii_at_svc);
96
97   ti = proto_tree_add_text(tree, tvb, 128 * 6, 512, "Device Status");
98   subtree_devstat = proto_item_add_subtree(ti, ett_siii_at_devstats);
99
100   for(idx = 0; idx < 128; ++idx) /* each AT of CP1/2 has data of 128 different slaves */
101   {
102     tvb_n = tvb_new_subset(tvb, 6 * idx, 6, 6); /* subset for service channel data */
103
104     ti = proto_tree_add_text(subtree_svc, tvb_n, 0, 6, "Device %u", idx + devstart);
105     subtree = proto_item_add_subtree(ti, ett_siii_at_svc_channel[idx]);
106     dissect_siii_at_svc(tvb_n, pinfo, subtree, idx + devstart);
107
108     tvb_n = tvb_new_subset(tvb, 128 * 6 + 4 * idx, 2, 2); /* subset for device status information */
109
110     ti = proto_tree_add_text(subtree_devstat, tvb_n, 0, 2, "Device %u", idx + devstart);
111     subtree = proto_item_add_subtree(ti, ett_siii_at_dev_status[idx]);
112     dissect_siii_at_devstat(tvb_n, pinfo, subtree);
113   }
114 }
115
116 void dissect_siii_at_cp3_4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint telno)
117 {
118   proto_item* ti;
119   proto_tree* subtree_svc;
120   proto_tree* subtree_devstat;
121
122   if(0 == telno) /* dissect hotplug field in AT0 only */
123     dissect_siii_at_hp(tvb, pinfo, tree);
124
125   /* offsets of service channel, device status and connections are unknown
126    * this data could be extracted from svc communication during CP2
127    */
128   ti = proto_tree_add_text(tree, tvb, 0, 0, "Service Channels");
129   subtree_svc = proto_item_add_subtree(ti, ett_siii_at_svc);
130
131   ti = proto_tree_add_text(tree, tvb, 0, 0, "Device Status");
132   subtree_devstat = proto_item_add_subtree(ti, ett_siii_at_devstats);
133 }
134
135
136 void dissect_siii_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
137 {
138   proto_item*  ti; /* temporary item */
139   proto_tree* subtree;
140   tvbuff_t* tvb_n;
141
142   guint8 phase;
143   guint telno;
144
145   phase = (tvb_get_guint8(tvb, 1)&0x8F); /* read communication phase out of SERCOS III header*/
146   telno = (tvb_get_guint8(tvb, 0) & 0xF); /* read number of AT out of SERCOS III header */
147
148   if(check_col(pinfo->cinfo, COL_PROTOCOL))
149     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIII AT");
150
151   if(check_col(pinfo->cinfo, COL_INFO))
152   {
153       if(phase & 0x80) /* communication phase switching in progress */
154       {
155         col_append_fstr(pinfo->cinfo, COL_INFO, " Phase=CP?s -> CP%u",
156               (phase&0x0f));
157       }
158       else /* communication as usual */
159       {
160         col_append_fstr(pinfo->cinfo, COL_INFO, " Phase=CP%u",
161               (phase&0x0f));
162       }
163   }
164
165   ti = proto_tree_add_text(tree, tvb, 0, -1, "AT%u", telno);
166   subtree = proto_item_add_subtree(ti, ett_siii_at);
167
168   dissect_siii_mst(tvb, pinfo, subtree); /* dissect SERCOS III header */
169
170     switch(phase) /* call the AT dissector depending on the current communication phase */
171     {
172     case COMMUNICATION_PHASE_0: /* CP0 */
173       tvb_n = tvb_new_subset(tvb, 6, 1024, 1024);
174       dissect_siii_at_cp0(tvb_n, pinfo, subtree);
175     break;
176
177     case COMMUNICATION_PHASE_1: /* CP1 */
178     case COMMUNICATION_PHASE_2: /* CP2 */
179       tvb_n = tvb_new_subset(tvb, 6, 1280, 1280);
180       dissect_siii_at_cp1_2(tvb_n, pinfo, subtree, telno);
181     break;
182
183     case COMMUNICATION_PHASE_3: /* CP3 */
184     case COMMUNICATION_PHASE_4: /* CP4 */
185       tvb_n = tvb_new_subset(tvb, 6, -1, -1);
186       dissect_siii_at_cp3_4(tvb_n, pinfo, subtree, telno);
187     break;
188
189     default:
190       proto_tree_add_text(tree, tvb, 6, -1, "CP is unknown");
191     break;
192     }
193 }
194
195 void dissect_siii_at_init(gint proto_siii _U_)
196 {
197   gint idx;
198
199   /* Setup protocol subtree array */
200   static gint *ett[] = {
201     &ett_siii_at,
202     &ett_siii_at_svc,
203     &ett_siii_at_devstats
204   };
205
206   static gint* etts[MAX_SERCOS_DEVICES];
207
208   for(idx = 0; idx < MAX_SERCOS_DEVICES; ++idx)
209   {
210     ett_siii_at_svc_channel[idx] = -1;
211     etts[idx] = &ett_siii_at_svc_channel[idx];
212   }
213   proto_register_subtree_array(etts, array_length(etts));
214
215   for(idx = 0; idx < MAX_SERCOS_DEVICES; ++idx)
216   {
217     ett_siii_at_dev_status[idx] = -1;
218     etts[idx] = &ett_siii_at_dev_status[idx];
219   }
220   proto_register_subtree_array(etts, array_length(etts));
221
222   /* Required function calls to register the header fields and subtrees used */
223   proto_register_subtree_array(ett, array_length(ett));
224 }