summaryrefslogtreecommitdiff
path: root/NodeStateAccess/NodeStateAccess.cpp
blob: efd2c62e386c2dfbb0cd85d3909b91516c0a10f7 (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
/**********************************************************************************************************************
 *
 * Copyright (C) 2017 BMW AG
 *
 * Interface between NodeStateManager and IPC
 *
 * This source file is a part of the NodeStateAccess library (NSMA).
 * The architecture requires that the NodeStateManager (NSM) is independent from the CommonAPI binding.
 * Therefore, the CommonAPI communication is handled inside of this library.
 * The library offers the NSM an interface to use objects generated via CommonAPI.
 *
 * 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/.
 *
 **********************************************************************************************************************/

/**********************************************************************************************************************
 *
 * Header includes
 *
 **********************************************************************************************************************/

/* generic includes for the NodeStateAccess library        */
#include "NodeStateAccess.h" /* own header                 */
#include "NodeStateAccess.hpp"
#include "NodeStateTypes.h"  /* Type defintions of the NSM */

#include <algorithm>
#include <csignal>
#include <cstdio>
#include <iostream>
#include <dlt.h>
#include <string.h>

#ifdef COVERAGE_ENABLED
extern "C" void __gcov_flush();
#endif

#include "NodeStateConsumer_StubImpl.h"
#include "NodeStateLifecycleControl_StubImpl.h"
#include "SimpleTimer.h"

#include <v1/org/genivi/nodestatemanager/ConsumerInstanceIds.hpp>
#include <v1/org/genivi/nodestatemanager/LifecycleControlInstanceIds.hpp>

namespace GENIVI = v1::org::genivi;
using namespace GENIVI::nodestatemanager;

// CommonAPI connection ID
const char* gConnectionID = "NSMimpl";
const char* gCapiDomain = "local";

DLT_IMPORT_CONTEXT(NsmaContext);

/**********************************************************************************************************************
 *
 * Local variables
 *
 **********************************************************************************************************************/

static gboolean NSMA__boLoopEndByUser = FALSE;
static gboolean NSMA__boInitialized = FALSE;

/* Structure with callback functions to the NSM */
static NSMA_tstObjectCallbacks NSMA__stObjectCallbacks = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

static std::shared_ptr<NodeStateConsumer_StubImpl> NSMA_consumerService = NULL;
static std::shared_ptr<NodeStateLifecycleControl_StubImpl> NSMA_lifecycleControlService = NULL;

static std::unordered_map<std::size_t, std::shared_ptr<SimpleTimer>> NSMA_clParallelShutdownPendingReceivers;

static std::shared_ptr<CommonAPI::Runtime> NSMA_runtime = NULL;

static std::shared_ptr<SimpleTimer> NSMA_simpleTimer = NULL;

static size_t NSMA_currentConsumer = 0;

static std::mutex NSMA__mutexParallelShutdown;
static std::mutex NSMA__mutexWaitForEvents;
static std::mutex NSMA__mutex;
static std::condition_variable NSMA__condVarShutdown;

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

/**********************************************************************************************************************
*
* The function is called when the async. call to a life cycle clients "LifecycleRequest" method timed out.
*
**********************************************************************************************************************/
static void NSMA__boHandleSequentialRequestTimeout()
{
   std::unique_lock<std::mutex> lock(NSMA__mutexParallelShutdown);
   if (NULL != NSMA_consumerService)
   {
      DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Error! Sequential client has timed out! Client ID:"), DLT_UINT64(NSMA_currentConsumer));

      size_t oldConsumer = NSMA_currentConsumer;
      NSMA_currentConsumer = 0;

      lock.unlock();
      NSMA__stObjectCallbacks.pfLcClientRequestFinish(oldConsumer, TRUE, FALSE);
   }
}

/**********************************************************************************************************************
*
* The function is called when the async. call to a parallel life cycle clients "LifecycleRequest" method timed out.
*
**********************************************************************************************************************/
static void NSMA__boHandleParallelRequestTimeout(std::size_t client)
{
   std::unique_lock<std::mutex> lock(NSMA__mutexParallelShutdown);

   /* Find timed out client in parallel shutdown receivers list */
   auto search = NSMA_clParallelShutdownPendingReceivers.find(client);
   if(NULL != NSMA_consumerService && search != NSMA_clParallelShutdownPendingReceivers.end())
   {
      DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Error! Parallel client has timed out! Client ID:"), DLT_UINT64(client));

      /* Remove this client from the list of pending clients */
      NSMA_clParallelShutdownPendingReceivers.erase(search);

      lock.unlock();
      NSMA__stObjectCallbacks.pfLcClientRequestFinish(client, TRUE, FALSE);
   }
}

/**********************************************************************************************************************
 *
 * Interfaces. Exported functions. See Header for detailed description.
 *
 **********************************************************************************************************************/
gboolean NSMA_boInit(const NSMA_tstObjectCallbacks *pstCallbacks)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   /* Initialize file local variables */
   NSMA__boInitialized = FALSE;

   memset(&NSMA__stObjectCallbacks, 0, sizeof(NSMA_tstObjectCallbacks));
   std::lock_guard<std::mutex> lock(NSMA__mutex);

   /* Check if all callbacks for the NSM have been configured. */
   if(   pstCallbacks != NULL
     && (pstCallbacks->pfSetBootModeCb               != NULL)
     && (pstCallbacks->pfRequestNodeRestartCb        != NULL)
     && (pstCallbacks->pfSetAppHealthStatusCb        != NULL)
     && (pstCallbacks->pfCheckLucRequiredCb          != NULL)
     && (pstCallbacks->pfRegisterSessionCb           != NULL)
     && (pstCallbacks->pfUnRegisterSessionCb         != NULL)
     && (pstCallbacks->pfRegisterLifecycleClientCb   != NULL)
     && (pstCallbacks->pfUnRegisterLifecycleClientCb != NULL)
     && (pstCallbacks->pfGetSessionStateCb           != NULL)
     && (pstCallbacks->pfGetNodeStateCb              != NULL)
     && (pstCallbacks->pfSetNodeStateCb              != NULL)
     && (pstCallbacks->pfSetSessionStateCb           != NULL)
     && (pstCallbacks->pfGetAppHealthCountCb         != NULL)
     && (pstCallbacks->pfGetInterfaceVersionCb       != NULL)
     && (pstCallbacks->pfLcClientRequestFinish       != NULL))
   {
      /* Store the passed callbacks. */
      memcpy(&NSMA__stObjectCallbacks, pstCallbacks, sizeof(NSMA_tstObjectCallbacks));

      if (NULL == (NSMA_runtime = CommonAPI::Runtime::get()))
      {
         DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Failed to get CAPI Runtime!"));
      }
      else if(NULL == (NSMA_consumerService = std::make_shared<NodeStateConsumer_StubImpl>(&NSMA__stObjectCallbacks)))
      {
         DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Failed to create NSMA_consumerService"));
      }
      else if (NULL == (NSMA_lifecycleControlService = std::make_shared<NodeStateLifecycleControl_StubImpl>(&NSMA__stObjectCallbacks)))
      {
         DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Failed to create NSMA_lifecycleControlService"));
      }
      else if (!NSMA_runtime->registerService(gCapiDomain, v1::org::genivi::nodestatemanager::Consumer_INSTANCES[0], NSMA_consumerService, gConnectionID))
      {
         DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Failed to register NSMConsumer"));
      }
      else if (!NSMA_runtime->registerService(gCapiDomain, v1::org::genivi::nodestatemanager::LifecycleControl_INSTANCES[0], NSMA_lifecycleControlService, "LifecycleControl"))
      {
         DLT_LOG(NsmaContext, DLT_LOG_ERROR, DLT_STRING("NSMA: Failed to register NSMLifecycleControl"));
      }
      else
      {
         DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Successfully registered NSMA"));
         /* All callbacks are configured. */
         NSMA__boInitialized = TRUE;
      }
   }
   else
   {
      /* Error: Callbacks are configured incorrectly. */
      NSMA__boInitialized = FALSE;
   }

   return NSMA__boInitialized;
}

