summaryrefslogtreecommitdiff
path: root/NodeStateManager/NodeStateManager.c
blob: 4ed51509735e8474ca805555b1b9f6c54df450b3 (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
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
/**********************************************************************************************************************
*
* Copyright (C) 2012 Continental Automotive Systems, Inc.
*
* Author: Jean-Pierre.Bogler@continental-corporation.com
*
* Implementation of the NodeStateManager
*
* The NodeStateManager (NSM) is a central state manager for the system node. It manages the "NodeState",
* the "ApplicationMode" and many other states of the complete system. In addition, the NSM offers a
* session handling and a shutdown management.
* The NSM communicates with the NodeStateMachine (NSMC) to request and inform it about state changes
* and the NodeStateAccess (NSMA) to connect to the D-Bus.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Date             Author              Reason
* 20.06.2012       Jean-Pierre Bogler  CSP_WZ#388:  Initial creation
* 19.09.2012       Jean-Pierre Bogler  OvipRbt#135: Added new default sessions for product, thermal and power
*                                                   management. Fixed bug, when calling 'GetSessionState'.
*                                                   Undefined parameter 'SessionOwner' was used.
* 27.09.2012       Jean-Pierre Bogler  CSP_WZ#1194: Changed file header structure and license to be released
*                                                   as open source package. Introduced 'NodeStateTypes.h' to
*                                                   avoid circle includes and encapsulate type definitions.
* 08.10.2012       Jean-Pierre Bogler  CSP_WZ#951:  Introduced improvements and changes from 4-eye review:
*                                                     - Changed handling of failed applications
*                                                     - Changed handling of platform sessions
*                                                     - Fixed some QAC warnings
*                                                     - Added deallocation of GError objects
* 24.10.2012       Jean-Pierre Bogler  CSP_WZ#1322: The NSM does not use generated D-Bus objects anymore,
*                                                   due to legal restrictions. Instead, it registers
*                                                   callbacks at the NodeStateAccess library NSMA.
*                                                   Therefore, the parameters of the callbacks changed.
*                                                   In addition, the establishment of the connection and
*                                                   the handling of life cycle clients (timeout observation)
*                                                   is done by the NSMA.
* 01.11.2012       C. Domke            CSP_WZ#666:  Instrumented with LTPRO messages
* 10.01.2013       Jean-Pierre Bogler  CSP_WZ#1322: Initialize variables at declaration instead of using
*                                                   memset and use g_utf8_strlen instead of strlen to
*                                                   simplify configure srcipt (avoid some checks).
*
**********************************************************************************************************************/


/**********************************************************************************************************************
*
* Header includes
*
**********************************************************************************************************************/
#include "NodeStateManager.h" /* Own Header file                */
#include "NodeStateTypes.h"   /* Typedefinitions to use the NSM */
#include "string.h"           /* Memcpy etc.                    */
#include "gio/gio.h"          /* GLib lists                     */
#include "dlt/dlt.h"          /* DLT Log'n'Trace                */
#include "NodeStateMachine.h" /* Talk to NodeStateMachine       */
#include "NodeStateAccess.h"  /* Access the IPC (D-Bus)         */
#include "syslog.h"           /* Syslog messages                */

/**********************************************************************************************************************
*
* Local defines, macros and type definitions.
*
**********************************************************************************************************************/

/* The type defines the structure for a lifecycle consumer client                             */
typedef struct
{
  gchar                  *sBusName;          /* Bus name of the lifecycle client              */
  gchar                  *sObjName;          /* Object path of the client                     */
  guint32                 u32RegisteredMode; /* Bit array of shutdown modes                   */
  NSMA_tLcConsumerHandle  hClient;           /* Handle for proxy object for lifecycle client  */
  gboolean                boShutdown;        /* Only "run up" clients which are shut down     */
} NSM__tstLifecycleClient;


/* The type is used to store failed applications. A struct is used to allow extsions in future */
typedef struct
{
  gchar sName[NSM_MAX_SESSION_OWNER_LENGTH];
} NSM__tstFailedApplication;


/* List of names for the available default sessions, will are automatically provided by NSM    */
static const gchar* NSM__asDefaultSessions[] = { "DiagnosisSession",
                                                 "HevacSession",
                                                 "HmiActiveSession",
                                                 "NetworkActiveSession",
                                                 "NetworkPassiveSession",
                                                 "PdcSession",
                                                 "PermanentModeSession",
                                                 "PhoneSession",
                                                 "RvcSession",
                                                 "SwlSession",
                                                 "ProductLcSession",
                                                 "PlatformThermalSession",
                                                 "PlatformSupplySession",
                                                 "PersistencySession"
                                               };

/**********************************************************************************************************************
*
* Prototypes for file local functions (see implementation for description)
*
**********************************************************************************************************************/

/* Helper functions to destruct objects */
static void NSM__vFreeFailedApplicationObject(gpointer pFailedApplication);
static void NSM__vFreeSessionObject          (gpointer pSession          );
static void NSM__vFreeLifecycleClientObject  (gpointer pLifecycleClient  );


/* Helper functions to compare objects in lists */
static gboolean NSM__boIsPlatformSession           (NsmSession_s *pstSession);
static gint     NSM__i32LifecycleClientCompare     (gconstpointer pL1, gconstpointer pL2);
static gint     NSM__i32SessionOwnerNameSeatCompare(gconstpointer pS1, gconstpointer pS2);
static gint     NSM__i32SessionNameSeatCompare     (gconstpointer pS1, gconstpointer pS2);
static gint     NSM__i32SessionOwnerCompare        (gconstpointer pS1, gconstpointer pS2);
static gint     NSM__i32ApplicationCompare         (gconstpointer pA1, gconstpointer pA2);


/* Helper functions to recognize failed applications and disable their sessions */
static void             NSM__vDisableSessionsForApp(NSM__tstFailedApplication* pstFailedApp);
static NsmErrorStatus_e NSM__enSetAppStateFailed   (NSM__tstFailedApplication* pstFailedApp);
static NsmErrorStatus_e NSM__enSetAppStateValid    (NSM__tstFailedApplication* pstFailedApp);


/* Helper functions to control and start the "lifecycle request" sequence */
static void NSM__vCallNextLifecycleClient(void);
static void NSM__vOnLifecycleRequestFinish(const NsmErrorStatus_e enErrorStatus);


/* Internal functions, to set and get values. Indirectly used by D-Bus and StateMachine */
static NsmErrorStatus_e     NSM__enSetNodeState          (NsmNodeState_e       enNodeState,
                                                          gboolean             boInformBus,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enSetBootMode           (const gint           i32BootMode,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enSetApplicationMode    (NsmApplicationMode_e enApplicationMode,
                                                          gboolean             boInformBus,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enSetShutdownReason     (NsmShutdownReason_e  enNewShutdownReason,
                                                          gboolean             boInformMachine);

static void                 NSM__vPublishSessionChange   (NsmSession_s        *pstChangedSession,
                                                          gboolean             boInformBus,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enSetDefaultSessionState(NsmSession_s        *pstSession,
                                                          gboolean             boInformBus,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enSetProductSessionState(NsmSession_s        *pstSession,
                                                          gboolean             boInformBus,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enSetSessionState       (NsmSession_s        *pstSession,
                                                          gboolean             boInformBus,
                                                          gboolean             boInformMachine);
static NsmErrorStatus_e     NSM__enGetSessionState       (NsmSession_s        *pstSession);


/* Internal functions that are directly used from D-Bus and StateMachine */
static NsmErrorStatus_e     NSM__enGetNodeState      (NsmNodeState_e *penNodeState);
static NsmErrorStatus_e     NSM__enGetApplicationMode(NsmApplicationMode_e *penApplicationMode);


/* Callbacks for D-Bus interfaces of the NodeStateManager */
static NsmErrorStatus_e NSM__enOnHandleSetBootMode              (const gint                  i32BootMode);
static NsmErrorStatus_e NSM__enOnHandleSetNodeState             (const NsmNodeState_e        enNodeState);
static NsmErrorStatus_e NSM__enOnHandleSetApplicationMode       (const NsmApplicationMode_e  enApplMode);
static NsmErrorStatus_e NSM__enOnHandleRequestNodeRestart       (const NsmRestartReason_e    enRestartReason,
                                                                 const guint                 u32RestartType);
static NsmErrorStatus_e NSM__enOnHandleSetAppHealthStatus       (const gchar                *sAppName,
                                                                 const gboolean              boAppState);
static gboolean         NSM__boOnHandleCheckLucRequired         (void);
static NsmErrorStatus_e NSM__enOnHandleRegisterSession          (const gchar                *sSessionName,
                                                                 const gchar                *sSessionOwner,
                                                                 const NsmSeat_e             enSeatId,
                                                                 const NsmSessionState_e     enSessionState);
static NsmErrorStatus_e NSM__enOnHandleUnRegisterSession        (const gchar                *sSessionName,
                                                                 const gchar                *sSessionOwner,
                                                                 const NsmSeat_e             enSeatId);
static NsmErrorStatus_e NSM__enOnHandleRegisterLifecycleClient  (const gchar                *sBusName,
                                                                 const gchar                *sObjName,
                                                                 const guint                 u32ShutdownMode,
                                                                 const guint                 u32TimeoutMs);
static NsmErrorStatus_e NSM__enOnHandleUnRegisterLifecycleClient(const gchar                *sBusName,
                                                                 const gchar                *sObjName,
                                                                 const guint                 u32ShutdownMode);
static NsmErrorStatus_e NSM__enOnHandleGetSessionState          (const gchar                *sSessionName,
                                                                 const NsmSeat_e             enSeatId,
                                                                 NsmSessionState_e          *penSessionState);
static NsmErrorStatus_e NSM__enOnHandleSetSessionState          (const gchar                *sSessionName,
                                                                 const gchar                *sSessionOwner,
                                                                 const NsmSeat_e             enSeatId,
                                                                 const NsmSessionState_e     enSessionState);
static guint NSM__u32OnHandleGetAppHealthCount                  (void);
static guint NSM__u32OnHandleGetInterfaceVersion                (void);

/* Functions to simplify internal work flow */
static void  NSM__vInitializeVariables   (void);
static void  NSM__vCreatePlatformSessions(void);
static void  NSM__vCreateMutexes         (void);
static void  NSM__vDeleteMutexes         (void);

/* LTPROF helper function */
static void NSM__vLtProf(gchar *pszBus, gchar *pszObj, guint32 dwReason, gchar *pszInOut, guint32 dwValue);
static void NSM__vSyslogOpen(void);
static void NSM__vSyslogClose(void);


/**********************************************************************************************************************
*
* Local variables and constants
*
**********************************************************************************************************************/

/* Context for Log'n'Trace */
DLT_DECLARE_CONTEXT(NsmContext);

/* Variables for "Properties" hosted by the NSM */
static GMutex                    *NSM__pSessionMutex           = NULL;
static GSList                    *NSM__pSessions               = NULL;

static GList                     *NSM__pLifecycleClients       = NULL;

static GMutex                    *NSM__pNodeStateMutex         = NULL;
static NsmNodeState_e             NSM__enNodeState             = NsmNodeState_NotSet;

static GMutex                    *NSM__pApplicationModeMutex   = NULL;
static NsmApplicationMode_e       NSM__enApplicationMode       = NsmApplicationMode_NotSet;

static GSList                    *NSM__pFailedApplications     = NULL;

/* Variables for internal state management (of lifecycle requests) */
static NSM__tstLifecycleClient   *NSM__pCurrentLifecycleClient = NULL;

/* Constant array of callbacks which are registered at the NodeStateAccess library */
static const NSMA_tstObjectCallbacks NSM__stObjectCallBacks = { &NSM__enOnHandleSetBootMode,
                                                                &NSM__enOnHandleSetNodeState,
                                                                &NSM__enOnHandleSetApplicationMode,
                                                                &NSM__enOnHandleRequestNodeRestart,
                                                                &NSM__enOnHandleSetAppHealthStatus,
                                                                &NSM__boOnHandleCheckLucRequired,
                                                                &NSM__enOnHandleRegisterSession,
                                                                &NSM__enOnHandleUnRegisterSession,
                                                                &NSM__enOnHandleRegisterLifecycleClient,
                                                                &NSM__enOnHandleUnRegisterLifecycleClient,
                                                                &NSM__enGetApplicationMode,
                                                                &NSM__enOnHandleGetSessionState,
                                                                &NSM__enGetNodeState,
                                                                &NSM__enOnHandleSetSessionState,
                                                                &NSM__u32OnHandleGetAppHealthCount,
                                                                &NSM__u32OnHandleGetInterfaceVersion,
                                                                &NSM__vOnLifecycleRequestFinish
                                                              };

/**********************************************************************************************************************
*
* Local (static) functions
*
**********************************************************************************************************************/


/**********************************************************************************************************************
*
* This helper function is called from various places to check if a session is a "platform" session.
*
* @param  pstSession: Pointer to the session for which a check should be done, if it is a platform session
*
* @return TRUE:  The session is a "platform" session
*         FALSE: The session is not a "platform" session
*
**********************************************************************************************************************/
static gboolean NSM__boIsPlatformSession(NsmSession_s *pstSession)
{
  /* Function local variables */
  gboolean boIsPlatformSession = FALSE;
  guint16  u16SessionIdx       = 0;

  for(u16SessionIdx = 0;
         (u16SessionIdx       <  sizeof(NSM__asDefaultSessions)/sizeof(gchar*))
      && (boIsPlatformSession == FALSE);
      u16SessionIdx++)
  {
    boIsPlatformSession = (g_strcmp0(pstSession->sName, NSM__asDefaultSessions[u16SessionIdx]) == 0);
  }

  return boIsPlatformSession;
}


/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to set the NodeState.
*
* @param enNodeState:     New NodeState that should be stored.
* @param boInformBus:     Defines whether a D-Bus signal should be send when the NodeState could be changed.
* @param boInformMachine: Defines whether the StateMachine should be informed about the new NodeState.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetNodeState(NsmNodeState_e enNodeState, gboolean boInformBus, gboolean boInformMachine)
{
  /* Function local variables                                        */
  NsmErrorStatus_e enRetVal = NsmErrorStatus_NotSet; /* Return value */

  /* Check if the passed parameter is valid */
  if((enNodeState > NsmNodeState_NotSet) && (enNodeState < NsmNodeState_Last))
  {
    /* Assert that the Node not already is shut down. Otherwise it will switch of immediately */
    enRetVal = NsmErrorStatus_Ok;

    g_mutex_lock(NSM__pNodeStateMutex);

    /* Only store the new value and emit a signal, if the new value is different */
    if(NSM__enNodeState != enNodeState)
    {
      /* Store the last NodeState, before switching to the new one */
      DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed NodeState."                           ),
                                        DLT_STRING(" Old NodeState: "), DLT_INT((gint) NSM__enNodeState),
                                        DLT_STRING(" New NodeState: "), DLT_INT((gint) enNodeState     ));


      /* Store the passed NodeState and emit a signal to inform system that the NodeState changed */
      NSM__enNodeState = enNodeState;

      /* If required, inform the D-Bus about the change (send signal) */
      if(boInformBus == TRUE)
      {
        (void) NSMA_boSendNodeStateSignal(NSM__enNodeState);
      }

      /* If required, inform the StateMachine about the change */
      if(boInformMachine == TRUE)
      {
        NsmcSetData(NsmDataType_NodeState, (unsigned char*) &NSM__enNodeState,  sizeof(NsmDataType_NodeState));
      }

      /* Leave the lock now, because its not recursive. 'NSM__vCallNextLifecycleClient' may need it. */
      g_mutex_unlock(NSM__pNodeStateMutex);

      /* Check if a new life cycle request needs to be started based on the new ShutdownType */
      if(NSM__pCurrentLifecycleClient == NULL)
      {
        NSM__vCallNextLifecycleClient();
      }
    }
    else
    {
      /* NodeState stays the same. Just leave the lock. */
      g_mutex_unlock(NSM__pNodeStateMutex);
    }
  }
  else
  {
    /* Error: The passed boot mode is invalid. Return an error. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to change NodeState. Invalid parameter."),
                                       DLT_STRING(" Old NodeState: "),     DLT_INT(NSM__enNodeState    ),
                                       DLT_STRING(" Desired NodeState: "), DLT_INT((gint) enNodeState) );
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to get the NodeState.
*
* @return see NsmNodeState_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enGetNodeState(NsmNodeState_e *penNodeState)
{
  NsmErrorStatus_e enRetVal = NsmErrorStatus_NotSet;

  if(penNodeState != NULL)
  {
    enRetVal = NsmErrorStatus_Ok;

    g_mutex_lock(NSM__pNodeStateMutex);
    *penNodeState = NSM__enNodeState;
    g_mutex_unlock(NSM__pNodeStateMutex);
  }
  else
  {
    enRetVal = NsmErrorStatus_Parameter;
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to set the BootMode.
*
* @param i32BootMode:     New BootMode that should be stored.
* @param boInformBus:     Defines whether a D-Bus signal should be send when the BootMode could be changed.
* @param boInformMachine: Defines whether the StateMachine should be informed about the new BootMode.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetBootMode(const gint i32BootMode, gboolean boInformMachine)
{
  /* Function local variables */
  gint             i32CurrentBootMode = 0;
  NsmErrorStatus_e enRetVal           = NsmErrorStatus_NotSet;

  /* The BootMode property should be thread safe by D-Bus. No critical section need.  */
  (void) NSMA_boGetBootMode(&i32CurrentBootMode);
  enRetVal           = NsmErrorStatus_Ok;

  if(i32CurrentBootMode != i32BootMode)
  {
    (void) NSMA_boSetBootMode(i32BootMode);

    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed BootMode."                      ),
                                      DLT_STRING(" Old BootMode: "), DLT_INT(i32CurrentBootMode),
                                      DLT_STRING(" New BootMode: "), DLT_INT(i32BootMode       ));

    /* Inform the machine if desired. The D-Bus will auto. update, because this is property */
    if(boInformMachine == TRUE)
    {
       NsmcSetData(NsmDataType_BootMode, (unsigned char*) &i32BootMode,  sizeof(gint));
    }
  }

  /* Return ok. There is no limitation for this value. */
  return enRetVal;
}

/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to set the ApplicationMode.
*
* @param enApplicationMode: New application mode that should be stored.
* @param boInformBus:       Defines whether a D-Bus signal should be send when the ApplicationMode could be changed.
* @param boInformMachine:   Defines whether the StateMachine should be informed about the new ApplicationMode.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetApplicationMode(NsmApplicationMode_e enApplicationMode, gboolean boInformBus, gboolean boInformMachine)
{
  /* Function local variables                                        */
  NsmErrorStatus_e enRetVal = NsmErrorStatus_NotSet; /* Return value */

  /* Check if the passed parameter is valid */
  if((enApplicationMode > NsmApplicationMode_NotSet) && (enApplicationMode < NsmApplicationMode_Last))
  {
    /* The passed parameter is valid. Return OK */
    enRetVal = NsmErrorStatus_Ok;

    g_mutex_lock(NSM__pApplicationModeMutex);

    /* Only store the new value and emit a signal, if the new value is different */
    if(NSM__enApplicationMode != enApplicationMode)
    {
      /* Store new value and emit signal with new application mode */
      DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed ApplicationMode."),
                                        DLT_STRING(" Old ApplicationMode: "), DLT_INT(NSM__enApplicationMode  ),
                                        DLT_STRING(" New ApplicationMode: "), DLT_INT((gint) enApplicationMode));

      NSM__enApplicationMode = enApplicationMode;

      if(boInformBus == TRUE)
      {
        NSMA_boSendApplicationModeSignal(NSM__enApplicationMode);
      }

      if(boInformMachine == TRUE)
      {
         NsmcSetData(NsmDataType_AppMode, (unsigned char*) &NSM__enApplicationMode,  sizeof(NsmApplicationMode_e));
      }
    }

    g_mutex_unlock(NSM__pApplicationModeMutex);
  }
  else
  {
    /* Error: The passed application mode is invalid. Return an error. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to change ApplicationMode. Invalid parameter."    ),
                                       DLT_STRING(" Old ApplicationMode: "),     DLT_INT(NSM__enApplicationMode  ),
                                       DLT_STRING(" Desired ApplicationMode: "), DLT_INT((gint) enApplicationMode));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to get the ApplicationMode.
*
* @return see NsmApplicationMode_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enGetApplicationMode(NsmApplicationMode_e *penApplicationMode)
{
  NsmErrorStatus_e enRetVal = NsmErrorStatus_NotSet;

  if(penApplicationMode != NULL)
  {
    enRetVal = NsmErrorStatus_Ok;
    g_mutex_lock(NSM__pApplicationModeMutex);
    *penApplicationMode = NSM__enApplicationMode;
    g_mutex_unlock(NSM__pApplicationModeMutex);
  }
  else
  {
    enRetVal = NsmErrorStatus_Parameter;
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called from the StateMachine. There is no D-Bus interface to set the ShutdownReason,
* because it is a property.
*
* @param enNewShutdownReason: New ShutdownReason that should be stored.
* @param boInformMachine:     Determines if StateMachine needs to be called on a successful change.
*                             Most of the time this should be false, because the machine sets the
*                             value and can check the return value for errors.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetShutdownReason(NsmShutdownReason_e enNewShutdownReason, gboolean boInformMachine)
{
  /* Function local variables                                                          */
  NsmErrorStatus_e    enRetVal                = NsmErrorStatus_NotSet; /* Return value */
  NsmShutdownReason_e enCurrentShutdownReason = NsmShutdownReason_NotSet;

  /* Check if the passed parameter is valid */
  if((enNewShutdownReason > NsmShutdownReason_NotSet) && (enNewShutdownReason < NsmShutdownReason_Last))
  {
    /* The passed parameter is valid. Return OK */
    enRetVal = NsmErrorStatus_Ok;
    (void) NSMA_boGetShutdownReason(&enCurrentShutdownReason);

    /* Only store the new value and emit a signal, if the new value is different */
    if(enNewShutdownReason != enCurrentShutdownReason)
    {
      /* Store new value and emit signal with new application mode */
      DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed ShutdownReason."),
                                        DLT_STRING(" Old ShutdownReason: "), DLT_INT((gint) enCurrentShutdownReason),
                                        DLT_STRING(" New ShutdownReason: "), DLT_INT((gint) enNewShutdownReason    ));

      (void) NSMA_boSetShutdownReason(enNewShutdownReason);

      if(boInformMachine == TRUE)
      {
         NsmcSetData(NsmDataType_ShutdownReason, (unsigned char*) &enNewShutdownReason, sizeof(NsmShutdownReason_e));
      }
    }
  }
  else
  {
    /* Error: The passed application mode is invalid. Return an error. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to change ShutdownReason. Invalid parameter."          ),
                                       DLT_STRING(" Old ShutdownReason: "),     DLT_INT((gint) enCurrentShutdownReason),
                                       DLT_STRING(" Desired ShutdownReason: "), DLT_INT((gint) enNewShutdownReason    ));
  }

  return enRetVal;
}

/**********************************************************************************************************************
*
* The function is called when a session state changed. It informs the system (IPC and StateMachine) about
* the changed session state.
*
* @param pstSession:      Pointer to structure with updated session information.
* @param boInformBus:     Defines whether a D-Bus signal should be send on session change.
* @param boInformMachine: Defines whether the StateMachine should be informed about session change.
*
**********************************************************************************************************************/
static void NSM__vPublishSessionChange(NsmSession_s *pstChangedSession, gboolean boInformBus, gboolean boInformMachine)
{
  NsmErrorStatus_e enStateMachineReturn = NsmErrorStatus_NotSet;

  if(boInformBus == TRUE)
  {
    NSMA_boSendSessionSignal(pstChangedSession);
  }

  if(boInformMachine == TRUE)
  {
    enStateMachineReturn = NsmcSetData(NsmDataType_SessionState, (unsigned char*) pstChangedSession, sizeof(NsmSession_s));

    if(enStateMachineReturn != NsmErrorStatus_Ok)
    {
      DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to inform state machine about changed session state."       ),
                                         DLT_STRING(" State machine returned: "),      DLT_INT(   enStateMachineReturn       ),
                                         DLT_STRING(" Application: "),                 DLT_STRING(pstChangedSession->sOwner  ),
                                         DLT_STRING(" Session: "),                     DLT_STRING(pstChangedSession->sName   ),
                                         DLT_STRING(" Seat: "),                        DLT_INT(   pstChangedSession->enSeat  ),
                                         DLT_STRING(" Desired state: "),               DLT_INT(   pstChangedSession->enState));
    }
  }
}


/**********************************************************************************************************************
*
* The function is called when the state of a product session should be changed.
*
* @param pstSession:      Pointer to structure where session name, owner, seat and desired SessionState are defined.
* @param boInformBus:     Defines whether a D-Bus signal should be send on session change.
* @param boInformMachine: Defines whether the StateMachine should be informed about session change.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetProductSessionState(NsmSession_s *pstSession, gboolean boInformBus, gboolean boInformMachine)
{
  /* Function local variables                                         */
  NsmErrorStatus_e  enRetVal = NsmErrorStatus_NotSet; /* Return value */
  GSList           *pListEntry                   = NULL;
  NsmSession_s     *pExistingSession             = NULL;

  g_mutex_lock(NSM__pSessionMutex);

  pListEntry = g_slist_find_custom(NSM__pSessions, pstSession, &NSM__i32SessionOwnerNameSeatCompare);

  if(pListEntry != NULL)
  {
    enRetVal = NsmErrorStatus_Ok;
    pExistingSession = (NsmSession_s*) pListEntry->data;

    if(pExistingSession->enState != pstSession->enState)
    {
      pExistingSession->enState = pstSession->enState;
      NSM__vPublishSessionChange(pExistingSession, boInformBus, boInformMachine);
    }
  }
  else
  {
    enRetVal = NsmErrorStatus_WrongSession;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to set session state. Session unknown."),
                                       DLT_STRING(" Application: "),   DLT_STRING(pstSession->sOwner  ),
                                       DLT_STRING(" Session: "),       DLT_STRING(pstSession->sName   ),
                                       DLT_STRING(" Seat: "),          DLT_INT(   pstSession->enSeat  ),
                                       DLT_STRING(" Desired state: "), DLT_INT(   pstSession->enState));
  }

  g_mutex_unlock(NSM__pSessionMutex);

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called when the state of a default session should be changed.
*
* @param pstSession:      Pointer to structure where session name, owner, seat and desired SessionState are defined.
* @param boInformBus:     Defines whether a D-Bus signal should be send on session change.
* @param boInformMachine: Defines whether the StateMachine should be informed about session change.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetDefaultSessionState(NsmSession_s *pstSession, gboolean boInformBus, gboolean boInformMachine)
{
  /* Function local variables                                                  */
  NsmErrorStatus_e  enRetVal          = NsmErrorStatus_NotSet; /* Return value */
  GSList           *pListEntry        = NULL;
  NsmSession_s     *pExistingSession  = NULL;

  /* Lock the sessions to be able to change them! */
  g_mutex_lock(NSM__pSessionMutex);

  pListEntry = g_slist_find_custom(NSM__pSessions, pstSession, &NSM__i32SessionNameSeatCompare);

  if(pListEntry != NULL)
  {
    pExistingSession = (NsmSession_s*) pListEntry->data;

    /* Check that the caller owns the session */
    if(g_strcmp0(pExistingSession->sOwner, pstSession->sOwner) == 0)
    {
      enRetVal = NsmErrorStatus_Ok;

      if(pExistingSession->enState != pstSession->enState)
      {
        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed default session's state."),
                                          DLT_STRING(" Application: "), DLT_STRING(pExistingSession->sOwner ),
                                          DLT_STRING(" Session: "),     DLT_STRING(pExistingSession->sName  ),
                                          DLT_STRING(" Seat: "),        DLT_INT(   pExistingSession->enSeat ),
                                          DLT_STRING(" Old state: "),   DLT_INT(   pExistingSession->enState),
                                          DLT_STRING(" New state: "),   DLT_INT(   pstSession->enState      ));

        pExistingSession->enState = pstSession->enState;

        NSM__vPublishSessionChange(pExistingSession, boInformBus, boInformMachine);

        if(pstSession->enState == NsmSessionState_Inactive)
        {
          g_strlcpy(pExistingSession->sOwner, NSM_DEFAULT_SESSION_OWNER, sizeof(pExistingSession->sOwner));
        }
      }
    }
    else
    {
      /* The caller does not own the session. Check if he can become the owner. */
      if(g_strcmp0(pExistingSession->sOwner, NSM_DEFAULT_SESSION_OWNER) == 0)
      {
        /* The session has no owner. The new owner can obtain the session by setting it to an "active" state */
        if(pstSession->enState != NsmSessionState_Inactive)
        {
          /* The session has been activated. Overtake the owner. Broadcast new state. */
          enRetVal = NsmErrorStatus_Ok;
          g_strlcpy(pExistingSession->sOwner, pstSession->sOwner, sizeof(pExistingSession->sOwner));

          DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed default session's state."),
                                            DLT_STRING(" Application: "), DLT_STRING(pExistingSession->sOwner ),
                                            DLT_STRING(" Session: "),     DLT_STRING(pExistingSession->sName  ),
                                            DLT_STRING(" Seat: "),        DLT_INT(   pExistingSession->enSeat ),
                                            DLT_STRING(" Old state: "),   DLT_INT(   pExistingSession->enState),
                                            DLT_STRING(" New state: "),   DLT_INT(   pstSession->enState      ));

          pExistingSession->enState = pstSession->enState;

          NSM__vPublishSessionChange(pExistingSession, boInformBus, boInformMachine);
        }
        else
        {
          /* The session has no owner, but could not be activated because the passed state is "inactive". */
          enRetVal = NsmErrorStatus_Parameter;

          DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to enable default session. Passed state is 'inactive'. "),
                                             DLT_STRING(" Session: "),                DLT_STRING(pstSession->sName           ),
                                             DLT_STRING(" Seat: "),                   DLT_INT(   pstSession->enSeat          ),
                                             DLT_STRING(" Owning     application: "), DLT_STRING(pExistingSession->sOwner    ),
                                             DLT_STRING(" Requesting application: "), DLT_STRING(pstSession->sOwner          ));
        }
      }
      else
      {
        /* The session owners do not match and the existing session has an owner */
        enRetVal = NsmErrorStatus_Error;

        DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to set default session state. Session has another owner."),
                                           DLT_STRING(" Session: "),                DLT_STRING(pstSession->sName            ),
                                           DLT_STRING(" Seat: "),                   DLT_INT(   pstSession->enSeat           ),
                                           DLT_STRING(" Owning     application: "), DLT_STRING(pExistingSession->sOwner     ),
                                           DLT_STRING(" Requesting application: "), DLT_STRING(pstSession->sOwner           ));
      }
    }
  }
  else
  {
    /* This should never happen, because the function is only called for default sessions! */
    enRetVal = NsmErrorStatus_Internal;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Critical error. Default session not found in session list!"),
                                       DLT_STRING(" Application: "),   DLT_STRING(pstSession->sOwner               ),
                                       DLT_STRING(" Session: "),       DLT_STRING(pstSession->sName                ),
                                       DLT_STRING(" Seat: "),          DLT_INT(   pstSession->enSeat               ),
                                       DLT_STRING(" Desired state: "), DLT_INT(   pstSession->enState              ));
  }

  /* Unlock the sessions again. */
  g_mutex_unlock(NSM__pSessionMutex);

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to set a session state.
*
* @param pstSession:      Pointer to structure where session name, owner, seat and desired SessionState are defined.
* @param boInformBus:     Defines whether a D-Bus signal should be send on session change.
* @param boInformMachine: Defines whether the StateMachine should be informed about session change.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetSessionState(NsmSession_s *pstSession, gboolean boInformBus, gboolean boInformMachine)
{
  /* Function local variables                                           */
  NsmErrorStatus_e    enRetVal = NsmErrorStatus_NotSet; /* Return value */

  /* Check if the passed parameters are valid. */
  if(   (g_strcmp0(pstSession->sOwner, NSM_DEFAULT_SESSION_OWNER) != 0)
     && (pstSession->enState > NsmSessionState_Unregistered           )
     && (pstSession->enSeat  >  NsmSeat_NotSet                        )
     && (pstSession->enSeat  <  NsmSeat_Last                          ))
  {
    /* Parameters are valid. Check if a platform session state is set */
    if(NSM__boIsPlatformSession(pstSession) == TRUE)
    {
      enRetVal = NSM__enSetDefaultSessionState(pstSession, boInformBus, boInformMachine);
    }
    else
    {
      enRetVal = NSM__enSetProductSessionState(pstSession, boInformBus, boInformMachine);
    }
  }
  else
  {
    /* Error: An invalid parameter has been passed. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to change session state. Invalid paramter."),
                                       DLT_STRING(" Application: "),   DLT_STRING(pstSession->sOwner      ),
                                       DLT_STRING(" Session: "),       DLT_STRING(pstSession->sName       ),
                                       DLT_STRING(" Seat: "),          DLT_INT(   pstSession->enSeat      ),
                                       DLT_STRING(" Desired state: "), DLT_INT(   pstSession->enState     ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called from IPC and StateMachine to get the session state.
*
* @param pstSession: Pointer to structure where session name, owner and seat are defined and SessionState will be set.
*
* @return see NsmErrorStatus_e
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enGetSessionState(NsmSession_s *pstSession)
{
  /* Function local variables                                                                  */
  NsmErrorStatus_e    enRetVal         = NsmErrorStatus_NotSet; /* Return value                */
  NsmSession_s       *pExistingSession = NULL;                  /* Pointer to existing session */
  GSList             *pListEntry       = NULL;

  g_mutex_lock(NSM__pSessionMutex);

  /* Search for session with name, seat and owner. */
  pListEntry = g_slist_find_custom(NSM__pSessions, pstSession, &NSM__i32SessionNameSeatCompare);

  if(pListEntry != NULL)
  {
    /* Found the session in the list. Return its state. */
    enRetVal            = NsmErrorStatus_Ok;
    pExistingSession    = (NsmSession_s*) pListEntry->data;
    pstSession->enState = pExistingSession->enState;
  }
  else
  {
    /* Error: The session is unknown. */
    enRetVal = NsmErrorStatus_WrongSession;
    DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to retrieve session state. Unknown session."),
                                      DLT_STRING(" Session: "),       DLT_STRING(pstSession->sName        ),
                                      DLT_STRING(" Seat: "),          DLT_INT(   pstSession->enSeat       ));
  }

  g_mutex_unlock(NSM__pSessionMutex);

  return enRetVal;
}


static void NSM__vFreeFailedApplicationObject(gpointer pFailedApplication)
{
  /* Function local variables. Cast the passed object */
  NSM__tstFailedApplication *pstFailedApplication = (NSM__tstFailedApplication*) pFailedApplication;

  g_free(pstFailedApplication);
}


/**********************************************************************************************************************
*
* The function is called either manually for one object or for every "session object", when the list of registered
* sessions is destroyed with "g_slist_free_full". All memory occupied by the "session object" is released.
*
* @param pSession: Pointer to the session object
*
* @return void
*
**********************************************************************************************************************/
static void NSM__vFreeSessionObject(gpointer pSession)
{
  /* Function local variables. Cast the passed object */
  NsmSession_s *pstSession = (NsmSession_s*) pSession;

  /* Free the session object */
  g_free(pstSession);
}


/**********************************************************************************************************************
*
* The function is called either manually for one object or for every "lifecycle client object", when the list of
* registered lifecycle clients is destroyed with "g_slist_free_full".
* All memory occupied by the "lifecycle client object" is released.
*
* @param pLifecycleClient: Pointer to the lifecycle client object
*
* @return void
*
**********************************************************************************************************************/
static void NSM__vFreeLifecycleClientObject(gpointer pLifecycleClient)
{
  /* Function local variables. Cast the passed object */
  NSM__tstLifecycleClient *pstLifecycleClient = (NSM__tstLifecycleClient*) pLifecycleClient;

  /* Free internal strings and objects */
  g_free(pstLifecycleClient->sBusName);
  g_free(pstLifecycleClient->sObjName);

  /* No need to check for NULL. Only valid clients come here */
  NSMA_boFreeLcConsumerProxy(pstLifecycleClient->hClient);

  /* Free the shutdown client object */
  g_free(pstLifecycleClient);
}


/**********************************************************************************************************************
*
* The function is used to "custom compare" and identify a lifecycle client in the list of clients.
* Because the function is not used for sorting, the return value 1 is not used.
*
* @param pS1: Lifecycle client from list
* @param pS2: Lifecycle client to compare
*
* @return -1: pL1 < pL2
*          0: pL1 = pL2
*          1: pL1 > pL2 (unused, because function not used for sorting)
*
**********************************************************************************************************************/
static gint NSM__i32LifecycleClientCompare(gconstpointer pL1, gconstpointer pL2)
{
  /* Function local variables. Cast the passed objects */
  NSM__tstLifecycleClient *pListClient    = NULL;
  NSM__tstLifecycleClient *pCompareClient = NULL;
  gint                     i32RetVal      = 1;

  pListClient    = (NSM__tstLifecycleClient*) pL1;
  pCompareClient = (NSM__tstLifecycleClient*) pL2;

  /* Compare the bus name of the client */
  if(g_strcmp0(pListClient->sBusName, pCompareClient->sBusName) == 0)
  {
    /* Bus names are equal. Now compare object name */
    if(g_strcmp0(pListClient->sObjName, pCompareClient->sObjName) == 0)
    {
      i32RetVal = 0;  /* Clients are identical. Return 0.       */
    }
    else
    {
      i32RetVal = -1; /* Object names are different. Return -1. */
    }
  }
  else
  {
    i32RetVal = -1;   /* Bus names are different. Return -1.    */
  }

  return i32RetVal;   /* Return result of comparison.           */
}


/**********************************************************************************************************************
*
* The function is used to "custom compare" and identify a session in the list of sessions.
* It compares the "session name", the "session owner" and "seat".
* Because the function is not used for sorting, the return value 1 is not used.
*
* @param pS1: Session from list
* @param pS2: Session to compare
*
* @return -1: pS1 < pS2
*          0: pS1 = pS2
*          1: pS1 > pS2 (unused, because function not used for sorting)
*
**********************************************************************************************************************/
static gint NSM__i32SessionOwnerNameSeatCompare(gconstpointer pS1, gconstpointer pS2)
{
  /* Function local variables. Cast the passed objects */
  NsmSession_s *pListSession   = NULL;
  NsmSession_s *pSearchSession = NULL;
  gint          i32RetVal      = 1;

  pListSession   = (NsmSession_s*) pS1;
  pSearchSession = (NsmSession_s*) pS2;

  if(g_strcmp0(pListSession->sOwner,  pSearchSession->sOwner) == 0)
  {
    i32RetVal = NSM__i32SessionNameSeatCompare(pS1, pS2);
  }
  else
  {
    i32RetVal = -1;    /* Session owners differ. Return -1.  */
  }

  return i32RetVal;    /* Return result of comparison        */
}


/**********************************************************************************************************************
*
* The function is used to "custom compare" and identify a session in the list of sessions.
* It compares the "session name" and "seat".
* Because the function is not used for sorting, the return value 1 is not used.
*
* @param pS1: Session from list
* @param pS2: Session to compare
*
* @return -1: pS1 < pS2
*          0: pS1 = pS2
*          1: pS1 > pS2 (unused, because function not used for sorting)
*
**********************************************************************************************************************/
static gint NSM__i32SessionNameSeatCompare(gconstpointer pS1, gconstpointer pS2)
{
  /* Function local variables. Cast the passed objects */
  NsmSession_s *pListSession   = NULL;
  NsmSession_s *pSearchSession = NULL;
  gint          i32RetVal      = 1;

  pListSession   = (NsmSession_s*) pS1;
  pSearchSession = (NsmSession_s*) pS2;

  /* Compare seats of the sessions. */
  if(pListSession->enSeat == pSearchSession->enSeat)
  {
    /* Seats are equal. Compare session names. */
    if(g_strcmp0(pListSession->sName,  pSearchSession->sName)  == 0)
    {
      i32RetVal = 0;  /* Session are equal. Return 0.      */
    }
    else
    {
      i32RetVal = -1; /* Session names differ. Return -1.  */
    }
  }
  else
  {
    i32RetVal = -1;   /* Session seats differ. Return -1.  */
  }

  return i32RetVal;
}


/**********************************************************************************************************************
*
* The function is used to "custom compare" and identify an application name.
* Because the function is not used for sorting, the return value 1 is not used.
*
* @param pA1: Application object from list
* @param pA2: Application object to compare
*
* @return -1: pA1 < pA2
*          0: pA1 = pA2
*          1: pA1 > pA2 (unused, because function not used for sorting)
*
**********************************************************************************************************************/
static gint NSM__i32ApplicationCompare(gconstpointer pA1, gconstpointer pA2)
{
  /* Function local variables. Cast the passed objects */
  NSM__tstFailedApplication *pListApp   = NULL;
  NSM__tstFailedApplication *pSearchApp = NULL;
  gint          i32RetVal      = 1;

  pListApp   = (NSM__tstFailedApplication*) pA1;
  pSearchApp = (NSM__tstFailedApplication*) pA2;

  /* Compare names of the applications */
  if(g_strcmp0(pListApp->sName, pSearchApp->sName) == 0)
  {
    i32RetVal = 0;  /* Names are equal. Return 0.      */
  }
  else
  {
    i32RetVal = -1; /* Names are different. Return -1. */
  }

  return i32RetVal; /* Return result of comparison     */
}


/**********************************************************************************************************************
*
* The function is used to "custom compare" and identify a session with a special owner.
* Because the function is not used for sorting, the return value 1 is not used.
*
* @param pS1: Session from list
* @param pS2: Session to compare
*
* @return -1: pS1 < pS2
*          0: pS1 = pS2
*          1: pS1 > pS2 (unused, because function not used for sorting)
*
**********************************************************************************************************************/
static gint NSM__i32SessionOwnerCompare(gconstpointer pS1, gconstpointer pS2)
{
  /* Function local variables. Cast the passed objects */
  NsmSession_s *pListSession   = NULL;
  NsmSession_s *pSearchSession = NULL;
  gint          i32RetVal      = 1;

  pListSession   = (NsmSession_s*) pS1;
  pSearchSession = (NsmSession_s*) pS2;

  /* Compare owners of the sessions */
  if(g_strcmp0(pListSession->sOwner, pSearchSession->sOwner) == 0)
  {
    i32RetVal = 0;  /* Owners are equal. Return 0.      */
  }
  else
  {
    i32RetVal = -1; /* Owners are different. Return -1. */
  }

  return i32RetVal; /* Return result of comparison      */
}


/**********************************************************************************************************************
*
* The function is called after a lifecycle client was informed about the changed life cycle.
* The return value of the last informed client will be evaluated and the next lifecycle client
* to inform will be determined and called.
* If there is no client left, the lifecycle sequence will be finished.
*
* @param pSrcObject: Source object (lifecycle client proxy)
* @param pRes:       Result of asynchronous call
* @param pUserData:  Pointer to the current lifecycle client object
*
* @return void
*
**********************************************************************************************************************/
static void NSM__vOnLifecycleRequestFinish(const NsmErrorStatus_e enErrorStatus)
{
  if(enErrorStatus == NsmErrorStatus_Ok)
  {
    /* The clients "LifecycleRequest" has been successfully processed. */
	NSM__vLtProf(NSM__pCurrentLifecycleClient->sBusName, NSM__pCurrentLifecycleClient->sObjName, 0, "leave: ", 0);
    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Successfully called lifecycle client."));
  }
  else
  {
    /* Error: The method of the lifecycle client returned an error */
    NSM__vLtProf(NSM__pCurrentLifecycleClient->sBusName, NSM__pCurrentLifecycleClient->sObjName, 0, "leave: error: ", enErrorStatus);
    DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to call life cycle client."       ),
                                      DLT_STRING(" Return Value: "), DLT_INT((gint) enErrorStatus));
  }

  NSM__vCallNextLifecycleClient();
}


