summaryrefslogtreecommitdiffstats
path: root/plugins/epan/profinet/packet-pn-rtc-one.c
blob: 383909a26846a8290fd8483a259769acc4448687 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
/* packet-pn-rtc-one.c
 * Routines for PROFINET IO - RTC1 dissection.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1999 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * The PN-IO protocol is a field bus protocol related to decentralized
 * periphery and is developed by the PROFIBUS Nutzerorganisation e.V. (PNO),
 * see: www.profibus.com
 *
 *
 * PN-IO is based on the common DCE-RPC and the "lightweight" PN-RT
 * (ethernet type 0x8892) protocols.
 *
 * The context manager (CM) part is handling context information
 * (like establishing, ...) and is using DCE-RPC as its underlying
 * protocol.
 *
 * The actual cyclic data transfer and acyclic notification uses the
 * "lightweight" PN-RT protocol.
 *
 * There are some other related PROFINET protocols (e.g. PN-DCP, which is
 * handling addressing topics).
 *
 * Please note: the PROFINET CBA protocol is independent of the PN-IO protocol!
 */

/*
 * Cyclic PNIO RTC1 Data Dissection:
 *
 * To dissect cyclic PNIO RTC1 frames, this plug-in has to collect important module
 * information out of "Ident OK", "Connect Request" and "Write Response"
 * frames first.
 *
 * The data of Stationname-, -type and -id will be gained out of
 * packet-pn-dcp.c. The header packet-pn.h will transfer those data between
 * those two files.
 *
 * This file is used as a "addon" for packet-dcerpc-pn-io.c. Within "packet-dcerpc-pn-io.c"
 * the defined structures in "packet-pn.h" will be filled with all necessary information.
 * Those informations will be used in thise file to dissect cyclic PNIO RTC1 and PROFIsafe
 * frames. Furthermore since RTC1 is a special frame type of PNIO, this dissection uses the
 * already defined protocol PNIO.
 *
 * Overview for cyclic PNIO RTC1 data dissection functions:
 *   -> dissect_PNIO_C_SDU_RTC1 (general dissection of RTC1)
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <epan/packet.h>
#include <epan/dissectors/packet-dcerpc.h>
#include <epan/proto.h>
#include <epan/expert.h>

#include "packet-pn.h"


#define F_MESSAGE_TRAILER_4BYTE  4      /* PROFIsafe: Defines the Amount of Bytes for CRC and Status-/Controlbyte in PROFIsafe 2.4 */
#define F_MESSAGE_TRAILER_5BYTE  5      /* PROFIsafe: Defines the Amount of Bytes for CRC and Status-/Controlbyte in PROFIsafe 2.6 */
#define PN_INPUT_CR              1      /* PROFINET Input Connect Request value */
#define PN_INPUT_DATADESCRITPION 1      /* PROFINET Input Data Description value */

#define PA_PROFILE_API 0x9700u

static int proto_pn_io_rtc1 = -1;

/* General module information */
static int hf_pn_io_frame_info_type = -1;
static int hf_pn_io_frame_info_vendor = -1;
static int hf_pn_io_frame_info_nameofstation = -1;
static int hf_pn_io_frame_info_gsd_found = -1;
static int hf_pn_io_frame_info_gsd_error = -1;
static int hf_pn_io_frame_info_gsd_path = -1;
static int hf_pn_io_io_data_object = -1;
static int hf_pn_io_io_data_object_info_module_diff = -1;
static int hf_pn_io_io_data_object_info_moduleidentnumber = -1;
static int hf_pn_io_io_data_object_info_submoduleidentnumber = -1;

static int hf_pn_io_iocs = -1;
static int hf_pn_io_iops = -1;
static int hf_pn_io_ioxs_extension = -1;
static int hf_pn_io_ioxs_res14 = -1;
static int hf_pn_io_ioxs_instance = -1;
static int hf_pn_io_ioxs_datastate = -1;

/* PROFIsafe statusbyte and controlbyte */
static int hf_pn_io_ps_sb = -1;
static int hf_pn_io_ps_sb_iparOK = -1;
static int hf_pn_io_ps_sb_DeviceFault = -1;
static int hf_pn_io_ps_sb_CECRC = -1;
static int hf_pn_io_ps_sb_WDtimeout = -1;
static int hf_pn_io_ps_sb_FVactivated = -1;
static int hf_pn_io_ps_sb_Toggle_d = -1;
static int hf_pn_io_ps_sb_ConsNr_reset = -1;
static int hf_pn_io_ps_sb_res = -1;
static int hf_pn_io_ps_sb_toggelBitChanged = -1;
static int hf_pn_io_ps_sb_toggelBitChange_slot_nr = -1;
static int hf_pn_io_ps_sb_toggelBitChange_subslot_nr = -1;

static int hf_pn_io_ps_cb = -1;
static int hf_pn_io_ps_cb_iparEN = -1;
static int hf_pn_io_ps_cb_OAReq = -1;
static int hf_pn_io_ps_cb_resetConsNr = -1;
static int hf_pn_io_ps_cb_useTO2 = -1;
static int hf_pn_io_ps_cb_activateFV = -1;
static int hf_pn_io_ps_cb_Toggle_h = -1;
static int hf_pn_io_ps_cb_Chf_ACK = -1;
static int hf_pn_io_ps_cb_loopcheck = -1;
static int hf_pn_io_ps_cb_toggelBitChanged = -1;
static int hf_pn_io_ps_cb_toggelBitChange_slot_nr = -1;
static int hf_pn_io_ps_cb_toggelBitChange_subslot_nr = -1;

/* PROFIsafe */
static int hf_pn_io_ps_f_dest_adr = -1;
static int hf_pn_io_ps_f_data = -1;

/* PA Profile 4.02 */
static int hf_pn_pa_profile_status = -1;
static int hf_pn_pa_profile_status_quality = -1;
static int hf_pn_pa_profile_status_substatus_bad = -1;
static int hf_pn_pa_profile_status_substatus_uncertain = -1;
static int hf_pn_pa_profile_status_substatus_good = -1;
static int hf_pn_pa_profile_status_update_event = -1;
static int hf_pn_pa_profile_status_simulate = -1;
static int hf_pn_pa_profile_value_8bit = -1;
static int hf_pn_pa_profile_value_16bit = -1;
static int hf_pn_pa_profile_value_float = -1;

static gint ett_pn_io_rtc = -1;
static gint ett_pn_io_ioxs = -1;
static gint ett_pn_io_io_data_object = -1;
static gint ett_pn_pa_profile_status = -1;

static expert_field ei_pn_io_too_many_data_objects = EI_INIT;

static const value_string pn_io_ioxs_extension[] = {
    { 0x00 /*  0*/, "No IOxS octet follows" },
    { 0x01 /*  1*/, "One more IOxS octet follows" },
    { 0, NULL }
};

static const value_string pn_io_ioxs_instance[] = {
    { 0x00 /*  0*/, "Detected by subslot" },
    { 0x01 /*  1*/, "Detected by slot" },
    { 0x02 /*  2*/, "Detected by IO device" },
    { 0x03 /*  3*/, "Detected by IO controller" },
    { 0, NULL }
};