void signal_handler(int signal)
{
   DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Received signal"), DLT_INT(signal));
   std::unique_lock<std::mutex> lock(NSMA__mutexWaitForEvents);
   NSMA__boLoopEndByUser = TRUE;
   lock.unlock();
   NSMA__condVarShutdown.notify_all();
}

gboolean NSMA_boWaitForEvents(void)
{
   std::signal(SIGTERM, signal_handler);
   std::signal(SIGINT, signal_handler);

   std::unique_lock<std::mutex> lock(NSMA__mutexWaitForEvents);
   while (FALSE == NSMA__boLoopEndByUser)
   {
      NSMTriggerWatchdog(NsmWatchdogState_Sleep);
      NSMA__condVarShutdown.wait(lock);
      NSMTriggerWatchdog(NsmWatchdogState_Active);
   }

   /* Delete signal handlers */
   signal(SIGTERM, SIG_DFL);
   signal(SIGINT, SIG_DFL);
   lock.unlock();
   std::fflush(stdout);
   std::fflush(stderr);

#ifdef COVERAGE_ENABLED
   __gcov_flush();
#endif

   return TRUE;
}

gboolean NSMA_boSendNodeStateSignal(const NsmNodeState_e enNodeState)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->fireNodeStateEvent(GENIVI::NodeStateManagerTypes::NsmNodeState_e((GENIVI::NodeStateManagerTypes::NsmNodeState_e::Literal) enNodeState));
   }
   return boRetVal;
}