/**********************************************************************************************************************
*
* The function is called when:
*    - The NodeState changes (NSM__boHandleSetNodeState), to initiate a lifecycle sequence
*    - A client returned and the next client has to be called (NSM__vOnLifecycleRequestFinish)
*
* If the clients need to "run up" or shut down for the current NodeState, the function
* searches the list forward or backward until a client is found, which needs to be informed.
*
* PLEASE NOTE: If all clients have been informed about a "shut down", this function will quit the
*              "g_main_loop", which leads to the the termination of the NSM!
*
* @return void
*
**********************************************************************************************************************/
static void NSM__vCallNextLifecycleClient(void)
{
  /* Function local variables                                                                      */
  GList                   *pListEntry      = NULL;                 /* Iterate through list entries */
  NSM__tstLifecycleClient *pClient         = NULL;                 /* Client object from list      */
  guint32                  u32ShutdownType = NSM_SHUTDOWNTYPE_NOT; /* Return value                 */
  gboolean                 boShutdown      = FALSE;

  NSM__pCurrentLifecycleClient = NULL;

  g_mutex_lock(NSM__pNodeStateMutex);

  /* Based on NodeState determine if clients have to shutdown or run up. Find a client that has not been informed */
  switch(NSM__enNodeState)
  {
    /* For "shutdown" search backward in the list, until there is a client that has not been shut down */
    case NsmNodeState_ShuttingDown:
      u32ShutdownType = NSM_SHUTDOWNTYPE_NORMAL;
      for( pListEntry = g_list_last(NSM__pLifecycleClients);
          (pListEntry != NULL) && (NSM__pCurrentLifecycleClient == NULL);
          pListEntry = g_list_previous(pListEntry))
      {
        /* Check if client has not been shut down and is registered for "normal shutdown" */
        pClient = (NSM__tstLifecycleClient*) pListEntry->data;
        if(   (  pClient->boShutdown                           == FALSE)
           && ( (pClient->u32RegisteredMode & u32ShutdownType) != 0    ))
        {
          /* Found a "running" previous client, registered for the shutdown mode */
          NSM__pCurrentLifecycleClient = (NSM__tstLifecycleClient*) pListEntry->data;
        }
      }
    break;

    /* For "fast shutdown" search backward in the list, until there is a client that has not been shut down */
    case NsmNodeState_FastShutdown:
      u32ShutdownType = NSM_SHUTDOWNTYPE_FAST;
      for( pListEntry = g_list_last(NSM__pLifecycleClients);
          (pListEntry != NULL) && (NSM__pCurrentLifecycleClient == NULL);
          pListEntry = g_list_previous(pListEntry))
      {
        /* Check if client has not been shut down and is registered for "fast shutdown" */
        pClient = (NSM__tstLifecycleClient*) pListEntry->data;
        if(   (  pClient->boShutdown                           == FALSE)
           && ( (pClient->u32RegisteredMode & u32ShutdownType) != 0    ))
        {
          /* Found a "running" previous client, registered for the shutdown mode */
          NSM__pCurrentLifecycleClient = (NSM__tstLifecycleClient*) pListEntry->data;
        }
      }
    break;

    /* For a "running" mode search forward in the list (get next), until there is a client that is shut down */
    default:
      u32ShutdownType = NSM_SHUTDOWNTYPE_RUNUP;
      for(pListEntry = g_list_first(NSM__pLifecycleClients);
          (pListEntry != NULL) && (NSM__pCurrentLifecycleClient == NULL);
          pListEntry = g_list_next(pListEntry))
      {
        /* Check if client is shut down */
        pClient = (NSM__tstLifecycleClient*) pListEntry->data;
        if(pClient->boShutdown == TRUE)
        {
          /* The client was shutdown. It should run up, because we are in a running mode */
          NSM__pCurrentLifecycleClient = (NSM__tstLifecycleClient*) pListEntry->data;
        }
      }
    break;
  }

  /* Check if a client could be found that needs to be informed */
  if(NSM__pCurrentLifecycleClient != NULL)
  {
    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Call lifecycle client."                                                  ),
                                      DLT_STRING(" Bus name: "),         DLT_STRING(NSM__pCurrentLifecycleClient->sBusName      ),
                                      DLT_STRING(" Obj name: "),         DLT_STRING(NSM__pCurrentLifecycleClient->sObjName      ),
                                      DLT_STRING(" Registered types: "), DLT_INT(NSM__pCurrentLifecycleClient->u32RegisteredMode),
                                      DLT_STRING(" Client: "),           DLT_INT( (guint) NSM__pCurrentLifecycleClient->hClient ),
                                      DLT_STRING(" ShutdownType: "),     DLT_UINT(u32ShutdownType                               ));

    /* Remember that client received a run-up or shutdown call */
    pClient->boShutdown = (u32ShutdownType != NSM_SHUTDOWNTYPE_RUNUP);

    NSM__vLtProf(NSM__pCurrentLifecycleClient->sBusName, NSM__pCurrentLifecycleClient->sObjName, u32ShutdownType, "enter: ", 0);

    NSMA_boCallLcClientRequest(NSM__pCurrentLifecycleClient->hClient, u32ShutdownType);
    boShutdown = FALSE;
  }
  else
  {
    /* The last client was called. Depending on the NodeState check if we can end. */
    switch(NSM__enNodeState)
    {
      /* All registered clients have been 'fast shutdown'. Set NodeState to "shutdown" */
      case NsmNodeState_FastShutdown:
        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Informed all registered clients about 'fast shutdown'. Set NodeState to 'shutdown'"));

        NSM__enNodeState = NsmNodeState_Shutdown;
        NsmcSetData(NsmDataType_NodeState, (unsigned char*) &NSM__enNodeState, sizeof(NsmNodeState_e));
        NSMA_boSendNodeStateSignal(NSM__enNodeState);
        boShutdown = TRUE;
      break;

      /* All registered clients have been 'shutdown'. Set NodeState to "shutdown" */
      case NsmNodeState_ShuttingDown:
        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Informed all registered clients about 'shutdown'. Set NodeState to 'shutdown'."));

        NSM__enNodeState = NsmNodeState_Shutdown;
        NsmcSetData(NsmDataType_NodeState, (unsigned char*) &NSM__enNodeState, sizeof(NsmNodeState_e));
        NSMA_boSendNodeStateSignal(NSM__enNodeState);
        boShutdown = TRUE;
      break;

      /* We are in a running state. Nothing to do */
      default:
        boShutdown = FALSE;
      break;
    }
  }

  g_mutex_unlock(NSM__pNodeStateMutex);

  if(boShutdown == TRUE)
  {
    NSMA_boQuitEventLoop();
  }
}


