e667f6dfcc90304c1b31099442d9079b949930b5
[obnox/wireshark/wip.git] / asn1 / sv / packet-sv-template.c
1 /* packet-sv.c
2  * Routines for IEC 61850 Sampled Vales packet dissection
3  * Michael Bernhard 2008
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/asn1.h>
33 #include <epan/etypes.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "packet-ber.h"
39 #include "packet-acse.h"
40
41 #include "tap.h"
42
43 #include "packet-sv.h"
44
45 #define PNAME  "IEC61850 Sampled Values"
46 #define PSNAME "SV"
47 #define PFNAME "sv"
48
49 /* see IEC61850-8-1 8.2 */
50 #define Q_VALIDITY_GOOD                 (0x0 << 0)
51 #define Q_VALIDITY_INVALID              (0x1 << 0)
52 #define Q_VALIDITY_QUESTIONABLE (0x3 << 0)
53 #define Q_VALIDITY_MASK                 (0x3 << 0)
54
55 #define Q_OVERFLOW                              (1 << 2)
56 #define Q_OUTOFRANGE                    (1 << 3)
57 #define Q_BADREFERENCE                  (1 << 4)
58 #define Q_OSCILLATORY                   (1 << 5)
59 #define Q_FAILURE                               (1 << 6)
60 #define Q_OLDDATA                               (1 << 7)
61 #define Q_INCONSISTENT                  (1 << 8)
62 #define Q_INACCURATE                    (1 << 9)
63
64 #define Q_SOURCE_PROCESS                (0 << 10)
65 #define Q_SOURCE_SUBSTITUTED    (1 << 10)
66 #define Q_SOURCE_MASK                   (1 << 10)
67
68 #define Q_TEST                                  (1 << 11)
69 #define Q_OPERATORBLOCKED               (1 << 12)
70
71 /* see UCA Implementation Guideline for IEC 61850-9-2 */
72 #define Q_DERIVED                               (1 << 13)
73
74
75 /* Data for SV tap */
76 static int sv_tap = -1;
77 static sv_frame_data sv_data;
78
79 /* Initialize the protocol and registered fields */
80 static int proto_sv = -1;
81 static int hf_sv_appid = -1;
82 static int hf_sv_length = -1;
83 static int hf_sv_reserve1 = -1;
84 static int hf_sv_reserve2 = -1;
85 static int hf_sv_phmeas_instmag_i = -1;
86 static int hf_sv_phsmeas_q = -1;
87 static int hf_sv_phsmeas_q_validity = -1;
88 static int hf_sv_phsmeas_q_overflow = -1;
89 static int hf_sv_phsmeas_q_outofrange = -1;
90 static int hf_sv_phsmeas_q_badreference = -1;
91 static int hf_sv_phsmeas_q_oscillatory = -1;
92 static int hf_sv_phsmeas_q_failure = -1;
93 static int hf_sv_phsmeas_q_olddata = -1;
94 static int hf_sv_phsmeas_q_inconsistent = -1;
95 static int hf_sv_phsmeas_q_inaccurate = -1;
96 static int hf_sv_phsmeas_q_source = -1;
97 static int hf_sv_phsmeas_q_test = -1;
98 static int hf_sv_phsmeas_q_operatorblocked = -1;
99 static int hf_sv_phsmeas_q_derived = -1;
100
101 #include "packet-sv-hf.c"
102
103 /* Initialize the subtree pointers */
104 static int ett_sv = -1;
105 static int ett_phsmeas = -1;
106 static int ett_phsmeas_q = -1;
107
108 #include "packet-sv-ett.c"
109
110 static const value_string sv_q_validity_vals[] = {
111   {   0, "good" },
112   {   1, "invalid" },
113   {   3, "questionable" },
114   { 0, NULL }
115 };
116
117 static const value_string sv_q_source_vals[] = {
118   {   0, "process" },
119   {   1, "substituted" },
120   { 0, NULL }
121 };
122
123 static int
124 dissect_PhsMeas1(gboolean implicit_tag, packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id _U_)
125 {
126         gint8 class;
127         gboolean pc;
128         gint32 tag;
129         guint32 len;
130         proto_item *it;
131         proto_tree *subtree = NULL;
132         gint32 value;
133         guint32 qual;
134         guint32 i;
135
136         static const int *q_flags[] = {
137                 &hf_sv_phsmeas_q_validity,
138                 &hf_sv_phsmeas_q_overflow,
139                 &hf_sv_phsmeas_q_outofrange,
140                 &hf_sv_phsmeas_q_badreference,
141                 &hf_sv_phsmeas_q_oscillatory,
142                 &hf_sv_phsmeas_q_failure,
143                 &hf_sv_phsmeas_q_olddata,
144                 &hf_sv_phsmeas_q_inconsistent,
145                 &hf_sv_phsmeas_q_inaccurate,
146                 &hf_sv_phsmeas_q_source,
147                 &hf_sv_phsmeas_q_test,
148                 &hf_sv_phsmeas_q_operatorblocked,
149                 &hf_sv_phsmeas_q_derived,
150                 NULL
151                 };
152
153         if (!implicit_tag) {
154                 offset=dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
155                 offset=dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
156         } else {
157                 len=tvb_length_remaining(tvb, offset);
158         }
159
160         if (tree) {
161                 it = proto_tree_add_text(tree, tvb, offset, len, "PhsMeas1");
162                 subtree = proto_item_add_subtree(it, ett_phsmeas);
163         }
164
165         sv_data.num_phsMeas = 0;
166         for (i = 0; i < len/8; i++) {
167                 if (tree && subtree) {
168                         value = tvb_get_ntohl(tvb, offset);
169                         qual = tvb_get_ntohl(tvb, offset + 4);
170
171                         proto_tree_add_item(subtree, hf_sv_phmeas_instmag_i, tvb, offset, 4, FALSE);
172                         proto_tree_add_bitmask(subtree, tvb, offset + 4, hf_sv_phsmeas_q, ett_phsmeas_q, q_flags, FALSE);
173
174                         if (i < IEC61850_SV_MAX_PHSMEAS_ENTRIES) {
175                                 sv_data.phsMeas[i].value = value;
176                                 sv_data.phsMeas[i].qual = qual;
177                                 sv_data.num_phsMeas++;
178                         }
179                 }
180
181                 offset += 8;
182         }
183
184         return offset;
185 }
186
187 #include "packet-sv-fn.c"
188
189 /*
190 * Dissect SV PDUs inside a PPDU.
191 */
192 static void
193 dissect_sv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
194 {
195         int offset = 0;
196         int old_offset;
197         proto_item *item = NULL;
198         proto_tree *tree = NULL;
199         asn1_ctx_t asn1_ctx;
200
201         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
202
203         if (parent_tree){
204                 item = proto_tree_add_item(parent_tree, proto_sv, tvb, 0, -1, FALSE);
205                 tree = proto_item_add_subtree(item, ett_sv);
206         }
207         col_set_str(pinfo->cinfo, COL_PROTOCOL, PNAME);
208         col_clear(pinfo->cinfo, COL_INFO);
209
210         /* APPID */
211         if (tree && tvb_reported_length_remaining(tvb, offset) >= 2)
212                 proto_tree_add_item(tree, hf_sv_appid, tvb, offset, 2, FALSE);
213
214         /* Length */
215         if (tree && tvb_reported_length_remaining(tvb, offset) >= 4)
216                 proto_tree_add_item(tree, hf_sv_length, tvb, offset + 2, 2, FALSE);
217
218         /* Reserved 1 */
219         if (tree && tvb_reported_length_remaining(tvb, offset) >= 6)
220                 proto_tree_add_item(tree, hf_sv_reserve1, tvb, offset + 4, 2, FALSE);
221
222         /* Reserved 2 */
223         if (tree && tvb_reported_length_remaining(tvb, offset) >= 8)
224                 proto_tree_add_item(tree, hf_sv_reserve2, tvb, offset + 6, 2, FALSE);
225
226         offset = 8;
227         while (tree && tvb_reported_length_remaining(tvb, offset) > 0){
228                 old_offset = offset;
229                 offset = dissect_sv_SampledValues(FALSE, tvb, offset, &asn1_ctx , tree, -1);
230                 if (offset == old_offset) {
231                         proto_tree_add_text(tree, tvb, offset, -1, "Internal error, zero-byte SV PDU");
232                         offset = tvb_length(tvb);
233                         break;
234                 }
235         }
236
237         if(tree)
238                 tap_queue_packet(sv_tap, pinfo, &sv_data);
239 }
240
241
242 /*--- proto_register_sv -------------------------------------------*/
243 void proto_register_sv(void) {
244
245         /* List of fields */
246         static hf_register_info hf[] = {
247                 { &hf_sv_appid,
248                 { "APPID",      "sv.appid", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
249
250                 { &hf_sv_length,
251                 { "Length",     "sv.length", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
252
253                 { &hf_sv_reserve1,
254                 { "Reserved 1", "sv.reserve1", FT_UINT16, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
255
256                 { &hf_sv_reserve2,
257                 { "Reserved 2", "sv.reserve2", FT_UINT16, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
258
259                 { &hf_sv_phmeas_instmag_i,
260                 { "value", "sv.meas_value", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL}},
261
262                 { &hf_sv_phsmeas_q,
263                 { "quality", "sv.meas_quality", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}},
264
265                 { &hf_sv_phsmeas_q_validity,
266                 { "validity", "sv.meas_quality.validity", FT_UINT32, BASE_HEX, VALS(sv_q_validity_vals), Q_VALIDITY_MASK, NULL, HFILL}},
267
268                 { &hf_sv_phsmeas_q_overflow,
269                 { "overflow", "sv.meas_quality.overflow", FT_BOOLEAN, 32, NULL, Q_OVERFLOW, NULL, HFILL}},
270
271                 { &hf_sv_phsmeas_q_outofrange,
272                 { "out of range", "sv.meas_quality.outofrange", FT_BOOLEAN, 32, NULL, Q_OUTOFRANGE, NULL, HFILL}},
273
274                 { &hf_sv_phsmeas_q_badreference,
275                 { "bad reference", "sv.meas_quality.badreference", FT_BOOLEAN, 32, NULL, Q_BADREFERENCE, NULL, HFILL}},
276
277                 { &hf_sv_phsmeas_q_oscillatory,
278                 { "oscillatory", "sv.meas_quality.oscillatory", FT_BOOLEAN, 32, NULL, Q_OSCILLATORY, NULL, HFILL}},
279
280                 { &hf_sv_phsmeas_q_failure,
281                 { "failure", "sv.meas_quality.failure", FT_BOOLEAN, 32, NULL, Q_FAILURE, NULL, HFILL}},
282
283                 { &hf_sv_phsmeas_q_olddata,
284                 { "old data", "sv.meas_quality.olddata", FT_BOOLEAN, 32, NULL, Q_OLDDATA, NULL, HFILL}},
285
286                 { &hf_sv_phsmeas_q_inconsistent,
287                 { "inconsistent", "sv.meas_quality.inconsistent", FT_BOOLEAN, 32, NULL, Q_INCONSISTENT, NULL, HFILL}},
288
289                 { &hf_sv_phsmeas_q_inaccurate,
290                 { "inaccurate", "sv.meas_quality.inaccurate", FT_BOOLEAN, 32, NULL, Q_INACCURATE, NULL, HFILL}},
291
292                 { &hf_sv_phsmeas_q_source,
293                 { "source", "sv.meas_quality.source", FT_UINT32, BASE_HEX, VALS(sv_q_source_vals), Q_SOURCE_MASK, NULL, HFILL}},
294
295                 { &hf_sv_phsmeas_q_test,
296                 { "test", "sv.meas_quality.teset", FT_BOOLEAN, 32, NULL, Q_TEST, NULL, HFILL}},
297
298                 { &hf_sv_phsmeas_q_operatorblocked,
299                 { "operator blocked", "sv.meas_quality.operatorblocked", FT_BOOLEAN, 32, NULL, Q_OPERATORBLOCKED, NULL, HFILL}},
300
301                 { &hf_sv_phsmeas_q_derived,
302                 { "derived", "sv.meas_quality.derived", FT_BOOLEAN, 32, NULL, Q_DERIVED, NULL, HFILL}},
303
304
305 #include "packet-sv-hfarr.c"
306         };
307
308         /* List of subtrees */
309         static gint *ett[] = {
310                 &ett_sv,
311                 &ett_phsmeas,
312                 &ett_phsmeas_q,
313 #include "packet-sv-ettarr.c"
314         };
315
316         /* Register protocol */
317         proto_sv = proto_register_protocol(PNAME, PSNAME, PFNAME);
318         register_dissector("sv", dissect_sv, proto_sv);
319
320         /* Register fields and subtrees */
321         proto_register_field_array(proto_sv, hf, array_length(hf));
322         proto_register_subtree_array(ett, array_length(ett));
323
324         /* Register tap */
325         sv_tap = register_tap("sv");
326 }
327
328 /*--- proto_reg_handoff_sv --- */
329 void proto_reg_handoff_sv(void) {
330
331         dissector_handle_t sv_handle;
332         sv_handle = find_dissector("sv");
333
334         dissector_add_uint("ethertype", ETHERTYPE_IEC61850_SV, sv_handle);
335 }