CORDET Framework - C2 Implementation
CrFwOutFactory.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 /* Include configuration files */
21 #include "CrFwOutFactoryUserPar.h"
22 #include "CrFwCmpData.h"
23 /* Include framework files */
24 #include "CrFwConstants.h"
26 #include "OutCmp/CrFwOutCmp.h"
27 #include "BaseCmp/CrFwBaseCmp.h"
28 #include "BaseCmp/CrFwInitProc.h"
29 #include "BaseCmp/CrFwResetProc.h"
31 #include "Pckt/CrFwPckt.h"
33 #include "CrFwOutFactory.h"
34 #include "CrFwTime.h"
35 /* Include FW Profile files */
36 #include "FwPrConfig.h"
37 #include "FwPrDCreate.h"
38 #include "FwSmConfig.h"
39 #include "FwSmCore.h"
40 #include "FwSmDCreate.h"
41 #include "FwPrCore.h"
42 
45 
48 
51 
54 
57 
60 
63 
69 
72 
74 static FwSmDesc_t outFactory;
75 
78 
81 
84 
93 static void OutFactoryInitAction(FwPrDesc_t initPr);
94 
109 static void OutFactoryConfigAction(FwPrDesc_t initPr);
110 
119 static void OutFactoryShutdownAction(FwSmDesc_t smDesc);
120 
121 /* ------------------------------------------------------------------------------------ */
122 FwSmDesc_t CrFwOutFactoryMake() {
123  FwPrDesc_t resetPr, execPr, initPr;
124 
125  if (outFactory != NULL) {
126  return outFactory;
127  }
128 
129  /* Extend the Base Component */
130  outFactory = FwSmCreateDer(CrFwBaseCmpMake());
131 
132  /* Create the Reset Procedure for the OutFactory Component */
133  resetPr = FwPrCreateDer(CrFwCmpGetResetProc());
134  FwPrOverrideAction(resetPr, &CrFwBaseCmpDefConfigAction, &OutFactoryConfigAction);
135 
136  /* Create the Initialization Procedure for the OuFactory Component */
137  initPr = FwPrCreateDer(CrFwCmpGetInitProc());
138  FwPrOverrideAction(initPr, &CrFwBaseCmpDefInitAction, &OutFactoryInitAction);
139 
140  /* Override the Shutdown Action for the OuFactory Component */
142 
143  /* Get the Dummy Execution Procedure for the OuFactory Component */
144  execPr = CrFwBaseCmpGetDummyExecProc();
145 
146  /* Initialize the data for the requested SM */
147  outFactoryData.outcome = 1;
148  outFactoryData.initProc = initPr;
149  outFactoryData.resetProc = resetPr;
150  outFactoryData.execProc = execPr;
151  outFactoryData.instanceId = 0;
152  outFactoryData.typeId = CR_FW_OUTFACTORY_TYPE;
153 
154  /* Attach the data to the OutFactory state machine and to its procedures. */
155  FwSmSetData(outFactory, &outFactoryData);
156  FwPrSetData(outFactoryData.initProc, &outFactoryData);
157  FwPrSetData(outFactoryData.resetProc, &outFactoryData);
158 
159  /* Start the OuFactory */
160  FwSmStart(outFactory);
161 
162  return outFactory;
163 }
164 
165 /* ------------------------------------------------------------------------------------ */
167  CrFwDiscriminant_t discriminant, CrFwPcktLength_t length) {
168  CrFwOutFactoryPoolIndex_t j, k, freePos;
169  CrFwCmdRepKindIndex_t kindIndex;
170  CrFwCmdRepKindKey_t targetKey;
171  CrFwPckt_t pckt;
172  CrFwPcktLength_t len;
173 
174  if (nextFreePos == CR_FW_OUTFACTORY_MAX_NOF_OUTCMP) { /* All positions are occupied */
176  return NULL;
177  }
178 
181  if (kindIndex == CR_FW_OUTCMP_NKINDS) {
183  return NULL;
184  }
185 
186  if (length == 0)
187  len = outCmpKindDesc[kindIndex].pcktLength;
188  else
189  len = length;
190 
191  pckt = CrFwPcktMake(len); /* The packet length is assumed to be non-negative */
192  if (pckt == NULL) {
194  return NULL;
195  }
196 
197  freePos = nextFreePos;
198  outCmpSpecificData[freePos].index = CrFwOutRegistryGetCmdRepIndex(type,subType);
199  outCmpSpecificData[freePos].isEnabled = outCmpKindDesc[kindIndex].isEnabled;
200  outCmpSpecificData[freePos].isReady = outCmpKindDesc[kindIndex].isReady;
201  outCmpSpecificData[freePos].isRepeat = outCmpKindDesc[kindIndex].isRepeat;
202  outCmpSpecificData[freePos].serialize = outCmpKindDesc[kindIndex].serialize;
203  outCmpSpecificData[freePos].update= outCmpKindDesc[kindIndex].update;
204  outCmpSpecificData[freePos].factoryPoolIndex = freePos;
205 
206  CrFwPcktSetCmdRepType(pckt,outCmpKindDesc[kindIndex].cmdRepType);
208  CrFwPcktSetGroup(pckt,0);
209  CrFwPcktSetServType(pckt,type);
210  CrFwPcktSetServSubType(pckt,subType);
211  CrFwPcktSetDiscriminant(pckt,discriminant);
212  CrFwPcktSetSeqCnt(pckt,0);
213 
214  outCmpSpecificData[freePos].pckt = pckt;
215 
220 
221  /* Reset the OutComponent */
222  CrFwCmpReset(outCmp[freePos]);
223 
225  outCmpInUse[freePos] = 1;
226 
227  /* Find the next free position in the pool of pre-allocated OutComponent instances */
228  k = (CrFwOutFactoryPoolIndex_t)(freePos + 1);
229  for (j=0; j<(CR_FW_OUTFACTORY_MAX_NOF_OUTCMP-1); j++) {
231  k = 0;
232  if (outCmpInUse[k] == 0) {
233  nextFreePos = k;
234  return (outCmp[freePos]); /* Next free position has been found */
235  }
236  k = (CrFwOutFactoryPoolIndex_t)(k + 1);
237  }
238 
239  nextFreePos = CR_FW_OUTFACTORY_MAX_NOF_OUTCMP; /* There are no free positions left */
240  return (outCmp[freePos]);
241 }
242 
243 /* ------------------------------------------------------------------------------------ */
244 void CrFwOutFactoryReleaseOutCmp(FwSmDesc_t outCmpInstance) {
245  CrFwCmpData_t* outCmpInstanceData = (CrFwCmpData_t*)FwSmGetData(outCmpInstance);
246  CrFwOutCmpData_t* cmpSpecificData = (CrFwOutCmpData_t*)(outCmpInstanceData->cmpSpecificData);
247  CrFwOutFactoryPoolIndex_t posToBeFreed;
248 
249  posToBeFreed = cmpSpecificData->factoryPoolIndex;
250  if (outCmpInUse[posToBeFreed] == 1) {
251  outCmpInUse[posToBeFreed] = 0;
253  nextFreePos = posToBeFreed;
254  CrFwPcktRelease(outCmpSpecificData[posToBeFreed].pckt);
255  outCmpSpecificData[posToBeFreed].pckt = NULL;
256  return;
257  }
259 
260  return;
261 }
262 
263 /* ------------------------------------------------------------------------------------ */
265  return nOfAllocatedOutCmp;
266 }
267 
268 /* ------------------------------------------------------------------------------------ */
271 }
272 
273 /* ------------------------------------------------------------------------------------ */
275  return maxInstanceId;
276 }
277 
278 /*------------------------------------------------------------------------------------*/
279 static void OutFactoryInitAction(FwPrDesc_t initPr) {
280  CrFwCounterU2_t i;
282  FwPrDesc_t outCmpInitPr, outCmpResetPr, outCmpExecPr;
283  CRFW_UNUSED(initPr);
284 
285  /* Create the pre-allocated OutComponents */
286  for (i=0; i<CR_FW_OUTFACTORY_MAX_NOF_OUTCMP; i++) {
287  /* Create the i-th OutCmp as an extension of the Base OutComponent */
288  outCmp[i] = FwSmCreateDer(CrFwOutCmpMakeBase());
289 
290  /* Create the Reset Procedure for the OutComponent */
291  outCmpResetPr = FwPrCreateDer(CrFwCmpGetResetProc());
292  outCmpData[i].resetProc = outCmpResetPr;
293 
294  /* Create the Initialization Procedure for the OutComponent */
295  outCmpInitPr = FwPrCreateDer(CrFwCmpGetInitProc());
296  outCmpData[i].initProc = outCmpInitPr;
297 
298  /* Get the Dummy Execution Procedure for the OutComponent */
299  outCmpExecPr = CrFwBaseCmpGetDummyExecProc();
300  outCmpData[i].execProc = outCmpExecPr;
301 
302  /* Set the OutComponent type */
303  outCmpData[i].typeId = CR_FW_OUTCMP_TYPE;
304 
305  /* Set the pointer to the component-specific data */
306  outCmpData[i].cmpSpecificData = &outCmpSpecificData[i];
307 
308  /* Attach the data to the OutComponent state machine and to its procedures. */
309  FwSmSetData(outCmp[i], &outCmpData[i]);
310  FwSmSetData(FwSmGetEmbSm(outCmp[i], CR_FW_BASE_STATE_CONFIGURED), &outCmpData[i]);
311  FwPrSetData(outCmpData[i].initProc, &outCmpData[i]);
312  FwPrSetData(outCmpData[i].resetProc, &outCmpData[i]);
313 
314  /* Start and initialize the OutComponent */
315  FwSmStart(outCmp[i]);
316  CrFwCmpInit(outCmp[i]);
317  }
318 
319  /* Initialize the array holding the keys of the OutCommand kinds */
320  for (j=0; j<CR_FW_OUTCMP_NKINDS; j++)
322  outCmpKindDesc[j].servSubType*CR_FW_MAX_DISCRIMINANT+outCmpKindDesc[j].discriminant);
323 }
324 
325 /*------------------------------------------------------------------------------------*/
326 static void OutFactoryConfigAction(FwPrDesc_t initPr) {
327  CrFwCounterU2_t i;
328  CrFwCmpData_t* cmpData = (CrFwCmpData_t*)FwPrGetData(initPr);
329 
330  for (i=0; i<CR_FW_OUTFACTORY_MAX_NOF_OUTCMP; i++)
331  if (outCmpInUse[i] != 0) {
332  outCmpInUse[i] = 0;
333  CrFwPcktRelease(outCmpSpecificData[i].pckt);
334  outCmpSpecificData[i].pckt = NULL;
335  }
336 
337 
338  nOfAllocatedOutCmp = 0;
340  nextFreePos = 0;
341 
342  cmpData->outcome = 1;
343 }
344 
345 /*------------------------------------------------------------------------------------*/
346 static void OutFactoryShutdownAction(FwSmDesc_t smDesc) {
347  CrFwCounterU2_t i;
348  CRFW_UNUSED(smDesc);
349 
350  for (i=0; i<CR_FW_OUTFACTORY_MAX_NOF_OUTCMP; i++) {
351  /* Release memory allocated to OutComponent Initialization Procedure */
352  FwPrReleaseDer(outCmpData[i].initProc);
353  /* Release memory allocated to OutComponent Reset Procedure */
354  FwPrReleaseDer(outCmpData[i].resetProc);
355  /* Release memory allocated to OutComponent */
356  FwSmReleaseDer(FwSmGetEmbSm(outCmp[i],CR_FW_BASE_STATE_CONFIGURED));
357  FwSmReleaseDer(outCmp[i]);
358  /* Mark all OutComponents as not-in-use */
359  outCmpInUse[i] = 0;
360  /* Release packet associated to OutComponent */
361  if (outCmpSpecificData[i].pckt != NULL)
362  CrFwPcktRelease(outCmpSpecificData[i].pckt);
363  }
364 
365  nOfAllocatedOutCmp = 0;
366 }
367 
CrFwOutCmpSerialize_t serialize
Function which implements the Serialization Operation for the out-going command or report...
CrFwPcktLength_t pcktLength
The length of the packet attached to the OutComponent.
Type for the Framework Component Data (FCD).
Definition: CrFwCmpData.h:79
Interface through which framework components access the current time.
unsigned short CrFwInstanceId_t
Type used for instance identifiers.
CrFwOutcome_t outcome
The outcome of an action or check executed by a state machine or by one of its procedures.
Definition: CrFwCmpData.h:93
void CrFwPcktSetSrc(CrFwPckt_t pckt, CrFwDestSrc_t src)
Set the source of the command or report encapsulated in a packet.
Definition: CrFwPckt.c:321
#define CR_FW_OUTCMP_TYPE
Type identifier for the OutComponent components.
unsigned char CrFwOutFactoryPoolIndex_t
Type for the index in the pool of pre-allocated OutComponents in the OutFactory (see CrFwOutFactory...
void CrFwPcktSetGroup(CrFwPckt_t pckt, CrFwGroup_t group)
Set the group of the command/report encapsulated in a packet.
Definition: CrFwPckt.c:392
#define CR_FW_OUTFACTORY_MAX_NOF_OUTCMP
The maximum number of OutComponents which may be allocated at any one time.
void CrFwCmpInit(FwSmDesc_t smDesc)
Initialize a framework component.
Definition: CrFwBaseCmp.c:112
FwPrDesc_t CrFwCmpGetResetProc()
Retrieve the singleton instance of the CRP.
Definition: CrFwResetProc.c:45
Definition of the Framework Component Data (FCD) Type.
A framework function was called with an illegal type/sub-type/discriminant triplet for an OutComponen...
static FwSmDesc_t outCmp[CR_FW_OUTFACTORY_MAX_NOF_OUTCMP]
The pre-allocated OutComponent instances.
#define CRFW_UNUSED(x)
A macro that can be used to specify that a function parameter is not used.
Definition: CrFwConstants.h:29
static void OutFactoryInitAction(FwPrDesc_t initPr)
Initialization action for OutFactory.
FwSmDesc_t CrFwOutCmpMakeBase()
Return the base OutComponent from which all other OutComponents are derived.
Definition: CrFwOutCmp.c:105
void CrFwSetAppErrCode(CrFwAppErrCode_t errCode)
Set the value of the application error code (see CrFwGetAppErrCode).
#define CR_FW_HOST_APP_ID
The identifier of the host application.
unsigned char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:38
CrFwPckt_t CrFwPcktMake(CrFwPcktLength_t pcktLength)
Make function for command or report packets.
Definition: CrFwPckt.c:129
static CrFwInstanceId_t apidInstanceId
The part of the command or report identifier which depends on the application identifier.
Definition of the OutFactory component.
Type for the data describing an OutComponent.
CrFwServType_t servType
The service type.
CrFwPckt_t pckt
Packet to which the out-going command or report is serialized.
Interface for creating and accessing a report or command packet.
CrFwOutCmpEnableCheck_t isEnabled
Function which implements the Enable Check for the out-going command or report.
void CrFwBaseCmpDefShutdownAction(FwSmDesc_t smDesc)
Function which performs the Shutdown Action for the Base State Machine.
Definition: CrFwBaseCmp.c:217
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:32
FwPrDesc_t resetProc
The Component Reset Procedure (CRP) (see CrFwResetProc.h).
Definition: CrFwCmpData.h:97
CrFwCmdRepKindIndex_t CrFwFindCmdRepKindIndex(CrFwCmdRepKindKey_t *cmdRepKindArray, CrFwCmdRepKindIndex_t length, CrFwCmdRepKindKey_t targetKey)
Convenience function to retrieve the index of an array where a certain target value is located...
Dummy Component Execution Procedure (CEP) for the Base Component.
Definition of the OutRegistry Component.
#define CR_FW_NBITS_APP_ID
The number of bits reserved for the application identifier in a command or report identifier...
Header file to define all invariant publicly available constants and types for the CORDET Framework...
void CrFwPcktRelease(CrFwPckt_t pckt)
Release function for command or report packets.
Definition: CrFwPckt.c:156
unsigned short CrFwDiscriminant_t
Type used for the discriminant of a command or report.
CrFwInstanceId_t instanceId
The instance identifier of the framework component.
Definition: CrFwCmpData.h:81
CrFwCmdRepIndex_t CrFwOutRegistryGetCmdRepIndex(CrFwServType_t servType, CrFwServSubType_t servSubType)
Get the index corresponding to the argument [service type, service sub-type] of an out-going command ...
void CrFwBaseCmpDefInitAction(FwPrDesc_t prDesc)
Function which performs the default Initialization Action of the CIP.
Definition: CrFwInitProc.c:42
unsigned int CrFwCmdRepKindKey_t
Type for the component kind key in CrFwInFactory.c and CrFwOutFactory.c.
An OutComponent allocation request has failed (see CrFwOutFactoryMakeOutCmp).
static FwSmDesc_t outFactory
The singleton instance of the OutFactory.
CrFwOutCmpRepeatCheck_t isRepeat
Function which implements the Repeat Check for the out-going command or report.
unsigned short CrFwCmdRepKindIndex_t
Type for the index of a command or report kind.
OutComponent kind descriptor type.
void CrFwPcktSetCmdRepType(CrFwPckt_t pckt, CrFwCmdRepType_t type)
Set the type of a packet (either a command packet or a report packet).
Definition: CrFwPckt.c:217
CrFwOutCmpUpdate_t update
The pointer to the function implementing the Update Operation.
unsigned short CrFwCounterU2_t
Type used for unsigned integers with a "medium" range.
CrFwCmdRepIndex_t index
Index of out-going command or report (see CrFwOutRegistry.h)
#define CR_FW_MAX_DISCRIMINANT
Maximum value of the discriminant attribute of InReports and InCommands.
void CrFwPcktSetServSubType(CrFwPckt_t pckt, CrFwServSubType_t servSubType)
Set the service sub-type of the command or report encapsulated in a packet.
Definition: CrFwPckt.c:297
static CrFwInstanceId_t maxInstanceId
The highest value of cmd/rep identifier (before overflowing into the application identifier bits) ...
void * cmpSpecificData
Derived data which are specific to each type of framework component.
Definition: CrFwCmpData.h:101
void CrFwBaseCmpDefConfigAction(FwPrDesc_t prDesc)
Function which performs the default Configuration Action of the CRP.
Definition: CrFwResetProc.c:39
static CrFwCmpData_t outFactoryData
The data for the OutFactory singleton.
Definition of the utility functions for the CORDET Framework.
static void OutFactoryConfigAction(FwPrDesc_t initPr)
Configuration action for OutFactory.
FwPrDesc_t execProc
The Component Execution Procedure (CEP) (see CrFwBaseCmp.h).
Definition: CrFwCmpData.h:99
CrFwOutFactoryPoolIndex_t CrFwOutFactoryGetMaxNOfOutCmp()
Return the maximum number of OutComponents which may be allocated at any one time.
FwSmDesc_t CrFwBaseCmpMake()
Retrieve the singleton instance of the Base State Machine.
Definition: CrFwBaseCmp.c:77
static CrFwCmdRepKindKey_t outCmpKindKey[CR_FW_OUTCMP_NKINDS]
Array holding the keys of the OutComponents kinds.
#define CR_FW_MAX_SERV_SUBTYPE
Maximum value of the service sub-type attribute of InReports and InCommands.
CrFwOutCmpEnableCheck_t isEnabled
The pointer to the function implementing the Enable Check Operation.
void CrFwCmpReset(FwSmDesc_t smDesc)
Reset a framework component.
Definition: CrFwBaseCmp.c:117
FwPrDesc_t CrFwBaseCmpGetDummyExecProc()
Retrieve the singleton instance of the Dummy CEP.
CrFwServSubType_t servSubType
The service sub-type.
static CrFwOutFactoryPoolIndex_t nextFreePos
The index of the next free position in the pool of pre-allocated OutComponent instances (a value of C...
void CrFwPcktSetServType(CrFwPckt_t pckt, CrFwServType_t servType)
Set the service type of the command or report encapsulated in a packet.
Definition: CrFwPckt.c:285
static void OutFactoryShutdownAction(FwSmDesc_t smDesc)
Shutdown action for OutFactory.
CrFwOutFactoryPoolIndex_t CrFwOutFactoryGetNOfAllocatedOutCmp()
Return the number of OutComponents which are currently allocated.
Definition of Base Component.
CrFwOutCmpReadyCheck_t isReady
Function which implements the Ready Check for the out-going command or report.
User-modifiable parameters for the OutFactory component (see CrFwOutFactory.h).
void CrFwOutFactoryReleaseOutCmp(FwSmDesc_t outCmpInstance)
Release function for an OutComponent.
static CrFwOutFactoryPoolIndex_t nOfAllocatedOutCmp
The number of currently allocated OutComponents.
CrFwOutCmpReadyCheck_t isReady
The pointer to the function implementing the Ready Check Operation.
Component Initialization Procedure (CIP) for the Base Component.
unsigned short int CrFwPcktLength_t
Type for the packet length.
Definition of the OutComponent Component of the framework.
CrFwDiscriminant_t discriminant
The discriminant value (or zero if no discriminant for this type/sub-type)
FwSmDesc_t CrFwOutFactoryMake()
Factory function for the singleton instance of the OutFactory.
static CrFwCmpData_t outCmpData[CR_FW_OUTFACTORY_MAX_NOF_OUTCMP]
The data for the pre-allocated OutComponent instances.
An OutComponent release request has encountered an error (see CrFwOutFactoryReleaseOutCmp).
#define CR_FW_OUTFACTORY_TYPE
Type identifier for the OutFactory component.
CrFwTypeId_t typeId
The type identifier of the framework component.
Definition: CrFwCmpData.h:83
#define CR_FW_OUTCMP_INIT_KIND_DESC
Definition of the OutComponent kinds supported by an application.
static CrFwBool_t outCmpInUse[CR_FW_OUTFACTORY_MAX_NOF_OUTCMP]
The in-use status of each pre-allocated OutComponent instance.
static CrFwOutCmpKindDesc_t outCmpKindDesc[CR_FW_OUTCMP_NKINDS]
Array of service descriptors.
static CrFwOutCmpData_t outCmpSpecificData[CR_FW_OUTFACTORY_MAX_NOF_OUTCMP]
The component-specific data for the pre-allocated OutReport instances.
void CrFwPcktSetSeqCnt(CrFwPckt_t pckt, CrFwSeqCnt_t seqCnt)
Set the sequence counter of the command or report encapsulated in a packet.
Definition: CrFwPckt.c:229
#define CR_FW_BASE_STATE_CONFIGURED
State identifier for state CONFIGURED in the Base State Machine.
static CrFwInstanceId_t nOfAllocatedOutCmpSinceReset
The total number of OutComponents allocated since the OutFactory was reset.
CrFwOutCmpUpdate_t update
Function which implements the Update Operation for the out-going command or report.
#define CR_FW_OUTCMP_NKINDS
The total number of kinds of OutComponents supported by the application.
static CrFwCmdRepType_t cmdRepType
Destination.
void CrFwPcktSetDiscriminant(CrFwPckt_t pckt, CrFwDiscriminant_t discriminant)
Set the discriminant of the command or report encapsulated in a packet.
Definition: CrFwPckt.c:279
FwSmDesc_t CrFwOutFactoryMakeOutCmp(CrFwServType_t type, CrFwServSubType_t subType, CrFwDiscriminant_t discriminant, CrFwPcktLength_t length)
Make function for an OutComponent.
CrFwOutCmpSerialize_t serialize
The pointer to the function implementing the Serialize Operation.
FwPrDesc_t CrFwCmpGetInitProc()
Retrieve the singleton instance of the CIP.
Definition: CrFwInitProc.c:48
unsigned char CrFwServType_t
Type used for the service type of a command or report.
CrFwOutFactoryPoolIndex_t factoryPoolIndex
Index of the position in the pool of pre-allocated OutComponents in the OutFactory to which the OutCo...
CrFwOutCmpRepeatCheck_t isRepeat
The pointer to the function implementing the Ready Check Operation.
Component Reset Procedure (CRP) for the Base Component.
CrFwInstanceId_t CrFwOutFactoryGetNOfInstanceId()
Return the number of distinct instance identifiers supported by the OutFactory.
FwPrDesc_t initProc
The Component Initialization Procedure (CIP) (see CrFwInitProc.h).
Definition: CrFwCmpData.h:95
unsigned char CrFwServSubType_t
Type used for the command or report sub-type.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved