- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
589
simcter.c
589
simcter.c
@@ -52,309 +52,290 @@
|
||||
*/
|
||||
static float FAILRATE;
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static float SimRandom(void)
|
||||
{
|
||||
float fVal;
|
||||
|
||||
fVal = ( (float) rand() / (float)RAND_MAX) * 100.0;
|
||||
return fVal;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
long lEnd;
|
||||
int iPause;
|
||||
long tStart;
|
||||
} SimSt;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMGetStatus(struct __COUNTER *self, float *fControl)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD, tDe;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *)self->pData;
|
||||
assert(pSim);
|
||||
|
||||
/*
|
||||
no fail, no wait case
|
||||
*/
|
||||
if(FAILRATE < .0)
|
||||
{
|
||||
return HWIdle;
|
||||
}
|
||||
static float SimRandom(void)
|
||||
{
|
||||
float fVal;
|
||||
|
||||
if(pSim->iPause == 1)
|
||||
{
|
||||
return HWPause;
|
||||
}
|
||||
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
/* calculate time */
|
||||
self->fTime = (long)time(NULL) - pSim->tStart;
|
||||
|
||||
iRun = 1;
|
||||
if(self->eMode == eTimer)
|
||||
{
|
||||
if((long)time(&tD) > pSim->lEnd)
|
||||
{
|
||||
iRun = 0;
|
||||
}
|
||||
tDe = pSim->lEnd - tD;
|
||||
*fControl = (float)tDe;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
pSim->lEnd += SimRandom();
|
||||
if(pSim->lEnd > (long)self->fPreset)
|
||||
{
|
||||
iRun = 0;
|
||||
}
|
||||
*fControl = (float)pSim->lEnd;
|
||||
}
|
||||
if(iRun)
|
||||
{
|
||||
return HWBusy;
|
||||
}
|
||||
else
|
||||
{
|
||||
return HWIdle;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMStart(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *)self->pData;
|
||||
assert(pSim);
|
||||
|
||||
pSim->iPause = 0;
|
||||
pSim->tStart = time(NULL);
|
||||
if(self->eMode == eTimer)
|
||||
{
|
||||
pSim->lEnd = (long)time(&tD) + (long)self->fPreset;
|
||||
}
|
||||
else
|
||||
{
|
||||
pSim->lEnd = 0;
|
||||
}
|
||||
|
||||
if(FAILRATE < .0)
|
||||
{
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
return OKOK;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMPause(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *)self->pData;
|
||||
assert(pSim);
|
||||
|
||||
pSim->iPause = 1;
|
||||
|
||||
/*
|
||||
no fail, no wait case
|
||||
*/
|
||||
if(FAILRATE < .0)
|
||||
{
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
return OKOK;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMContinue(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *)self->pData;
|
||||
assert(pSim);
|
||||
|
||||
pSim->iPause = 0;
|
||||
|
||||
if(FAILRATE < .0)
|
||||
{
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
return OKOK;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMHalt(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *)self->pData;
|
||||
assert(pSim);
|
||||
|
||||
if(FAILRATE < .0)
|
||||
{
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return HWFault;
|
||||
}
|
||||
if(self->eMode == eTimer)
|
||||
{
|
||||
pSim->lEnd = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pSim->lEnd = (long)self->fPreset + 10;
|
||||
}
|
||||
return OKOK;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
static int SIMReadValues(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
int i;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *)self->pData;
|
||||
assert(pSim);
|
||||
|
||||
/*
|
||||
no fail, no wait case
|
||||
*/
|
||||
if(FAILRATE < .0)
|
||||
{
|
||||
for(i = 0; i < MAXCOUNT; i++)
|
||||
{
|
||||
self->lCounts[i] = (long)(SimRandom()*100);
|
||||
}
|
||||
self->lCounts[1] = self->fPreset;
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
for(i = 0; i < MAXCOUNT; i++)
|
||||
{
|
||||
self->lCounts[i] = (long)(SimRandom()*100);
|
||||
}
|
||||
self->lCounts[1] = self->fPreset;
|
||||
return OKOK;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
static int SIMGetError(struct __COUNTER *self, int *iCode, char *error,
|
||||
int iErrLen)
|
||||
{
|
||||
strncpy(error,"Randomly simulated counter error",iErrLen);
|
||||
*iCode = 1;
|
||||
return 1;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMTryAndFixIt(struct __COUNTER *self, int iCode)
|
||||
{
|
||||
if(SimRandom() < FAILRATE)
|
||||
{
|
||||
return COTERM;
|
||||
}
|
||||
return COREDO;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMSet(struct __COUNTER *self, char *name, int iCter, float FVal)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMGet(struct __COUNTER *self, char *name, int iCter, float *fVal)
|
||||
{
|
||||
*fVal = 25.999;
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMSend(struct __COUNTER *self, char *pText,
|
||||
char *pReply, int iReplyLen)
|
||||
{
|
||||
strncpy(pReply,"Simulated response",iReplyLen);
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
pCounterDriver NewSIMCounter(char *name, float fFail)
|
||||
{
|
||||
pCounterDriver pRes = NULL;
|
||||
SimSt *pData = NULL;
|
||||
int iRet;
|
||||
int iC1, iC2, iC3;
|
||||
char *pErr;
|
||||
char pBueffel[132];
|
||||
|
||||
pRes = CreateCounterDriver(name, "SIM");
|
||||
if(!pRes)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pData = (SimSt *)malloc(sizeof(SimSt));
|
||||
if(!pData)
|
||||
{
|
||||
DeleteCounterDriver(pRes);
|
||||
return NULL;
|
||||
}
|
||||
pData->lEnd = 0;
|
||||
pData->iPause = 0;
|
||||
pRes->pData = (void *)pData;
|
||||
pRes->iNoOfMonitors = 8;
|
||||
pRes->fTime = 0;
|
||||
|
||||
/* assign functions */
|
||||
pRes->GetStatus = SIMGetStatus;
|
||||
pRes->Start = SIMStart;
|
||||
pRes->Halt = SIMHalt;
|
||||
pRes->ReadValues = SIMReadValues;
|
||||
pRes->GetError = SIMGetError;
|
||||
pRes->TryAndFixIt = SIMTryAndFixIt;
|
||||
pRes->Pause = SIMPause;
|
||||
pRes->Continue = SIMContinue;
|
||||
pRes->Set = SIMSet;
|
||||
pRes->Get = SIMGet;
|
||||
pRes->Send = SIMSend;
|
||||
pRes->KillPrivate = NULL;
|
||||
|
||||
FAILRATE = fFail;
|
||||
return pRes;
|
||||
fVal = ((float) rand() / (float) RAND_MAX) * 100.0;
|
||||
return fVal;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
long lEnd;
|
||||
int iPause;
|
||||
long tStart;
|
||||
} SimSt;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMGetStatus(struct __COUNTER *self, float *fControl)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD, tDe;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *) self->pData;
|
||||
assert(pSim);
|
||||
|
||||
/*
|
||||
no fail, no wait case
|
||||
*/
|
||||
if (FAILRATE < .0) {
|
||||
return HWIdle;
|
||||
}
|
||||
|
||||
if (pSim->iPause == 1) {
|
||||
return HWPause;
|
||||
}
|
||||
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
/* calculate time */
|
||||
self->fTime = (long) time(NULL) - pSim->tStart;
|
||||
|
||||
iRun = 1;
|
||||
if (self->eMode == eTimer) {
|
||||
if ((long) time(&tD) > pSim->lEnd) {
|
||||
iRun = 0;
|
||||
}
|
||||
tDe = pSim->lEnd - tD;
|
||||
*fControl = (float) tDe;
|
||||
|
||||
} else {
|
||||
pSim->lEnd += SimRandom();
|
||||
if (pSim->lEnd > (long) self->fPreset) {
|
||||
iRun = 0;
|
||||
}
|
||||
*fControl = (float) pSim->lEnd;
|
||||
}
|
||||
if (iRun) {
|
||||
return HWBusy;
|
||||
} else {
|
||||
return HWIdle;
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMStart(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *) self->pData;
|
||||
assert(pSim);
|
||||
|
||||
pSim->iPause = 0;
|
||||
pSim->tStart = time(NULL);
|
||||
if (self->eMode == eTimer) {
|
||||
pSim->lEnd = (long) time(&tD) + (long) self->fPreset;
|
||||
} else {
|
||||
pSim->lEnd = 0;
|
||||
}
|
||||
|
||||
if (FAILRATE < .0) {
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMPause(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *) self->pData;
|
||||
assert(pSim);
|
||||
|
||||
pSim->iPause = 1;
|
||||
|
||||
/*
|
||||
no fail, no wait case
|
||||
*/
|
||||
if (FAILRATE < .0) {
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMContinue(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
time_t tD;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *) self->pData;
|
||||
assert(pSim);
|
||||
|
||||
pSim->iPause = 0;
|
||||
|
||||
if (FAILRATE < .0) {
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMHalt(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
int iRun;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *) self->pData;
|
||||
assert(pSim);
|
||||
|
||||
if (FAILRATE < .0) {
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return HWFault;
|
||||
}
|
||||
if (self->eMode == eTimer) {
|
||||
pSim->lEnd = 0;
|
||||
} else {
|
||||
pSim->lEnd = (long) self->fPreset + 10;
|
||||
}
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
static int SIMReadValues(struct __COUNTER *self)
|
||||
{
|
||||
SimSt *pSim = NULL;
|
||||
int i;
|
||||
|
||||
assert(self);
|
||||
pSim = (SimSt *) self->pData;
|
||||
assert(pSim);
|
||||
|
||||
/*
|
||||
no fail, no wait case
|
||||
*/
|
||||
if (FAILRATE < .0) {
|
||||
for (i = 0; i < MAXCOUNT; i++) {
|
||||
self->lCounts[i] = (long) (SimRandom() * 100);
|
||||
}
|
||||
self->lCounts[1] = self->fPreset;
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return HWFault;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAXCOUNT; i++) {
|
||||
self->lCounts[i] = (long) (SimRandom() * 100);
|
||||
}
|
||||
self->lCounts[1] = self->fPreset;
|
||||
return OKOK;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
static int SIMGetError(struct __COUNTER *self, int *iCode, char *error,
|
||||
int iErrLen)
|
||||
{
|
||||
strncpy(error, "Randomly simulated counter error", iErrLen);
|
||||
*iCode = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMTryAndFixIt(struct __COUNTER *self, int iCode)
|
||||
{
|
||||
if (SimRandom() < FAILRATE) {
|
||||
return COTERM;
|
||||
}
|
||||
return COREDO;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMSet(struct __COUNTER *self, char *name, int iCter,
|
||||
float FVal)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SIMGet(struct __COUNTER *self, char *name, int iCter,
|
||||
float *fVal)
|
||||
{
|
||||
*fVal = 25.999;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int SIMSend(struct __COUNTER *self, char *pText,
|
||||
char *pReply, int iReplyLen)
|
||||
{
|
||||
strncpy(pReply, "Simulated response", iReplyLen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
pCounterDriver NewSIMCounter(char *name, float fFail)
|
||||
{
|
||||
pCounterDriver pRes = NULL;
|
||||
SimSt *pData = NULL;
|
||||
int iRet;
|
||||
int iC1, iC2, iC3;
|
||||
char *pErr;
|
||||
char pBueffel[132];
|
||||
|
||||
pRes = CreateCounterDriver(name, "SIM");
|
||||
if (!pRes) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pData = (SimSt *) malloc(sizeof(SimSt));
|
||||
if (!pData) {
|
||||
DeleteCounterDriver(pRes);
|
||||
return NULL;
|
||||
}
|
||||
pData->lEnd = 0;
|
||||
pData->iPause = 0;
|
||||
pRes->pData = (void *) pData;
|
||||
pRes->iNoOfMonitors = 8;
|
||||
pRes->fTime = 0;
|
||||
|
||||
/* assign functions */
|
||||
pRes->GetStatus = SIMGetStatus;
|
||||
pRes->Start = SIMStart;
|
||||
pRes->Halt = SIMHalt;
|
||||
pRes->ReadValues = SIMReadValues;
|
||||
pRes->GetError = SIMGetError;
|
||||
pRes->TryAndFixIt = SIMTryAndFixIt;
|
||||
pRes->Pause = SIMPause;
|
||||
pRes->Continue = SIMContinue;
|
||||
pRes->Set = SIMSet;
|
||||
pRes->Get = SIMGet;
|
||||
pRes->Send = SIMSend;
|
||||
pRes->KillPrivate = NULL;
|
||||
|
||||
FAILRATE = fFail;
|
||||
return pRes;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user