gboolean NSMA_boSendSessionSignal(const NsmSession_s *pstSession)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->fireSessionStateChangedEvent(pstSession->sName, GENIVI::NodeStateManagerTypes::NsmSeat_e((GENIVI::NodeStateManagerTypes::NsmSeat_e::Literal)pstSession->enSeat), GENIVI::NodeStateManagerTypes::NsmSessionState_e((GENIVI::NodeStateManagerTypes::NsmSessionState_e::Literal)pstSession->enState));
   }
   return boRetVal;
}

gboolean NSMA_boSendApplicationModeSignal(const NsmApplicationMode_e enApplicationMode)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->fireNodeApplicationModeEvent(GENIVI::NodeStateManagerTypes::NsmApplicationMode_e((GENIVI::NodeStateManagerTypes::NsmApplicationMode_e::Literal) enApplicationMode));
   }
   return boRetVal;
}

gboolean NSMA_boSetBootMode(gint i32BootMode)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->setBootModeAttribute(i32BootMode);
   }
   return boRetVal;
}

gboolean NSMA_boGetBootMode(gint *pi32BootMode)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      *pi32BootMode = NSMA_consumerService->getBootModeAttribute();
   }
   return boRetVal;
}

gboolean NSMA_boSetRunningReason(const NsmRunningReason_e enRunningReason)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->setWakeUpReasonAttribute(GENIVI::NodeStateManagerTypes::NsmRunningReason_e((GENIVI::NodeStateManagerTypes::NsmRunningReason_e::Literal) enRunningReason));
   }
   return boRetVal;
}

gboolean NSMA_boGetRunningReason(NsmRunningReason_e *penRunningReason)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      *penRunningReason = (NsmRunningReason_e)(int) NSMA_consumerService->getWakeUpReasonAttribute();
   }
   return boRetVal;
}

gboolean NSMA_boSetShutdownReason(const NsmShutdownReason_e enShutdownReason)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->setShutdownReasonAttribute(GENIVI::NodeStateManagerTypes::NsmShutdownReason_e((GENIVI::NodeStateManagerTypes::NsmShutdownReason_e::Literal) enShutdownReason));
   }
   return boRetVal;
}

gboolean NSMA_boGetShutdownReason(NsmShutdownReason_e *penShutdownReason)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      *penShutdownReason = (NsmShutdownReason_e)(int) NSMA_consumerService->getShutdownReasonAttribute();
   }
   return boRetVal;
}

