Don't assign a value to a variable which is then never used: Coverity 1160 [UNUSED]
[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
115   if(0 == telno) /* dissect hotplug field in AT0 only */
116     dissect_siii_at_hp(tvb, pinfo, tree);
117
118   /* offsets of service channel, device status and connections are unknown
119    * this data could be extracted from svc communication during CP2
120    */
121   proto_tree_add_text(tree, tvb, 0, 0, "Service Channels");  
122   proto_tree_add_text(tree, tvb, 0, 0, "Device Status");
123 }
124
125
126 void dissect_siii_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
127 {
128   proto_item*  ti; /* temporary item */
129   proto_tree* subtree;
130   tvbuff_t* tvb_n;
131
132   guint8 phase;
133   guint telno;
134
135   phase = (tvb_get_guint8(tvb, 1)&0x8F); /* read communication phase out of SERCOS III header*/
136   telno = (tvb_get_guint8(tvb, 0) & 0xF); /* read number of AT out of SERCOS III header */
137
138   col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIII AT");
139
140   if(phase & 0x80) /* communication phase switching in progress */
141   {
142     col_append_fstr(pinfo->cinfo, COL_INFO, " Phase=CP?s -> CP%u",
143           (phase&0x0f));
144   }
145   else /* communication as usual */
146   {
147      col_append_fstr(pinfo->cinfo, COL_INFO, " Phase=CP%u",
148           (phase&0x0f));
149   }
150
151   ti = proto_tree_add_text(tree, tvb, 0, -1, "AT%u", telno);
152   subtree = proto_item_add_subtree(ti, ett_siii_at);
153
154   dissect_siii_mst(tvb, pinfo, subtree); /* dissect SERCOS III header */
155
156     switch(phase) /* call the AT dissector depending on the current communication phase */
157     {
158     case COMMUNICATION_PHASE_0: /* CP0 */
159       tvb_n = tvb_new_subset(tvb, 6, 1024, 1024);
160       dissect_siii_at_cp0(tvb_n, pinfo, subtree);
161     break;
162
163     case COMMUNICATION_PHASE_1: /* CP1 */
164     case COMMUNICATION_PHASE_2: /* CP2 */
165       tvb_n = tvb_new_subset(tvb, 6, 1280, 1280);
166       dissect_siii_at_cp1_2(tvb_n, pinfo, subtree, telno);
167     break;
168
169     case COMMUNICATION_PHASE_3: /* CP3 */
170     case COMMUNICATION_PHASE_4: /* CP4 */
171       tvb_n = tvb_new_subset_remaining(tvb, 6);
172       dissect_siii_at_cp3_4(tvb_n, pinfo, subtree, telno);
173     break;
174
175     default:
176       proto_tree_add_text(tree, tvb, 6, -1, "CP is unknown");
177     break;
178     }
179 }
180
181 void dissect_siii_at_init(gint proto_siii _U_)
182 {
183   gint idx;
184
185   /* Setup protocol subtree array */
186   static gint *ett[] = {
187     &ett_siii_at,
188     &ett_siii_at_svc,
189     &ett_siii_at_devstats
190   };
191
192   gint* etts[MAX_SERCOS_DEVICES];
193
194   for(idx = 0; idx < MAX_SERCOS_DEVICES; ++idx)
195   {
196     ett_siii_at_svc_channel[idx] = -1;
197     etts[idx] = &ett_siii_at_svc_channel[idx];
198   }
199   proto_register_subtree_array(etts, array_length(etts));
200
201   for(idx = 0; idx < MAX_SERCOS_DEVICES; ++idx)
202   {
203     ett_siii_at_dev_status[idx] = -1;
204     etts[idx] = &ett_siii_at_dev_status[idx];
205   }
206   proto_register_subtree_array(etts, array_length(etts));
207
208   /* Required function calls to register the header fields and subtrees used */
209   proto_register_subtree_array(ett, array_length(ett));
210 }