summaryrefslogtreecommitdiffstats
path: root/ucs2-lib/src/ucs_sys_diag.c
blob: 0c80eb6893585cd3b5b31f067fa37088a0b03a69 (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
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491                                                                            */
/* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
/*                                                                                                */
/* This program is free software: you can redistribute it and/or modify                           */
/* it under the terms of the GNU General Public License as published by                           */
/* the Free Software Foundation, either version 2 of the License, or                              */
/* (at your option) any later version.                                                            */
/*                                                                                                */
/* This program is distributed in the hope that it will be useful,                                */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
/* GNU General Public License for more details.                                                   */
/*                                                                                                */
/* You should have received a copy of the GNU General Public License                              */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
/*                                                                                                */
/* You may also obtain this software under a propriety license from Microchip.                    */
/* Please contact Microchip for further information.                                              */
/*------------------------------------------------------------------------------------------------*/

/*!
 * \file
 * \brief   Implementation of the System Diagnosis class
 * \details Performs the System Diagnosis
 *
 * \cond UCS_INTERNAL_DOC
 * \addtogroup G_SYS_DIAG
 * @{
 */

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_misc.h"
#include "ucs_ret_pb.h"
#include "ucs_sys_diag.h"
/*#include "ucs_mnsa.h"*/



/*------------------------------------------------------------------------------------------------*/
/* Internal constants                                                                             */
/*------------------------------------------------------------------------------------------------*/
#define SYS_DIAG_NUM_STATES            10U    /*!< \brief Number of state machine states */
#define SYS_DIAG_NUM_EVENTS            17U    /*!< \brief Number of state machine events */

#define SD_NUM_HELLO                   10U    /*!< \brief Number of Hello.Get Retries */
#define SD_TIMEOUT_HELLO              150U    /*!< \brief timeout used for repeating Hello.Get messages */
#define SD_TIMEOUT_COMMAND            100U    /*!< \brief timeout used for supervising INIC commands */
#define SD_TIMEOUT_CABLE_DIAGNOSIS   3000U    /*!< \brief timeout used for supervising cable link diagnosis */
#define SD_DIAG_ADDR_BASE          0x0500U    /*!< \brief Diagnosis Node Address of own node */

#define SD_WELCOME_SUCCESS              0U    /*!< \brief Welcome.Result reports success */

#define SD_SIGNATURE_VERSION            1U    /*!< \brief signature version used for System Diagnosis */


/*------------------------------------------------------------------------------------------------*/
/* Service parameters                                                                             */
/*------------------------------------------------------------------------------------------------*/
/*! Priority of the System Diagnosis service used by scheduler */
static const uint8_t SD_SRV_PRIO = 248U;   /* parasoft-suppress  MISRA2004-8_7 "Value shall be part of the module, not part of a function." */
/*! Main event for the System Diagnosis service */
static const Srv_Event_t SD_EVENT_SERVICE = 1U;


/*------------------------------------------------------------------------------------------------*/
/* Internal enumerators                                                                           */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Possible events of the system diagnosis state machine */
typedef enum SysDiag_Events_
{
    SD_E_NIL             = 0U,      /*!< \brief NIL Event */
    SD_E_STARTDIAG       = 1U,      /*!< \brief StartDiag API function was called */
    SD_E_SD_RES_OK       = 2U,      /*!< \brief MOSTNetworkSystemDiagnosis.Result received  */
    SD_E_ABORT           = 3U,      /*!< \brief Application requires stop of System Diagnosis */
    SD_E_HELLO_OK        = 4U,      /*!< \brief Hello.Status received */
    SD_E_HELLO_RETRY     = 5U,      /*!< \brief Retry the Hello.Get command */
    SD_E_HELLO_ALL_DONE  = 6U,      /*!< \brief All retries of the Hello.Get command are done */
    SD_E_WELCOME         = 7U,      /*!< \brief Welcome.Result, may be Ok or NotOk*/
    SD_E_ALL_DONE        = 8U,      /*!< \brief All branches and segments of the network were explored*/
    SD_E_PORT_FOUND      = 9U,      /*!< \brief An unexplored port was found */
    SD_E_PORT_ENABLED    = 10U,     /*!< \brief A port was succesful enabled */
    SD_E_PORT_DISABLED   = 11U,     /*!< \brief A port was succesful disabled */
    SD_E_BRANCH_FOUND    = 12U,     /*!< \brief Another branch was found */
    SD_E_CABLE_LINK_RES  = 13U,     /*!< \brief The CableLinkDiagnosis reported a result */
    SD_E_ERROR           = 14U,     /*!< \brief An error was detected */
    SD_E_TIMEOUT         = 15U,     /*!< \brief An timeout has been occurred */
    SD_E_NO_SUCCESS      = 16U      /*!< \brief Welcome result was NoSuccess */
} SysDiag_Events_t;

/*! \brief States of the system diagnosis state machine */
typedef enum SysDiag_State_
{
    SD_S_IDLE            =  0U,     /*!< \brief Idle state */
    SD_S_WAIT_DIAG       =  1U,     /*!< \brief System Diagnosis started */
    SD_S_WAIT_HELLO      =  2U,     /*!< \brief Hello command sent */
    SD_S_HELLO_TIMEOUT   =  3U,     /*!< \brief Hello command timed out */
    SD_S_WAIT_WELCOME    =  4U,     /*!< \brief Welcome sent */
    SD_S_NEXT_PORT       =  5U,     /*!< \brief Next port found to be tested */
    SD_S_WAIT_ENABLE     =  6U,     /*!< \brief Port Enable sent */
    SD_S_WAIT_DISABLE    =  7U,     /*!< \brief Port Disable sent */
    SD_S_CABLE_LINK_DIAG =  8U,      /*!< \brief Wait for CableL Link Diagnosis Result */
    SD_S_END             =  9U      /*!< \brief Wait for System Diagnosis stop */
} SysDiag_State_t;



/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes                                                                            */
/*------------------------------------------------------------------------------------------------*/
static void Sd_Service(void *self);

static void Sd_SysDiagInit(void* self);
static void Sd_SysDiagStart(void *self);
static void Sd_SysDiagStop(void *self);
static void Sd_SendHello(void *self);
static void Sd_Error(void *self);
static void Sd_ErrorWelcome(void *self);
static void Sd_SendWelcome(void *self);
static void Sd_CableLinkDiagnosis(void *self);
static void Sd_CalcPort(void *self);
static void Sd_AllDone(void *self);
static void Sd_EnablePort(void *self);
static void Sd_DisablePort(void *self);
static void Sd_Finish(void *self);
static void Sd_Abort(void *self);
static void Sd_StopDiagFailed(void *self);

static void Sd_HelloTimeout(void *self);
static void Sd_SysDiagTimeout(void *self);
static void Sd_WelcomeTimeout(void *self);
static void Sd_EnablePortTimeout(void *self);
static void Sd_DisablePortTimeout(void *self);
static void Sd_CableLinkDiagnosisTimeout(void *self);

static void Sd_SysDiagStartResultCb(void *self, void *result_ptr);
static void Sd_SysDiagStopResultCb(void *self, void *result_ptr);
static void Sd_HelloStatusCb(void *self, void *result_ptr);
static void Sd_WelcomeResultCb(void *self, void *result_ptr);
static void Sd_EnablePortResultCb(void *self, void *result_ptr);
static void Sd_DisablePortResultCb(void *self, void *result_ptr);
static void Sd_CableLinkDiagnosisResultCb(void *self, void *result_ptr);
static void Sd_OnTerminateEventCb(void *self, void *result_ptr);
static void Sd_TimerCb(void *self);




/*------------------------------------------------------------------------------------------------*/
/* State transition table (used by finite state machine)                                          */
/*------------------------------------------------------------------------------------------------*/
/*! \brief State transition table */
static const Fsm_StateElem_t sys_diag_trans_tab[SYS_DIAG_NUM_STATES][SYS_DIAG_NUM_EVENTS] =    /* parasoft-suppress  MISRA2004-8_7 "Value shall be part of the module, not part of a function." */
{

    { /* State SD_S_IDLE */
        /* SD_E_NIL            */ {NULL,                          SD_S_IDLE            },
        /* SD_E_STARTDIAG      */ {&Sd_SysDiagStart,              SD_S_WAIT_DIAG       },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_IDLE            },
        /* SD_E_ABORT          */ {NULL,                          SD_S_IDLE            },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_IDLE            },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_IDLE            },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_IDLE            },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_IDLE            },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_IDLE            },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_IDLE            },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_IDLE            },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_IDLE            },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_IDLE            },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_IDLE            },
        /* SD_E_ERROR          */ {NULL,                          SD_S_IDLE            },
        /* SD_E_TIMEOUT        */ {NULL,                          SD_S_IDLE            },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_IDLE            }
    },

    { /* State SD_S_WAIT_DIAG */
        /* SD_E_NIL            */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_SD_RES_OK      */ {&Sd_SendHello,                 SD_S_WAIT_HELLO      },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_WAIT_DIAG       },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {&Sd_SysDiagTimeout,            SD_S_END             },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_WAIT_DIAG       }
    },

    { /* State  SD_S_WAIT_HELLO*/
        /* SD_E_NIL            */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {&Sd_SendWelcome,               SD_S_WAIT_WELCOME    },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_WAIT_HELLO      },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {&Sd_HelloTimeout,              SD_S_HELLO_TIMEOUT   },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_WAIT_HELLO      }
    },

    { /* State SD_S_HELLO_TIMEOUT */
        /* SD_E_NIL            */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_HELLO_RETRY    */ {&Sd_SendHello,                 SD_S_WAIT_HELLO      },
        /* SD_E_HELLO_ALL_DONE */ {&Sd_CableLinkDiagnosis,        SD_S_CABLE_LINK_DIAG },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {NULL,                          SD_S_HELLO_TIMEOUT   },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_HELLO_TIMEOUT   }
    },

    { /* State SD_S_WAIT_WELCOME */
        /* SD_E_NIL            */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_WELCOME        */ {&Sd_CalcPort,                  SD_S_NEXT_PORT       },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_WAIT_WELCOME    },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {&Sd_WelcomeTimeout,            SD_S_END             },
        /* SD_E_NO_SUCCESS     */ {&Sd_ErrorWelcome,              SD_S_END             }
    },

    { /* State SD_S_NEXT_PORT */
        /* SD_E_NIL            */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_ALL_DONE       */ {&Sd_AllDone,                   SD_S_END             },
        /* SD_E_PORT_FOUND     */ {&Sd_EnablePort,                SD_S_WAIT_ENABLE     },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_BRANCH_FOUND   */ {&Sd_DisablePort,               SD_S_WAIT_DISABLE    },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {NULL,                          SD_S_NEXT_PORT       },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_NEXT_PORT       }
    },

    { /* State SD_S_WAIT_ENABLE */
        /* SD_E_NIL            */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_PORT_ENABLED   */ {&Sd_SendHello,                 SD_S_WAIT_HELLO      },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_WAIT_ENABLE     },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {&Sd_EnablePortTimeout,         SD_S_END             },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_WAIT_ENABLE     }
    },

    { /* State SD_S_WAIT_DISABLE */
        /* SD_E_NIL            */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_PORT_DISABLED  */ {&Sd_EnablePort,                SD_S_WAIT_ENABLE     },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_WAIT_DISABLE    },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {&Sd_DisablePortTimeout,        SD_S_END             },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_WAIT_DISABLE    }
    },

    { /* State SD_S_CABLE_LINK_DIAG */
        /* SD_E_NIL            */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_SD_RES_OK      */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_ABORT          */ {&Sd_Abort,                     SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_CABLE_LINK_DIAG },
        /* SD_E_CABLE_LINK_RES */ {&Sd_CalcPort,                  SD_S_NEXT_PORT       },
        /* SD_E_ERROR          */ {&Sd_Error,                     SD_S_END             },
        /* SD_E_TIMEOUT        */ {&Sd_CableLinkDiagnosisTimeout, SD_S_END             },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_CABLE_LINK_DIAG }
    },

    { /* State SD_S_END */
        /* SD_E_NIL            */ {NULL,                          SD_S_END             },
        /* SD_E_STARTDIAG      */ {NULL,                          SD_S_END             },
        /* SD_E_SD_RES_OK      */ {Sd_Finish,                     SD_S_IDLE            },
        /* SD_E_ABORT          */ {NULL,                          SD_S_END             },
        /* SD_E_HELLO_OK       */ {NULL,                          SD_S_END             },
        /* SD_E_HELLO_RETRY    */ {NULL,                          SD_S_END             },
        /* SD_E_HELLO_ALL_DONE */ {NULL,                          SD_S_END             },
        /* SD_E_WELCOME        */ {NULL,                          SD_S_END             },
        /* SD_E_ALL_DONE       */ {NULL,                          SD_S_END             },
        /* SD_E_PORT_FOUND     */ {NULL,                          SD_S_END             },
        /* SD_E_PORT_ENABLED   */ {NULL,                          SD_S_END             },
        /* SD_E_PORT_DISABLED  */ {NULL,                          SD_S_END             },
        /* SD_E_BRANCH_FOUND   */ {NULL,                          SD_S_END             },
        /* SD_E_CABLE_LINK_RES */ {NULL,                          SD_S_END             },
        /* SD_E_ERROR          */ {Sd_StopDiagFailed,             SD_S_IDLE            },
        /* SD_E_TIMEOUT        */ {Sd_StopDiagFailed,             SD_S_IDLE            },
        /* SD_E_NO_SUCCESS     */ {NULL,                          SD_S_END             }
    }

};



