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:
@@ -51,67 +51,39 @@
|
||||
#include <sics.h>
|
||||
#include <modriv.h>
|
||||
#include <nwatch.h>
|
||||
#include <multichan.h>
|
||||
#include <asyncqueue.h>
|
||||
#include "nhq200util.h"
|
||||
/*-------------------------------------------------------------------*/
|
||||
|
||||
typedef struct __command Command, *pCommand;
|
||||
typedef int (*CommandCallback)(void* ctx, const char* resp, int resp_len);
|
||||
static pAsyncProtocol NHQ_Protocol = NULL;
|
||||
|
||||
struct __command {
|
||||
pMultiChan unit;
|
||||
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 NHQ_Tx1(void* ctx)
|
||||
static int NHQ_Tx1(pAsyncProtocol p, void* ctx)
|
||||
{
|
||||
int iRet = 1;
|
||||
pCommand myCmd = (pCommand) ctx;
|
||||
pAsyncTxn myCmd = (pAsyncTxn) ctx;
|
||||
|
||||
assert(myCmd);
|
||||
iRet = MultiChanWrite(myCmd->unit, &myCmd->out_buf[myCmd->out_idx], 1);
|
||||
iRet = AsyncUnitWrite(myCmd->unit, &myCmd->out_buf[myCmd->out_idx], 1);
|
||||
return iRet;
|
||||
}
|
||||
|
||||
static int NHQ_Tx(void* ctx)
|
||||
static int NHQ_Tx(pAsyncProtocol p, pAsyncTxn myCmd)
|
||||
{
|
||||
pCommand myCmd = (pCommand) ctx;
|
||||
|
||||
/*
|
||||
* Set/reset command states for send/resend of command
|
||||
*/
|
||||
myCmd->cstate = 0;
|
||||
myCmd->lstate = 0;
|
||||
myCmd->txn_state = 0;
|
||||
myCmd->out_idx = 0;
|
||||
myCmd->inp_idx = 0;
|
||||
return NHQ_Tx1(myCmd);
|
||||
myCmd->txn_status = ATX_ACTIVE;
|
||||
return NHQ_Tx1(p, myCmd);
|
||||
}
|
||||
|
||||
static int NHQ_Rx(void* ctx, int rxchar)
|
||||
static int NHQ_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: /* send with echo */
|
||||
if (rxchar != myCmd->out_buf[myCmd->out_idx]) {
|
||||
/* TODO: bad echo */
|
||||
@@ -120,12 +92,12 @@ static int NHQ_Rx(void* ctx, int rxchar)
|
||||
myCmd->out_idx > 0 &&
|
||||
myCmd->out_buf[myCmd->out_idx - 1] == 0x0D) {
|
||||
myCmd->inp_idx = 0;
|
||||
myCmd->cstate = 1;
|
||||
myCmd->txn_state = 1;
|
||||
/* TODO: end of line */
|
||||
}
|
||||
else if (myCmd->out_idx < myCmd->out_len) {
|
||||
myCmd->out_idx++;
|
||||
iRet = NHQ_Tx1(myCmd);
|
||||
iRet = NHQ_Tx1(p, myCmd);
|
||||
}
|
||||
else {
|
||||
/* TODO: out of data */
|
||||
@@ -135,65 +107,37 @@ static int NHQ_Rx(void* ctx, int rxchar)
|
||||
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) {
|
||||
/* end of line */
|
||||
myCmd->cstate = 3;
|
||||
myCmd->txn_state = 3;
|
||||
myCmd->inp_idx -= 2;
|
||||
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->txn_status = ATX_COMPLETE;
|
||||
iRet = 0;
|
||||
}
|
||||
else
|
||||
myCmd->cstate = 1;
|
||||
myCmd->txn_state = 1;
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
int NHQ_SendCmd(pMultiChan unit,
|
||||
char* command, int cmd_len,
|
||||
CommandCallback callback, void* context, int rsp_len)
|
||||
static int NHQ_Ev(pAsyncProtocol p, pAsyncTxn myCmd, int event)
|
||||
{
|
||||
pCommand myCmd = NULL;
|
||||
|
||||
assert(unit);
|
||||
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->unit = unit;
|
||||
return MultiChanEnque(unit, myCmd, NHQ_Tx, NHQ_Rx);
|
||||
return AQU_POP_CMD;
|
||||
}
|
||||
|
||||
static void NHQ_Notify(void* context, int event)
|
||||
@@ -201,14 +145,14 @@ static void NHQ_Notify(void* context, int event)
|
||||
pNHQ200 self = (pNHQ200) context;
|
||||
|
||||
switch (event) {
|
||||
case MCC_DISCONNECT:
|
||||
case AQU_DISCONNECT:
|
||||
if (self->transWait == 1) {
|
||||
self->transWait = NHQ200__FAULT;
|
||||
strcpy(self->pAns, "DISCONNECTED");
|
||||
}
|
||||
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 */
|
||||
@@ -302,13 +246,15 @@ static void parse_Vx(pNHQ200 self, const char* resp, int resp_len)
|
||||
#define STATE_NX 5
|
||||
#define STATE_VX 6
|
||||
#define STATE_END 9
|
||||
static int InitCallback(void* ctx, const char* resp, int resp_len)
|
||||
static int InitCallback(pAsyncTxn pTxn)
|
||||
{
|
||||
char cmd[20];
|
||||
int cmd_len;
|
||||
pNHQ200 self = (pNHQ200) ctx;
|
||||
const char* resp = pTxn->inp_buf;
|
||||
int resp_len = pTxn->inp_idx;
|
||||
pNHQ200 self = (pNHQ200) pTxn->cntx;
|
||||
/* TODO: FIXME finish initialisation */
|
||||
if (resp_len < 0) {
|
||||
if (pTxn->txn_status == ATX_TIMEOUT) {
|
||||
self->iError = NHQ200__BADSET;
|
||||
self->iState = 0;
|
||||
}
|
||||
@@ -316,37 +262,37 @@ static int InitCallback(void* ctx, const char* resp, int resp_len)
|
||||
switch (self->iState) {
|
||||
case 0: /* Initial */
|
||||
cmd_len = snprintf(cmd, sizeof(cmd), "#");
|
||||
NHQ_SendCmd(self->mcc, cmd, cmd_len, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, cmd, cmd_len, InitCallback, self, 80);
|
||||
self->iState = STATE_HASH;
|
||||
break;
|
||||
case STATE_HASH: /* # */
|
||||
parse_hash(self, resp, resp_len);
|
||||
cmd_len = snprintf(cmd, sizeof(cmd), "S%d", self->iControl);
|
||||
NHQ_SendCmd(self->mcc, cmd, cmd_len, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, cmd, cmd_len, InitCallback, self, 80);
|
||||
self->iState = STATE_SX;
|
||||
break;
|
||||
case STATE_SX: /* Sx */
|
||||
parse_Sx(self, resp, resp_len);
|
||||
cmd_len = snprintf(cmd, sizeof(cmd), "T%d", self->iControl);
|
||||
NHQ_SendCmd(self->mcc, cmd, cmd_len, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, cmd, cmd_len, InitCallback, self, 80);
|
||||
self->iState = STATE_TX;
|
||||
break;
|
||||
case STATE_TX: /* Tx */
|
||||
parse_Tx(self, resp, resp_len);
|
||||
cmd_len = snprintf(cmd, sizeof(cmd), "M%d", self->iControl);
|
||||
NHQ_SendCmd(self->mcc, cmd, cmd_len, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, cmd, cmd_len, InitCallback, self, 80);
|
||||
self->iState = STATE_MX;
|
||||
break;
|
||||
case STATE_MX: /* Mx */
|
||||
parse_Mx(self, resp, resp_len);
|
||||
cmd_len = snprintf(cmd, sizeof(cmd), "N%d", self->iControl);
|
||||
NHQ_SendCmd(self->mcc, cmd, cmd_len, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, cmd, cmd_len, InitCallback, self, 80);
|
||||
self->iState = STATE_NX;
|
||||
break;
|
||||
case STATE_NX: /* Nx */
|
||||
parse_Nx(self, resp, resp_len);
|
||||
cmd_len = snprintf(cmd, sizeof(cmd), "V%d", self->iControl);
|
||||
NHQ_SendCmd(self->mcc, cmd, cmd_len, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, cmd, cmd_len, InitCallback, self, 80);
|
||||
self->iState = STATE_VX;
|
||||
break;
|
||||
case STATE_VX: /* Vx */
|
||||
@@ -363,19 +309,20 @@ static int InitCallback(void* ctx, const char* resp, int resp_len)
|
||||
static void NHQ_Init(pNHQ200 self)
|
||||
{
|
||||
self->iState = 0;
|
||||
NHQ_SendCmd(self->mcc, "", 0, InitCallback, self, 80);
|
||||
AsyncUnitSendTxn(self->unit, "", 0, InitCallback, self, 80);
|
||||
}
|
||||
|
||||
/*
|
||||
* \brief GetCallback is the callback for the get position/value command.
|
||||
*/
|
||||
static int GetCallback(void* ctx, const char* resp, int resp_len)
|
||||
static int GetCallback(pAsyncTxn pTxn)
|
||||
{
|
||||
int iRet;
|
||||
float fRead;
|
||||
pNHQ200 self = (pNHQ200) ctx;
|
||||
const char* resp = pTxn->inp_buf;
|
||||
pNHQ200 self = (pNHQ200) pTxn->cntx;
|
||||
|
||||
if (resp_len < 0) {
|
||||
if (pTxn->txn_status == ATX_TIMEOUT) {
|
||||
self->iError = NHQ200__BADREAD;
|
||||
}
|
||||
else {
|
||||
@@ -396,11 +343,13 @@ static int GetCallback(void* ctx, const char* resp, int resp_len)
|
||||
/*
|
||||
* \brief TransCallback is the callback for the general command transaction.
|
||||
*/
|
||||
static int TransCallback(void* ctx, const char* resp, int resp_len)
|
||||
static int TransCallback(pAsyncTxn pTxn)
|
||||
{
|
||||
pNHQ200 self = (pNHQ200) ctx;
|
||||
const char* resp = pTxn->inp_buf;
|
||||
int resp_len = pTxn->inp_idx;
|
||||
pNHQ200 self = (pNHQ200) pTxn->cntx;
|
||||
|
||||
if (resp_len < 0) {
|
||||
if (pTxn->txn_status == ATX_TIMEOUT) {
|
||||
self->transReply[0] = '\0';
|
||||
self->transWait = -1;
|
||||
}
|
||||
@@ -419,7 +368,7 @@ int transactNHQ200(pNHQ200 self, void *send, int sendLen,
|
||||
assert(self);
|
||||
self->transReply = reply;
|
||||
self->transWait = 1;
|
||||
NHQ_SendCmd(self->mcc,
|
||||
AsyncUnitSendTxn(self->unit,
|
||||
send, sendLen,
|
||||
TransCallback, self, replyLen);
|
||||
while (self->transWait == 1)
|
||||
@@ -448,7 +397,8 @@ int NHQ200_Check_Status(pNHQ200 self)
|
||||
do
|
||||
{
|
||||
sprintf(pCommand,"S%d", self->iControl);
|
||||
if ((iRet=transactNHQ200(self,pCommand,strlen(pCommand),pReply,79))<=0)
|
||||
iRet=AsyncUnitTransact(self->unit, pCommand, strlen(pCommand), pReply, 79);
|
||||
if (iRet <= 0)
|
||||
{
|
||||
printf("Comms error!\n");
|
||||
return iRet; // Comms problem
|
||||
@@ -472,10 +422,6 @@ int NHQ200_Check_Status(pNHQ200 self)
|
||||
/* Operations common to both Open and Config functions */
|
||||
static int NHQ200_Setup(pNHQ200 self, int iControl)
|
||||
{
|
||||
int iRet;
|
||||
char pCommand[20];
|
||||
char pReply[132];
|
||||
|
||||
if (!self)
|
||||
return NHQ200__BADCOM;
|
||||
|
||||
@@ -505,12 +451,12 @@ int NHQ200_Open(pNHQ200 *pData, char *pName, int iSensor, int iCTRL, int iMode)
|
||||
self->fDiv = 1.0;
|
||||
self->fMult = 1.0;
|
||||
|
||||
if (MultiChanCreate(pName, &self->mcc) == 0) {
|
||||
if (AsyncUnitCreate(pName, &self->unit) == 0) {
|
||||
return NHQ200__NONHQ200;
|
||||
}
|
||||
MultiChanSetNotify(self->mcc, self, NHQ_Notify);
|
||||
AsyncUnitSetNotify(self->unit, self, NHQ_Notify);
|
||||
|
||||
sock = MultiChanGetSocket(self->mcc);
|
||||
sock = AsyncUnitGetSocket(self->unit);
|
||||
if (sock) {
|
||||
int flag = 1;
|
||||
iRet = setsockopt(sock->sockid, /* socket affected */
|
||||
@@ -560,7 +506,7 @@ int NHQ200_Send(pNHQ200 *pData, char *pCommand, char *pReply, int iLen)
|
||||
/* Send command direct to the NHQ200 */
|
||||
commandlen=strlen(pCommand);
|
||||
|
||||
iRet=transactNHQ200(self,pCommand,commandlen,pReply,iLen);
|
||||
iRet=AsyncUnitTransact(self->unit, pCommand, commandlen, pReply, iLen);
|
||||
|
||||
return iRet;
|
||||
}
|
||||
@@ -581,13 +527,20 @@ int NHQ200_Read(pNHQ200 *pData, float *fVal)
|
||||
struct timeval tv_this;
|
||||
gettimeofday(&tv_this, NULL);
|
||||
if ((tv_this.tv_sec - self->tv_last.tv_sec) > 0) {
|
||||
NHQ_SendCmd(self->mcc,
|
||||
AsyncUnitSendTxn(self->unit,
|
||||
pCommand, 2,
|
||||
GetCallback, self, 132);
|
||||
self->iGetOut = 1;
|
||||
self->tv_last = tv_this;
|
||||
}
|
||||
}
|
||||
while (self->iGetOut) {
|
||||
struct timeval tv_this;
|
||||
gettimeofday(&tv_this, NULL);
|
||||
if ((tv_this.tv_sec - self->tv_last.tv_sec) > 1)
|
||||
break;
|
||||
TaskYield(pServ->pTasker);
|
||||
}
|
||||
*fVal = self->fValue;
|
||||
iRet = 1;
|
||||
|
||||
@@ -597,7 +550,7 @@ int NHQ200_Read(pNHQ200 *pData, float *fVal)
|
||||
int NHQ200_Set(pNHQ200 *pData, float fVal)
|
||||
{
|
||||
char pCommand[20], pCommandRead[20], pReply[132], pCommandGo[20];
|
||||
int iRet, i;
|
||||
int iRet;
|
||||
const float fPrecision = 0.1;
|
||||
float fDelta, fRead;
|
||||
pNHQ200 self;
|
||||
@@ -615,11 +568,11 @@ int NHQ200_Set(pNHQ200 *pData, float fVal)
|
||||
sprintf(pCommandRead,"D%d", self->iControl);
|
||||
|
||||
/* send Dn=nnn command, we get a blank line response */
|
||||
iRet = transactNHQ200(self,pCommand,strlen(pCommand),pReply,131);
|
||||
iRet = AsyncUnitTransact(self->unit,pCommand,strlen(pCommand),pReply,131);
|
||||
if (iRet <= 0)
|
||||
return iRet;
|
||||
/* read the set value again using the Dn command */
|
||||
iRet = transactNHQ200(self,pCommandRead,strlen(pCommandRead),pReply,131);
|
||||
iRet = AsyncUnitTransact(self->unit,pCommandRead,strlen(pCommandRead),pReply,131);
|
||||
if (iRet <= 0)
|
||||
return iRet;
|
||||
printf("D%d: Response %d chars: '%s'\n",self->iControl, iRet, pReply);
|
||||
@@ -636,7 +589,7 @@ int NHQ200_Set(pNHQ200 *pData, float fVal)
|
||||
if(fDelta < fPrecision)
|
||||
{
|
||||
sprintf(pCommandGo, "G%d", self->iControl);
|
||||
iRet = transactNHQ200(self,pCommandGo,strlen(pCommandGo),pReply,131);
|
||||
iRet = AsyncUnitTransact(self->unit,pCommandGo,strlen(pCommandGo),pReply,131);
|
||||
if (iRet <= 0)
|
||||
return iRet;
|
||||
printf("G%d: Response %d chars: '%s'\n",self->iControl, iRet, pReply);
|
||||
@@ -686,3 +639,14 @@ void NHQ200_ErrorTxt(pNHQ200 *pData,int iCode, char *pError, int iLen)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void NHQ200InitProtocol(SicsInterp *pSics) {
|
||||
if (NHQ_Protocol == NULL) {
|
||||
NHQ_Protocol = AsyncProtocolCreate(pSics, "NHQ200", NULL, NULL);
|
||||
NHQ_Protocol->sendCommand = NHQ_Tx;
|
||||
NHQ_Protocol->handleInput = NHQ_Rx;
|
||||
NHQ_Protocol->handleEvent = NHQ_Ev;
|
||||
NHQ_Protocol->prepareTxn = NULL;
|
||||
NHQ_Protocol->killPrivate = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user