- Installed json protocol
SKIPPED: psi/libpsi.a psi/tecs/libtecsl.a
This commit is contained in:
@ -50,7 +50,9 @@ typedef enum {
|
|||||||
eFinish,
|
eFinish,
|
||||||
eEvent,
|
eEvent,
|
||||||
eWarning,
|
eWarning,
|
||||||
eError
|
eError,
|
||||||
|
eHdbValue,
|
||||||
|
eHdbEvent
|
||||||
} OutCode;
|
} OutCode;
|
||||||
|
|
||||||
#include "interrupt.h"
|
#include "interrupt.h"
|
||||||
|
@ -26,7 +26,7 @@ SUBLIBS = psi/libpsi.a psi/hardsup/libhlib.a matrix/libmatrix.a \
|
|||||||
LIBS = -L$(HDFROOT)/lib $(SUBLIBS) $(NILIB)\
|
LIBS = -L$(HDFROOT)/lib $(SUBLIBS) $(NILIB)\
|
||||||
-ltcl8.4 -lmxml $(HDFROOT)/lib/libhdf5.a \
|
-ltcl8.4 -lmxml $(HDFROOT)/lib/libhdf5.a \
|
||||||
$(HDFROOT)/lib/libmfhdf.a $(HDFROOT)/lib/libdf.a \
|
$(HDFROOT)/lib/libmfhdf.a $(HDFROOT)/lib/libdf.a \
|
||||||
-lmxml -lghttp -ljpeg -ldl -lz -lsz -lm -lc
|
-lmxml -lghttp -ljpeg -ljson -ldl -lz -lsz -lm -lc
|
||||||
|
|
||||||
include make_gen
|
include make_gen
|
||||||
|
|
||||||
|
BIN
matrix/libmatrix.a
Normal file
BIN
matrix/libmatrix.a
Normal file
Binary file not shown.
222
protocol.c
222
protocol.c
@ -16,11 +16,13 @@
|
|||||||
#include <dynstring.h>
|
#include <dynstring.h>
|
||||||
#include "commandlog.h"
|
#include "commandlog.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
#include <json/json.h>
|
||||||
|
|
||||||
#define MAXMSG 1024
|
#define MAXMSG 1024
|
||||||
#define INIT_STR_SIZE 256
|
#define INIT_STR_SIZE 256
|
||||||
#define STR_RESIZE_LENGTH 256
|
#define STR_RESIZE_LENGTH 256
|
||||||
|
#define NUMPROS 5
|
||||||
|
#define PROLISTLEN 6
|
||||||
typedef struct __Protocol {
|
typedef struct __Protocol {
|
||||||
pObjectDescriptor pDes; /* required as first field */
|
pObjectDescriptor pDes; /* required as first field */
|
||||||
char *name; /* protocol handler name */
|
char *name; /* protocol handler name */
|
||||||
@ -28,7 +30,7 @@ typedef struct __Protocol {
|
|||||||
int iNumPros; /* number of valid protocols? */
|
int iNumPros; /* number of valid protocols? */
|
||||||
writeFunc defaultWriter; /* default write function */
|
writeFunc defaultWriter; /* default write function */
|
||||||
int isDefaultSet;
|
int isDefaultSet;
|
||||||
char *pProList[5]; /* list of valid protocols? */
|
char *pProList[PROLISTLEN]; /* list of valid protocols? */
|
||||||
} Protocol;
|
} Protocol;
|
||||||
|
|
||||||
char *pEventType[]={
|
char *pEventType[]={
|
||||||
@ -85,8 +87,6 @@ pProtocol CreateProtocol(void);
|
|||||||
static int ProtocolOptions(SConnection* pCon, pProtocol pPro);
|
static int ProtocolOptions(SConnection* pCon, pProtocol pPro);
|
||||||
static int ProtocolHelp(SConnection* pCon, Protocol* pPro);
|
static int ProtocolHelp(SConnection* pCon, Protocol* pPro);
|
||||||
static int ProtocolSet(SConnection* pCon, Protocol* pPro, char *pProName);
|
static int ProtocolSet(SConnection* pCon, Protocol* pPro, char *pProName);
|
||||||
static int ProtocolGet(SConnection* pCon, Protocol* pPro, char *pProName,
|
|
||||||
int *pIndex);
|
|
||||||
static int ProtocolList(SConnection* pCon, Protocol* pPro);
|
static int ProtocolList(SConnection* pCon, Protocol* pPro);
|
||||||
int ProtocolAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
int ProtocolAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||||
int argc, char *argv[]);
|
int argc, char *argv[]);
|
||||||
@ -95,15 +95,17 @@ static int InitDefaultProtocol(SConnection* pCon, Protocol *pPro);
|
|||||||
|
|
||||||
/* Signatures for protocol writers implemented in this file */
|
/* Signatures for protocol writers implemented in this file */
|
||||||
int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut);
|
int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut);
|
||||||
|
int SCWriteJSON_String(SConnection *pCon, char *pBuffer, int iOut);
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
pProtocol CreateProtocol(void)
|
pProtocol CreateProtocol(void)
|
||||||
{
|
{
|
||||||
int i, iNumPros = 4;
|
int i, iNumPros = NUMPROS;
|
||||||
char *pPros[5] = {"default",
|
char *pPros[] = {"default",
|
||||||
"normal",
|
"normal",
|
||||||
"withcode",
|
"withcode",
|
||||||
"sycamore",
|
"sycamore",
|
||||||
|
"json",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
pProtocol pNew = NULL;
|
pProtocol pNew = NULL;
|
||||||
@ -276,7 +278,9 @@ static int ProtocolSet(SConnection* pCon, Protocol* pPro, char *pProName)
|
|||||||
case 3: /* sycamore */
|
case 3: /* sycamore */
|
||||||
SCSetWriteFunc(pCon,SCWriteSycamore);
|
SCSetWriteFunc(pCon,SCWriteSycamore);
|
||||||
break;
|
break;
|
||||||
|
case 4: /* json */
|
||||||
|
SCSetWriteFunc(pCon,SCWriteJSON_String);
|
||||||
|
break;
|
||||||
case 0: /* default = psi_sics */
|
case 0: /* default = psi_sics */
|
||||||
default:
|
default:
|
||||||
SCSetWriteFunc(pCon,pPro->defaultWriter);
|
SCSetWriteFunc(pCon,pPro->defaultWriter);
|
||||||
@ -288,9 +292,10 @@ static int ProtocolSet(SConnection* pCon, Protocol* pPro, char *pProName)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
static int ProtocolGet(SConnection* pCon, Protocol* pPro, char *pProName,
|
int ProtocolGet(SConnection* pCon, void* pData, char *pProName, int len)
|
||||||
int *pIndex)
|
|
||||||
{
|
{
|
||||||
|
int Index;
|
||||||
|
Protocol *pPro = (Protocol *)pData;
|
||||||
if(!SCVerifyConnection(pCon))
|
if(!SCVerifyConnection(pCon))
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@ -303,24 +308,27 @@ static int ProtocolGet(SConnection* pCon, Protocol* pPro, char *pProName,
|
|||||||
pPro->isDefaultSet = 1;
|
pPro->isDefaultSet = 1;
|
||||||
pCon->iProtocolID = 0;
|
pCon->iProtocolID = 0;
|
||||||
}
|
}
|
||||||
|
strncpy(pProName, pPro->pProList[pCon->iProtocolID], len);
|
||||||
*pIndex = (int)malloc(sizeof(int));
|
return 1;
|
||||||
*pIndex = pCon->iProtocolID;
|
#if 0
|
||||||
|
Index = pCon->iProtocolID;
|
||||||
|
|
||||||
/* check list of protocols for valid name */
|
/* check list of protocols for valid name */
|
||||||
switch(*pIndex)
|
switch(Index)
|
||||||
{
|
{
|
||||||
case 0: /* default = psi_sics */
|
case 0: /* default = psi_sics */
|
||||||
case 1: /* normal (connection start default) */
|
case 1: /* normal (connection start default) */
|
||||||
case 2: /* outcodes */
|
case 2: /* outcodes */
|
||||||
case 3: /* sycamore */
|
case 3: /* sycamore */
|
||||||
pProName = strdup(pPro->pProList[*pIndex]);
|
case 4: /* json */
|
||||||
|
pProName = pPro->pProList[Index];
|
||||||
return 1;
|
return 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
@ -351,10 +359,10 @@ int ProtocolAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|||||||
{NULL}
|
{NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
assert(pCon);
|
assert(pCon != NULL);
|
||||||
assert(pSics);
|
assert(pSics != NULL);
|
||||||
pPro = (pProtocol)pData;
|
pPro = (pProtocol)pData;
|
||||||
assert(pPro);
|
assert(pPro != NULL);
|
||||||
|
|
||||||
/* You need to have User level access rights to use this facility */
|
/* You need to have User level access rights to use this facility */
|
||||||
if(!SCMatchRights(pCon,usSpy))
|
if(!SCMatchRights(pCon,usSpy))
|
||||||
@ -427,8 +435,6 @@ static int InitDefaultProtocol(SConnection* pCon, Protocol *pPro)
|
|||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------*/
|
||||||
void sycformat(char *tag, OutCode msgFlag, pDynString msgString, pDynString msgOut) {
|
void sycformat(char *tag, OutCode msgFlag, pDynString msgString, pDynString msgOut) {
|
||||||
char typePrefix[] = "type.";
|
|
||||||
char statusPrefix[] = "status.";
|
|
||||||
DynStringConcat(msgOut," ");
|
DynStringConcat(msgOut," ");
|
||||||
switch (msgFlag) {
|
switch (msgFlag) {
|
||||||
eEvent:
|
eEvent:
|
||||||
@ -445,19 +451,13 @@ void sycformat(char *tag, OutCode msgFlag, pDynString msgString, pDynString msgO
|
|||||||
}
|
}
|
||||||
int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut)
|
int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut)
|
||||||
{
|
{
|
||||||
int i, iPtr, iRet;
|
int iRet;
|
||||||
int bDevIDdone = 0;
|
|
||||||
int bFlagDone = 0;
|
|
||||||
char pBueffel[MAXMSG], *pBufferFrom, *pBufferTo;
|
char pBueffel[MAXMSG], *pBufferFrom, *pBufferTo;
|
||||||
long taskID = 0;
|
long taskID = 0;
|
||||||
/* char pPrefix[40];*/
|
/* char pPrefix[40];*/
|
||||||
pDynString pMsg = NULL;
|
pDynString pMsg = NULL;
|
||||||
pDynString pMsgString = NULL;
|
pDynString pMsgString = NULL;
|
||||||
SicsInterp *pSics;
|
|
||||||
TokenList *pList = NULL;
|
|
||||||
TokenList *pCurrent;
|
|
||||||
commandContext comCon;
|
commandContext comCon;
|
||||||
char *savedTclResult = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
if (strlen(pBuffer) == 0) {
|
if (strlen(pBuffer) == 0) {
|
||||||
@ -517,11 +517,6 @@ int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
build the Tcl-command to execute for formatting the
|
|
||||||
data into a sycamore string
|
|
||||||
*/
|
|
||||||
pSics = GetInterpreter();
|
|
||||||
taskID = comCon.transID;
|
taskID = comCon.transID;
|
||||||
|
|
||||||
pMsg = CreateDynString(INIT_STR_SIZE, STR_RESIZE_LENGTH);
|
pMsg = CreateDynString(INIT_STR_SIZE, STR_RESIZE_LENGTH);
|
||||||
@ -552,9 +547,14 @@ int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut)
|
|||||||
|
|
||||||
if (iOut == eEvent) {
|
if (iOut == eEvent) {
|
||||||
DynStringConcat(pMsgString, " type=");
|
DynStringConcat(pMsgString, " type=");
|
||||||
DynStringConcat(pMsgString, pEventType[pCon->conEventType]);
|
/* Default type to VALUECHANGE if conEventType not set */
|
||||||
|
if (-1 == pCon->conEventType)
|
||||||
|
DynStringConcat(pMsgString, pEventType[0]);
|
||||||
|
else
|
||||||
|
DynStringConcat(pMsgString, pEventType[pCon->conEventType]);
|
||||||
/* DynStringConcat(pMsgString, " status=");
|
/* DynStringConcat(pMsgString, " status=");
|
||||||
DynStringConcat(pMsgString, pStatus[pCon->conStatus]);*/
|
DynStringConcat(pMsgString, pStatus[pCon->conStatus]);*/
|
||||||
|
DynStringConcat(pMsgString,",");
|
||||||
}
|
}
|
||||||
DynStringConcat(pMsgString," ");
|
DynStringConcat(pMsgString," ");
|
||||||
DynStringConcat(pMsgString,pBuffer);
|
DynStringConcat(pMsgString,pBuffer);
|
||||||
@ -577,6 +577,163 @@ int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Only work for hipadaba commands, hlist, hset, hget, hnotify
|
||||||
|
* A multiline string (ie have crnl) will be converted to an array.
|
||||||
|
* Strings with '=' will be converted to name value pairs
|
||||||
|
*/
|
||||||
|
struct json_object *mkJSON_Object(SConnection *pCon, char *pBuffer, int iOut)
|
||||||
|
{
|
||||||
|
int linenum = __LINE__;
|
||||||
|
char pBueffel[MAXMSG], *pBufferFrom, *pBufferTo;
|
||||||
|
long taskID = 0;
|
||||||
|
struct json_object *msg_json=NULL, *tmp_json=NULL;
|
||||||
|
|
||||||
|
commandContext comCon;
|
||||||
|
char pError[256];
|
||||||
|
pError[0]='\0';
|
||||||
|
|
||||||
|
|
||||||
|
if (strlen(pBuffer) == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!SCVerifyConnection(pCon))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
comCon = SCGetContext(pCon);
|
||||||
|
|
||||||
|
/* Return 0 without dying if no message data */
|
||||||
|
if(pBuffer == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
build the Tcl-command to execute for formatting the
|
||||||
|
data into a sycamore string
|
||||||
|
*/
|
||||||
|
taskID = comCon.transID;
|
||||||
|
|
||||||
|
pBueffel[0] = '\0';
|
||||||
|
msg_json = json_object_new_object();
|
||||||
|
if (is_error(msg_json)) { linenum = __LINE__; goto reporterr; }
|
||||||
|
/* field 1: connID */
|
||||||
|
json_object_object_add(msg_json, "con", json_object_new_int(pCon->ident));
|
||||||
|
/* field 2: taskID */
|
||||||
|
json_object_object_add(msg_json, "trans", json_object_new_int(taskID));
|
||||||
|
/* deviceID */
|
||||||
|
json_object_object_add(msg_json, "object", json_object_new_string(comCon.deviceID));
|
||||||
|
|
||||||
|
/* msgFlag */
|
||||||
|
switch(iOut) {
|
||||||
|
case 5: /* eValue */
|
||||||
|
json_object_object_add(msg_json, "flag", json_object_new_string("out"));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
json_object_object_add(msg_json, "flag", json_object_new_string(pCode[iOut]));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (iOut == eHdbValue || iOut == eHdbEvent) {
|
||||||
|
tmp_json = json_tokener_parse(pBuffer);
|
||||||
|
if (is_error(tmp_json)) { linenum = __LINE__; goto reporterr; }
|
||||||
|
} else {
|
||||||
|
/* Strip \r and \n */
|
||||||
|
for (pBufferFrom=pBufferTo=pBuffer; ; pBufferFrom++) {
|
||||||
|
if (*pBufferFrom == '\r' || *pBufferFrom == '\n')
|
||||||
|
continue;
|
||||||
|
pBufferTo = pBufferFrom;
|
||||||
|
if (*pBufferTo == '\0')
|
||||||
|
break;
|
||||||
|
pBufferTo++;
|
||||||
|
}
|
||||||
|
tmp_json = json_object_new_string(pBuffer);
|
||||||
|
if (is_error(tmp_json)) { linenum = __LINE__; goto reporterr; }
|
||||||
|
}
|
||||||
|
json_object_object_add(msg_json, "data", tmp_json);
|
||||||
|
return msg_json;
|
||||||
|
|
||||||
|
reporterr:
|
||||||
|
SCSetWriteFunc(pCon,SCNormalWrite);
|
||||||
|
snprintf(pError, 256,"{\"ERROR\": \"%s:%d Error making json object\"}", __FILE__, linenum);
|
||||||
|
SCWrite(pCon,pError,eError);
|
||||||
|
SCSetWriteFunc(pCon,SCWriteJSON_String);
|
||||||
|
cleanup:
|
||||||
|
if (tmp_json != NULL && !is_error(tmp_json))
|
||||||
|
json_object_put(tmp_json);
|
||||||
|
if (msg_json != NULL && !is_error(msg_json))
|
||||||
|
json_object_put(msg_json);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SCWriteJSON_String(SConnection *pCon, char *pBuffer, int iOut)
|
||||||
|
{
|
||||||
|
struct json_object *my_object=NULL, *tmp_json=NULL;
|
||||||
|
char pBueffel[MAXMSG], errBuff[MAXMSG];
|
||||||
|
int iRet, errLen = MAXMSG;
|
||||||
|
|
||||||
|
if (strlen(pBuffer) == 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
/* log it for any case */
|
||||||
|
if(pCon->pSock)
|
||||||
|
{
|
||||||
|
iRet = pCon->pSock->sockid;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iRet = 0;
|
||||||
|
}
|
||||||
|
sprintf(pBueffel,"Next line intended for socket: %d",iRet);
|
||||||
|
SICSLogWrite(pBueffel,eInternal);
|
||||||
|
SICSLogWrite(pBuffer,iOut);
|
||||||
|
|
||||||
|
/* write to commandlog if user or manager privilege */
|
||||||
|
if(SCGetRights(pCon) <= usUser)
|
||||||
|
{
|
||||||
|
if(pCon->iMacro != 1)
|
||||||
|
{
|
||||||
|
sprintf(pBueffel,"To sock %d :",iRet);
|
||||||
|
WriteToCommandLog(pBueffel,pBuffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(iOut == eError || iOut == eWarning)
|
||||||
|
{
|
||||||
|
sprintf(pBueffel,"To sock %d :",iRet);
|
||||||
|
WriteToCommandLog(pBueffel,pBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(SCinMacro(pCon))
|
||||||
|
{
|
||||||
|
InterpWrite(pServ->pSics,pBuffer);
|
||||||
|
/* print it to client if error message */
|
||||||
|
if((iOut== eError) || (iOut == eWarning) )
|
||||||
|
{
|
||||||
|
tmp_json = json_object_new_string(pBuffer);
|
||||||
|
iRet = SCDoSockWrite(pCon,json_object_to_json_string(tmp_json));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((my_object = mkJSON_Object(pCon, pBuffer, iOut)) == NULL) {
|
||||||
|
snprintf(errBuff, errLen, "failed to make JSON object from, %s", pBuffer);
|
||||||
|
tmp_json = json_object_new_string(errBuff);
|
||||||
|
my_object = json_object_new_object();
|
||||||
|
json_object_object_add(my_object, "ERROR", tmp_json);
|
||||||
|
SCDoSockWrite(pCon,json_object_to_json_string(my_object));
|
||||||
|
iRet = 0;
|
||||||
|
} else {
|
||||||
|
iRet = SCDoSockWrite(pCon,json_object_to_json_string(my_object));
|
||||||
|
SCWriteToLogFiles(pCon,pBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tmp_json != NULL && !is_error(tmp_json))
|
||||||
|
json_object_put(tmp_json);
|
||||||
|
if (my_object != NULL && !is_error(my_object))
|
||||||
|
json_object_put(my_object);
|
||||||
|
return iRet;
|
||||||
|
}
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
/* Protocol API */
|
/* Protocol API */
|
||||||
char * GetProtocolName(SConnection* pCon)
|
char * GetProtocolName(SConnection* pCon)
|
||||||
@ -604,6 +761,7 @@ char * GetProtocolName(SConnection* pCon)
|
|||||||
case 1: /* normal (connection start default) */
|
case 1: /* normal (connection start default) */
|
||||||
case 2: /* outcodes */
|
case 2: /* outcodes */
|
||||||
case 3: /* sycamore */
|
case 3: /* sycamore */
|
||||||
|
case 4: /* json */
|
||||||
return strdup(pPro->pProList[pCon->iProtocolID]);
|
return strdup(pPro->pProList[pCon->iProtocolID]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -30,5 +30,6 @@ int SCWriteSycamore(SConnection *pCon, char *pBuffer, int iOut);
|
|||||||
/*--------------------- implement protocol API -----------------------*/
|
/*--------------------- implement protocol API -----------------------*/
|
||||||
char * GetProtocolName(SConnection *pCon);
|
char * GetProtocolName(SConnection *pCon);
|
||||||
int GetProtocolID(SConnection *pCon);
|
int GetProtocolID(SConnection *pCon);
|
||||||
|
int ProtocolGet(SConnection* pCon, void* pData, char *pProName, int len);
|
||||||
/*-----------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------*/
|
||||||
#endif
|
#endif
|
||||||
|
145
sicshipadaba.c
145
sicshipadaba.c
@ -23,6 +23,7 @@
|
|||||||
#include "sicspoll.h"
|
#include "sicspoll.h"
|
||||||
#include <lld.h>
|
#include <lld.h>
|
||||||
#include <stptok.h>
|
#include <stptok.h>
|
||||||
|
#include "protocol.h"
|
||||||
|
|
||||||
/*== there can be only hipadaba in SICS, some globals to care for that == */
|
/*== there can be only hipadaba in SICS, some globals to care for that == */
|
||||||
static pHdb root = NULL;
|
static pHdb root = NULL;
|
||||||
@ -147,6 +148,53 @@ typedef struct {
|
|||||||
SConnection *pCon;
|
SConnection *pCon;
|
||||||
commandContext context;
|
commandContext context;
|
||||||
}HdbCBInfo;
|
}HdbCBInfo;
|
||||||
|
|
||||||
|
static int isJSON(SConnection *pCon) {
|
||||||
|
char proName[128];
|
||||||
|
void *pData;
|
||||||
|
|
||||||
|
if(SCinMacro(pCon)){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
pData = FindCommandData(pServ->pSics, "protocol","Protocol");
|
||||||
|
ProtocolGet(pCon, pData, proName, 128);
|
||||||
|
if (strcmp(proName, "json") == 0)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int formatNameValue(int jsonSet, char *name, char *value, pDynString result) {
|
||||||
|
if (name == NULL) {
|
||||||
|
if (jsonSet) {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else if (value == NULL) {
|
||||||
|
if (jsonSet) {
|
||||||
|
DynStringInsert(result,"\": ", 0);
|
||||||
|
DynStringInsert(result,name,0);
|
||||||
|
DynStringInsert(result,"{\"", 0);
|
||||||
|
DynStringConcat(result,"}");
|
||||||
|
} else {
|
||||||
|
DynStringInsert(result," =",0);
|
||||||
|
DynStringInsert(result,name,0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (jsonSet) {
|
||||||
|
DynStringCopy(result,"{\"");
|
||||||
|
DynStringConcat(result,name);
|
||||||
|
DynStringConcat(result,"\": ");
|
||||||
|
DynStringConcat(result,value);
|
||||||
|
DynStringConcat(result,"}");
|
||||||
|
} else {
|
||||||
|
DynStringCopy(result,name);
|
||||||
|
DynStringConcat(result," = ");
|
||||||
|
DynStringConcat(result,value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return jsonSet;
|
||||||
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------------------*/
|
||||||
static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
||||||
hdbValue v){
|
hdbValue v){
|
||||||
@ -154,10 +202,16 @@ static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
|||||||
pDynString printedData = NULL;
|
pDynString printedData = NULL;
|
||||||
pDynString result = NULL;
|
pDynString result = NULL;
|
||||||
char *pPath = NULL;
|
char *pPath = NULL;
|
||||||
|
int protocol = 0, outCode;
|
||||||
|
|
||||||
cbInfo = (HdbCBInfo *)userData;
|
cbInfo = (HdbCBInfo *)userData;
|
||||||
pPath = GetHipadabaPath(node);
|
pPath = GetHipadabaPath(node);
|
||||||
result = CreateDynString(128,128);
|
result = CreateDynString(128,128);
|
||||||
|
if ((protocol = isJSON(cbInfo->pCon)) == 1)
|
||||||
|
outCode = eHdbEvent;
|
||||||
|
else
|
||||||
|
outCode = eEvent;
|
||||||
|
|
||||||
if(v.arrayLength < 100){
|
if(v.arrayLength < 100){
|
||||||
printedData = formatValue(v);
|
printedData = formatValue(v);
|
||||||
if(pPath == NULL || printedData == NULL || result == NULL){
|
if(pPath == NULL || printedData == NULL || result == NULL){
|
||||||
@ -169,17 +223,14 @@ static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
|||||||
*/
|
*/
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
DynStringCopy(result,pPath);
|
formatNameValue(protocol, pPath, GetCharArray(printedData), result);
|
||||||
DynStringConcat(result," = ");
|
|
||||||
DynStringConcat(result,GetCharArray(printedData));
|
|
||||||
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
||||||
eEvent,cbInfo->context);
|
outCode,cbInfo->context);
|
||||||
DeleteDynString(printedData);
|
DeleteDynString(printedData);
|
||||||
} else {
|
} else {
|
||||||
DynStringCopy(result,"!!datachange!! = ");
|
formatNameValue(protocol,"!!datachange!!", pPath, result);
|
||||||
DynStringConcat(result,pPath);
|
|
||||||
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
||||||
eEvent,cbInfo->context);
|
outCode,cbInfo->context);
|
||||||
}
|
}
|
||||||
free(pPath);
|
free(pPath);
|
||||||
DeleteDynString(result);
|
DeleteDynString(result);
|
||||||
@ -203,12 +254,21 @@ static int TreeChangeCallback(void *userData, void *callData, pHdb node,
|
|||||||
hdbValue v){
|
hdbValue v){
|
||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
pDynString result = NULL;
|
||||||
|
int protocol = 0, outCode;
|
||||||
|
|
||||||
|
result = CreateDynString(128,128);
|
||||||
HdbCBInfo *cbInfo = (HdbCBInfo *)userData;
|
HdbCBInfo *cbInfo = (HdbCBInfo *)userData;
|
||||||
|
|
||||||
if(cbInfo != NULL && cbInfo->pCon != NULL){
|
if(cbInfo != NULL && cbInfo->pCon != NULL){
|
||||||
path = GetHipadabaPath(node);
|
path = GetHipadabaPath(node);
|
||||||
snprintf(buffer,1023,"treechange = %s", path);
|
if ((protocol = isJSON(cbInfo->pCon)) == 1)
|
||||||
SCWriteInContext(cbInfo->pCon,buffer,eEvent,cbInfo->context);
|
outCode = eHdbEvent;
|
||||||
|
else
|
||||||
|
outCode = eEvent;
|
||||||
|
formatNameValue(protocol, "treechange", path, result);
|
||||||
|
SCWriteInContext(cbInfo->pCon,GetCharArray(result),outCode,cbInfo->context);
|
||||||
|
DeleteDynString(result);
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -1668,6 +1728,7 @@ static int GetHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|||||||
pDynString parData = NULL;
|
pDynString parData = NULL;
|
||||||
char error[512], oriPath[512];;
|
char error[512], oriPath[512];;
|
||||||
int i, status;
|
int i, status;
|
||||||
|
int protocol = 0, outCode;
|
||||||
|
|
||||||
if(argc < 2) {
|
if(argc < 2) {
|
||||||
SCWrite(pCon,"ERROR: need path to node to print",eError);
|
SCWrite(pCon,"ERROR: need path to node to print",eError);
|
||||||
@ -1686,9 +1747,13 @@ static int GetHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|||||||
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
DynStringInsert(parData," =",0);
|
if ((protocol = isJSON(pCon)) == 1)
|
||||||
DynStringInsert(parData,oriPath,0);
|
outCode = eHdbEvent;
|
||||||
SCWrite(pCon,GetCharArray(parData),eValue);
|
else
|
||||||
|
outCode = eEvent;
|
||||||
|
|
||||||
|
formatNameValue(protocol, oriPath, NULL, parData);
|
||||||
|
SCWrite(pCon,GetCharArray(parData),outCode);
|
||||||
DeleteDynString(parData);
|
DeleteDynString(parData);
|
||||||
ReleaseHdbValue(&newValue);
|
ReleaseHdbValue(&newValue);
|
||||||
|
|
||||||
@ -1811,7 +1876,7 @@ static int HdbNodeVal(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|||||||
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
SCWrite(pCon,GetCharArray(parData),eValue);
|
SCWrite(pCon,GetCharArray(parData),eHdbValue);
|
||||||
DeleteDynString(parData);
|
DeleteDynString(parData);
|
||||||
ReleaseHdbValue(&newValue);
|
ReleaseHdbValue(&newValue);
|
||||||
|
|
||||||
@ -1850,6 +1915,47 @@ static pDynString formatPlainList(pHdb node){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static pDynString formatJSONList(pHdb node){
|
||||||
|
pHdb current;
|
||||||
|
pDynString result = NULL;
|
||||||
|
pDynString data = NULL;
|
||||||
|
|
||||||
|
if (node->child == NULL) return NULL;
|
||||||
|
result = CreateDynString(128,128);
|
||||||
|
if(result == NULL){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(node->child->value.dataType == HIPNONE)
|
||||||
|
DynStringCopy(result,"[");
|
||||||
|
else
|
||||||
|
DynStringCopy(result,"{");
|
||||||
|
|
||||||
|
current = node->child;
|
||||||
|
while(current != NULL){
|
||||||
|
DynStringConcat(result,"\"");
|
||||||
|
DynStringConcat(result,current->name);
|
||||||
|
DynStringConcat(result,"\"");
|
||||||
|
if(current->value.dataType != HIPNONE){
|
||||||
|
data = formatValue(current->value);
|
||||||
|
if(data != NULL){
|
||||||
|
DynStringConcat(result,": ");
|
||||||
|
DynStringConcat(result,GetCharArray(data));
|
||||||
|
DeleteDynString(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (current->next != NULL) DynStringConcat(result,", ");
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(node->child->value.dataType == HIPNONE)
|
||||||
|
DynStringConcat(result,"]");
|
||||||
|
else
|
||||||
|
DynStringConcat(result,"}");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
static pDynString formatListWithVal(pHdb node){
|
static pDynString formatListWithVal(pHdb node){
|
||||||
pHdb current;
|
pHdb current;
|
||||||
pDynString result = NULL;
|
pDynString result = NULL;
|
||||||
@ -1952,6 +2058,7 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|||||||
pHdb node = NULL;
|
pHdb node = NULL;
|
||||||
int pathArg = 1;
|
int pathArg = 1;
|
||||||
pDynString listData = NULL;
|
pDynString listData = NULL;
|
||||||
|
int protocol = 0, outCode;
|
||||||
|
|
||||||
if(argc < 2) {
|
if(argc < 2) {
|
||||||
SCWrite(pCon,"ERROR: need path to node to print",eError);
|
SCWrite(pCon,"ERROR: need path to node to print",eError);
|
||||||
@ -1979,14 +2086,24 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|||||||
} else if(strcmp(argv[1],"-cli") == 0){
|
} else if(strcmp(argv[1],"-cli") == 0){
|
||||||
listData = formatClientList(node);
|
listData = formatClientList(node);
|
||||||
} else {
|
} else {
|
||||||
|
if ((protocol = isJSON(pCon)) == 1) {
|
||||||
|
listData = formatJSONList(node);
|
||||||
|
outCode = eHdbEvent;
|
||||||
|
} else {
|
||||||
listData = formatPlainList(node);
|
listData = formatPlainList(node);
|
||||||
|
outCode = eEvent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(listData == NULL){
|
if(listData == NULL){
|
||||||
SCWrite(pCon,"ERROR: failed to format list",
|
SCWrite(pCon,"ERROR: failed to format list",
|
||||||
eError);
|
eError);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
SCWrite(pCon,GetCharArray(listData),eValue);
|
if( (strcmp(argv[1],"-val") == 0) || (strcmp(argv[1],"-cli") == 0) ){
|
||||||
|
SCWrite(pCon,GetCharArray(listData),eValue);
|
||||||
|
} else {
|
||||||
|
SCWrite(pCon,GetCharArray(listData),outCode);
|
||||||
|
}
|
||||||
DeleteDynString(listData);
|
DeleteDynString(listData);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user