gboolean NSMA_boSetRestartReason(const NsmRestartReason_e enRestartReason)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      NSMA_consumerService->setRestartReasonAttribute(GENIVI::NodeStateManagerTypes::NsmRestartReason_e((GENIVI::NodeStateManagerTypes::NsmRestartReason_e::Literal) enRestartReason));
   }
   return boRetVal;
}

gboolean NSMA_boGetRestartReason(NsmRestartReason_e *penRestartReason)
{
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   gboolean boRetVal = FALSE;
   /* Check if library has been initialized (objects are available) */
   if (NSMA__boInitialized == TRUE)
   {
      boRetVal = TRUE; /* Send the signal */
      *penRestartReason = (NsmRestartReason_e)(int) NSMA_consumerService->getRestartReasonAttribute();
   }
   return boRetVal;
}

gboolean NSMA_boCallParallelLcClientsRequest(NSM__tstLifecycleClient *client, guint numClients, guint u32ShutdownType)
{
   gboolean retVal = FALSE;
   guint i = 0;

   if (NULL != client && 0 < numClients)
   {
      std::lock_guard<std::mutex> lock(NSMA__mutexParallelShutdown);
      std::shared_ptr<CommonAPI::ClientIdList> receiver = std::make_shared<CommonAPI::ClientIdList>();
      std::shared_ptr<CommonAPI::ClientIdList> availableClients(NSMA_consumerService->getSubscribersForShutdownEventsSelective());
      for (i = 0; i < numClients; i++)
      {
         for (const auto &clientIdIterator : *availableClients)
         {
            if (clientIdIterator->hashCode() == client[i].clientHash)
            {
               NSMA_clParallelShutdownPendingReceivers.insert({clientIdIterator->hashCode(), SimpleTimer::CreateTimer((int)client[i].timeout, 1, &NSMA__boHandleParallelRequestTimeout, clientIdIterator->hashCode())});
                receiver->insert(clientIdIterator);
               if(u32ShutdownType == NSM_SHUTDOWNTYPE_RUNUP || u32ShutdownType == (NSM_SHUTDOWNTYPE_RUNUP | NSM_SHUTDOWNTYPE_PARALLEL))
                  DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Will inform client"), DLT_INT64(client[i].clientHash), DLT_STRING("about parallel run up ("), DLT_UINT(u32ShutdownType), DLT_STRING(")!"));
               else
                  DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Will inform client"), DLT_INT64(client[i].clientHash), DLT_STRING("about parallel shutdown ("), DLT_UINT(u32ShutdownType), DLT_STRING(")!"));
               break;
            }
         }
      }
      NSMA_consumerService->fireShutdownEventsSelective(u32ShutdownType, receiver);

      retVal = TRUE;
   }
   return retVal;
}

gboolean NSMA_boCallLcClientRequest(NSM__tstLifecycleClient *client, guint u32ShutdownType)
{
   gboolean retVal = FALSE;

   if (NULL != client)
   {
      std::lock_guard<std::mutex> lock(NSMA__mutexParallelShutdown);
      std::shared_ptr<CommonAPI::ClientIdList> receivers = std::make_shared<CommonAPI::ClientIdList>();
      std::shared_ptr<CommonAPI::ClientIdList> availableClients = NSMA_consumerService->getSubscribersForShutdownEventsSelective();

      for (const auto &clientIdIterator : *availableClients)
      {
         if (clientIdIterator->hashCode() == client->clientHash)
         {
            receivers->insert(clientIdIterator);
            if(u32ShutdownType == NSM_SHUTDOWNTYPE_RUNUP)
               DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Will inform client"), DLT_INT64(client->clientHash), DLT_STRING("about run up ("), DLT_UINT(u32ShutdownType), DLT_STRING(")!"));
            else
               DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Will inform client"), DLT_INT64(client->clientHash), DLT_STRING("about shutdown ("), DLT_UINT(u32ShutdownType), DLT_STRING(")!"));
            break;
         }
      }

      /* Set current consumer. This way nsm can check if the correct consumer responds */
      NSMA_currentConsumer = client->clientHash;

      NSMA_stopLcClientTimeout();
      /* Start timer so HandleRequestTimeout will be called when this timer is not canceled within timeout ms */
      NSMA_simpleTimer = SimpleTimer::CreateTimer((int) client->timeout, 1, &NSMA__boHandleSequentialRequestTimeout);

      NSMA_consumerService->fireShutdownEventsSelective(u32ShutdownType, receivers);

      retVal = TRUE;
   }
   return retVal;
}