/*------------------------------------------------------------------------------------------------*/
/* Implementation                                                                                 */
/*------------------------------------------------------------------------------------------------*/

/*! \brief Constructor of class CSysDiag.
 *  \param self         Reference to CSysDiag instance
 *  \param inic         Reference to CInic instance
 *  \param base         Reference to CBase instance
 *  \param exc          Reference to CExc instance
 */
 void SysDiag_Ctor(CSysDiag *self, CInic *inic, CBase *base, CExc *exc)
{
    MISC_MEM_SET((void *)self, 0, sizeof(*self));

    self->inic = inic;
    self->exc  = exc;
    self->base = base;

    Fsm_Ctor(&self->fsm, self, &(sys_diag_trans_tab[0][0]), SYS_DIAG_NUM_EVENTS, SD_E_NIL);

    Sobs_Ctor(&self->sys_diag_start,           self, &Sd_SysDiagStartResultCb);
    Sobs_Ctor(&self->sys_diag_stop,            self, &Sd_SysDiagStopResultCb);
    Sobs_Ctor(&self->sys_hello,                self, &Sd_HelloStatusCb);
    Sobs_Ctor(&self->sys_welcome,              self, &Sd_WelcomeResultCb);
    Sobs_Ctor(&self->sys_enable_port,          self, &Sd_EnablePortResultCb);
    Sobs_Ctor(&self->sys_disable_port,         self, &Sd_DisablePortResultCb);
    Sobs_Ctor(&self->sys_cable_link_diagnosis, self, &Sd_CableLinkDiagnosisResultCb);

    /* register termination events */
    Mobs_Ctor(&self->sys_terminate, self, EH_M_TERMINATION_EVENTS, &Sd_OnTerminateEventCb);
    Eh_AddObsrvInternalEvent(&self->base->eh, &self->sys_terminate);

    /* Initialize System Diagnosis service */
    Srv_Ctor(&self->sd_srv, SD_SRV_PRIO, self, &Sd_Service);
    /* Add System Diagnosis service to scheduler */
    (void)Scd_AddService(&self->base->scd, &self->sd_srv);

}