static const value_string pn_io_ioxs_datastate[] = {
    { 0x00 /*  0*/, "Bad" },
    { 0x01 /*  1*/, "Good" },
    { 0, NULL }
};

static const value_string pn_pa_profile_status_quality[] = {
    { 0x00 /*  0*/, "BAD" },
    { 0x01 /*  1*/, "UNCERTAIN" },
    { 0x02 /*  2*/, "GOOD" },
    { 0, NULL }
};

static const value_string pn_pa_profile_status_substatus_bad[] = {
    { 0x0, "Non specific" },
    { 0x2, "Not connected" },
    { 0x8, "Passivated" },
    { 0x9, "Maintenance alarm, more diagnosis" },
    { 0xA, "Process related, no maintenance" },
    { 0xF, "Function check, value not usable" },
    { 0, NULL }
};

static const value_string pn_pa_profile_status_substatus_uncertain[] = {
    { 0x2, "Substitute set" },
    { 0x3, "Initial value" },
    { 0xA, "Maintenance demanded" },
    { 0xE, "Process related, no maintenance" },
    { 0, NULL }
};

static const value_string pn_pa_profile_status_substatus_good[] = {
    { 0x0, "Good" },
    { 0x7, "Local override" },
    { 0x8, "Initial fail safe" },
    { 0x9, "Maintenance required" },
    { 0xA, "Maintenance demanded" },
    { 0xF, "Function check" },
    { 0, NULL }
};

static const value_string pn_pa_profile_status_update_event[] = {
    { 0x0, "No event" },
    { 0x1, "Update event" },
    { 0, NULL }
};

static const value_string pn_pa_profile_status_simulate[] = {
    { 0x0, "Simulation off" },
    { 0x1, "Simulation active" },
    { 0, NULL }
};


static int * const ps_sb_fields[] = {
    &hf_pn_io_ps_sb_res,
    &hf_pn_io_ps_sb_ConsNr_reset,
    &hf_pn_io_ps_sb_Toggle_d,
    &hf_pn_io_ps_sb_FVactivated,
    &hf_pn_io_ps_sb_WDtimeout,
    &hf_pn_io_ps_sb_CECRC,
    &hf_pn_io_ps_sb_DeviceFault,
    &hf_pn_io_ps_sb_iparOK,
    NULL
};

static int * const ps_cb_fields[] = {
    &hf_pn_io_ps_cb_loopcheck,
    &hf_pn_io_ps_cb_Chf_ACK,
    &hf_pn_io_ps_cb_Toggle_h,
    &hf_pn_io_ps_cb_activateFV,
    &hf_pn_io_ps_cb_useTO2,
    &hf_pn_io_ps_cb_resetConsNr,
    &hf_pn_io_ps_cb_OAReq,
    &hf_pn_io_ps_cb_iparEN,
    NULL
};

static int * const ioxs_fields[] = {
    &hf_pn_io_ioxs_datastate,
    &hf_pn_io_ioxs_instance,
    &hf_pn_io_ioxs_res14,
    &hf_pn_io_ioxs_extension,
    NULL
};

/*
static int * const pa_profile_status_fields[] = {
    &hf_pn_pa_profile_status_quality,
    &hf_pn_pa_profile_status_substatus_bad,
    &hf_pn_pa_profile_status_substatus_uncertain,
    &hf_pn_pa_profile_status_substatus_good,
    &hf_pn_pa_profile_status_update_event,
    &hf_pn_pa_profile_status_simulate,
    NULL
};
*/

/* Dissector for PROFIsafe Status Byte */
static int
dissect_pn_io_ps_SB(tvbuff_t *tvb, int offset,
packet_info *pinfo _U_, proto_tree *tree, guint8 *drep _U_, int hfindex, int * const *fields)
{

    if (tree) {
        guint8     u8StatusByte;
        proto_item *sb_item;

        u8StatusByte = tvb_get_guint8(tvb, offset);

        /* Add Status Byte subtree */
        sb_item = proto_tree_add_bitmask_with_flags(tree, tvb, offset, hfindex, ett_pn_io_ioxs, fields,
            ENC_LITTLE_ENDIAN, BMT_NO_APPEND);
        proto_item_append_text(sb_item, " (%s)", ((u8StatusByte == 0x20) || (u8StatusByte == 0x00)) ? "normal" : "unnormal");
    }

    return offset + 1;
}


/* Dissector for PROFIsafe Control Byte */
static int
dissect_pn_io_ps_CB(tvbuff_t *tvb, int offset,
packet_info *pinfo _U_, proto_tree *tree, guint8 *drep _U_, int hfindex, int * const *fields)
{

    if (tree) {
        guint8     u8ControlByte;
        proto_item *cb_item;

        u8ControlByte = tvb_get_guint8(tvb, offset);

        /* Add Status Byte subtree */
        cb_item = proto_tree_add_bitmask_with_flags(tree, tvb, offset, hfindex, ett_pn_io_ioxs, fields,
            ENC_LITTLE_ENDIAN, BMT_NO_APPEND);
        proto_item_append_text(cb_item, " (%s)", ((u8ControlByte == 0x20) || (u8ControlByte == 0x00) ||
            (u8ControlByte == 0xa0) || (u8ControlByte == 0x80)) ? "normal" : "unnormal");
    }

    return offset + 1;
}


/* Dissector for IOCS (As each IOCS stands for a specific Slot & Subslot) */
static int
dissect_PNIO_IOCS(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree,
            guint8 *drep _U_, int hfindex, guint16 slotNr, guint16 subSlotNr, int * const *fields)
{

    if (tree) {
        guint8      u8IOxS;
        proto_item *ioxs_item;

        u8IOxS = tvb_get_guint8(tvb, offset);

        /* Add ioxs subtree */
        ioxs_item = proto_tree_add_bitmask_with_flags(tree, tvb, offset, hfindex,
            ett_pn_io_ioxs, fields, ENC_LITTLE_ENDIAN, BMT_NO_APPEND);
        proto_item_append_text(ioxs_item,
            " (%s%s), Slot: 0x%x, Subslot: 0x%x",
            (u8IOxS & 0x01) ? "another IOxS follows " : "",
            (u8IOxS & 0x80) ? "good" : "bad",
            slotNr,
            subSlotNr);
    }

    return offset + 1;
}


/* dissect the IOxS (IOCS, IOPS) field */
static int
dissect_PNIO_IOxS(tvbuff_t *tvb, int offset,
packet_info *pinfo _U_, proto_tree *tree, guint8 *drep _U_, int hfindex, int * const *fields)
{

    if (tree) {
        guint8     u8IOxS;
        proto_item *ioxs_item;

        u8IOxS = tvb_get_guint8(tvb, offset);

        /* Add ioxs subtree */
        ioxs_item = proto_tree_add_bitmask_with_flags(tree, tvb, offset, hfindex,
            ett_pn_io_ioxs, fields, ENC_LITTLE_ENDIAN, BMT_NO_APPEND);
        proto_item_append_text(ioxs_item,
            " (%s%s)",
            (u8IOxS & 0x01) ? "another IOxS follows " : "",
            (u8IOxS & 0x80) ? "good" : "bad");
    }

    return offset + 1;
}


