2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup tag_NSFramework
21 ///////////////////////////////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup tag_NSFramework
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.
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////////////////////////////
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>
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 /// CFrameworkunifiedCompositeState
48 /// Parameterized constructor
49 ///////////////////////////////////////////////////////////////////////////////////////////
50 CFrameworkunifiedCompositeState::CFrameworkunifiedCompositeState(std::string f_pName): CFrameworkunifiedState(f_pName) {
52 // ChildState map stores the pointers to sub state, key is state name
53 m_pChildStates = new ChildStateList();
55 m_pDefaultState = NULL;
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());
64 ///////////////////////////////////////////////////////////////////////////////////////////
65 /// ~CFrameworkunifiedState
67 ///////////////////////////////////////////////////////////////////////////////////////////
68 CFrameworkunifiedCompositeState::~CFrameworkunifiedCompositeState() {
69 FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, "CFrameworkunifiedCompositeState destructor");
70 StateIterator l_objStateIterator;
72 CHKNULL(m_pChildStates);
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());
81 CHKNULL((*l_objStateIterator).second);
83 delete(*l_objStateIterator).second;
84 (*l_objStateIterator).second = NULL;
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());
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;
108 CHKNULL(m_pChildStates);
110 // Set current state as the parent state of given state object
111 f_pState->m_pParentState = this;
113 // if given state is default state then set it as the default and active state of parent
115 if (eFrameworkunifiedDefaultState == f_eStateType) {
116 m_pDefaultState = f_pState;
117 m_pActiveState = f_pState;
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();
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();
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));
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());
146 eStatus = eFrameworkunifiedStatusNullPointer;
151 ///////////////////////////////////////////////////////////////////////////////////////////
152 /// FrameworkunifiedGetDefaultState
153 /// Returns the default state of the current composite state.
154 ///////////////////////////////////////////////////////////////////////////////////////////
155 CFrameworkunifiedState *CFrameworkunifiedCompositeState::FrameworkunifiedGetDefaultState() {
156 return m_pDefaultState;
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;
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;
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;
187 CHKNULL(m_pEventList);
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());
193 FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "NULL:%s", m_strStateName.c_str());
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());
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());
208 CHKNULL(m_pChildStates);
210 for (l_objStateIterator = m_pChildStates->begin();
211 l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
212 (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedPrintStates();
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;
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() {
232 CFrameworkunifiedState *CFrameworkunifiedCompositeState::FrameworkunifiedGetActiveState() {
233 CFrameworkunifiedState *l_pActiveState = NULL;
234 if (NULL != m_pActiveState) {
235 l_pActiveState = m_pActiveState->FrameworkunifiedGetActiveState();
237 l_pActiveState = this;
239 return l_pActiveState;
242 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedSetHSM(CFrameworkunifiedHSM *f_pStatemachine) {
243 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
244 StateIterator l_objStateIterator;
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);
252 m_pStateMachine = f_pStatemachine;
253 } catch (std::exception &e) {
254 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
255 l_eStatus = eFrameworkunifiedStatusNullPointer;
260 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedPrintXML(std::ostringstream &f_strXMLString) {
261 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
262 EventReactionIterator l_objEventIterator;
263 StateIterator l_objStateIterator;
265 CHKNULL(m_pEventList);
266 CHKNULL(m_pEventName);
267 CHKNULL(m_pDeferredEventList);
269 f_strXMLString << "<" << m_strStateName.c_str() << ">";
271 CHKNULL(m_pChildStates);
272 f_strXMLString << "<ChildStates>";
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);
279 f_strXMLString << "</ChildStates>";
281 f_strXMLString << "<EventList>";
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);
288 UI_32 l_uiEventId = (*l_objEventIterator).first;
290 f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";
292 f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";
294 f_strXMLString << "</Event>";
296 f_strXMLString << "</EventList>";
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);
303 std::string l_strEventName = (m_pEventName->find(l_uiEventId)->second);
305 f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";
307 f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";
309 f_strXMLString << "</Event>";
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;
322 ////////////////////////////////////////////////////////////////////////////////////////////////////
324 /// This function stores the last active state
325 ////////////////////////////////////////////////////////////////////////////////////////////////////
326 EFrameworkunifiedStatus CFrameworkunifiedCompositeState::UpdateHistory() {
327 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
329 // update shallow history state
330 l_eStatus = CheckHistory(SHALLOWHISTORYSTATE);
332 if (eFrameworkunifiedStatusOK != l_eStatus) {
333 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s Shallow History Update Failed", m_strStateName.c_str());
337 // update deep history state
338 l_eStatus = CheckHistory(DEEPHISTORYSTATE);
340 if (eFrameworkunifiedStatusOK != l_eStatus) {
341 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s Deep History Update Failed", m_strStateName.c_str());
348 ////////////////////////////////////////////////////////////////////////////////////////////////////
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;
355 CHKNULL(m_pChildStates);
357 CFrameworkunifiedState *l_pHistoryState = NULL;
359 std::map<std::string, CFrameworkunifiedState *>::iterator l_itChildStates;
361 // update history state
362 l_itChildStates = m_pChildStates->find(l_cHistoryType);
364 if (m_pChildStates->end() != l_itChildStates) {
365 l_pHistoryState = (*l_itChildStates).second;
366 CHKNULL(l_pHistoryState);
367 l_eStatus = l_pHistoryState->UpdateHistory();