/*! \brief Service function of the System Diagnosis service.
 *  \param self    Reference to System Diagnosis object
 */
static void Sd_Service(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;
    Srv_Event_t event_mask;
    Srv_GetEvent(&self_->sd_srv, &event_mask);
    if(SD_EVENT_SERVICE == (event_mask & SD_EVENT_SERVICE))   /* Is event pending? */
    {
        Fsm_State_t result;
        Srv_ClearEvent(&self_->sd_srv, SD_EVENT_SERVICE);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "FSM __ %d %d", 2U, self_->fsm.current_state, self_->fsm.event_occured));
        result = Fsm_Service(&self_->fsm);
        TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", (result != FSM_STATE_ERROR));
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "FSM -> %d", 1U, self_->fsm.current_state));
        MISC_UNUSED(result);
    }
}


/*! \brief Starts the System Diagnosis State machine
 *
 * \param *self         Reference to System Diagnosis object
 * \param *obs_ptr      Observer pointer
 * \return UCS_RET_SUCCESS              Operation successful
 * \return UCS_RET_ERR_API_LOCKED       System Diagnosis was already started
 * \return UCS_RET_ERR_BUFFER_OVERFLOW  Invalid observer
 */
Ucs_Return_t SysDiag_Run(CSysDiag *self, CSingleObserver *obs_ptr)
{
    Ucs_Return_t ret_val = UCS_RET_SUCCESS;

    if (self->startup_locked == false)
    {
        Ssub_Ret_t ret_ssub;

        ret_ssub = Ssub_AddObserver(&self->sysdiag, obs_ptr);
        if (ret_ssub != SSUB_UNKNOWN_OBSERVER)  /* obs_ptr == NULL ? */
        {
            self->startup_locked = true;

            Sd_SysDiagInit(self);

            Fsm_SetEvent(&self->fsm, SD_E_STARTDIAG);
            Srv_SetEvent(&self->sd_srv, SD_EVENT_SERVICE);

            TR_INFO((self->base->ucs_user_ptr, "[SD]", "SysDiag_Run", 0U));
        }
        else
        {
            ret_val = UCS_RET_ERR_BUFFER_OVERFLOW;  /* obs_ptr was invalid */
        }
    }
    else
    {
        ret_val = UCS_RET_ERR_API_LOCKED;
    }

    return ret_val;
}


