multichan becomes AsyncQueue and AsyncProtocol
r1957 | dcl | 2007-05-11 17:28:31 +1000 (Fri, 11 May 2007) | 2 lines
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <sics.h>
|
||||
#include "network.h"
|
||||
#include "multichan.h"
|
||||
#include "asyncqueue.h"
|
||||
#include "nwatch.h"
|
||||
#include "safetyplc.h"
|
||||
#include "sicsvar.h"
|
||||
@@ -41,6 +41,8 @@ extern int DMC2280MotionControl;
|
||||
#define MOTOR_BOTH_BITS (MOTOR_ENABLED_BIT | MOTOR_DISABLED_BIT)
|
||||
#define ACCESS_BOTH_BITS (ACCESS_LOCKED_BIT | ACCESS_UNLOCKED_BIT)
|
||||
|
||||
static pAsyncProtocol PLC_Protocol = NULL;
|
||||
|
||||
int PLC_UserPriv = 0; /* Internal */
|
||||
typedef enum {
|
||||
Unknown_low, Invalid_high, Enabled, Disabled,
|
||||
@@ -64,45 +66,27 @@ typedef struct __SafetyPLCController SafetyPLCController, *pSafetyPLCController;
|
||||
|
||||
struct __SafetyPLCController {
|
||||
pObjectDescriptor pDes;
|
||||
pMultiChan mcc; /* associated MultiChan object */
|
||||
pAsyncUnit unit; /* associated AsyncUnit object */
|
||||
int iGetOut;
|
||||
int iValue;
|
||||
int oldValue;
|
||||
pNWTimer nw_tmr; /* NetWait timer handle */
|
||||
pNWTimer nw_tmr; /* periodic timer handle */
|
||||
pNWTimer oneshot; /* oneshot timer handle */
|
||||
int timeout;
|
||||
struct timeval tvSend;
|
||||
};
|
||||
|
||||
typedef struct __command Command, *pCommand;
|
||||
typedef int (*CommandCallback)(void* ctx, const char* resp, int resp_len);
|
||||
|
||||
struct __command {
|
||||
pSafetyPLCController plc;
|
||||
int cstate;
|
||||
int lstate;
|
||||
char* out_buf;
|
||||
int out_len;
|
||||
int out_idx;
|
||||
char* inp_buf;
|
||||
int inp_len;
|
||||
int inp_idx;
|
||||
CommandCallback func;
|
||||
void* cntx;
|
||||
};
|
||||
|
||||
static int PLC_GetState(void *pData, char *param, PLC_STATUS *retState);
|
||||
|
||||
static int PLC_Tx(void* ctx)
|
||||
static int PLC_Tx(pAsyncProtocol p, pAsyncTxn myCmd)
|
||||
{
|
||||
int iRet = 1;
|
||||
pCommand myCmd = (pCommand) ctx;
|
||||
|
||||
if (myCmd) {
|
||||
gettimeofday(&myCmd->plc->tvSend, NULL); /* refresh */
|
||||
iRet = MultiChanWrite(myCmd->plc->mcc, myCmd->out_buf, myCmd->out_len);
|
||||
myCmd->txn_status = ATX_ACTIVE;
|
||||
iRet = AsyncUnitWrite(myCmd->unit, myCmd->out_buf, myCmd->out_len);
|
||||
/* TODO handle errors */
|
||||
if (iRet < 0) { /* TODO: EOF */
|
||||
iRet = MultiChanReconnect(myCmd->plc->mcc);
|
||||
iRet = AsyncUnitReconnect(myCmd->unit);
|
||||
if (iRet == 0)
|
||||
return 0;
|
||||
}
|
||||
@@ -110,94 +94,52 @@ static int PLC_Tx(void* ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int PLC_Rx(void* ctx, int rxchar)
|
||||
static int PLC_Rx(pAsyncProtocol p, pAsyncTxn myCmd, int rxchar)
|
||||
{
|
||||
int iRet = 1;
|
||||
pCommand myCmd = (pCommand) ctx;
|
||||
|
||||
if (rxchar == MCC_TIMEOUT) {
|
||||
/* TODO: handle command timeout */
|
||||
if (myCmd->func)
|
||||
iRet = myCmd->func(myCmd->cntx, NULL, MCC_TIMEOUT);
|
||||
free(myCmd->out_buf);
|
||||
free(myCmd->inp_buf);
|
||||
free(myCmd);
|
||||
return MCC_POP_CMD;
|
||||
}
|
||||
|
||||
switch (myCmd->cstate) {
|
||||
switch (myCmd->txn_state) {
|
||||
case 0: /* first character */
|
||||
/* normal data */
|
||||
myCmd->cstate = 1;
|
||||
myCmd->txn_state = 1;
|
||||
/* note fallthrough */
|
||||
case 1: /* receiving reply */
|
||||
if (myCmd->inp_idx < myCmd->inp_len)
|
||||
myCmd->inp_buf[myCmd->inp_idx++] = rxchar;
|
||||
if (rxchar == 0x0D)
|
||||
myCmd->cstate = 2;
|
||||
myCmd->txn_state = 2;
|
||||
break;
|
||||
case 2: /* received CR and looking for LF */
|
||||
if (myCmd->inp_idx < myCmd->inp_len)
|
||||
myCmd->inp_buf[myCmd->inp_idx++] = rxchar;
|
||||
if (rxchar == 0x0A) {
|
||||
myCmd->cstate = 99;
|
||||
myCmd->txn_state = 99;
|
||||
/* end of line */
|
||||
}
|
||||
else
|
||||
myCmd->cstate = 1;
|
||||
myCmd->txn_state = 1;
|
||||
break;
|
||||
}
|
||||
if (myCmd->cstate == 99) {
|
||||
if (myCmd->txn_state == 99) {
|
||||
myCmd->inp_buf[myCmd->inp_idx] = '\0';
|
||||
if (myCmd->func)
|
||||
iRet = myCmd->func(myCmd->cntx, myCmd->inp_buf, myCmd->inp_idx);
|
||||
else
|
||||
iRet = 0;
|
||||
myCmd->cstate = 0;
|
||||
myCmd->inp_idx = 0;
|
||||
iRet = 0;
|
||||
myCmd->txn_state = 0;
|
||||
myCmd->txn_status = ATX_COMPLETE;
|
||||
}
|
||||
if (iRet == 0) { /* end of command */
|
||||
free(myCmd->out_buf);
|
||||
free(myCmd->inp_buf);
|
||||
free(myCmd);
|
||||
return MCC_POP_CMD;
|
||||
return AQU_POP_CMD;
|
||||
}
|
||||
return iRet;
|
||||
}
|
||||
|
||||
static int PLC_SendCmd(pSafetyPLCController self,
|
||||
char* command, int cmd_len,
|
||||
CommandCallback callback, void* context, int rsp_len)
|
||||
static int PLC_Ev(pAsyncProtocol p, pAsyncTxn myCmd, int event)
|
||||
{
|
||||
pCommand myCmd = NULL;
|
||||
|
||||
assert(self);
|
||||
assert(self->mcc);
|
||||
myCmd = (pCommand) malloc(sizeof(Command));
|
||||
assert(myCmd);
|
||||
memset(myCmd, 0, sizeof(Command));
|
||||
myCmd->out_buf = (char*) malloc(cmd_len + 5);
|
||||
memcpy(myCmd->out_buf, command, cmd_len);
|
||||
myCmd->out_len = cmd_len;
|
||||
if (myCmd->out_len < 2 ||
|
||||
myCmd->out_buf[myCmd->out_len - 1] != 0x0A ||
|
||||
myCmd->out_buf[myCmd->out_len - 2] != 0x0D) {
|
||||
myCmd->out_buf[myCmd->out_len++] = 0x0D;
|
||||
myCmd->out_buf[myCmd->out_len++] = 0x0A;
|
||||
if (event == AQU_TIMEOUT) {
|
||||
/* TODO: handle command timeout */
|
||||
myCmd->txn_status = ATX_TIMEOUT;
|
||||
return AQU_POP_CMD;
|
||||
}
|
||||
myCmd->out_buf[myCmd->out_len] = '\0';
|
||||
myCmd->func = callback;
|
||||
myCmd->cntx = context;
|
||||
if (rsp_len == 0)
|
||||
myCmd->inp_buf = NULL;
|
||||
else {
|
||||
myCmd->inp_buf = malloc(rsp_len + 1);
|
||||
memset(myCmd->inp_buf, 0, rsp_len + 1);
|
||||
}
|
||||
myCmd->inp_len = rsp_len;
|
||||
myCmd->plc = self;
|
||||
gettimeofday(&self->tvSend, NULL); /* refresh */
|
||||
return MultiChanEnque(self->mcc, myCmd, PLC_Tx, PLC_Rx);
|
||||
return AQU_POP_CMD;
|
||||
}
|
||||
|
||||
static void PLC_Notify(void* context, int event)
|
||||
@@ -205,9 +147,9 @@ static void PLC_Notify(void* context, int event)
|
||||
pSafetyPLCController self = (pSafetyPLCController) context;
|
||||
|
||||
switch (event) {
|
||||
case MCC_RECONNECT:
|
||||
case AQU_RECONNECT:
|
||||
do {
|
||||
mkChannel* sock = MultiChanGetSocket(self->mcc);
|
||||
mkChannel* sock = AsyncUnitGetSocket(self->unit);
|
||||
int flag = 1;
|
||||
setsockopt(sock->sockid, /* socket affected */
|
||||
IPPROTO_TCP, /* set option at TCP level */
|
||||
@@ -223,14 +165,16 @@ static void PLC_Notify(void* context, int event)
|
||||
/*
|
||||
* \brief GetCallback is the callback for the read command.
|
||||
*/
|
||||
static int GetCallback(void* ctx, const char* resp, int resp_len)
|
||||
static int GetCallback(pAsyncTxn txn)
|
||||
{
|
||||
int iRet,i;
|
||||
unsigned int iRead;
|
||||
char* resp = txn->inp_buf;
|
||||
int resp_len = txn->inp_idx;
|
||||
PLC_STATUS plcState;
|
||||
pSicsVariable plcVar=NULL;
|
||||
|
||||
pSafetyPLCController self = (pSafetyPLCController) ctx;
|
||||
pSafetyPLCController self = (pSafetyPLCController) txn->cntx;
|
||||
if (resp_len < 0) {
|
||||
DMC2280MotionControl = -1;
|
||||
}
|
||||
@@ -269,20 +213,33 @@ static int MyTimerCallback(void* context, int mode)
|
||||
{
|
||||
pSafetyPLCController self = (pSafetyPLCController) context;
|
||||
if (self->iGetOut) {
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
/* TODO error handling */
|
||||
if (((now.tv_sec - self->tvSend.tv_sec) * 1000
|
||||
+ (now.tv_usec / 1000)
|
||||
- (self->tvSend.tv_usec / 1000)) < self->timeout)
|
||||
return 1;
|
||||
DMC2280MotionControl = -1;
|
||||
}
|
||||
self->iGetOut = 1;
|
||||
PLC_SendCmd(self, "READ", 4, GetCallback, self, 132);
|
||||
AsyncUnitSendTxn(self->unit, "READ", 4, GetCallback, self, 132);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int MyOneShotCallback(void* context, int mode)
|
||||
{
|
||||
pSafetyPLCController self = (pSafetyPLCController) context;
|
||||
self->oneshot = 0;
|
||||
AsyncUnitSendTxn(self->unit, "WRITE 0", 7, NULL, NULL, 132);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* \brief PutCallback is the callback for the write command.
|
||||
*/
|
||||
static int PutCallback(pAsyncTxn txn)
|
||||
{
|
||||
pSafetyPLCController self = (pSafetyPLCController) txn->cntx;
|
||||
if (self->oneshot)
|
||||
NetWatchRemoveTimer(self->oneshot);
|
||||
NetWatchRegisterTimer(&self->oneshot, 500, MyOneShotCallback, self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int PLC_GetState(void *pData, char *param, PLC_STATUS *retState)
|
||||
{
|
||||
pSafetyPLCController self = (pSafetyPLCController) pData;
|
||||
@@ -427,13 +384,32 @@ static int PLC_Action(SConnection *pCon, SicsInterp *pSics,
|
||||
} else if (argc == 3) {
|
||||
if (strcasecmp(argv[1], "hattach") == 0) {
|
||||
}
|
||||
else if (strcasecmp(argv[1], "shutter") == 0) {
|
||||
if (strcasecmp(argv[2], "open") == 0) {
|
||||
/* open shutter */
|
||||
AsyncUnitSendTxn(self->unit, "WRITE 1", 4, PutCallback, self, 132);
|
||||
return OKOK;
|
||||
}
|
||||
else if (strcasecmp(argv[2], "close") == 0 ||
|
||||
strcasecmp(argv[2], "shut") == 0) {
|
||||
/* close shutter */
|
||||
AsyncUnitSendTxn(self->unit, "WRITE 2", 4, PutCallback, self, 132);
|
||||
return OKOK;
|
||||
}
|
||||
else {
|
||||
snprintf(line, 132, "%s %s does not understand %s",
|
||||
argv[0], argv[1], argv[2]);
|
||||
SCWrite(pCon, line, eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
snprintf(line, 132, "%s does not understand %s", argv[0], argv[1]);
|
||||
SCWrite(pCon, line, eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static pSafetyPLCController PLC_Create(const char* pName, int port)
|
||||
static pSafetyPLCController PLC_Create(const char* pName)
|
||||
{
|
||||
pSafetyPLCController self = NULL;
|
||||
|
||||
@@ -441,11 +417,11 @@ static pSafetyPLCController PLC_Create(const char* pName, int port)
|
||||
if (self == NULL)
|
||||
return NULL;
|
||||
memset(self, 0, sizeof(SafetyPLCController));
|
||||
if (MultiChanCreate(pName, &self->mcc) == 0) {
|
||||
if (AsyncUnitCreate(pName, &self->unit) == 0) {
|
||||
free(self);
|
||||
return NULL;
|
||||
}
|
||||
MultiChanSetNotify(self->mcc, self, PLC_Notify);
|
||||
AsyncUnitSetNotify(self->unit, self, PLC_Notify);
|
||||
|
||||
self->pDes = CreateDescriptor("SafetyPLC");
|
||||
return self;
|
||||
@@ -457,7 +433,7 @@ static int PLC_Init(pSafetyPLCController self)
|
||||
if (self->nw_tmr != NULL)
|
||||
NetWatchRemoveTimer(self->nw_tmr);
|
||||
NetWatchRegisterTimerPeriodic(&self->nw_tmr,
|
||||
1000, 100,
|
||||
1000, 1000,
|
||||
MyTimerCallback,
|
||||
self);
|
||||
self->timeout=120000; /* huge */
|
||||
@@ -473,6 +449,17 @@ static void PLC_Kill(void* pData)
|
||||
return;
|
||||
}
|
||||
|
||||
void SafetyPLCInitProtocol(SicsInterp *pSics) {
|
||||
if (PLC_Protocol == NULL) {
|
||||
PLC_Protocol = AsyncProtocolCreate(pSics, "SafetyPLC", NULL, NULL);
|
||||
PLC_Protocol->sendCommand = PLC_Tx;
|
||||
PLC_Protocol->handleInput = PLC_Rx;
|
||||
PLC_Protocol->handleEvent = PLC_Ev;
|
||||
PLC_Protocol->prepareTxn = NULL;
|
||||
PLC_Protocol->killPrivate = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int SafetyPLCFactory(SConnection *pCon, SicsInterp *pSics,
|
||||
void *pData, int argc, char *argv[])
|
||||
{
|
||||
@@ -482,7 +469,7 @@ int SafetyPLCFactory(SConnection *pCon, SicsInterp *pSics,
|
||||
pSicsVariable plcVar=NULL;
|
||||
PLC_STATUS plcState;
|
||||
|
||||
if(argc < 4)
|
||||
if(argc < 3)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: insufficient no of arguments to SafetyPLCFactory",
|
||||
eError);
|
||||
@@ -492,7 +479,7 @@ int SafetyPLCFactory(SConnection *pCon, SicsInterp *pSics,
|
||||
/*
|
||||
create data structure and open port
|
||||
*/
|
||||
pNew = PLC_Create(argv[2], atoi(argv[3]));
|
||||
pNew = PLC_Create(argv[2]);
|
||||
|
||||
if(!pNew)
|
||||
{
|
||||
@@ -524,5 +511,6 @@ int SafetyPLCFactory(SConnection *pCon, SicsInterp *pSics,
|
||||
PLC_Kill(pNew);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user