summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_FrameworkCore/src/statemachine/frameworkunified_sm_compositestate.cpp
blob: d975e93053c02ffb085265c90f9c730ba31978e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

///////////////////////////////////////////////////////////////////////////////////////////////////
/// \defgroup <<Group Tag>> <<Group Name>>
/// \ingroup  tag_NSFramework
/// .
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NSFramework
/// \brief
///
/// This file has the CFrameworkunifiedCompositeState class definitions. CFrameworkunifiedCompositeState is derived from C
/// FrameworkunifiedState class.This class implements the additional functionality supported by HSM Composite
/// state. It provides the standard interfaces for adding state.
///
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <native_service/frameworkunified_sm_compositestate.h>
#include <native_service/frameworkunified_sm_framework_types.h>
#include <native_service/frameworkunified_sm_hsm.h>
#include <native_service/frameworkunified_sm_historystate.h>

#include <sstream>
#include <string>
#include <map>
#include <utility>

///////////////////////////////////////////////////////////////////////////////////////////
/// CFrameworkunifiedCompositeState
/// Parameterized constructor
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedCompositeState::CFrameworkunifiedCompositeState(std::string f_pName): CFrameworkunifiedState(f_pName) {
  try {
    // ChildState map stores the pointers to sub state, key is state name
    m_pChildStates = new ChildStateList();

    m_pDefaultState = NULL;

    m_pActiveState = NULL;
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed in %s state ", f_pName.c_str());
  }
}

///////////////////////////////////////////////////////////////////////////////////////////
/// ~CFrameworkunifiedState
/// Class destructor
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedCompositeState::~CFrameworkunifiedCompositeState() {
  FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, "CFrameworkunifiedCompositeState destructor");
  StateIterator l_objStateIterator;
  try {
    CHKNULL(m_pChildStates);

    // Deleting the States
    for (l_objStateIterator = m_pChildStates->begin();
         l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_DEV_INFO, __FUNCTION__, " deleting the state %s in state %s ",
             (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->m_strStateName.c_str(),
             m_strStateName.c_str());

      CHKNULL((*l_objStateIterator).second);

      delete(*l_objStateIterator).second;
      (*l_objStateIterator).second = NULL;
    }
    m_pChildStates->clear();
    delete m_pChildStates;
    m_pChildStates = NULL;
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to delete state %s",
           ((reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->m_strStateName).c_str());
  }
}
///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedAddState
/// sets the given state as a substate of the current state. If the f_eStateType is
/// eFrameworkunifiedDefaultState then substate is default state for current state.
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedAddState(CFrameworkunifiedState *f_pState,
                                           FRAMEWORKUNIFIED_STATE_TYPE f_eStateType) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  std::map<std::string, CFrameworkunifiedState *>::iterator l_itChildStates;
  CFrameworkunifiedHistoryState *l_pHistoryState = NULL;
  try {
    CHKNULL(f_pState);

    CHKNULL(m_pChildStates);

    // Set current state as the parent state of given state object
    f_pState->m_pParentState = this;

    // if given state is default state then set it as the default and active state of parent
    // state
    if (eFrameworkunifiedDefaultState == f_eStateType) {
      m_pDefaultState = f_pState;
      m_pActiveState = f_pState;

      // set default shallow history state
      l_itChildStates = m_pChildStates->find(SHALLOWHISTORYSTATE);
      if (m_pChildStates->end() !=  l_itChildStates) {
        l_pHistoryState = static_cast<CFrameworkunifiedHistoryState *>((*l_itChildStates).second);
        CHKNULL(l_pHistoryState);
        eStatus = l_pHistoryState->SetDefaultHistory();
      }

      // set default deep history state
      l_itChildStates = m_pChildStates->find(DEEPHISTORYSTATE);
      if (m_pChildStates->end() !=  l_itChildStates) {
        l_pHistoryState = static_cast<CFrameworkunifiedHistoryState *>((*l_itChildStates).second);
        CHKNULL(l_pHistoryState);
        eStatus = l_pHistoryState->SetDefaultHistory();
      }
    }

    // Insert the state in the map with key as state name
    m_pChildStates->insert(std::pair<std::string, CFrameworkunifiedState *>(f_pState->m_strStateName, f_pState));

    FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " %s state added in the state %s "
           , (f_pState->m_strStateName).c_str(), (this->m_strStateName).c_str());
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to add state %s",
           f_pState->m_strStateName.c_str());

    eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return eStatus;
}
///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedGetDefaultState
/// Returns the default state of the current composite state.
///////////////////////////////////////////////////////////////////////////////////////////
CFrameworkunifiedState *CFrameworkunifiedCompositeState::FrameworkunifiedGetDefaultState() {
  return m_pDefaultState;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedOnEntry
/// state initialization can be performed in this function.
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedOnEntry(CEventDataPtr f_pEventData) {
  FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Entering state %s ", m_strStateName.c_str());
  return eFrameworkunifiedStatusOK;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedOnExit
/// state cleanup can be performed in this function.
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedOnExit(CEventDataPtr f_pEventData) {
  FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, " Leaving state %s ", m_strStateName.c_str());
  return eFrameworkunifiedStatusOK;
}


///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedPrintStates
/// This logs the state name and events associated with the state
///////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedPrintStates() {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  StateIterator l_objStateIterator;
  EventReactionIterator l_objEventIterator;
  try {
    CHKNULL(m_pEventList);

    if (m_pParentState) {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "%s:%s",
             (m_pParentState->m_strStateName).c_str(), m_strStateName.c_str());
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "NULL:%s", m_strStateName.c_str());
    }

    // print event
    for (l_objEventIterator = m_pEventList->begin();
         l_objEventIterator != m_pEventList->end(); l_objEventIterator++) {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "event %d %s", ((*l_objEventIterator).first),
             (m_pEventName->find((*l_objEventIterator).first)->second).c_str());
    }
    CHKNULL(m_pDeferredEventList);
    // print deferred events
    for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
      FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__, "deferred event %d %s", m_pDeferredEventList->at(l_uiCount),
             (m_pEventName->find(m_pDeferredEventList->at(l_uiCount))->second).c_str());
    }
    CHKNULL(m_pChildStates);
    // print states
    for (l_objStateIterator = m_pChildStates->begin();
         l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
      (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedPrintStates();
    }
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to print in state %s",
           m_strStateName.c_str());
    eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return eStatus;;
}

