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_orthogonalstate.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 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.
30 ///
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
32
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>
37 #include <sstream>
38 #include <string>
39
40 ///////////////////////////////////////////////////////////////////////////////////////////
41 /// CFrameworkunifiedCompositeState
42 /// Parameterized constructor
43 ///////////////////////////////////////////////////////////////////////////////////////////
44 CFrameworkunifiedOrthogonalState::CFrameworkunifiedOrthogonalState(std::string f_pName): CFrameworkunifiedState(f_pName) {
45   try {
46     m_pOrthogonalReigonList = new OrthogonalRegionList();
47   } catch (std::exception &e) {
48     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
49   }
50 }
51
52 ///////////////////////////////////////////////////////////////////////////////////////////
53 /// ~CFrameworkunifiedOrthogonalState
54 /// Class destructor
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);
60     }
61   }
62   m_pOrthogonalReigonList->clear();
63   FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "CFrameworkunifiedOrthogonalState destructor");
64 }
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;
72 }
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;
80 }
81
82 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedAddOrthogonalRegion(CFrameworkunifiedCompositeState *f_pOrthogonalRegion) {
83   try {
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;
91   }
92
93   return eFrameworkunifiedStatusOK;
94 }
95
96 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedOnHSMStart(CEventDataPtr f_pEventData) {
97   CFrameworkunifiedState *l_pCurrentState = NULL;
98   CFrameworkunifiedState *l_pActiveState = NULL;
99
100   try {
101     FrameworkunifiedOnEntry(f_pEventData);
102
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);
108
109         l_pCurrentState = l_pActiveState->FrameworkunifiedOnHSMStart(f_pEventData);
110         CHKNULL(l_pCurrentState);
111
112         if (!IsOrthogonalChildState(l_pCurrentState)) {
113           break;
114         } else {
115           l_pCurrentState = this;
116         }
117       }
118     }
119
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;
123     }
124   } catch (std::exception &e) {
125     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
126     return NULL;
127   }
128
129   return l_pCurrentState;
130 }
131
132 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedOnHSMStop(CEventDataPtr f_pEventData) {
133   CFrameworkunifiedState *l_pCurrentState = NULL;
134   CFrameworkunifiedState *l_pActiveState = NULL;
135
136   try {
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);
142
143         l_pCurrentState = l_pActiveState->FrameworkunifiedOnHSMStop(f_pEventData);
144         CHKNULL(l_pCurrentState);
145
146         if (!IsOrthogonalChildState(l_pCurrentState)) {
147           break;
148         } else {
149           l_pCurrentState = this;
150         }
151       }
152     }
153     FrameworkunifiedOnExit(f_pEventData);
154   } catch (std::exception &e) {
155     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
156     return NULL;
157   }
158
159   return l_pCurrentState;
160 }
161
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;
176
177   try {
178     for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size(); l_uiCount++) {
179       l_pOrthogonalRegion =  m_pOrthogonalReigonList->at(l_uiCount);
180
181       if (l_pOrthogonalRegion) {
182         // get the current active state
183         l_pCurrentState = l_pOrthogonalRegion->FrameworkunifiedGetActiveState();
184         CHKNULL(l_pCurrentState);
185
186         l_pStateIterator = l_pCurrentState;
187
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;
193             break;
194           }
195
196           // iterate to parent state in orthogonal region
197           l_pStateIterator = l_pStateIterator->m_pParentState;
198         }
199
200         // if reaction is found, post the event
201         if (l_bReactionAvailable) {
202           l_bReactionAvailable = FALSE;
203
204           l_pCurrentState = l_pCurrentState->FrameworkunifiedOnEvent(f_pEventData);
205           CHKNULL(l_pCurrentState);
206
207           // check whether current active state is within the orthogonal state
208           if (IsOrthogonalChildState(l_pCurrentState)) {
209             l_pCurrentState = this;
210           }
211
212           break;
213         } else {
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;
217         }
218       }
219     }
220
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);
224
225       // check whether current active state is within the orthogonal state
226       if (IsOrthogonalChildState(l_pCurrentState)) {
227         l_pCurrentState = this;
228       }
229     }
230   } catch (std::exception &e) {
231     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
232     return NULL;
233   }
234
235   return l_pCurrentState;
236 }
237
238 BOOL CFrameworkunifiedOrthogonalState::FrameworkunifiedHasOrthogoanlRegions() {
239   try {
240     CHKNULL(m_pOrthogonalReigonList);
241
242     if (m_pOrthogonalReigonList->size()) {
243       return TRUE;
244     } else {
245       return FALSE;
246     }
247   } catch (std::exception &e) {
248     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
249     return FALSE;
250   }
251 }
252
253 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedPrintStates() {
254   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
255
256   try {
257     FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s:%s",
258            (m_pParentState->m_strStateName).c_str(), m_strStateName.c_str());
259
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();
263       }
264     }
265   } catch (std::exception &e) {
266     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
267     return eFrameworkunifiedStatusNullPointer;
268   }
269   return l_eStatus;
270 }
271
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;
278       break;
279     }
280
281     l_pParentState = l_pParentState->m_pParentState;
282   }
283
284   return l_bIsOrthgonalChild;
285 }
286
287 CFrameworkunifiedState *CFrameworkunifiedOrthogonalState::FrameworkunifiedGetActiveState() {
288   return this;
289 }
290
291 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedSetHSM(CFrameworkunifiedHSM *f_pStatemachine) {
292   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
293   try {
294     CHKNULL(f_pStatemachine);
295     for (UI_32 l_uiCount = 0; l_uiCount < m_pOrthogonalReigonList->size();
296          l_uiCount++) {
297       if (m_pOrthogonalReigonList->at(l_uiCount)) {
298         m_pOrthogonalReigonList->at(l_uiCount)->FrameworkunifiedSetHSM(f_pStatemachine);
299       }
300     }
301     m_pStateMachine = f_pStatemachine;
302   } catch (std::exception &e) {
303     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
304     l_eStatus = eFrameworkunifiedStatusNullPointer;
305   }
306   return l_eStatus;
307 }
308
309 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::FrameworkunifiedPrintXML(std::ostringstream &f_strXMLString) {
310   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
311
312   try {
313     f_strXMLString << "<" << m_strStateName.c_str() << ">";
314     f_strXMLString << "<OrthogonalRegions>";
315
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);
319       }
320     }
321
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;
327   }
328   return l_eStatus;
329 }
330
331 ////////////////////////////////////////////////////////////////////////////////////////////////////
332 /// UpdateHistory
333 /// This function stores the last active state
334 ////////////////////////////////////////////////////////////////////////////////////////////////////
335 EFrameworkunifiedStatus CFrameworkunifiedOrthogonalState::UpdateHistory() {
336   return eFrameworkunifiedStatusOK;
337 }