Replace glib memory with wmem memory
[metze/wireshark/wip.git] / epan / dissectors / packet-synphasor.c
1 /* packet-synphasor.c
2  * Dissector for IEEE C37.118 synchrophasor frames.
3  *
4  * Copyright 2008, Jens Steinhauser <jens.steinhauser@omicron.at>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28 #include <epan/conversation.h>
29 #include <epan/crc16-tvb.h>
30 #include <epan/dissectors/packet-tcp.h>
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/wmem/wmem.h>
34
35 #include <math.h>
36
37 #define PROTOCOL_NAME       "IEEE C37.118 Synchrophasor Protocol"
38 #define PROTOCOL_SHORT_NAME "SYNCHROPHASOR"
39 #define PROTOCOL_ABBREV     "synphasor"
40
41 /* forward references */
42 void proto_register_synphasor(void);
43 void proto_reg_handoff_synphasor(void);
44
45 /* global variables */
46
47 static int proto_synphasor       = -1;
48
49 /* user preferences */
50 static guint global_pref_tcp_port = 4712;
51 static guint global_pref_udp_port = 4713;
52
53 /* the ett... variables hold the state (open/close) of the treeview in the GUI */
54 static gint ett_synphasor          = -1; /* root element for this protocol */
55   /* used in the common header */
56   static gint ett_frtype           = -1;
57   static gint ett_timequal         = -1;
58   /* used for config frames */
59   static gint ett_conf             = -1;
60     static gint ett_conf_station   = -1;
61       static gint ett_conf_format  = -1;
62       static gint ett_conf_phnam   = -1;
63       static gint ett_conf_annam   = -1;
64       static gint ett_conf_dgnam   = -1;
65       static gint ett_conf_phconv  = -1;
66       static gint ett_conf_anconv  = -1;
67       static gint ett_conf_dgmask  = -1;
68   /* used for data frames */
69   static gint ett_data             = -1;
70     static gint ett_data_block     = -1;
71       static gint ett_data_stat    = -1;
72       static gint ett_data_phasors = -1;
73       static gint ett_data_analog  = -1;
74       static gint ett_data_digital = -1;
75   /* used for command frames */
76   static gint ett_command          = -1;
77
78 /* handles to the header fields hf[] in proto_register_synphasor() */
79 static int hf_sync                  = -1;
80 static int hf_sync_frtype           = -1;
81 static int hf_sync_version          = -1;
82 static int hf_idcode                = -1;
83 static int hf_frsize                = -1;
84 static int hf_soc                   = -1;
85 static int hf_timeqal_lsdir         = -1;
86 static int hf_timeqal_lsocc         = -1;
87 static int hf_timeqal_lspend        = -1;
88 static int hf_timeqal_timequalindic = -1;
89 static int hf_fracsec               = -1;
90 static int hf_conf_timebase         = -1;
91 static int hf_conf_numpmu           = -1;
92 static int hf_conf_formatb3         = -1;
93 static int hf_conf_formatb2         = -1;
94 static int hf_conf_formatb1         = -1;
95 static int hf_conf_formatb0         = -1;
96 static int hf_conf_fnom             = -1;
97 static int hf_conf_cfgcnt           = -1;
98 static int hf_data_statb15          = -1;
99 static int hf_data_statb14          = -1;
100 static int hf_data_statb13          = -1;
101 static int hf_data_statb12          = -1;
102 static int hf_data_statb11          = -1;
103 static int hf_data_statb10          = -1;
104 static int hf_data_statb05to04      = -1;
105 static int hf_data_statb03to00      = -1;
106 static int hf_command               = -1;
107
108 static dissector_handle_t synphasor_udp_handle;
109
110 /* the five different frame types for this protocol */
111 enum FrameType {
112         DATA = 0,
113         HEADER,
114         CFG1,
115         CFG2,
116         CMD
117 };
118
119 /* the channel names in the protocol are all 16 bytes
120  * long (and don't have to be NULL terminated) */
121 #define CHNAM_LEN 16
122
123 /* Structures to save CFG frame content. */
124
125 /* type to indicate the format for (D)FREQ/PHASORS/ANALOG in data frame  */
126 typedef enum { integer,         /* 16 bit signed integer */
127                floating_point   /* single precision floating point */
128 } data_format;
129
130 typedef enum { rect, polar } phasor_notation_e;
131
132 typedef enum { V, A } unit_e;
133
134 /* holds the information required to dissect a single phasor */
135 typedef struct {
136         char          name[CHNAM_LEN + 1];
137         unit_e        unit;
138         guint32       conv; /* conversation factor in 10^-5 scale */
139 } phasor_info;
140
141 /* holds the information for an analog value */
142 typedef struct {
143         char    name[CHNAM_LEN + 1];
144         guint32 conv; /* conversation factor, user defined scaling (so it's pretty useless) */
145 } analog_info;
146
147 /* holds information required to dissect a single PMU block in a data frame */
148 typedef struct {
149         guint16         id;                  /* identifies source of block     */
150         char            name[CHNAM_LEN + 1]; /* holds STN                      */
151         data_format     format_fr;           /* data format of FREQ and DFREQ  */
152         data_format     format_ph;           /* data format of PHASORS         */
153         data_format     format_an;           /* data format of ANALOG          */
154         phasor_notation_e phasor_notation;   /* format of the phasors          */
155         guint           fnom;                /* nominal line frequency         */
156         guint           num_dg;              /* number of digital status words */
157         wmem_array_t           *phasors;             /* array of phasor_infos          */
158         wmem_array_t           *analogs;             /* array of analog_infos          */
159 } config_block;
160
161 /* holds the id the configuration comes from an and
162  * an array of config_block members */
163 typedef struct {
164         guint32  fnum;          /* frame number */
165
166         guint16  id;
167         wmem_array_t    *config_blocks; /* Contains a config_block struct for
168                                  * every PMU included in the config frame */
169 } config_frame;
170
171 /* strings for type bits in SYNC */
172 static const value_string typenames[] = {
173         { 0, "Data Frame"            },
174         { 1, "Header Frame"          },
175         { 2, "Configuration Frame 1" },
176         { 3, "Configuration Frame 2" },
177         { 4, "Command Frame"         },
178         { 0, NULL                    }
179 };
180
181 /* strings for version bits in SYNC */
182 static const value_string versionnames[] = {
183         { 1, "IEEE C37.118-2005 initial publication" },
184         { 0, NULL                                    }
185 };
186
187 /* strings for the time quality flags in FRACSEC */
188 static const value_string timequalcodes[] = {
189         { 0xF, "Clock failure, time not reliable"    },
190         { 0xB, "Clock unlocked, time within 10 s"    },
191         { 0xA, "Clock unlocked, time within 1 s"     },
192         { 0x9, "Clock unlocked, time within 10^-1 s" },
193         { 0x8, "Clock unlocked, time within 10^-2 s" },
194         { 0x7, "Clock unlocked, time within 10^-3 s" },
195         { 0x6, "Clock unlocked, time within 10^-4 s" },
196         { 0x5, "Clock unlocked, time within 10^-5 s" },
197         { 0x4, "Clock unlocked, time within 10^-6 s" },
198         { 0x3, "Clock unlocked, time within 10^-7 s" },
199         { 0x2, "Clock unlocked, time within 10^-8 s" },
200         { 0x1, "Clock unlocked, time within 10^-9 s" },
201         { 0x0, "Normal operation, clock locked"      },
202         {  0 , NULL                                  }
203 };
204
205 /* strings for flags in the FORMAT word of a configuration frame */
206 static const true_false_string conf_formatb123names = {
207         "floating point",
208         "16-bit integer"
209 };
210 static const true_false_string conf_formatb0names = {
211         "polar",
212         "rectangular"
213 };
214
215 /* strings to decode ANUNIT in configuration frame */
216 static const range_string conf_anconvnames[] = {
217         {  0,   0, "single point-on-wave" },
218         {  1,   1, "rms of analog input"  },
219         {  2,   2, "peak of input"        },
220         {  3,   4, "undefined"            },
221         {  5,  64, "reserved"             },
222         { 65, 255, "user defined"         },
223         {  0,   0, NULL                   }
224 };
225
226 /* strings for the FNOM field */
227 static const true_false_string conf_fnomnames = {
228         "50Hz",
229         "60Hz"
230 };
231
232 /* strings for flags in the STAT word of a data frame */
233 static const true_false_string data_statb15names = {
234         "Data is invalid",
235         "Data is valid"
236 };
237 static const true_false_string data_statb14names = {
238         "Error",
239         "No error"
240 };
241 static const true_false_string data_statb13names = {
242         "Synchronization lost",
243         "Clock is synchronized"
244 };
245 static const true_false_string data_statb12names = {
246         "By arrival",
247         "By timestamp"
248 };
249 static const true_false_string data_statb11names = {
250         "Trigger detected",
251         "No trigger"
252 };
253 static const true_false_string data_statb10names = {
254         "Within 1 minute",
255         "No"
256 };
257 static const value_string      data_statb05to04names[] = {
258         { 0, "Time locked, best quality" },
259         { 1, "Unlocked for 10s"          },
260         { 2, "Unlocked for 100s"         },
261         { 3, "Unlocked for over 1000s"   },
262         { 0, NULL                        }
263 };
264 static const value_string      data_statb03to00names[] = {
265         { 0x0, "Manual"             },
266         { 0x1, "Magnitude low"      },
267         { 0x2, "Magnitude high"     },
268         { 0x3, "Phase-angel diff"   },
269         { 0x4, "Frequency high/low" },
270         { 0x5, "df/dt high"         },
271         { 0x6, "Reserved"           },
272         { 0x7, "Digital"            },
273         { 0x8, "User defined"       },
274         { 0x9, "User defined"       },
275         { 0xA, "User defined"       },
276         { 0xB, "User defined"       },
277         { 0xC, "User defined"       },
278         { 0xD, "User defined"       },
279         { 0xE, "User defined"       },
280         { 0xF, "User defined"       },
281         {  0 , NULL                 }
282 };
283
284 /* strings to decode the commands */
285 static const value_string command_names[] = {
286         {  0, "unknown command"       },
287         {  1, "data transmission off" },
288         {  2, "data transmission on"  },
289         {  3, "send HDR frame"        },
290         {  4, "send CFG-1 frame"      },
291         {  5, "send CFG-2 frame"      },
292         {  6, "unknown command"       },
293         {  7, "unknown command"       },
294         {  8, "extended frame"        },
295         {  9, "unknown command"       },
296         { 10, "unknown command"       },
297         { 11, "unknown command"       },
298         { 12, "unknown command"       },
299         { 13, "unknown command"       },
300         { 14, "unknown command"       },
301         { 15, "unknown command"       },
302         {  0, NULL                    }
303 };
304
305 /* Dissects a configuration frame (only the most important stuff, tries
306  * to be fast, does no GUI stuff) and returns a pointer to a config_frame
307  * struct that contains all the information from the frame needed to
308  * dissect a DATA frame.
309  *
310  * use 'config_frame_free()' to free the config_frame again
311  */
312 static config_frame* config_frame_fast(tvbuff_t *tvb)
313 {
314         guint16       idcode, num_pmu;
315         gint          offset;
316         config_frame *frame;
317
318         /* get a new frame and initialize it */
319         frame = wmem_new(wmem_file_scope(), config_frame);
320
321         frame->config_blocks = wmem_array_new(wmem_file_scope(), sizeof(config_block));
322
323         idcode = tvb_get_ntohs(tvb, 4);
324         frame->id       = idcode;
325
326         num_pmu = tvb_get_ntohs(tvb, 18);
327         offset = 20; /* start of repeating blocks */
328
329         while (num_pmu) {
330                 guint16      format_flags;
331                 gint         num_ph,
332                              num_an,
333                              num_dg;
334                 gint         i,
335                              phunit,
336                              anunit,
337                              fnom;
338                 config_block block;
339
340                 /* initialize the block */
341                 block.phasors = wmem_array_new(wmem_file_scope(), sizeof(phasor_info));
342                 block.analogs = wmem_array_new(wmem_file_scope(), sizeof(analog_info));
343                 /* copy the station name from the tvb to block, and add NULL byte */
344                 tvb_memcpy(tvb, block.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
345                 block.name[CHNAM_LEN] = '\0';
346
347                 block.id = tvb_get_ntohs(tvb, offset); offset += 2;
348
349                 format_flags          = tvb_get_ntohs(tvb, offset); offset += 2;
350                 block.format_fr       = (format_flags & 0x0008) ? floating_point : integer;
351                 block.format_an       = (format_flags & 0x0004) ? floating_point : integer;
352                 block.format_ph       = (format_flags & 0x0002) ? floating_point : integer;
353                 block.phasor_notation = (format_flags & 0x0001) ? polar          : rect;
354
355                 num_ph = tvb_get_ntohs(tvb, offset); offset += 2;
356                 num_an = tvb_get_ntohs(tvb, offset); offset += 2;
357                 num_dg = tvb_get_ntohs(tvb, offset); offset += 2;
358                 block.num_dg = num_dg;
359
360                 /* the offset of the PHUNIT, ANUNIT, and FNOM blocks */
361                 phunit = offset + (num_ph + num_an + num_dg * CHNAM_LEN) * CHNAM_LEN;
362                 anunit = phunit + num_ph * 4;
363                 fnom   = anunit + num_an * 4 + num_dg * 4;
364
365                 /* read num_ph phasor names and conversation factors */
366                 for (i = 0; i != num_ph; i++) {
367                         phasor_info  pi;
368                         guint32      conv;
369
370                         /* copy the phasor name from the tvb, and add NULL byte */
371                         tvb_memcpy(tvb, pi.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
372                         pi.name[CHNAM_LEN] = '\0';
373
374                         conv = tvb_get_ntohl(tvb, phunit + 4 * i);
375                         pi.unit = conv & 0xFF000000 ? A : V;
376                         pi.conv = conv & 0x00FFFFFF;
377
378                         wmem_array_append_one(block.phasors, pi);
379                 }
380
381                 /* read num_an analog value names and conversation factors */
382                 for (i = 0; i != num_an; i++) {
383                         analog_info ai;
384                         guint32     conv;
385
386                         /* copy the phasor name from the tvb, and add NULL byte */
387                         tvb_memcpy(tvb, ai.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
388                         ai.name[CHNAM_LEN] = '\0';
389
390                         conv = tvb_get_ntohl(tvb, anunit + 4 * i);
391                         ai.conv = conv;
392
393                         wmem_array_append_one(block.analogs, ai);
394                 }
395
396                 /* the names for the bits in the digital status words aren't saved,
397                    there is no space to display them in the GUI anyway */
398
399                 /* save FNOM */
400                 block.fnom = tvb_get_ntohs(tvb, fnom) & 0x0001 ? 50 : 60;
401                 offset = fnom + 2;
402
403                 /* skip CFGCNT */
404                 offset += 2;
405
406                 wmem_array_append_one(frame->config_blocks, block);
407                 num_pmu--;
408         }
409
410         return frame;
411 }
412
413 /* Checks the CRC of a synchrophasor frame, 'tvb' has to include the whole
414  * frame, including CRC, the calculated CRC is returned in '*computedcrc'.
415  */
416 static gboolean check_crc(tvbuff_t *tvb, guint16 *computedcrc)
417 {
418         guint16 crc;
419         guint   len = tvb_get_ntohs(tvb, 2);
420
421         crc = tvb_get_ntohs(tvb, len - 2);
422         *computedcrc = crc16_x25_ccitt_tvb(tvb, len - 2);
423
424         if (crc == *computedcrc)
425                 return TRUE;
426
427         return FALSE;
428 }
429
430 /* forward declarations of the subdissectors for the data
431  * in the frame that is not common to all types of frames
432  */
433 static int dissect_config_frame (tvbuff_t *, proto_item *);
434 static int dissect_data_frame   (tvbuff_t *, proto_item *, packet_info *);
435 static int dissect_command_frame(tvbuff_t *, proto_item *, packet_info *);
436 /* to keep 'dissect_common()' shorter */
437 static gint dissect_header(tvbuff_t *, proto_tree *);
438
439 /* Dissects the header (common to all types of frames) and then calls
440  * one of the subdissectors (declared above) for the rest of the frame.
441  */
442 static int dissect_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
443 {
444         guint8  frame_type;
445         guint16 crc;
446         guint   tvbsize = tvb_length(tvb);
447
448         /* some heuristics */
449         if (tvbsize < 17                    /* 17 bytes = header frame with only a
450                                                NULL character, useless but valid */
451          || tvb_get_guint8(tvb, 0) != 0xAA) /* every synchrophasor frame starts with 0xAA */
452                 return 0;
453
454         /* write the protocol name to the info column */
455         col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTOCOL_SHORT_NAME);
456
457         frame_type = tvb_get_guint8(tvb, 1) >> 4;
458
459         col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str_const(frame_type, typenames, "invalid packet type"));
460
461         /* CFG-2 and DATA frames need special treatment during the first run:
462          * For CFG-2 frames, a 'config_frame' struct is created to hold the
463          * information necessary to decode DATA frames. A pointer to this
464          * struct is saved in the conversation and is copied to the
465          * per-packet information if a DATA frame is dissected.
466          */
467         if (!pinfo->fd->flags.visited) {
468                 if (CFG2 == frame_type &&
469                     check_crc(tvb, &crc)) {
470                         conversation_t *conversation;
471
472                         /* fill the config_frame */
473                         config_frame *frame = config_frame_fast(tvb);
474                         frame->fnum = pinfo->fd->num;
475
476                         /* find a conversation, create a new if no one exists */
477                         conversation = find_or_create_conversation(pinfo);
478
479                         /* remove data from a previous CFG-2 frame, only
480                          * the most recent configuration frame is relevant */
481                         if (conversation_get_proto_data(conversation, proto_synphasor))
482                                 conversation_delete_proto_data(conversation, proto_synphasor);
483
484                         conversation_add_proto_data(conversation, proto_synphasor, frame);
485                 }
486                 else if (DATA == frame_type) {
487                         conversation_t *conversation = find_conversation(pinfo->fd->num,
488                                                                          &pinfo->src, &pinfo->dst,
489                                                                          pinfo->ptype,
490                                                                          pinfo->srcport, pinfo->destport,
491                                                                          0);
492
493                         if (conversation) {
494                                 config_frame *conf = (config_frame *)conversation_get_proto_data(conversation, proto_synphasor);
495                                 /* no problem if 'conf' is NULL, the DATA frame dissector checks this again */
496                         p_add_proto_data(wmem_file_scope(), pinfo, proto_synphasor, 0, conf);
497                         }
498                 }
499         } /* if (!visited) */
500
501         if (tree) { /* we are being asked for details */
502                 proto_tree *synphasor_tree = NULL;
503                 proto_item *temp_item      = NULL;
504                 proto_item *sub_item       = NULL;
505
506                 gint     offset;
507                 guint16  framesize;
508                 tvbuff_t *sub_tvb;
509
510                 temp_item = proto_tree_add_item(tree, proto_synphasor, tvb, 0, -1, ENC_NA);
511                 proto_item_append_text(temp_item, ", %s", val_to_str_const(frame_type, typenames,
512                                                                            ", invalid packet type"));
513
514                 /* synphasor_tree is where from now on all new elements for this protocol get added */
515                 synphasor_tree = proto_item_add_subtree(temp_item, ett_synphasor);
516
517                 framesize = dissect_header(tvb, synphasor_tree);
518                 offset = 14; /* header is 14 bytes long */
519
520                 /* check CRC, call appropriate subdissector for the rest of the frame if CRC is correct*/
521                 sub_item  = proto_tree_add_text(synphasor_tree, tvb, offset     , tvbsize - 16, "Data"     );
522                 temp_item = proto_tree_add_text(synphasor_tree, tvb, tvbsize - 2, 2           , "Checksum:");
523                 if (!check_crc(tvb, &crc)) {
524                         proto_item_append_text(sub_item,  ", not dissected because of wrong checksum");
525                         proto_item_append_text(temp_item, " 0x%04x [incorrect]", crc);
526                 }
527                 else {
528                         /* create a new tvb to pass to the subdissector
529                            '-16': length of header + 2 CRC bytes */
530                         sub_tvb = tvb_new_subset(tvb, offset, tvbsize - 16, framesize - 16);
531
532                         /* call subdissector */
533                         switch (frame_type) {
534                                 case DATA:
535                                         dissect_data_frame(sub_tvb, sub_item, pinfo);
536                                         break;
537                                 case HEADER: /* no further dissection is done/needed */
538                                         proto_item_append_text(sub_item, "Header Frame");
539                                         break;
540                                 case CFG1:
541                                 case CFG2:
542                                         dissect_config_frame(sub_tvb, sub_item);
543                                         break;
544                                 case CMD:
545                                         dissect_command_frame(sub_tvb, sub_item, pinfo);
546                                         break;
547
548                                 default:
549                                         proto_item_append_text(sub_item, " of unknown type");
550                         }
551                         proto_item_append_text(temp_item, " 0x%04x [correct]", crc);
552                 }
553
554                 /*offset += 2;*/ /* CRC */
555         } /* if (tree) */
556
557     return tvb_length(tvb);
558 } /* dissect_synphasor() */
559
560 /* called for synchrophasors over UDP */
561 static int dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
562 {
563         return dissect_common(tvb, pinfo, tree, data);
564 }
565
566 /* callback for 'tcp_dissect_pdus()' to give it the length of the frame */
567 static guint get_pdu_length(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
568 {
569         return tvb_get_ntohs(tvb, offset + 2);
570 }
571
572 static int dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
573 {
574         tcp_dissect_pdus(tvb, pinfo, tree, TRUE, 4, get_pdu_length, dissect_common, data);
575
576         return tvb_length(tvb);
577 }
578
579
580 /* Dissects the common header of frames.
581  *
582  * Returns the framesize, in contrast to most
583  * other helper functions that return the offset.
584  */
585 static gint dissect_header(tvbuff_t *tvb, proto_tree *tree)
586 {
587         proto_tree *temp_tree;
588         proto_item *temp_item;
589
590         gint    offset = 0;
591         guint16 framesize;
592
593         /* SYNC and flags */
594         temp_item = proto_tree_add_item(tree, hf_sync, tvb, offset, 2, ENC_BIG_ENDIAN);
595         temp_tree = proto_item_add_subtree(temp_item, ett_frtype);
596                 proto_tree_add_item(temp_tree, hf_sync_frtype,  tvb, offset, 2, ENC_BIG_ENDIAN);
597                 proto_tree_add_item(temp_tree, hf_sync_version, tvb, offset, 2, ENC_BIG_ENDIAN);
598         offset += 2;
599
600         /* FRAMESIZE */
601         proto_tree_add_item(tree, hf_frsize, tvb, offset, 2, ENC_BIG_ENDIAN);
602         framesize = tvb_get_ntohs(tvb, offset); offset += 2;
603
604         /* IDCODE */
605         proto_tree_add_item(tree, hf_idcode, tvb, offset, 2, ENC_BIG_ENDIAN);
606         offset += 2;
607
608         /* SOC */
609         {
610                 /* can't use 'proto_tree_add_time()' because we need UTC */
611                 char   buf[20];
612                 struct tm* t;
613                 time_t soc = tvb_get_ntohl(tvb, offset);
614                 t = gmtime(&soc);
615                 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
616                 proto_tree_add_string(tree, hf_soc, tvb, offset, 4, buf);
617                 offset += 4;
618         }
619
620         /* FRACSEC */
621         /* time quality flags */
622         temp_item = proto_tree_add_text(tree, tvb, offset, 1, "Time quality flags");
623         temp_tree = proto_item_add_subtree(temp_item, ett_timequal);
624                 proto_tree_add_item(temp_tree, hf_timeqal_lsdir,         tvb, offset, 1, ENC_BIG_ENDIAN);
625                 proto_tree_add_item(temp_tree, hf_timeqal_lsocc,         tvb, offset, 1, ENC_BIG_ENDIAN);
626                 proto_tree_add_item(temp_tree, hf_timeqal_lspend,        tvb, offset, 1, ENC_BIG_ENDIAN);
627                 proto_tree_add_item(temp_tree, hf_timeqal_timequalindic, tvb, offset, 1, ENC_BIG_ENDIAN);
628         offset += 1;
629
630         proto_tree_add_item(tree, hf_fracsec,  tvb, offset, 3, ENC_BIG_ENDIAN);
631         /*offset += 3;*/
632
633         return framesize;
634 }
635
636 /* forward declarations of helper functions for 'dissect_config_frame()' */
637 static gint dissect_CHNAM  (tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt, const char *prefix);
638 static gint dissect_PHUNIT (tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt);
639 static gint dissect_ANUNIT (tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt);
640 static gint dissect_DIGUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt);
641
642 /* dissects a configuration frame (type 1 and 2) and adds fields to 'config_item' */
643 static int dissect_config_frame(tvbuff_t *tvb, proto_item *config_item)
644 {
645         proto_tree *config_tree = NULL;
646         proto_item *temp_item   = NULL;
647         proto_tree *temp_tree   = NULL;
648         gint     offset = 0, j;
649         guint16  num_pmu;
650
651         proto_item_set_text   (config_item, "Configuration data");
652         config_tree = proto_item_add_subtree(config_item, ett_conf);
653
654         /* TIME_BASE and NUM_PMU */
655         offset += 1; /* skip the reserved byte */
656         proto_tree_add_item(config_tree, hf_conf_timebase, tvb, offset, 3, ENC_BIG_ENDIAN); offset += 3;
657         proto_tree_add_item(config_tree, hf_conf_numpmu,   tvb, offset, 2, ENC_BIG_ENDIAN);
658         /* add number of included PMUs to the text in the list view  */
659         num_pmu = tvb_get_ntohs(tvb, offset); offset += 2;
660         proto_item_append_text(config_item, ", %"G_GUINT16_FORMAT" PMU(s) included", num_pmu);
661
662         /* dissect the repeating PMU blocks */
663         for (j = 0; j < num_pmu; j++) {
664                 guint16  num_ph, num_an, num_dg;
665                 proto_item *station_item = NULL;
666                 proto_tree *station_tree = NULL;
667                 char *str;
668
669                 gint oldoffset = offset; /* to calculate the length of the whole PMU block later */
670
671                 /* STN with new tree to add the rest of the PMU block */
672                 str = tvb_get_string(wmem_packet_scope(), tvb, offset, CHNAM_LEN);
673                 station_item = proto_tree_add_text(config_tree, tvb, offset, CHNAM_LEN, "Station #%i: \"%s\"", j + 1, str);
674                 station_tree = proto_item_add_subtree(station_item, ett_conf_station);
675                 offset += CHNAM_LEN;
676
677                 /* IDCODE */
678                 proto_tree_add_item(station_tree, hf_idcode, tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
679
680                 /* FORMAT */
681                 temp_item = proto_tree_add_text(station_tree, tvb, offset, 2, "Data format in data frame");
682                 temp_tree = proto_item_add_subtree(temp_item, ett_conf_format);
683                         proto_tree_add_item(temp_tree, hf_conf_formatb3, tvb, offset, 2, ENC_BIG_ENDIAN);
684                         proto_tree_add_item(temp_tree, hf_conf_formatb2, tvb, offset, 2, ENC_BIG_ENDIAN);
685                         proto_tree_add_item(temp_tree, hf_conf_formatb1, tvb, offset, 2, ENC_BIG_ENDIAN);
686                         proto_tree_add_item(temp_tree, hf_conf_formatb0, tvb, offset, 2, ENC_BIG_ENDIAN);
687                 offset += 2;
688
689                 /* PHNMR, ANNMR, DGNMR */
690                 num_ph = tvb_get_ntohs(tvb, offset    );
691                 num_an = tvb_get_ntohs(tvb, offset + 2);
692                 num_dg = tvb_get_ntohs(tvb, offset + 4);
693                 proto_tree_add_text(station_tree, tvb, offset    , 2, "Number of phasors: %u",              num_ph);
694                 proto_tree_add_text(station_tree, tvb, offset + 2, 2, "Number of analog values: %u",        num_an);
695                 proto_tree_add_text(station_tree, tvb, offset + 4, 2, "Number of digital status words: %u", num_dg);
696                 offset += 6;
697
698                 /* CHNAM, the channel names */
699                 offset = dissect_CHNAM(tvb, station_tree, offset, num_ph     , "Phasor name"         );
700                 offset = dissect_CHNAM(tvb, station_tree, offset, num_an     , "Analog value"        );
701                 offset = dissect_CHNAM(tvb, station_tree, offset, num_dg * 16, "Digital status label");
702
703                 /* PHUNIT, ANUINT and DIGUNIT */
704                 offset = dissect_PHUNIT (tvb, station_tree, offset, num_ph);
705                 offset = dissect_ANUNIT (tvb, station_tree, offset, num_an);
706                 offset = dissect_DIGUNIT(tvb, station_tree, offset, num_dg);
707
708                 /* FNOM and CFGCNT */
709                 proto_tree_add_item(station_tree, hf_conf_fnom,   tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
710                 proto_tree_add_item(station_tree, hf_conf_cfgcnt, tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
711
712                 /* set the correct length for the "Station :" item */
713                 proto_item_set_len(station_item, offset - oldoffset);
714         } /* for() PMU blocks */
715
716         /* DATA_RATE */
717         {
718                 gint16 tmp = tvb_get_ntohs(tvb, offset);
719                 temp_item  = proto_tree_add_text(config_tree, tvb, offset, 2, "Rate of transmission: "); offset += 2;
720                 if (tmp > 0)
721                         proto_item_append_text(temp_item, "%"G_GINT16_FORMAT" frame(s) per second", tmp);
722                 else
723                         proto_item_append_text(temp_item, "1 frame per %"G_GINT16_FORMAT" second(s)", (gint16)-tmp);
724         }
725
726         return offset;
727 } /* dissect_config_frame() */
728
729 /* forward declarations of helper functions for 'dissect_data_frame()' */
730 static gint dissect_PHASORS(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset);
731 static gint dissect_DFREQ  (tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset);
732 static gint dissect_ANALOG (tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset);
733 static gint dissect_DIGITAL(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset);
734 /* calculates the size (in bytes) of a data frame that the config_block describes */
735 #define BLOCKSIZE(x) (2                                                    /* STAT    */ \
736                    + wmem_array_get_count((x).phasors) * (integer == (x).format_ph ? 4 : 8) /* PHASORS */ \
737                    +                                     (integer == (x).format_fr ? 4 : 8) /* (D)FREQ */ \
738                    + wmem_array_get_count((x).analogs) * (integer == (x).format_an ? 2 : 4) /* ANALOG  */ \
739                    + (x).num_dg * 2)                                       /* DIGITAL */
740
741 /* Dissects a data frame */
742 static int dissect_data_frame(tvbuff_t    *tvb,
743                               proto_item  *data_item, /* all items are placed beneath this item   */
744                               packet_info *pinfo)     /* used to find the data from a CFG-2 frame */
745 {
746         proto_tree   *data_tree  = NULL;
747         gint          offset     = 0;
748         guint         i;
749         config_frame *conf;
750
751         proto_item_set_text(data_item, "Measurement data");
752         data_tree = proto_item_add_subtree(data_item, ett_data);
753
754         /* search for configuration information to dissect the frame */
755         {
756                 gboolean config_found = FALSE;
757                 conf = (config_frame *)p_get_proto_data(wmem_file_scope(), pinfo, proto_synphasor, 0);
758
759                 if (conf) {
760                         /* check if the size of the current frame is the
761                            size of the frame the config_frame describes */
762                         size_t reported_size = 0;
763                         for (i = 0; i < wmem_array_get_count(conf->config_blocks); i++) {
764                                 config_block *block = (config_block*)wmem_array_index(conf->config_blocks, i);
765                                 reported_size += BLOCKSIZE(*block);
766                         }
767
768                         if (tvb_length(tvb) == reported_size) {
769                                 proto_item_append_text(data_item, ", using frame number %"G_GUINT32_FORMAT" as configuration frame",
770                                                        conf->fnum);
771                                 config_found = TRUE;
772                         }
773                 }
774
775                 if (!config_found) {
776                         proto_item_append_text(data_item, ", no configuration frame found");
777                         return 0;
778                 }
779         }
780
781         /* dissect a PMU block for every config_block in the frame */
782         for (i = 0; i < wmem_array_get_count(conf->config_blocks); i++) {
783                 config_block *block = (config_block*)wmem_array_index(conf->config_blocks, i);
784
785                 proto_item *block_item = proto_tree_add_text(data_tree, tvb, offset, BLOCKSIZE(*block),
786                                                  "Station: \"%s\"", block->name);
787                 proto_tree *block_tree = proto_item_add_subtree(block_item, ett_data_block);
788
789                 /* STAT */
790                 proto_item *temp_item = proto_tree_add_text(block_tree, tvb, offset, 2, "Flags");
791                 proto_tree *temp_tree = proto_item_add_subtree(temp_item, ett_data_stat);
792                         proto_tree_add_item(temp_tree, hf_data_statb15,     tvb, offset, 2, ENC_BIG_ENDIAN);
793                         proto_tree_add_item(temp_tree, hf_data_statb14,     tvb, offset, 2, ENC_BIG_ENDIAN);
794                         proto_tree_add_item(temp_tree, hf_data_statb13,     tvb, offset, 2, ENC_BIG_ENDIAN);
795                         proto_tree_add_item(temp_tree, hf_data_statb12,     tvb, offset, 2, ENC_BIG_ENDIAN);
796                         proto_tree_add_item(temp_tree, hf_data_statb11,     tvb, offset, 2, ENC_BIG_ENDIAN);
797                         proto_tree_add_item(temp_tree, hf_data_statb10,     tvb, offset, 2, ENC_BIG_ENDIAN);
798                         proto_tree_add_item(temp_tree, hf_data_statb05to04, tvb, offset, 2, ENC_BIG_ENDIAN);
799                         proto_tree_add_item(temp_tree, hf_data_statb03to00, tvb, offset, 2, ENC_BIG_ENDIAN);
800                 offset += 2;
801
802                 /* PHASORS, (D)FREQ, ANALOG, and DIGITAL */
803                 offset = dissect_PHASORS(tvb, block_item, block, offset);
804                 offset = dissect_DFREQ  (tvb, block_item, block, offset);
805                 offset = dissect_ANALOG (tvb, block_item, block, offset);
806                 offset = dissect_DIGITAL(tvb, block_item, block, offset);
807         }
808         return offset;
809 } /* dissect_data_frame() */
810
811 /* Dissects a command frame and adds fields to config_item.
812  *
813  * 'pinfo' is used to add the type of command
814  * to the INFO column in the packet list.
815  */
816 static int dissect_command_frame(tvbuff_t    *tvb,
817                                  proto_item  *command_item,
818                                  packet_info *pinfo)
819 {
820         proto_tree *command_tree  = NULL;
821         guint       tvbsize       = tvb_length(tvb);
822         const char *s;
823
824         proto_item_set_text(command_item, "Command data");
825         command_tree = proto_item_add_subtree(command_item, ett_command);
826
827         /* CMD */
828         proto_tree_add_item(command_tree, hf_command, tvb, 0, 2, ENC_BIG_ENDIAN);
829
830         s = val_to_str_const(tvb_get_ntohs(tvb, 0), command_names, "invalid command");
831         col_append_str(pinfo->cinfo, COL_INFO, ", ");
832         col_append_str(pinfo->cinfo, COL_INFO, s);
833
834         if (tvbsize > 2) {
835                 if (tvb_get_ntohs(tvb, 0) == 0x0008) {
836                         /* Command: Extended Frame, the extra data is ok */
837                                 proto_item* i = proto_tree_add_text(command_tree, tvb, 2, tvbsize - 2, "Extended frame data");
838                                 if (tvbsize % 2)
839                                         proto_item_append_text(i, ", but size not multiple of 16-bit word");
840                 }
841                 else
842                         proto_tree_add_text(command_tree, tvb, 2, tvbsize - 2, "Unknown data");
843         }
844
845         return tvbsize;
846 } /* dissect_command_frame() */
847
848 /****************************************************************/
849 /* after this line: helper functions for 'dissect_data_frame()' */
850 /****************************************************************/
851
852 /* Dissects a single phasor for 'dissect_PHASORS()' */
853 static int dissect_single_phasor(tvbuff_t *tvb, int offset,
854                                         double* mag, double* phase, /* returns the resulting values here */
855                                         data_format     format,     /* information needed to... */
856                                         phasor_notation_e notation)   /*         ...dissect the phasor  */
857 {
858         if (floating_point == format) {
859                 if (polar == notation) {
860                         /* float, polar */
861                         *mag   = tvb_get_ntohieee_float(tvb, offset    );
862                         *phase = tvb_get_ntohieee_float(tvb, offset + 4);
863                 }
864                 else {
865                         /* float, rect */
866                         gfloat real, imag;
867                         real = tvb_get_ntohieee_float(tvb, offset    );
868                         imag = tvb_get_ntohieee_float(tvb, offset + 4);
869
870                         *mag   = sqrt(pow(real, 2) + pow(imag, 2));
871                         *phase = atan2(imag, real);
872                 }
873         }
874         else {
875                 if (polar == notation) {
876                         /* int, polar */
877                         *mag    = (guint16)tvb_get_ntohs(tvb, offset    );
878                         *phase  = (gint16) tvb_get_ntohs(tvb, offset + 2);
879                         *phase /= 10000.0; /* angle is in radians*10^4 */
880                 }
881                 else {
882                         /* int, rect */
883                         gint16 real, imag;
884                         real = tvb_get_ntohs(tvb, offset    );
885                         imag = tvb_get_ntohs(tvb, offset + 2);
886
887                         *mag   = sqrt(pow(real, 2) + pow(imag, 2));
888                         *phase = atan2(imag, real);
889                 }
890         }
891
892         return floating_point == format ? 8 : 4;
893 }
894
895 /* used by 'dissect_data_frame()' to dissect the PHASORS field */
896 static gint dissect_PHASORS(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
897 {
898         proto_item *temp_item   = NULL;
899         proto_tree *phasor_tree = NULL;
900         guint       length;
901         gint        j,
902                     cnt = wmem_array_get_count(block->phasors); /* number of phasors to dissect */
903
904         if (0 == cnt)
905                 return offset;
906
907         length      = wmem_array_get_count(block->phasors) * (floating_point == block->format_ph ? 8 : 4);
908         temp_item   = proto_tree_add_text(tree, tvb, offset, length, "Phasors (%u)", cnt);
909         phasor_tree = proto_item_add_subtree(temp_item, ett_data_phasors);
910
911         /* dissect a phasor for every phasor_info saved in the config_block */
912         for (j = 0; j < cnt; j++) {
913                 double       mag, phase;
914                 phasor_info *pi;
915
916                 pi = (phasor_info*)wmem_array_index(block->phasors, j);
917                 temp_item = proto_tree_add_text(phasor_tree, tvb, offset,
918                                                 floating_point == block->format_ph ? 8 : 4,
919                                                 "Phasor #%u: \"%s\"", j + 1, pi->name);
920
921                 offset += dissect_single_phasor(tvb, offset,
922                                                 &mag, &phase,
923                                                 block->format_ph,
924                                                 block->phasor_notation);
925
926                 /* for values in integer format, apply conversation factor */
927                 if (integer == block->format_ph)
928                         mag = (mag * pi->conv) * 0.00001;
929
930                 #define ANGLE  "/_"
931                 #define DEGREE "\xC2\xB0" /* DEGREE signs in UTF-8 */
932
933                 proto_item_append_text(temp_item, ", %10.2f%c" ANGLE "%7.2f" DEGREE,
934                                                   mag,
935                                                   V == pi->unit ? 'V' : 'A',
936                                                   phase *180.0/G_PI);
937                 #undef ANGLE
938                 #undef DEGREE
939         }
940         return offset;
941 }
942
943 /* used by 'dissect_data_frame()' to dissect the FREQ and DFREQ fields */
944 static gint dissect_DFREQ(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
945 {
946         if (floating_point == block->format_fr) {
947                 gfloat tmp;
948
949                 tmp = tvb_get_ntohieee_float(tvb, offset);
950                 proto_tree_add_text(tree, tvb, offset, 4, "Actual frequency value: %fHz", tmp); offset += 4;
951
952                 /* The standard doesn't clearly say how to interpret this value, but
953                  * http://www.pes-psrc.org/h/C37_118_H11_FAQ_Jan2008.pdf provides further information.
954                  * --> no scaling factor is applied to DFREQ
955                  */
956                 tmp = tvb_get_ntohieee_float(tvb, offset);
957                 proto_tree_add_text(tree, tvb, offset, 4, "Rate of change of frequency: %fHz/s", tmp); offset += 4;
958         }
959         else {
960                 gint16 tmp;
961
962                 tmp = tvb_get_ntohs(tvb, offset);
963                 proto_tree_add_text(tree, tvb, offset, 2,
964                                     "Frequency deviation from nominal: %" G_GINT16_FORMAT "mHz (actual frequency: %.3fHz)",
965                                     tmp, block->fnom + (tmp / 1000.0));
966                 offset += 2;
967
968                 tmp = tvb_get_ntohs(tvb, offset);
969                 proto_tree_add_text(tree, tvb, offset, 2, "Rate of change of frequency: %.3fHz/s", tmp / 100.0); offset += 2;
970         }
971         return offset;
972 }
973
974 /* used by 'dissect_data_frame()' to dissect the ANALOG field */
975 static gint dissect_ANALOG(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
976 {
977         proto_tree *analog_tree = NULL;
978         proto_item *temp_item   = NULL;
979         guint       length;
980         gint        j,
981                     cnt = wmem_array_get_count(block->analogs); /* number of analog values to dissect */
982
983         if (0 == cnt)
984                 return offset;
985
986         length      = wmem_array_get_count(block->analogs) * (floating_point == block->format_an ? 4 : 2);
987         temp_item   = proto_tree_add_text(tree, tvb, offset, length, "Analog values (%u)", cnt);
988
989         analog_tree = proto_item_add_subtree(temp_item, ett_data_analog);
990
991         for (j = 0; j < cnt; j++) {
992                 analog_info *ai = (analog_info *)wmem_array_index(block->analogs, j);
993
994                 temp_item = proto_tree_add_text(analog_tree, tvb, offset,
995                                                 floating_point == block->format_an ? 4 : 2,
996                                                 "Analog value #%u: \"%s\"", j + 1, ai->name);
997
998                 if (floating_point == block->format_an) {
999                         gfloat tmp = tvb_get_ntohieee_float(tvb, offset); offset += 4;
1000                         proto_item_append_text(temp_item, ", %.3f", tmp);
1001                 }
1002                 else {
1003                         /* the "standard" doesn't say if this is signed or unsigned,
1004                          * so I just use gint16, the scaling of the conversation factor
1005                          * is also "user defined", so I just write it after the analog value */
1006                         gint16 tmp = tvb_get_ntohs(tvb, offset); offset += 2;
1007                         proto_item_append_text(temp_item, ", %" G_GINT16_FORMAT " (conversation factor: %#06x)",
1008                                                tmp, ai->conv);
1009                 }
1010         }
1011         return offset;
1012 }
1013
1014 /* used by 'dissect_data_frame()' to dissect the DIGITAL field */
1015 static gint dissect_DIGITAL(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
1016 {
1017         proto_item *digital_item = NULL;
1018         gint        j,
1019                     cnt = block->num_dg; /* number of digital status words to dissect */
1020
1021         if (0 == cnt)
1022                 return offset;
1023
1024         digital_item = proto_tree_add_text(tree, tvb, offset, cnt * 2, "Digital status words (%u)", cnt);
1025         tree         = proto_item_add_subtree(digital_item, ett_data_digital);
1026
1027         for (j = 0; j < cnt; j++) {
1028                 guint16 tmp = tvb_get_ntohs(tvb, offset);
1029                 proto_tree_add_text(tree, tvb, offset, 2, "Digital status word #%u: 0x%04x", j + 1, tmp);
1030                 offset += 2;
1031         }
1032         return offset;
1033 }
1034
1035 /*******************************************************************/
1036 /* after this line:  helper functions for 'dissect_config_frame()' */
1037 /*******************************************************************/
1038
1039 /* used by 'dissect_config_frame()' to dissect the PHUNIT field */
1040 static gint dissect_PHUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1041 {
1042         proto_item *temp_item = NULL;
1043         proto_tree *temp_tree = NULL;
1044         int i;
1045
1046         if (0 == cnt)
1047                 return offset;
1048
1049         temp_item = proto_tree_add_text(tree, tvb, offset, 4 * cnt, "Phasor conversation factors (%u)", cnt);
1050         temp_tree = proto_item_add_subtree(temp_item, ett_conf_phconv);
1051
1052         /* Conversion factor for phasor channels. Four bytes for each phasor.
1053          * MSB:           0 = voltage, 1 = current
1054          * Lower 3 Bytes: unsigned 24-bit word in 10^-5 V or A per bit to scale the phasor value
1055          */
1056         for (i = 0; i < cnt; i++) {
1057                 guint32 tmp = tvb_get_ntohl(tvb, offset);
1058                 proto_tree_add_text(temp_tree, tvb, offset, 4,
1059                                     "#%u factor: %u * 10^-5, unit: %s",
1060                                     i + 1,
1061                                     tmp & 0x00FFFFFF,
1062                                     tmp & 0xFF000000 ? "Ampere" : "Volt");
1063                 offset += 4;
1064         }
1065
1066         return offset;
1067 }
1068
1069 /* used by 'dissect_config_frame()' to dissect the ANUNIT field */
1070 static gint dissect_ANUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1071 {
1072         proto_item *temp_item = NULL;
1073         proto_tree *temp_tree = NULL;
1074         int i;
1075
1076         if (0 == cnt)
1077                 return offset;
1078
1079         temp_item = proto_tree_add_text(tree, tvb, offset, 4 * cnt, "Analog values conversation factors (%u)", cnt);
1080         temp_tree = proto_item_add_subtree(temp_item, ett_conf_anconv);
1081
1082         /* Conversation factor for analog channels. Four bytes for each analog value.
1083          * MSB: see 'synphasor_conf_anconvnames' in 'synphasor_strings.c'
1084          * Lower 3 Bytes: signed 24-bit word, user-defined scaling
1085          */
1086         for (i = 0; i < cnt; i++) {
1087                 gint32 tmp = tvb_get_ntohl(tvb, offset);
1088                 temp_item = proto_tree_add_text(temp_tree, tvb, offset, 4,
1089                                                 "Factor for analog value #%i: %s",
1090                                                 i + 1,
1091                                                 try_rval_to_str((tmp >> 24) & 0x000000FF, conf_anconvnames));
1092
1093                         tmp &= 0x00FFFFFF;
1094                 if (    tmp &  0x00800000) /* sign bit set */
1095                         tmp |= 0xFF000000;
1096
1097                 proto_item_append_text(temp_item, ", value: %" G_GINT32_FORMAT, tmp);
1098
1099                 offset += 4;
1100         }
1101
1102         return offset;
1103 }
1104
1105 /* used by 'dissect_config_frame()' to dissect the DIGUNIT field */
1106 static gint dissect_DIGUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1107 {
1108         proto_item *temp_item = NULL;
1109         proto_tree *temp_tree = NULL;
1110         int i;
1111
1112         if (0 == cnt)
1113                 return offset;
1114
1115         temp_item = proto_tree_add_text(tree, tvb, offset, 4 * cnt, "Masks for digital status words (%u)", cnt);
1116         temp_tree = proto_item_add_subtree(temp_item, ett_conf_dgmask);
1117
1118         /* Mask words for digital status words. Two 16-bit words for each digital word. The first
1119          * inidcates the normal status of the inputs, the second indicated the valid bits in
1120          * the status word
1121          */
1122         for (i = 0; i < cnt; i++) {
1123                 guint32 tmp = tvb_get_ntohl(tvb, offset);
1124
1125                 temp_item = proto_tree_add_text(temp_tree, tvb, offset, 4, "Mask for status word #%u: ", i + 1);
1126                 proto_item_append_text(temp_item, "normal state: 0x%04"G_GINT16_MODIFIER"x", (guint16)((tmp & 0xFFFF0000) >> 16));
1127                 proto_item_append_text(temp_item, ", valid bits: 0x%04"G_GINT16_MODIFIER"x", (guint16)( tmp & 0x0000FFFF));
1128
1129                 offset += 4;
1130         }
1131
1132         return offset;
1133 }
1134
1135 /* used by 'dissect_config_frame()' to dissect the "channel name"-fields */
1136 static gint dissect_CHNAM(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt, const char *prefix)
1137 {
1138         proto_item *temp_item = NULL;
1139         proto_tree *temp_tree = NULL;
1140         int i;
1141
1142         if (0 == cnt)
1143                 return offset;
1144
1145         temp_item = proto_tree_add_text(tree, tvb, offset, CHNAM_LEN * cnt, "%ss (%u)", prefix, cnt);
1146         temp_tree = proto_item_add_subtree(temp_item, ett_conf_phnam);
1147
1148         /* dissect the 'cnt' channel names */
1149         for (i = 0; i < cnt; i++) {
1150                 char *str;
1151                 str = tvb_get_string(wmem_packet_scope(), tvb, offset, CHNAM_LEN);
1152                 proto_tree_add_text(temp_tree, tvb, offset, CHNAM_LEN,
1153                                     "%s #%i: \"%s\"", prefix, i+1, str);
1154                 offset += CHNAM_LEN;
1155         }
1156
1157         return offset;
1158 }
1159
1160 void proto_register_synphasor(void)
1161 {
1162         static hf_register_info hf[] = {
1163                 /* Sync word */
1164                 { &hf_sync,
1165                 { "Synchronization word", PROTOCOL_ABBREV ".sync", FT_UINT16, BASE_HEX,
1166                   NULL, 0x0, NULL, HFILL }},
1167                         /* Flags in the Sync word */
1168                         { &hf_sync_frtype,
1169                         { "Frame Type", PROTOCOL_ABBREV ".frtype", FT_UINT16, BASE_HEX,
1170                           VALS(typenames), 0x0070, NULL, HFILL }},
1171
1172                         { &hf_sync_version,
1173                         { "Version",    PROTOCOL_ABBREV ".version", FT_UINT16, BASE_DEC,
1174                           VALS(versionnames), 0x000F, NULL, HFILL }},
1175
1176                 { &hf_frsize,
1177                 { "Framesize", PROTOCOL_ABBREV ".frsize", FT_UINT16, BASE_DEC,
1178                   NULL, 0x0, NULL, HFILL }},
1179
1180                 { &hf_idcode,
1181                 { "PMU/DC ID number", PROTOCOL_ABBREV ".idcode", FT_UINT16, BASE_DEC,
1182                   NULL, 0x0, NULL, HFILL }},
1183
1184                 { &hf_soc,
1185                 { "SOC time stamp (UTC)", PROTOCOL_ABBREV ".soc", FT_STRINGZ, BASE_NONE,
1186                   NULL, 0x0, NULL, HFILL }},
1187
1188                 /* Time quality flags in fracsec */
1189                 { &hf_timeqal_lsdir,
1190                 { "Leap second direction", PROTOCOL_ABBREV ".timeqal.lsdir", FT_BOOLEAN, 8,
1191                   NULL, 0x40, NULL, HFILL }},
1192
1193                 { &hf_timeqal_lsocc,
1194                 { "Leap second occurred", PROTOCOL_ABBREV ".timeqal.lsocc", FT_BOOLEAN, 8,
1195                   NULL, 0x20, NULL, HFILL }},
1196
1197                 { &hf_timeqal_lspend,
1198                 { "Leap second pending", PROTOCOL_ABBREV ".timeqal.lspend", FT_BOOLEAN, 8,
1199                   NULL, 0x10, NULL, HFILL }},
1200
1201                 { &hf_timeqal_timequalindic,
1202                 { "Time Quality indicator code", PROTOCOL_ABBREV ".timeqal.timequalindic", FT_UINT8, BASE_HEX,
1203                   VALS(timequalcodes), 0x0F, NULL, HFILL }},
1204
1205                 /* Fraction of second */
1206                 { &hf_fracsec,
1207                 { "Fraction of second (raw)", PROTOCOL_ABBREV ".fracsec", FT_UINT24, BASE_DEC,
1208                   NULL, 0x0, NULL, HFILL }},
1209
1210         /* Data types for configuration frames */
1211                 { &hf_conf_timebase,
1212                 { "Resolution of fractional second time stamp", PROTOCOL_ABBREV ".conf.timebase", FT_UINT24, BASE_DEC,
1213                   NULL, 0x0, NULL, HFILL }},
1214
1215                 { &hf_conf_numpmu,
1216                 { "Number of PMU blocks included in the frame", PROTOCOL_ABBREV ".conf.numpmu", FT_UINT16, BASE_DEC,
1217                   NULL, 0x0, NULL, HFILL }},
1218
1219                 /* Bits in the FORMAT word */
1220                 { &hf_conf_formatb3,
1221                 { "FREQ/DFREQ format", PROTOCOL_ABBREV ".conf.dfreq_format", FT_BOOLEAN, 16,
1222                   TFS(&conf_formatb123names), 0x8, NULL, HFILL }},
1223
1224                 { &hf_conf_formatb2,
1225                 { "Analog values format", PROTOCOL_ABBREV ".conf.analog_format", FT_BOOLEAN, 16,
1226                   TFS(&conf_formatb123names), 0x4, NULL, HFILL }},
1227
1228                 { &hf_conf_formatb1,
1229                 { "Phasor format", PROTOCOL_ABBREV ".conf.phasor_format", FT_BOOLEAN, 16,
1230                   TFS(&conf_formatb123names), 0x2, NULL, HFILL }},
1231
1232                 { &hf_conf_formatb0,
1233                 { "Phasor notation", PROTOCOL_ABBREV ".conf.phasor_notation", FT_BOOLEAN, 16,
1234                   TFS(&conf_formatb0names), 0x1, NULL, HFILL }},
1235
1236                 { &hf_conf_fnom,
1237                 { "Nominal line freqency", PROTOCOL_ABBREV ".conf.fnom", FT_BOOLEAN, 16,
1238                   TFS(&conf_fnomnames), 0x0001, NULL, HFILL }},
1239
1240                 { &hf_conf_cfgcnt,
1241                 { "Configuration change count", PROTOCOL_ABBREV ".conf.cfgcnt", FT_UINT16, BASE_DEC,
1242                   NULL, 0, NULL, HFILL }},
1243
1244         /* Data types for data frames */
1245                 /* Flags in the STAT word */
1246                 { &hf_data_statb15,
1247                 { "Data valid", PROTOCOL_ABBREV ".data.valid", FT_BOOLEAN, 16,
1248                   TFS(&data_statb15names), 0x8000, NULL, HFILL }},
1249
1250                 { &hf_data_statb14,
1251                 { "PMU error", PROTOCOL_ABBREV ".data.PMUerror", FT_BOOLEAN, 16,
1252                   TFS(&data_statb14names), 0x4000, NULL, HFILL }},
1253
1254                 { &hf_data_statb13,
1255                 { "Time synchronized", PROTOCOL_ABBREV ".data.sync", FT_BOOLEAN, 16,
1256                   TFS(&data_statb13names), 0x2000, NULL, HFILL }},
1257
1258                 { &hf_data_statb12,
1259                 { "Data sorting", PROTOCOL_ABBREV ".data.sorting", FT_BOOLEAN, 16,
1260                   TFS(&data_statb12names), 0x1000, NULL, HFILL }},
1261
1262                 { &hf_data_statb11,
1263                 { "Trigger detected", PROTOCOL_ABBREV ".data.trigger", FT_BOOLEAN, 16,
1264                   TFS(&data_statb11names), 0x0800, NULL, HFILL }},
1265
1266                 { &hf_data_statb10,
1267                 { "Configuration changed", PROTOCOL_ABBREV ".data.CFGchange", FT_BOOLEAN, 16,
1268                   TFS(&data_statb10names), 0x0400, NULL, HFILL }},
1269
1270                 { &hf_data_statb05to04,
1271                 { "Unlocked time", PROTOCOL_ABBREV  ".data.t_unlock", FT_UINT16, BASE_HEX,
1272                   VALS(data_statb05to04names), 0x0030, NULL, HFILL }},
1273
1274                 { &hf_data_statb03to00,
1275                 { "Trigger reason", PROTOCOL_ABBREV  ".data.trigger_reason", FT_UINT16, BASE_HEX,
1276                   VALS(data_statb03to00names), 0x000F, NULL, HFILL }},
1277
1278         /* Data type for command frame */
1279                 { &hf_command,
1280                 { "Command", PROTOCOL_ABBREV ".command", FT_UINT16, BASE_HEX,
1281                   VALS(command_names), 0x000F, NULL, HFILL }}
1282         };
1283
1284         /* protocol subtree array */
1285         static gint *ett[] = {
1286                 &ett_synphasor,
1287                 &ett_frtype,
1288                 &ett_timequal,
1289                 &ett_conf,
1290                 &ett_conf_station,
1291                 &ett_conf_format,
1292                 &ett_conf_phnam,
1293                 &ett_conf_annam,
1294                 &ett_conf_dgnam,
1295                 &ett_conf_phconv,
1296                 &ett_conf_anconv,
1297                 &ett_conf_dgmask,
1298                 &ett_data,
1299                 &ett_data_block,
1300                 &ett_data_stat,
1301                 &ett_data_phasors,
1302                 &ett_data_analog,
1303                 &ett_data_digital,
1304                 &ett_command
1305         };
1306
1307         module_t *synphasor_module;
1308
1309         /* register protocol */
1310         proto_synphasor = proto_register_protocol(PROTOCOL_NAME,
1311                                                   PROTOCOL_SHORT_NAME,
1312                                                   PROTOCOL_ABBREV);
1313
1314         /* Registering protocol to be called by another dissector */
1315         synphasor_udp_handle = new_register_dissector("synphasor", dissect_udp, proto_synphasor);
1316
1317         proto_register_field_array(proto_synphasor, hf, array_length(hf));
1318         proto_register_subtree_array(ett, array_length(ett));
1319
1320         /* register preferences */
1321         synphasor_module = prefs_register_protocol(proto_synphasor, proto_reg_handoff_synphasor);
1322
1323         /* the port numbers of the lower level protocols */
1324         prefs_register_uint_preference(synphasor_module, "udp_port", "Synchrophasor UDP port",
1325                                        "Set the port number for synchrophasor frames over UDP" \
1326                                        "(if other than the default of 4713)",
1327                                        10, &global_pref_udp_port);
1328         prefs_register_uint_preference(synphasor_module, "tcp_port", "Synchrophasor TCP port",
1329                                        "Set the port number for synchrophasor frames over TCP" \
1330                                        "(if other than the default of 4712)",
1331                                        10, &global_pref_tcp_port);
1332
1333 } /* proto_register_synphasor() */
1334
1335 /* called at startup and when the preferences change */
1336 void proto_reg_handoff_synphasor(void)
1337 {
1338         static gboolean           initialized = FALSE;
1339         static dissector_handle_t synphasor_tcp_handle;
1340         static guint              current_udp_port;
1341         static guint              current_tcp_port;
1342
1343         if (!initialized) {
1344                 synphasor_tcp_handle = new_create_dissector_handle(dissect_tcp, proto_synphasor);
1345
1346                 initialized = TRUE;
1347         }
1348         else {
1349                 /* update preferences */
1350                 dissector_delete_uint("udp.port", current_udp_port, synphasor_udp_handle);
1351                 dissector_delete_uint("tcp.port", current_tcp_port, synphasor_tcp_handle);
1352         }
1353
1354         current_udp_port = global_pref_udp_port;
1355         current_tcp_port = global_pref_tcp_port;
1356
1357         dissector_add_uint("udp.port", current_udp_port, synphasor_udp_handle);
1358         dissector_add_uint("tcp.port", current_tcp_port, synphasor_tcp_handle);
1359 } /* proto_reg_handoff_synphasor() */