/* Universel dissector for flexibel PROFIsafe Data 8 to 64 Bits */
static int
dissect_pn_io_ps_uint(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
    proto_tree *tree, guint8 *drep,
int hfindex, guint8 bytelength, guint64 *pdata)
{
    guint64  data;
    gboolean generalDissection;

    generalDissection = FALSE;

    switch (bytelength) {
    case 1:     /* 8 Bit Safety IO Data */
        data = tvb_get_guint8(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 2:     /* 16 Bit Safety IO Data */
        data = tvb_get_letohs(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 3:     /* 24 Bit Safety IO Data */
        data = tvb_get_letoh24(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 4:     /* 32 Bit Safety IO Data */
        data = tvb_get_letohl(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 5:     /* 40 Bit Safety IO Data */
        data = tvb_get_letoh40(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 6:     /* 48 Bit Safety IO Data */
        data = tvb_get_letoh48(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 7:     /* 56 Bit Safety IO Data */
        data = tvb_get_letoh56(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    case 8:     /* 64 Bit Safety IO Data */
        data = tvb_get_letoh64(tvb, offset);
        if (pdata)
            *pdata = data;
        break;

    default:    /* Safety IO Data is too big to save it into one variable */
        dissect_pn_user_data(tvb, offset, pinfo, tree, bytelength, "Safety IO Data");
        generalDissection = TRUE;
        break;
    }

    if (tree && generalDissection == FALSE) {
        proto_tree_add_item(tree, hfindex, tvb, offset, bytelength, DREP_ENC_INTEGER(drep));
    }

    return offset + bytelength;
}


/* dissect a PN-IO RTC1 Cyclic Service Data Unit */
int
dissect_PNIO_C_SDU_RTC1(tvbuff_t *tvb, int offset,
    packet_info *pinfo, proto_tree *tree, guint8 *drep _U_, guint16 frameid)
{
    proto_tree  *data_tree = NULL;

    /* Count & offset for comparation of the arrays */
    guint16     frameOffset;
    guint32     objectCounter;
    gboolean    inputFlag;
    gboolean    outputFlag;
    gboolean    psInfoText;     /* Used to display only once per frame the info text "PROFIsafe Device" */

    proto_item *data_item;
    proto_item *IODataObject_item;
    proto_item *IODataObject_item_info;
    proto_tree *IODataObject_tree;
    proto_item *ModuleID_item;
    proto_item *ModuleDiff_item;

    wmem_strbuf_t *moduleName;

    guint8  toggleBitSb;
    guint8  toggleBitCb;
    guint64 f_data;

    guint8  statusbyte;
    guint8  controlbyte;
    guint8  safety_io_data_length;

    guint16 number_io_data_objects_input_cr;
    guint16 number_iocs_input_cr;
    guint16 number_io_data_objects_output_cr;
    guint16 number_iocs_output_cr;

    conversation_t    *conversation;
    stationInfo       *station_info = NULL;
    iocsObject        *iocs_object;
    ioDataObject      *io_data_object;
    moduleDiffInfo    *module_diff_info;
    wmem_list_frame_t *frame;
    wmem_list_frame_t *frame_diff;

    /* Initial */
    frameOffset = 0;
    f_data = 0;
    inputFlag = FALSE;
    outputFlag = FALSE;
    psInfoText = FALSE;
    number_io_data_objects_input_cr = 0;
    number_iocs_input_cr = 0;
    number_io_data_objects_output_cr = 0;
    number_iocs_output_cr = 0;

    wmem_list_frame_t  *aruuid_frame;
    ARUUIDFrame        *current_aruuid_frame = NULL;
    guint32             current_aruuid = 0;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "PNIO");            /* set protocol name */

    data_item = proto_tree_add_protocol_format(tree, proto_pn_io_rtc1, tvb, offset, tvb_captured_length(tvb),
            "PROFINET IO Cyclic Service Data Unit: %u bytes", tvb_captured_length(tvb));
    data_tree = proto_item_add_subtree(data_item, ett_pn_io_rtc);

    /* dissect_dcerpc_uint16(tvb, offset, pinfo, data_tree, drep, hf_pn_io_packedframe_SFCRC, &u16SFCRC); */
    if (!(dissect_CSF_SDU_heur(tvb, pinfo, data_tree, NULL) == FALSE))
        return(tvb_captured_length(tvb));

    /* Only dissect cyclic RTC1 frames, if PN Connect Request has been read */
    conversation = find_conversation(pinfo->num, &pinfo->dl_src, &pinfo->dl_dst, CONVERSATION_NONE, 0, 0, 0);

    /* Detect input data package and output data package */
    if (conversation != NULL) {
        if (aruuid_frame_setup_list != NULL) {
            for (aruuid_frame = wmem_list_tail(aruuid_frame_setup_list); aruuid_frame != NULL; aruuid_frame = wmem_list_frame_prev(aruuid_frame)) {
                current_aruuid_frame = (ARUUIDFrame*)wmem_list_frame_data(aruuid_frame);
                /* There are prerequisites to dissect RTC frame data */
                /* Current station info must be found before RTC frame dissection starts */
                /* if RTC frame has setup frame and setup frame number is less than RTC frame number AND if RTC frame has release frame and release frame number is greater than RTC frame number */
                /* if RTC frame has setup frame and setup frame number is less than RTC frame number AND RTC frame does not have release frame yet! */
                /* then, get AR UUID of current station info */
                if ((current_aruuid_frame->setupframe && current_aruuid_frame->setupframe < pinfo->num) &&
                   ((current_aruuid_frame->releaseframe && current_aruuid_frame->releaseframe > pinfo->num) ||
                    !current_aruuid_frame->releaseframe)) {
                    if (current_aruuid_frame->inputframe == frameid) {
                        current_aruuid = current_aruuid_frame->aruuid.data1;
                        break;
                    }
                    else if (current_aruuid_frame->outputframe == frameid) {
                        current_aruuid = current_aruuid_frame->aruuid.data1;
                        break;
                    }
                }
            }
        }

        station_info = (stationInfo*)conversation_get_proto_data(conversation, current_aruuid);

        if (station_info != NULL) {
            pn_find_dcp_station_info(station_info, conversation);

            if (pnio_ps_selection == TRUE) {
                col_set_str(pinfo->cinfo, COL_PROTOCOL, "PNIO_PS");    /* set PROFISsafe protocol name */
            }

            if (addresses_equal(&(pinfo->src), conversation_key_addr1(conversation->key_ptr)) && addresses_equal(&(pinfo->dst), conversation_key_addr2(conversation->key_ptr))) {
                inputFlag = TRUE;
                outputFlag = FALSE;
                number_io_data_objects_input_cr = station_info->ioDataObjectNr_in;
                number_iocs_input_cr = station_info->iocsNr_in;
            }

            if (addresses_equal(&(pinfo->dst), conversation_key_addr1(conversation->key_ptr)) && addresses_equal(&(pinfo->src), conversation_key_addr2(conversation->key_ptr))) {
                outputFlag = TRUE;
                inputFlag = FALSE;
                number_io_data_objects_output_cr = station_info->ioDataObjectNr_out;
                number_iocs_output_cr = station_info->iocsNr_out;
            }
        }
    }

    /* ------- Input (PNIO) / Response (PNIO_PS) Frame Handling ------- */
    if (inputFlag) {
        if (pnio_ps_selection == TRUE) {
            proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_type, tvb,
                offset, 0, "Response", "Response Frame (IO_Device -> IO_Controller)");
        }
        else {
            proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_type, tvb,
                offset, 0, "Input", "Input Frame (IO_Device -> IO_Controller)");
        }

        if (station_info != NULL) {
            if (station_info->typeofstation != NULL) {
                proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_vendor, tvb, 0,
                    0, station_info->typeofstation, "\"%s\"", station_info->typeofstation);
            }
            if (station_info->nameofstation != NULL) {
                proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_nameofstation, tvb, 0,
                    0, station_info->nameofstation, "\"%s\"", station_info->nameofstation);
            }

            if (station_info->gsdPathLength == TRUE) {      /* given path isn't too long for the array */
                if (station_info->gsdFound == TRUE) {       /* found a GSD-file */
                    if (station_info->gsdLocation != NULL) {
                        IODataObject_item_info = proto_tree_add_item(data_tree, hf_pn_io_frame_info_gsd_found, tvb, offset, 0, ENC_NA);
                        proto_item_append_text(IODataObject_item_info, ": \"%s\"", station_info->gsdLocation);
                    }
                }
                else {
                    if (station_info->gsdLocation != NULL) {
                        IODataObject_item_info = proto_tree_add_item(data_tree, hf_pn_io_frame_info_gsd_error, tvb, offset, 0, ENC_NA);
                        proto_item_append_text(IODataObject_item_info, " Please place relevant GSD-file under \"%s\"", station_info->gsdLocation);
                    }
                }
            }
            else {
                IODataObject_item_info = proto_tree_add_item(data_tree, hf_pn_io_frame_info_gsd_path, tvb, offset, 0, ENC_NA);
                proto_item_append_text(IODataObject_item_info, " Please check your GSD-file networkpath. (No Path configured)");
            }
        }

        /* ---- Input IOData-/IOCS-Object Handling ---- */
        objectCounter = number_io_data_objects_input_cr + number_iocs_input_cr;
        if (objectCounter > (guint)tvb_reported_length_remaining(tvb, offset)) {
            expert_add_info_format(pinfo, data_item, &ei_pn_io_too_many_data_objects, "Too many data objects: %d", objectCounter);
            return(tvb_captured_length(tvb));
        }

        while (objectCounter--) {
            /* ---- Input IO Data Object Handling ---- */
            if (station_info != NULL) {
                for (frame = wmem_list_head(station_info->ioobject_data_in); frame != NULL; frame = wmem_list_frame_next(frame)) {
                    io_data_object = (ioDataObject*)wmem_list_frame_data(frame);
                    if (io_data_object->frameOffset == frameOffset) {
                        /* Found following object */

                        IODataObject_item = proto_tree_add_item(data_tree, hf_pn_io_io_data_object, tvb, offset, 0, ENC_NA);
                        IODataObject_tree = proto_item_add_subtree(IODataObject_item, ett_pn_io_io_data_object);

                        /* Control: the Device still uses the correct ModuleIdentNumber? */
                        for (frame_diff = wmem_list_head(station_info->diff_module); frame_diff != NULL; frame_diff = wmem_list_frame_next(frame_diff)) {
                            module_diff_info = (moduleDiffInfo*)wmem_list_frame_data(frame_diff);
                            if (io_data_object->moduleIdentNr != module_diff_info->modulID) {
                                ModuleDiff_item = proto_tree_add_item(IODataObject_tree, hf_pn_io_io_data_object_info_module_diff, tvb, 0, 0, ENC_NA);
                                proto_item_append_text(ModuleDiff_item, ": Device using ModuleIdentNumber 0x%08x instead of 0x%08x", module_diff_info->modulID, io_data_object->moduleIdentNr);
                                break;
                            }
                        }

                        proto_tree_add_uint(IODataObject_tree, hf_pn_io_io_data_object_info_moduleidentnumber, tvb, 0, 0, io_data_object->moduleIdentNr);
                        proto_tree_add_uint(IODataObject_tree, hf_pn_io_io_data_object_info_submoduleidentnumber, tvb, 0, 0, io_data_object->subModuleIdentNr);

                        /* PROFIsafe Supported Inputmodule handling */
                        if (io_data_object->profisafeSupported == TRUE && pnio_ps_selection == TRUE) {
                            if (io_data_object->profisafeSupported == TRUE && psInfoText == FALSE) {
                                /* Only add one information string per device to the infotext */
                                col_append_str(pinfo->cinfo, COL_INFO, ", PROFIsafe Device");    /* Add string to wireshark infotext */
                                psInfoText = TRUE;
                            }

                            proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_f_dest_adr, tvb, 0, 0, io_data_object->f_dest_adr);

                            /* Get Safety IO Data */
                            if (io_data_object->f_crc_seed == FALSE) {
                                safety_io_data_length = io_data_object->length - F_MESSAGE_TRAILER_4BYTE;
                            } else {
                                safety_io_data_length = io_data_object->length - F_MESSAGE_TRAILER_5BYTE;
                            }

                            if (safety_io_data_length > 0) {
                                offset = dissect_pn_io_ps_uint(tvb, offset, pinfo, IODataObject_tree, drep, hf_pn_io_ps_f_data,
                                    safety_io_data_length, &f_data);
                            }

                            /* ---- Check for new PNIO data using togglebit ---- */
                            statusbyte = tvb_get_guint8(tvb, offset);
                            toggleBitSb = statusbyte & 0x20;     /* get ToggleBit of StatusByte */

                            if (io_data_object->lastToggleBit != toggleBitSb) {    /* ToggleBit has changed --> new Data incoming */
                                /* Special Filter for ToggleBit within Statusbyte */
                                ModuleID_item = proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_sb_toggelBitChanged, tvb, offset, 0, toggleBitSb);
                                proto_item_set_hidden(ModuleID_item);

                                ModuleID_item = proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_sb_toggelBitChange_slot_nr, tvb, offset, 0, io_data_object->slotNr);
                                proto_item_set_hidden(ModuleID_item);

                                ModuleID_item = proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_sb_toggelBitChange_subslot_nr, tvb, offset, 0, io_data_object->subSlotNr);
                                proto_item_set_hidden(ModuleID_item);
                            }

                            offset = dissect_pn_io_ps_SB(tvb, offset, pinfo, IODataObject_tree, drep, hf_pn_io_ps_sb, ps_sb_fields);
                            offset = dissect_pn_user_data(tvb, offset, pinfo, IODataObject_tree, io_data_object->f_crc_len, "CRC");

                            io_data_object->last_sb_cb = statusbyte;       /* save the value of current statusbyte */
                            io_data_object->lastToggleBit = toggleBitSb;   /* save the value of current togglebit within statusbyte */
                        }    /* END of PROFIsafe Module Handling */

                        else {
                            /* Module is not PROFIsafe supported */
                            if (io_data_object->api == PA_PROFILE_API)
                            {
                                offset = dissect_pn_pa_profile_data(tvb, offset, pinfo, IODataObject_tree, io_data_object->length, "IO Data");
                            }
                            else
                            {
                                offset = dissect_pn_user_data(tvb, offset, pinfo, IODataObject_tree, io_data_object->length, "IO Data");
                            }
                        }

                        if (io_data_object->discardIOXS == FALSE) {
                            offset = dissect_PNIO_IOxS(tvb, offset, pinfo, IODataObject_tree, drep, hf_pn_io_iops, ioxs_fields);
                            proto_item_set_len(IODataObject_item, io_data_object->length + 1);     /* Length = Databytes + IOXS Byte */
                        }
                        else {
                            proto_item_set_len(IODataObject_item, io_data_object->length);         /* Length = Databytes */
                        }

                        proto_item_append_text(IODataObject_item, ": Slot: 0x%x Subslot: 0x%x",
                            io_data_object->slotNr, io_data_object->subSlotNr);


                        /* ModuleIdentNr appears not only once in GSD-file -> set module name more generally */
                        if (io_data_object->amountInGSDML > 1) {    /* if ModuleIdentNr only appears once in GSD-file, use the found GSD-file-ModuleName, else ... */
                            if (io_data_object->slotNr == 0) {
                                moduleName = wmem_strbuf_new(pinfo->pool, "Headstation");
                            }
                            else {
                                moduleName = wmem_strbuf_new(pinfo->pool, "Module");
                            }

                            if (io_data_object->profisafeSupported == TRUE) {
                                /* PROFIsafe */
                                if (io_data_object->length >= 5) {        /* 5 due to 3 CRC bytes &  1 status byte & (at least) 1 data byte */
                                    wmem_strbuf_append(moduleName, ", DI");
                                }
                                else {
                                    wmem_strbuf_append(moduleName, ", DO");
                                }
                            }
                            else {
                                /* PROFINET */
                                if (io_data_object->length > 0) {
                                    wmem_strbuf_append(moduleName, ", DI");
                                }
                                else {
                                    wmem_strbuf_append(moduleName, ", DO");
                                }
                            }

                            io_data_object->moduleNameStr = wmem_strdup(wmem_file_scope(), wmem_strbuf_get_str(moduleName));
                        }

                        proto_item_append_text(IODataObject_item, " ModuleName: \"%s\"", io_data_object->moduleNameStr);

                        /* emphasize the PROFIsafe supported Modul */
                        if (io_data_object->profisafeSupported == TRUE && pnio_ps_selection == TRUE) {
                            (proto_item_append_text(IODataObject_item, " (PROFIsafe Module)"));
                        }


                        /* Set frameOffset to its new value, to find the next object */
                        frameOffset = frameOffset + io_data_object->length;  /* frameOffset = current value + data bytes */
                        if (io_data_object->discardIOXS == FALSE) {
                            frameOffset = frameOffset + 1;      /* frameOffset = current value + iops byte */
                        }
                    }
                }
            }

            /* ---- Input IOCS Object Handling ---- */
            if (station_info != NULL) {
                for (frame = wmem_list_head(station_info->iocs_data_in); frame != NULL; frame = wmem_list_frame_next(frame)) {
                    iocs_object = (iocsObject*)wmem_list_frame_data(frame);
                    if (iocs_object->frameOffset == frameOffset) {
                        offset = dissect_PNIO_IOCS(tvb, offset, pinfo, data_tree, drep, hf_pn_io_iocs, iocs_object->slotNr,
                            iocs_object->subSlotNr, ioxs_fields);

                        /* Set frameOffset to its new value, to find the next object */
                        frameOffset = frameOffset + 1;      /* frameOffset = current value + iops byte */

                        break;
                    }
                }
            }
        }

        /* Dissect padding */
        offset = dissect_pn_user_data(tvb, offset, pinfo, tree, tvb_captured_length_remaining(tvb, offset), "GAP and RTCPadding");
    }   /* END of Input Frame Handling */

    /* ----- Output (PNIO) / Request (PNIO_PS) Frame Handling ------ */
    else if (outputFlag) {
        if (pnio_ps_selection == TRUE) {
            proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_type, tvb,
                offset, 0, "Request", "Request Frame (IO_Controller -> IO_Device)");
        }
        else {
            proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_type, tvb,
                offset, 0, "Output", "Output Frame (IO_Controller -> IO_Device)");
        }

        if (station_info != NULL) {
            if (station_info->typeofstation != NULL) {
                proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_vendor, tvb, 0,
                    0, station_info->typeofstation, "\"%s\"", station_info->typeofstation);
            }
            if (station_info->nameofstation != NULL) {
                proto_tree_add_string_format_value(data_tree, hf_pn_io_frame_info_nameofstation, tvb, 0,
                    0, station_info->nameofstation, "\"%s\"", station_info->nameofstation);
            }

            if (station_info->gsdPathLength == TRUE) {      /* given path isn't too long for the array */
                if (station_info->gsdFound == TRUE) {       /* found a GSD-file */
                    if (station_info->gsdLocation != NULL) {
                        IODataObject_item_info = proto_tree_add_item(data_tree, hf_pn_io_frame_info_gsd_found, tvb, offset, 0, ENC_NA);
                        proto_item_append_text(IODataObject_item_info, ": \"%s\"", station_info->gsdLocation);
                    }
                }
                else {
                    if (station_info->gsdLocation != NULL) {
                        IODataObject_item_info = proto_tree_add_item(data_tree, hf_pn_io_frame_info_gsd_error, tvb, offset, 0, ENC_NA);
                        proto_item_append_text(IODataObject_item_info, " Please place relevant GSD-file under \"%s\"", station_info->gsdLocation);
                    }
                }
            }
            else {
                IODataObject_item_info = proto_tree_add_item(data_tree, hf_pn_io_frame_info_gsd_path, tvb, offset, 0, ENC_NA);
                proto_item_append_text(IODataObject_item_info, " Please check your GSD-file networkpath. (No Path configured)");
            }
        }

        /* ---- Output IOData-/IOCS-Object Handling ---- */
        objectCounter = number_io_data_objects_output_cr + number_iocs_output_cr;
        if (objectCounter > (guint)tvb_reported_length_remaining(tvb, offset)) {
            expert_add_info_format(pinfo, data_item, &ei_pn_io_too_many_data_objects, "Too many data objects: %d", objectCounter);
            return(tvb_captured_length(tvb));
        }
        while (objectCounter--) {
            /* ---- Output IO Data Object Handling ---- */
            if (station_info != NULL) {
                for (frame = wmem_list_head(station_info->ioobject_data_out); frame != NULL; frame = wmem_list_frame_next(frame)) {
                    io_data_object = (ioDataObject*)wmem_list_frame_data(frame);
                    if (io_data_object != NULL && io_data_object->frameOffset == frameOffset) {
                        /* Found following object */

                        IODataObject_item = proto_tree_add_item(data_tree, hf_pn_io_io_data_object, tvb, offset, 0, ENC_NA);
                        IODataObject_tree = proto_item_add_subtree(IODataObject_item, ett_pn_io_io_data_object);

                        /* Control: the Device still uses the correct ModuleIdentNumber? */
                        for (frame_diff = wmem_list_head(station_info->diff_module); frame_diff != NULL; frame_diff = wmem_list_frame_next(frame_diff)) {
                            module_diff_info = (moduleDiffInfo*)wmem_list_frame_data(frame_diff);
                            if (io_data_object->moduleIdentNr != module_diff_info->modulID) {
                                ModuleDiff_item = proto_tree_add_item(IODataObject_tree, hf_pn_io_io_data_object_info_module_diff, tvb, 0, 0, ENC_NA);
                                proto_item_append_text(ModuleDiff_item, ": Device using ModuleIdentNumber 0x%08x instead of 0x%08x", module_diff_info->modulID, io_data_object->moduleIdentNr);
                                break;
                            }
                        }

                        proto_tree_add_uint(IODataObject_tree, hf_pn_io_io_data_object_info_moduleidentnumber, tvb, 0, 0, io_data_object->moduleIdentNr);
                        proto_tree_add_uint(IODataObject_tree, hf_pn_io_io_data_object_info_submoduleidentnumber, tvb, 0, 0, io_data_object->subModuleIdentNr);

                        if (io_data_object->profisafeSupported == TRUE && pnio_ps_selection == TRUE) {
                            if (io_data_object->profisafeSupported == TRUE && psInfoText == FALSE) {
                                /* Only add one information string per device to the infotext */
                                col_append_str(pinfo->cinfo, COL_INFO, ", PROFIsafe Device");    /* Add string to wireshark infotext */
                                psInfoText = TRUE;
                            }

                            proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_f_dest_adr, tvb, 0, 0, io_data_object->f_dest_adr);

                            /* Get Safety IO Data */
                            if (io_data_object->f_crc_seed == FALSE) {
                                safety_io_data_length = io_data_object->length - F_MESSAGE_TRAILER_4BYTE;
                            } else {
                                safety_io_data_length = io_data_object->length - F_MESSAGE_TRAILER_5BYTE;
                            }

                            if (safety_io_data_length > 0) {
                                offset = dissect_pn_io_ps_uint(tvb, offset, pinfo, IODataObject_tree, drep, hf_pn_io_ps_f_data,
                                    safety_io_data_length, &f_data);
                            }

                            /* ---- Check for new PNIO data using togglebit ---- */
                            controlbyte = tvb_get_guint8(tvb, offset);
                            toggleBitCb = controlbyte & 0x20;               /* get ToggleBit of Controlbyte */

                            if (io_data_object->lastToggleBit != toggleBitCb) {   /* ToggleBit has changed --> new Data incoming */
                                /* Special Filter for ToggleBit within Controlbyte */
                                ModuleID_item = proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_cb_toggelBitChanged, tvb, offset, 0, toggleBitCb);
                                proto_item_set_hidden(ModuleID_item);

                                ModuleID_item = proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_cb_toggelBitChange_slot_nr, tvb, offset, 0, io_data_object->slotNr);
                                proto_item_set_hidden(ModuleID_item);

                                ModuleID_item = proto_tree_add_uint(IODataObject_tree, hf_pn_io_ps_cb_toggelBitChange_subslot_nr, tvb, offset, 0, io_data_object->subSlotNr);
                                proto_item_set_hidden(ModuleID_item);
                            }

                            offset = dissect_pn_io_ps_CB(tvb, offset, pinfo, IODataObject_tree, drep, hf_pn_io_ps_cb, ps_cb_fields);
                            offset = dissect_pn_user_data(tvb, offset, pinfo, IODataObject_tree, io_data_object->f_crc_len, "CRC");

                            io_data_object->last_sb_cb = controlbyte;         /* save the value of current controlbyte */
                            io_data_object->lastToggleBit = toggleBitCb;      /* save the value of current togglebit within controlbyte */
                        }    /* End of PROFIsafe Module Handling */
                        else {
                            /* Module is not PROFIsafe supported */
                            if (io_data_object->api == PA_PROFILE_API)
                            {
                                offset = dissect_pn_pa_profile_data(tvb, offset, pinfo, IODataObject_tree, io_data_object->length, "IO Data");
                            }
                            else
                            {
                                offset = dissect_pn_user_data(tvb, offset, pinfo, IODataObject_tree, io_data_object->length, "IO Data");
                            }
                        }

                        if (io_data_object->discardIOXS == FALSE) {
                            offset = dissect_PNIO_IOxS(tvb, offset, pinfo, IODataObject_tree, drep, hf_pn_io_iops, ioxs_fields);
                            proto_item_set_len(IODataObject_item, io_data_object->length + 1);        /* Length = Databytes + IOXS Byte */
                        }
                        else {
                            proto_item_set_len(IODataObject_item, io_data_object->length);            /* Length = Databytes */
                        }

                        proto_item_append_text(IODataObject_item, ": Slot: 0x%x Subslot: 0x%x",
                            io_data_object->slotNr, io_data_object->subSlotNr);


                        /* ModuleIdentNr appears not only once in GSD-file -> set module name more generally */
                        if (io_data_object->amountInGSDML > 1) {    /* if ModuleIdentNr only appears once in GSD-file, use the found GSD-file-ModuleName, else ... */
                            if (io_data_object->slotNr == 0) {
                                moduleName = wmem_strbuf_new(pinfo->pool, "Headstation");
                            }
                            else {
                                moduleName = wmem_strbuf_new(pinfo->pool, "Module");
                            }

                            if (io_data_object->profisafeSupported == TRUE) {
                                /* PROFIsafe */
                                if (io_data_object->length >= 5) {        /* 5 due to 3 CRC bytes &  1 status byte & (at least) 1 data byte */
                                    wmem_strbuf_append(moduleName, ", DO");
                                }
                                else {
                                    wmem_strbuf_append(moduleName, ", DI");
                                }
                            }
                            else {
                                /* PROFINET */
                                if (io_data_object->length > 0) {
                                    wmem_strbuf_append(moduleName, ", DO");
                                }
                                else {
                                    wmem_strbuf_append(moduleName, ", DI");
                                }
                            }

                            io_data_object->moduleNameStr = wmem_strdup(wmem_file_scope(), wmem_strbuf_get_str(moduleName));
                        }

                        proto_item_append_text(IODataObject_item, " ModuleName: \"%s\"", io_data_object->moduleNameStr);

                        /* emphasize the PROFIsafe supported Modul */
                        if (io_data_object->profisafeSupported == TRUE && pnio_ps_selection == TRUE) {
                            proto_item_append_text(IODataObject_item, " (PROFIsafe Module)");
                        }

                        /* Set frameOffset to its new value, to find the next object */
                        frameOffset = frameOffset + io_data_object->length; /* frameOffset = current value + data bytes */
                        if (io_data_object->discardIOXS == FALSE) {
                            frameOffset = frameOffset + 1;      /* frameOffset = current value + iops byte */
                        }
                    }
                }
            }

            /* ---- Output IOCS Object Handling ---- */
            if (station_info != NULL) {
                for (frame = wmem_list_head(station_info->iocs_data_out); frame != NULL; frame = wmem_list_frame_next(frame)) {
                    iocs_object = (iocsObject*)wmem_list_frame_data(frame);
                    if (iocs_object->frameOffset == frameOffset) {
                        offset = dissect_PNIO_IOCS(tvb, offset, pinfo, data_tree, drep, hf_pn_io_iocs, iocs_object->slotNr,
                            iocs_object->subSlotNr, ioxs_fields);

                        /* Set frameOffset to its new value, to find the next object */
                        frameOffset = frameOffset + 1;      /* frameOffset = current value + iops byte */

                        break;
                    }
                }
            }
        }

        /* Dissect padding */
        offset = dissect_pn_user_data(tvb, offset, pinfo, tree, tvb_captured_length_remaining(tvb, offset), "GAP and RTCPadding");
    }   /* END of Output Frame Handling */

    return offset;
}


