d975e93053c02ffb085265c90f9c730ba31978e9
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_FrameworkCore / src / statemachine / frameworkunified_sm_compositestate.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 CFrameworkunifiedCompositeState class definitions. CFrameworkunifiedCompositeState is derived from C
28 /// FrameworkunifiedState class.This class implements the additional functionality supported by HSM Composite
29 /// state. It provides the standard interfaces for adding state.
30 ///
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////////////////////////////
34 // Include Files
35 ///////////////////////////////////////////////////////////////////////////////////////////////////
36 #include <native_service/frameworkunified_sm_compositestate.h>
37 #include <native_service/frameworkunified_sm_framework_types.h>
38 #include <native_service/frameworkunified_sm_hsm.h>
39 #include <native_service/frameworkunified_sm_historystate.h>
40
41 #include <sstream>
42 #include <string>
43 #include <map>
44 #include <utility>
45
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 /// CFrameworkunifiedCompositeState
48 /// Parameterized constructor
49 ///////////////////////////////////////////////////////////////////////////////////////////
50 CFrameworkunifiedCompositeState::CFrameworkunifiedCompositeState(std::string f_pName): CFrameworkunifiedState(f_pName) {
51   try {
52     // ChildState map stores the pointers to sub state, key is state name
53     m_pChildStates = new ChildStateList();
54
55     m_pDefaultState = NULL;
56
57     m_pActiveState = NULL;
58   } catch (std::exception &e) {
59     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
60     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed in %s state ", f_pName.c_str());
61   }
62 }
63
64 ///////////////////////////////////////////////////////////////////////////////////////////
65 /// ~CFrameworkunifiedState
66 /// Class destructor
67 ///////////////////////////////////////////////////////////////////////////////////////////
68 CFrameworkunifiedCompositeState::~CFrameworkunifiedCompositeState() {
69   FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, "CFrameworkunifiedCompositeState destructor");
70   StateIterator l_objStateIterator;
71   try {
72     CHKNULL(m_pChildStates);
73
74     // Deleting the States
75     for (l_objStateIterator = m_pChildStates->begin();
76          l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
77       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, " deleting the state %s in state %s ",
78              (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->m_strStateName.c_str(),
79              m_strStateName.c_str());
80
81       CHKNULL((*l_objStateIterator).second);
82
83       delete(*l_objStateIterator).second;
84       (*l_objStateIterator).second = NULL;
85     }
86     m_pChildStates->clear();
87     delete m_pChildStates;
88     m_pChildStates = NULL;
89   } catch (std::exception &e) {
90     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
91     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to delete state %s",
92            ((reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->m_strStateName).c_str());
93   }
94 }
95 ///////////////////////////////////////////////////////////////////////////////////////////
96 /// FrameworkunifiedAddState
97 /// sets the given state as a substate of the current state. If the f_eStateType is
98 /// eFrameworkunifiedDefaultState then substate is default state for current state.
99 ///////////////////////////////////////////////////////////////////////////////////////////
100 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedAddState(CFrameworkunifiedState *f_pState,
101                                            FRAMEWORKUNIFIED_STATE_TYPE f_eStateType) {
102   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
103   std::map<std::string, CFrameworkunifiedState *>::iterator l_itChildStates;
104   CFrameworkunifiedHistoryState *l_pHistoryState = NULL;
105   try {
106     CHKNULL(f_pState);
107
108     CHKNULL(m_pChildStates);
109
110     // Set current state as the parent state of given state object
111     f_pState->m_pParentState = this;
112
113     // if given state is default state then set it as the default and active state of parent
114     // state
115     if (eFrameworkunifiedDefaultState == f_eStateType) {
116       m_pDefaultState = f_pState;
117       m_pActiveState = f_pState;
118
119       // set default shallow history state
120       l_itChildStates = m_pChildStates->find(SHALLOWHISTORYSTATE);
121       if (m_pChildStates->end() !=  l_itChildStates) {
122         l_pHistoryState = static_cast<CFrameworkunifiedHistoryState *>((*l_itChildStates).second);
123         CHKNULL(l_pHistoryState);
124         eStatus = l_pHistoryState->SetDefaultHistory();
125       }
126
127       // set default deep history state
128       l_itChildStates = m_pChildStates->find(DEEPHISTORYSTATE);
129       if (m_pChildStates->end() !=  l_itChildStates) {
130         l_pHistoryState = static_cast<CFrameworkunifiedHistoryState *>((*l_itChildStates).second);
131         CHKNULL(l_pHistoryState);
132         eStatus = l_pHistoryState->SetDefaultHistory();
133       }
134     }
135
136     // Insert the state in the map with key as state name
137     m_pChildStates->insert(std::pair<std::string, CFrameworkunifiedState *>(f_pState->m_strStateName, f_pState));
138
139     FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " %s state added in the state %s "
140            , (f_pState->m_strStateName).c_str(), (this->m_strStateName).c_str());
141   } catch (std::exception &e) {
142     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
143     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to add state %s",
144            f_pState->m_strStateName.c_str());
145
146     eStatus = eFrameworkunifiedStatusNullPointer;
147   }
148
149   return eStatus;
150 }
151 ///////////////////////////////////////////////////////////////////////////////////////////
152 /// FrameworkunifiedGetDefaultState
153 /// Returns the default state of the current composite state.
154 ///////////////////////////////////////////////////////////////////////////////////////////
155 CFrameworkunifiedState *CFrameworkunifiedCompositeState::FrameworkunifiedGetDefaultState() {
156   return m_pDefaultState;
157 }
158
159 ///////////////////////////////////////////////////////////////////////////////////////////
160 /// FrameworkunifiedOnEntry
161 /// state initialization can be performed in this function.
162 ///////////////////////////////////////////////////////////////////////////////////////////
163 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedOnEntry(CEventDataPtr f_pEventData) {
164   FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Entering state %s ", m_strStateName.c_str());
165   return eFrameworkunifiedStatusOK;
166 }
167
168 ///////////////////////////////////////////////////////////////////////////////////////////
169 /// FrameworkunifiedOnExit
170 /// state cleanup can be performed in this function.
171 ///////////////////////////////////////////////////////////////////////////////////////////
172 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedOnExit(CEventDataPtr f_pEventData) {
173   FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Leaving state %s ", m_strStateName.c_str());
174   return eFrameworkunifiedStatusOK;
175 }
176
177
178 ///////////////////////////////////////////////////////////////////////////////////////////
179 /// FrameworkunifiedPrintStates
180 /// This logs the state name and events associated with the state
181 ///////////////////////////////////////////////////////////////////////////////////////////
182 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedPrintStates() {
183   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
184   StateIterator l_objStateIterator;
185   EventReactionIterator l_objEventIterator;
186   try {
187     CHKNULL(m_pEventList);
188
189     if (m_pParentState) {
190       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s:%s",
191              (m_pParentState->m_strStateName).c_str(), m_strStateName.c_str());
192     } else {
193       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "NULL:%s", m_strStateName.c_str());
194     }
195
196     // print event
197     for (l_objEventIterator = m_pEventList->begin();
198          l_objEventIterator != m_pEventList->end(); l_objEventIterator++) {
199       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "event %d %s", ((*l_objEventIterator).first),
200              (m_pEventName->find((*l_objEventIterator).first)->second).c_str());
201     }
202     CHKNULL(m_pDeferredEventList);
203     // print deferred events
204     for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
205       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "deferred event %d %s", m_pDeferredEventList->at(l_uiCount),
206              (m_pEventName->find(m_pDeferredEventList->at(l_uiCount))->second).c_str());
207     }
208     CHKNULL(m_pChildStates);
209     // print states
210     for (l_objStateIterator = m_pChildStates->begin();
211          l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
212       (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedPrintStates();
213     }
214   } catch (std::exception &e) {
215     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
216     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to print in state %s",
217            m_strStateName.c_str());
218     eStatus = eFrameworkunifiedStatusNullPointer;
219   }
220   return eStatus;;
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////////////////
224 /// FrameworkunifiedHasSubStates
225 /// This indicates if the state has sub states. It returns TRUE only in the CompositeState
226 /// where this function is overridden
227 ///////////////////////////////////////////////////////////////////////////////////////////
228 BOOL CFrameworkunifiedCompositeState::FrameworkunifiedHasSubStates() {
229   return TRUE;
230 }
231
232 CFrameworkunifiedState *CFrameworkunifiedCompositeState::FrameworkunifiedGetActiveState() {
233   CFrameworkunifiedState *l_pActiveState = NULL;
234   if (NULL != m_pActiveState) {
235     l_pActiveState = m_pActiveState->FrameworkunifiedGetActiveState();
236   } else {
237     l_pActiveState = this;
238   }
239   return l_pActiveState;
240 }
241
242 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedSetHSM(CFrameworkunifiedHSM *f_pStatemachine) {
243   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
244   StateIterator l_objStateIterator;
245   try {
246     CHKNULL(f_pStatemachine);
247     // iterate child states
248     for (l_objStateIterator = m_pChildStates->begin();
249          l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
250       (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedSetHSM(f_pStatemachine);
251     }
252     m_pStateMachine = f_pStatemachine;
253   } catch (std::exception &e) {
254     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
255     l_eStatus = eFrameworkunifiedStatusNullPointer;
256   }
257   return l_eStatus;
258 }
259
260 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedPrintXML(std::ostringstream &f_strXMLString) {
261   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
262   EventReactionIterator l_objEventIterator;
263   StateIterator l_objStateIterator;
264   try {
265     CHKNULL(m_pEventList);
266     CHKNULL(m_pEventName);
267     CHKNULL(m_pDeferredEventList);
268
269     f_strXMLString << "<" << m_strStateName.c_str() << ">";
270
271     CHKNULL(m_pChildStates);
272     f_strXMLString << "<ChildStates>";
273     // print states
274     for (l_objStateIterator = m_pChildStates->begin();
275          l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
276       (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedPrintXML(f_strXMLString);
277     }
278
279     f_strXMLString << "</ChildStates>";
280
281     f_strXMLString << "<EventList>";
282     // print events
283     for (l_objEventIterator = m_pEventList->begin();
284          l_objEventIterator != m_pEventList->end(); l_objEventIterator++) {
285       std::string l_strEventName =
286         (m_pEventName->find((*l_objEventIterator).first)->second);
287
288       UI_32 l_uiEventId = (*l_objEventIterator).first;
289
290       f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";
291
292       f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";
293
294       f_strXMLString << "</Event>";
295     }
296     f_strXMLString << "</EventList>";
297
298     // print deferred events
299     f_strXMLString << "<DeferredEventList>";
300     for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
301       UI_32 l_uiEventId = m_pDeferredEventList->at(l_uiCount);
302
303       std::string l_strEventName = (m_pEventName->find(l_uiEventId)->second);
304
305       f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";
306
307       f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";
308
309       f_strXMLString << "</Event>";
310     }
311
312     f_strXMLString << "</DeferredEventList>";
313     f_strXMLString << "</" << m_strStateName.c_str() << ">";
314   } catch (std::exception &e) {
315     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
316     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to print in state %s", m_strStateName.c_str());
317     l_eStatus = eFrameworkunifiedStatusNullPointer;
318   }
319   return l_eStatus;
320 }
321
322 ////////////////////////////////////////////////////////////////////////////////////////////////////
323 /// UpdateHistory
324 /// This function stores the last active state
325 ////////////////////////////////////////////////////////////////////////////////////////////////////
326 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::UpdateHistory() {
327   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
328
329   // update shallow history state
330   l_eStatus = CheckHistory(SHALLOWHISTORYSTATE);
331
332   if (eFrameworkunifiedStatusOK != l_eStatus) {
333     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s Shallow History Update Failed", m_strStateName.c_str());
334     return l_eStatus;
335   }
336
337   // update deep history state
338   l_eStatus = CheckHistory(DEEPHISTORYSTATE);
339
340   if (eFrameworkunifiedStatusOK != l_eStatus) {
341     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s Deep History Update Failed", m_strStateName.c_str());
342     return l_eStatus;
343   }
344
345   return l_eStatus;
346 }
347
348 ////////////////////////////////////////////////////////////////////////////////////////////////////
349 /// CheckHistory
350 /// This function searches for history state in this composite state and updates it.
351 ////////////////////////////////////////////////////////////////////////////////////////////////////
352 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::CheckHistory(std::string l_cHistoryType) {
353   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
354
355   CHKNULL(m_pChildStates);
356
357   CFrameworkunifiedState *l_pHistoryState = NULL;
358
359   std::map<std::string, CFrameworkunifiedState *>::iterator l_itChildStates;
360
361   // update history state
362   l_itChildStates = m_pChildStates->find(l_cHistoryType);
363
364   if (m_pChildStates->end() !=  l_itChildStates) {
365     l_pHistoryState = (*l_itChildStates).second;
366     CHKNULL(l_pHistoryState);
367     l_eStatus = l_pHistoryState->UpdateHistory();
368   }
369
370   return l_eStatus;
371 }