574 lines
14 KiB
C
574 lines
14 KiB
C
/*
|
|
* Low Side Server Monitor - obtains latest RCMS data
|
|
*
|
|
* Rodney Davies March 2008
|
|
*
|
|
*/
|
|
|
|
#include <netinet/tcp.h>
|
|
#include <sys/time.h>
|
|
#include <sics.h>
|
|
#include "network.h"
|
|
#include "asyncqueue.h"
|
|
#include "nwatch.h"
|
|
#include "lssmonitor.h"
|
|
#include "sicsvar.h"
|
|
#include <mxml.h>
|
|
|
|
#define _GNU_SOURCE
|
|
#include <string.h>
|
|
|
|
|
|
char *strcasestr(const char *haystack, const char *needle);
|
|
|
|
extern int DMC2280MotionControl;
|
|
|
|
#define KEY_ENABLED_BIT (1 << 0)
|
|
#define KEY_DISABLED_BIT (1 << 1)
|
|
#define SEC_OPENED_BIT (1 << 2)
|
|
#define SEC_CLOSED_BIT (1 << 3)
|
|
#define TER_OPENED_BIT (1 << 4)
|
|
#define TER_CLOSED_BIT (1 << 5)
|
|
#define MOTOR_ENABLED_BIT (1 << 6)
|
|
#define MOTOR_DISABLED_BIT (1 << 7)
|
|
#define ACCESS_LOCKED_BIT (1 << 8)
|
|
#define ACCESS_UNLOCKED_BIT (1 << 9)
|
|
#define DC_POWEROK_BIT (1 << 10)
|
|
#define EXIT_INPROGRESS_BIT (1 << 11)
|
|
#define SAFETY_TRIPPED_BIT (1 << 12)
|
|
#define SAFETY_MALFUNCTION_BIT (1 << 13)
|
|
#define TER_OPERATE_BIT (1 << 14)
|
|
#define RELAY_ENABLED_BIT (1 << 15)
|
|
#define INST_READY_BIT (1 << 16)
|
|
#define LAMP_TEST_BIT (1 << 17)
|
|
|
|
#define KEY_BOTH_BITS (KEY_ENABLED_BIT | KEY_DISABLED_BIT)
|
|
#define SEC_BOTH_BITS (SEC_OPENED_BIT | SEC_CLOSED_BIT)
|
|
#define TER_BOTH_BITS (TER_OPENED_BIT | TER_CLOSED_BIT)
|
|
#define MOTOR_BOTH_BITS (MOTOR_ENABLED_BIT | MOTOR_DISABLED_BIT)
|
|
#define ACCESS_BOTH_BITS (ACCESS_LOCKED_BIT | ACCESS_UNLOCKED_BIT)
|
|
|
|
static pAsyncProtocol LSS_Protocol = NULL;
|
|
|
|
int LSS_UserPriv = 0; /* Internal */
|
|
typedef enum {
|
|
Unknown_low, Invalid_high, Enabled, Disabled,
|
|
Opened, Closed, Locked, Unlocked, True, False,}LSS_STATUS;
|
|
|
|
char *lss_states[] = {
|
|
"Unknown_low", "Invalid_high", "Enabled",
|
|
"Disabled", "Opened", "Closed",
|
|
"Locked", "Unlocked", "True", "False"};
|
|
|
|
typedef enum {
|
|
Key,Secondary,Tertiary,MotionControl,Access,
|
|
DC,Exit,Trip,Fault,Operate,Relay,Ready,}LSS_PARAM;
|
|
|
|
char *lss_parname[] = {
|
|
"lss_key","lss_secondary","lss_tertiary","lss_motioncontrol",
|
|
"lss_access","lss_dc","lss_exit","lss_trip",
|
|
"lss_fault","lss_operate","lss_relay","lss_ready"};
|
|
|
|
typedef struct __LSSController LSSController, *pLSSController;
|
|
|
|
struct __LSSController {
|
|
pObjectDescriptor pDes;
|
|
pAsyncUnit unit; /* associated AsyncUnit object */
|
|
int iGetOut;
|
|
int iValue;
|
|
int oldValue;
|
|
pNWTimer nw_tmr; /* periodic timer handle */
|
|
pNWTimer oneshot; /* oneshot timer handle */
|
|
int timeout;
|
|
};
|
|
|
|
|
|
struct __RCMSData {
|
|
char desc[132]; /* description */
|
|
char tag[132]; /* tag */
|
|
char time[25]; /* time field */
|
|
char value[132]; /* value */
|
|
};
|
|
|
|
|
|
/* XML Tree */
|
|
mxml_node_t *tree;
|
|
|
|
|
|
static int LSS_GetState(void *pData, char *param, LSS_STATUS *retState);
|
|
|
|
static int LSS_Tx(pAsyncProtocol p, pAsyncTxn myCmd)
|
|
{
|
|
int iRet = 1;
|
|
|
|
if (myCmd) {
|
|
myCmd->txn_status = ATX_ACTIVE;
|
|
iRet = AsyncUnitWrite(myCmd->unit, myCmd->out_buf, myCmd->out_len);
|
|
/* TODO handle errors */
|
|
if (iRet < 0) { /* TODO: EOF */
|
|
iRet = AsyncUnitReconnect(myCmd->unit);
|
|
if (iRet == 0)
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int LSS_Rx(pAsyncProtocol p, pAsyncTxn myCmd, int rxchar)
|
|
{
|
|
int iRet = 1;
|
|
|
|
switch (myCmd->txn_state) {
|
|
case 0: /* first character */
|
|
/* normal data */
|
|
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->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->txn_state = 99;
|
|
/* end of line */
|
|
}
|
|
else
|
|
myCmd->txn_state = 1;
|
|
break;
|
|
}
|
|
if (myCmd->txn_state == 99) {
|
|
myCmd->inp_buf[myCmd->inp_idx] = '\0';
|
|
iRet = 0;
|
|
myCmd->txn_state = 0;
|
|
myCmd->txn_status = ATX_COMPLETE;
|
|
}
|
|
if (iRet == 0) { /* end of command */
|
|
return AQU_POP_CMD;
|
|
}
|
|
return iRet;
|
|
}
|
|
|
|
static int LSS_Ev(pAsyncProtocol p, pAsyncTxn myCmd, int event)
|
|
{
|
|
if (event == AQU_TIMEOUT) {
|
|
/* TODO: handle command timeout */
|
|
myCmd->txn_status = ATX_TIMEOUT;
|
|
return AQU_POP_CMD;
|
|
}
|
|
return AQU_POP_CMD;
|
|
}
|
|
|
|
static void LSS_Notify(void* context, int event)
|
|
{
|
|
char line[132];
|
|
|
|
sprintf(line, "LSS_Notify: (AQU_RECONNECT)%d [%d]", AQU_RECONNECT, event);
|
|
SICSLogWrite(line, eStatus);
|
|
|
|
pLSSController self = (pLSSController) context;
|
|
|
|
switch (event) {
|
|
case AQU_RECONNECT:
|
|
do {
|
|
mkChannel* sock = AsyncUnitGetSocket(self->unit);
|
|
int flag = 1;
|
|
setsockopt(sock->sockid, /* socket affected */
|
|
IPPROTO_TCP, /* set option at TCP level */
|
|
TCP_NODELAY, /* name of option */
|
|
(char *) &flag, /* the cast is historical cruft */
|
|
sizeof(int)); /* length of option value */
|
|
return;
|
|
} while (0);
|
|
}
|
|
return;
|
|
}
|
|
|
|
/* Callback function to inspect each XML node as the string is parsed */
|
|
static mxml_type_t xmlLoadStringCallback(mxml_node_t *node) {
|
|
|
|
char line[132];
|
|
char value[132];
|
|
|
|
const char *type;
|
|
mxml_type_t myType;
|
|
|
|
|
|
sprintf(value, "NO VALUE");
|
|
|
|
type = mxmlElementGetAttr(node, "type");
|
|
|
|
if (type == NULL)
|
|
type = node->value.element.name;
|
|
|
|
|
|
if (!strcmp(type, "integer"))
|
|
myType = (MXML_INTEGER);
|
|
else if (!strcmp(type, "opaque"))
|
|
myType = (MXML_OPAQUE);
|
|
else if (!strcmp(type, "real"))
|
|
myType = (MXML_REAL);
|
|
else myType = (MXML_TEXT);
|
|
|
|
if (myType == (MXML_INTEGER))
|
|
sprintf(value, "%d", node->value.integer);
|
|
|
|
if (myType == (MXML_TEXT))
|
|
sprintf(value, "%s", "text");
|
|
|
|
if (myType == (MXML_REAL))
|
|
sprintf(value, "%f", node->value.real);
|
|
|
|
if (myType == (MXML_OPAQUE))
|
|
sprintf(value, "%s", node->value.opaque);
|
|
|
|
|
|
/*
|
|
if (node->value.text.string != NULL) {
|
|
if (myType == (MXML_TEXT))
|
|
snprintf(value, 132, "[%c]", node->value.text.string);
|
|
}
|
|
*/
|
|
sprintf(line, "xmlLoadStringCallback: [%s] [%s] [%s] [%s] [%s] [%s]", node->value.element.name, type, mxmlElementGetAttr(node, "description"), mxmlElementGetAttr(node, "time"), mxmlElementGetAttr(node, "tag"), value);
|
|
SICSLogWrite(line, eStatus);
|
|
|
|
return myType;
|
|
}
|
|
|
|
/*
|
|
* \brief GetCallback is the callback for the read command.
|
|
*/
|
|
static int GetCallback(pAsyncTxn txn)
|
|
{
|
|
|
|
FILE *fp;
|
|
char line[132];
|
|
LSS_STATUS state;
|
|
int iRet,i;
|
|
unsigned int iRead;
|
|
char* resp = txn->inp_buf;
|
|
int resp_len = txn->inp_idx;
|
|
LSS_STATUS lssState;
|
|
pSicsVariable lssVar=NULL;
|
|
|
|
pLSSController self = (pLSSController) txn->cntx;
|
|
if (resp_len <= 0) {
|
|
snprintf(line, 132, "lss1 = NO INPUT!!");
|
|
SICSLogWrite(line, eStatus);
|
|
return 1;
|
|
}
|
|
else {
|
|
snprintf(line, 132, "lss1 = [%d] [%s]", strlen(resp), resp);
|
|
SICSLogWrite(line, eStatus);
|
|
|
|
|
|
/* free memory from previous tree */
|
|
if (tree) {
|
|
mxmlDelete(tree);
|
|
}
|
|
|
|
/* parse the string into an XML tree - resp *should* be raw XML data */
|
|
tree = mxmlLoadString(NULL, resp, MXML_TEXT_CALLBACK);
|
|
|
|
}
|
|
/*
|
|
if (self->oldValue != self->iValue) {
|
|
for (i=0; i < sizeof(lss_parname)/sizeof(lss_parname[0]); i++) {
|
|
lssVar = (pSicsVariable)FindCommandData(pServ->pSics,lss_parname[i],"SicsVariable");
|
|
LSS_GetState(self,lss_parname[i],&lssState);
|
|
VarSetText(lssVar,lss_states[lssState],LSS_UserPriv);
|
|
}
|
|
}
|
|
*/
|
|
self->oldValue = self->iValue;
|
|
self->iGetOut = 0;
|
|
|
|
/* send out another read request */
|
|
AsyncUnitSendTxn(self->unit, "READ", 4, GetCallback, self, 1024*1024);
|
|
return 0;
|
|
}
|
|
|
|
static int MyOneShotCallback(void* context, int mode)
|
|
{
|
|
|
|
char line[132];
|
|
|
|
pLSSController self = (pLSSController) context;
|
|
self->oneshot = 0;
|
|
AsyncUnitSendTxn(self->unit, "WRITE 0", 7, NULL, NULL, 132);
|
|
sprintf(line, "lssmonitor.c: MyOneShotCallback() -> AsyncUnitSendTxn( WRITE ) ");
|
|
SICSLogWrite(line, eStatus);
|
|
return 0;
|
|
}
|
|
|
|
static int MyTimerCallback(void* context, int mode)
|
|
{
|
|
char line[132];
|
|
|
|
pLSSController self = (pLSSController) context;
|
|
if (self->iGetOut) {
|
|
/* TODO error handling */
|
|
}
|
|
self->iGetOut = 1;
|
|
|
|
/* disable READ-polling timer for now - wait for reply ... */
|
|
NetWatchRemoveTimer(self->nw_tmr);
|
|
|
|
|
|
AsyncUnitSendTxn(self->unit, "READ", 4, GetCallback, self, 1024*1024);
|
|
sprintf(line, "lssmonitor.c: MyTimerCallback() -> AsyncUnitSendTxn(READ 1MB)");
|
|
SICSLogWrite(line, eStatus);
|
|
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* \brief PutCallback is the callback for the write command.
|
|
*/
|
|
static int PutCallback(pAsyncTxn txn)
|
|
{
|
|
char line[132];
|
|
|
|
pLSSController self = (pLSSController) txn->cntx;
|
|
if (self->oneshot)
|
|
NetWatchRemoveTimer(self->oneshot);
|
|
NetWatchRegisterTimer(&self->oneshot, 1200, MyOneShotCallback, self);
|
|
|
|
|
|
sprintf(line, "lssmonitor.c: PutCallback() -> RegisterTimer ");
|
|
SICSLogWrite(line, eStatus);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int LSS_GetState(void *pData, char *param, LSS_STATUS *retState)
|
|
{
|
|
pLSSController self = (pLSSController) pData;
|
|
/*
|
|
if (strcasecmp(param, lss_parname[Key]) == 0) {
|
|
*retState = Unknown_low;
|
|
if ((self->iValue & KEY_BOTH_BITS) == KEY_BOTH_BITS)
|
|
*retState = Invalid_high;
|
|
else if (self->iValue & KEY_ENABLED_BIT)
|
|
*retState = Enabled;
|
|
else if (self->iValue & KEY_DISABLED_BIT)
|
|
*retState = Disabled;
|
|
return OKOK;
|
|
}
|
|
*/
|
|
*retState = Unknown_low;
|
|
return OKOK;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int LSS_Print(SConnection *pCon, SicsInterp *pSics,
|
|
void *pData, char *name, char *param)
|
|
{
|
|
char line[132];
|
|
LSS_STATUS state;
|
|
|
|
if (LSS_GetState(pData, param, &state) != OKOK) {
|
|
return 0;
|
|
} else {
|
|
snprintf(line, 132, "%s.%s = %s", name, param, lss_states[state]);
|
|
SCWrite(pCon, line, eStatus);
|
|
return OKOK;
|
|
}
|
|
}
|
|
|
|
|
|
static int findElement(SConnection *pCon, const char *string) {
|
|
|
|
char line[132];
|
|
mxml_node_t *n = NULL;
|
|
int found = 0;
|
|
|
|
for(n = mxmlWalkNext(tree, tree, MXML_DESCEND); n != NULL; n = mxmlWalkNext(n, tree, MXML_DESCEND)) {
|
|
if (n->value.element.attrs) {
|
|
if (strcasestr(mxmlElementGetAttr(n, "description"), string) != NULL) {
|
|
snprintf(line, 132, "%s.%s %s = %s (%s)", string, mxmlElementGetAttr(n, "description"), mxmlElementGetAttr(n, "tag"), n->child->value.text.string, mxmlElementGetAttr(n, "time"));
|
|
SCWrite(pCon, line, eStatus);
|
|
found = 1;
|
|
}
|
|
|
|
if (strcasestr(mxmlElementGetAttr(n, "tag"), string) != NULL) {
|
|
snprintf(line, 132, "%s.%s %s = %s (%s)", string, mxmlElementGetAttr(n, "description"), mxmlElementGetAttr(n, "tag"), n->child->value.text.string, mxmlElementGetAttr(n, "time"));
|
|
SCWrite(pCon, line, eStatus);
|
|
found = 1;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return found;
|
|
}
|
|
|
|
|
|
static int LSS_Action(SConnection *pCon, SicsInterp *pSics,
|
|
void *pData, int argc, char *argv[])
|
|
{
|
|
mxml_node_t *node;
|
|
mxml_node_t *current;
|
|
|
|
char line[132];
|
|
pLSSController self = (pLSSController) pData;
|
|
if (argc == 1) {
|
|
snprintf(line, 132, "%s.iValue = %06X", argv[0], self->iValue & 0xffffff);
|
|
SCWrite(pCon, line, eStatus);
|
|
return OKOK;
|
|
}
|
|
else if (argc == 2) {
|
|
if (strcasecmp(argv[1], "list") == 0) {
|
|
|
|
current = tree;
|
|
|
|
for (node = mxmlWalkNext(current, tree, MXML_DESCEND); node != NULL; node = mxmlWalkNext(node, tree, MXML_DESCEND)) {
|
|
if (node->value.element.attrs) {
|
|
|
|
snprintf(line, 132, "%s.%s %s = %s (%s)", argv[0], mxmlElementGetAttr(node, "description"), mxmlElementGetAttr(node, "tag"), node->child->value.text.string, mxmlElementGetAttr(node, "time"));
|
|
SCWrite(pCon, line, eStatus);
|
|
}
|
|
}
|
|
|
|
return OKOK;
|
|
}
|
|
|
|
|
|
/* search by sub string */
|
|
if (findElement(pCon, argv[1]) > 0) {
|
|
return OKOK;
|
|
}
|
|
}
|
|
|
|
snprintf(line, 132, "%s does not understand %s", argv[0], argv[1]);
|
|
SCWrite(pCon, line, eError);
|
|
return 0;
|
|
}
|
|
|
|
static pLSSController LSS_Create(const char* pName)
|
|
{
|
|
pLSSController self = NULL;
|
|
|
|
self = (pLSSController) malloc(sizeof(LSSController));
|
|
if (self == NULL)
|
|
return NULL;
|
|
memset(self, 0, sizeof(LSSController));
|
|
if (AsyncUnitCreate(pName, &self->unit) == 0) {
|
|
free(self);
|
|
return NULL;
|
|
}
|
|
AsyncUnitSetNotify(self->unit, self, LSS_Notify);
|
|
AsyncUnitSetDelay(self->unit, 50);
|
|
|
|
self->pDes = CreateDescriptor("LSS");
|
|
return self;
|
|
}
|
|
|
|
static int LSS_Init(pLSSController self)
|
|
{
|
|
/* TODO: Init the controller */
|
|
if (self->nw_tmr != NULL)
|
|
NetWatchRemoveTimer(self->nw_tmr);
|
|
|
|
AsyncUnitSendTxn(self->unit, "READ", 4, GetCallback, self, 1024*1024);
|
|
|
|
/*
|
|
NetWatchRegisterTimerPeriodic(&self->nw_tmr,
|
|
2000, 2000,
|
|
MyTimerCallback,
|
|
self);
|
|
self->timeout=120000;
|
|
*/
|
|
return 1;
|
|
}
|
|
|
|
static void LSS_Kill(void* pData)
|
|
{
|
|
/* free memory from previous tree */
|
|
if (tree) {
|
|
mxmlDelete(tree);
|
|
}
|
|
|
|
pLSSController self = (pLSSController) pData;
|
|
if (self->nw_tmr)
|
|
NetWatchRemoveTimer(self->nw_tmr);
|
|
if (self->pDes) {
|
|
DeleteDescriptor(self->pDes);
|
|
self->pDes = NULL;
|
|
}
|
|
free(self);
|
|
return;
|
|
}
|
|
|
|
void LSSInitProtocol(SicsInterp *pSics) {
|
|
if (LSS_Protocol == NULL) {
|
|
LSS_Protocol = AsyncProtocolCreate(pSics, "LSS", NULL, NULL);
|
|
LSS_Protocol->sendCommand = LSS_Tx;
|
|
LSS_Protocol->handleInput = LSS_Rx;
|
|
LSS_Protocol->handleEvent = LSS_Ev;
|
|
LSS_Protocol->prepareTxn = NULL;
|
|
LSS_Protocol->killPrivate = NULL;
|
|
}
|
|
}
|
|
|
|
int LSSFactory(SConnection *pCon, SicsInterp *pSics,
|
|
void *pData, int argc, char *argv[])
|
|
{
|
|
pLSSController pNew = NULL;
|
|
int iRet, status, i;
|
|
char pError[256];
|
|
pSicsVariable lssVar=NULL;
|
|
LSS_STATUS lssState;
|
|
|
|
if(argc < 3)
|
|
{
|
|
SCWrite(pCon,"ERROR: insufficient no of arguments to LSSFactory",
|
|
eError);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
create data structure and open port
|
|
*/
|
|
pNew = LSS_Create(argv[2]);
|
|
|
|
if(!pNew)
|
|
{
|
|
SCWrite(pCon,"ERROR: failed to create LSS in LSSFactory",eError);
|
|
return 0;
|
|
}
|
|
|
|
status = LSS_Init(pNew);
|
|
if(status != 1)
|
|
{
|
|
sprintf(pError,"ERROR: failed to connect to %s",argv[2]);
|
|
SCWrite(pCon,pError,eError);
|
|
}
|
|
/*
|
|
for (i=0; i < sizeof(lss_parname)/sizeof(lss_parname[0]); i++) {
|
|
lssVar = VarCreate(LSS_UserPriv,veText,lss_parname[i]);
|
|
LSS_GetState(pNew,lss_parname[i],&lssState);
|
|
VarSetText(lssVar,lss_states[lssState],LSS_UserPriv);
|
|
AddCommand(pSics,lss_parname[i],VarWrapper,(KillFunc)VarKill,lssVar);
|
|
}
|
|
*/
|
|
/*
|
|
create the command
|
|
*/
|
|
|
|
iRet = AddCommand(pSics, argv[1], LSS_Action, LSS_Kill, pNew);
|
|
if(!iRet)
|
|
{
|
|
sprintf(pError,"ERROR: duplicate command %s not created [%d]", argv[1], iRet);
|
|
SCWrite(pCon,pError,eError);
|
|
LSS_Kill(pNew);
|
|
return 0;
|
|
}
|
|
SCSendOK(pCon);
|
|
return 1;
|
|
}
|