summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_FrameworkCore/src/statemachine/frameworkunified_sm_hsm.cpp
blob: f6cdb098f986c21f05cdb99ba21050012d562c0f (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
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//////////////////////////////////////////////////////////////////////////////////////////////////
/// \defgroup <<Group Tag>> <<Group Name>>
/// \ingroup  tag_NSFramework
/// .
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NSFramework
/// \brief
///
/// This file has the CFrameworkunifiedHSM class definitions. CFrameworkunifiedHSM is base class for HSM Framework.
/// This class implements interfaces for connecting child states to parent states, connecting events
/// to state.
///////////////////////////////////////////////////////////////////////////////////////////////////

#include <native_service/frameworkunified_sm_hsm.h>
#include <native_service/frameworkunified_sm_reaction.h>
#include <native_service/frameworkunified_sm_state.h>
#include <native_service/frameworkunified_sm_compositestate.h>
#include <native_service/frameworkunified_sm_framework_types.h>
#include <native_service/frameworkunified_sm_orthogonalstate.h>
#include <native_service/frameworkunified_framework_if.h>

#include <cstdio>
#include <sstream>
#include <string>

#include "frameworkunified_framework_internal.h"
#include "frameworkunified_sm_framework_core.h"

///////////////////////////////////////////////////////////////////////////////////////////
/// CFrameworkunifiedHSM
/// Class constructor
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedHSM::CFrameworkunifiedHSM(PVOID f_pHApp): m_uiCurrentEvent(0), m_pHApp(f_pHApp), m_pActiveState(NULL), m_pRootState(NULL),
  m_bIsTransitioning(FALSE) {
  // PostEventList stores the list of events posted in the state
  m_pPostEventList =  new EventInfoList();
}

///////////////////////////////////////////////////////////////////////////////////////////
/// CFrameworkunifiedHSM
/// Class constructor
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedHSM::CFrameworkunifiedHSM(): m_uiCurrentEvent(0), m_pHApp(NULL), m_pActiveState(NULL), m_pRootState(NULL),
  m_bIsTransitioning(FALSE) {
  // PostEventList stores the list of events posted in the state
  m_pPostEventList =  new EventInfoList();
  m_pHApp = NULL;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// ~CFrameworkunifiedHSM
/// Class destructor
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedHSM::~CFrameworkunifiedHSM() {
  if (NULL != m_pPostEventList) {
    delete m_pPostEventList;
    m_pPostEventList = NULL;
  }

  if (m_pRootState) {
    FrameworkunifiedClose();
  }

  // we are not deleting this pointer because memory pointed by this pointer
  // had already been deleted in above step.
  m_pActiveState = NULL;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedGetActiveState
/// Returns the active state of the statemachine
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedState *CFrameworkunifiedHSM::FrameworkunifiedGetActiveState() {
  CFrameworkunifiedState *l_pCurrentState = m_pRootState;
  CFrameworkunifiedState *l_pActiveState = NULL;

  try {
    // Iterate till the current state is leafstate or orthogonal state
    while (l_pCurrentState != l_pActiveState) {
      l_pActiveState = l_pCurrentState;
      l_pCurrentState = l_pCurrentState->FrameworkunifiedGetActiveState();
      CHKNULL(l_pCurrentState);
    }

    // Set active state
    m_pActiveState = l_pActiveState;

    FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Active state is  %s "
           , m_pActiveState->m_strStateName.c_str());
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    l_pActiveState = NULL;
  }


  return l_pActiveState;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedStart
/// This starts the state machine
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedStart(CEventDataPtr f_pEventData) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  try {
    CHKNULL(m_pActiveState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    m_bIsTransitioning = TRUE;

    // Start the state machine execution on current state
    m_pActiveState = m_pActiveState->FrameworkunifiedOnHSMStart(f_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch

    // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
    FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Active state is  %s "
           , m_pActiveState->m_strStateName.c_str());
    // LCOV_EXCL_BR_STOP
    // post the event from events list
    eStatus = ProcessEventQueue();  // LCOV_EXCL_BR_LINE 11:except branch

    m_bIsTransitioning = FALSE;
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return eStatus;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedConnect
/// This connects the reaction to event and add event to child states then add child state
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::CFrameworkunifiedHSM::FrameworkunifiedConnect(CFrameworkunifiedState *f_pParentState, CFrameworkunifiedState *f_pChildState,
                                        UI_32 f_uiEventId, CFrameworkunifiedReaction *f_pReaction,
                                        BOOL f_bIsDefaultState, BOOL f_bIsDeferredEventType,
                                        std::string f_strEventName) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  try {
    // attach reaction to event and add it to the  child state
    eStatus = FrameworkunifiedConnect(f_pChildState, f_uiEventId, f_pReaction, f_strEventName, f_bIsDeferredEventType);
    if (eFrameworkunifiedStatusOK == eStatus) {
      // add child state to parent state
      eStatus = FrameworkunifiedConnect(f_pParentState, f_pChildState, f_bIsDefaultState);
    }
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;
}
///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedConnect
/// This add child state to parent state.
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedConnect(CFrameworkunifiedState *f_pParentState, CFrameworkunifiedState *f_pChildState,
                               BOOL f_bIsDefaultState) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(f_pParentState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    CHKNULL(f_pChildState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    // Check if the state is composite state
    if (f_pParentState->FrameworkunifiedHasSubStates()) {
      // Check if the child state is default state
      if (f_bIsDefaultState) {
        // Add child state as default state
        (reinterpret_cast<CFrameworkunifiedCompositeState *>(f_pParentState))->FrameworkunifiedAddState(f_pChildState,
                                                            CFrameworkunifiedCompositeState::eFrameworkunifiedDefaultState);  // LCOV_EXCL_BR_LINE 11: except branch
      } else {
        // Add state as regular state
        (reinterpret_cast<CFrameworkunifiedCompositeState *>(f_pParentState))->FrameworkunifiedAddState(f_pChildState);  // LCOV_EXCL_BR_LINE 11: except branch
      }

      f_pChildState->FrameworkunifiedSetHSM(this);  // LCOV_EXCL_BR_LINE 11: except branch

    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error %s is not a composite state",
             f_pParentState->m_strStateName.c_str());
    }
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return eStatus;  //LCOV_EXCL_BR_LINE 11:except branch
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedConnect
/// This connects the reaction to event and add event to child states
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedConnect(CFrameworkunifiedState *f_pState, UI_32 f_uiEventId, CFrameworkunifiedReaction *f_pReaction,
                               std::string f_strEventName , BOOL f_bIsDeferredEventType) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(f_pState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    // check if the event is deferred event
    if (f_bIsDeferredEventType) {
      // Add event as deferred event
      f_pState->FrameworkunifiedAddDeferredEvent(f_uiEventId, f_strEventName);  // LCOV_EXCL_BR_LINE 11: except branch
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Deferred Event %d %s is "
        "associated with Reaction and added to state %s "
        , f_uiEventId, f_strEventName.c_str(), (f_pState->m_strStateName).c_str());
      // LCOV_EXCL_BR_STOP
    } else {
      CHKNULL(f_pReaction);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
      // Add event as regular event
      f_pState->FrameworkunifiedAddEvent(f_uiEventId, f_pReaction, f_strEventName);  // LCOV_EXCL_BR_LINE 11: except branch

      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Event %d %s is associated with Reaction and added to state %s "
             , f_uiEventId, f_strEventName.c_str(), (f_pState->m_strStateName).c_str());
      // LCOV_EXCL_BR_STOP
    }
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to add event %d in %s", f_uiEventId,
           (f_pState->m_strStateName).c_str());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return eStatus;;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedPostEvent
/// This creates the default event data and sends the event to the active HSM state.
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedPostEvent(UI_32 f_uiEventId) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;  // LCOV_EXCL_BR_LINE 11: except branch
  try {
    CEventDataPtr l_pEventData(new CEventData(f_uiEventId));  // LCOV_EXCL_BR_LINE 11:except branch
    CHKNULL(l_pEventData);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    l_eStatus = FrameworkunifiedPostEvent(l_pEventData);  // LCOV_EXCL_BR_LINE 11: except branch
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return l_eStatus;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedPostEvent
/// This sends the event to the active HSM state
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedPostEvent(CEventDataPtr f_pEventData) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(f_pEventData);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    if (m_bIsTransitioning) {
      eStatus = FrameworkunifiedQueueEvent(f_pEventData);  // LCOV_EXCL_BR_LINE 11: except branch
    } else {
      m_bIsTransitioning = TRUE;

      eStatus = ProcessEvent(f_pEventData);  // LCOV_EXCL_BR_LINE 11: except branch

      m_bIsTransitioning = FALSE;

      CHKNULL(m_pActiveState);
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Active state is  %s ", m_pActiveState->m_strStateName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
    }
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;
}

EFrameworkunifiedStatus CFrameworkunifiedHSM::ProcessEvent(CEventDataPtr f_pEventData) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(m_pActiveState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    CHKNULL(f_pEventData);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    CHKNULL(m_pActiveState->m_pEventName);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    EventNameList::iterator l_itEventName;
    l_itEventName = m_pActiveState->m_pEventName->find(f_pEventData->m_uiEventId);

    if (m_pActiveState->m_pEventName->end() != l_itEventName) {
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Posting event %d %s to state  %s",
             f_pEventData->m_uiEventId,
             l_itEventName->second.c_str(),
             m_pActiveState->m_strStateName.c_str());
      // LCOV_EXCL_BR_STOP
    }

    m_uiCurrentEvent = f_pEventData->m_uiEventId;

    // Send event to active state for processing
    m_pActiveState = m_pActiveState->FrameworkunifiedOnEvent(f_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch

    CHKNULL(m_pActiveState);
    FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Active state is  %s ", m_pActiveState->m_strStateName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"

    // post the event from events list
    eStatus = ProcessEventQueue();  // LCOV_EXCL_BR_LINE 11:except branch
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"

    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedClose
/// This stops the state machine and destroys all states and events
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedClose(CEventDataPtr f_pEventData) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(m_pRootState);
    delete m_pRootState;
    m_pRootState = NULL;
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedConnect
/// This sets the givens state as root state in the state machine
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedConnect(CFrameworkunifiedState *f_pRootState) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(f_pRootState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    m_pActiveState = f_pRootState;
    m_pRootState = f_pRootState;

    m_pRootState->FrameworkunifiedSetHSM(this);  // LCOV_EXCL_BR_LINE 11:except branch
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;  // LCOV_EXCL_BR_LINE 11:except branch
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedPrintAllStates
/// This prints all states and events associated with every state on console.
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedPrintAllStates() {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(m_pRootState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    // Print the states
    m_pRootState->FrameworkunifiedPrintStates();
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;  // LCOV_EXCL_BR_LINE 11:except branch
}

EFrameworkunifiedStatus  CFrameworkunifiedHSM::FrameworkunifiedConnectOrthogonal(CFrameworkunifiedOrthogonalState *f_pOrthogonalState,
                                          CFrameworkunifiedCompositeState *f_pOrthogonalRegion) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(f_pOrthogonalState)  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    CHKNULL(f_pOrthogonalRegion)  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    f_pOrthogonalState->FrameworkunifiedAddOrthogonalRegion(f_pOrthogonalRegion);
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;
}

HANDLE CFrameworkunifiedHSM::FrameworkunifiedGetAppHandle() {
  return m_pHApp;
}

EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedPrintXML() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  try {
    std::ostringstream l_strXMLString;
    l_strXMLString << "<Statemachine>";

    CHKNULL(m_pRootState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    // Write statemachine information in XML stream
    m_pRootState->FrameworkunifiedPrintXML(l_strXMLString);
    l_strXMLString << "</Statemachine>";

    // Write a stream to XML file
    if (m_pHApp) {
      size_t l_uiLength = static_cast<size_t>(l_strXMLString.str().length());

      PCHAR l_pStream = new CHAR[l_uiLength + 1];
      if (NULL != l_pStream) {
        std::FILE *l_pFile = NULL;

        std::memset(l_pStream, 0, l_uiLength + 1);
        std::strncpy(l_pStream, l_strXMLString.str().c_str(), l_uiLength);

        std::ostringstream l_strFileName;

        l_strFileName << "StatemachineXML_";

        l_strFileName << FrameworkunifiedGetAppName(m_pHApp) << ".xml";

        l_pFile = std::fopen(l_strFileName.str().c_str(), "wbe");

        if (l_pFile) {
          std::fwrite(l_pStream, l_uiLength, 1, l_pFile);
          std::fclose(l_pFile);
          FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s File Created"
                 , l_strFileName.str().c_str());
        }
        delete[] l_pStream;
        l_pStream = NULL;
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: StatemachineXML_%s.xml file not created", FrameworkunifiedGetAppName(m_pHApp));
      }
    }
    FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, l_strXMLString.str().c_str());
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
  }
  return l_eStatus;
}

EFrameworkunifiedStatus CFrameworkunifiedHSM::FrameworkunifiedQueueEvent(CEventDataPtr f_pEventData) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  try {
    CHKNULL(m_pPostEventList);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    CHKNULL(m_pActiveState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
    CHKNULL(f_pEventData);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    // Push the event in the post event list(FIFO)
    m_pPostEventList->push_back(f_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;  // LCOV_EXCL_BR_LINE 11:except branch
}

///////////////////////////////////////////////////////////////////////////////////////////
/// ProcessEventQueue
/// This post the event from events list
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::ProcessEventQueue() {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;

  try {
    CHKNULL(m_pPostEventList);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

    if (!m_pPostEventList->empty()) {
      CEventDataPtr l_pEventData = m_pPostEventList->front();  // LCOV_EXCL_BR_LINE 11:except branch
      CHKNULL(l_pEventData);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h

      CHKNULL(m_pActiveState);  // LCOV_EXCL_BR_LINE 15: marco defined in frameworkunified_sm_framework_types.h
      // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, "Processing posted event %d in state %s",
             l_pEventData->m_uiEventId, m_pActiveState->m_strStateName.c_str());
      // LCOV_EXCL_BR_STOP
      m_pPostEventList->erase(m_pPostEventList->begin());

      eStatus = ProcessEvent(l_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch
    }
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// RemoveEventFromPostedEventQueue
/// This API is used to remove the all events of eventId f_uiEventId from event queue of statemachine
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedHSM::RemoveEventFromPostedEventQueue(const UI_32 f_uiEventId) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldID;
  FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "+");

  if (NULL != m_pPostEventList) {
    int32_t l_siCnt = static_cast<int32_t>(m_pPostEventList->size() - 1);

    for (; l_siCnt >= 0; l_siCnt--) {
      if (NULL != m_pPostEventList->at(l_siCnt).get()) {
        if (f_uiEventId == m_pPostEventList->at(l_siCnt).get()->m_uiEventId) {
          m_pPostEventList->erase(m_pPostEventList->begin() + l_siCnt);
          l_eStatus = eFrameworkunifiedStatusOK;
        }
      }
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}