/**********************************************************************************************************************
*
* The callback is called when a check for LUC is required.
* It uses the NodeStateMachine to determine whether LUC is required.
*
* @param pboRetVal: Pointer, where to store the StateMAchine's return value
*
**********************************************************************************************************************/
static gboolean NSM__boOnHandleCheckLucRequired(void)
{
  /* Determine if LUC is required by asking the NodeStateMachine */
  return (NsmcLucRequired() == 0x01) ? TRUE : FALSE;
}


/**********************************************************************************************************************
*
* The callback is called when the "boot mode" should be set.
* It sets the BootMode using an internal function.
*
* @param i32BootMode: New boot mode
* @param penRetVal:   Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleSetBootMode(const gint i32BootMode)
{
  /* Use internal setter to set the BootMode and inform the StateMachine */
  return NSM__enSetBootMode(i32BootMode, TRUE);
}


/**********************************************************************************************************************
*
* The callback is called when the "node state" should be set.
* It sets the NodeState using an internal function.
*
* @param enNodeStateId: New node state
* @param penRetVal:     Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleSetNodeState(const NsmNodeState_e  enNodeState)
{
  return NSM__enSetNodeState(enNodeState, TRUE, TRUE);
}


/**********************************************************************************************************************
*
* The callback is called when the "application mode" should be set.
* It sets the ApplicationMode using an internal function.
*
* @param enApplicationModeId: New application mode
* @param penRetVal:           Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleSetApplicationMode(const NsmApplicationMode_e enApplMode)
{
  return NSM__enSetApplicationMode(enApplMode, TRUE, TRUE);
}


/**********************************************************************************************************************
*
* The callback is called when the node reset is requested.
* It passes the request to the NodestateMachine.
*
* @param i32RestartReason:     Restart reason
* @param i32RestartType:       Restart type
* @param penRetVal:            Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleRequestNodeRestart(const NsmRestartReason_e enRestartReason,
                                                          const guint              u32RestartType)
{
  NsmErrorStatus_e enRetVal = NsmErrorStatus_NotSet;

  DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Node restart has been requested."));

  if(NsmcRequestNodeRestart() == 0x01)
  {
    enRetVal = NsmErrorStatus_Ok;
    (void) NSMA_boSetRestartReason(enRestartReason);
  }
  else
  {
    enRetVal = NsmErrorStatus_Error;
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The called is called when a new session should be registered.
* It checks the passed parameters and creates a NsmSession_s structure of them.
* If everything is ok, the new session will be created and the system and StateMachine will be informed.
*
* @param sSessionName:   Name of the new session
* @param sSessionOwner:  Owner of the new session
* @param enSeatId:       Seat which belongs to the new session
* @param enSessionState: Initial state of the new session
* @param penRetVal:      Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleRegisterSession(const gchar             *sSessionName,
                                                       const gchar             *sSessionOwner,
                                                       const NsmSeat_e          enSeatId,
                                                       const NsmSessionState_e  enSessionState)
{
  /* Function local variables                                                     */
  NsmSession_s     *pNewSession        = NULL;  /* Pointer to new created session */
  GSList           *pListEntry         = NULL;  /* Pointer to list entry          */
  glong             u32SessionNameLen  = 0;     /* Length of passed session owner */
  glong             u32SessionOwnerLen = 0;     /* Length of passed session name  */
  NsmSession_s      stSearchSession    = {0};   /* To search for existing session */
  gboolean          boOwnerValid       = FALSE;
  NsmErrorStatus_e  enRetVal           = NsmErrorStatus_NotSet;

  /* Check if the passed parameters are valid */
  u32SessionNameLen  = g_utf8_strlen(sSessionName,  -1);
  u32SessionOwnerLen = g_utf8_strlen(sSessionOwner, -1);
  boOwnerValid       = (g_strcmp0(sSessionOwner, NSM_DEFAULT_SESSION_OWNER) != 0);

  if(   (boOwnerValid       == TRUE                        )
     && (u32SessionNameLen  <  NSM_MAX_SESSION_NAME_LENGTH )
     && (u32SessionOwnerLen <  NSM_MAX_SESSION_OWNER_LENGTH)
     && (enSeatId           >  NsmSeat_NotSet              )
     && (enSeatId           <  NsmSeat_Last                )
     && (enSessionState     >  NsmSessionState_Unregistered))
  {
    /* Initialize temporary session object to check if session already exists */
    g_strlcpy((gchar*) stSearchSession.sName,  sSessionName,  sizeof(stSearchSession.sName) );
    g_strlcpy((gchar*) stSearchSession.sOwner, sSessionOwner, sizeof(stSearchSession.sOwner));
    stSearchSession.enSeat  = enSeatId;
    stSearchSession.enState = enSessionState;

    if(NSM__boIsPlatformSession(&stSearchSession) == FALSE)
    {
      g_mutex_lock(NSM__pSessionMutex);

      pListEntry = g_slist_find_custom(NSM__pSessions, &stSearchSession, &NSM__i32SessionNameSeatCompare);

      if(pListEntry == NULL)
      {
        enRetVal = NsmErrorStatus_Ok;

        pNewSession  = g_new0(NsmSession_s, 1);
        memcpy(pNewSession, &stSearchSession, sizeof(NsmSession_s));

        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Registered session."                        ),
                                          DLT_STRING(" Name: "         ), DLT_STRING(sSessionName      ),
                                          DLT_STRING(" Owner: "        ), DLT_STRING(sSessionOwner     ),
                                          DLT_STRING(" Seat: "         ), DLT_INT((gint) enSeatId      ),
                                          DLT_STRING(" Initial state: "), DLT_INT((gint) enSessionState));

        /* Return OK and append new object */
        NSM__pSessions = g_slist_append(NSM__pSessions, pNewSession);

        /* Inform D-Bus and StateMachine about the new session. */
        NSM__vPublishSessionChange(pNewSession, TRUE, TRUE);
      }
      else
      {
        /* Error: The session already exists. Don't store passed state. */
        enRetVal = NsmErrorStatus_WrongSession;
        DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to register session. Session already exists."),
                                          DLT_STRING(" Name: "         ), DLT_STRING(sSessionName              ),
                                          DLT_STRING(" Owner: "        ), DLT_STRING(sSessionOwner             ),
                                          DLT_STRING(" Seat: "         ), DLT_INT((gint) enSeatId              ),
                                          DLT_STRING(" Initial state: "), DLT_INT((gint) enSessionState        ));
      }

      g_mutex_unlock(NSM__pSessionMutex);
    }
    else
    {
      /* Error: It is not allowed to re-register a default session! */
      enRetVal = NsmErrorStatus_Parameter;
      DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to register session. Re-Registration of default session not allowed."),
                                         DLT_STRING(" Name: "         ), DLT_STRING(sSessionName                                      ),
                                         DLT_STRING(" Owner: "        ), DLT_STRING(sSessionOwner                                     ),
                                         DLT_STRING(" Seat: "         ), DLT_INT((gint) enSeatId                                      ),
                                         DLT_STRING(" Initial state: "), DLT_INT((gint) enSessionState                                ));
    }
  }
  else
  {
    /* Error: A parameter with an invalid value has been passed */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to register session. 'Unregistered' not allowed."),
                                       DLT_STRING(" Name: "         ), DLT_STRING(sSessionName                  ),
                                       DLT_STRING(" Owner: "        ), DLT_STRING(sSessionOwner                 ),
                                       DLT_STRING(" Seat: "         ), DLT_INT((gint) enSeatId                  ),
                                       DLT_STRING(" Initial state: "), DLT_INT((gint) enSessionState            ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The callback is called when a session should be unregistered.
* It checks the passed parameters and creates a NsmSession_s structure of them.
* If everything is ok, the new session will be removed and the system and StateMachine will be informed.
*
* @param sSessionName:  Name of the new session that should be unregistered.
* @param sSessionOwner: Current owner of the session that should be unregistered.
* @param enSeat:        Seat for which the session should be unregistered.
* @param penRetVal:     Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleUnRegisterSession(const gchar     *sSessionName,
                                                         const gchar     *sSessionOwner,
                                                         const NsmSeat_e  enSeatId)
{
  /* Function local variables                                                                   */
  NsmSession_s     *pExistingSession = NULL;                  /* Pointer to existing session    */
  GSList           *pListEntry         = NULL;                /* Pointer to list entry          */
  glong             u32SessionNameLen  = 0;                   /* Length of passed session owner */
  glong             u32SessionOwnerLen = 0;                   /* Length of passed session name  */
  NsmSession_s      stSearchSession    = {0};                 /* To search for existing session */
  NsmErrorStatus_e  enRetVal           = NsmErrorStatus_NotSet;

  /* Check if the passed parameters are valid */
  u32SessionNameLen  = g_utf8_strlen(sSessionName,  -1);
  u32SessionOwnerLen = g_utf8_strlen(sSessionOwner, -1);

  if(   (u32SessionNameLen   < NSM_MAX_SESSION_NAME_LENGTH )
     && (u32SessionOwnerLen  < NSM_MAX_SESSION_OWNER_LENGTH))
  {
    /* Assign seat, session name and owner to search for session */
    stSearchSession.enSeat = enSeatId;
    g_strlcpy((gchar*) stSearchSession.sName,  sSessionName,  sizeof(stSearchSession.sName) );
    g_strlcpy((gchar*) stSearchSession.sOwner, sSessionOwner, sizeof(stSearchSession.sOwner));

    if(NSM__boIsPlatformSession(&stSearchSession) == FALSE)
    {
      g_mutex_lock(NSM__pSessionMutex);

      pListEntry = g_slist_find_custom(NSM__pSessions, &stSearchSession, &NSM__i32SessionOwnerNameSeatCompare);

      /* Check if the session exists */
      if(pListEntry != NULL)
      {
        /* Found the session in the list. Now remove it. */
        enRetVal = NsmErrorStatus_Ok;
        pExistingSession = (NsmSession_s*) pListEntry->data;

        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Unregistered session."                          ),
                                          DLT_STRING(" Name: "      ), DLT_STRING(pExistingSession->sName  ),
                                          DLT_STRING(" Owner: "     ), DLT_STRING(pExistingSession->sOwner ),
                                          DLT_STRING(" Seat: "      ), DLT_INT(   pExistingSession->enSeat ),
                                          DLT_STRING(" Last state: "), DLT_INT(   pExistingSession->enState));

        pExistingSession->enState = NsmSessionState_Unregistered;

        /* Inform D-Bus and StateMachine about the unregistered session */
        NSM__vPublishSessionChange(pExistingSession, TRUE, TRUE);

        NSM__vFreeSessionObject(pExistingSession);
        NSM__pSessions = g_slist_remove(NSM__pSessions, pExistingSession);
      }
      else
      {
        /* Error: The session is unknown. */
        enRetVal = NsmErrorStatus_WrongSession;
        DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to unregister session. Session unknown."),
                                          DLT_STRING(" Name: "      ), DLT_STRING(sSessionName            ),
                                          DLT_STRING(" Owner: "     ), DLT_STRING(sSessionOwner           ),
                                          DLT_STRING(" Seat: "      ), DLT_INT((gint) enSeatId            ));
      }

      g_mutex_unlock(NSM__pSessionMutex);
    }
    else
    {
      /* Error: Failed to unregister session. The passed session is a "platform" session. */
      enRetVal = NsmErrorStatus_WrongSession;
      DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to unregister session. The session is a platform session."),
                                         DLT_STRING(" Name: "      ), DLT_STRING(sSessionName                              ),
                                         DLT_STRING(" Owner: "     ), DLT_STRING(sSessionOwner                             ),
                                         DLT_STRING(" Seat: "      ), DLT_INT((gint) enSeatId                              ));
    }
  }
  else
  {
    /* Error: Invalid parameter. The session or owner name is to long. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to unregister session. The session or owner name is to long."),
                                       DLT_STRING(" Name: "      ), DLT_STRING(sSessionName                                 ),
                                       DLT_STRING(" Owner: "     ), DLT_STRING(sSessionOwner                                ),
                                       DLT_STRING(" Seat: "      ), DLT_INT((gint) enSeatId                                 ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The callback is called when a lifecycle client should be registered.
* In the list of lifecycle clients it will be checked if the client already exists.
* If it exists, it's settings will be updated. Otherwise a new client will be created.
*
* @param sBusName:        Bus name of the remote application that hosts the lifecycle client interface
* @param sObjName:        Object name of the lifecycle client
* @param u32ShutdownMode: Shutdown mode for which the client wants to be informed
* @param u32TimeoutMs:    Timeout in ms. If the client does not return after the specified time, the NSM
*                         aborts its shutdown and calls the next client.
* @param penRetVal:       Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleRegisterLifecycleClient(const gchar *sBusName,
                                                               const gchar *sObjName,
                                                               const guint  u32ShutdownMode,
                                                               const guint  u32TimeoutMs)
{
  NSM__tstLifecycleClient     stTestLifecycleClient = {0};
  NSM__tstLifecycleClient    *pstNewClient          = NULL;
  NSM__tstLifecycleClient    *pstExistingClient     = NULL;
  GList                      *pListEntry            = NULL;
  NSMA_tLcConsumerHandle     *hConsumer             = NULL;
  GError                     *pError                = NULL;
  NsmErrorStatus_e            enRetVal              = NsmErrorStatus_NotSet;

  /* The parameters are valid. Create a temporary client to search the list */
  stTestLifecycleClient.sBusName = (gchar*) sBusName;
  stTestLifecycleClient.sObjName = (gchar*) sObjName;

  /* Check if the lifecycle client already is registered */
  pListEntry = g_list_find_custom(NSM__pLifecycleClients, &stTestLifecycleClient, &NSM__i32LifecycleClientCompare);

  if(pListEntry == NULL)
  {
    /* The client does not exist. Try to create a new proxy */
    hConsumer = NSMA_hCreateLcConsumer(sBusName, sObjName, u32TimeoutMs);

    /* The new proxy could be created. Create and store new client */
    if(hConsumer != NULL)
    {
      enRetVal = NsmErrorStatus_Ok;

      /* Create client object and copies of the strings. */
      pstNewClient = g_new0(NSM__tstLifecycleClient, 1);
      pstNewClient->u32RegisteredMode = u32ShutdownMode;
      pstNewClient->sBusName          = g_strdup(sBusName);
      pstNewClient->sObjName          = g_strdup(sObjName);
      pstNewClient->boShutdown        = FALSE;
      pstNewClient->hClient           = hConsumer;


      /* Append the new client to the list */
      NSM__pLifecycleClients = g_list_append(NSM__pLifecycleClients, pstNewClient);

      DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Registered new lifecycle consumer."                 ),
                                        DLT_STRING(" Bus name: "), DLT_STRING(pstNewClient->sBusName         ),
                                        DLT_STRING(" Obj name: "), DLT_STRING(pstNewClient->sObjName         ),
                                        DLT_STRING(" Timeout: " ), DLT_UINT(  u32TimeoutMs                   ),
                                        DLT_STRING(" Mode(s): "),  DLT_INT(   pstNewClient->u32RegisteredMode),
                                        DLT_STRING(" Client: "),   DLT_UINT((guint) pstNewClient->hClient    ));
    }
    else
    {
      enRetVal = NsmErrorStatus_Dbus;
      DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Failed to register new lifecycle consumer. D-Bus error."),
                                        DLT_STRING(" Bus name: "),           DLT_STRING(sBusName                 ),
                                        DLT_STRING(" Obj name: "),           DLT_STRING(sObjName                 ),
                                        DLT_STRING(" Timeout: " ),           DLT_UINT(  u32TimeoutMs             ),
                                        DLT_STRING(" Registered mode(s): "), DLT_INT(   u32ShutdownMode          ),
                                        DLT_STRING(" Error: "),              DLT_STRING(pError->message          ));

      g_error_free(pError);
    }
  }
  else
  {
    /* The client already exists. Assert to update the values for timeout and mode */
    enRetVal = NsmErrorStatus_Ok;
    pstExistingClient = (NSM__tstLifecycleClient*) pListEntry->data;
    pstExistingClient->u32RegisteredMode |= u32ShutdownMode;
    NSMA_boSetLcClientTimeout(pstExistingClient->hClient, u32TimeoutMs);

    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Changed lifecycle consumer registration."                          ),
                                      DLT_STRING(" Bus name: "),           DLT_STRING(pstExistingClient->sBusName         ),
                                      DLT_STRING(" Obj name: "),           DLT_STRING(pstExistingClient->sObjName         ),
                                      DLT_STRING(" Timeout: " ),           DLT_UINT(  u32TimeoutMs                        ),
                                      DLT_STRING(" Registered mode(s): "), DLT_INT(   pstExistingClient->u32RegisteredMode));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The callback is called when a lifecycle client should be unregistered or a shutdown