/*! \brief Aborts the System Diagnosis State machine
 *
 * \param *self         Reference to System Diagnosis object
 * \return UCS_RET_SUCCESS              Operation successful
 * \return UCS_RET_ERR_NOT_AVAILABLE    System Diagnosis not running
 */
Ucs_Return_t SysDiag_Abort(CSysDiag *self)
{
    Ucs_Return_t ret_val = UCS_RET_SUCCESS;

    if (self->startup_locked == true)       /* check if System Diagnosis was started */
    {
        Tm_ClearTimer(&self->base->tm, &self->timer);

        Fsm_SetEvent(&self->fsm, SD_E_ABORT);
        Srv_SetEvent(&self->sd_srv, SD_EVENT_SERVICE);
        TR_INFO((self->base->ucs_user_ptr, "[SD]", "SysDiag_Abort", 0U));
    }
    else
    {
        ret_val = UCS_RET_ERR_NOT_AVAILABLE;
    }

    return ret_val;
}

/*!  Initialize the System Diagnosis
 *
 * \param self Reference to System Diagnosis object
 */
static void Sd_SysDiagInit(void* self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    self_->hello_retry              = SD_NUM_HELLO;
    self_->segment_nr               = 0U;
    self_->num_ports                = 0U;
    self_->curr_branch              = 0U;
    self_->source.node_address      = 0xFFFFU;
    self_->source.available         = false;
    self_->last_result              = SD_INIT;

    self_->target.node_address      = 0x0001U; /* address of own INIC */
    self_->target.available         = false;

    self_->admin_node_address        = SD_DIAG_ADDR_BASE;
}


/*! FSM action function: sets the INIC into System Diagnosis Mode
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_SysDiagStart(void *self)
{
    Ucs_Return_t ret_val;

    CSysDiag *self_ = (CSysDiag *)self;

    ret_val = Inic_NwSysDiagnosis(self_->inic, &self_->sys_diag_start);
    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_SysDiagStart", 0U));

    Tm_SetTimer(&self_->base->tm,
                &self_->timer,
                &Sd_TimerCb,
                self_,
                SD_TIMEOUT_COMMAND,
                0U);

    MISC_UNUSED(ret_val);
}


/*! Callback function for the Inic_NwSysDiagnosis() command
 *
 * \param *self         Reference to System Diagnosis object
 * \param *result_ptr   Result of the Inic_NwSysDiagnosis() command
 */
