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