gboolean NSMA_boCallLcClientRequestWithoutTimeout(NSM__tstLifecycleClient *client, guint u32ShutdownType)
{
   gboolean retVal = FALSE;
   if (NULL != client)
   {
      std::lock_guard<std::mutex> lock(NSMA__mutexParallelShutdown);
      std::shared_ptr<CommonAPI::ClientIdList> receivers = std::make_shared<CommonAPI::ClientIdList>();
      std::shared_ptr<CommonAPI::ClientIdList> availableClients = NSMA_consumerService->getSubscribersForShutdownEventsSelective();

      for (const auto &clientIdIterator : *availableClients)
      {
         if (clientIdIterator->hashCode() == client->clientHash)
         {
            receivers->insert(clientIdIterator);
            if(u32ShutdownType | NSM_SHUTDOWNTYPE_RUNUP)
               DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Will inform client"), DLT_INT64(client->clientHash), DLT_STRING("about run up ("), DLT_UINT(u32ShutdownType), DLT_STRING(") without timeout!"));
            else
               DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: Will inform client"), DLT_INT64(client->clientHash), DLT_STRING("about shutdown ("), DLT_UINT(u32ShutdownType), DLT_STRING(") without timeout!"));
            break;
         }
      }
      NSMA_consumerService->fireShutdownEventsSelective(u32ShutdownType, receivers);
      retVal = TRUE;
   }
   return retVal;
}

int32_t NSMA_ClientRequestFinish(const std::shared_ptr<CommonAPI::ClientId> client, int32_t status)
{
  int32_t errorCode = NsmErrorStatus_Error;
  size_t clientID = client->hashCode();
  std::unique_lock<std::mutex> lock(NSMA__mutexParallelShutdown);

  /* Current sequential client */
  if (clientID == NSMA_currentConsumer)
  {
    NSMA_stopLcClientTimeout();
    NSMA_currentConsumer = 0;
    errorCode = NsmErrorStatus_Ok;
    DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: LifecycleRequest successful for (sequential) client:"), DLT_INT64(clientID), DLT_STRING("Return Value:"), DLT_STRING(ERRORSTATUS_STRING[status]));
    lock.unlock();
    NSMA__stObjectCallbacks.pfLcClientRequestFinish(clientID, FALSE, FALSE);
  }
  else /* Maybe parallel? */
  {
    /* There should be exactly one client in parallelShutdownReceivers with matching ClientId */
    auto search = NSMA_clParallelShutdownPendingReceivers.find(clientID);
    if (search != NSMA_clParallelShutdownPendingReceivers.end())
    {
      /* Cancel timeout for one parallel shutdown client */
      search->second->cancelTimer();
      NSMA_clParallelShutdownPendingReceivers.erase(search);
      errorCode = NsmErrorStatus_Ok;
      DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: LifecycleRequest successful for (parallel) client:"), DLT_INT64(clientID), DLT_STRING("Return Value:"), DLT_STRING(ERRORSTATUS_STRING[status]));
      lock.unlock();
      NSMA__stObjectCallbacks.pfLcClientRequestFinish(clientID, FALSE, FALSE);
    }
    else
    {
      /* Maybe late client? */
      std::shared_ptr<CommonAPI::ClientIdList> availableClients(NSMA_consumerService->getSubscribersForShutdownEventsSelective());
      for (const auto &clientIdIterator : *availableClients)
      {
        if (clientIdIterator->hashCode() == clientID)
        {
          /* Client has been found but it was not the expected one -> as it is late? */
          errorCode = NsmErrorStatus_WrongClient;
          DLT_LOG(NsmaContext, DLT_LOG_INFO, DLT_STRING("NSMA: LifecycleRequest returned too late for client:"), DLT_INT64(clientID), DLT_STRING("Return Value:"), DLT_STRING(ERRORSTATUS_STRING[status]));
          lock.unlock();
          NSMA__stObjectCallbacks.pfLcClientRequestFinish(clientID, FALSE, TRUE);
          break;
        }
      }
    }
  }
  return errorCode;
}