static void Sd_SysDiagStartResultCb(void *self, void *result_ptr)
{
    CSysDiag *self_               = (CSysDiag *)self;
    Inic_StdResult_t *result_ptr_ = (Inic_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        Fsm_SetEvent(&self_->fsm, SD_E_SD_RES_OK);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_SysDiagStartResultCb SD_E_SD_RES_OK", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_SysDiagStartResultCb SD_E_ERROR", 0U));
    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}


/*! FSM action function: Timeout occured
 *
 * \param *self Reference to System Diagnosis object
 */
static void Sd_SysDiagTimeout(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    TR_FAILED_ASSERT(self_->base->ucs_user_ptr, "[SD]");

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code            = UCS_SD_ERROR;
    self_->report.err_info        = UCS_SD_ERR_UNSPECIFIED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    Sd_SysDiagStop(self_);
}

/*! FSM action function: Timeout occured
 *
 * \param *self Reference to System Diagnosis object
 */
static void Sd_EnablePortTimeout(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    TR_FAILED_ASSERT(self_->base->ucs_user_ptr, "[SD]");

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code            = UCS_SD_ERROR;
    self_->report.err_info        = UCS_SD_ERR_UNSPECIFIED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    Sd_SysDiagStop(self_);
}

/*! FSM action function: Timeout occured
 *
 * \param *self Reference to System Diagnosis object
 */
static void Sd_DisablePortTimeout(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    TR_FAILED_ASSERT(self_->base->ucs_user_ptr, "[SD]");

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code            = UCS_SD_ERROR;
    self_->report.err_info        = UCS_SD_ERR_UNSPECIFIED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    Sd_SysDiagStop(self_);
}

/*! Helper function. Stops the System Diagnosis
 *
 * \param *self Reference to System Diagnosis object
 */
static void Sd_SysDiagStop(void *self)
{
    Ucs_Return_t ret_val;

    CSysDiag *self_ = (CSysDiag *)self;

    ret_val = Inic_NwSysDiagEnd(self_->inic, &self_->sys_diag_stop);
    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_SysDiagStop", 0U));
    if (ret_val == UCS_RET_SUCCESS)
    {
        Tm_SetTimer(&self_->base->tm,
                    &self_->timer,
                    &Sd_TimerCb,
                    self_,
                    SD_TIMEOUT_COMMAND,
                    0U);
    }
    else
    {
        MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
        self_->report.code      = UCS_SD_ERROR;
        self_->report.err_info  = UCS_SD_ERR_STOP_SYSDIAG_FAILED;

        Ssub_Notify(&self_->sysdiag, &self_->report, false);

        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
    }
}


/*! \brief Callback function for the Inic_NwSysDiagEnd() command
 *
 * \param *self         Reference to System Diagnosis object
 * \param *result_ptr   Result of the Inic_NwSysDiagEnd() command
 */
static void Sd_SysDiagStopResultCb(void *self, void *result_ptr)
{
    CSysDiag *self_               = (CSysDiag *)self;
    Inic_StdResult_t *result_ptr_ = (Inic_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", UCS_RES_SUCCESS == result_ptr_->result.code);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        Fsm_SetEvent(&self_->fsm, SD_E_SD_RES_OK);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_SysDiagStopResultCb SD_E_SD_RES_OK", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_SysDiagStopResultCb SD_E_ERROR", 0U));
    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}



/*! FSM action function: Send Hello.Get command
 *
 * \param *self Reference to System Diagnosis object
 */
static void Sd_SendHello(void *self)
{
    Ucs_Return_t ret_val;

    CSysDiag *self_ = (CSysDiag *)self;

    ret_val = Exc_Hello_Get(self_->exc, 
                            UCS_ADDR_BROADCAST_BLOCKING, 
                            SD_SIGNATURE_VERSION, 
                            &self_->sys_hello);
    Tm_SetTimer(&self_->base->tm,
                &self_->timer,
                &Sd_TimerCb,
                self_,
                SD_TIMEOUT_HELLO,
                0U);

    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    MISC_UNUSED(ret_val);
}

/*! Callback function for the Enc.Hello.Status message
 *
 * \param *self         Reference to System Diagnosis object
 * \param *result_ptr   Result of the Exc_Hello_Get() command
 */
static void Sd_HelloStatusCb(void *self, void *result_ptr)
{
    CSysDiag *self_              = (CSysDiag *)self;
    Exc_StdResult_t *result_ptr_ = (Exc_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        /* read signature and store it for the Welcome command */
        self_->target.signature = (*(Exc_HelloStatus_t *)(result_ptr_->data_info)).signature;
        self_->target.version   = (*(Exc_HelloStatus_t *)(result_ptr_->data_info)).version;

        if (self_->segment_nr != 0U)
        {
            self_->target.node_address = self_->segment_nr + 0x0400U;

        }

        Fsm_SetEvent(&self_->fsm, SD_E_HELLO_OK);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_HelloStatusCb SD_E_SD_RES_OK", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_HelloStatusCb SD_E_ERROR", 0U));
    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}

/*! \brief Timer callback used for supervising INIC command timeouts.
 *  \param self    Reference to System Diagnosis object
 */
static void Sd_TimerCb(void *self)
{
    CSysDiag *self_              = (CSysDiag *)self;

    Fsm_SetEvent(&self_->fsm, SD_E_TIMEOUT);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_TimerCb SD_E_TIMEOUT", 0U));

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}


