FW Profile - C1 Implementation
FwSmCore.c
Go to the documentation of this file.
1 
18 #include "FwSmCore.h"
19 #include "FwSmPrivate.h"
20 #include <stdlib.h>
21 
37 static void ExecTrans(FwSmDesc_t smDesc, SmTrans_t* trans);
38 
39 /* ----------------------------------------------------------------------------------------------------------------- */
40 void SmDummyAction(FwSmDesc_t smDesc) {
41  (void)(smDesc);
42  return;
43 }
44 
45 /* ----------------------------------------------------------------------------------------------------------------- */
47  (void)(smDesc);
48  return 1;
49 }
50 
51 /* ----------------------------------------------------------------------------------------------------------------- */
52 void FwSmStart(FwSmDesc_t smDesc) {
53  SmTrans_t* trans;
54 
55  if (smDesc->curState != 0) { /* Check if SM is already STARTED */
56  return;
57  }
58 
59  /* Reset execution counters */
60  smDesc->smExecCnt = 0;
61  smDesc->stateExecCnt = 0;
62 
63  /* Execution transition into initial state */
64  trans = &(smDesc->smBase->trans[0]);
65  ExecTrans(smDesc, trans);
66 }
67 
68 /* ----------------------------------------------------------------------------------------------------------------- */
69 void FwSmStop(FwSmDesc_t smDesc) {
70  SmPState_t* pState;
71  SmBaseDesc_t* smBase = smDesc->smBase;
72  FwSmCounterS1_t iCurState = smDesc->curState;
73 
74  /* check if state machine (SM) is already stopped */
75  if (iCurState == 0) {
76  return;
77  }
78 
79  pState = &(smBase->pStates[iCurState - 1]); /* get current state */
80  /* If the current state (CS) has an embedded SM (ESM), stop it */
81  if (smDesc->esmDesc[iCurState - 1] != NULL) {
82  FwSmStop(smDesc->esmDesc[iCurState - 1]);
83  }
84  /* execute exit action of current state */
85  smDesc->smActions[pState->iExitAction](smDesc);
86  /* set state of SM to "undefined" */
87  smDesc->curState = 0;
88  return;
89 }
90 
91 /* ----------------------------------------------------------------------------------------------------------------- */
92 void FwSmMakeTrans(FwSmDesc_t smDesc, FwSmCounterU2_t transId) {
94  SmTrans_t* trans;
97  SmBaseDesc_t* smBase = smDesc->smBase;
98 
99  /* check if state machine (SM) is started */
100  if (smDesc->curState == 0) {
101  return;
102  }
103 
104  /* get current state */
105  curState = &(smBase->pStates[(smDesc->curState) - 1]);
106 
107  /* If this is the "execute" transition, increment execution counters and execute do-action */
108  if (transId == FW_TR_EXECUTE) {
109  smDesc->smExecCnt++;
110  smDesc->stateExecCnt++;
111  smDesc->smActions[curState->iDoAction](smDesc);
112  }
113 
114  /* If there is an embedded SM (ESM), propagate transition trigger to it */
115  esmDesc = smDesc->esmDesc[(smDesc->curState) - 1];
116  if (esmDesc != NULL) {
117  FwSmMakeTrans(esmDesc, transId);
118  }
119 
120  /* look for transition from CS matching transition trigger */
121  for (i = 0; i < curState->nOfOutTrans; i++) {
122  trans = &(smBase->trans[curState->outTransIndex + i]);
123  /* check if outgoing transition responds to trigger tr_id */
124  if (trans->id == transId) {
125  /* check if outgoing transition has a true guard */
126  if (smDesc->smGuards[trans->iTrGuard](smDesc) != 0) {
127  /* If CS has an ESM, stop it before exiting the CS */
128  if (esmDesc != NULL) {
129  FwSmStop(esmDesc);
130  }
131  /* Execute exit action of CS */
132  smDesc->smActions[curState->iExitAction](smDesc);
133  ExecTrans(smDesc, trans);
134  return;
135  }
136  }
137  }
138  return;
139 }
140 
141 /* ----------------------------------------------------------------------------------------------------------------- */
142 void FwSmExecute(FwSmDesc_t smDesc) {
143  FwSmMakeTrans(smDesc, FW_TR_EXECUTE);
144 }
145 
146 /* ----------------------------------------------------------------------------------------------------------------- */
147 static void ExecTrans(FwSmDesc_t smDesc, SmTrans_t* trans) {
148  SmPState_t* pDest;
149  SmCState_t* cDest;
150  SmTrans_t* cTrans;
151  FwSmCounterS1_t i;
153  SmBaseDesc_t* smBase = smDesc->smBase;
154 
155  /* execute transition action */
156  smDesc->smActions[trans->iTrAction](smDesc);
157 
158  if (trans->dest > 0) { /* destination is a proper state */
159  smDesc->curState = trans->dest;
160  smDesc->stateExecCnt = 0;
161  pDest = &(smBase->pStates[(trans->dest) - 1]);
162  /* execute entry action of destination state */
163  smDesc->smActions[pDest->iEntryAction](smDesc);
164  esmDesc = smDesc->esmDesc[(trans->dest) - 1];
165  if (esmDesc != NULL) {
166  FwSmStart(esmDesc);
167  }
168  return;
169  }
170 
171  if (trans->dest < 0) { /* destination is a choice pseudo-state */
172  cDest = &(smBase->cStates[-(trans->dest) - 1]);
173  for (i = 0; i < cDest->nOfOutTrans; i++) {
174  cTrans = &(smBase->trans[cDest->outTransIndex + i]);
175  if (smDesc->smGuards[cTrans->iTrGuard](smDesc) != 0) {
176  /* Execute transition from choice pseudo-state */
177  smDesc->smActions[cTrans->iTrAction](smDesc);
178  if (cTrans->dest > 0) { /* destination is a proper state */
179  smDesc->curState = cTrans->dest;
180  smDesc->stateExecCnt = 0;
181  pDest = &(smBase->pStates[(cTrans->dest) - 1]);
182  /* execute entry action of destination state */
183  smDesc->smActions[pDest->iEntryAction](smDesc);
184  esmDesc = smDesc->esmDesc[(cTrans->dest) - 1];
185  if (esmDesc != NULL) {
186  FwSmStart(esmDesc);
187  }
188  return;
189  }
190 
191  if (cTrans->dest == 0) { /* destination is a final state */
192  smDesc->curState = 0;
193  return;
194  }
195 
196  break; /* this point is reached only if there is a transition from a CPS to a CPS */
197  }
198  }
199  smDesc->errCode = smTransErr;
200  }
201  else { /* destination is a final pseudo-state */
202  smDesc->curState = 0;
203  return;
204  }
205 }
206 
207 /* ----------------------------------------------------------------------------------------------------------------- */
209  if (smDesc->curState > 0) {
210  return smDesc->esmDesc[(smDesc->curState) - 1];
211  }
212 
213  return NULL;
214 }
215 
216 /* ----------------------------------------------------------------------------------------------------------------- */
218  return smDesc->esmDesc[i - 1];
219 }
220 
221 /* ----------------------------------------------------------------------------------------------------------------- */
223  return smDesc->curState;
224 }
225 
226 /* ----------------------------------------------------------------------------------------------------------------- */
228  if (smDesc->curState == 0) {
229  return -1;
230  }
231 
232  if (smDesc->esmDesc[(smDesc->curState) - 1] != NULL) {
233  return smDesc->esmDesc[(smDesc->curState) - 1]->curState;
234  }
235 
236  return -1;
237 }
238 
239 /* ----------------------------------------------------------------------------------------------------------------- */
241  if (smDesc->curState != 0) {
242  return 1;
243  }
244  return 0;
245 }
246 
247 /* ----------------------------------------------------------------------------------------------------------------- */
249  return smDesc->errCode;
250 }
251 
252 /* ----------------------------------------------------------------------------------------------------------------- */
254  return smDesc->smExecCnt;
255 }
256 
257 /* ----------------------------------------------------------------------------------------------------------------- */
259  return smDesc->stateExecCnt;
260 }
FwSmBool_t SmDummyGuard(FwSmDesc_t smDesc)
Dummy guard which always returns true.
Definition: FwSmCore.c:46
FwSmCounterS1_t curState
the current state of the state machine
Definition: FwSmPrivate.h:319
FwSmCounterS1_t outTransIndex
index of first out-going transition in transition array of SmBaseDesc_t
Definition: FwSmPrivate.h:151
FwSmDesc_t FwSmGetEmbSmCur(FwSmDesc_t smDesc)
Return the state machine embedded in the current state.
Definition: FwSmCore.c:208
#define FW_TR_EXECUTE
Identifier of "Execute" transition in a state machine.
FwSmCounterS1_t dest
the index of the destination of the transition
Definition: FwSmPrivate.h:197
FwSmCounterS1_t nOfOutTrans
number of outgoing transitions from the choice pseudo-state
Definition: FwSmPrivate.h:153
FwSmCounterS1_t iTrGuard
the index of the guard associated to the transition
Definition: FwSmPrivate.h:203
FwSmErrCode_t
Error codes and function return codes for the state machine functions.
Definition: FwSmConstants.h:82
FwSmBool_t FwSmIsStarted(FwSmDesc_t smDesc)
Check whether the state machine is started.
Definition: FwSmCore.c:240
Declaration of the execution interface for a FW State Machine.
FwSmErrCode_t FwSmGetErrCode(FwSmDesc_t smDesc)
Return the error code of the argument state machine.
Definition: FwSmCore.c:248
FwSmAction_t * smActions
the state machine actions (state and transition actions)
Definition: FwSmPrivate.h:307
FwSmCounterS1_t iDoAction
the do action for the state
Definition: FwSmPrivate.h:129
An error was encountered while executing a transition in a state machine (see FwSmMakeTrans).
FwSmCounterU3_t FwSmGetExecCnt(FwSmDesc_t smDesc)
Return the State Machine Execution Counter.
Definition: FwSmCore.c:253
void FwSmStop(FwSmDesc_t smDesc)
Stop a state machine.
Definition: FwSmCore.c:69
FwSmCounterU3_t FwSmGetStateExecCnt(FwSmDesc_t smDesc)
Return the State Execution Counter.
Definition: FwSmCore.c:258
void FwSmExecute(FwSmDesc_t smDesc)
Convenience method to execute a state machine.
Definition: FwSmCore.c:142
SmCState_t * cStates
array holding the choice pseudo-states in the state machine
Definition: FwSmPrivate.h:239
FwSmCounterU2_t id
the identifier (the "name") of the transition
Definition: FwSmPrivate.h:199
FwSmErrCode_t errCode
either &#39;success&#39; or the code of the last error encountered by the state machine
Definition: FwSmPrivate.h:325
FwSmCounterS1_t outTransIndex
index of first out-going transition in the transition array of SmBaseDesc_t
Definition: FwSmPrivate.h:123
struct FwSmDesc ** esmDesc
the state machines embedded in the state machine
Definition: FwSmPrivate.h:311
int FwSmBool_t
Type used for booleans (0 is "false" and 1 is "true").
Definition: FwSmConstants.h:49
FwSmCounterU3_t smExecCnt
the state machine execution counter
Definition: FwSmPrivate.h:321
FwSmCounterS1_t nOfOutTrans
number of outgoing transitions
Definition: FwSmPrivate.h:125
unsigned short int FwSmCounterU2_t
Type used for unsigned counters with a "medium" range.
Definition: FwSmConstants.h:70
unsigned int FwSmCounterU3_t
Type used for unsigned counters with a "long" range.
Definition: FwSmConstants.h:73
FwSmDesc_t FwSmGetEmbSm(FwSmDesc_t smDesc, FwSmCounterS1_t i)
Return the state machine embedded in the i-th state of the argument state machine.
Definition: FwSmCore.c:217
void FwSmStart(FwSmDesc_t smDesc)
Start a state machine.
Definition: FwSmCore.c:52
Structure representing a transition.
Definition: FwSmPrivate.h:195
void FwSmMakeTrans(FwSmDesc_t smDesc, FwSmCounterU2_t transId)
Trigger a transition in a state machine.
Definition: FwSmCore.c:92
Structure representing a state machine descriptor.
Definition: FwSmPrivate.h:303
FwSmCounterS1_t FwSmGetCurState(FwSmDesc_t smDesc)
Return the identifier of the current state in a state machine (or zero if the state machine is stoppe...
Definition: FwSmCore.c:222
FwSmCounterU3_t stateExecCnt
the state execution counter
Definition: FwSmPrivate.h:323
FwSmCounterS1_t iExitAction
the exit action for the state
Definition: FwSmPrivate.h:131
FwSmCounterS1_t iTrAction
the index of the action associated to the transition
Definition: FwSmPrivate.h:201
Structure representing the base descriptor of a state machine.
Definition: FwSmPrivate.h:235
FwSmGuard_t * smGuards
the transition guards in the state machine
Definition: FwSmPrivate.h:309
void SmDummyAction(FwSmDesc_t smDesc)
Dummy action which returns without doing anything.
Definition: FwSmCore.c:40
SmPState_t * pStates
array holding the proper states in the state machine
Definition: FwSmPrivate.h:237
SmBaseDesc_t * smBase
pointer to the base descriptor
Definition: FwSmPrivate.h:305
Structure representing a choice pseudo state in a state machine.
Definition: FwSmPrivate.h:149
Structure representing a proper state in state machine.
Definition: FwSmPrivate.h:121
signed char FwSmCounterS1_t
Type used for signed counters with a "short" range.
Definition: FwSmConstants.h:79
FwSmCounterS1_t iEntryAction
the entry action for the state
Definition: FwSmPrivate.h:127
Declaration of the internal data structures of the FW State Machine Module.
SmTrans_t * trans
array holding the transitions in the state machine
Definition: FwSmPrivate.h:241
FwSmCounterS1_t FwSmGetCurStateEmb(FwSmDesc_t smDesc)
Return the identifier of the current state of the state machine embedded in the current state (the su...
Definition: FwSmCore.c:227
P&P Software GmbH, Copyright 2011, All Rights Reserved