/* dissect the PA Profile status field */
static int
dissect_pn_pa_profile_status(tvbuff_t *tvb, int offset,
                             packet_info *pinfo _U_, proto_tree *tree, int hfindex)
{

    if (tree) {
        guint8      u8status;
        guint8      quality;
        proto_item *status_item;
        proto_tree *status_tree;
        const gchar* quality_name = NULL;

        u8status = tvb_get_guint8(tvb, offset);
        quality = (u8status >> 6u) & 0x3u;

        /* add status subtree */
        status_item = proto_tree_add_uint(tree, hfindex, tvb, offset, 1, u8status);

        quality_name = try_val_to_str(quality, pn_pa_profile_status_quality);

        proto_item_append_text(status_item,
                               " (%s)",
                               (quality_name != NULL) ? quality_name : "invalid");

        status_tree = proto_item_add_subtree(status_item, ett_pn_pa_profile_status);

        proto_tree_add_item(status_tree, hf_pn_pa_profile_status_quality, tvb, offset, 1, ENC_NA);

        switch(quality)
        {
            case 0:
                proto_tree_add_item(status_tree, hf_pn_pa_profile_status_substatus_bad, tvb, offset, 1, ENC_NA);
                break;
            case 1:
                proto_tree_add_item(status_tree, hf_pn_pa_profile_status_substatus_uncertain, tvb, offset, 1, ENC_NA);
                break;
            case 2:
                proto_tree_add_item(status_tree, hf_pn_pa_profile_status_substatus_good, tvb, offset, 1, ENC_NA);
                break;
            default:
                break;
        }
        proto_tree_add_item(status_tree, hf_pn_pa_profile_status_update_event, tvb, offset, 1, ENC_NA);
        proto_tree_add_item(status_tree, hf_pn_pa_profile_status_simulate,     tvb, offset, 1, ENC_NA);
    }

    return offset + 1;
}