/*! FSM action function: retry hello command or start CableLinkDiagnosis
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_HelloTimeout(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    if (self_->hello_retry > 0U)
    {
        --self_->hello_retry;
        Fsm_SetEvent(&self_->fsm, SD_E_HELLO_RETRY);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_HelloTimeout SD_E_HELLO_RETRY", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_HELLO_ALL_DONE);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_HelloTimeout SD_E_HELLO_ALL_DONE", 0U));
    }

    /*Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);*/
}


/*! FSM action function: Send Welcome message
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_SendWelcome(void *self)
{
    Ucs_Return_t  ret_val;
    CSysDiag     *self_ = (CSysDiag *)self;

    self_->admin_node_address = SD_DIAG_ADDR_BASE + self_->segment_nr;

    ret_val = Exc_Welcome_Sr(self_->exc,
                             self_->target.node_address,
                             self_->admin_node_address,
                             SD_SIGNATURE_VERSION,
                             self_->target.signature,
                             &self_->sys_welcome);
    Tm_SetTimer(&self_->base->tm,
                &self_->timer,
                &Sd_TimerCb,
                self_,
                SD_TIMEOUT_COMMAND,
                0U);
    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    MISC_UNUSED(ret_val);
}


/*! \brief  Function is called on reception of the Welcome.Result messsage
 *  \param  self        Reference to System Diagnosis object
 *  \param  result_ptr  Pointer to the result of the Welcome message
 */
static void Sd_WelcomeResultCb(void *self, void *result_ptr)
{
    CSysDiag *self_              = (CSysDiag *)self;
    Exc_StdResult_t *result_ptr_ = (Exc_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        /* read signature and store it for the Welcome command */
        self_->target.result = (*(Exc_WelcomeResult_t *)(result_ptr_->data_info)).res;

        if (self_->target.result == SD_WELCOME_SUCCESS)
        {
            self_->target.available = true;

            if (self_->segment_nr == 0U)
            {
                self_->num_ports = self_->target.signature.num_ports;
            }
            else
            {
                self_->last_result = SD_SEGMENT;
            }
            /* do not report result for own node */
            if (self_->segment_nr != 0U)
            {
                MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));

                self_->report.code            = UCS_SD_TARGET_FOUND;
                self_->report.segment.branch  = self_->curr_branch;
                self_->report.segment.num     = self_->segment_nr;
                self_->report.segment.source  = self_->source.signature;
                self_->report.segment.target  = self_->target.signature;
                /*self_->report.cable_link_info = 0U;*/     /* element is not written deliberately */
                /*self_->report.err_info        = 0U;*/     /* element is not written deliberately */

                Ssub_Notify(&self_->sysdiag, &self_->report, false);
                TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_WelcomeResultCb ReportSegment", 0U));
            }

            Fsm_SetEvent(&self_->fsm, SD_E_WELCOME);
            TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_WelcomeResultCb SD_E_WELCOME", 0U));
        }
        else
        {
            MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));

            self_->report.code            = UCS_SD_ERROR;
            self_->report.segment.branch  = self_->curr_branch;
            self_->report.segment.num     = self_->segment_nr;
            self_->report.segment.source  = self_->source.signature;
            self_->report.segment.target  = self_->target.signature;
            /*self_->report.cable_link_info = 0U;*/     /* element is not written deliberately */
            self_->report.err_info        = UCS_SD_ERR_WELCOME_NO_SUCCESS;

            Ssub_Notify(&self_->sysdiag, &self_->report, false);

            Fsm_SetEvent(&self_->fsm, SD_E_NO_SUCCESS);
            TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_WelcomeResultCb reported NoSuccess", 0U));
        }

    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_WelcomeResultCb Error SD_E_ERROR 0x%x", 1U, result_ptr_->result.code));

    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}


/*! \brief FSM action function:  Calculate the next port tobe examined
 *  \param  self     Reference to System Diagnosis object
 */
static void Sd_CalcPort(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    switch (self_->last_result)
    {
        case SD_INIT:
            self_->curr_branch  = 0U;             /* Master device has at least one port */
            self_->source = self_->target;
            self_->master = self_->target;

            MISC_MEM_SET(&(self_->target), 0, sizeof(self_->target));
            self_->last_result = SD_SEGMENT;
            Fsm_SetEvent(&self_->fsm, SD_E_PORT_FOUND);
            TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CalcPort SD_E_PORT_FOUND", 0U));
            break;

        case SD_SEGMENT:
            if (self_->target.signature.num_ports > 1U)
            {
                self_->source = self_->target;
                MISC_MEM_SET(&(self_->target), 0, sizeof(self_->target));
                Fsm_SetEvent(&self_->fsm, SD_E_PORT_FOUND);
                TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CalcPort SD_E_PORT_FOUND", 0U));
            }
            else                                /* switch to next branch if possible*/
            {
                if (self_->num_ports == (self_->curr_branch + 1U))     /* last branch */
                {
                    Fsm_SetEvent(&self_->fsm, SD_E_ALL_DONE);
                    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CalcPort SD_E_ALL_DONE", 0U));
                }
                else
                {
                    self_->segment_nr = 1U;                         /* reset segment number */
                    self_->curr_branch++;                           /* switch to next port */
                    self_->source = self_->master;
                    MISC_MEM_SET(&(self_->target), 0, sizeof(self_->target));
                    Fsm_SetEvent(&self_->fsm, SD_E_BRANCH_FOUND);
                    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CalcPort SD_E_BRANCH_FOUND", 0U));
                }
            }
            break;

        case SD_CABLE_LINK:
            if (self_->num_ports == (self_->curr_branch + 1U))     /* last branch */
            {
                Fsm_SetEvent(&self_->fsm, SD_E_ALL_DONE);
                TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CalcPort SD_E_ALL_DONE", 0U));
            }
            else
            {
                self_->segment_nr = 1U;                             /* reset segment number */
                self_->curr_branch++;                               /* switch to next port */
                self_->source = self_->master;
                MISC_MEM_SET(&(self_->target), 0, sizeof(self_->target));
                Fsm_SetEvent(&self_->fsm, SD_E_BRANCH_FOUND);
                TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CalcPort SD_E_BRANCH_FOUND", 0U));
            }
            break;

        default:
            break;
    }

    /*Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);*/
}


