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 CFrameworkunifiedOrthogonalState class definitions. CFrameworkunifiedOrthogonalState is derived from
28 /// CFrameworkunifiedCompositeState class.This class implements the additional functionality supported by HSM
29 /// Orthogonal state. It provides the standard interfaces for adding orthogonal state machines.
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
33 #include <native_service/frameworkunified_sm_orthogonalstate.h>
34 #include <native_service/frameworkunified_sm_compositestate.h>
35 #include <native_service/frameworkunified_sm_framework_types.h>
36 #include <native_service/frameworkunified_sm_hsm.h>
40 ///////////////////////////////////////////////////////////////////////////////////////////
41 /// CFrameworkunifiedCompositeState
42 /// Parameterized constructor
43 ///////////////////////////////////////////////////////////////////////////////////////////
44 CFrameworkunifiedOrthogonalState::CFrameworkunifiedOrthogonalState(std::string f_pName): CFrameworkunifiedState(f_pName) {
46 m_pOrthogonalReigonList = new OrthogonalRegionList();
47 } catch (std::exception &e) {
48 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
52 ///////////////////////////////////////////////////////////////////////////////////////////
53 /// ~CFrameworkunifiedOrthogonalState
55 ///////////////////////////////////////////////////////////////////////////////////////////
56 CFrameworkunifiedOrthogonalState::~CFrameworkunifiedOrthogonalState() {
57 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
58 if (m_pOrthogonalReigonList->at(l_uiCount)) {
59 delete m_pOrthogonalReigonList->at(l_uiCount);
62 m_pOrthogonalReigonList->clear();
63 FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "CFrameworkunifiedOrthogonalState destructor");
65 ///////////////////////////////////////////////////////////////////////////////////////////
66 /// FrameworkunifiedOnEntry
67 /// state initialization can be performed in this function.
68 ///////////////////////////////////////////////////////////////////////////////////////////
69 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedOnEntry(CEventDataPtr f_pEventData) {
70 FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Entering state %s ", m_strStateName.c_str());
71 return eFrameworkunifiedStatusOK;
73 ///////////////////////////////////////////////////////////////////////////////////////////
74 /// FrameworkunifiedOnExit
75 /// state cleanup can be performed in this function.
76 ///////////////////////////////////////////////////////////////////////////////////////////
77 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedOnExit(CEventDataPtr f_pEventData) {
78 FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Leaving state %s ", m_strStateName.c_str());
79 return eFrameworkunifiedStatusOK;
82 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedAddOrthogonalRegion(CFrameworkunifiedCompositeState *f_pOrthogonalRegion) {
84 CHKNULL(f_pOrthogonalRegion);
85 CHKNULL(m_pOrthogonalReigonList);
86 f_pOrthogonalRegion->m_pParentState = this;
87 m_pOrthogonalReigonList->push_back(f_pOrthogonalRegion);
88 } catch (std::exception &e) {
89 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
90 return eFrameworkunifiedStatusNullPointer;
93 return eFrameworkunifiedStatusOK;
96 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedOnHSMStart(CEventDataPtr f_pEventData) {
97 CFrameworkunifiedState *l_pCurrentState = NULL;
98 CFrameworkunifiedState *l_pActiveState = NULL;
101 FrameworkunifiedOnEntry(f_pEventData);
103 CHKNULL(m_pOrthogonalReigonList);
104 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
105 if (m_pOrthogonalReigonList->at(l_uiCount)) {
106 l_pActiveState = (m_pOrthogonalReigonList->at(l_uiCount));
107 CHKNULL(l_pActiveState);
109 l_pCurrentState = l_pActiveState->FrameworkunifiedOnHSMStart(f_pEventData);
110 CHKNULL(l_pCurrentState);
112 if (!IsOrthogonalChildState(l_pCurrentState)) {
115 l_pCurrentState = this;
120 // set current state as the active state of its parent state to maintain the Hierarchy
121 if (m_pParentState) {
122 m_pParentState->m_pActiveState = l_pCurrentState;
124 } catch (std::exception &e) {
125 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
129 return l_pCurrentState;
132 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedOnHSMStop(CEventDataPtr f_pEventData) {
133 CFrameworkunifiedState *l_pCurrentState = NULL;
134 CFrameworkunifiedState *l_pActiveState = NULL;
137 CHKNULL(m_pOrthogonalReigonList);
138 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
139 if (m_pOrthogonalReigonList->at(l_uiCount)) {
140 l_pActiveState = (m_pOrthogonalReigonList->at(l_uiCount));
141 CHKNULL(l_pActiveState);
143 l_pCurrentState = l_pActiveState->FrameworkunifiedOnHSMStop(f_pEventData);
144 CHKNULL(l_pCurrentState);
146 if (!IsOrthogonalChildState(l_pCurrentState)) {
149 l_pCurrentState = this;
153 FrameworkunifiedOnExit(f_pEventData);
154 } catch (std::exception &e) {
155 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
159 return l_pCurrentState;
162 ///////////////////////////////////////////////////////////////////////////////////////////
163 /// FrameworkunifiedOnEvent
164 /// This function processes the event. If the reaction for event is available in the current
165 /// state within eventlist and deferred eventlist then it is consumed in the current state
166 /// otherwise forwarded to the parent state. Event forwarding is done recursively till either
167 /// event is consumed or the root state has encountered. This also process the events posted
168 /// in the reactions recursively till all posted events are cleared.
169 ///////////////////////////////////////////////////////////////////////////////////////////
170 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedOnEvent(CEventDataPtr f_pEventData) {
171 CFrameworkunifiedState *l_pCurrentState = NULL;
172 CFrameworkunifiedState *l_pStateIterator = NULL;
173 CFrameworkunifiedState *l_pOrthogonalRegion = NULL;
174 BOOL l_bReactionAvailable = FALSE;
175 BOOL l_bIsEventProcessed = FALSE;
178 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
179 l_pOrthogonalRegion = m_pOrthogonalReigonList->at(l_uiCount);
181 if (l_pOrthogonalRegion) {
182 // get the current active state
183 l_pCurrentState = l_pOrthogonalRegion->FrameworkunifiedGetActiveState();
184 CHKNULL(l_pCurrentState);
186 l_pStateIterator = l_pCurrentState;
188 // checks whether the reaction for the event is available in this orthogonal region
189 while (this != l_pStateIterator) {
190 if (l_pStateIterator->FrameworkunifiedIsReactionAvailable(f_pEventData->m_uiEventId)) {
191 l_bReactionAvailable = TRUE;
192 l_bIsEventProcessed = TRUE;
196 // iterate to parent state in orthogonal region
197 l_pStateIterator = l_pStateIterator->m_pParentState;
200 // if reaction is found, post the event
201 if (l_bReactionAvailable) {
202 l_bReactionAvailable = FALSE;
204 l_pCurrentState = l_pCurrentState->FrameworkunifiedOnEvent(f_pEventData);
205 CHKNULL(l_pCurrentState);
207 // check whether current active state is within the orthogonal state
208 if (IsOrthogonalChildState(l_pCurrentState)) {
209 l_pCurrentState = this;
214 FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "Reaction not available in orthogonal region %s",
215 l_pOrthogonalRegion->m_strStateName.c_str());
216 l_pCurrentState = this;
221 // if event is not processed in any of orthogonal region, post the event to orthogonal state
222 if (!l_bIsEventProcessed) {
223 l_pCurrentState = CFrameworkunifiedState::FrameworkunifiedOnEvent(f_pEventData);
225 // check whether current active state is within the orthogonal state
226 if (IsOrthogonalChildState(l_pCurrentState)) {
227 l_pCurrentState = this;
230 } catch (std::exception &e) {
231 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
235 return l_pCurrentState;
238 BOOL CFrameworkunifiedOrthogonalState::FrameworkunifiedHasOrthogoanlRegions() {
240 CHKNULL(m_pOrthogonalReigonList);
242 if (m_pOrthogonalReigonList->size()) {
247 } catch (std::exception &e) {
248 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
253 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedPrintStates() {
254 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
257 FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s:%s",
258 (m_pParentState->m_strStateName).c_str(), m_strStateName.c_str());
260 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
261 if (m_pOrthogonalReigonList->at(l_uiCount)) {
262 m_pOrthogonalReigonList->at(l_uiCount)->FrameworkunifiedPrintStates();
265 } catch (std::exception &e) {
266 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
267 return eFrameworkunifiedStatusNullPointer;
272 BOOL CFrameworkunifiedOrthogonalState::IsOrthogonalChildState(CFrameworkunifiedState *f_pChildState) {
273 CFrameworkunifiedState *l_pParentState = f_pChildState;
274 BOOL l_bIsOrthgonalChild = FALSE;
275 while (l_pParentState) {
276 if (this == l_pParentState) {
277 l_bIsOrthgonalChild = TRUE;
281 l_pParentState = l_pParentState->m_pParentState;
284 return l_bIsOrthgonalChild;
287 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedGetActiveState() {
291 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedSetHSM(CFrameworkunifiedHSM *f_pStatemachine) {
292 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
294 CHKNULL(f_pStatemachine);
295 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size();
297 if (m_pOrthogonalReigonList->at(l_uiCount)) {
298 m_pOrthogonalReigonList->at(l_uiCount)->FrameworkunifiedSetHSM(f_pStatemachine);
301 m_pStateMachine = f_pStatemachine;
302 } catch (std::exception &e) {
303 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
304 l_eStatus = eFrameworkunifiedStatusNullPointer;
309 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedPrintXML(std::ostringstream &f_strXMLString) {
310 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
313 f_strXMLString << "<" << m_strStateName.c_str() << ">";
314 f_strXMLString << "<OrthogonalRegions>";
316 for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
317 if (m_pOrthogonalReigonList->at(l_uiCount)) {
318 m_pOrthogonalReigonList->at(l_uiCount)->FrameworkunifiedPrintXML(f_strXMLString);
322 f_strXMLString << "</OrthogonalRegions>";
323 f_strXMLString << "</" << m_strStateName.c_str() << ">";
324 } catch (std::exception &e) {
325 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
326 return eFrameworkunifiedStatusNullPointer;
331 ////////////////////////////////////////////////////////////////////////////////////////////////////
333 /// This function stores the last active state
334 ////////////////////////////////////////////////////////////////////////////////////////////////////
335 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::UpdateHistory() {
336 return eFrameworkunifiedStatusOK;