void NSMA_stopLcClientTimeout()
{
   if (NSMA_simpleTimer != NULL)
   {
      NSMA_simpleTimer->stopTimer();
      NSMA_simpleTimer = NULL;
   }
}

void NSMA_stopParallelLcClientTimeout()
{
   for (const auto clientIdIterator : NSMA_clParallelShutdownPendingReceivers)
   {
      clientIdIterator.second->stopTimer();
   }
   NSMA_clParallelShutdownPendingReceivers.clear();
}

gboolean NSMA__ParallelClientHasPendingActiveCall(size_t clientID)
{
  gboolean retVal = FALSE;
  if(clientID == 0)
  {
    retVal = !NSMA_clParallelShutdownPendingReceivers.empty();
  }
  else
  {
    auto search = NSMA_clParallelShutdownPendingReceivers.find(clientID);
    if (search != NSMA_clParallelShutdownPendingReceivers.end())
    {
      retVal = TRUE;
    }
  }
  return retVal;
}

gboolean NSMA__SequentialClientHasPendingActiveCall()
{
  return (NSMA_currentConsumer != 0);
}

gboolean NSMA_boDeleteLifecycleClient(NSM__tstLifecycleClient *client)
{
  gboolean retVal = FALSE;
  std::lock_guard<std::mutex> lock(NSMA__mutexParallelShutdown);
  if(client->clientHash == NSMA_currentConsumer)
  {
    NSMA_stopLcClientTimeout();
  }
  else
  {
    auto search = NSMA_clParallelShutdownPendingReceivers.find(client->clientHash);
    if (search != NSMA_clParallelShutdownPendingReceivers.end())
    {
      /* Cancel timeout for one parallel shutdown client */
      search->second->cancelTimer();
      NSMA_clParallelShutdownPendingReceivers.erase(search);
    }
  }
  return retVal;
}

void NSMA_setLcCollectiveTimeout()
{
   std::lock_guard<std::mutex> lock(NSMA__mutexParallelShutdown);

   NSMA_stopLcClientTimeout();
   NSMA_currentConsumer = 0;

   for (const auto clientIdIterator : NSMA_clParallelShutdownPendingReceivers)
   {
      clientIdIterator.second->cancelTimer();
   }
   NSMA_clParallelShutdownPendingReceivers.clear();
}

gboolean NSMA_boDeInit(void)
{
   NSMTriggerWatchdog(NsmWatchdogState_Active);
   gboolean retVal = TRUE;
   NSMA__boInitialized = FALSE;
   NSMA_stopLcClientTimeout();
   NSMA_stopParallelLcClientTimeout();
   for (const auto clientIdIterator : NSMA_clParallelShutdownPendingReceivers)
   {
      clientIdIterator.second->stopTimer();
   }
   retVal &= NSMA_runtime->unregisterService(gCapiDomain, NodeStateConsumer_StubImpl::StubInterface::getInterface(), v1::org::genivi::nodestatemanager::Consumer_INSTANCES[0]);
   retVal &= NSMA_runtime->unregisterService(gCapiDomain, NodeStateLifecycleControl_StubImpl::StubInterface::getInterface(), v1::org::genivi::nodestatemanager::LifecycleControl_INSTANCES[0]);
   std::lock_guard<std::mutex> lock(NSMA__mutex);
   NSMA_consumerService = NULL;
   NSMA_lifecycleControlService = NULL;
   return retVal;
}