/*! \brief FSM action function: Enable port
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_EnablePort(void *self)
{
    CSysDiag     *self_ = (CSysDiag *)self;
    uint16_t      target_address;
    uint8_t       port_number;
    Ucs_Return_t  ret_val;

    if (self_->segment_nr == 0U)
    {
        port_number    = self_->curr_branch;
        target_address = 0x0001U;
    }
    else
    {
        port_number    = 1U;
        target_address = self_->source.node_address;
    }

    ret_val = Exc_EnablePort_Sr(self_->exc, target_address, port_number, true, &self_->sys_enable_port);
    Tm_SetTimer(&self_->base->tm,
                &self_->timer,
                &Sd_TimerCb,
                self_,
                SD_TIMEOUT_COMMAND,
                0U);

    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    MISC_UNUSED(ret_val);
}


/*! Function is called on reception of the EnablePort.Result messsage
 *
 * \param *self         Reference to System Diagnosis object
 * \param *result_ptr
 */
static void Sd_EnablePortResultCb(void *self, void *result_ptr)
{
    CSysDiag *self_              = (CSysDiag *)self;
    Exc_StdResult_t *result_ptr_ = (Exc_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        self_->segment_nr++;
        Fsm_SetEvent(&self_->fsm, SD_E_PORT_ENABLED);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_EnablePortResultCb SD_E_PORT_ENABLED", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_EnablePortResultCb SD_E_ERROR", 0U));
    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}


/*! \brief FSM action function:
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_DisablePort(void *self)
{
    CSysDiag     *self_ = (CSysDiag *)self;
    uint16_t      target_address;
    uint8_t       port_number;
    Ucs_Return_t  ret_val;

    target_address = self_->admin_node_address;
    port_number = self_->curr_branch;

    ret_val = Exc_EnablePort_Sr(self_->exc, target_address, port_number, false, &self_->sys_disable_port);
    Tm_SetTimer(&self_->base->tm,
                &self_->timer,
                &Sd_TimerCb,
                self_,
                SD_TIMEOUT_COMMAND,
                0U);

    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    MISC_UNUSED(ret_val);
}


static void Sd_DisablePortResultCb(void *self, void *result_ptr)
{
    CSysDiag *self_              = (CSysDiag *)self;
    Exc_StdResult_t *result_ptr_ = (Exc_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        Fsm_SetEvent(&self_->fsm, SD_E_PORT_DISABLED);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_DisablePortResultCb SD_E_PORT_DISABLED", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_DisablePortResultCb SD_E_ERROR", 0U));
    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
}


/*! \brief FSM action function: Start CableLinkDiagnosis
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_CableLinkDiagnosis(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;
    uint16_t      target_address;
    uint8_t       port_number;
    Ucs_Return_t  ret_val;


    if (self_->segment_nr != 0U)    /* do not start CableLinkDiagnosis when connecting to local INIC */
    {
    target_address = self_->source.node_address;

    if (self_->segment_nr == 1U)
    {
        port_number = self_->curr_branch;
    }
    else
    {
        port_number = 1U;                   /* OS81119: always port 1 */
    }

    self_->last_result = SD_CABLE_LINK;

    ret_val = Exc_CableLinkDiagnosis_Start(self_->exc, target_address, port_number, &self_->sys_cable_link_diagnosis);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CableLinkDiagnosis", 0U));

    Tm_SetTimer(&self_->base->tm,
            &self_->timer,
            &Sd_TimerCb,
            self_,
            SD_TIMEOUT_CABLE_DIAGNOSIS,
            0U);

    TR_ASSERT(self_->base->ucs_user_ptr, "[SD]", ret_val == UCS_RET_SUCCESS);
    MISC_UNUSED(ret_val);
}
    else    /* stop SystemDiagnosis when connecting to local INIC failed */
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);
    }
}