int
dissect_pn_pa_profile_data(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
                          proto_tree *tree, guint32 length, const char *text)
{
    (void)text;
    /*
        All PA Profile submodules carry an 8-bit "status" plus the real data, which
        currently is a float, an 8-bit integer or a 16-bit integer.
        So we will have either 2, 3 or 5 bytes.
    */
    if (length == 2u)
    {
        proto_tree_add_item(tree, hf_pn_pa_profile_value_8bit, tvb, offset, 1, ENC_BIG_ENDIAN);
        dissect_pn_pa_profile_status(tvb, offset+1, pinfo, tree, hf_pn_pa_profile_status);
    }
    else if (length == 3u)
    {
        proto_tree_add_item(tree, hf_pn_pa_profile_value_16bit, tvb, offset, 2, ENC_BIG_ENDIAN);
        dissect_pn_pa_profile_status(tvb, offset+2, pinfo, tree, hf_pn_pa_profile_status);
    }
    else if (length == 5u)
    {
        proto_tree_add_item(tree, hf_pn_pa_profile_value_float, tvb, offset, 4, ENC_BIG_ENDIAN);
        dissect_pn_pa_profile_status(tvb, offset+4, pinfo, tree, hf_pn_pa_profile_status);
    }
    else
    {
        /* Delegate to standard user data if unknown */
        (void)dissect_pn_user_data(tvb, offset, pinfo, tree, length, "IO Data");
    }

    return offset + length;
}

