Add inter-command delay, fix compiler warning

r1612 | dcl | 2007-03-08 13:28:58 +1100 (Thu, 08 Mar 2007) | 2 lines
This commit is contained in:
Douglas Clowes
2007-03-08 13:28:58 +11:00
parent 75793f3141
commit d9fd20803c
2 changed files with 76 additions and 1 deletions

View File

@@ -10,6 +10,7 @@
* *
*/ */
#include <sys/time.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <sics.h> #include <sics.h>
#include <rs232controller.h> #include <rs232controller.h>
@@ -41,6 +42,8 @@ struct __MultiChanController {
pObjectDescriptor pDes; pObjectDescriptor pDes;
char* pHost; char* pHost;
int iPort; int iPort;
int iDelay; /* intercommand delay in milliseconds */
struct timeval tvLastCmd; /* time of completion of last command */
int unit_count; /* number of units connected */ int unit_count; /* number of units connected */
pMultiChan units; /* head of unit chain */ pMultiChan units; /* head of unit chain */
prs232 controller; /* associated RS232 controller object */ prs232 controller; /* associated RS232 controller object */
@@ -96,6 +99,31 @@ static int StartCommand(pMultiChanController self)
if (self->nw_tmr) if (self->nw_tmr)
NetWatchRemoveTimer(self->nw_tmr); NetWatchRemoveTimer(self->nw_tmr);
/*
* Implement the inter-command delay
*/
if (self->iDelay) {
struct timeval now, when;
gettimeofday(&now, NULL);
if (self->tvLastCmd.tv_sec == 0)
self->tvLastCmd = now;
when.tv_sec = self->tvLastCmd.tv_sec;
when.tv_usec = self->tvLastCmd.tv_usec + 1000 * self->iDelay;
if (when.tv_usec >= 1000000) {
when.tv_sec += when.tv_usec / 1000000;
when.tv_usec %= 1000000;
}
if (when.tv_sec > now.tv_sec ||
(when.tv_sec == now.tv_sec && when.tv_usec > now.tv_usec)) {
int delay = when.tv_sec - now.tv_sec;
delay *= 1000;
delay += (when.tv_usec - now.tv_usec + (1000 - 1)) / 1000;
NetWatchRegisterTimer(&self->nw_tmr, delay,
CommandTimeout, self);
return OKOK;
}
}
/* /*
* Discard any input before sending command * Discard any input before sending command
*/ */
@@ -150,6 +178,7 @@ static int PopCommand(pMultiChanController self)
if (self->nw_tmr) if (self->nw_tmr)
NetWatchRemoveTimer(self->nw_tmr); NetWatchRemoveTimer(self->nw_tmr);
self->nw_tmr = 0; self->nw_tmr = 0;
gettimeofday(&self->tvLastCmd, NULL);
/* /*
* If this is not the last in queue, start transmission * If this is not the last in queue, start transmission
*/ */
@@ -252,6 +281,21 @@ void MultiChanSetNotify(pMultiChan unit, void* context, MCC_Notify notify)
unit->notify_cntx = context; unit->notify_cntx = context;
} }
int MultiChanGetDelay(pMultiChan unit)
{
assert(unit);
return unit->mcc->iDelay;
}
int MultiChanSetDelay(pMultiChan unit, int iDelay)
{
int old_iDelay;
assert(unit);
old_iDelay = unit->mcc->iDelay;
unit->mcc->iDelay = iDelay;
return old_iDelay;
}
int MultiChanGetTimeout(pMultiChan unit) int MultiChanGetTimeout(pMultiChan unit)
{ {
assert(unit); assert(unit);
@@ -270,7 +314,35 @@ int MultiChanSetTimeout(pMultiChan unit, int timeout)
int MultiChanAction(SConnection *pCon, SicsInterp *pSics, int MultiChanAction(SConnection *pCon, SicsInterp *pSics,
void *pData, int argc, char *argv[]) void *pData, int argc, char *argv[])
{ {
char line[132];
pMultiChanController self = (pMultiChanController) pData; pMultiChanController self = (pMultiChanController) pData;
if (strcasecmp(argv[1], "delay") == 0) {
if (argc > 2) {
int delay;
int iRet;
iRet = sscanf(argv[2], "%d", &delay);
if (iRet != 1) {
snprintf(line, 132, "Invalid argument: %s", argv[2]);
SCWrite(pCon, line, eError);
return 0;
}
else {
if (delay < 0 || delay > 30000) {
snprintf(line, 132, "Value out of range: %d", delay);
SCWrite(pCon, line, eError);
return 0;
}
self->iDelay = delay;
return OKOK;
}
}
else {
snprintf(line, 132, "%s.delay = %d", argv[0], self->iDelay);
SCWrite(pCon, line, eStatus);
return OKOK;
}
return OKOK;
}
/* TODO: handle private stuff */ /* TODO: handle private stuff */
return RS232Action(pCon, pSics, self->controller, argc, argv); return RS232Action(pCon, pSics, self->controller, argc, argv);
} }
@@ -324,9 +396,10 @@ static int MC_Init(pMultiChanController self)
return 1; return 1;
} }
static void MC_Kill(pMultiChanController self) static void MC_Kill(void* pData)
{ {
int i; int i;
pMultiChanController self = (pMultiChanController) pData;
for (i = 0; i < mcc_index; ++i) for (i = 0; i < mcc_index; ++i)
if (mcc_array[i] == self) { if (mcc_array[i] == self) {
--mcc_index; --mcc_index;

View File

@@ -36,6 +36,8 @@ int MultiChanWrite(pMultiChan unit, void* buffer, int buflen);
typedef void (*MCC_Notify)(void* context, int event); typedef void (*MCC_Notify)(void* context, int event);
void MultiChanSetNotify(pMultiChan unit, void* context, MCC_Notify notify); void MultiChanSetNotify(pMultiChan unit, void* context, MCC_Notify notify);
int MultiChanGetDelay(pMultiChan unit);
int MultiChanSetDelay(pMultiChan unit, int iDelay);
int MultiChanGetTimeout(pMultiChan unit); int MultiChanGetTimeout(pMultiChan unit);
int MultiChanSetTimeout(pMultiChan unit, int timeout); int MultiChanSetTimeout(pMultiChan unit, int timeout);