///////////////////////////////////////////////////////////////////////////////////////////
/// FrameworkunifiedHasSubStates
/// This indicates if the state has sub states. It returns TRUE only in the CompositeState
/// where this function is overridden
///////////////////////////////////////////////////////////////////////////////////////////
BOOL CFrameworkunifiedCompositeState::FrameworkunifiedHasSubStates() {
  return TRUE;
}

CFrameworkunifiedState *CFrameworkunifiedCompositeState::FrameworkunifiedGetActiveState() {
  CFrameworkunifiedState *l_pActiveState = NULL;
  if (NULL != m_pActiveState) {
    l_pActiveState = m_pActiveState->FrameworkunifiedGetActiveState();
  } else {
    l_pActiveState = this;
  }
  return l_pActiveState;
}

EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedSetHSM(CFrameworkunifiedHSM *f_pStatemachine) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  StateIterator l_objStateIterator;
  try {
    CHKNULL(f_pStatemachine);
    // iterate child states
    for (l_objStateIterator = m_pChildStates->begin();
         l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
      (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedSetHSM(f_pStatemachine);
    }
    m_pStateMachine = f_pStatemachine;
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return l_eStatus;
}

EFrameworkunifiedStatus CFrameworkunifiedCompositeState::FrameworkunifiedPrintXML(std::ostringstream &f_strXMLString) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  EventReactionIterator l_objEventIterator;
  StateIterator l_objStateIterator;
  try {
    CHKNULL(m_pEventList);
    CHKNULL(m_pEventName);
    CHKNULL(m_pDeferredEventList);

    f_strXMLString << "<" << m_strStateName.c_str() << ">";

    CHKNULL(m_pChildStates);
    f_strXMLString << "<ChildStates>";
    // print states
    for (l_objStateIterator = m_pChildStates->begin();
         l_objStateIterator != m_pChildStates->end(); l_objStateIterator++) {
      (reinterpret_cast<CFrameworkunifiedState *>((*l_objStateIterator).second))->FrameworkunifiedPrintXML(f_strXMLString);
    }

    f_strXMLString << "</ChildStates>";

    f_strXMLString << "<EventList>";
    // print events
    for (l_objEventIterator = m_pEventList->begin();
         l_objEventIterator != m_pEventList->end(); l_objEventIterator++) {
      std::string l_strEventName =
        (m_pEventName->find((*l_objEventIterator).first)->second);

      UI_32 l_uiEventId = (*l_objEventIterator).first;

      f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";

      f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";

      f_strXMLString << "</Event>";
    }
    f_strXMLString << "</EventList>";

    // print deferred events
    f_strXMLString << "<DeferredEventList>";
    for (UI_32 l_uiCount = 0; l_uiCount < m_pDeferredEventList->size(); l_uiCount++) {
      UI_32 l_uiEventId = m_pDeferredEventList->at(l_uiCount);

      std::string l_strEventName = (m_pEventName->find(l_uiEventId)->second);

      f_strXMLString << "<Event " << "Id = " << "\"" << l_uiEventId << "\">";

      f_strXMLString << "<Name>" << l_strEventName.c_str() << "</Name>";

      f_strXMLString << "</Event>";
    }

    f_strXMLString << "</DeferredEventList>";
    f_strXMLString << "</" << m_strStateName.c_str() << ">";
  } catch (std::exception &e) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to print in state %s", m_strStateName.c_str());
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }
  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// UpdateHistory
/// This function stores the last active state
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedCompositeState::UpdateHistory() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  // update shallow history state
  l_eStatus = CheckHistory(SHALLOWHISTORYSTATE);

  if (eFrameworkunifiedStatusOK != l_eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s Shallow History Update Failed", m_strStateName.c_str());
    return l_eStatus;
  }

  // update deep history state
  l_eStatus = CheckHistory(DEEPHISTORYSTATE);

  if (eFrameworkunifiedStatusOK != l_eStatus) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s Deep History Update Failed", m_strStateName.c_str());
    return l_eStatus;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// CheckHistory
/// This function searches for history state in this composite state and updates it.
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CFrameworkunifiedCompositeState::CheckHistory(std::string l_cHistoryType) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  CHKNULL(m_pChildStates);

  CFrameworkunifiedState *l_pHistoryState = NULL;

  std::map<std::string, CFrameworkunifiedState *>::iterator l_itChildStates;

  // update history state
  l_itChildStates = m_pChildStates->find(l_cHistoryType);

  if (m_pChildStates->end() !=  l_itChildStates) {
    l_pHistoryState = (*l_itChildStates).second;
    CHKNULL(l_pHistoryState);
    l_eStatus = l_pHistoryState->UpdateHistory();
  }

  return l_eStatus;
}