void
init_pn_io_rtc1(int proto)
{
    static hf_register_info hf[] = {
        { &hf_pn_io_io_data_object,
            { "IODataObject", "pn_io.io_data_object",
            FT_NONE, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_io_data_object_info_module_diff,
            { "Difference", "pn_io.io_data_object.diff_module",
            FT_NONE, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_io_data_object_info_moduleidentnumber,
            { "ModuleIdentNumber", "pn_io.io_data_object.module_nr",
            FT_UINT32, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_io_data_object_info_submoduleidentnumber,
            { "SubmoduleIdentNumber", "pn_io.io_data_object.submodule_nr",
            FT_UINT32, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_frame_info_type,
            { "PN Frame Type", "pn_io.frame_info.type",
            FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_frame_info_vendor,
            { "DeviceVendorValue", "pn_io.frame_info.vendor",
            FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_frame_info_nameofstation,
            { "NameOfStation", "pn_io.frame_info.nameofstation",
            FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_frame_info_gsd_found,
            { "GSD-file found", "pn_io.frame_info.gsd_found",
            FT_NONE, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_frame_info_gsd_error,
            { "GSD-file not found.", "pn_io.frame_info.gsd_error",
            FT_NONE, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_frame_info_gsd_path,
            { "GSD-file networkpath failure!", "pn_io.frame_info.gsd_path",
            FT_NONE, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_iocs,
            { "IOCS", "pn_io.ioxs",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_iops,
            { "IOPS", "pn_io.ioxs",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ioxs_extension,
            { "Extension", "pn_io.ioxs.extension",
            FT_UINT8, BASE_HEX, VALS(pn_io_ioxs_extension), 0x01,
            NULL, HFILL }
        },
        { &hf_pn_io_ioxs_res14,
            { "Reserved", "pn_io.ioxs.res14",
            FT_UINT8, BASE_HEX, NULL, 0x1E,
            NULL, HFILL }
        },
        { &hf_pn_io_ioxs_instance,
            { "Instance", "pn_io.ioxs.instance",
            FT_UINT8, BASE_HEX, VALS(pn_io_ioxs_instance), 0x60,
            NULL, HFILL }
        },
        { &hf_pn_io_ioxs_datastate,
            { "DataState", "pn_io.ioxs.datastate",
            FT_UINT8, BASE_HEX, VALS(pn_io_ioxs_datastate), 0x80,
            NULL, HFILL }
        },
        /* PROFIsafe parameter */
        /* Status Byte & Control Byte for PROFIsafe --- dissector handle */
        { &hf_pn_io_ps_sb,
            { "Status Byte", "pn_io.ps.sb",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_toggelBitChanged,
            { "Status Byte", "pn_io.ps.sb.toggle_d_changed",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_toggelBitChange_slot_nr,
            { "Slot_Number", "pn_io.ps.sb.toggle_d_changed.slot",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_toggelBitChange_subslot_nr,
            { "Sub_Slot_Number", "pn_io.ps.sb.toggle_d_changed.subslot",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb,
            { "Control Byte", "pn_io.ps.cb",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_toggelBitChanged,
            { "Control Byte", "pn_io.ps.cb.toggle_h_changed",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_toggelBitChange_slot_nr,
            { "Slot_Number", "pn_io.ps.cb.toggle_h_changed.slot",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_toggelBitChange_subslot_nr,
            { "Sub_Slot_Number", "pn_io.ps.cb.toggle_h_changed.subslot",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        /* Structures for dissecting Status Byte & Control Byte PROFIsafe ---dissector details */
        { &hf_pn_io_ps_sb_iparOK,
            { "iPar_OK - F-Device has new iParameter values assigned", "pn_io.ps.sb.iPar_OK",
            FT_UINT8, BASE_HEX, NULL, 0x01,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_DeviceFault,
            { "Device_Fault - Failure exists in F-Device or F-Module", "pn_io.ps.sb.DeviceFault",
            FT_UINT8, BASE_HEX, NULL, 0x02,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_CECRC,
            { "CE_CRC - CRC Communication fault", "pn_io.ps.sb.CE_CRC",
            FT_UINT8, BASE_HEX, NULL, 0x04,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_WDtimeout,
            { "WD_timeout - WatchDog timeout Communication fault", "pn_io.ps.sb.WD_timeout",
            FT_UINT8, BASE_HEX, NULL, 0x08,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_FVactivated,
            { "FV_activated - Fail-safe values (FV) activated", "pn_io.ps.sb.FV_activated",
            FT_UINT8, BASE_HEX, NULL, 0x10,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_Toggle_d,
            { "Toggle_d - Device-based Toggle Bit", "pn_io.ps.sb.Toggle_d",
            FT_UINT8, BASE_HEX, NULL, 0x20,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_ConsNr_reset,
            { "cons_nr_R - F-Device has reset its consecutive number counter", "pn_io.ps.sb.cons_nr_R",
            FT_UINT8, BASE_HEX, NULL, 0x40,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_sb_res,
            { "Bit7 - reserved for future releases", "pn_io.ps.sb.bit7",
            FT_UINT8, BASE_HEX, NULL, 0x80,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_iparEN,
            { "iPar_EN - iParameter assignment deblocked", "pn_io.ps.cb.iparEN",
            FT_UINT8, BASE_HEX, NULL, 0x01,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_OAReq,
            { "OA_Req - Operator acknowledge requested", "pn_io.ps.cb.OA_Req",
            FT_UINT8, BASE_HEX, NULL, 0x02,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_resetConsNr,
            { "R_cons_nr - Set the Virtual Consecutive Number within the F-Device to be \"0\"", "pn_io.ps.cb.R_cons_nr",
            FT_UINT8, BASE_HEX, NULL, 0x04,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_useTO2,
            { "Bit3 - Reserved or Use the secondary watchdog (Use_TO2)", "pn_io.ps.cb.bit3",
            FT_UINT8, BASE_HEX, NULL, 0x08,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_activateFV,
            { "activate_FV - Fail-safe values (FV) to be activated", "pn_io.ps.cb.activate_FV",
            FT_UINT8, BASE_HEX, NULL, 0x10,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_Toggle_h,
            { "Toggle_h - Host-based Toggle Bit", "pn_io.ps.cb.Toggle_h",
            FT_UINT8, BASE_HEX, NULL, 0x20,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_Chf_ACK,
            { "Bit6 - Reserved or Operator acknowledge after cleared channel fault (ChF_Ack)", "pn_io.ps.cb.bit6",
            FT_UINT8, BASE_HEX, NULL, 0x40,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_cb_loopcheck,
            { "Bit7 - Reserved or Loop-back check (Loopcheck, shall be set to 1)", "pn_io.ps.cb.bit7",
            FT_UINT8, BASE_HEX, NULL, 0x80,
            NULL, HFILL }
        },
        /* PROFIsafe */
        { &hf_pn_io_ps_f_dest_adr,
            { "F_Dest_Add", "pn_io.ps.f_dest_add",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_io_ps_f_data,
            { "SafetyIO Data", "pn_io.ps.f_data",
            FT_UINT64, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status,
            { "Status", "pn_io.pa.status",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status_quality,
            { "Quality", "pn_io.pa.status.quality",
            FT_UINT8, BASE_HEX, VALS(pn_pa_profile_status_quality), 0xC0,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status_substatus_bad,
            { "Substatus", "pn_io.pa.status.substatus",
            FT_UINT8, BASE_HEX, VALS(pn_pa_profile_status_substatus_bad), 0x3C,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status_substatus_uncertain,
            { "Substatus", "pn_io.pa.status.substatus",
            FT_UINT8, BASE_HEX, VALS(pn_pa_profile_status_substatus_uncertain), 0x3C,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status_substatus_good,
            { "Substatus", "pn_io.pa.status.substatus",
            FT_UINT8, BASE_HEX, VALS(pn_pa_profile_status_substatus_good), 0x3C,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status_update_event,
            { "Update Event", "pn_io.pa.status.update",
            FT_UINT8, BASE_HEX, VALS(pn_pa_profile_status_update_event), 0x02,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_status_simulate,
            { "Simulate", "pn_io.pa.status.simulate",
            FT_UINT8, BASE_HEX, VALS(pn_pa_profile_status_simulate), 0x01,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_value_8bit,
            { "Value", "pn_io.pa.value",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_value_16bit,
            { "Value", "pn_io.pa.value",
            FT_UINT16, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_pn_pa_profile_value_float,
            { "Value", "pn_io.pa.value.float",
            FT_FLOAT, BASE_NONE, NULL, 0x0,
            NULL, HFILL }
        },
    };

    static gint *ett[] = {
        &ett_pn_io_rtc,
        &ett_pn_io_ioxs,
        &ett_pn_io_io_data_object,
        &ett_pn_pa_profile_status
    };

    static ei_register_info ei[] = {
        { &ei_pn_io_too_many_data_objects, { "pn_io.too_many_data_objects", PI_MALFORMED, PI_ERROR, "Too many data objects", EXPFILL }},
    };

    expert_module_t* expert_pn_io;

    proto_pn_io_rtc1 = proto;
    proto_register_field_array(proto, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_pn_io = expert_register_protocol(proto_pn_io_rtc1);
    expert_register_field_array(expert_pn_io, ei, array_length(ei));
}


/*
* Editor modelines  -  https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/