common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_FrameworkCore / src / statemachine / frameworkunified_sm_state.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup  tag_NSFramework
20 /// .
21 ///////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ///////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup  tag_NSFramework
25 /// \brief
26 ///
27 /// This file has the CFrameworkunifiedState class implementation. CFrameworkunifiedState is base class for all types of
28 /// state classes.This class implements the basic functionality required for HSM state.
29 /// It provides the standard interfaces for entering, exiting and reacting in a state.
30 ///
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////////////////////////////
34 // Include Files
35 ///////////////////////////////////////////////////////////////////////////////////////////////////
36 #include <native_service/frameworkunified_sm_state.h>
37 #include <native_service/frameworkunified_sm_reaction.h>
38 #include <native_service/frameworkunified_sm_framework_types.h>
39 #include <native_service/frameworkunified_sm_hsm.h>
40 #include <sstream>
41 #include <string>
42 #include <utility>
43 #include "frameworkunified_sm_framework_core.h"
44 #include "frameworkunified_framework_internal.h"
45
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 /// CFrameworkunifiedState
48 /// Parameterized constructor
49 ///////////////////////////////////////////////////////////////////////////////////////////
50 CFrameworkunifiedState::CFrameworkunifiedState(std::string f_pName): m_strStateName(f_pName) {
51   try {
52     // EventList  stores the list of events associated with the state
53     m_pEventList = new EventReactionList();  // LCOV_EXCL_BR_LINE 11:except branch
54
55     // Deferred eventlist stores the list of deferred events associated
56     // with the state
57     m_pDeferredEventList = new DeferredEventList();  // LCOV_EXCL_BR_LINE 11:except branch
58
59     // Deferred PostEventList stores the list of posted deferred events posted
60     // in the state
61     m_pDeferredPostEventList =   new EventInfoList();  // LCOV_EXCL_BR_LINE 11:except branch
62
63     // EventName map stores the Name of event against event id, The event name is
64     // only used for debugging so this can be disbaled in case of release build
65     m_pEventName = new EventNameList();  // LCOV_EXCL_BR_LINE 11:except branch
66
67     m_pDefaultState = NULL;
68     m_pActiveState = NULL;
69     m_pParentState = NULL;
70     m_pStateMachine = NULL;
71
72     FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s state created ", f_pName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
73   } catch (std::exception &e) {
74     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
75     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed in %s state ", f_pName.c_str());
76   }
77 }
78
79 ///////////////////////////////////////////////////////////////////////////////////////////
80 /// ~CFrameworkunifiedState
81 /// Class destructor
82 ///////////////////////////////////////////////////////////////////////////////////////////
83 CFrameworkunifiedState::~CFrameworkunifiedState() {
84   EventReactionIterator l_objEventIterator;
85   EventReactionIterator l_objDeferredEventIterator;
86
87   try {
88     // Delete the event list
89     CHKNULL(m_pEventList);
90     for (l_objEventIterator = m_pEventList->begin();
91          l_objEventIterator != m_pEventList->end();
92          l_objEventIterator++) {
93       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, " deleting the event %d in state %s",
94              (*l_objEventIterator).first , m_strStateName.c_str());
95
96       if (NULL != (*l_objEventIterator).second) {
97         (*l_objEventIterator).second->m_ucRefCount--;
98
99         if (0 == ((*l_objEventIterator).second->m_ucRefCount)) {
100           delete(*l_objEventIterator).second;
101           (*l_objEventIterator).second = NULL;
102         }
103       }
104     }
105
106     // Delete the eventlist
107     m_pEventList->clear();
108     delete m_pEventList;
109     m_pEventList = NULL;
110
111     // Delete the deferred eventlist
112     CHKNULL(m_pDeferredEventList);
113     m_pDeferredEventList->clear();
114     delete m_pDeferredEventList;
115     m_pDeferredEventList = NULL;
116
117     // delete deferred Post event list
118     CHKNULL(m_pDeferredPostEventList);
119     m_pDeferredPostEventList->clear();
120     delete m_pDeferredPostEventList;
121     m_pDeferredPostEventList = NULL;
122
123     // delete event name list
124     CHKNULL(m_pEventName);
125     m_pEventName->clear();
126     delete m_pEventName;
127     m_pEventName = NULL;
128   } catch (std::exception &e) {
129     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
130   }
131 }
132 ///////////////////////////////////////////////////////////////////////////////////////////
133 /// FrameworkunifiedAddEvent
134 /// Associates the event id with the reaction in the state. When the event is posted to the
135 /// state the associated reaction is executed. This also adds the event name to the map
136 /// which is used for debugging.
137 ///////////////////////////////////////////////////////////////////////////////////////////
138 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedAddEvent(UI_32 f_uiEventId, CFrameworkunifiedReaction *f_pReaction, std::string f_strEventName) {
139   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
140
141   try {
142     // LCOV_EXCL_BR_START 15:marco defined in frameworkunified_sm_framework_types.h
143     CHKNULL(m_pEventName);
144     CHKNULL(m_pEventList);
145     CHKNULL(f_pReaction);
146     // LCOV_EXCL_BR_STOP
147     // associate the eventname with event id (debugging only)
148     m_pEventName->insert(std::pair<UI_32, std::string>(f_uiEventId, f_strEventName));
149
150     f_pReaction->m_ucRefCount++;
151
152     // associate the reaction with event id
153     m_pEventList->insert(std::pair<UI_32, CFrameworkunifiedReaction *>(f_uiEventId, f_pReaction));
154
155     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
156     FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "Reaction associated with the event %d %s in the state %s "
157            , f_uiEventId, (m_pEventName->find(f_uiEventId)->second).c_str(), m_strStateName.c_str());
158     // LCOV_EXCL_BR_STOP
159   } catch (std::exception &e) {
160     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
161     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to add event %d %s in state %s",
162            f_uiEventId , (m_pEventName->find(f_uiEventId)->second).c_str(), m_strStateName.c_str());
163     eStatus = eFrameworkunifiedStatusNullPointer;
164   }
165
166   return eStatus;
167 }
168
169 ///////////////////////////////////////////////////////////////////////////////////////////
170 /// FrameworkunifiedOnEvent
171 /// This function processes the event. If the reaction for event is available in the current
172 /// state within eventlist and deferred eventlist then it is consumed in the current state
173 /// otherwise forwarded to the parent state. Event forwarding is done recursively till either
174 /// event is consumed or the root state has encountered. This also process the events posted
175 /// in the reactions recursively till all posted events are cleared.
176 ///////////////////////////////////////////////////////////////////////////////////////////
177 CFrameworkunifiedState *CFrameworkunifiedState::FrameworkunifiedOnEvent(CEventDataPtr f_pEventData) {
178   CFrameworkunifiedState *l_pCurrentState = this;
179   CFrameworkunifiedReaction *l_pEventReaction = NULL;
180
181   try {
182     // LCOV_EXCL_BR_START 15:marco defined in frameworkunified_sm_framework_types.h
183     CHKNULL(m_pEventList);
184     CHKNULL(m_pDeferredEventList);
185     // LCOV_EXCL_BR_STOP
186     // Find the reaction object for given event id
187
188     /**
189      * @todo
190      * Unauthorized accesses will occur if NULL is specified for the event data.
191      */
192     if (m_pEventList->end() != m_pEventList->find(f_pEventData->m_uiEventId)) {
193       l_pEventReaction = reinterpret_cast<CFrameworkunifiedReaction *>((m_pEventList->find(f_pEventData->m_uiEventId))->second);
194     }
195
196     if (l_pEventReaction) {
197       // execute the reaction associated with the event
198       l_pCurrentState = l_pEventReaction->FrameworkunifiedReaction(this, f_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch
199
200       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
201       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, "Reaction completed for event %d %s in state %s "
202              , f_pEventData->m_uiEventId, (m_pEventName->find(f_pEventData->m_uiEventId)->second).c_str(),
203              m_strStateName.c_str());
204       // LCOV_EXCL_BR_STOP
205     } else if (IsEventDeferred(f_pEventData->m_uiEventId)) {
206       // If given event is deferred event then handle defer event
207       CHKNULL(m_pDeferredPostEventList);
208
209       m_pDeferredPostEventList->push_back(f_pEventData);
210
211       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "deferred event %d %s posted to state %s ",
212              f_pEventData->m_uiEventId,
213              (m_pEventName->find(f_pEventData->m_uiEventId)->second).c_str(),
214              m_strStateName.c_str());
215
216     } else {
217       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
218       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, "Reaction not available or event %d not found in state %s"
219              , f_pEventData->m_uiEventId, m_strStateName.c_str());
220       // LCOV_EXCL_BR_STOP
221       // check if the current state has parent state
222       if (m_pParentState) {
223         // No reaction available fot given event in the current state
224         // then forward event to parent state
225         // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
226         FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "Forwarding an event %d to %s"
227                , f_pEventData->m_uiEventId, m_pParentState->m_strStateName.c_str());
228         // LCOV_EXCL_BR_STOP
229         l_pCurrentState = m_pParentState->FrameworkunifiedOnEvent(f_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch
230
231       } else {
232         // No parent is available then reached root state,
233         // No reaction available in the statemachine then discard the event
234         // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
235         FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "Discarding an event %d ", f_pEventData->m_uiEventId);
236         // LCOV_EXCL_BR_STOP
237       }
238
239       CHKNULL(l_pCurrentState);
240
241       // This is a recursive function that recurse in parent states till the event is
242       // consumed or discarded So Setting the current state as the active state of the
243       // returned state, as the return state is parent state
244       if (l_pCurrentState->m_pActiveState) {
245         l_pCurrentState = l_pCurrentState->m_pActiveState;
246
247       } else {
248         // do nothing in leaf state, as current state is active state
249       }
250     }
251   } catch (std::exception &e) {
252     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
253     return NULL;
254   }
255   return l_pCurrentState;
256 }
257
258 //////////////////////////////////////////////////////////////////////////////////////////////////
259 /// FrameworkunifiedAddDeferredEvent
260 /// When the event is posted to the state the event is deferred and stored in the state.
261 /// In case of implicit recall of the deferred events, events are processed before exiting the state.
262 /// The deferred events can also be recalled explicitly in the state.
263 /// This also adds the event name to the map which is used for debugging.
264 //////////////////////////////////////////////////////////////////////////////////////////////////
265 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedAddDeferredEvent(UI_32 f_uiEventId, std::string f_strEventName) {
266   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
267
268   try {
269     // LCOV_EXCL_BR_START 15:marco defined in frameworkunified_sm_framework_types.h
270     CHKNULL(m_pEventName);
271
272     CHKNULL(m_pDeferredEventList);
273     // LCOV_EXCL_BR_STOP
274     // associate the deferred eventname with event id (debugging only)
275     m_pEventName->insert(std::pair<UI_32, std::string>(f_uiEventId, f_strEventName));
276
277     // associate the reaction with deferred event id
278     m_pDeferredEventList->push_back(f_uiEventId);  // LCOV_EXCL_BR_LINE 11:except branch
279     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
280     FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "Added deferred event %d %s in the state %s "
281            , f_uiEventId, (m_pEventName->find(f_uiEventId)->second).c_str(), m_strStateName.c_str());
282     // LCOV_EXCL_BR_STOP
283   } catch (std::exception &e) {
284     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
285     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to add event %d %s in state %s",
286            f_uiEventId, (m_pEventName->find(f_uiEventId)->second).c_str(), m_strStateName.c_str());
287
288     eStatus = eFrameworkunifiedStatusNullPointer;
289   }
290
291   return eStatus;
292 }
293
294 ///////////////////////////////////////////////////////////////////////////////////////////
295 /// FrameworkunifiedPostEvent
296 /// This function creates new eventdata object and add the to event queue of the state.
297 /// The events are posted in the reaction which are executed in the state.
298 /// The event queue is processed once the execution of the reaction is completed.
299 ///////////////////////////////////////////////////////////////////////////////////////////
300 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedPostEvent(UI_32 f_uiEventId) {
301   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
302   try {
303     CHKNULL(m_pStateMachine);  // LCOV_EXCL_BR_LINE 15:marco defined in frameworkunified_sm_framework_types.h
304
305     CEventDataPtr l_pEventData(new CEventData(f_uiEventId));  // LCOV_EXCL_BR_LINE 11:except branch
306
307     l_eStatus = m_pStateMachine->FrameworkunifiedPostEvent(l_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch  // LCOV_EXCL_BR_LINE 11:except branch
308   } catch (std::exception &e) {
309     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
310     l_eStatus = eFrameworkunifiedStatusNullPointer;
311   }
312
313   return l_eStatus;
314 }
315
316 ///////////////////////////////////////////////////////////////////////////////////////////
317 /// FrameworkunifiedPostEvent
318 /// This function adds the event queue of the state. The events are posted in the reaction
319 /// which are executed in the state. The event queue is processed once the execution of the
320 /// reaction is completed.
321 ///////////////////////////////////////////////////////////////////////////////////////////
322 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedPostEvent(CEventDataPtr f_pEventData) {
323   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
324
325   try {
326     CHKNULL(m_pStateMachine);
327     CHKNULL(f_pEventData);
328
329     l_eStatus = m_pStateMachine->FrameworkunifiedPostEvent(f_pEventData);
330   } catch (std::exception &e) {
331     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
332     l_eStatus = eFrameworkunifiedStatusNullPointer;
333   }
334
335   return l_eStatus;
336 }
337
338 ///////////////////////////////////////////////////////////////////////////////////////////
339 /// FrameworkunifiedOnHSMStart
340 /// This function is called recursively till the leaf state is reached. This internally
341 /// calls the Entry function of the current state.
342 ///////////////////////////////////////////////////////////////////////////////////////////
343 CFrameworkunifiedState *CFrameworkunifiedState::FrameworkunifiedOnHSMStart(CEventDataPtr f_pEventData) {
344   CFrameworkunifiedState *l_pActiveState = this;
345   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
346
347   try {
348     // Call Entry method of the current state. Entry method of state is called in the order of
349     // Hierarchy from Outer state to Inner state
350     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedOnEntry(f_pEventData))) {
351       // If current state has sub states then enter into active state for state entry
352       // active state is same as the default state. In this case the FrameworkunifiedOnStart is called
353       // recursively and recursion breaks when the current state is leafstate that does not have
354       // any active/default state.
355       if (m_pActiveState) {
356         l_pActiveState = m_pActiveState->FrameworkunifiedOnHSMStart(f_pEventData);  // LCOV_EXCL_BR_LINE 11:except branch
357       } else {
358         l_pActiveState = this;
359       }
360
361       // set current state as the active state of its parent state to maintain the Hierarchy
362       if (m_pParentState) {
363         m_pParentState->m_pActiveState = this;
364       }
365     } else {
366       // If FrameworkunifiedOnEntry failed then statemachine should report the error
367       // We can throw an exception but for now as a quick fix we are setting
368       // l_pActiveState as NULL which will stop the statemachine
369       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
370       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error:%d in FrameworkunifiedOnEntry of state %s", eStatus,
371              l_pActiveState->m_strStateName.c_str());
372       // LCOV_EXCL_BR_STOP
373       // l_pActiveState = NULL;
374       /* Commenting it, because it was making state machine inactive. This should not be the expected behavior.
375        * Just log and take no action, if user return non-ok value.
376        * User defined error values should be handled separately */
377     }
378   } catch (std::exception &e) {
379     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
380     return NULL;
381   }
382
383   return l_pActiveState;
384 }
385
386 ///////////////////////////////////////////////////////////////////////////////////////////
387 /// FrameworkunifiedOnHSMStop
388 /// This function is called recursively till the required parent state is reached. This
389 /// internally calls the Exit function of the current state.
390 ///////////////////////////////////////////////////////////////////////////////////////////
391 CFrameworkunifiedState *CFrameworkunifiedState::FrameworkunifiedOnHSMStop(CEventDataPtr f_pEventData) {
392   CFrameworkunifiedState *l_pActiveState = this;
393   try {
394     // if active state is composite state, update the shallow and deep history state(if exists)
395     UpdateHistory();  // LCOV_EXCL_BR_LINE 11:except branch
396
397     // if current state has active state then recursively call the FrameworkunifiedOnHSMStop till current
398     // state has no active state i.e. current state is leaf state
399     if (m_pActiveState) {
400       m_pActiveState->FrameworkunifiedOnHSMStop(f_pEventData);
401     }
402
403     m_pActiveState =  m_pDefaultState;
404
405     // Post deferred events to statemachine event queue
406     CHKNULL(m_pDeferredPostEventList);  // LCOV_EXCL_BR_LINE 15:marco defined in frameworkunified_sm_framework_types.h
407
408     // recall deferred events till the vector is empty
409     while (!m_pDeferredPostEventList->empty()) {
410       // get the first event list object
411       CEventDataPtr l_pEventData = m_pDeferredPostEventList->front();
412
413       CHKNULL(l_pEventData);
414       CHKNULL(m_pEventName);
415
416       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "Recalling event %d %s in state %s"
417              , l_pEventData->m_uiEventId,
418              (m_pEventName->find(l_pEventData->m_uiEventId)->second).c_str(), m_strStateName.c_str());
419
420       m_pDeferredPostEventList->erase(m_pDeferredPostEventList->begin());
421
422       // recall the event stored in the eventinfo object
423       FrameworkunifiedPostEvent(l_pEventData);
424     }
425
426     FrameworkunifiedOnExit(f_pEventData);
427   } catch (std::exception &e) {
428     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
429     return NULL;
430   }
431
432   return l_pActiveState;
433 }
434
435 ///////////////////////////////////////////////////////////////////////////////////////////
436 /// IsEventDeferred
437 /// This checks if the given event is marked as deferred in the state.
438 ///////////////////////////////////////////////////////////////////////////////////////////
439 BOOL CFrameworkunifiedState::IsEventDeferred(UI_32 f_uiEventId) {
440   BOOL bStatus = FALSE;
441   try {
442     CHKNULL(m_pDeferredEventList);  // LCOV_EXCL_BR_LINE 15:marco defined in frameworkunified_sm_framework_types.h
443     for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
444       if (f_uiEventId == m_pDeferredEventList->at(l_uiCount)) {
445         bStatus = TRUE;
446         break;
447       }
448     }
449   } catch (std::exception &e) {
450     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
451   }
452
453   return bStatus;  // LCOV_EXCL_BR_LINE 15:marco defined in frameworkunified_sm_framework_types.h
454 }
455
456 ///////////////////////////////////////////////////////////////////////////////////////////
457 /// FrameworkunifiedRemoveEventFromDeferredEventList
458 /// This function removes the event from the posted deferred queue list of the state.
459 ///////////////////////////////////////////////////////////////////////////////////////////
460 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedRemoveEventFromDeferredEventList(UI_32 f_uiEventId) {
461   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldID;
462   FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "+");
463
464   try {
465     CHKNULL(m_pDeferredPostEventList);
466     int32_t l_siCnt = static_cast<int32_t>(m_pDeferredPostEventList->size() - 1);
467
468     for (; l_siCnt >= 0; l_siCnt--) {
469       if (NULL != m_pDeferredPostEventList->at(l_siCnt).get()) {
470         if (f_uiEventId == m_pDeferredPostEventList->at(l_siCnt).get()->m_uiEventId) {
471           m_pDeferredPostEventList->erase(m_pDeferredPostEventList->begin() + l_siCnt);
472           l_eStatus = eFrameworkunifiedStatusOK;
473         }
474       }
475     }
476   } catch (std::exception &e) {
477     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
478     l_eStatus = eFrameworkunifiedStatusNullPointer;
479   }
480   FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "-");
481   return l_eStatus;
482 }
483
484 ///////////////////////////////////////////////////////////////////////////////////////////
485 /// FrameworkunifiedRecallEvent
486 /// This indicates if the state has sub states. It returns TRUE only in the CompositeState
487 /// where this function is overridden
488 ///////////////////////////////////////////////////////////////////////////////////////////
489 BOOL CFrameworkunifiedState::FrameworkunifiedHasSubStates() {
490   return FALSE;
491 }
492
493 ///////////////////////////////////////////////////////////////////////////////////////////
494 /// FrameworkunifiedPrintStates
495 /// This logs the state name and events associated with the state
496 ///////////////////////////////////////////////////////////////////////////////////////////
497 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedPrintStates() {
498   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
499   EventReactionIterator l_objEventIterator;
500   try {
501     CHKNULL(m_pEventList);
502     CHKNULL(m_pDeferredEventList);
503     CHKNULL(m_pEventName);
504
505     FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s:%s",
506            (m_pParentState->m_strStateName).c_str(), m_strStateName.c_str());
507
508     // print events
509     for (l_objEventIterator = m_pEventList->begin();
510          l_objEventIterator != m_pEventList->end(); l_objEventIterator++) {
511       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "event %d %s", ((*l_objEventIterator).first),
512              (m_pEventName->find((*l_objEventIterator).first)->second).c_str());
513     }
514
515     // print deferred events
516     for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
517       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "deferred event %d %s", m_pDeferredEventList->at(l_uiCount),
518              (m_pEventName->find(m_pDeferredEventList->at(l_uiCount))->second).c_str());
519     }
520   } catch (std::exception &e) {
521     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
522     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to print in state %s", m_strStateName.c_str());
523     eStatus = eFrameworkunifiedStatusNullPointer;
524   }
525
526   return eStatus;
527 }
528
529 BOOL CFrameworkunifiedState::FrameworkunifiedHasOrthogoanlRegions() {
530   return FALSE;
531 }
532
533 BOOL CFrameworkunifiedState::FrameworkunifiedIsReactionAvailable(UI_32 f_uiEventId) {
534   BOOL IsReactionAvailable = FALSE;
535   CFrameworkunifiedReaction *l_pEventReaction = NULL;
536
537   try {
538     // LCOV_EXCL_BR_START 15:marco defined in frameworkunified_sm_framework_types.h
539     CHKNULL(m_pEventList);
540     CHKNULL(m_pDeferredEventList);
541     // LCOV_EXCL_BR_STOP
542     if (m_pEventList->end() != m_pEventList->find(f_uiEventId)) {
543       // Find the reaction object for given event id
544       l_pEventReaction = reinterpret_cast<CFrameworkunifiedReaction *>((m_pEventList->find(f_uiEventId))->second);
545     }
546
547     if (l_pEventReaction) {
548       IsReactionAvailable = TRUE;
549     } else {
550       if (IsEventDeferred(f_uiEventId)) {
551         IsReactionAvailable = TRUE;
552       }
553     }
554   } catch (std::exception &e) {
555     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
556   }
557
558   return IsReactionAvailable;  // LCOV_EXCL_BR_LINE 11:except branch
559 }
560
561 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedSetHSM(CFrameworkunifiedHSM *f_pStatemachine) {
562   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
563
564   try {
565     CHKNULL(f_pStatemachine);  // LCOV_EXCL_BR_LINE 15:marco defined in frameworkunified_sm_framework_types.h
566     m_pStateMachine = f_pStatemachine;
567   } catch (std::exception &e) {
568     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
569     l_eStatus = eFrameworkunifiedStatusNullPointer;
570   }
571   return l_eStatus;  // LCOV_EXCL_BR_LINE 11:except branch
572 }
573
574 HANDLE CFrameworkunifiedState:: FrameworkunifiedGetAppHandle() {
575   HANDLE l_pHApp = NULL;
576   try {
577     CHKNULL(m_pStateMachine);  // LCOV_EXCL_BR_LINE 15:marco defined in frameworkunified_sm_framework_types.h
578     l_pHApp = m_pStateMachine->FrameworkunifiedGetAppHandle();  // LCOV_EXCL_BR_LINE 11:except branch
579   } catch (std::exception &e) {
580     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
581   }
582   return l_pHApp;  // LCOV_EXCL_BR_LINE 11:except branch
583 }
584
585 EFrameworkunifiedStatus CFrameworkunifiedState::FrameworkunifiedPrintXML(std::ostringstream &f_strXMLString) {
586   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
587   EventReactionIterator l_objEventIterator;
588   try {
589     CHKNULL(m_pEventList);
590     CHKNULL(m_pDeferredEventList);
591     CHKNULL(m_pEventName);
592
593     f_strXMLString << "<" << m_strStateName.c_str() << ">";
594
595     f_strXMLString << "<EventList>";
596     // print events
597     for (l_objEventIterator = m_pEventList->begin();
598          l_objEventIterator != m_pEventList->end(); l_objEventIterator++) {
599       std::string l_strEventName =
600         (m_pEventName->find((*l_objEventIterator).first)->second);
601
602       UI_32 l_uiEventId = (*l_objEventIterator).first;
603
604       f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";
605
606       f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";
607
608       f_strXMLString << "</Event>";
609     }
610     f_strXMLString << "</EventList>";
611
612     // print deferred events
613     f_strXMLString << "<DeferredEventList>";
614     for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
615       UI_32 l_uiEventId = m_pDeferredEventList->at(l_uiCount);
616
617       std::string l_strEventName = (m_pEventName->find(l_uiEventId)->second);
618
619       f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";
620
621       f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";
622
623       f_strXMLString << "</Event>";
624     }
625
626     f_strXMLString << "</DeferredEventList>";
627     f_strXMLString << "</" << m_strStateName.c_str() << ">";
628   } catch (std::exception &e) {
629     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
630     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to print in state %s", m_strStateName.c_str());
631     l_eStatus = eFrameworkunifiedStatusNullPointer;
632   }
633   return l_eStatus;
634 }