-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathpacket-synphasor.c
2374 lines (1976 loc) · 81.8 KB
/
packet-synphasor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* packet-synphasor.c
* Dissector for IEEE C37.118 synchrophasor frames.
*
* Copyright 2008, Jens Steinhauser <[email protected]>
* Copyright 2019, Dwayne Rich <[email protected]>
* Copyright 2020, Dmitriy Eliseev <[email protected]>
* Copyright 2024, Ivan Ugryumov <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <math.h>
#include <epan/packet.h>
#include <epan/crc16-tvb.h>
#include <epan/expert.h>
#include <epan/proto_data.h>
#include <epan/tfs.h>
#include <epan/unit_strings.h>
#include <wsutil/array.h>
#include "packet-tcp.h"
#include <wsutil/utf8_entities.h>
#define PNAME "IEEE C37.118 Synchrophasor Protocol"
#define PSNAME "SYNCHROPHASOR"
#define PFNAME "synphasor"
/* forward references */
void proto_register_synphasor(void);
void proto_reg_handoff_synphasor(void);
/* global variables */
static int proto_synphasor;
/* user preferences */
#define SYNPHASOR_TCP_PORT 4712 /* Not IANA registered */
#define SYNPHASOR_UDP_PORT 4713 /* Not IANA registered */
/* Config 1 & 2 frames have channel names that are all 16 bytes long */
/* Config 3 frame channel names have a variable length with a max of 255 characters */
#define CHNAM_LEN 16
#define MAX_NAME_LEN 255
#define G_PMU_ID_LEN 16
/* the ett... variables hold the state (open/close) of the treeview in the GUI */
static int ett_synphasor; /* root element for this protocol */
/* used in the common header */
static int ett_frtype;
static int ett_timequal;
/* used for config frames */
static int ett_conf;
static int ett_conf_station;
static int ett_conf_format;
static int ett_conf_phnam;
static int ett_conf_annam;
static int ett_conf_dgnam;
static int ett_conf_phconv;
static int ett_conf_phlist;
static int ett_conf_phflags;
static int ett_conf_phmod_flags;
static int ett_conf_ph_user_flags;
static int ett_conf_anconv;
static int ett_conf_anlist;
static int ett_conf_dgmask;
static int ett_conf_chnam;
static int ett_conf_wgs84;
/* used for data frames */
static int ett_data;
static int ett_data_block;
static int ett_data_stat;
static int ett_data_phasors;
static int ett_data_analog;
static int ett_data_digital;
/* used for command frames */
static int ett_command;
static int ett_status_word_mask;
/* handles to the header fields hf[] in proto_register_synphasor() */
static int hf_sync;
static int hf_sync_frtype;
static int hf_sync_version;
static int hf_station_name_len;
static int hf_station_name;
static int hf_idcode_stream_source;
static int hf_idcode_data_source;
static int hf_g_pmu_id;
static int hf_frsize;
static int hf_soc;
static int hf_timeqal_lsdir;
static int hf_timeqal_lsocc;
static int hf_timeqal_lspend;
static int hf_timeqal_timequalindic;
static int hf_fracsec_raw;
static int hf_fracsec_ms;
static int hf_cont_idx;
static int hf_conf_timebase;
static int hf_conf_numpmu;
static int hf_conf_formatb3;
static int hf_conf_formatb2;
static int hf_conf_formatb1;
static int hf_conf_formatb0;
static int hf_conf_chnam_len;
static int hf_conf_chnam;
static int hf_conf_phasor_mod_b15;
static int hf_conf_phasor_mod_b10;
static int hf_conf_phasor_mod_b09;
static int hf_conf_phasor_mod_b08;
static int hf_conf_phasor_mod_b07;
static int hf_conf_phasor_mod_b06;
static int hf_conf_phasor_mod_b05;
static int hf_conf_phasor_mod_b04;
static int hf_conf_phasor_mod_b03;
static int hf_conf_phasor_mod_b02;
static int hf_conf_phasor_mod_b01;
static int hf_conf_phasor_type_b03;
static int hf_conf_phasor_type_b02to00;
static int hf_conf_phasor_user_data;
static int hf_conf_phasor_scale_factor;
static int hf_conf_phasor_angle_offset;
static int hf_conf_analog_scale_factor;
static int hf_conf_analog_offset;
static int hf_conf_pmu_lat;
static int hf_conf_pmu_lon;
static int hf_conf_pmu_elev;
static int hf_conf_pmu_lat_unknown;
static int hf_conf_pmu_lon_unknown;
static int hf_conf_pmu_elev_unknown;
static int hf_conf_svc_class;
static int hf_conf_window;
static int hf_conf_grp_dly;
static int hf_conf_fnom;
static int hf_conf_cfgcnt;
static int hf_data_statb15to14;
static int hf_data_statb13;
static int hf_data_statb12;
static int hf_data_statb11;
static int hf_data_statb10;
static int hf_data_statb09;
static int hf_data_statb08to06;
static int hf_data_statb05to04;
static int hf_data_statb03to00;
static int hf_command;
static int hf_cfg_frame_num;
/* Generated from convert_proto_tree_add_text.pl */
static int hf_synphasor_data;
static int hf_synphasor_checksum;
static int hf_synphasor_checksum_status;
static int hf_synphasor_num_phasors;
static int hf_synphasor_num_analog_values;
static int hf_synphasor_num_digital_status_words;
static int hf_synphasor_rate_of_transmission;
static int hf_synphasor_phasor;
static int hf_synphasor_actual_frequency_value;
static int hf_synphasor_rate_change_frequency;
static int hf_synphasor_frequency_deviation_from_nominal;
static int hf_synphasor_analog_value;
static int hf_synphasor_digital_status_word;
static int hf_synphasor_conversion_factor;
static int hf_synphasor_factor_for_analog_value;
static int hf_synphasor_channel_name;
static int hf_synphasor_extended_frame_data;
static int hf_synphasor_unknown_data;
static int hf_synphasor_status_word_mask_normal_state;
static int hf_synphasor_status_word_mask_valid_bits;
static expert_field ei_synphasor_extended_frame_data;
static expert_field ei_synphasor_checksum;
static expert_field ei_synphasor_data_error;
static expert_field ei_synphasor_pmu_not_sync;
static dissector_handle_t synphasor_udp_handle;
static dissector_handle_t synphasor_tcp_handle;
/* the different frame types for this protocol */
enum FrameType {
DATA = 0,
HEADER,
CFG1,
CFG2,
CMD,
CFG3
};
/* Structures to save CFG frame content. */
/* type to indicate the format for (D)FREQ/PHASORS/ANALOG in data frame */
typedef enum {
integer, /* 16-bit signed integer */
floating_point /* single precision floating point */
} data_format;
typedef enum { rect, polar } phasor_notation_e;
typedef enum { V, A } unit_e;
/* holds the information required to dissect a single phasor */
typedef struct {
char name[MAX_NAME_LEN + 1];
unit_e unit;
uint32_t conv; /* cfg-2 conversion factor in 10^-5 scale */
float conv_cfg3; /* cfg-3 conversion scale factor */
float angle_offset_cfg3; /* cfg-3 angle offset */
} phasor_info;
/* holds the information for an analog value */
typedef struct {
char name[MAX_NAME_LEN + 1];
uint32_t conv; /* cfg-2 conversion scale factor, user defined scaling (so it's pretty useless) */
float conv_cfg3; /* cfg-3 conversion scale factor */
float offset_cfg3; /* cfg-3 conversion offset */
} analog_info;
/* holds information required to dissect a single PMU block in a data frame */
typedef struct {
uint16_t id; /* (Data Source ID) identifies source of block */
char name[MAX_NAME_LEN + 1]; /* holds STN */
uint8_t cfg_frame_type; /* Config Frame Type (1,2,3,...) */
data_format format_fr; /* data format of FREQ and DFREQ */
data_format format_ph; /* data format of PHASORS */
data_format format_an; /* data format of ANALOG */
phasor_notation_e phasor_notation; /* format of the phasors */
unsigned fnom; /* nominal line frequency */
unsigned num_dg; /* number of digital status words */
wmem_array_t *phasors; /* array of phasor_infos */
wmem_array_t *analogs; /* array of analog_infos */
} config_block;
/* holds the id the configuration comes from an and
* an array of config_block members */
typedef struct {
uint32_t fnum; /* frame number */
uint16_t id; /* (Stream Source ID) identifies source of stream */
uint32_t time_base; /* Time base - resolution of FRACSEC time stamp. */
wmem_array_t *config_blocks; /* Contains a config_block struct for
* every PMU included in the config frame */
} config_frame;
/* strings for type bits in SYNC */
static const value_string typenames[] = {
{ 0, "Data Frame" },
{ 1, "Header Frame" },
{ 2, "Configuration Frame 1" },
{ 3, "Configuration Frame 2" },
{ 4, "Command Frame" },
{ 5, "Configuration Frame 3" },
{ 0, NULL }
};
/* strings for version bits in SYNC */
static const value_string versionnames[] = {
{ 1, "Defined in IEEE Std C37.118-2005" },
{ 2, "Added in IEEE Std C37.118.2-2011" },
{ 0, NULL }
};
/* strings for the time quality flags in FRACSEC */
static const true_false_string leapseconddir = {
"Add",
"Delete"
};
static const value_string timequalcodes[] = {
{ 0xF, "Clock failure, time not reliable" },
{ 0xB, "Clock unlocked, time within 10 s" },
{ 0xA, "Clock unlocked, time within 1 s" },
{ 0x9, "Clock unlocked, time within 10^-1 s" },
{ 0x8, "Clock unlocked, time within 10^-2 s" },
{ 0x7, "Clock unlocked, time within 10^-3 s" },
{ 0x6, "Clock unlocked, time within 10^-4 s" },
{ 0x5, "Clock unlocked, time within 10^-5 s" },
{ 0x4, "Clock unlocked, time within 10^-6 s" },
{ 0x3, "Clock unlocked, time within 10^-7 s" },
{ 0x2, "Clock unlocked, time within 10^-8 s" },
{ 0x1, "Clock unlocked, time within 10^-9 s" },
{ 0x0, "Normal operation, clock locked" },
{ 0 , NULL }
};
/* strings for flags in the FORMAT word of a configuration frame */
static const true_false_string conf_formatb123names = {
"32-bit IEEE floating point",
"16-bit integer"
};
static const true_false_string conf_formatb0names = {
"polar",
"rectangular"
};
/* strings to decode ANUNIT in configuration frame */
static const range_string conf_anconvnames[] = {
{ 0, 0, "single point-on-wave" },
{ 1, 1, "rms of analog input" },
{ 2, 2, "peak of input" },
{ 3, 4, "undefined" },
{ 5, 64, "reserved" },
{ 65, 255, "user defined" },
{ 0, 0, NULL }
};
/* strings for the FNOM field */
static const true_false_string conf_fnomnames = {
"50Hz",
"60Hz"
};
static const true_false_string conf_phasor_mod_b15 = {
"Modification applied, type not here defined",
"None"
};
static const true_false_string conf_phasor_mod_b10 = {
"Pseudo-phasor value (combined from other phasors)",
"None"
};
static const true_false_string conf_phasor_mod_b09 = {
"Phasor phase adjusted for rotation",
"None"
};
static const true_false_string conf_phasor_mod_b08 = {
"Phasor phase adjusted for calibration",
"None"
};
static const true_false_string conf_phasor_mod_b07 = {
"Phasor magnitude adjusted for calibration",
"None"
};
static const true_false_string conf_phasor_mod_b06 = {
"Filtered without changing sampling",
"None"
};
static const true_false_string conf_phasor_mod_b05 = {
"Down sampled with non-FIR filter",
"None"
};
static const true_false_string conf_phasor_mod_b04 = {
"Down sampled with FIR filter",
"None"
};
static const true_false_string conf_phasor_mod_b03 = {
"Down sampled by reselection",
"None"
};
static const true_false_string conf_phasor_mod_b02 = {
"Up sampled with extrapolation",
"None"
};
static const true_false_string conf_phasor_mod_b01 = {
"Up sampled with interpolation",
"None"
};
static const value_string conf_phasor_type[] = {
{ 0, "Voltage, Zero sequence" },
{ 1, "Voltage, Positive sequence" },
{ 2, "Voltage, Negative sequence" },
{ 3, "Voltage, Reserved" },
{ 4, "Voltage, Phase A" },
{ 5, "Voltage, Phase B" },
{ 6, "Voltage, Phase C" },
{ 7, "Voltage, Reserved" },
{ 8, "Current, Zero sequence" },
{ 9, "Current, Positive sequence" },
{ 10, "Current, Negative sequence" },
{ 11, "Current, Reserved" },
{ 12, "Current, Phase A" },
{ 13, "Current, Phase B" },
{ 14, "Current, Phase C" },
{ 15, "Current, Reserved" },
{ 0, NULL }
};
static const true_false_string conf_phasor_type_b03 = {
"Current",
"Voltage"
};
static const value_string conf_phasor_type_b02to00[] = {
{ 0, "Zero sequence" },
{ 1, "Positive sequence"},
{ 2, "Negative sequence"},
{ 3, "Reserved" },
{ 4, "Phase A" },
{ 5, "Phase B" },
{ 6, "Phase C" },
{ 7, "Reserved" },
{ 0, NULL }
};
static const true_false_string conf_phasor_user_defined = {
"Flags set",
"No flags set"
};
/* strings for flags in the STAT word of a data frame */
static const value_string data_statb15to14names[] = {
{ 0, "Good measurement data, no errors" },
{ 1, "PMU error, no information about data" },
{ 2, "PMU in test mode or absent data tags have been inserted (do not use values)" },
{ 3, "PMU error (do not use values)" },
{ 0, NULL }
};
static const true_false_string data_statb13names = {
"Synchronization lost",
"Clock is synchronized"
};
static const true_false_string data_statb12names = {
"By arrival",
"By timestamp"
};
static const true_false_string data_statb11names = {
"Trigger detected",
"No trigger"
};
static const true_false_string data_statb10names = {
"Within 1 minute",
"No"
};
static const true_false_string data_statb09names = {
"Data modified by a post-processing device",
"Data not modified"
};
static const value_string data_statb08to06names[] = {
{ 0, "Not used (indicates code from previous version of profile)" },
{ 1, "Estimated maximum time error < 100 ns" },
{ 2, "Estimated maximum time error < 1 " UTF8_MICRO_SIGN "s" },
{ 3, "Estimated maximum time error < 10 " UTF8_MICRO_SIGN "s" },
{ 4, "Estimated maximum time error < 100 " UTF8_MICRO_SIGN "s" },
{ 5, "Estimated maximum time error < 1 ms" },
{ 6, "Estimated maximum time error < 10 ms" },
{ 7, "Estimated maximum time error > 10 ms or time error unknown" },
{ 0, NULL }
};
static const value_string data_statb05to04names[] = {
{ 0, "Locked or unlocked less than 10 s"},
{ 1, "Unlocked for 10-100 s" },
{ 2, "Unlocked for 100-1000 s" },
{ 3, "Unlocked for over 1000 s" },
{ 0, NULL }
};
static const value_string data_statb03to00names[] = {
{ 0x0, "Manual" },
{ 0x1, "Magnitude low" },
{ 0x2, "Magnitude high" },
{ 0x3, "Phase-angel diff" },
{ 0x4, "Frequency high or low" },
{ 0x5, "df/dt high" },
{ 0x6, "Reserved" },
{ 0x7, "Digital" },
{ 0x8, "User defined" },
{ 0x9, "User defined" },
{ 0xA, "User defined" },
{ 0xB, "User defined" },
{ 0xC, "User defined" },
{ 0xD, "User defined" },
{ 0xE, "User defined" },
{ 0xF, "User defined" },
{ 0, NULL }
};
/* strings to decode the commands (CMD Field) according Table 15, p.26
* 0000 0000 0000 0001 - Turn off transmission of data frames
* 0000 0000 0000 0010 - Turn on transmission of data frames
* 0000 0000 0000 0011 - Send HDR frame
* 0000 0000 0000 0100 - Send CFG-1 frame.
* 0000 0000 0000 0101 - Send CFG-2 frame.
* 0000 0000 0000 0110 - Send CFG-3 frame (optional command).
* 0000 0000 0000 1000 - Extended frame.
* 0000 0000 xxxx xxxx - All undesignated codes reserved.
* 0000 yyyy xxxx xxxx - All codes where yyyy ≠ 0 available for user designation.
* zzzz xxxx xxxx xxxx - All codes where zzzz ≠ 0 reserved.
*/
static const range_string command_names[] = {
{ 0x0000, 0x0000, "reserved codes" },
{ 0x0001, 0x0001, "data transmission off" },
{ 0x0002, 0x0002, "data transmission on" },
{ 0x0003, 0x0003, "send HDR frame" },
{ 0x0004, 0x0004, "send CFG-1 frame" },
{ 0x0005, 0x0005, "send CFG-2 frame" },
{ 0x0006, 0x0006, "send CFG-3 frame" },
{ 0x0007, 0x0007, "reserved codes" },
{ 0x0008, 0x0008, "extended frame" },
{ 0x0009, 0x00FF, "reserved codes" },
{ 0x0100, 0x0FFF, "user designation" },
{ 0x1000, 0xFFFF, "reserved codes" },
{ 0x0000, 0x0000, NULL }
};
/******************************************************************************
* functions
******************************************************************************/
/* read in the size length for names found in config 3 frames
0 - no name
1-255 - length of name
*/
static uint8_t get_name_length(tvbuff_t *tvb, int offset)
{
uint8_t name_length;
/* read the size of the name */
name_length = tvb_get_uint8(tvb, offset);
return name_length;
}
/* Checks the CRC of a synchrophasor frame, 'tvb' has to include the whole
* frame, including CRC, the calculated CRC is returned in '*computedcrc'.
*/
static bool check_crc(tvbuff_t *tvb, uint16_t *computedcrc)
{
uint16_t crc;
unsigned len = tvb_get_ntohs(tvb, 2);
crc = tvb_get_ntohs(tvb, len - 2);
*computedcrc = crc16_x25_ccitt_tvb(tvb, len - 2);
if (crc == *computedcrc)
return true;
return false;
}
/* Dissects a configuration frame (only the most important stuff, tries
* to be fast, does no GUI stuff) and returns a pointer to a config_frame
* struct that contains all the information from the frame needed to
* dissect a DATA frame.
*
* use 'config_frame_free()' to free the config_frame again
*/
static config_frame *config_frame_fast(tvbuff_t *tvb)
{
uint16_t num_pmu;
int offset;
config_frame *frame;
/* get a new frame and initialize it */
frame = wmem_new(wmem_file_scope(), config_frame);
frame->config_blocks = wmem_array_new(wmem_file_scope(), sizeof(config_block));
// Start with Stream Source ID - identifies source of stream
offset = 4;
frame->id = tvb_get_ntohs(tvb, offset);
/* Skip to time base for FRACSEC */
offset += 11; // high 8 bits reserved for flags, so +1 byte
frame->time_base = tvb_get_uint24(tvb, offset,ENC_BIG_ENDIAN);
/* Next number of PMU blocks */
offset += 3;
num_pmu = tvb_get_ntohs(tvb, offset);
// Start of repeating blocks
offset += 2;
while (num_pmu) {
uint16_t format_flags;
uint16_t i, num_ph, num_an, num_dg;
int phunit, anunit, fnom;
config_block block;
/* initialize the block */
block.phasors = wmem_array_new(wmem_file_scope(), sizeof(phasor_info));
block.analogs = wmem_array_new(wmem_file_scope(), sizeof(analog_info));
/* copy the station name from the tvb to block, and add NULL byte */
tvb_memcpy(tvb, block.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
block.name[CHNAM_LEN] = '\0';
block.cfg_frame_type = 2;
block.id = tvb_get_ntohs(tvb, offset); offset += 2;
format_flags = tvb_get_ntohs(tvb, offset); offset += 2;
block.format_fr = (format_flags & 0x0008) ? floating_point : integer;
block.format_an = (format_flags & 0x0004) ? floating_point : integer;
block.format_ph = (format_flags & 0x0002) ? floating_point : integer;
block.phasor_notation = (format_flags & 0x0001) ? polar : rect;
num_ph = tvb_get_ntohs(tvb, offset); offset += 2;
num_an = tvb_get_ntohs(tvb, offset); offset += 2;
num_dg = tvb_get_ntohs(tvb, offset); offset += 2;
block.num_dg = num_dg;
/* the offset of the PHUNIT, ANUNIT, and FNOM blocks */
phunit = offset + (num_ph + num_an + num_dg * CHNAM_LEN) * CHNAM_LEN;
anunit = phunit + num_ph * 4;
fnom = anunit + num_an * 4 + num_dg * 4;
/* read num_ph phasor names and conversion factors */
for (i = 0; i != num_ph; i++) {
phasor_info pi;
uint32_t conv;
/* copy the phasor name from the tvb, and add NULL byte */
tvb_memcpy(tvb, pi.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
pi.name[CHNAM_LEN] = '\0';
conv = tvb_get_ntohl(tvb, phunit + 4 * i);
pi.unit = conv & 0xFF000000 ? A : V;
pi.conv = conv & 0x00FFFFFF;
pi.conv_cfg3 = 1;
pi.angle_offset_cfg3 = 0;
wmem_array_append_one(block.phasors, pi);
}
/* read num_an analog value names and conversion factors */
for (i = 0; i != num_an; i++) {
analog_info ai;
uint32_t conv;
/* copy the phasor name from the tvb, and add NULL byte */
tvb_memcpy(tvb, ai.name, offset, CHNAM_LEN); offset += CHNAM_LEN;
ai.name[CHNAM_LEN] = '\0';
conv = tvb_get_ntohl(tvb, anunit + 4 * i);
ai.conv = conv;
ai.conv_cfg3 = 1;
ai.offset_cfg3 = 0;
wmem_array_append_one(block.analogs, ai);
}
/* the names for the bits in the digital status words aren't saved,
there is no space to display them in the GUI anyway */
/* save FNOM */
block.fnom = tvb_get_ntohs(tvb, fnom) & 0x0001 ? 50 : 60;
offset = fnom + 2;
/* skip CFGCNT */
offset += 2;
wmem_array_append_one(frame->config_blocks, block);
num_pmu--;
}
return frame;
} /* config_frame_fast() */
/* Dissects a configuration 3 frame (only the most important stuff, tries
* to be fast, does no GUI stuff) and returns a pointer to a config_frame
* struct that contains all the information from the frame needed to
* dissect a DATA frame.
*
* use 'config_frame_free()' to free the config_frame again
*/
static config_frame * config_3_frame_fast(tvbuff_t *tvb)
{
uint16_t num_pmu;
int offset;
config_frame *frame;
phasor_info *pi = NULL;
analog_info *ai = NULL;
bool frame_not_fragmented;
/* get a new frame and initialize it */
frame = wmem_new(wmem_file_scope(), config_frame);
frame->config_blocks = wmem_array_new(wmem_file_scope(), sizeof(config_block));
// Start with Stream Source ID - identifies source of stream
offset = 4;
frame->id = tvb_get_ntohs(tvb, offset);
/* Skip to CONT_IDX -- Fragmented Frames not supported at this time */
offset += 10;
frame_not_fragmented = tvb_get_uint16(tvb, offset, ENC_BIG_ENDIAN) == 0;
/* Skip to time base for FRACSEC */
offset += 3; // high 8 bits reserved for flags, so +1 byte
frame->time_base = tvb_get_uint24(tvb, offset,ENC_BIG_ENDIAN);
/* Skip to number of PMU blocks */
offset += 3;
num_pmu = tvb_get_ntohs(tvb, offset);
/* start of repeating blocks */
offset += 2;
while ((num_pmu) && (frame_not_fragmented)) {
uint16_t format_flags;
uint16_t i, num_ph, num_an, num_dg;
uint8_t name_length;
config_block block;
/* initialize the block */
block.phasors = wmem_array_new(wmem_file_scope(), sizeof(phasor_info));
block.analogs = wmem_array_new(wmem_file_scope(), sizeof(analog_info));
/* copy the station name from the tvb to block, and add NULL byte */
/* first byte is name size */
name_length = get_name_length(tvb, offset);
offset += 1;
tvb_memcpy(tvb, block.name, offset, name_length);
offset += name_length;
block.name[name_length] = '\0';
block.cfg_frame_type = 3;
/* Block ID and Global PMU ID */
block.id = tvb_get_ntohs(tvb, offset);
offset += 2;
/* skip over Global PMU ID */
offset += G_PMU_ID_LEN;
format_flags = tvb_get_ntohs(tvb, offset);
offset += 2;
block.format_fr = (format_flags & 0x0008) ? floating_point : integer;
block.format_an = (format_flags & 0x0004) ? floating_point : integer;
block.format_ph = (format_flags & 0x0002) ? floating_point : integer;
block.phasor_notation = (format_flags & 0x0001) ? polar : rect;
num_ph = tvb_get_ntohs(tvb, offset);
offset += 2;
num_an = tvb_get_ntohs(tvb, offset);
offset += 2;
num_dg = tvb_get_ntohs(tvb, offset);
offset += 2;
block.num_dg = num_dg;
/* grab phasor names */
if (num_ph > 0)
{
pi = (phasor_info *)wmem_alloc(wmem_file_scope(), sizeof(phasor_info)*num_ph);
for (i = 0; i != num_ph; i++) {
/* copy the phasor name from the tvb, and add NULL byte */
name_length = get_name_length(tvb, offset);
offset += 1;
tvb_memcpy(tvb, pi[i].name, offset, name_length);
offset += name_length;
pi[i].name[name_length] = '\0';
}
}
/* grab analog names */
if (num_an > 0)
{
ai = (analog_info *)wmem_alloc(wmem_file_scope(), sizeof(analog_info)*num_an);
for (i = 0; i != num_an; i++) {
/* copy the phasor name from the tvb, and add NULL byte */
name_length = get_name_length(tvb, offset);
offset += 1;
tvb_memcpy(tvb, ai[i].name, offset, name_length);
offset += name_length;
ai[i].name[name_length] = '\0';
}
}
/* skip digital names */
if (num_dg > 0)
{
for (i = 0; i != num_dg * 16; i++) {
name_length = get_name_length(tvb, offset);
offset += name_length + 1;
}
}
/* get phasor conversion factors */
if (num_ph > 0)
{
for (i = 0; i != num_ph; i++) {
uint32_t phasor_unit;
/* get unit */
phasor_unit = tvb_get_ntohl(tvb, offset);
pi[i].unit = phasor_unit & 0x00000800 ? A : V;
pi[i].conv = 1;
pi[i].conv_cfg3 = tvb_get_ntohieee_float(tvb, offset + 4);
pi[i].angle_offset_cfg3 = tvb_get_ntohieee_float(tvb, offset + 8);
wmem_array_append_one(block.phasors, pi[i]);
offset += 12;
}
}
/* get analog conversion factors */
if (num_an > 0)
{
for (i = 0; i != num_an; i++) {
ai[i].conv = 1;
ai[i].conv_cfg3 = tvb_get_ntohieee_float(tvb, offset);
ai[i].offset_cfg3 = tvb_get_ntohieee_float(tvb, offset + 4);
wmem_array_append_one(block.analogs, ai[i]);
offset += 8;
}
}
/* skip digital masks */
if (num_dg > 0)
{
for (i = 0; i != num_dg; i++) {
offset += 4;
}
}
/* Skip to FNOM */
offset += 21;
/* save FNOM */
block.fnom = tvb_get_ntohs(tvb, offset) & 0x0001 ? 50 : 60;
offset += 2;
/* skip CFGCNT - offset ready for next PMU */
offset += 2;
wmem_array_append_one(frame->config_blocks, block);
num_pmu--;
}
return frame;
} /* config_3_frame_fast() */
/* Dissects the common header of frames.
*
* Returns the framesize, in contrast to most
* other helper functions that return the offset.
*/
static int dissect_header(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo)
{
proto_tree *temp_tree;
proto_item *temp_item;
config_frame *conf;
int offset = 0;
uint16_t framesize;
conf = (config_frame *)p_get_proto_data(wmem_file_scope(), pinfo, proto_synphasor, 0);
/* SYNC and flags */
temp_item = proto_tree_add_item(tree, hf_sync, tvb, offset, 2, ENC_BIG_ENDIAN);
temp_tree = proto_item_add_subtree(temp_item, ett_frtype);
proto_tree_add_item(temp_tree, hf_sync_frtype, tvb, offset, 2, ENC_BIG_ENDIAN);
proto_tree_add_item(temp_tree, hf_sync_version, tvb, offset, 2, ENC_BIG_ENDIAN);
offset += 2;
/* FRAMESIZE */
proto_tree_add_item(tree, hf_frsize, tvb, offset, 2, ENC_BIG_ENDIAN);
framesize = tvb_get_ntohs(tvb, offset); offset += 2;
/* IDCODE */
proto_tree_add_item(tree, hf_idcode_stream_source, tvb, offset, 2, ENC_BIG_ENDIAN);
offset += 2;
/* SOC */
proto_tree_add_item(tree, hf_soc, tvb, offset, 4, ENC_TIME_SECS | ENC_BIG_ENDIAN);
offset += 4;
/* FRACSEC */
/* time quality flags */
temp_tree = proto_tree_add_subtree(tree, tvb, offset, 1, ett_timequal, NULL, "Time quality flags");
proto_tree_add_item(temp_tree, hf_timeqal_lsdir, tvb, offset, 1, ENC_BIG_ENDIAN);
proto_tree_add_item(temp_tree, hf_timeqal_lsocc, tvb, offset, 1, ENC_BIG_ENDIAN);
proto_tree_add_item(temp_tree, hf_timeqal_lspend, tvb, offset, 1, ENC_BIG_ENDIAN);
proto_tree_add_item(temp_tree, hf_timeqal_timequalindic, tvb, offset, 1, ENC_BIG_ENDIAN);
offset += 1;
// Add RAW FRACSEC
proto_tree_add_item(tree, hf_fracsec_raw, tvb, offset, 3, ENC_BIG_ENDIAN);
// If exist configuration frame, add fracsec in milliseconds
if (conf){
uint32_t fracsec_raw = tvb_get_uint24(tvb, offset, ENC_BIG_ENDIAN);
float fracsec_ms = 1000.0f*fracsec_raw/conf->time_base;
proto_tree_add_float(tree, hf_fracsec_ms, tvb, offset, 3, fracsec_ms);
} else
{
}
/*offset += 3;*/
return framesize;
}
/* Dissects a single phasor for 'dissect_PHASORS()' */
static int dissect_single_phasor(tvbuff_t *tvb, int offset,
double *mag, double *phase, /* returns the resulting values in polar format here */
double* real, double* imag, /* returns the resulting values in rectangular format here*/
double* mag_real_unscaled, double* phase_imag_unscaled, /* returns unscaled values*/
config_block *block, /* information needed to... */
phasor_info* pi) /* ...dissect the phasor */
{
if (floating_point == block->format_ph) {
if (polar == block->phasor_notation) {
/* float, polar */
*mag = tvb_get_ntohieee_float(tvb, offset );
*phase = tvb_get_ntohieee_float(tvb, offset + 4);
*real = (*mag) * cos(*phase);
*imag = (*mag) * sin(*phase);
}
else {
/* float, rect */
*real = tvb_get_ntohieee_float(tvb, offset );
*imag = tvb_get_ntohieee_float(tvb, offset + 4);
*mag = sqrt(pow(*real, 2) + pow(*imag, 2));
*phase = atan2(*imag, *real);
}
}
else {
if (polar == block->phasor_notation) {
/* int, polar */
*mag_real_unscaled = tvb_get_ntohs(tvb, offset );
*phase_imag_unscaled = tvb_get_ntohis(tvb, offset + 2);
/* For fixed-point data in polar format all values are permissible for the magnitude
field. However, the angle field is restricted to ±31416. A value of 0x8000 (–32768) used in the angle field
will be used to signify absent data.
bullet 6.3.1 page 16 IEEE Std C37.118.2-2011
*/
if (*phase_imag_unscaled == -32768) {
*phase_imag_unscaled = NAN;
*mag_real_unscaled = NAN;
}
*phase = *phase_imag_unscaled/10000.0; /* angle is in radians*10^4 */
/* for values in integer format, consider conversation factor */
if (block->cfg_frame_type == 3){
*mag = (*mag_real_unscaled * pi->conv_cfg3);
*phase = *phase - pi->angle_offset_cfg3;
}
else{
*mag = (*mag_real_unscaled * pi->conv) * 0.00001;
}
*real = (*mag) * cos(*phase);
*imag = (*mag) * sin(*phase);
}
else {
/* int, rect */
*mag_real_unscaled = tvb_get_ntohis(tvb, offset );
*phase_imag_unscaled = tvb_get_ntohis(tvb, offset + 2);
/* For fixed-point data in rectangular format the PDC will use
0x8000 (–32768) as the substitute for the absent data.
bullet 6.3.1 page 16 IEEE Std C37.118.2-2011
*/
if (*mag_real_unscaled == -32768) {
*mag_real_unscaled = NAN;
}
if (*phase_imag_unscaled == -32768) {
*phase_imag_unscaled = NAN;
}
*mag = sqrt(pow(*mag_real_unscaled, 2) + pow(*phase_imag_unscaled, 2));
*phase = atan2(*phase_imag_unscaled, *mag_real_unscaled);
/* for values in integer format, consider conversation factor */
if (block->cfg_frame_type == 3) {
*mag = (*mag * pi->conv_cfg3);
*phase = *phase - pi->angle_offset_cfg3;
}
else {
*mag = (*mag * pi->conv) * 0.00001;
}
*real = (*mag) * cos(*phase);
*imag = (*mag) * sin(*phase);
}
}
return floating_point == block->format_ph ? 8 : 4;
}
/* used by 'dissect_data_frame()' to dissect the PHASORS field */
static int dissect_PHASORS(tvbuff_t *tvb, proto_tree *tree, config_block *block, int offset)
{
proto_tree *phasor_tree;
unsigned length;