1 /* packet-synphasor.c
2  * Dissector for IEEE C37.118 synchrophasor frames.
3  *
4  * Copyright 2008, Jens Steinhauser <jens.steinhauser@omicron.at>
5  * Copyright 2019, Dwayne Rich <dwayne_rich@selinc.com>
6  * Copyright 2020, Dmitriy Eliseev <eliseev_d@ntcees.ru>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14 
15 #include "config.h"
16 
17 #include <math.h>
18 #include <stdio.h>
19 
20 #include <epan/packet.h>
21 #include <epan/crc16-tvb.h>
22 #include <epan/expert.h>
23 #include <epan/proto_data.h>
24 #include "packet-tcp.h"
25 
26 #include <wsutil/utf8_entities.h>
27 
28 #define PROTOCOL_NAME	    "IEEE C37.118 Synchrophasor Protocol"
29 #define PROTOCOL_SHORT_NAME "SYNCHROPHASOR"
30 #define PROTOCOL_ABBREV	    "synphasor"
31 
32 /* forward references */
33 void proto_register_synphasor(void);
34 void proto_reg_handoff_synphasor(void);
35 
36 /* global variables */
37 
38 static int proto_synphasor	 = -1;
39 
40 /* user preferences */
41 #define SYNPHASOR_TCP_PORT  4712 /* Not IANA registered */
42 #define SYNPHASOR_UDP_PORT  4713 /* Not IANA registered */
43 
44 /* Config 1 & 2 frames have channel names that are all 16 bytes long */
45 /* Config 3 frame channel names have a variable length with a max of 255 characters */
46 #define CHNAM_LEN 16
47 #define MAX_NAME_LEN 255
48 #define G_PMU_ID_LEN 16
49 
50 /* the ett... variables hold the state (open/close) of the treeview in the GUI */
51 static gint ett_synphasor			= -1; /* root element for this protocol */
52   /* used in the common header */
53   static gint ett_frtype			= -1;
54   static gint ett_timequal			= -1;
55   /* used for config frames */
56   static gint ett_conf				= -1;
57     static gint ett_conf_station		= -1;
58       static gint ett_conf_format		= -1;
59       static gint ett_conf_phnam		= -1;
60       static gint ett_conf_annam		= -1;
61       static gint ett_conf_dgnam		= -1;
62       static gint ett_conf_phconv		= -1;
63       static gint ett_conf_phlist		= -1;
64       static gint ett_conf_phflags		= -1;
65       static gint ett_conf_phmod_flags		= -1;
66       static gint ett_conf_ph_user_flags	= -1;
67       static gint ett_conf_anconv		= -1;
68       static gint ett_conf_anlist		= -1;
69       static gint ett_conf_dgmask		= -1;
70       static gint ett_conf_chnam		= -1;
71       static gint ett_conf_wgs84		= -1;
72   /* used for data frames */
73   static gint ett_data				= -1;
74     static gint ett_data_block			= -1;
75       static gint ett_data_stat			= -1;
76       static gint ett_data_phasors		= -1;
77       static gint ett_data_analog		= -1;
78       static gint ett_data_digital		= -1;
79   /* used for command frames */
80   static gint ett_command			= -1;
81   static gint ett_status_word_mask		= -1;
82 
83 /* handles to the header fields hf[] in proto_register_synphasor() */
84 static int hf_sync			= -1;
85 static int hf_sync_frtype		= -1;
86 static int hf_sync_version		= -1;
87 static int hf_station_name_len		= -1;
88 static int hf_station_name		= -1;
89 static int hf_idcode_stream_source	= -1;
90 static int hf_idcode_data_source	= -1;
91 static int hf_g_pmu_id			= -1;
92 static int hf_frsize			= -1;
93 static int hf_soc			= -1;
94 static int hf_timeqal_lsdir		= -1;
95 static int hf_timeqal_lsocc		= -1;
96 static int hf_timeqal_lspend		= -1;
97 static int hf_timeqal_timequalindic	= -1;
98 static int hf_fracsec_raw		= -1;
99 static int hf_fracsec_ms		= -1;
100 static int hf_cont_idx			= -1;
101 static int hf_conf_timebase		= -1;
102 static int hf_conf_numpmu		= -1;
103 static int hf_conf_formatb3		= -1;
104 static int hf_conf_formatb2		= -1;
105 static int hf_conf_formatb1		= -1;
106 static int hf_conf_formatb0		= -1;
107 static int hf_conf_chnam_len		= -1;
108 static int hf_conf_chnam		= -1;
109 static int hf_conf_phasor_mod_b15	= -1;
110 static int hf_conf_phasor_mod_b10	= -1;
111 static int hf_conf_phasor_mod_b09	= -1;
112 static int hf_conf_phasor_mod_b08	= -1;
113 static int hf_conf_phasor_mod_b07	= -1;
114 static int hf_conf_phasor_mod_b06	= -1;
115 static int hf_conf_phasor_mod_b05	= -1;
116 static int hf_conf_phasor_mod_b04	= -1;
117 static int hf_conf_phasor_mod_b03	= -1;
118 static int hf_conf_phasor_mod_b02	= -1;
119 static int hf_conf_phasor_mod_b01	= -1;
120 static int hf_conf_phasor_type_b03	= -1;
121 static int hf_conf_phasor_type_b02to00	= -1;
122 static int hf_conf_phasor_user_data	= -1;
123 static int hf_conf_phasor_scale_factor	= -1;
124 static int hf_conf_phasor_angle_offset	= -1;
125 static int hf_conf_analog_scale_factor	= -1;
126 static int hf_conf_analog_offset	= -1;
127 static int hf_conf_pmu_lat		= -1;
128 static int hf_conf_pmu_lon		= -1;
129 static int hf_conf_pmu_elev		= -1;
130 static int hf_conf_pmu_lat_unknown	= -1;
131 static int hf_conf_pmu_lon_unknown	= -1;
132 static int hf_conf_pmu_elev_unknown	= -1;
133 static int hf_conf_svc_class		= -1;
134 static int hf_conf_window		= -1;
135 static int hf_conf_grp_dly		= -1;
136 static int hf_conf_fnom			= -1;
137 static int hf_conf_cfgcnt		= -1;
138 static int hf_data_statb15to14		= -1;
139 static int hf_data_statb13		= -1;
140 static int hf_data_statb12		= -1;
141 static int hf_data_statb11		= -1;
142 static int hf_data_statb10		= -1;
143 static int hf_data_statb09		= -1;
144 static int hf_data_statb08to06		= -1;
145 static int hf_data_statb05to04		= -1;
146 static int hf_data_statb03to00		= -1;
147 static int hf_command			= -1;
148 static int hf_cfg_frame_num		= -1;
149 
150 /* Generated from convert_proto_tree_add_text.pl */
151 static int hf_synphasor_data					= -1;
152 static int hf_synphasor_checksum				= -1;
153 static int hf_synphasor_checksum_status				= -1;
154 static int hf_synphasor_num_phasors				= -1;
155 static int hf_synphasor_num_analog_values			= -1;
156 static int hf_synphasor_num_digital_status_words		= -1;
157 static int hf_synphasor_rate_of_transmission			= -1;
158 static int hf_synphasor_phasor					= -1;
159 static int hf_synphasor_actual_frequency_value			= -1;
160 static int hf_synphasor_rate_change_frequency			= -1;
161 static int hf_synphasor_frequency_deviation_from_nominal	= -1;
162 static int hf_synphasor_analog_value				= -1;
163 static int hf_synphasor_digital_status_word			= -1;
164 static int hf_synphasor_conversion_factor			= -1;
165 static int hf_synphasor_factor_for_analog_value			= -1;
166 static int hf_synphasor_channel_name				= -1;
167 static int hf_synphasor_extended_frame_data			= -1;
168 static int hf_synphasor_unknown_data				= -1;
169 static int hf_synphasor_status_word_mask_normal_state		= -1;
170 static int hf_synphasor_status_word_mask_valid_bits		= -1;
171 
172 static expert_field ei_synphasor_extended_frame_data	= EI_INIT;
173 static expert_field ei_synphasor_checksum		= EI_INIT;
174 static expert_field ei_synphasor_data_error		= EI_INIT;
175 static expert_field ei_synphasor_pmu_not_sync		= EI_INIT;
176 
177 static dissector_handle_t synphasor_udp_handle;
178 
179 /* the different frame types for this protocol */
180 enum FrameType {
181 	DATA = 0,
182 	HEADER,
183 	CFG1,
184 	CFG2,
185 	CMD,
186 	CFG3
187 };
188 
189 /* Structures to save CFG frame content. */
190 
191 /* type to indicate the format for (D)FREQ/PHASORS/ANALOG in data frame	 */
192 typedef enum {	integer,	/* 16 bit signed integer */
193 	       floating_point	/* single precision floating point */
194 } data_format;
195 
196 typedef enum { rect, polar } phasor_notation_e;
197 
198 typedef enum { V, A } unit_e;
199 
200 /* holds the information required to dissect a single phasor */
201 typedef struct {
202 	char	name[MAX_NAME_LEN + 1];
203 	unit_e	unit;
204 	guint32	conv;			/* cfg-2 conversion factor in 10^-5 scale */
205 	float	conv_cfg3;		/* cfg-3 conversion scale factor */
206 	float	angle_offset_cfg3;	/* cfg-3 angle offset */
207 } phasor_info;
208 
209 /* holds the information for an analog value */
210 typedef struct {
211 	char	name[MAX_NAME_LEN + 1];
212 	guint32	conv;		/* cfg-2 conversion scale factor, user defined scaling (so it's pretty useless) */
213 	float	conv_cfg3;	/* cfg-3 conversion scale factor */
214 	float	offset_cfg3;	/* cfg-3 conversion offset */
215 } analog_info;
216 
217 /* holds information required to dissect a single PMU block in a data frame */
218 typedef struct {
219 	guint16		   id;			/* (Data Source ID) identifies source of block     */
220 	char		   name[MAX_NAME_LEN + 1]; /* holds STN			  */
221 	guint8		   cfg_frame_type;	/* Config Frame Type (1,2,3,...)  */
222 	data_format	   format_fr;		/* data format of FREQ and DFREQ  */
223 	data_format	   format_ph;		/* data format of PHASORS	  */
224 	data_format	   format_an;		/* data format of ANALOG	  */
225 	phasor_notation_e  phasor_notation;	/* format of the phasors	  */
226 	guint		   fnom;		/* nominal line frequency	  */
227 	guint		   num_dg;		/* number of digital status words */
228 	wmem_array_t	  *phasors;		/* array of phasor_infos	  */
229 	wmem_array_t	  *analogs;		/* array of analog_infos	  */
230 } config_block;
231 
232 /* holds the id the configuration comes from an and
233  * an array of config_block members */
234 typedef struct {
235 	guint32		fnum;		/* frame number */
236 	guint16		id;		/* (Stream Source ID) identifies source of stream */
237 	guint32		time_base;	/* Time base - resolution of FRACSEC time stamp. */
238 	wmem_array_t	*config_blocks; /* Contains a config_block struct for
239 				      * every PMU included in the config frame */
240 } config_frame;
241 
242 /* strings for type bits in SYNC */
243 static const value_string typenames[] = {
244 	{ 0, "Data Frame"		},
245 	{ 1, "Header Frame"		},
246 	{ 2, "Configuration Frame 1"	},
247 	{ 3, "Configuration Frame 2"	},
248 	{ 4, "Command Frame"		},
249 	{ 5, "Configuration Frame 3"	},
250 	{ 0, NULL			}
251 };
252 
253 /* strings for version bits in SYNC */
254 static const value_string versionnames[] = {
255 	{ 1, "Defined in IEEE Std C37.118-2005"	},
256 	{ 2, "Added in IEEE Std C37.118.2-2011"	},
257 	{ 0, NULL				}
258 };
259 
260 /* strings for the time quality flags in FRACSEC */
261 static const true_false_string leapseconddir = {
262 	"Add",
263 	"Delete"
264 };
265 static const value_string timequalcodes[] = {
266 	{ 0xF, "Clock failure, time not reliable"	},
267 	{ 0xB, "Clock unlocked, time within 10 s"	},
268 	{ 0xA, "Clock unlocked, time within 1 s"	},
269 	{ 0x9, "Clock unlocked, time within 10^-1 s"	},
270 	{ 0x8, "Clock unlocked, time within 10^-2 s"	},
271 	{ 0x7, "Clock unlocked, time within 10^-3 s"	},
272 	{ 0x6, "Clock unlocked, time within 10^-4 s"	},
273 	{ 0x5, "Clock unlocked, time within 10^-5 s"	},
274 	{ 0x4, "Clock unlocked, time within 10^-6 s"	},
275 	{ 0x3, "Clock unlocked, time within 10^-7 s"	},
276 	{ 0x2, "Clock unlocked, time within 10^-8 s"	},
277 	{ 0x1, "Clock unlocked, time within 10^-9 s"	},
278 	{ 0x0, "Normal operation, clock locked"		},
279 	{  0 , NULL					}
280 };
281 
282 /* strings for flags in the FORMAT word of a configuration frame */
283 static const true_false_string conf_formatb123names = {
284 	"32-bit IEEE floating point",
285 	"16-bit integer"
286 };
287 static const true_false_string conf_formatb0names = {
288 	"polar",
289 	"rectangular"
290 };
291 
292 /* strings to decode ANUNIT in configuration frame */
293 static const range_string conf_anconvnames[] = {
294 	{  0,	0, "single point-on-wave"	},
295 	{  1,	1, "rms of analog input"	},
296 	{  2,	2, "peak of input"		},
297 	{  3,	4, "undefined"			},
298 	{  5,  64, "reserved"			},
299 	{ 65, 255, "user defined"		},
300 	{  0,	0, NULL				}
301 };
302 
303 /* strings for the FNOM field */
304 static const true_false_string conf_fnomnames = {
305 	"50Hz",
306 	"60Hz"
307 };
308 
309 static const true_false_string conf_phasor_mod_b15 = {
310 	"Modification applied, type not here defined",
311 	"None"
312 };
313 
314 static const true_false_string conf_phasor_mod_b10 = {
315 	"Pseudo-phasor value (combined from other phasors)",
316 	"None"
317 };
318 
319 static const true_false_string conf_phasor_mod_b09 = {
320 	"Phasor phase adjusted for rotation",
321 	"None"
322 };
323 
324 static const true_false_string conf_phasor_mod_b08 = {
325 	"Phasor phase adjusted for calibration",
326 	"None"
327 };
328 
329 static const true_false_string conf_phasor_mod_b07 = {
330 	"Phasor magnitude adjusted for calibration",
331 	"None"
332 };
333 
334 static const true_false_string conf_phasor_mod_b06 = {
335 	"Filtered without changing sampling",
336 	"None"
337 };
338 
339 static const true_false_string conf_phasor_mod_b05 = {
340 	"Down sampled with non-FIR filter",
341 	"None"
342 };
343 
344 static const true_false_string conf_phasor_mod_b04 = {
345 	"Down sampled with FIR filter",
346 	"None"
347 };
348 
349 static const true_false_string conf_phasor_mod_b03 = {
350 	"Down sampled by reselection",
351 	"None"
352 };
353 
354 static const true_false_string conf_phasor_mod_b02 = {
355 	"Up sampled with extrapolation",
356 	"None"
357 };
358 
359 static const true_false_string conf_phasor_mod_b01 = {
360 	"Up sampled with interpolation",
361 	"None"
362 };
363 
364 static const value_string conf_phasor_type[] = {
365 	{ 0, "Voltage, Zero sequence"		},
366 	{ 1, "Voltage, Positive sequence"	},
367 	{ 2, "Voltage, Negative sequence"	},
368 	{ 3, "Voltage, Reserved"		},
369 	{ 4, "Voltage, Phase A"			},
370 	{ 5, "Voltage, Phase B"			},
371 	{ 6, "Voltage, Phase C"			},
372 	{ 7, "Voltage, Reserved"		},
373 	{ 8, "Current, Zero sequence"		},
374 	{ 9, "Current, Positive sequence"	},
375 	{ 10, "Current, Negative sequence"	},
376 	{ 11, "Current, Reserved"		},
377 	{ 12, "Current, Phase A"		},
378 	{ 13, "Current, Phase B"		},
379 	{ 14, "Current, Phase C"		},
380 	{ 15, "Current, Reserved"		},
381 	{  0, NULL				}
382 };
383 
384 static const true_false_string conf_phasor_type_b03 = {
385 	"Current",
386 	"Voltage"
387 };
388 
389 static const value_string conf_phasor_type_b02to00[] = {
390 	{ 0, "Zero sequence"	},
391 	{ 1, "Positive sequence"},
392 	{ 2, "Negative sequence"},
393 	{ 3, "Reserved"		},
394 	{ 4, "Phase A"		},
395 	{ 5, "Phase B"		},
396 	{ 6, "Phase C"		},
397 	{ 7, "Reserved"		},
398 	{ 0, NULL		}
399 };
400 
401 static const true_false_string conf_phasor_user_defined = {
402 	"Flags set",
403 	"No flags set"
404 };
405 
406 /* strings for flags in the STAT word of a data frame */
407 static const value_string data_statb15to14names[] = {
408 	{ 0, "Good measurement data, no errors"							},
409 	{ 1, "PMU error, no information about data"						},
410 	{ 2, "PMU in test mode or absent data tags have been inserted (do not use values)"	},
411 	{ 3, "PMU error (do not use values)"							},
412 	{ 0, NULL										}
413 };
414 static const true_false_string data_statb13names = {
415 	"Synchronization lost",
416 	"Clock is synchronized"
417 };
418 static const true_false_string data_statb12names = {
419 	"By arrival",
420 	"By timestamp"
421 };
422 static const true_false_string data_statb11names = {
423 	"Trigger detected",
424 	"No trigger"
425 };
426 static const true_false_string data_statb10names = {
427 	"Within 1 minute",
428 	"No"
429 };
430 static const true_false_string data_statb09names = {
431 	"Data modified by a post-processing device",
432 	"Data not modified"
433 };
434 static const value_string      data_statb08to06names[] = {
435 	{ 0, "Not used (indicates code from previous version of profile)"	},
436 	{ 1, "Estimated maximum time error < 100 ns" 				},
437 	{ 2, "Estimated maximum time error < 1 " UTF8_MICRO_SIGN "s"		},
438 	{ 3, "Estimated maximum time error < 10 " UTF8_MICRO_SIGN "s"		},
439 	{ 4, "Estimated maximum time error < 100 " UTF8_MICRO_SIGN "s"		},
440 	{ 5, "Estimated maximum time error < 1 ms"				},
441 	{ 6, "Estimated maximum time error < 10 ms"				},
442 	{ 7, "Estimated maximum time error > 10 ms or time error unknown"	},
443 	{ 0, NULL								}
444 };
445 static const value_string      data_statb05to04names[] = {
446 	{ 0, "Locked or unlocked less than 10 s"},
447 	{ 1, "Unlocked for 10-100 s"		},
448 	{ 2, "Unlocked for 100-1000 s"		},
449 	{ 3, "Unlocked for over 1000 s"		},
450 	{ 0, NULL				}
451 };
452 static const value_string      data_statb03to00names[] = {
453 	{ 0x0, "Manual"				},
454 	{ 0x1, "Magnitude low"			},
455 	{ 0x2, "Magnitude high"			},
456 	{ 0x3, "Phase-angel diff"		},
457 	{ 0x4, "Frequency high or low"		},
458 	{ 0x5, "df/dt high"			},
459 	{ 0x6, "Reserved"			},
460 	{ 0x7, "Digital"			},
461 	{ 0x8, "User defined"			},
462 	{ 0x9, "User defined"			},
463 	{ 0xA, "User defined"			},
464 	{ 0xB, "User defined"			},
465 	{ 0xC, "User defined"			},
466 	{ 0xD, "User defined"			},
467 	{ 0xE, "User defined"			},
468 	{ 0xF, "User defined"			},
469 	{   0, NULL				}
470 };
471 
472 /* strings to decode the commands (CMD Field) acording Table 15, p.26
473 *  0000 0000 0000 0001  -  Turn off transmission of data frames
474 *  0000 0000 0000 0010  -  Turn on transmission of data frames
475 *  0000 0000 0000 0011  -  Send HDR frame
476 *  0000 0000 0000 0100  -  Send CFG-1 frame.
477 *  0000 0000 0000 0101  -  Send CFG-2 frame.
478 *  0000 0000 0000 0110  -  Send CFG-3 frame (optional command).
479 *  0000 0000 0000 1000  -  Extended frame.
480 *  0000 0000 xxxx xxxx  -  All undesignated codes reserved.
481 *  0000 yyyy xxxx xxxx  -  All codes where yyyy ≠ 0 available for user designation.
482 *  zzzz xxxx xxxx xxxx  -  All codes where zzzz ≠ 0 reserved.
483 */
484 static const range_string command_names[] = {
485 	{  0x0000, 0x0000, "reserved codes"		},
486 	{  0x0001, 0x0001, "data transmission off"	},
487 	{  0x0002, 0x0002, "data transmission on"	},
488 	{  0x0003, 0x0003, "send HDR frame"		},
489 	{  0x0004, 0x0004, "send CFG-1 frame"		},
490 	{  0x0005, 0x0005, "send CFG-2 frame"		},
491 	{  0x0006, 0x0006, "send CFG-3 frame"		},
492 	{  0x0007, 0x0007, "reserved codes"		},
493 	{  0x0008, 0x0008, "extended frame"		},
494 	{  0x0009, 0x00FF, "reserved codes"		},
495 	{  0x0100, 0x0FFF, "user designation"		},
496 	{  0x1000, 0xFFFF, "reserved codes"		},
497 	{  0x0000, 0x0000, NULL				}
498 };
499 
500 
501 /******************************************************************************
502 * functions
503 ******************************************************************************/
504 
505 /* read in the size length for names found in config 3 frames
506 	0 - no name
507 	1-255 - length of name
508 */
get_name_length(tvbuff_t * tvb,gint offset)509 static guint8 get_name_length(tvbuff_t *tvb, gint offset)
510 {
511 	guint8 name_length;
512 
513 	/* read the size of the name */
514 	name_length = tvb_get_guint8(tvb, offset);
515 
516 	return name_length;
517 }
518 
519 /* Checks the CRC of a synchrophasor frame, 'tvb' has to include the whole
520  * frame, including CRC, the calculated CRC is returned in '*computedcrc'.
521  */
check_crc(tvbuff_t * tvb,guint16 * computedcrc)522 static gboolean check_crc(tvbuff_t *tvb, guint16 *computedcrc)
523 {
524 	guint16 crc;
525 	guint	len = tvb_get_ntohs(tvb, 2);
526 
527 	crc = tvb_get_ntohs(tvb, len - 2);
528 	*computedcrc = crc16_x25_ccitt_tvb(tvb, len - 2);
529 
530 	if (crc == *computedcrc)
531 		return TRUE;
532 
533 	return FALSE;
534 }
535 
536 /* Dissects a configuration frame (only the most important stuff, tries
537  * to be fast, does no GUI stuff) and returns a pointer to a config_frame
538  * struct that contains all the information from the frame needed to
539  * dissect a DATA frame.
540  *
541  * use 'config_frame_free()' to free the config_frame again
542  */
config_frame_fast(tvbuff_t * tvb)543 static config_frame *config_frame_fast(tvbuff_t *tvb)
544 {
545 	guint16		num_pmu;
546 	gint		offset;
547 	config_frame	*frame;
548 
549 	/* get a new frame and initialize it */
550 	frame = wmem_new(wmem_file_scope(), config_frame);
551 
552 	frame->config_blocks = wmem_array_new(wmem_file_scope(), sizeof(config_block));
553 
554 	// Start with Stream Source ID - identifies source of stream
555 	offset = 4;
556 	frame->id = tvb_get_ntohs(tvb, offset);
557 
558 	/* Skip to time base for FRACSEC */
559 	offset += 11; // high 8 bits reserved for flags, so +1 byte
560 	frame->time_base = tvb_get_guint24(tvb, offset,ENC_BIG_ENDIAN);
561 
562 	/* Next number of PMU blocks */
563 	offset += 3;
564 	num_pmu = tvb_get_ntohs(tvb, offset);
565 
566 	// Start of repeating blocks
567 	offset += 2;
568 
569 	while (num_pmu) {
570 		guint16	     format_flags;
571 		gint	     num_ph,
572 			     num_an,
573 			     num_dg;
574 		gint	     i,
575 			     phunit,
576 			     anunit,
577 			     fnom;
578 		config_block block;
579 
580 		/* initialize the block */
581 		block.phasors = wmem_array_new(wmem_file_scope(), sizeof(phasor_info));
582 		block.analogs = wmem_array_new(wmem_file_scope(), sizeof(analog_info));
583 		/* copy the station name from the tvb to block, and add NULL byte */
584 		tvb_memcpy(tvb, block.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
585 		block.name[CHNAM_LEN] = '\0';
586 		block.cfg_frame_type = 2;
587 		block.id = tvb_get_ntohs(tvb, offset); offset += 2;
588 
589 		format_flags	      = tvb_get_ntohs(tvb, offset); offset += 2;
590 		block.format_fr	      = (format_flags & 0x0008) ? floating_point : integer;
591 		block.format_an	      = (format_flags & 0x0004) ? floating_point : integer;
592 		block.format_ph	      = (format_flags & 0x0002) ? floating_point : integer;
593 		block.phasor_notation = (format_flags & 0x0001) ? polar		 : rect;
594 
595 		num_ph = tvb_get_ntohs(tvb, offset); offset += 2;
596 		num_an = tvb_get_ntohs(tvb, offset); offset += 2;
597 		num_dg = tvb_get_ntohs(tvb, offset); offset += 2;
598 		block.num_dg = num_dg;
599 
600 		/* the offset of the PHUNIT, ANUNIT, and FNOM blocks */
601 		phunit = offset + (num_ph + num_an + num_dg * CHNAM_LEN) * CHNAM_LEN;
602 		anunit = phunit + num_ph * 4;
603 		fnom   = anunit + num_an * 4 + num_dg * 4;
604 
605 		/* read num_ph phasor names and conversion factors */
606 		for (i = 0; i != num_ph; i++) {
607 			phasor_info  pi;
608 			guint32	     conv;
609 
610 			/* copy the phasor name from the tvb, and add NULL byte */
611 			tvb_memcpy(tvb, pi.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
612 			pi.name[CHNAM_LEN] = '\0';
613 
614 			conv = tvb_get_ntohl(tvb, phunit + 4 * i);
615 			pi.unit = conv & 0xFF000000 ? A : V;
616 			pi.conv = conv & 0x00FFFFFF;
617 			pi.conv_cfg3 = 1;
618 			pi.angle_offset_cfg3 = 0;
619 
620 			wmem_array_append_one(block.phasors, pi);
621 		}
622 
623 		/* read num_an analog value names and conversion factors */
624 		for (i = 0; i != num_an; i++) {
625 			analog_info ai;
626 			guint32	    conv;
627 
628 			/* copy the phasor name from the tvb, and add NULL byte */
629 			tvb_memcpy(tvb, ai.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
630 			ai.name[CHNAM_LEN] = '\0';
631 
632 			conv = tvb_get_ntohl(tvb, anunit + 4 * i);
633 			ai.conv = conv;
634 			ai.conv_cfg3 = 1;
635 			ai.offset_cfg3 = 0;
636 
637 			wmem_array_append_one(block.analogs, ai);
638 		}
639 
640 		/* the names for the bits in the digital status words aren't saved,
641 		   there is no space to display them in the GUI anyway */
642 
643 		/* save FNOM */
644 		block.fnom = tvb_get_ntohs(tvb, fnom) & 0x0001 ? 50 : 60;
645 		offset = fnom + 2;
646 
647 		/* skip CFGCNT */
648 		offset += 2;
649 
650 		wmem_array_append_one(frame->config_blocks, block);
651 		num_pmu--;
652 	}
653 
654 	return frame;
655 } /* config_frame_fast() */
656 
657 /* Dissects a configuration 3 frame (only the most important stuff, tries
658  * to be fast, does no GUI stuff) and returns a pointer to a config_frame
659  * struct that contains all the information from the frame needed to
660  * dissect a DATA frame.
661  *
662  * use 'config_frame_free()' to free the config_frame again
663  */
config_3_frame_fast(tvbuff_t * tvb)664 static config_frame * config_3_frame_fast(tvbuff_t *tvb)
665 {
666 	guint16	      num_pmu;
667 	gint	      offset;
668 	config_frame *frame;
669 	phasor_info  *pi = NULL;
670 	analog_info  *ai = NULL;
671 	gboolean      frame_not_fragmented;
672 
673 	/* get a new frame and initialize it */
674 	frame = wmem_new(wmem_file_scope(), config_frame);
675 
676 	frame->config_blocks = wmem_array_new(wmem_file_scope(), sizeof(config_block));
677 
678 	// Start with Stream Source ID - identifies source of stream
679 	offset = 4;
680 	frame->id = tvb_get_ntohs(tvb, offset);
681 
682 	/* Skip to CONT_IDX -- Fragmented Frames not supported at this time */
683 	offset += 10;
684 	frame_not_fragmented = tvb_get_guint16(tvb, offset, ENC_BIG_ENDIAN) == 0;
685 
686 	/* Skip to time base for FRACSEC */
687 	offset += 3; // high 8 bits reserved for flags, so +1 byte
688 	frame->time_base = tvb_get_guint24(tvb, offset,ENC_BIG_ENDIAN);
689 
690 	/* Skip to number of PMU blocks */
691 	offset += 3;
692 	num_pmu = tvb_get_ntohs(tvb, offset);
693 
694 	/* start of repeating blocks */
695 	offset += 2;
696 	while ((num_pmu) && (frame_not_fragmented)) {
697 		guint16	     format_flags;
698 		gint	     num_ph,
699 			     num_an,
700 			     num_dg;
701 		gint	     i;
702 		guint8	     name_length;
703 		config_block block;
704 
705 		/* initialize the block */
706 		block.phasors = wmem_array_new(wmem_file_scope(), sizeof(phasor_info));
707 		block.analogs = wmem_array_new(wmem_file_scope(), sizeof(analog_info));
708 
709 		/* copy the station name from the tvb to block, and add NULL byte */
710 		/* first byte is name size */
711 		name_length = get_name_length(tvb, offset);
712 		offset += 1;
713 
714 		tvb_memcpy(tvb, block.name, offset, name_length);
715 		offset += name_length;
716 
717 		block.name[name_length] = '\0';
718 		block.cfg_frame_type = 3;
719 
720 		/* Block ID and Global PMU ID */
721 		block.id = tvb_get_ntohs(tvb, offset);
722 		offset += 2;
723 
724 		/* skip over Global PMU ID */
725 		offset += G_PMU_ID_LEN;
726 
727 		format_flags	      = tvb_get_ntohs(tvb, offset);
728 		offset += 2;
729 
730 		block.format_fr	      = (format_flags & 0x0008) ? floating_point : integer;
731 		block.format_an	      = (format_flags & 0x0004) ? floating_point : integer;
732 		block.format_ph	      = (format_flags & 0x0002) ? floating_point : integer;
733 		block.phasor_notation = (format_flags & 0x0001) ? polar		 : rect;
734 
735 		num_ph = tvb_get_ntohs(tvb, offset);
736 		offset += 2;
737 
738 		num_an = tvb_get_ntohs(tvb, offset);
739 		offset += 2;
740 
741 		num_dg = tvb_get_ntohs(tvb, offset);
742 		offset += 2;
743 		block.num_dg = num_dg;
744 
745 		/* grab phasor names */
746 		if (num_ph > 0)
747 		{
748 			pi = (phasor_info *)wmem_alloc(wmem_file_scope(), sizeof(phasor_info)*num_ph);
749 
750 			for (i = 0; i != num_ph; i++) {
751 				/* copy the phasor name from the tvb, and add NULL byte */
752 				name_length = get_name_length(tvb, offset);
753 				offset += 1;
754 
755 				tvb_memcpy(tvb, pi[i].name, offset, name_length);
756 				offset += name_length;
757 
758 				pi[i].name[name_length] = '\0';
759 			}
760 		}
761 
762 		/* grab analog names */
763 		if (num_an > 0)
764 		{
765 			ai = (analog_info *)wmem_alloc(wmem_file_scope(), sizeof(analog_info)*num_an);
766 
767 			for (i = 0; i != num_an; i++) {
768 				/* copy the phasor name from the tvb, and add NULL byte */
769 				name_length = get_name_length(tvb, offset);
770 				offset += 1;
771 
772 				tvb_memcpy(tvb, ai[i].name, offset, name_length);
773 				offset += name_length;
774 
775 				ai[i].name[name_length] = '\0';
776 			}
777 		}
778 
779 		/* skip digital names */
780 		if (num_dg > 0)
781 		{
782 			for (i = 0; i != num_dg * 16; i++) {
783 				name_length = get_name_length(tvb, offset);
784 				offset += name_length + 1;
785 			}
786 		}
787 
788 		/* get phasor conversion factors */
789 		if (num_ph > 0)
790 		{
791 			for (i = 0; i != num_ph; i++) {
792 				guint32 phasor_unit;
793 
794 				/* get unit */
795 				phasor_unit = tvb_get_ntohl(tvb, offset);
796 				pi[i].unit = phasor_unit & 0x00000800 ? A : V;
797 				pi[i].conv = 1;
798 				pi[i].conv_cfg3 = tvb_get_ntohieee_float(tvb, offset + 4);
799 				pi[i].angle_offset_cfg3 = tvb_get_ntohieee_float(tvb, offset + 8);
800 
801 				wmem_array_append_one(block.phasors, pi[i]);
802 
803 				offset += 12;
804 			}
805 		}
806 
807 		/* get analog conversion factors */
808 		if (num_an > 0)
809 		{
810 			for (i = 0; i != num_an; i++) {
811 				ai[i].conv = 1;
812 				ai[i].conv_cfg3 = tvb_get_ntohieee_float(tvb, offset);
813 				ai[i].offset_cfg3 = tvb_get_ntohieee_float(tvb, offset + 4);
814 
815 				wmem_array_append_one(block.analogs, ai[i]);
816 
817 				offset += 8;
818 			}
819 		}
820 
821 		/* skip digital masks */
822 		if (num_dg > 0)
823 		{
824 			for (i = 0; i != num_dg; i++) {
825 				offset += 4;
826 			}
827 		}
828 
829 		/* Skip to FNOM */
830 		offset += 21;
831 
832 		/* save FNOM */
833 		block.fnom = tvb_get_ntohs(tvb, offset) & 0x0001 ? 50 : 60;
834 		offset += 2;
835 
836 		/* skip CFGCNT - offset ready for next PMU */
837 		offset += 2;
838 
839 		wmem_array_append_one(frame->config_blocks, block);
840 		num_pmu--;
841 	}
842 
843 	return frame;
844 } /* config_3_frame_fast() */
845 
846 /* Dissects the common header of frames.
847  *
848  * Returns the framesize, in contrast to most
849  * other helper functions that return the offset.
850  */
dissect_header(tvbuff_t * tvb,proto_tree * tree,packet_info * pinfo)851 static gint dissect_header(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo)
852 {
853 	proto_tree *temp_tree;
854 	proto_item *temp_item;
855 	config_frame *conf;
856 
857 	gint	offset = 0;
858 	guint16	framesize;
859 
860 	conf = (config_frame *)p_get_proto_data(wmem_file_scope(), pinfo, proto_synphasor, 0);
861 
862 	/* SYNC and flags */
863 	temp_item = proto_tree_add_item(tree, hf_sync, tvb, offset, 2, ENC_BIG_ENDIAN);
864 	temp_tree = proto_item_add_subtree(temp_item, ett_frtype);
865 	proto_tree_add_item(temp_tree, hf_sync_frtype,	tvb, offset, 2, ENC_BIG_ENDIAN);
866 	proto_tree_add_item(temp_tree, hf_sync_version, tvb, offset, 2, ENC_BIG_ENDIAN);
867 	offset += 2;
868 
869 	/* FRAMESIZE */
870 	proto_tree_add_item(tree, hf_frsize, tvb, offset, 2, ENC_BIG_ENDIAN);
871 	framesize = tvb_get_ntohs(tvb, offset); offset += 2;
872 
873 	/* IDCODE */
874 	proto_tree_add_item(tree, hf_idcode_stream_source, tvb, offset, 2, ENC_BIG_ENDIAN);
875 	offset += 2;
876 
877 	/* SOC */
878 	proto_tree_add_item(tree, hf_soc, tvb, offset, 4, ENC_TIME_SECS | ENC_BIG_ENDIAN);
879 	offset += 4;
880 
881 	/* FRACSEC */
882 	/* time quality flags */
883 	temp_tree = proto_tree_add_subtree(tree, tvb, offset, 1, ett_timequal, NULL, "Time quality flags");
884 	proto_tree_add_item(temp_tree, hf_timeqal_lsdir,	 tvb, offset, 1, ENC_BIG_ENDIAN);
885 	proto_tree_add_item(temp_tree, hf_timeqal_lsocc,	 tvb, offset, 1, ENC_BIG_ENDIAN);
886 	proto_tree_add_item(temp_tree, hf_timeqal_lspend,	 tvb, offset, 1, ENC_BIG_ENDIAN);
887 	proto_tree_add_item(temp_tree, hf_timeqal_timequalindic, tvb, offset, 1, ENC_BIG_ENDIAN);
888 	offset += 1;
889 
890 	// Add RAW FRACSEC
891 	proto_tree_add_item(tree, hf_fracsec_raw,  tvb, offset, 3, ENC_BIG_ENDIAN);
892 
893 	// If exist configuration frame, add fracsec in milliseconds
894 	if (conf){
895 		guint32 fracsec_raw = tvb_get_guint24(tvb, offset, ENC_BIG_ENDIAN);
896 		float	fracsec_ms = 1000.0f*fracsec_raw/conf->time_base;
897 		proto_tree_add_float(tree, hf_fracsec_ms, tvb, offset, 3, fracsec_ms);
898 	} else
899 	{
900 	}
901 	/*offset += 3;*/
902 
903 	return framesize;
904 }
905 
906 /* Dissects a single phasor for 'dissect_PHASORS()' */
dissect_single_phasor(tvbuff_t * tvb,int offset,gdouble * mag,gdouble * phase,gdouble * real,gdouble * imag,gdouble * mag_real_unscaled,gdouble * phase_imag_unscaled,config_block * block,phasor_info * pi)907 static int dissect_single_phasor(tvbuff_t *tvb, int offset,
908 					gdouble *mag, gdouble *phase, /* returns the resulting values in polar format here */
909 					gdouble* real, gdouble* imag, /* returns the resulting values in rectangular format here*/
910 					gdouble* mag_real_unscaled, gdouble* phase_imag_unscaled, /* returns unscaled values*/
911 					config_block *block,	      /* information needed to... */
912 					phasor_info* pi)	      /*  ...dissect the phasor	*/
913 {
914 	if (floating_point == block->format_ph) {
915 		if (polar == block->phasor_notation) {
916 			/* float, polar */
917 			*mag   = tvb_get_ntohieee_float(tvb, offset    );
918 			*phase = tvb_get_ntohieee_float(tvb, offset + 4);
919 
920 			*real = (*mag) * cos(*phase);
921 			*imag = (*mag) * sin(*phase);
922 		}
923 		else {
924 			/* float, rect */
925 			*real = tvb_get_ntohieee_float(tvb, offset    );
926 			*imag = tvb_get_ntohieee_float(tvb, offset + 4);
927 
928 			*mag   = sqrt(pow(*real, 2) + pow(*imag, 2));
929 			*phase = atan2(*imag, *real);
930 		}
931 	}
932 	else {
933 		if (polar == block->phasor_notation) {
934 			/* int, polar */
935 			*mag_real_unscaled = tvb_get_ntohs(tvb, offset	);
936 			*phase_imag_unscaled = tvb_get_ntohis(tvb, offset + 2);
937 
938 			/* For fixed-point data in polar format all values are permissible for the magnitude
939 			   field. However, the angle field is restricted to ±31416. A value of 0x8000 (–32768) used in the angle field
940 			   will be used to signify absent data.
941 			   bullet 6.3.1 page 16 IEEE Std C37.118.2-2011
942 			*/
943 			if (*phase_imag_unscaled == -32768) {
944 				*phase_imag_unscaled = NAN;
945 				*mag_real_unscaled = NAN;
946 			}
947 
948 			*phase = *phase_imag_unscaled/10000.0; /* angle is in radians*10^4 */
949 
950 			/* for values in integer format, consider conversation factor */
951 			if (block->cfg_frame_type == 3){
952 				*mag = (*mag_real_unscaled * pi->conv_cfg3);
953 				*phase = *phase - pi->angle_offset_cfg3;
954 			}
955 			else{
956 				*mag = (*mag_real_unscaled * pi->conv) * 0.00001;
957 			}
958 
959 			*real = (*mag) * cos(*phase);
960 			*imag = (*mag) * sin(*phase);
961 		}
962 		else {
963 			/* int, rect */
964 			*mag_real_unscaled = tvb_get_ntohis(tvb, offset    );
965 			*phase_imag_unscaled = tvb_get_ntohis(tvb, offset + 2);
966 
967 			/* For fixed-point data in rectangular format the PDC will use
968 			   0x8000 (–32768) as the substitute for the absent data.
969 			   bullet 6.3.1 page 16 IEEE Std C37.118.2-2011
970 			*/
971 			if (*mag_real_unscaled == -32768) {
972 				*mag_real_unscaled = NAN;
973 			}
974 			if (*phase_imag_unscaled == -32768) {
975 				*phase_imag_unscaled = NAN;
976 			}
977 
978 			*mag = sqrt(pow(*mag_real_unscaled, 2) + pow(*phase_imag_unscaled, 2));
979 			*phase = atan2(*phase_imag_unscaled, *mag_real_unscaled);
980 
981 			/* for values in integer format, consider conversation factor */
982 			if (block->cfg_frame_type == 3) {
983 				*mag = (*mag * pi->conv_cfg3);
984 				*phase = *phase - pi->angle_offset_cfg3;
985 			}
986 			else {
987 				*mag = (*mag * pi->conv) * 0.00001;
988 			}
989 
990 			*real = (*mag) * cos(*phase);
991 			*imag = (*mag) * sin(*phase);
992 		}
993 	}
994 
995 	return floating_point == block->format_ph ? 8 : 4;
996 }
997 
998 /* used by 'dissect_data_frame()' to dissect the PHASORS field */
dissect_PHASORS(tvbuff_t * tvb,proto_tree * tree,config_block * block,gint offset)999 static gint dissect_PHASORS(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
1000 {
1001 	proto_tree *phasor_tree;
1002 	guint	    length;
1003 	gint	    j;
1004 	gint	    cnt = wmem_array_get_count(block->phasors); /* number of phasors to dissect */
1005 
1006 	if (0 == cnt)
1007 		return offset;
1008 
1009 	length	    = wmem_array_get_count(block->phasors) * (floating_point == block->format_ph ? 8 : 4);
1010 	phasor_tree = proto_tree_add_subtree_format(tree, tvb, offset, length, ett_data_phasors, NULL,
1011 						    "Phasors (%u), notation: %s, format: %s", cnt,
1012 							block->phasor_notation ? "polar" : "rectangular",
1013 							block->format_ph ? "floating point" : "integer");
1014 
1015 	/* dissect a phasor for every phasor_info saved in the config_block */
1016 	for (j = 0; j < cnt; j++) {
1017 		proto_item  *temp_item;
1018 		gdouble	     mag, phase,real, imag;
1019 		gdouble	     mag_real_unscaled = NAN, phase_imag_unscaled = NAN;
1020 		phasor_info *pi;
1021 
1022 		pi = (phasor_info *)wmem_array_index(block->phasors, j);
1023 		temp_item = proto_tree_add_string_format(phasor_tree, hf_synphasor_phasor, tvb, offset,
1024 						floating_point == block->format_ph ? 8 : 4, pi->name,
1025 						"Phasor #%u: \"%s\"", j + 1, pi->name);
1026 
1027 		offset += dissect_single_phasor(tvb, offset,
1028 						&mag, &phase, &real, &imag,
1029 						&mag_real_unscaled, &phase_imag_unscaled,
1030 						block,pi);
1031 
1032 		#define SYNP_ANGLE "\xe2\x88\xa0"      /*   8736 / 0x2220 */
1033 
1034 		char phasor_unit = V == pi->unit ? 'V' : 'A';
1035 
1036 		proto_item_append_text(temp_item, ", %10.3F%c " SYNP_ANGLE "%7.3F" UTF8_DEGREE_SIGN " alt %7.3F+j%7.3F%c",
1037 			mag, phasor_unit, phase * 180.0 / G_PI,
1038 			real, imag, phasor_unit);
1039 		if (integer == block->format_ph) {
1040 			proto_item_append_text(temp_item, "; unscaled: %5.0F, %5.0F",
1041 				mag_real_unscaled, phase_imag_unscaled);
1042 		}
1043 		#undef SYNP_ANGLE
1044 	}
1045 	return offset;
1046 }
1047 
1048 /* used by 'dissect_data_frame()' to dissect the FREQ and DFREQ fields */
dissect_DFREQ(tvbuff_t * tvb,proto_tree * tree,config_block * block,gint offset)1049 static gint dissect_DFREQ(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
1050 {
1051 	if (floating_point == block->format_fr) {
1052 		proto_tree_add_item(tree, hf_synphasor_actual_frequency_value, tvb, offset, 4, ENC_BIG_ENDIAN);
1053 		offset += 4;
1054 
1055 		/* In new version of the standard IEEE Std C37.118.2-2011: "Can be 16-bit integer or IEEE floating point, same as FREQ above."
1056 		 * --> no scaling factor is applied to DFREQ
1057 		 */
1058 		proto_tree_add_item(tree, hf_synphasor_rate_change_frequency, tvb, offset, 4, ENC_BIG_ENDIAN);
1059 		offset += 4;
1060 	}
1061 	else {
1062 		gint16 tmp;
1063 
1064 		tmp = tvb_get_ntohs(tvb, offset);
1065 		proto_tree_add_int_format_value(tree, hf_synphasor_frequency_deviation_from_nominal, tvb, offset, 2, tmp,
1066 				    "%dmHz (actual frequency: %.3fHz)", tmp, block->fnom + (tmp / 1000.0));
1067 		offset += 2;
1068 
1069 		tmp = tvb_get_ntohs(tvb, offset);
1070 		proto_tree_add_float_format_value(tree, hf_synphasor_rate_change_frequency, tvb, offset, 2, (gfloat)(tmp / 100.0), "%.3fHz/s", tmp / 100.0); offset += 2;
1071 	}
1072 	return offset;
1073 }
1074 
1075 /* used by 'dissect_data_frame()' to dissect the ANALOG field */
dissect_ANALOG(tvbuff_t * tvb,proto_tree * tree,config_block * block,gint offset)1076 static gint dissect_ANALOG(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
1077 {
1078 	proto_tree *analog_tree;
1079 	guint	    length;
1080 	gint	    j;
1081 	gint	    cnt = wmem_array_get_count(block->analogs); /* number of analog values to dissect */
1082 
1083 	if (0 == cnt)
1084 		return offset;
1085 
1086 	length	    = wmem_array_get_count(block->analogs) * (floating_point == block->format_an ? 4 : 2);
1087 	analog_tree = proto_tree_add_subtree_format(tree, tvb, offset, length, ett_data_analog, NULL,
1088 						    "Analog values (%u)", cnt);
1089 
1090 	for (j = 0; j < cnt; j++) {
1091 		proto_item *temp_item;
1092 		analog_info *ai = (analog_info *)wmem_array_index(block->analogs, j);
1093 
1094 		temp_item = proto_tree_add_string_format(analog_tree, hf_synphasor_analog_value, tvb, offset,
1095 						floating_point == block->format_an ? 4 : 2, ai->name,
1096 						"Analog value #%u: \"%s\"", j + 1, ai->name);
1097 
1098 		if (block->cfg_frame_type == 3)
1099 		{
1100 			if (floating_point == block->format_an) {
1101 				gfloat tmp;
1102 
1103 				tmp = tvb_get_ntohieee_float(tvb, offset);
1104 				offset += 4;
1105 
1106 				proto_item_append_text(temp_item, ", %.3f", tmp);
1107 			}
1108 			else {
1109 				/* the "standard" doesn't say if this is signed or unsigned,
1110 				* so I just use gint16 */
1111 				gint16 tmp_i;
1112 				gfloat tmp_f;
1113 
1114 				tmp_i = tvb_get_ntohs(tvb, offset);
1115 				offset += 2;
1116 
1117 				tmp_f = (tmp_i * ai->conv_cfg3) + ai->offset_cfg3;
1118 
1119 				proto_item_append_text(temp_item, ", %.3f", tmp_f);
1120 			}
1121 		}
1122 		else
1123 		{
1124 			if (floating_point == block->format_an) {
1125 				gfloat tmp = tvb_get_ntohieee_float(tvb, offset); offset += 4;
1126 				proto_item_append_text(temp_item, ", %.3f", tmp);
1127 			}
1128 			else {
1129 				/* the "standard" doesn't say if this is signed or unsigned,
1130 				 * so I just use gint16; the scaling of the conversion factor
1131 				 * is also "user defined", so I just write it after the analog value */
1132 				gint16 tmp = tvb_get_ntohs(tvb, offset); offset += 2;
1133 				proto_item_append_text(temp_item, ", %" G_GINT16_FORMAT " (conversion factor: %#06x)",
1134 						       tmp, ai->conv);
1135 			}
1136 		}
1137 	}
1138 	return offset;
1139 }
1140 
1141 /* used by 'dissect_data_frame()' to dissect the DIGITAL field */
dissect_DIGITAL(tvbuff_t * tvb,proto_tree * tree,config_block * block,gint offset)1142 static gint dissect_DIGITAL(tvbuff_t *tvb, proto_tree *tree, config_block *block, gint offset)
1143 {
1144 	gint	    j;
1145 	gint	    cnt = block->num_dg; /* number of digital status words to dissect */
1146 
1147 	if (0 == cnt)
1148 		return offset;
1149 
1150 	tree = proto_tree_add_subtree_format(tree, tvb, offset, cnt * 2, ett_data_digital, NULL,
1151 					     "Digital status words (%u)", cnt);
1152 
1153 	for (j = 0; j < cnt; j++) {
1154 		guint16 tmp = tvb_get_ntohs(tvb, offset);
1155 		proto_tree_add_uint_format(tree, hf_synphasor_digital_status_word, tvb, offset, 2, tmp, "Digital status word #%u: 0x%04x", j + 1, tmp);
1156 		offset += 2;
1157 	}
1158 	return offset;
1159 }
1160 
1161 /* used by 'dissect_config_frame()' to dissect the PHUNIT field */
dissect_PHUNIT(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt)1162 static gint dissect_PHUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1163 {
1164 	proto_tree *temp_tree;
1165 	gint i;
1166 
1167 	if (0 == cnt)
1168 		return offset;
1169 
1170 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, 4 * cnt, ett_conf_phconv, NULL,
1171 						  "Phasor conversion factors (%u)", cnt);
1172 
1173 	/* Conversion factor for phasor channels. Four bytes for each phasor.
1174 	 * MSB:		  0 = voltage, 1 = current
1175 	 * Lower 3 Bytes: unsigned 24-bit word in 10^-5 V or A per bit to scale the phasor value
1176 	 */
1177 	for (i = 0; i < cnt; i++) {
1178 		guint32 tmp = tvb_get_ntohl(tvb, offset);
1179 		proto_tree_add_uint_format(temp_tree, hf_synphasor_conversion_factor, tvb, offset, 4,
1180 				    tmp, "#%u factor: %u * 10^-5, unit: %s",
1181 				    i + 1,
1182 				    tmp & 0x00FFFFFF,
1183 				    tmp & 0xFF000000 ? "Ampere" : "Volt");
1184 		offset += 4;
1185 	}
1186 
1187 	return offset;
1188 }
1189 
1190 /* used by 'dissect_config_3_frame()' to dissect the PHSCALE field */
dissect_PHSCALE(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt)1191 static gint dissect_PHSCALE(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1192 {
1193 	proto_tree *temp_tree;
1194 	gint i;
1195 
1196 	if (0 == cnt) {
1197 		return offset;
1198 	}
1199 
1200 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, 12 * cnt, ett_conf_phconv, NULL,
1201 						  "Phasor scaling and data flags (%u)", cnt);
1202 
1203 	for (i = 0; i < cnt; i++) {
1204 		proto_tree *single_phasor_scaling_and_flags_tree;
1205 		proto_tree *phasor_flag1_tree;
1206 		proto_tree *phasor_flag2_tree;
1207 		proto_tree *data_flag_tree;
1208 
1209 		single_phasor_scaling_and_flags_tree = proto_tree_add_subtree_format(temp_tree, tvb, offset, 12,
1210 										     ett_conf_phlist, NULL,
1211 										     "Phasor #%u", i + 1);
1212 
1213 		data_flag_tree = proto_tree_add_subtree_format(single_phasor_scaling_and_flags_tree, tvb, offset, 4,
1214 							       ett_conf_phflags, NULL, "Phasor Data flags: %s",
1215 							       conf_phasor_type[tvb_get_guint8(tvb, offset + 2)].strptr);
1216 
1217 		/* first and second bytes - phasor modification flags*/
1218 		phasor_flag1_tree = proto_tree_add_subtree_format(data_flag_tree, tvb, offset, 2, ett_conf_phmod_flags,
1219 								  NULL, "Modification Flags: 0x%04x",
1220 								  tvb_get_ntohs(tvb, offset));
1221 
1222 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b15, tvb, offset, 2, ENC_BIG_ENDIAN);
1223 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b10, tvb, offset, 2, ENC_BIG_ENDIAN);
1224 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b09, tvb, offset, 2, ENC_BIG_ENDIAN);
1225 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b08, tvb, offset, 2, ENC_BIG_ENDIAN);
1226 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b07, tvb, offset, 2, ENC_BIG_ENDIAN);
1227 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b06, tvb, offset, 2, ENC_BIG_ENDIAN);
1228 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b05, tvb, offset, 2, ENC_BIG_ENDIAN);
1229 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b04, tvb, offset, 2, ENC_BIG_ENDIAN);
1230 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b03, tvb, offset, 2, ENC_BIG_ENDIAN);
1231 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b02, tvb, offset, 2, ENC_BIG_ENDIAN);
1232 		proto_tree_add_item(phasor_flag1_tree, hf_conf_phasor_mod_b01, tvb, offset, 2, ENC_BIG_ENDIAN);
1233 		offset += 2;
1234 
1235 		/* third byte - phasor type*/
1236 		proto_tree_add_item(data_flag_tree, hf_conf_phasor_type_b03, tvb, offset, 1, ENC_BIG_ENDIAN);
1237 		proto_tree_add_item(data_flag_tree, hf_conf_phasor_type_b02to00, tvb, offset, 1, ENC_BIG_ENDIAN);
1238 		offset += 1;
1239 
1240 		/* fourth byte - user designation*/
1241 		phasor_flag2_tree = proto_tree_add_subtree_format(data_flag_tree, tvb, offset, 1, ett_conf_ph_user_flags,
1242 								  NULL, "User designated flags: 0x%02x",
1243 								  tvb_get_guint8(tvb, offset));
1244 
1245 		proto_tree_add_item(phasor_flag2_tree, hf_conf_phasor_user_data, tvb, offset, 1, ENC_BIG_ENDIAN);
1246 		offset += 1;
1247 
1248 		/* phasor scalefactor */
1249 		proto_tree_add_item(single_phasor_scaling_and_flags_tree, hf_conf_phasor_scale_factor,
1250 				    tvb, offset, 4, ENC_BIG_ENDIAN);
1251 		offset += 4;
1252 
1253 		/* angle adjustment */
1254 		proto_tree_add_item(single_phasor_scaling_and_flags_tree, hf_conf_phasor_angle_offset,
1255 				    tvb, offset, 4, ENC_BIG_ENDIAN);
1256 		offset += 4;
1257 	}
1258 
1259 	return offset;
1260 }
1261 
1262 /* used by 'dissect_config_frame()' to dissect the ANUNIT field */
dissect_ANUNIT(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt)1263 static gint dissect_ANUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1264 {
1265 	proto_item *temp_item;
1266 	proto_tree *temp_tree;
1267 	gint i;
1268 
1269 	if (0 == cnt)
1270 		return offset;
1271 
1272 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, 4 * cnt, ett_conf_anconv, NULL,
1273 						  "Analog values conversion factors (%u)", cnt);
1274 
1275 	/* Conversion factor for analog channels. Four bytes for each analog value.
1276 	 * MSB: see 'synphasor_conf_anconvnames' in 'synphasor_strings.c'
1277 	 * Lower 3 Bytes: signed 24-bit word, user-defined scaling
1278 	 */
1279 	for (i = 0; i < cnt; i++) {
1280 		gint32 tmp = tvb_get_ntohl(tvb, offset);
1281 		temp_item = proto_tree_add_uint_format(temp_tree, hf_synphasor_factor_for_analog_value, tvb, offset, 4,
1282 						tmp, "Factor for analog value #%i: %s",
1283 						i + 1,
1284 						try_rval_to_str((tmp >> 24) & 0x000000FF, conf_anconvnames));
1285 
1286 		tmp &= 0x00FFFFFF;
1287 		if (	tmp &  0x00800000) /* sign bit set */
1288 			tmp |= 0xFF000000;
1289 
1290 		proto_item_append_text(temp_item, ", value: %" G_GINT32_FORMAT, tmp);
1291 
1292 		offset += 4;
1293 	}
1294 
1295 	return offset;
1296 }
1297 
1298 /* used by 'dissect_config_3_frame()' to dissect the ANSCALE field */
dissect_ANSCALE(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt)1299 static gint dissect_ANSCALE(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1300 {
1301 	proto_tree *temp_tree;
1302 	gint i;
1303 
1304 	if (0 == cnt) {
1305 		return offset;
1306 	}
1307 
1308 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, 8 * cnt, ett_conf_anconv, NULL,
1309 						  "Analog values conversion factors (%u)", cnt);
1310 
1311 	/* Conversion factor for analog channels. Four bytes for each analog value.
1312 	 * MSB: see 'synphasor_conf_anconvnames' in 'synphasor_strings.c'
1313 	 * Lower 3 Bytes: signed 24-bit word, user-defined scaling
1314 	 */
1315 	for (i = 0; i < cnt; i++) {
1316 		proto_tree *single_analog_scalefactor_tree;
1317 
1318 		single_analog_scalefactor_tree = proto_tree_add_subtree_format(temp_tree, tvb, offset, 8,
1319 									       ett_conf_phlist, NULL,
1320 									       "Analog #%u", i + 1);
1321 
1322 		/* analog scalefactor */
1323 		proto_tree_add_item(single_analog_scalefactor_tree, hf_conf_analog_scale_factor,
1324 				    tvb, offset, 4, ENC_BIG_ENDIAN);
1325 		offset += 4;
1326 
1327 		/* angle adjustment */
1328 		proto_tree_add_item(single_analog_scalefactor_tree, hf_conf_analog_offset,
1329 				    tvb, offset, 4, ENC_BIG_ENDIAN);
1330 		offset += 4;
1331 	}
1332 
1333 	return offset;
1334 }
1335 
1336 /* used by 'dissect_config_frame()' to dissect the DIGUNIT field */
dissect_DIGUNIT(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt)1337 static gint dissect_DIGUNIT(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt)
1338 {
1339 	proto_tree *temp_tree, *mask_tree;
1340 	gint i;
1341 
1342 	if (0 == cnt)
1343 		return offset;
1344 
1345 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, 4 * cnt, ett_conf_dgmask, NULL,
1346 						  "Masks for digital status words (%u)", cnt);
1347 
1348 	/* Mask words for digital status words. Two 16-bit words for each digital word. The first
1349 	 * indicates the normal status of the inputs, the second indicated the valid bits in
1350 	 * the status word
1351 	 */
1352 	for (i = 0; i < cnt; i++) {
1353 
1354 		mask_tree = proto_tree_add_subtree_format(temp_tree, tvb, offset, 4, ett_status_word_mask, NULL, "Mask for status word #%u: ", i + 1);
1355 		proto_tree_add_item(mask_tree, hf_synphasor_status_word_mask_normal_state, tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
1356 		proto_tree_add_item(mask_tree, hf_synphasor_status_word_mask_valid_bits, tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
1357 	}
1358 
1359 	return offset;
1360 }
1361 
1362 /* used by 'dissect_config_frame()' to dissect the "channel name"-fields */
dissect_CHNAM(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt,const char * prefix)1363 static gint dissect_CHNAM(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt, const char *prefix)
1364 {
1365 	proto_tree *temp_tree;
1366 	gint i;
1367 
1368 	if (0 == cnt)
1369 		return offset;
1370 
1371 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, CHNAM_LEN * cnt, ett_conf_phnam, NULL,
1372 						  "%ss (%u)", prefix, cnt);
1373 
1374 	/* dissect the 'cnt' channel names */
1375 	for (i = 0; i < cnt; i++) {
1376 		char *str;
1377 		str = (char *)tvb_get_string_enc(wmem_packet_scope(), tvb, offset, CHNAM_LEN, ENC_ASCII);
1378 		proto_tree_add_string_format(temp_tree, hf_synphasor_channel_name, tvb, offset, CHNAM_LEN,
1379 				    str, "%s #%i: \"%s\"", prefix, i+1, str);
1380 		offset += CHNAM_LEN;
1381 	}
1382 
1383 	return offset;
1384 }
1385 
1386 /* used by 'dissect_config_3_frame()' to dissect the "channel name"-fields */
dissect_config_3_CHNAM(tvbuff_t * tvb,proto_tree * tree,gint offset,gint cnt,const char * prefix)1387 static gint dissect_config_3_CHNAM(tvbuff_t *tvb, proto_tree *tree, gint offset, gint cnt, const char *prefix)
1388 {
1389 	proto_tree *temp_tree, *chnam_tree;
1390 	gint i;
1391 	guint8 name_length;
1392 	gint temp_offset;
1393 	gint subsection_length = 0;
1394 
1395 	if (0 == cnt) {
1396 		return offset;
1397 	}
1398 
1399 	/* get the subsection length */
1400 	temp_offset = offset;
1401 	for (i = 0; i < cnt; i++) {
1402 		name_length = get_name_length(tvb, temp_offset);
1403 		/* count the length byte and the actual name */
1404 		subsection_length += name_length + 1;
1405 		temp_offset += name_length + 1;
1406 	}
1407 
1408 	temp_tree = proto_tree_add_subtree_format(tree, tvb, offset, subsection_length, ett_conf_phnam,
1409 						  NULL, "%ss (%u)", prefix, cnt);
1410 
1411 	/* dissect the 'cnt' channel names */
1412 	for (i = 0; i < cnt; i++) {
1413 		char *str;
1414 
1415 		name_length = get_name_length(tvb, offset);
1416 		str = (char *)tvb_get_string_enc(wmem_packet_scope(), tvb, offset + 1, name_length, ENC_ASCII);
1417 		chnam_tree = proto_tree_add_subtree_format(temp_tree, tvb, offset, name_length + 1, ett_conf,
1418 							   NULL, "%s #%i: \"%s\"", prefix, i + 1, str);
1419 
1420 		proto_tree_add_item(chnam_tree, hf_conf_chnam_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1421 		offset += 1;
1422 
1423 		proto_tree_add_string(chnam_tree, hf_conf_chnam, tvb, offset, 1, str);
1424 		offset += name_length;
1425 	}
1426 
1427 	return offset;
1428 }
1429 
1430 /* dissects a configuration frame (type 1 and 2) and adds fields to 'config_item' */
dissect_config_frame(tvbuff_t * tvb,proto_item * config_item)1431 static int dissect_config_frame(tvbuff_t *tvb, proto_item *config_item)
1432 {
1433 	proto_tree *config_tree;
1434 	gint	    offset = 0;
1435 	guint16	    num_pmu, j;
1436 
1437 	proto_item_set_text   (config_item, "Configuration data");
1438 	config_tree = proto_item_add_subtree(config_item, ett_conf);
1439 
1440 	/* TIME_BASE and NUM_PMU */
1441 	offset += 1; /* skip the reserved byte */
1442 	proto_tree_add_item(config_tree, hf_conf_timebase, tvb, offset, 3, ENC_BIG_ENDIAN); offset += 3;
1443 	proto_tree_add_item(config_tree, hf_conf_numpmu,   tvb, offset, 2, ENC_BIG_ENDIAN);
1444 	/* add number of included PMUs to the text in the list view  */
1445 	num_pmu = tvb_get_ntohs(tvb, offset); offset += 2;
1446 	proto_item_append_text(config_item, ", %"G_GUINT16_FORMAT" PMU(s) included", num_pmu);
1447 
1448 	/* dissect the repeating PMU blocks */
1449 	for (j = 0; j < num_pmu; j++) {
1450 		guint16	    num_ph, num_an, num_dg;
1451 		proto_item *station_item;
1452 		proto_tree *station_tree;
1453 		proto_tree *temp_tree;
1454 		char	   *str;
1455 
1456 		gint oldoffset = offset; /* to calculate the length of the whole PMU block later */
1457 
1458 		/* STN with new tree to add the rest of the PMU block */
1459 		str = (char *)tvb_get_string_enc(wmem_packet_scope(), tvb, offset, CHNAM_LEN, ENC_ASCII);
1460 		station_tree = proto_tree_add_subtree_format(config_tree, tvb, offset, CHNAM_LEN,
1461 							     ett_conf_station, &station_item,
1462 							     "Station #%i: \"%s\"", j + 1, str);
1463 		offset += CHNAM_LEN;
1464 
1465 		/* IDCODE */
1466 		proto_tree_add_item(station_tree, hf_idcode_data_source, tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
1467 
1468 		/* FORMAT */
1469 		temp_tree = proto_tree_add_subtree(station_tree, tvb, offset, 2, ett_conf_format, NULL,
1470 						   "Data format in data frame");
1471 		proto_tree_add_item(temp_tree, hf_conf_formatb3, tvb, offset, 2, ENC_BIG_ENDIAN);
1472 		proto_tree_add_item(temp_tree, hf_conf_formatb2, tvb, offset, 2, ENC_BIG_ENDIAN);
1473 		proto_tree_add_item(temp_tree, hf_conf_formatb1, tvb, offset, 2, ENC_BIG_ENDIAN);
1474 		proto_tree_add_item(temp_tree, hf_conf_formatb0, tvb, offset, 2, ENC_BIG_ENDIAN);
1475 		offset += 2;
1476 
1477 		/* PHNMR, ANNMR, DGNMR */
1478 		num_ph = tvb_get_ntohs(tvb, offset    );
1479 		num_an = tvb_get_ntohs(tvb, offset + 2);
1480 		num_dg = tvb_get_ntohs(tvb, offset + 4);
1481 		proto_tree_add_uint(station_tree, hf_synphasor_num_phasors, tvb, offset, 2, num_ph);
1482 		proto_tree_add_uint(station_tree, hf_synphasor_num_analog_values, tvb, offset + 2, 2, num_an);
1483 		proto_tree_add_uint(station_tree, hf_synphasor_num_digital_status_words, tvb, offset + 4, 2, num_dg);
1484 		offset += 6;
1485 
1486 		/* CHNAM, the channel names */
1487 		offset = dissect_CHNAM(tvb, station_tree, offset, num_ph     , "Phasor name"	     );
1488 		offset = dissect_CHNAM(tvb, station_tree, offset, num_an     , "Analog value"	     );
1489 		offset = dissect_CHNAM(tvb, station_tree, offset, num_dg * 16, "Digital status label");
1490 
1491 		/* PHUNIT, ANUINT and DIGUNIT */
1492 		offset = dissect_PHUNIT (tvb, station_tree, offset, num_ph);
1493 		offset = dissect_ANUNIT (tvb, station_tree, offset, num_an);
1494 		offset = dissect_DIGUNIT(tvb, station_tree, offset, num_dg);
1495 
1496 		/* FNOM and CFGCNT */
1497 		proto_tree_add_item(station_tree, hf_conf_fnom,	  tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
1498 		proto_tree_add_item(station_tree, hf_conf_cfgcnt, tvb, offset, 2, ENC_BIG_ENDIAN); offset += 2;
1499 
1500 		/* set the correct length for the "Station :" item */
1501 		proto_item_set_len(station_item, offset - oldoffset);
1502 	} /* for() PMU blocks */
1503 
1504 	/* DATA_RATE */
1505 	{
1506 		gint16 tmp = tvb_get_ntohis(tvb, offset);
1507 		if (tmp > 0)
1508 			proto_tree_add_int_format_value(config_tree, hf_synphasor_rate_of_transmission, tvb, offset, 2, tmp,
1509                         "%d frame(s) per second", tmp);
1510 		else
1511 			proto_tree_add_int_format_value(config_tree, hf_synphasor_rate_of_transmission, tvb, offset, 2, tmp,
1512                         "1 frame per %d second(s)", (gint16)-tmp);
1513 		offset += 2;
1514 	}
1515 
1516 	return offset;
1517 } /* dissect_config_frame() */
1518 
1519 /* dissects a configuration frame type 3 and adds fields to 'config_item' */
dissect_config_3_frame(tvbuff_t * tvb,proto_item * config_item)1520 static int dissect_config_3_frame(tvbuff_t *tvb, proto_item *config_item)
1521 {
1522 	proto_tree *config_tree, *wgs84_tree;
1523 	gint	    offset = 0;
1524 	guint16	    num_pmu, j;
1525 
1526 	proto_item_set_text(config_item, "Configuration data");
1527 	config_tree = proto_item_add_subtree(config_item, ett_conf);
1528 
1529 	/* CONT_IDX */
1530 	proto_tree_add_item(config_tree, hf_cont_idx, tvb, offset, 2, ENC_BIG_ENDIAN);
1531 	offset += 2;
1532 
1533 	/* TIME_BASE and NUM_PMU */
1534 	offset += 1; /* skip the reserved byte */
1535 
1536 	proto_tree_add_item(config_tree, hf_conf_timebase, tvb, offset, 3, ENC_BIG_ENDIAN);
1537 	offset += 3;
1538 
1539 	proto_tree_add_item(config_tree, hf_conf_numpmu,   tvb, offset, 2, ENC_BIG_ENDIAN);
1540 
1541 	/* add number of included PMUs to the text in the list view  */
1542 	num_pmu = tvb_get_ntohs(tvb, offset);
1543 	offset += 2;
1544 
1545 	proto_item_append_text(config_item, ", %"G_GUINT16_FORMAT" PMU(s) included", num_pmu);
1546 
1547 	/* dissect the repeating PMU blocks */
1548 	for (j = 0; j < num_pmu; j++) {
1549 		guint16    num_ph, num_an, num_dg, i;
1550 		guint8     name_length;
1551 		gint       oldoffset;
1552 		gfloat     pmu_lat, pmu_long, pmu_elev;
1553 		proto_item *station_item;
1554 		proto_tree *station_tree;
1555 		proto_tree *temp_tree;
1556 		char       *str, *service_class;
1557 		char       *unspecified_location = "Unspecified Location";
1558 		guint8     g_pmu_id_array[G_PMU_ID_LEN];
1559 
1560 		oldoffset = offset; /* to calculate the length of the whole PMU block later */
1561 
1562 		/* STN with new tree to add the rest of the PMU block */
1563 		name_length = get_name_length(tvb, offset);
1564 		str = (char *)tvb_get_string_enc(wmem_packet_scope(), tvb, offset + 1, name_length, ENC_ASCII);
1565 		station_tree = proto_tree_add_subtree_format(config_tree, tvb, offset, name_length + 1,
1566 							     ett_conf_station, &station_item,
1567 							     "Station #%i: \"%s\"", j + 1, str);
1568 
1569 		/* Station Name Length */
1570 		proto_tree_add_item(station_tree, hf_station_name_len, tvb, offset, 1, ENC_BIG_ENDIAN);
1571 		offset += 1;
1572 
1573 		/* Station Name */
1574 		proto_tree_add_string(station_tree, hf_station_name, tvb, offset, 1, str);
1575 		offset += name_length;
1576 
1577 		/* IDCODE */
1578 		proto_tree_add_item(station_tree, hf_idcode_data_source, tvb, offset, 2, ENC_BIG_ENDIAN);
1579 		offset += 2;
1580 
1581 		/* G_PMU_ID */
1582 		/* A 128 bit display as raw bytes */
1583 		for (i = 0; i < G_PMU_ID_LEN; i++) {
1584 			g_pmu_id_array[i] = tvb_get_guint8(tvb, offset + i);
1585 		}
1586 
1587 		proto_tree_add_bytes_format(station_tree, hf_g_pmu_id, tvb, offset, G_PMU_ID_LEN, 0,
1588 					    "Global PMU ID (raw bytes): %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1589 					    g_pmu_id_array[0], g_pmu_id_array[1], g_pmu_id_array[2], g_pmu_id_array[3],
1590 					    g_pmu_id_array[4], g_pmu_id_array[5], g_pmu_id_array[6], g_pmu_id_array[7],
1591 					    g_pmu_id_array[8], g_pmu_id_array[9], g_pmu_id_array[10], g_pmu_id_array[11],
1592 					    g_pmu_id_array[12], g_pmu_id_array[13], g_pmu_id_array[14], g_pmu_id_array[15]);
1593 		offset += G_PMU_ID_LEN;
1594 
1595 		/* FORMAT */
1596 		temp_tree = proto_tree_add_subtree(station_tree, tvb, offset, 2, ett_conf_format, NULL,
1597 						   "Data format in data frame");
1598 		proto_tree_add_item(temp_tree, hf_conf_formatb3, tvb, offset, 2, ENC_BIG_ENDIAN);
1599 		proto_tree_add_item(temp_tree, hf_conf_formatb2, tvb, offset, 2, ENC_BIG_ENDIAN);
1600 		proto_tree_add_item(temp_tree, hf_conf_formatb1, tvb, offset, 2, ENC_BIG_ENDIAN);
1601 		proto_tree_add_item(temp_tree, hf_conf_formatb0, tvb, offset, 2, ENC_BIG_ENDIAN);
1602 		offset += 2;
1603 
1604 		/* PHNMR, ANNMR, DGNMR */
1605 		num_ph = tvb_get_ntohs(tvb, offset    );
1606 		num_an = tvb_get_ntohs(tvb, offset + 2);
1607 		num_dg = tvb_get_ntohs(tvb, offset + 4);
1608 		proto_tree_add_uint(station_tree, hf_synphasor_num_phasors, tvb, offset, 2, num_ph);
1609 		proto_tree_add_uint(station_tree, hf_synphasor_num_analog_values, tvb, offset + 2, 2, num_an);
1610 		proto_tree_add_uint(station_tree, hf_synphasor_num_digital_status_words, tvb, offset + 4, 2, num_dg);
1611 		offset += 6;
1612 
1613 		/* CHNAM, the channel names */
1614 		offset = dissect_config_3_CHNAM(tvb, station_tree, offset, num_ph, "Phasor name");
1615 		offset = dissect_config_3_CHNAM(tvb, station_tree, offset, num_an, "Analog value");
1616 		offset = dissect_config_3_CHNAM(tvb, station_tree, offset, num_dg * 16, "Digital label");
1617 
1618 		/* PHUNIT, ANUINT and DIGUNIT */
1619 		offset = dissect_PHSCALE(tvb, station_tree, offset, num_ph);
1620 		offset = dissect_ANSCALE(tvb, station_tree, offset, num_an);
1621 
1622 		offset = dissect_DIGUNIT(tvb, station_tree, offset, num_dg);
1623 
1624 		/* subtree for coordinate info*/
1625 		wgs84_tree = proto_tree_add_subtree_format(station_tree, tvb, offset, 12, ett_conf_wgs84, NULL,
1626 							   "World Geodetic System 84 data");
1627 
1628 		/* preview latitude, longitude, and elevation values */
1629 		/* INFINITY is an unspecified location, otherwise use the actual float value */
1630 		pmu_lat = tvb_get_ntohieee_float(tvb, offset);
1631 		pmu_long = tvb_get_ntohieee_float(tvb, offset + 4);
1632 		pmu_elev = tvb_get_ntohieee_float(tvb, offset + 8);
1633 
1634 		/* PMU_LAT */
1635 		if ((isinf(pmu_lat) == 1) || (isinf(pmu_lat) == -1)) {
1636 			proto_tree_add_float_format_value(wgs84_tree, hf_conf_pmu_lat_unknown, tvb, offset,
1637 					      4, INFINITY, "%s", unspecified_location);
1638 		}
1639 		else {
1640 			proto_tree_add_item(wgs84_tree, hf_conf_pmu_lat, tvb, offset, 4, ENC_BIG_ENDIAN);
1641 		}
1642 		offset += 4;
1643 
1644 		/* PMU_LON */
1645 		if ((isinf(pmu_long) == 1) || (isinf(pmu_long) == -1)) {
1646 			proto_tree_add_float_format_value(wgs84_tree, hf_conf_pmu_lon_unknown, tvb, offset,
1647 					      4, INFINITY, "%s", unspecified_location);
1648 		}
1649 		else {
1650 			proto_tree_add_item(wgs84_tree, hf_conf_pmu_lon, tvb, offset, 4, ENC_BIG_ENDIAN);
1651 		}
1652 		offset += 4;
1653 
1654 		/* PMU_ELEV */
1655 		if ((isinf(pmu_elev) == 1) || (isinf(pmu_elev) == -1)) {
1656 			proto_tree_add_float_format_value(wgs84_tree, hf_conf_pmu_elev_unknown, tvb, offset,
1657 					      4, INFINITY, "%s", unspecified_location);
1658 		}
1659 		else {
1660 			proto_tree_add_item(wgs84_tree, hf_conf_pmu_elev, tvb, offset, 4, ENC_BIG_ENDIAN);
1661 		}
1662 		offset += 4;
1663 
1664 		/* SVC_CLASS */
1665 		service_class = (char *)tvb_get_string_enc(wmem_packet_scope(), tvb, offset, 1, ENC_ASCII);
1666 		if ((strcmp(service_class, "P") == 0) || (strcmp(service_class, "p") == 0)) {
1667 			proto_tree_add_string(station_tree, hf_conf_svc_class, tvb, offset, 1, "Protection");
1668 		}
1669 		else if ((strcmp(service_class, "M") == 0) || (strcmp(service_class, "m") == 0)) {
1670 			proto_tree_add_string(station_tree, hf_conf_svc_class, tvb, offset, 1, "Monitoring");
1671 		}
1672 		else {
1673 			proto_tree_add_string(station_tree, hf_conf_svc_class, tvb, offset, 1, "Unknown");
1674 		}
1675 		offset += 1;
1676 
1677 		/* WINDOW */
1678 		proto_tree_add_item(station_tree, hf_conf_window, tvb, offset, 4, ENC_BIG_ENDIAN);
1679 		offset += 4;
1680 
1681 		/*GRP_DLY */
1682 		proto_tree_add_item(station_tree, hf_conf_grp_dly, tvb, offset, 4, ENC_BIG_ENDIAN);
1683 		offset += 4;
1684 
1685 		/* FNOM and CFGCNT */
1686 		proto_tree_add_item(station_tree, hf_conf_fnom,	  tvb, offset, 2, ENC_BIG_ENDIAN);
1687 		offset += 2;
1688 
1689 		proto_tree_add_item(station_tree, hf_conf_cfgcnt, tvb, offset, 2, ENC_BIG_ENDIAN);
1690 		offset += 2;
1691 
1692 		/* set the correct length for the "Station :" item */
1693 		proto_item_set_len(station_item, offset - oldoffset);
1694 	} /* for() PMU blocks */
1695 
1696 	/* DATA_RATE */
1697 	{
1698 		gint16 tmp = tvb_get_ntohis(tvb, offset);
1699 		if (tmp > 0) {
1700 			proto_tree_add_int_format_value(config_tree, hf_synphasor_rate_of_transmission, tvb, offset, 2, tmp,
1701 							"%d frame(s) per second", tmp);
1702 		}
1703 		else {
1704 			proto_tree_add_int_format_value(config_tree, hf_synphasor_rate_of_transmission, tvb, offset, 2, tmp,
1705 							"1 frame per %d second(s)", (gint16)-tmp);
1706 		}
1707 		offset += 2;
1708 	}
1709 
1710 	return offset;
1711 } /* dissect_config_3_frame() */
1712 
1713 /* calculates the size (in bytes) of a data frame that the config_block describes */
1714 #define SYNP_BLOCKSIZE(x) (2							   /* STAT    */ \
1715 		   + wmem_array_get_count((x).phasors) * (integer == (x).format_ph ? 4 : 8) /* PHASORS */ \
1716 		   +			                 (integer == (x).format_fr ? 4 : 8) /* (D)FREQ */ \
1717 		   + wmem_array_get_count((x).analogs) * (integer == (x).format_an ? 2 : 4) /* ANALOG  */ \
1718 		   + (x).num_dg * 2)					   /* DIGITAL */
1719 
1720 /* Dissects a data frame */
dissect_data_frame(tvbuff_t * tvb,proto_item * data_item,packet_info * pinfo)1721 static int dissect_data_frame(tvbuff_t	  *tvb,
1722 			      proto_item  *data_item, /* all items are placed beneath this item	  */
1723 			      packet_info *pinfo)     /* used to find the data from a CFG-2 or CFG-3 frame */
1724 {
1725 	proto_tree	*data_tree;
1726 	gint		offset	 = 0;
1727 	guint		i;
1728 	config_frame	*conf;
1729 
1730 	proto_item_set_text(data_item, "Measurement data");
1731 	data_tree = proto_item_add_subtree(data_item, ett_data);
1732 
1733 	/* search for configuration information to dissect the frame */
1734 	{
1735 		gboolean config_found = FALSE;
1736 		conf = (config_frame *)p_get_proto_data(wmem_file_scope(), pinfo, proto_synphasor, 0);
1737 
1738 		if (conf) {
1739 			/* check if the size of the current frame is the
1740 			   size of the frame the config_frame describes */
1741 			size_t reported_size = 0;
1742 			for (i = 0; i < wmem_array_get_count(conf->config_blocks); i++) {
1743 				config_block *block = (config_block*)wmem_array_index(conf->config_blocks, i);
1744 				reported_size += SYNP_BLOCKSIZE(*block);
1745 			}
1746 
1747 			if (tvb_reported_length(tvb) == reported_size) {
1748 				// Add link to CFG Frame
1749 				proto_item* item = proto_tree_add_uint(data_tree, hf_cfg_frame_num, tvb, 0,0, conf->fnum);
1750 				proto_item_set_generated(item);
1751 				config_found = TRUE;
1752 			}
1753 		}
1754 
1755 		if (!config_found) {
1756 			proto_item_append_text(data_item, ", no configuration frame found");
1757 			return 0;
1758 		}
1759 	}
1760 
1761 	/* dissect a PMU block for every config_block in the frame */
1762 	for (i = 0; i < wmem_array_get_count(conf->config_blocks); i++) {
1763 		config_block *block = (config_block*)wmem_array_index(conf->config_blocks, i);
1764 
1765 		proto_tree *block_tree = proto_tree_add_subtree_format(data_tree, tvb, offset, SYNP_BLOCKSIZE(*block),
1766 								       ett_data_block, NULL,
1767 								       "Station: \"%s\"", block->name);
1768 
1769 		/* STAT */
1770 		proto_tree *temp_tree = proto_tree_add_subtree(block_tree, tvb, offset, 2, ett_data_stat, NULL, "Flags");
1771 
1772 		proto_item *temp_item = proto_tree_add_item(temp_tree, hf_data_statb15to14, tvb, offset, 2, ENC_BIG_ENDIAN);
1773 		guint16 flag_bits = tvb_get_guint16(tvb, offset, ENC_BIG_ENDIAN)  >> 14; // Get bits 15-14
1774 		if (flag_bits != 0) {
1775 			expert_add_info(pinfo, temp_item, &ei_synphasor_data_error);
1776 		}
1777 		temp_item = proto_tree_add_item(temp_tree, hf_data_statb13,	    tvb, offset, 2, ENC_BIG_ENDIAN);
1778 		flag_bits = tvb_get_guint16(tvb, offset, ENC_BIG_ENDIAN); // Get flag bits
1779 		if ((flag_bits >> 13)&1) { // Check 13 bit
1780 			expert_add_info(pinfo, temp_item, &ei_synphasor_pmu_not_sync);
1781 		}
1782 		proto_tree_add_item(temp_tree, hf_data_statb12,	    tvb, offset, 2, ENC_BIG_ENDIAN);
1783 		proto_tree_add_item(temp_tree, hf_data_statb11,	    tvb, offset, 2, ENC_BIG_ENDIAN);
1784 		proto_tree_add_item(temp_tree, hf_data_statb10,	    tvb, offset, 2, ENC_BIG_ENDIAN);
1785 		proto_tree_add_item(temp_tree, hf_data_statb09,	    tvb, offset, 2, ENC_BIG_ENDIAN);
1786 		proto_tree_add_item(temp_tree, hf_data_statb08to06, tvb, offset, 2, ENC_BIG_ENDIAN);
1787 		proto_tree_add_item(temp_tree, hf_data_statb05to04, tvb, offset, 2, ENC_BIG_ENDIAN);
1788 		proto_tree_add_item(temp_tree, hf_data_statb03to00, tvb, offset, 2, ENC_BIG_ENDIAN);
1789 		offset += 2;
1790 
1791 		/* PHASORS, (D)FREQ, ANALOG, and DIGITAL */
1792 		offset = dissect_PHASORS(tvb, block_tree, block, offset);
1793 		offset = dissect_DFREQ	(tvb, block_tree, block, offset);
1794 		offset = dissect_ANALOG (tvb, block_tree, block, offset);
1795 		offset = dissect_DIGITAL(tvb, block_tree, block, offset);
1796 	}
1797 	return offset;
1798 } /* dissect_data_frame() */
1799 
1800 /* Dissects a command frame and adds fields to config_item.
1801  *
1802  * 'pinfo' is used to add the type of command
1803  * to the INFO column in the packet list.
1804  */
dissect_command_frame(tvbuff_t * tvb,proto_item * command_item,packet_info * pinfo)1805 static int dissect_command_frame(tvbuff_t    *tvb,
1806 				 proto_item  *command_item,
1807 				 packet_info *pinfo)
1808 {
1809 	proto_tree *command_tree;
1810 	guint	    tvbsize	  = tvb_reported_length(tvb);
1811 	const char *s;
1812 
1813 	proto_item_set_text(command_item, "Command data");
1814 	command_tree = proto_item_add_subtree(command_item, ett_command);
1815 
1816 	/* CMD */
1817 	proto_tree_add_item(command_tree, hf_command, tvb, 0, 2, ENC_BIG_ENDIAN);
1818 
1819 	s = rval_to_str_const(tvb_get_ntohs(tvb, 0), command_names, "invalid command");
1820 	col_append_str(pinfo->cinfo, COL_INFO, ", ");
1821 	col_append_str(pinfo->cinfo, COL_INFO, s);
1822 
1823 	if (tvbsize > 2) {
1824 		if (tvb_get_ntohs(tvb, 0) == 0x0008) {
1825 			/* Command: Extended Frame, the extra data is ok */
1826 			proto_item *ti = proto_tree_add_item(command_tree, hf_synphasor_extended_frame_data, tvb, 2, tvbsize - 2, ENC_NA);
1827 			if (tvbsize % 2)
1828 				expert_add_info(pinfo, ti, &ei_synphasor_extended_frame_data);
1829 		}
1830 		else
1831 			proto_tree_add_item(command_tree, hf_synphasor_unknown_data, tvb, 2, tvbsize - 2, ENC_NA);
1832 	}
1833 
1834 	return tvbsize;
1835 } /* dissect_command_frame() */
1836 
1837 /* Dissects the header (common to all types of frames) and then calls
1838  * one of the subdissectors (declared above) for the rest of the frame.
1839  */
dissect_common(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)1840 static int dissect_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
1841 {
1842 	guint8	frame_type;
1843 	guint16 crc;
1844 	guint	tvbsize = tvb_reported_length(tvb);
1845 
1846 	/* some heuristics */
1847 	if (tvbsize < 17		    /* 17 bytes = header frame with only a
1848 					       NULL character, useless but valid */
1849 	 || tvb_get_guint8(tvb, 0) != 0xAA) /* every synchrophasor frame starts with 0xAA */
1850 		return 0;
1851 
1852 	/* write the protocol name to the info column */
1853 	col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTOCOL_SHORT_NAME);
1854 
1855 	frame_type = tvb_get_guint8(tvb, 1) >> 4;
1856 
1857 	col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str_const(frame_type, typenames, "invalid packet type"));
1858 
1859 	/* CFG-2, CFG3, and DATA frames need special treatment during the first run:
1860 	 * For CFG-2 & CFG-3 frames, a 'config_frame' struct is created to hold the
1861 	 * information necessary to decode DATA frames. A pointer to this
1862 	 * struct is saved in the conversation and is copied to the
1863 	 * per-packet information if a DATA frame is dissected.
1864 	 */
1865 	if (!pinfo->fd->visited) {
1866 		if (CFG2 == frame_type &&
1867 		    check_crc(tvb, &crc)) {
1868 			conversation_t *conversation;
1869 
1870 			/* fill the config_frame */
1871 			config_frame *frame = config_frame_fast(tvb);
1872 			frame->fnum = pinfo->num;
1873 
1874 			/* find a conversation, create a new one if none exists */
1875 			conversation = find_or_create_conversation(pinfo);
1876 
1877 			/* remove data from a previous CFG-2 frame, only
1878 			 * the most recent configuration frame is relevant */
1879 			if (conversation_get_proto_data(conversation, proto_synphasor))
1880 				conversation_delete_proto_data(conversation, proto_synphasor);
1881 
1882 			conversation_add_proto_data(conversation, proto_synphasor, frame);
1883 		}
1884 		else if ((CFG3 == frame_type) && check_crc(tvb, &crc)) {
1885 			conversation_t *conversation;
1886 			config_frame *frame;
1887 
1888 			/* fill the config_frame */
1889 			frame = config_3_frame_fast(tvb);
1890 			frame->fnum = pinfo->num;
1891 
1892 			/* find a conversation, create a new one if none exists */
1893 			conversation = find_or_create_conversation(pinfo);
1894 
1895 			/* remove data from a previous CFG-3 frame, only
1896 			 * the most recent configuration frame is relevant */
1897 			if (conversation_get_proto_data(conversation, proto_synphasor)) {
1898 				conversation_delete_proto_data(conversation, proto_synphasor);
1899 			}
1900 
1901 			conversation_add_proto_data(conversation, proto_synphasor, frame);
1902 		}
1903 		// Add conf to any frame for dissection fracsec
1904 		conversation_t *conversation = find_conversation_pinfo(pinfo, 0);
1905 		if (conversation) {
1906 			config_frame *conf = (config_frame *)conversation_get_proto_data(conversation, proto_synphasor);
1907 			/* no problem if 'conf' is NULL, the frame dissector checks this again */
1908 		p_add_proto_data(wmem_file_scope(), pinfo, proto_synphasor, 0, conf);
1909 		}
1910 	} /* if (!visited) */
1911 
1912 	{
1913 		proto_tree *synphasor_tree;
1914 		proto_item *temp_item;
1915 		proto_item *sub_item;
1916 
1917 		gint	    offset;
1918 		guint16	    framesize;
1919 		tvbuff_t   *sub_tvb;
1920 		gboolean   crc_good;
1921 
1922 		temp_item = proto_tree_add_item(tree, proto_synphasor, tvb, 0, -1, ENC_NA);
1923 		proto_item_append_text(temp_item, ", %s", val_to_str_const(frame_type, typenames,
1924 									   ", invalid packet type"));
1925 
1926 		/* synphasor_tree is where from now on all new elements for this protocol get added */
1927 		synphasor_tree = proto_item_add_subtree(temp_item, ett_synphasor);
1928 		// Add pinfo for dissection fracsec
1929 		framesize = dissect_header(tvb, synphasor_tree, pinfo);
1930 		offset = 14; /* header is 14 bytes long */
1931 
1932 		/* check CRC, call appropriate subdissector for the rest of the frame if CRC is correct*/
1933 		sub_item  = proto_tree_add_item(synphasor_tree, hf_synphasor_data, tvb, offset, tvbsize - 16, ENC_NA);
1934 		crc_good = check_crc(tvb, &crc);
1935 		proto_tree_add_checksum(synphasor_tree, tvb, tvbsize - 2, hf_synphasor_checksum, hf_synphasor_checksum_status, &ei_synphasor_checksum,
1936 								pinfo, crc16_x25_ccitt_tvb(tvb, tvb_get_ntohs(tvb, 2) - 2), ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);
1937 		if (!crc_good) {
1938 			proto_item_append_text(sub_item,  ", not dissected because of wrong checksum");
1939 		}
1940 		else {
1941 			/* create a new tvb to pass to the subdissector
1942 			   '-16': length of header + 2 CRC bytes */
1943 			sub_tvb = tvb_new_subset_length_caplen(tvb, offset, tvbsize - 16, framesize - 16);
1944 
1945 			/* call subdissector */
1946 			switch (frame_type) {
1947 				case DATA:
1948 					dissect_data_frame(sub_tvb, sub_item, pinfo);
1949 					break;
1950 				case HEADER: /* no further dissection is done/needed */
1951 					proto_item_append_text(sub_item, "Header Frame");
1952 					break;
1953 				case CFG1:
1954 				case CFG2:
1955 					dissect_config_frame(sub_tvb, sub_item);
1956 					break;
1957 				case CMD:
1958 					dissect_command_frame(sub_tvb, sub_item, pinfo);
1959 					break;
1960 				case CFG3:
1961 					/* Note:  The C37.118-2.2001 stanadard is vague on how to handle fragmented frames.
1962 						  Until further clarification is given, fragmented frames with the CONT_IDX
1963 						  are not supported. */
1964 					if (tvb_get_guint16(tvb, offset, ENC_BIG_ENDIAN) != 0) {
1965 						proto_item_append_text(sub_item, ", CFG-3 Fragmented Frame (Not Supported)");
1966 					}
1967 					else {
1968 						dissect_config_3_frame(sub_tvb, sub_item);
1969 					}
1970 					break;
1971 				default:
1972 					proto_item_append_text(sub_item, " of unknown type");
1973 			}
1974 			proto_item_append_text(temp_item, " [correct]");
1975 		}
1976 
1977 		/* remaining 2 bytes are the CRC */
1978 	}
1979 
1980 	return tvb_reported_length(tvb);
1981 } /* dissect_common() */
1982 
1983 /* called for synchrophasors over UDP */
dissect_udp(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)1984 static int dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1985 {
1986 	return dissect_common(tvb, pinfo, tree, data);
1987 }
1988 
1989 /* callback for 'tcp_dissect_pdus()' to give it the length of the frame */
get_pdu_length(packet_info * pinfo _U_,tvbuff_t * tvb,int offset,void * data _U_)1990 static guint get_pdu_length(packet_info *pinfo _U_, tvbuff_t *tvb,
1991                             int offset, void *data _U_)
1992 {
1993 	return tvb_get_ntohs(tvb, offset + 2);
1994 }
1995 
dissect_tcp(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)1996 static int dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1997 {
1998 	tcp_dissect_pdus(tvb, pinfo, tree, TRUE, 4, get_pdu_length, dissect_common, data);
1999 
2000 	return tvb_reported_length(tvb);
2001 }
2002 
2003 /*******************************************************************/
2004 /* after this line:  Wireshark Register Routines                   */
2005 /*******************************************************************/
2006 
2007 /* Register Synchrophasor Protocol with Wireshark*/
proto_register_synphasor(void)2008 void proto_register_synphasor(void)
2009 {
2010 	static hf_register_info hf[] = {
2011 		/* Sync word */
2012 		{ &hf_sync,
2013 		{ "Synchronization word", "synphasor.sync", FT_UINT16, BASE_HEX,
2014 		  NULL, 0x0, NULL, HFILL }},
2015 
2016 		/* Flags in the Sync word */
2017 			{ &hf_sync_frtype,
2018 			{ "Frame Type", "synphasor.frtype", FT_UINT16, BASE_HEX,
2019 			   VALS(typenames), 0x0070, NULL, HFILL }},
2020 
2021 			{ &hf_sync_version,
2022 			{ "Version",	"synphasor.version", FT_UINT16, BASE_DEC,
2023 			  VALS(versionnames), 0x000F, NULL, HFILL }},
2024 
2025 		{ &hf_frsize,
2026 		{ "Framesize", "synphasor.frsize", FT_UINT16, BASE_DEC | BASE_UNIT_STRING,
2027 		  &units_byte_bytes, 0x0, NULL, HFILL }},
2028 
2029 		{ &hf_station_name_len,
2030 		{ "Station name length", "synphasor.station_name_len", FT_UINT8,
2031 		  BASE_DEC | BASE_UNIT_STRING, &units_byte_bytes, 0x0, NULL, HFILL }},
2032 
2033 		{ &hf_station_name,
2034 		{ "Station name", "synphasor.station_name", FT_STRING, BASE_NONE,
2035 		  NULL, 0x0, NULL, HFILL }},
2036 
2037 		{ &hf_idcode_stream_source,
2038 		{ "PMU/DC ID number (Stream source ID)", "synphasor.idcode_stream_source", FT_UINT16, BASE_DEC,
2039 		  NULL, 0x0, NULL, HFILL }},
2040 
2041 		{ &hf_idcode_data_source,
2042 		{ "PMU/DC ID number (Data source ID)", "synphasor.idcode_data_source", FT_UINT16, BASE_DEC,
2043 		  NULL, 0x0, NULL, HFILL }},
2044 
2045 		{ &hf_g_pmu_id,
2046 		{ "Global PMU ID (raw hex bytes)", "synphasor.gpmuid", FT_BYTES, BASE_NONE,
2047 		  NULL, 0x0, NULL, HFILL }},
2048 
2049 		{ &hf_soc,
2050 		{ "SOC time stamp", "synphasor.soc", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC,
2051 		  NULL, 0x0, NULL, HFILL }},
2052 
2053 		/* Time quality flags in fracsec */
2054 		{ &hf_timeqal_lsdir,
2055 		{ "Leap second direction", "synphasor.timeqal.lsdir", FT_BOOLEAN, 8,
2056 		  TFS(&leapseconddir), 0x40, NULL, HFILL }},
2057 
2058 		{ &hf_timeqal_lsocc,
2059 		{ "Leap second occurred", "synphasor.timeqal.lsocc", FT_BOOLEAN, 8,
2060 		  NULL, 0x20, NULL, HFILL }},
2061 
2062 		{ &hf_timeqal_lspend,
2063 		{ "Leap second pending", "synphasor.timeqal.lspend", FT_BOOLEAN, 8,
2064 		  NULL, 0x10, NULL, HFILL }},
2065 
2066 		{ &hf_timeqal_timequalindic,
2067 		{ "Message Time Quality indicator code", "synphasor.timeqal.timequalindic", FT_UINT8, BASE_HEX,
2068 		  VALS(timequalcodes), 0x0F, NULL, HFILL }},
2069 
2070 		/* Fraction of second */
2071 		{ &hf_fracsec_raw,
2072 		{ "Fraction of second (raw)", "synphasor.fracsec_raw", FT_UINT24, BASE_DEC,
2073 		  NULL, 0x0, NULL, HFILL }},
2074 
2075 		{ &hf_fracsec_ms,
2076 		{ "Fraction of second", "synphasor.fracsec_ms", FT_FLOAT, BASE_NONE | BASE_UNIT_STRING,
2077 		  &units_millisecond_milliseconds, 0x0, NULL, HFILL }},
2078 
2079 	/* Data types for configuration frames */
2080 		{ &hf_cont_idx,
2081 		{ "Continuation index", "synphasor.conf.contindx", FT_UINT16, BASE_DEC,
2082 		  NULL, 0x0, NULL, HFILL }},
2083 
2084 		{ &hf_conf_timebase,
2085 		{ "Resolution of fractional second time stamp", "synphasor.conf.timebase", FT_UINT24, BASE_DEC,
2086 		  NULL, 0x0, NULL, HFILL }},
2087 
2088 		{ &hf_conf_numpmu,
2089 		{ "Number of PMU blocks included in the frame", "synphasor.conf.numpmu", FT_UINT16, BASE_DEC,
2090 		  NULL, 0x0, NULL, HFILL }},
2091 
2092 		/* Bits in the FORMAT word */
2093 		{ &hf_conf_formatb3,
2094 		{ "FREQ/DFREQ format", "synphasor.conf.dfreq_format", FT_BOOLEAN, 16,
2095 		  TFS(&conf_formatb123names), 0x8, NULL, HFILL }},
2096 
2097 		{ &hf_conf_formatb2,
2098 		{ "Analog values format", "synphasor.conf.analog_format", FT_BOOLEAN, 16,
2099 		  TFS(&conf_formatb123names), 0x4, NULL, HFILL }},
2100 
2101 		{ &hf_conf_formatb1,
2102 		{ "Phasor format", "synphasor.conf.phasor_format", FT_BOOLEAN, 16,
2103 		  TFS(&conf_formatb123names), 0x2, NULL, HFILL }},
2104 
2105 		{ &hf_conf_formatb0,
2106 		{ "Phasor notation", "synphasor.conf.phasor_notation", FT_BOOLEAN, 16,
2107 		  TFS(&conf_formatb0names), 0x1, NULL, HFILL }},
2108 
2109 		{ &hf_conf_chnam_len,
2110 		{ "Channel name length", "synphasor.conf.chnam_len", FT_UINT8,
2111 		  BASE_DEC | BASE_UNIT_STRING, &units_byte_bytes, 0x0, NULL, HFILL }},
2112 
2113 		{ &hf_conf_chnam,
2114 		{ "Channel name", "synphasor.conf.chnam", FT_STRING, BASE_NONE,
2115 		  NULL, 0x0, NULL, HFILL }},
2116 
2117 		{ &hf_conf_phasor_mod_b15,
2118 		{ "Modification", "synphasor.conf.phasor_mod.type_not_def", FT_BOOLEAN, 16,
2119 		  TFS(&conf_phasor_mod_b15), 0x8000, NULL, HFILL }},
2120 
2121 		{ &hf_conf_phasor_mod_b10,
2122 		{ "Modification", "synphasor.conf.phasor_mod.pseudo_phasor", FT_BOOLEAN, 16,
2123 		  TFS(&conf_phasor_mod_b10), 0x0400, NULL, HFILL }},
2124 
2125 		{ &hf_conf_phasor_mod_b09,
2126 		{ "Modification", "synphasor.conf.phasor_mod.phase_rotation", FT_BOOLEAN, 16,
2127 		  TFS(&conf_phasor_mod_b09), 0x0200, NULL, HFILL }},
2128 
2129 		{ &hf_conf_phasor_mod_b08,
2130 		{ "Modification", "synphasor.conf.phasor_mod.phase_calibration", FT_BOOLEAN, 16,
2131 		  TFS(&conf_phasor_mod_b08), 0x0100, NULL, HFILL }},
2132 
2133 		{ &hf_conf_phasor_mod_b07,
2134 		{ "Modification", "synphasor.conf.phasor_mod.mag_calibration", FT_BOOLEAN, 16,
2135 		  TFS(&conf_phasor_mod_b07), 0x0080, NULL, HFILL }},
2136 
2137 		{ &hf_conf_phasor_mod_b06,
2138 		{ "Modification", "synphasor.conf.phasor_mod.filtered", FT_BOOLEAN, 16,
2139 		  TFS(&conf_phasor_mod_b06), 0x0040, NULL, HFILL }},
2140 
2141 		{ &hf_conf_phasor_mod_b05,
2142 		{ "Modification", "synphasor.conf.phasor_mod.downsampled", FT_BOOLEAN, 16,
2143 		  TFS(&conf_phasor_mod_b05), 0x0020, NULL, HFILL }},
2144 
2145 		{ &hf_conf_phasor_mod_b04,
2146 		{ "Modification", "synphasor.conf.phasor_mod.downsampled_fir", FT_BOOLEAN, 16,
2147 		  TFS(&conf_phasor_mod_b04), 0x0010, NULL, HFILL }},
2148 
2149 		{ &hf_conf_phasor_mod_b03,
2150 		{ "Modification", "synphasor.conf.phasor_mod.downsampled_reselect", FT_BOOLEAN, 16,
2151 		  TFS(&conf_phasor_mod_b03), 0x0008, NULL, HFILL }},
2152 
2153 		{ &hf_conf_phasor_mod_b02,
2154 		{ "Modification", "synphasor.conf.phasor_mod.upsampled_extrapolation", FT_BOOLEAN, 16,
2155 		  TFS(&conf_phasor_mod_b02), 0x0004, NULL, HFILL }},
2156 
2157 		{ &hf_conf_phasor_mod_b01,
2158 		{ "Modification", "synphasor.conf.phasor_mod.upsampled_interpolation", FT_BOOLEAN, 16,
2159 		  TFS(&conf_phasor_mod_b01), 0x0002, NULL, HFILL }},
2160 
2161 		{ &hf_conf_phasor_type_b03,
2162 		{ "Phasor Type", "synphasor.conf.phasor_type", FT_BOOLEAN, 8,
2163 		  TFS(&conf_phasor_type_b03), 0x8, NULL, HFILL }},
2164 
2165 		{ &hf_conf_phasor_type_b02to00,
2166 		{ "Phasor Type", "synphasor.conf.phasor_component", FT_UINT8, BASE_HEX,
2167 		  VALS(conf_phasor_type_b02to00), 0x7, NULL, HFILL }},
2168 
2169 		{ &hf_conf_phasor_user_data,
2170 		{ "Binary format", "synphasor.conf.phasor_user_flags", FT_BOOLEAN, 8,
2171 		  TFS(&conf_phasor_user_defined), 0x00ff, NULL, HFILL }},
2172 
2173 		{ &hf_conf_phasor_scale_factor,
2174 		{ "Phasor scale factor", "synphasor.conf.phasor_scale_factor", FT_FLOAT,
2175 		  BASE_NONE, NULL, 0x0, NULL, HFILL }},
2176 
2177 		{ &hf_conf_phasor_angle_offset,
2178 		{ "Phasor angle offset", "synphasor.conf.phasor_angle_offset", FT_FLOAT,
2179 		  BASE_NONE | BASE_UNIT_STRING, &units_degree_degrees, 0x0, NULL, HFILL }},
2180 
2181 		{ &hf_conf_analog_scale_factor,
2182 		{ "Analog scale factor", "synphasor.conf.analog_scale_factor", FT_FLOAT,
2183 		  BASE_NONE, NULL, 0x0, NULL, HFILL }},
2184 
2185 		{ &hf_conf_analog_offset,
2186 		{ "Analog offset", "synphasor.conf.analog_offset", FT_FLOAT,
2187 		  BASE_NONE, NULL, 0x0, NULL, HFILL }},
2188 
2189 		{ &hf_conf_pmu_lat,
2190 		{ "PMU Latitude", "synphasor.conf.pmu_latitude", FT_FLOAT,
2191 		  BASE_NONE | BASE_UNIT_STRING, &units_degree_degrees, 0x0, NULL, HFILL }},
2192 
2193 		{ &hf_conf_pmu_lon,
2194 		{ "PMU Longitude", "synphasor.conf.pmu_longitude", FT_FLOAT,
2195 		  BASE_NONE | BASE_UNIT_STRING, &units_degree_degrees, 0x0, NULL, HFILL }},
2196 
2197 		{ &hf_conf_pmu_elev,
2198 		{ "PMU Elevation", "synphasor.conf.pmu_elevation", FT_FLOAT,
2199 		  BASE_NONE | BASE_UNIT_STRING, &units_meter_meters, 0x0, NULL, HFILL }},
2200 
2201 		{ &hf_conf_pmu_lat_unknown,
2202 		{ "PMU Latitude", "synphasor.conf.pmu_latitude", FT_FLOAT, BASE_NONE,
2203 		  NULL, 0x0, NULL, HFILL }},
2204 
2205 		{ &hf_conf_pmu_lon_unknown,
2206 		{ "PMU Longitude", "synphasor.conf.pmu_longitude", FT_FLOAT, BASE_NONE,
2207 		  NULL, 0x0, NULL, HFILL }},
2208 
2209 		{ &hf_conf_pmu_elev_unknown,
2210 		{ "PMU Elevation", "synphasor.conf.pmu_elevation", FT_FLOAT, BASE_NONE,
2211 		  NULL, 0x0, NULL, HFILL }},
2212 
2213 		{ &hf_conf_svc_class,
2214 		{ "Service class", "synphasor.conf.svc_class", FT_STRING, BASE_NONE,
2215 		  NULL, 0x0, NULL, HFILL }},
2216 
2217 		{ &hf_conf_window,
2218 		{ "PM window length", "synphasor.conf.window", FT_UINT32,
2219 		  BASE_DEC | BASE_UNIT_STRING, &units_microsecond_microseconds, 0x0, NULL, HFILL }},
2220 
2221 		{ &hf_conf_grp_dly,
2222 		{ "PM group delay", "synphasor.conf.grp_dly", FT_UINT32,
2223 		  BASE_DEC | BASE_UNIT_STRING, &units_microsecond_microseconds, 0x0, NULL, HFILL }},
2224 
2225 		{ &hf_conf_fnom,
2226 		{ "Nominal line frequency", "synphasor.conf.fnom", FT_BOOLEAN, 16,
2227 		  TFS(&conf_fnomnames), 0x0001, NULL, HFILL }},
2228 
2229 		{ &hf_conf_cfgcnt,
2230 		{ "Configuration change count", "synphasor.conf.cfgcnt", FT_UINT16, BASE_DEC,
2231 		  NULL, 0, NULL, HFILL }},
2232 
2233 	/* Data types for data frames */
2234 		/* Link to CFG Frame */
2235 		{ &hf_cfg_frame_num,
2236 		{ "Dissected using configuration from frame", "synphasor.data.conf_frame", FT_FRAMENUM, BASE_NONE, NULL, 0x0,"", HFILL }},
2237 
2238 		/* Flags in the STAT word */
2239 		{ &hf_data_statb15to14,
2240 		{ "Data error", "synphasor.data.status", FT_UINT16, BASE_HEX,
2241 		  VALS(data_statb15to14names), 0xC000, NULL, HFILL }},
2242 
2243 		{ &hf_data_statb13,
2244 		{ "Time synchronized", "synphasor.data.sync", FT_BOOLEAN, 16,
2245 		  TFS(&data_statb13names), 0x2000, NULL, HFILL }},
2246 
2247 		{ &hf_data_statb12,
2248 		{ "Data sorting", "synphasor.data.sorting", FT_BOOLEAN, 16,
2249 		  TFS(&data_statb12names), 0x1000, NULL, HFILL }},
2250 
2251 		{ &hf_data_statb11,
2252 		{ "Trigger detected", "synphasor.data.trigger", FT_BOOLEAN, 16,
2253 		  TFS(&data_statb11names), 0x0800, NULL, HFILL }},
2254 
2255 		{ &hf_data_statb10,
2256 		{ "Configuration changed", "synphasor.data.CFGchange", FT_BOOLEAN, 16,
2257 		  TFS(&data_statb10names), 0x0400, NULL, HFILL }},
2258 
2259 		{ &hf_data_statb09,
2260 		{ "Data modified indicator", "synphasor.data.data_modified", FT_BOOLEAN, 16,
2261 		  TFS(&data_statb09names), 0x0200, NULL, HFILL }},
2262 
2263 		{ &hf_data_statb08to06,
2264 		{ "PMU Time Quality", "synphasor.data.pmu_tq", FT_UINT16, BASE_HEX,
2265 		  VALS(data_statb08to06names), 0x01C0, NULL, HFILL }},
2266 
2267 		{ &hf_data_statb05to04,
2268 		{ "Unlocked time", "synphasor.data.t_unlock", FT_UINT16, BASE_HEX,
2269 		  VALS(data_statb05to04names), 0x0030, NULL, HFILL }},
2270 
2271 		{ &hf_data_statb03to00,
2272 		{ "Trigger reason", "synphasor.data.trigger_reason", FT_UINT16, BASE_HEX,
2273 		  VALS(data_statb03to00names), 0x000F, NULL, HFILL }},
2274 
2275 	/* Data type for command frame */
2276 		{ &hf_command,
2277 		{ "Command", "synphasor.command", FT_UINT16, BASE_HEX|BASE_RANGE_STRING,
2278 		  RVALS(command_names), 0xFFFF, NULL, HFILL }},
2279 
2280       /* Generated from convert_proto_tree_add_text.pl */
2281       { &hf_synphasor_data, { "Data", "synphasor.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2282       { &hf_synphasor_checksum, { "Checksum", "synphasor.checksum", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2283       { &hf_synphasor_checksum_status, { "Checksum Status", "synphasor.checksum.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0, NULL, HFILL }},
2284       { &hf_synphasor_num_phasors, { "Number of phasors", "synphasor.num_phasors", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2285       { &hf_synphasor_num_analog_values, { "Number of analog values", "synphasor.num_analog_values", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2286       { &hf_synphasor_num_digital_status_words, { "Number of digital status words", "synphasor.num_digital_status_words", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2287       { &hf_synphasor_rate_of_transmission, { "Rate of transmission", "synphasor.rate_of_transmission", FT_INT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2288       { &hf_synphasor_phasor, { "Phasor", "synphasor.phasor", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2289       { &hf_synphasor_actual_frequency_value, { "Actual frequency value", "synphasor.actual_frequency_value", FT_FLOAT, BASE_NONE|BASE_UNIT_STRING, &units_hz, 0x0, NULL, HFILL }},
2290       { &hf_synphasor_rate_change_frequency, { "Rate of change of frequency", "synphasor.rate_change_frequency", FT_FLOAT, BASE_NONE|BASE_UNIT_STRING, &units_hz_s, 0x0, NULL, HFILL }},
2291       { &hf_synphasor_frequency_deviation_from_nominal, { "Frequency deviation from nominal", "synphasor.frequency_deviation_from_nominal", FT_INT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
2292       { &hf_synphasor_analog_value, { "Analog value", "synphasor.analog_value", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2293       { &hf_synphasor_digital_status_word, { "Digital status word", "synphasor.digital_status_word", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2294       { &hf_synphasor_conversion_factor, { "conversion factor", "synphasor.conversion_factor", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
2295       { &hf_synphasor_factor_for_analog_value, { "Factor for analog value", "synphasor.factor_for_analog_value", FT_UINT32, BASE_DEC, NULL, 0x000000FF, NULL, HFILL }},
2296       { &hf_synphasor_channel_name, { "Channel name", "synphasor.channel_name", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2297       { &hf_synphasor_extended_frame_data, { "Extended frame data", "synphasor.extended_frame_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2298       { &hf_synphasor_unknown_data, { "Unknown data", "synphasor.data.unknown", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
2299       { &hf_synphasor_status_word_mask_normal_state, { "Normal state", "synphasor.status_word_mask.normal_state", FT_UINT16, BASE_HEX, NULL, 0xFFFF, NULL, HFILL }},
2300       { &hf_synphasor_status_word_mask_valid_bits, { "Valid bits", "synphasor.status_word_mask.valid_bits", FT_UINT16, BASE_HEX, NULL, 0xFFFF, NULL, HFILL }},
2301 	};
2302 
2303 	/* protocol subtree array */
2304 	static gint *ett[] = {
2305 		&ett_synphasor,
2306 		&ett_frtype,
2307 		&ett_timequal,
2308 		&ett_conf,
2309 		&ett_conf_station,
2310 		&ett_conf_format,
2311 		&ett_conf_phnam,
2312 		&ett_conf_annam,
2313 		&ett_conf_dgnam,
2314 		&ett_conf_phconv,
2315 		&ett_conf_phlist,
2316 		&ett_conf_phflags,
2317 		&ett_conf_phmod_flags,
2318 		&ett_conf_ph_user_flags,
2319 		&ett_conf_anconv,
2320 		&ett_conf_anlist,
2321 		&ett_conf_dgmask,
2322 		&ett_conf_chnam,
2323 		&ett_conf_wgs84,
2324 		&ett_data,
2325 		&ett_data_block,
2326 		&ett_data_stat,
2327 		&ett_data_phasors,
2328 		&ett_data_analog,
2329 		&ett_data_digital,
2330 		&ett_command,
2331 		&ett_status_word_mask
2332 	};
2333 
2334 	static ei_register_info ei[] = {
2335 		{ &ei_synphasor_extended_frame_data, { "synphasor.extended_frame_data.unaligned", PI_PROTOCOL, PI_WARN, "Size not multiple of 16-bit word", EXPFILL }},
2336 		{ &ei_synphasor_checksum, { "synphasor.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
2337 		{ &ei_synphasor_data_error, { "synphasor.data_error", PI_RESPONSE_CODE, PI_NOTE, "Data Error flag set", EXPFILL }},
2338 		{ &ei_synphasor_pmu_not_sync, { "synphasor.pmu_not_sync", PI_RESPONSE_CODE, PI_NOTE, "PMU not sync flag set", EXPFILL }},
2339 	};
2340 
2341 	expert_module_t* expert_synphasor;
2342 
2343 	/* register protocol */
2344 	proto_synphasor = proto_register_protocol(PROTOCOL_NAME,
2345 						  PROTOCOL_SHORT_NAME,
2346 						  PROTOCOL_ABBREV);
2347 
2348 	/* Registering protocol to be called by another dissector */
2349 	synphasor_udp_handle = register_dissector("synphasor", dissect_udp, proto_synphasor);
2350 
2351 	proto_register_field_array(proto_synphasor, hf, array_length(hf));
2352 	proto_register_subtree_array(ett, array_length(ett));
2353 	expert_synphasor = expert_register_protocol(proto_synphasor);
2354 	expert_register_field_array(expert_synphasor, ei, array_length(ei));
2355 
2356 } /* proto_register_synphasor() */
2357 
2358 /* called at startup and when the preferences change */
proto_reg_handoff_synphasor(void)2359 void proto_reg_handoff_synphasor(void)
2360 {
2361 	dissector_handle_t synphasor_tcp_handle;
2362 
2363 	synphasor_tcp_handle = create_dissector_handle(dissect_tcp, proto_synphasor);
2364 	dissector_add_for_decode_as("rtacser.data", synphasor_udp_handle);
2365 	dissector_add_uint_with_preference("udp.port", SYNPHASOR_UDP_PORT, synphasor_udp_handle);
2366 	dissector_add_uint_with_preference("tcp.port", SYNPHASOR_TCP_PORT, synphasor_tcp_handle);
2367 
2368 } /* proto_reg_handoff_synphasor() */
2369 
2370 /*
2371  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
2372  *
2373  * Local variables:
2374  * c-basic-offset: 8
2375  * tab-width: 8
2376  * indent-tabs-mode: t
2377  * End:
2378  *
2379  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2380  * :indentSize=8:tabSize=8:noTabs=false:
2381  */
2382