FW Profile - C1 Implementation
FwSmDCreate.c
Go to the documentation of this file.
1 
18 #include "FwSmDCreate.h"
19 #include "FwSmPrivate.h"
20 #include <stdlib.h>
21 
22 /* ----------------------------------------------------------------------------------------------------------------- */
23 FwSmDesc_t FwSmCreate(FwSmCounterS1_t nOfStates, FwSmCounterS1_t nOfChoicePseudoStates, FwSmCounterS1_t nOfTrans,
25 
28  FwSmDesc_t smDesc;
29 
30  if (nOfTrans < 1) {
31  return NULL;
32  }
33 
34  if (nOfStates < 0) {
35  return NULL;
36  }
37 
38  if (nOfChoicePseudoStates < 0) {
39  return NULL;
40  }
41 
42  if (nOfActions < 0) {
43  return NULL;
44  }
45 
46  if (nOfGuards < 0) {
47  return NULL;
48  }
49 
50  smDesc = (FwSmDesc_t)malloc(sizeof(struct FwSmDesc));
51  if (smDesc == NULL) {
52  return NULL;
53  }
54 
55  smBase = (SmBaseDesc_t*)malloc(sizeof(SmBaseDesc_t));
56  if (smBase == NULL) {
57  return NULL;
58  }
59 
60  if (nOfStates > 0) {
61  smBase->pStates = (SmPState_t*)malloc(((FwSmCounterU4_t)(nOfStates)) * sizeof(SmPState_t));
62  if (smBase->pStates == NULL) {
63  return NULL;
64  }
65  for (i = 0; i < nOfStates; i++) {
66  smBase->pStates[i].outTransIndex = 0;
67  }
68  smDesc->esmDesc = (struct FwSmDesc**)malloc(((FwSmCounterU4_t)(nOfStates)) * sizeof(FwSmDesc_t));
69  if (smDesc->esmDesc == NULL) {
70  return NULL;
71  }
72  for (i = 0; i < nOfStates; i++) {
73  smDesc->esmDesc[i] = NULL;
74  }
75  }
76  else {
77  smBase->pStates = NULL;
78  smDesc->esmDesc = NULL;
79  }
80 
81  if (nOfChoicePseudoStates > 0) {
82  smBase->cStates = (SmCState_t*)malloc(((FwSmCounterU4_t)(nOfChoicePseudoStates)) * sizeof(SmCState_t));
83  if (smBase->cStates == NULL) {
84  return NULL;
85  }
86  for (i = 0; i < nOfChoicePseudoStates; i++) {
87  smBase->cStates[i].outTransIndex = 0;
88  }
89  }
90  else {
91  smBase->cStates = NULL;
92  }
93 
94  smBase->trans = (SmTrans_t*)malloc(((FwSmCounterU4_t)(nOfTrans)) * sizeof(SmTrans_t));
95  if (smBase->trans == NULL) {
96  return NULL;
97  }
98  for (i = 0; i < nOfTrans; i++) {
99  smBase->trans[i].iTrAction = -1;
100  }
101 
102  smDesc->smActions = (FwSmAction_t*)malloc(((FwSmCounterU4_t)(nOfActions + 1)) * sizeof(FwSmAction_t));
103  if (smDesc->smActions == NULL) {
104  return NULL;
105  }
106  smDesc->smActions[0] = &SmDummyAction;
107  for (i = 1; i <= nOfActions; i++) {
108  smDesc->smActions[i] = NULL;
109  }
110 
111  smDesc->smGuards = (FwSmGuard_t*)malloc(((FwSmCounterU4_t)(nOfGuards + 1)) * sizeof(FwSmGuard_t));
112  if (smDesc->smGuards == NULL) {
113  return NULL;
114  }
115  smDesc->smGuards[0] = &SmDummyGuard;
116  for (i = 1; i <= nOfGuards; i++) {
117  smDesc->smGuards[i] = NULL;
118  }
119 
120  smBase->nOfCStates = nOfChoicePseudoStates;
121  smBase->nOfPStates = nOfStates;
122  smBase->nOfTrans = nOfTrans;
123  smDesc->smBase = smBase;
124  smDesc->curState = 0;
125  smDesc->smData = NULL;
126  smDesc->transCnt = 1;
127  smDesc->nOfActions = (FwSmCounterS1_t)(nOfActions + 1);
128  smDesc->nOfGuards = (FwSmCounterS1_t)(nOfGuards + 1);
129  smDesc->smExecCnt = 0;
130  smDesc->stateExecCnt = 0;
131  smDesc->errCode = smSuccess;
132 
133  return smDesc;
134 }
135 
136 /* ----------------------------------------------------------------------------------------------------------------- */
138  FwSmCounterS1_t i;
139  SmBaseDesc_t* smBase = smDesc->smBase;
140  FwSmDesc_t extSmDesc;
141 
142  /* Create descriptor for derived SM */
143  extSmDesc = (FwSmDesc_t)malloc(sizeof(struct FwSmDesc));
144  if (extSmDesc == NULL) {
145  return NULL;
146  }
147 
148  /* Create array of embedded state machines in the derived SM */
149  if (smBase->nOfPStates > 0) {
150  extSmDesc->esmDesc = (struct FwSmDesc**)malloc(((FwSmCounterU4_t)(smBase->nOfPStates)) * sizeof(FwSmDesc_t));
151  if (extSmDesc->esmDesc == NULL) {
152  return NULL;
153  }
154  }
155  else {
156  extSmDesc->esmDesc = NULL;
157  }
158 
159  /* Create array of actions in the derived SM */
160  extSmDesc->smActions = (FwSmAction_t*)malloc(((FwSmCounterU4_t)(smDesc->nOfActions)) * sizeof(FwSmAction_t));
161  if (extSmDesc->smActions == NULL) {
162  return NULL;
163  }
164  for (i = 0; i < smDesc->nOfActions; i++) {
165  extSmDesc->smActions[i] = smDesc->smActions[i];
166  }
167 
168  /* Create array of guards in the derived SM */
169  extSmDesc->smGuards = (FwSmGuard_t*)malloc(((FwSmCounterU4_t)(smDesc->nOfGuards)) * sizeof(FwSmGuard_t));
170  if (extSmDesc->smGuards == NULL) {
171  return NULL;
172  }
173  for (i = 0; i < smDesc->nOfGuards; i++) {
174  extSmDesc->smGuards[i] = smDesc->smGuards[i];
175  }
176 
177  /* Create embedded state machines */
178  for (i = 0; i < smBase->nOfPStates; i++) {
179  if (smDesc->esmDesc[i] != NULL) {
180  extSmDesc->esmDesc[i] = FwSmCreateDer(smDesc->esmDesc[i]);
181  }
182  else {
183  extSmDesc->esmDesc[i] = NULL;
184  }
185  }
186 
187  extSmDesc->smBase = smBase;
188  extSmDesc->curState = 0;
189  extSmDesc->smData = NULL;
190  extSmDesc->transCnt = 0;
191  extSmDesc->nOfActions = smDesc->nOfActions;
192  extSmDesc->nOfGuards = smDesc->nOfGuards;
193  extSmDesc->errCode = smDesc->errCode;
194  extSmDesc->smExecCnt = 0;
195  extSmDesc->stateExecCnt = 0;
196 
197  return extSmDesc;
198 }
199 
200 /* ----------------------------------------------------------------------------------------------------------------- */
201 void FwSmRelease(FwSmDesc_t smDesc) {
203 
204  /* Release memory allocated to base descriptor */
205  smBase = smDesc->smBase;
206  free(smBase->pStates);
207  free(smBase->cStates);
208 
209  /* Release pointer to transition array (note that the transition array is guaranteed to exist and to
210  * have at least one element, see operation FwSmCreate) */
211  free(smBase->trans);
212 
213  /* Release memory allocated to base descriptor */
214  free(smDesc->smBase);
215 
216  /* Release memory allocated to extension part of the state machine descriptor */
217  FwSmReleaseDer(smDesc);
218 
219  return;
220 }
221 
222 /* ----------------------------------------------------------------------------------------------------------------- */
224 
225  /* Release pointer to the action and guard arrays (note that both arrays are guaranteed to
226  * have non-zero length) */
227  free(smDesc->smActions);
228  free(smDesc->smGuards);
229  free(smDesc->esmDesc);
230 
231  /* Release pointer to state machine descriptor */
232  free(smDesc);
233  smDesc = NULL;
234 
235  return;
236 }
237 
238 /* ----------------------------------------------------------------------------------------------------------------- */
240  FwSmCounterS1_t i;
241 
242  /* Release memory used by the embedded state machines */
243  for (i = 0; i < smDesc->smBase->nOfPStates; i++) {
244  if (smDesc->esmDesc[i] != NULL) {
245  FwSmReleaseRec(smDesc->esmDesc[i]);
246  }
247  }
248 
249  /* Release memory used by the embedding state machine */
250  FwSmRelease(smDesc);
251 
252  return;
253 }
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
Declaration of the dynamical creation interface for a FW State Machine.
void FwSmRelease(FwSmDesc_t smDesc)
Release the memory which was allocated when the state machine descriptor.
Definition: FwSmDCreate.c:201
FwSmCounterS1_t nOfActions
the number of actions (state actions + transition actions) in the state machine
Definition: FwSmPrivate.h:313
FwSmAction_t * smActions
the state machine actions (state and transition actions)
Definition: FwSmPrivate.h:307
void FwSmReleaseDer(FwSmDesc_t smDesc)
Release the memory allocated to a derived state machine descriptor.
Definition: FwSmDCreate.c:223
FwSmDesc_t FwSmCreate(FwSmCounterS1_t nOfStates, FwSmCounterS1_t nOfChoicePseudoStates, FwSmCounterS1_t nOfTrans, FwSmCounterS1_t nOfActions, FwSmCounterS1_t nOfGuards)
Create a new state machine descriptor.
Definition: FwSmDCreate.c:23
FwSmCounterS1_t nOfPStates
the number of states in the state machine
Definition: FwSmPrivate.h:243
SmCState_t * cStates
array holding the choice pseudo-states in the state machine
Definition: FwSmPrivate.h:239
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
FwSmCounterS1_t transCnt
the counter for the number of transitions added to the state machine
Definition: FwSmPrivate.h:317
struct FwSmDesc ** esmDesc
the state machines embedded in the state machine
Definition: FwSmPrivate.h:311
FwSmBool_t(* FwSmGuard_t)(FwSmDesc_t)
Type for a pointer to a state machine guard.
Definition: FwSmConstants.h:64
FwSmDesc_t FwSmCreateDer(FwSmDesc_t smDesc)
Create the descriptor of a derived state machine.
Definition: FwSmDCreate.c:137
FwSmCounterS1_t nOfCStates
the number of choice pseudo-states in the state machine
Definition: FwSmPrivate.h:245
FwSmCounterU3_t smExecCnt
the state machine execution counter
Definition: FwSmPrivate.h:321
void * smData
the pointer to the data manipulated by the state machine actions and guards
Definition: FwSmPrivate.h:327
void(* FwSmAction_t)(FwSmDesc_t)
Type for a pointer to a state machine action.
Definition: FwSmConstants.h:46
FwSmCounterS1_t nOfTrans
the number of transitions in SM
Definition: FwSmPrivate.h:247
Structure representing a transition.
Definition: FwSmPrivate.h:195
Structure representing a state machine descriptor.
Definition: FwSmPrivate.h:303
struct FwSmDesc * FwSmDesc_t
Forward declaration for the pointer to a state machine descriptor.
Definition: FwSmConstants.h:32
FwSmCounterU3_t stateExecCnt
the state execution counter
Definition: FwSmPrivate.h:323
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
FwSmCounterS1_t nOfGuards
the number of guards in the state machine
Definition: FwSmPrivate.h:315
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
long unsigned int FwSmCounterU4_t
Type used for unsigned counters with a "long int" range.
Definition: FwSmConstants.h:76
Return codes of a function which has completed execution without errors.
Definition: FwSmConstants.h:86
signed char FwSmCounterS1_t
Type used for signed counters with a "short" range.
Definition: FwSmConstants.h:79
void FwSmReleaseRec(FwSmDesc_t smDesc)
Recursively release the memory which was allocated when the state machine descriptor was created...
Definition: FwSmDCreate.c:239
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
P&P Software GmbH, Copyright 2011, All Rights Reserved