static void Sd_CableLinkDiagnosisResultCb(void *self, void *result_ptr)
{
    CSysDiag *self_              = (CSysDiag *)self;

    Exc_StdResult_t *result_ptr_ = (Exc_StdResult_t *)result_ptr;

    Tm_ClearTimer(&self_->base->tm, &self_->timer);

    if (result_ptr_->result.code == UCS_RES_SUCCESS)
    {
        MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));

        self_->report.code            = UCS_SD_CABLE_LINK_RES;
        self_->report.segment.branch  = self_->curr_branch;
        self_->report.segment.num     = self_->segment_nr;
        self_->report.segment.source  = self_->source.signature;
        /*self_->report.segment.target  = self_->target.signature;*/ /* structure is not written deliberately */
        self_->report.cable_link_info = (*(Exc_CableLinkDiagResult_t *)(result_ptr_->data_info)).result;
        /*self_->report.err_info        = 0U;*/     /* element is not written deliberately */

        Ssub_Notify(&self_->sysdiag, &self_->report, false);


        Fsm_SetEvent(&self_->fsm, SD_E_CABLE_LINK_RES);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CableLinkDiagnosisResultCb SD_E_CABLE_LINK_RES", 0U));
    }
    else
    {
        Fsm_SetEvent(&self_->fsm, SD_E_ERROR);
        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_CableLinkDiagnosisResultCb SD_E_ERROR %02X %02X %02X", 3U, result_ptr_->result.info_ptr[0], result_ptr_->result.info_ptr[1], result_ptr_->result.info_ptr[2]));
    }

    Srv_SetEvent(&self_->sd_srv, SD_EVENT_SERVICE);

}


/*! \brief FSM action function: React on Timeout of CableLinkDiagnosis
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_CableLinkDiagnosisTimeout(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code      = UCS_SD_ERROR;
    self_->report.err_info  = UCS_SD_ERR_UNSPECIFIED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    TR_FAILED_ASSERT(self_->base->ucs_user_ptr, "[SD]");
    Sd_SysDiagStop(self_);
}

/*! \brief FSM action function: React on Timeout of Welcome
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_WelcomeTimeout(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code      = UCS_SD_ERROR;
    self_->report.err_info  = UCS_SD_ERR_UNSPECIFIED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    TR_FAILED_ASSERT(self_->base->ucs_user_ptr, "[SD]");
    Sd_SysDiagStop(self_);
}




/*! \brief FSM action function: All branches and segments explored, finish System Diagnosis
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_AllDone(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_AllDone", 0U));

    Sd_SysDiagStop(self_);
}


/*! \brief FSM action function: INIC system Diagnosis mode ended
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_Finish(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));

    self_->report.code = UCS_SD_FINISHED;
    Ssub_Notify(&self_->sysdiag, &self_->report, true);

    self_->startup_locked = false;

    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_Finish", 0U));
}

/*! \brief FSM action function: An unexpected error occurred.
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_Error(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code     = UCS_SD_ERROR;
    self_->report.err_info = UCS_SD_ERR_UNSPECIFIED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    Sd_SysDiagStop(self_);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_Error", 0U));
}

/*! \brief FSM action function: Welcome reports NoSuccess.
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_ErrorWelcome(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    Sd_SysDiagStop(self_);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_ErrorWelcome", 0U));
}

/*! \brief FSM action function: stopping system diagnosis mode failed
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_StopDiagFailed(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code     = UCS_SD_ERROR;
    self_->report.err_info = UCS_SD_ERR_STOP_SYSDIAG_FAILED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    /* always finish the System Diagnosis with event UCS_SD_FINISHED */
    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code = UCS_SD_FINISHED;
    Ssub_Notify(&self_->sysdiag, &self_->report, true);     /* remove the observer function */

    self_->startup_locked = false;

    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_StopDiagFailed", 0U));
}

/*! \brief FSM action function: Application requested to abort the System Diagnosis.
 *
 * \param *self     Reference to System Diagnosis object
 */
static void Sd_Abort(void *self)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
    self_->report.code = UCS_SD_ABORTED;
    Ssub_Notify(&self_->sysdiag, &self_->report, false);

    Sd_SysDiagStop(self_);
    TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_Abort", 0U));
}



/*!  Function is called on severe internal errors
 *
 * \param *self         Reference to System Diagnosis object
 * \param *result_ptr   Reference to data
 */
static void Sd_OnTerminateEventCb(void *self, void *result_ptr)
{
    CSysDiag *self_ = (CSysDiag *)self;

    MISC_UNUSED(result_ptr);

    if (self_->fsm.current_state != SD_S_IDLE)
    {
        Tm_ClearTimer(&self_->base->tm, &self_->timer);

        MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
        self_->report.code     = UCS_SD_ERROR;
        self_->report.err_info = UCS_SD_ERR_TERMINATED;
        Ssub_Notify(&self_->sysdiag, &self_->report, false);

        /* always finish the System Diagnosis with event UCS_SD_FINISHED */
        MISC_MEM_SET(&self_->report, 0, sizeof(self_->report));
        self_->report.code = UCS_SD_FINISHED;
        Ssub_Notify(&self_->sysdiag, &self_->report, true);     /* remove the observer function */

        TR_INFO((self_->base->ucs_user_ptr, "[SD]", "Sd_OnTerminateEventCb", 0U));

        /* reset FSM */
        self_->startup_locked = false;
        Sd_SysDiagInit(self_);
        self_->fsm.current_state = SD_S_IDLE;
    }
}




/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/