* mode should be removed. In the list of lifecycle clients will be checked if the client exists. If the
* client is found, the registration for the passed shutdown modes will be removed. If the client finally
* is not registered for any shutdown mode, its entry will be removed from the list.
*
* @param sBusName:        Bus name of the remote application that hosts the lifecycle client interface
* @param sObjName:        Object name of the lifecycle client
* @param u32ShutdownMode: Shutdown mode for which the client wants to unregister
* @param penRetVal:       Pointer, where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleUnRegisterLifecycleClient(const gchar *sBusName,
                                                                 const gchar *sObjName,
                                                                 const guint  u32ShutdownMode)
{
  NSM__tstLifecycleClient *pstExistingClient = NULL;
  NSM__tstLifecycleClient  stSearchClient    = {0};
  GList                   *pListEntry        = NULL;
  NsmErrorStatus_e         enRetVal          = NsmErrorStatus_NotSet;

  stSearchClient.sBusName = (gchar*) sBusName;
  stSearchClient.sObjName = (gchar*) sObjName;

  /* Check if the lifecycle client already is registered */
  pListEntry = g_list_find_custom(NSM__pLifecycleClients, &stSearchClient, &NSM__i32LifecycleClientCompare);

  /* Check if an existing client could be found */
  if(pListEntry != NULL)
  {
    /* The client could be found in the list. Change the registered shutdown mode */
    enRetVal = NsmErrorStatus_Ok;
    pstExistingClient = (NSM__tstLifecycleClient*) pListEntry->data;
    pstExistingClient->u32RegisteredMode &= ~(u32ShutdownMode);

    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Unregistered lifecycle consumer for mode(s)."                ),
                                      DLT_STRING(" Bus name: "),     DLT_STRING(pstExistingClient->sBusName         ),
                                      DLT_STRING(" Obj name: "),     DLT_STRING(pstExistingClient->sObjName         ),
                                      DLT_STRING(" New mode: "),     DLT_INT(   pstExistingClient->u32RegisteredMode),
                                      DLT_STRING(" Client: "  ),     DLT_UINT((guint) pstExistingClient->hClient)   );

    if(pstExistingClient->u32RegisteredMode == NSM_SHUTDOWNTYPE_NOT)
    {
      /* The client is not registered for at least one mode. Remove it from the list */
      NSM__vFreeLifecycleClientObject(pstExistingClient);
      NSM__pLifecycleClients = g_list_remove(NSM__pLifecycleClients, pstExistingClient);
    }
  }
  else
  {
    /* Warning: The client could not be found in the list of clients. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to unregister lifecycle consumer."),
                                      DLT_STRING(" Bus name: "),             DLT_STRING(sBusName),
                                      DLT_STRING(" Obj name: "),             DLT_STRING(sObjName),
                                      DLT_STRING(" Unregistered mode(s): "), DLT_INT(   u32ShutdownMode));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is used to get the state of the passed session.
* It checks the passed parameters and creates a NsmSession_s structure of them.
* If everything is ok, the state of the session will be determined and written to penSessionState.
*
* @param sSessionName:    Name  of the session whose state just be returned
* @param sSessionName:    Owner of the session whose state just be returned
* @param enSeatId:        Seat of the session
* @param penSessionState: Pointer where to store the session state
* @param penRetVal:       Pointer where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleGetSessionState(const gchar       *sSessionName,
                                                       const NsmSeat_e    enSeatId,
                                                       NsmSessionState_e *penSessionState)
{
  /* Function local variables                                                                     */
  NsmErrorStatus_e  enRetVal           = NsmErrorStatus_NotSet;
  glong             u32SessionNameLen  = 0;                     /* Length of passed session owner */
  NsmSession_s      stSearchSession    = {0};                   /* To search for existing session */

  /* Check if the passed parameters are valid */
  u32SessionNameLen = g_utf8_strlen(sSessionName, -1);

  if(u32SessionNameLen < NSM_MAX_SESSION_OWNER_LENGTH)
  {
    /* Search for session with name, seat and owner. */
    stSearchSession.enSeat = enSeatId;
    g_strlcpy((gchar*) stSearchSession.sName,  sSessionName,  sizeof(stSearchSession.sName) );

    enRetVal = NSM__enGetSessionState(&stSearchSession);
    *penSessionState = stSearchSession.enState;
  }
  else
  {
    /* Error: Invalid parameter. The session or owner name is to long. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to get session state. The session name is to long."),
                                       DLT_STRING(" Name: "      ), DLT_STRING(sSessionName                       ),
                                       DLT_STRING(" Seat: "      ), DLT_INT((gint) enSeatId                       ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function sets the state of a session to a passed value.
* It checks the passed parameters and creates a NsmSession_s structure of them.
* If everything is ok, the state of the session will be set accordingly.
*
* @param sSessionName:   Name of the session whose state just be set
* @param sSessionOwner:  Owner of the session
* @param enSeatId:       Seat of the session
* @param enSessionState: New state of the session
* @param penRetVal:      Pointer where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleSetSessionState(const gchar             *sSessionName,
                                                       const gchar             *sSessionOwner,
                                                       const NsmSeat_e          enSeatId,
                                                       const NsmSessionState_e  enSessionState)
{
  /* Function local variables                                                                       */
  NsmErrorStatus_e enRetVal           = NsmErrorStatus_NotSet;
  glong            u32SessionNameLen  = 0;            /* Length of passed session owner             */
  glong            u32SessionOwnerLen = 0;            /* Length of passed session name              */
  NsmSession_s     stSession          = {0};          /* Session object passed to internal function */

  /* Check if the passed parameters are valid */
  u32SessionNameLen  = g_utf8_strlen(sSessionName,  -1);
  u32SessionOwnerLen = g_utf8_strlen(sSessionOwner, -1);

  if(   (u32SessionNameLen  < NSM_MAX_SESSION_NAME_LENGTH )
     && (u32SessionOwnerLen < NSM_MAX_SESSION_OWNER_LENGTH))
  {
    /* Build session object to pass it internally */
    g_strlcpy((gchar*) stSession.sName,  sSessionName,  sizeof(stSession.sName) );
    g_strlcpy((gchar*) stSession.sOwner, sSessionOwner, sizeof(stSession.sOwner));

    stSession.enSeat  = enSeatId;
    stSession.enState = enSessionState;

    enRetVal = NSM__enSetSessionState(&stSession, TRUE, TRUE);
  }
  else
  {
    /* Error: Invalid parameter. The session or owner name is to long. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to set session state. Invalid parameter."),
                                       DLT_STRING(" Name: "      ), DLT_STRING(sSessionName             ),
                                       DLT_STRING(" Owner: "     ), DLT_STRING(sSessionOwner            ),
                                       DLT_STRING(" Seat: "      ), DLT_INT((gint) enSeatId             ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The helper function is called by 'NSM__boOnHandleSetAppHealthStatus', when an application became valid again.
* It removes the application from the list of invalid apps.
*
* @param pstFailedApp: Pointer to structure with information about the failed application.
*
* @return NsmErrorStatus_Ok:           The application has been removed from the list of failed apps.
*         NsmErrorStatus_WrongSession: The application has never been on the list of failed apps.
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetAppStateValid(NSM__tstFailedApplication* pstFailedApp)
{
  /* Function local variables                                                                             */
  GSList                    *pAppListEntry          = NULL;                  /* List entry of application */
  NsmErrorStatus_e           enRetVal               = NsmErrorStatus_NotSet; /* Return value              */
  NSM__tstFailedApplication *pstExistingApplication = NULL;

  /* An application has become valid again. Check if it really was invalid before. */
  pAppListEntry = g_slist_find_custom(NSM__pFailedApplications, pstFailedApp, &NSM__i32ApplicationCompare);

  if(pAppListEntry != NULL)
  {
    /* We found at least one entry for the application. Remove it from the list */
    enRetVal = NsmErrorStatus_Ok;
    pstExistingApplication = (NSM__tstFailedApplication*) pAppListEntry->data;
    NSM__pFailedApplications = g_slist_remove(NSM__pFailedApplications, pstExistingApplication);
    NSM__vFreeFailedApplicationObject(pstExistingApplication);

    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: An application has become valid again."    ),
                                      DLT_STRING(" Application: "), DLT_STRING(pstFailedApp->sName));
  }
  else
  {
    /* Error: There was no session registered for the application that failed. */
    enRetVal = NsmErrorStatus_Error;
    DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: Failed to set application valid. Application was never invalid."),
                                      DLT_STRING(" Application: "), DLT_STRING(pstFailedApp->sName                     ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The helper function is called by 'NSM__enSetAppStateFailed', when an application failed.
* It looks for sessions that have been registered by the app.
*
* @param pstFailedApp: Pointer to structure with information about the failed application.
*
**********************************************************************************************************************/
static void NSM__vDisableSessionsForApp(NSM__tstFailedApplication* pstFailedApp)
{
  /* Function local variables */
  GSList       *pSessionListEntry  = NULL;
  NsmSession_s *pstExistingSession = NULL;
  NsmSession_s  stSearchSession    = {0};

  /* Only set the "owner" of the session (to the AppName) to search for all sessions of the app. */
  g_strlcpy(stSearchSession.sOwner, pstFailedApp->sName, sizeof(stSearchSession.sOwner));

  g_mutex_lock(NSM__pSessionMutex);
  pSessionListEntry = g_slist_find_custom(NSM__pSessions, &stSearchSession, &NSM__i32SessionOwnerCompare);

  if(pSessionListEntry != NULL)
  {
    /* Found at least one session. */
    do
    {
      /* Get the session object for the list entry */
      pstExistingSession = (NsmSession_s*) pSessionListEntry->data;
      pstExistingSession->enState = NsmSessionState_Unregistered;

      /* Inform D-Bus and StateMachine that a session became invalid */
      NSM__vPublishSessionChange(pstExistingSession, TRUE, TRUE);

      DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: A session has become invalid, because an application failed."),
                                        DLT_STRING(" Application: "), DLT_STRING(pstExistingSession->sOwner           ),
                                        DLT_STRING(" Session: "),     DLT_STRING(pstExistingSession->sName            ),
                                        DLT_STRING(" Seat: "),        DLT_INT(   pstExistingSession->enSeat           ),
                                        DLT_STRING(" State: "),       DLT_INT(   pstExistingSession->enState          ));

      /* Remove or "reset" session */
      if(NSM__boIsPlatformSession(pstExistingSession) == TRUE)
      {
        /* It is a default session. Don't remove it. Set owner to NSM again. */
        g_strlcpy(pstExistingSession->sOwner, NSM_DEFAULT_SESSION_OWNER, sizeof(pstExistingSession->sOwner));
      }
      else
      {
        /* The session has been registered by a failed app. Remove it. */
        NSM__pSessions = g_slist_remove(NSM__pSessions, pstExistingSession);
        NSM__vFreeSessionObject(pstExistingSession);
      }

      /* Try to find the next session that had been registered for the app. */
      pSessionListEntry = g_slist_find_custom(NSM__pSessions, &stSearchSession, &NSM__i32SessionOwnerCompare);

    } while(pSessionListEntry != NULL);
  }
  else
  {
    /* There have been no session registered for this application. */
    DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: There had been no registered sessions."    ),
                                      DLT_STRING(" Application: "), DLT_STRING(pstFailedApp->sName));
  }

  g_mutex_unlock(NSM__pSessionMutex);
}


/**********************************************************************************************************************
*
* The helper function is called by 'NSM__boOnHandleSetAppHealthStatus', when an application failed.
*
* @param pstFailedApp: Pointer to structure with information about the failed application.
*
* @return always "NsmErrorStatus_Ok"
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enSetAppStateFailed(NSM__tstFailedApplication* pstFailedApp)
{
  /* Function local variables                                                                            */
  GSList                    *pFailedAppListEntry   = NULL;                  /* List entry of application */
  NsmErrorStatus_e           enRetVal              = NsmErrorStatus_NotSet; /* Return value              */
  NSM__tstFailedApplication *pstFailedApplication  = NULL;

  /* An application failed. Check if the application already is known as 'failed'. */
  pFailedAppListEntry = g_slist_find_custom(NSM__pFailedApplications, pstFailedApp, &NSM__i32ApplicationCompare);

  if(pFailedAppListEntry == NULL)
  {
    /* The application is not on the list yet. Create it. */
    enRetVal = NsmErrorStatus_Ok;

    pstFailedApplication  = g_new(NSM__tstFailedApplication, 1);
    g_strlcpy(pstFailedApplication->sName, pstFailedApp->sName, sizeof(pstFailedApplication->sName));
    NSM__pFailedApplications = g_slist_append(NSM__pFailedApplications, pstFailedApplication);

    /* Disable all session that have been registered by the application */
    NSM__vDisableSessionsForApp(pstFailedApplication);
  }
  else
  {
    /* Warning: The application is already in the list of failed session. */
    enRetVal = NsmErrorStatus_Ok;
    DLT_LOG(NsmContext, DLT_LOG_WARN, DLT_STRING("NSM: The application has already been marked as 'failed'."),
                                      DLT_STRING(" Application: "), DLT_STRING(pstFailedApp->sName          ));
  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function is called when an application has become invalid or valid again.
* If an application became inactive, it will be added to the list of failed applications
* and signals for the session registered by the application will be emitted.
* If an application became valid again, it will only be removed from the list of failed sessions.
*
* @param sAppName:   Application which changed its state.
* @param boAppState: Indicates if the application became invalid or valid again.
* @param penRetVal:  Pointer where to store the return value
*
**********************************************************************************************************************/
static NsmErrorStatus_e NSM__enOnHandleSetAppHealthStatus(const gchar    *sAppName,
                                                          const gboolean  boAppState)
{
  /* Function local variables                                                                     */
  NSM__tstFailedApplication stSearchApplication = {0}; /* Temporary application object for search */
  NsmErrorStatus_e          enRetVal            = NsmErrorStatus_NotSet;

  /* Check if passed parameters are valid */
  if(strlen(sAppName) < NSM_MAX_SESSION_OWNER_LENGTH)
  {
    /* The application name is valid. Copy it for further checks. */
    g_strlcpy((gchar*) stSearchApplication.sName, sAppName, sizeof(stSearchApplication.sName));

    if(boAppState == TRUE)
    {
      enRetVal = NSM__enSetAppStateValid(&stSearchApplication);
    }
    else
    {
      enRetVal = NSM__enSetAppStateFailed(&stSearchApplication);
    }
  }
  else
  {
    /* Error: The passed application name is too long. */
    enRetVal = NsmErrorStatus_Parameter;
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Failed to set application health status. The application name is too long."),
                                       DLT_STRING(" Owner: "     ), DLT_STRING(sAppName                                            ),
                                       DLT_STRING(" State: "     ), DLT_INT(boAppState                                             ));

  }

  return enRetVal;
}


/**********************************************************************************************************************
*
* The function returns the current AppHealthCount, which is stored in local variable.
*
* @param pu32AppHealthCount: Pointer where to store the AppHealthCount (number of failed applications).
*
**********************************************************************************************************************/
static guint NSM__u32OnHandleGetAppHealthCount(void)
{
  return g_slist_length(NSM__pFailedApplications);
}


/**********************************************************************************************************************
*
* The function returns the current interface version of the NodeStateManager.
*
* @param pu32InterfaceVersion: Pointer where to store the interface version.
*
**********************************************************************************************************************/
static guint NSM__u32OnHandleGetInterfaceVersion(void)
{
  /* Return interface version to caller. */
  return NSM_INTERFACE_VERSION;
}


/**********************************************************************************************************************
*
* The function initializes all file local variables
*
**********************************************************************************************************************/
static void  NSM__vInitializeVariables(void)
{
  /* Initialize file local variables */
  NSM__pSessionMutex           = NULL;
  NSM__pSessions               = NULL;
  NSM__pLifecycleClients       = NULL;
  NSM__pNodeStateMutex         = NULL;
  NSM__enNodeState             = NsmNodeState_NotSet;
  NSM__pApplicationModeMutex   = NULL;
  NSM__enApplicationMode       = NsmApplicationMode_NotSet;
  NSM__pFailedApplications     = NULL;
  NSM__pCurrentLifecycleClient = NULL;
}


/**********************************************************************************************************************
*
* The function creates the platform sessions, configured in "NSM__asDefaultSessions".
*
**********************************************************************************************************************/
static void  NSM__vCreatePlatformSessions(void)
{
  NsmSession_s *pNewDefaultSession   = NULL;
  guint         u32DefaultSessionIdx = 0;
  NsmSeat_e     enSeatIdx            = NsmSeat_NotSet;

  /* Configure the default sessions, which are always available */
  for(u32DefaultSessionIdx = 0;
      u32DefaultSessionIdx < sizeof(NSM__asDefaultSessions)/sizeof(gchar*);
      u32DefaultSessionIdx++)
  {
    /* Create a session for every session name and seat */
    for(enSeatIdx = NsmSeat_NotSet + 1; enSeatIdx < NsmSeat_Last; enSeatIdx++)
    {
      pNewDefaultSession          = g_new0(NsmSession_s, 1);
      g_strlcpy((gchar*) pNewDefaultSession->sName,  NSM__asDefaultSessions[u32DefaultSessionIdx], sizeof(pNewDefaultSession->sName));
      g_strlcpy((gchar*) pNewDefaultSession->sOwner, NSM_DEFAULT_SESSION_OWNER, sizeof(pNewDefaultSession->sOwner));
      pNewDefaultSession->enSeat  = enSeatIdx;
      pNewDefaultSession->enState = NsmSessionState_Inactive;

      NSM__pSessions = g_slist_append(NSM__pSessions, pNewDefaultSession);
    }
  }
}


/**********************************************************************************************************************
*
* The function creates the mutexes used in the NSM.
*
**********************************************************************************************************************/
static void NSM__vCreateMutexes(void)
{
  /* Initialize the local mutexes */
  NSM__pNodeStateMutex       = g_mutex_new();
  NSM__pApplicationModeMutex = g_mutex_new();
  NSM__pSessionMutex         = g_mutex_new();
}


/**********************************************************************************************************************
*
* The function deletes the mutexes used in the NSM.
*
**********************************************************************************************************************/
static void NSM__vDeleteMutexes(void)
{
  /* Delete the local mutexes */
  g_mutex_free(NSM__pNodeStateMutex);
  g_mutex_free(NSM__pApplicationModeMutex);
  g_mutex_free(NSM__pSessionMutex);
}


/**********************************************************************************************************************
*
* The function is called to trace a syslog message for a shutdown client.
*
* @param sBus:          Bus name of the shutdown client.
* @param sObj:          Object name of the lifecycle client.
* @param u32Reason:     Shutdown reason send to the client.
* @param sInOut:        "enter" or "leave" (including failure reason)
* @param enErrorStatus: Error value
*
**********************************************************************************************************************/
static void NSM__vLtProf(gchar *sBus, gchar *sObj, guint32 u32Reason, gchar *sInOut, NsmErrorStatus_e enErrorStatus)
{
    gchar pszLtprof[128] = "LTPROF: bus:%s obj:%s (0x%08X:%d) ";
    guint32 dwLength = 128;

    g_strlcat(pszLtprof, sInOut, dwLength);

    if(u32Reason != 0)
    {
        if(u32Reason == NSM_SHUTDOWNTYPE_RUNUP)
        {
          g_strlcat(pszLtprof, "runup", dwLength);
        }
        else
        {
          g_strlcat(pszLtprof, "shutdown", dwLength);
        }
    }

    syslog(LOG_NOTICE, (char *)pszLtprof, sBus, sObj, u32Reason, enErrorStatus);
}


/**********************************************************************************************************************
*
* The function is used to initialize syslog
*
**********************************************************************************************************************/
static void NSM__vSyslogOpen(void)
{
  openlog("NSM", LOG_PID, LOG_USER);
}


/**********************************************************************************************************************
*
* The function is used to deinitialize syslog
*
**********************************************************************************************************************/
static void NSM__vSyslogClose(void)
{
  closelog();
}


/**********************************************************************************************************************
*
* Interfaces. Exported functions. See Header for detailed description.
*
**********************************************************************************************************************/


/* The function is called by the NodeStateMachine to set a "property" of the NSM. */
NsmErrorStatus_e NsmSetData(NsmDataType_e enData, unsigned char *pData, unsigned int u32DataLen)
{
  /* Function local variables                                        */
  NsmErrorStatus_e enRetVal = NsmErrorStatus_NotSet; /* Return value */

  /* Check which data the NSMC wants to set */
  switch(enData)
  {
    /* NSMC wants to set the NodeState */
    case NsmDataType_NodeState:
      enRetVal =   (u32DataLen == sizeof(NsmNodeState_e))
                 ? NSM__enSetNodeState((NsmNodeState_e) *pData, TRUE, FALSE)
                 : NsmErrorStatus_Parameter;
    break;

    /* NSMC wants to set the AppMode */
    case NsmDataType_AppMode:
      enRetVal =   (u32DataLen == sizeof(NsmApplicationMode_e))
                 ? NSM__enSetApplicationMode((NsmApplicationMode_e) *pData, TRUE, FALSE)
                 : NsmErrorStatus_Parameter;
    break;

    /* NSMC wants to set the BootMode */
    case NsmDataType_BootMode:
      enRetVal =   (u32DataLen == sizeof(gint))
                 ? NSM__enSetBootMode((gint) *pData, FALSE)
                 : NsmErrorStatus_Parameter;
    break;

    /* NSMC wants to set the ShutdownReason */
    case NsmDataType_ShutdownReason:
      enRetVal =   (u32DataLen == sizeof(NsmShutdownReason_e))
                 ? NSM__enSetShutdownReason((NsmShutdownReason_e) *pData, FALSE)
                 : NsmErrorStatus_Parameter;
    break;

    /* NSMC wants to set a SessionState */
    case NsmDataType_SessionState:
      enRetVal =   (u32DataLen == sizeof(NsmSession_s))
                 ? NSM__enSetSessionState((NsmSession_s*) pData, TRUE, FALSE)
                 : NsmErrorStatus_Parameter;
    break;

    /* Error: The type of the data NSMC is trying to set is unknown or the data is read only! */
    case NsmDataType_RestartReason:
    case NsmDataType_RunningReason:
    default:
      enRetVal = NsmErrorStatus_Parameter;
    break;
  }

  return enRetVal;
}


/* The function is called by the NodeStateMachine to get a "property" of the NSM. */
int NsmGetData(NsmDataType_e enData, unsigned char *pData, unsigned int u32DataLen)
{
  /* Function local variables                                             */
  int i32RetVal = -1; /* Return value. Positive: Amount of written bytes.
                                       Negative: An error occurred.       */

  /* Check which data the NSMC wants to get */
  switch(enData)
  {
    /* NSMC wants to get the NodeState */
    case NsmDataType_NodeState:
      if(u32DataLen == sizeof(NsmNodeState_e))
      {
        if(NSM__enGetNodeState((NsmNodeState_e*) pData) == NsmErrorStatus_Ok)
        {
          i32RetVal = sizeof(NsmNodeState_e);
        }
      }
    break;

    /* NSMC wants to get the ApplicationMode */
    case NsmDataType_AppMode:
      if(u32DataLen == sizeof(NsmApplicationMode_e))
      {
        if(NSM__enGetApplicationMode((NsmApplicationMode_e*) pData) == NsmErrorStatus_Ok)
        {
          i32RetVal = sizeof(NsmApplicationMode_e);
        }
      }
    break;

    /* NSMC wants to get the BootMode */
    case NsmDataType_BootMode:
      if(u32DataLen == sizeof(gint))
      {
        if(NSMA_boGetBootMode((gint*) pData) == TRUE)
        {
          i32RetVal = sizeof(gint);
        }
      }
    break;

    /* NSMC wants to get the RunningReason */
    case NsmDataType_RunningReason:
      if(u32DataLen == sizeof(NsmRunningReason_e))
      {
        if(NSMA_boGetRunningReason((NsmRunningReason_e*) pData) == TRUE)
        {
          i32RetVal = sizeof(NsmRunningReason_e);
        }
      }
    break;

    /* NSMC wants to get the ShutdownReason */
    case NsmDataType_ShutdownReason:
      if(u32DataLen == sizeof(NsmShutdownReason_e))
      {
        if(NSMA_boGetShutdownReason((NsmShutdownReason_e*) pData) == TRUE)
        {
          i32RetVal = sizeof(NsmShutdownReason_e);
        }
      }
    break;

    /* NSMC wants to get the RestartReason */
    case NsmDataType_RestartReason:
      if(u32DataLen == sizeof(NsmRestartReason_e))
      {
        if(NSMA_boGetRestartReason((NsmRestartReason_e*) pData) == TRUE)
        {
          i32RetVal = sizeof(NsmRestartReason_e);
        }
      }
    break;

    /* NSMC wants to get the SessionState */
    case NsmDataType_SessionState:
      if(u32DataLen == sizeof(NsmSession_s))
      {
        if(NSM__enGetSessionState((NsmSession_s*) pData) == NsmErrorStatus_Ok)
        {
          i32RetVal = sizeof(NsmSession_s);
        }
      }
    break;

    /* Error: The type of the data NSMC is trying to set is unknown. */
    default:
      i32RetVal = -1;
    break;
  }

  return i32RetVal;
}


unsigned int NsmGetInterfaceVersion(void)
{
	return NSM_INTERFACE_VERSION;
}


/* The main function of the NodeStateManager */
int main(void)
{
  gboolean boEndByUser = FALSE;

  /* Initialize glib for using "g" types */
  g_type_init();

  /* Register NSM for DLT */
  DLT_REGISTER_APP("NSM", "Node State Manager");
  DLT_REGISTER_CONTEXT(NsmContext, "005", "Context for the NSM");
  DLT_ENABLE_LOCAL_PRINT();

  /* Initialize syslog */
  NSM__vSyslogOpen();

  /* Print first msg. to show that NSM is going to start */
  DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: NodeStateManager started."));

  /* Currently no other resources accessing the NSM. Prepare it now! */
  NSM__vInitializeVariables();     /* Initialize file local variables*/
  NSM__vCreatePlatformSessions();  /* Create platform sessions       */
  NSM__vCreateMutexes();           /* Create mutexes                 */

  /* Initialize the NSMA before the NSMC, because the NSMC can access properties */
  if(NSMA_boInit(&NSM__stObjectCallBacks) == TRUE)
  {
    /* Set the properties to initial values */
    (void) NSMA_boSetBootMode(0);
    (void) NSMA_boSetRestartReason(NsmRestartReason_NotSet);
    (void) NSMA_boSetShutdownReason(NsmShutdownReason_NotSet);
    (void) NSMA_boSetRunningReason(NsmRunningReason_WakeupCan);

    /* Initialize/start the NSMC */
    if(NsmcInit() == 0x01)
    {
      /* The event loop is only canceled if the Node is completely shut down or there is an internal error. */
      boEndByUser = NSMA_boWaitForEvents();

      if(boEndByUser == TRUE)
      {
        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Successfully canceled event loop. "),
                                          DLT_STRING("Shutting down NodeStateManager."        ));
      }
      else
      {
        DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: Error in event loop. "     ),
                                          DLT_STRING("Shutting down NodeStateManager."));
      }

      /* The event loop returned. Clean up the NSMA. */
      (void) NSMA_boDeInit();
    }
    else
    {
      /* Error: Failed to initialize the NSMC. Clean up NSMA, because it is not needed anymore. */
      (void) NSMA_boDeInit();
      DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Error. Failed to initialize the NSMC."));
    }
  }
  else
  {
    /* Error: Failed to initialize the NSMA. */
    DLT_LOG(NsmContext, DLT_LOG_ERROR, DLT_STRING("NSM: Error. Failed to initialize the NSMA."));
  }

  /* Free the mutexes */
  NSM__vDeleteMutexes();

  /* Remove data from all lists */
  g_slist_free_full(NSM__pSessions,           &NSM__vFreeSessionObject);
  g_slist_free_full(NSM__pFailedApplications, &NSM__vFreeFailedApplicationObject);
  g_list_free_full (NSM__pLifecycleClients,   &NSM__vFreeLifecycleClientObject);

  DLT_LOG(NsmContext, DLT_LOG_INFO, DLT_STRING("NSM: NodeStateManager stopped."));

  /* Deinit syslog */
  NSM__vSyslogClose();

  /* Unregister NSM from DLT */
  DLT_UNREGISTER_CONTEXT(NsmContext);
  DLT_UNREGISTER_APP();

  return 0;
}