Merge branch 'master' of ssh://gitorious.psi.ch/sinqdev/sics

This commit is contained in:
2015-07-06 09:11:25 +02:00
3 changed files with 69 additions and 6 deletions

View File

@@ -266,8 +266,7 @@ int AsconReadChar(int fd, char *chr)
if (ret <= 0) if (ret <= 0)
return ret; return ret;
ret = recv(fd, chr, 1, 0); ret = recv(fd, chr, 1, 0);
/* PrintChar(*chr); */ /* PrintChar(*chr); fflush(stdout); */
fflush(stdout);
if (ret > 0) if (ret > 0)
return 1; return 1;
if (ret == 0) { if (ret == 0) {

View File

@@ -53,18 +53,19 @@
* modbus-crc: CRC-16-IBM, order: lo,hi * modbus-crc: CRC-16-IBM, order: lo,hi
* keller-crc: CRC-16-IBM, order: hi,lo * keller-crc: CRC-16-IBM, order: hi,lo
* sycon-crc: sort of mod 256 checksum, byte stuffing included (no float allowed) * sycon-crc: sort of mod 256 checksum, byte stuffing included (no float allowed)
* chksum-crc: simple 8bit checksum
*/ */
typedef enum {intType, hexType, floatType, typedef enum {intType, hexType, floatType,
skipType, codeType, crcType, dumpType, errorType} BinDataType; skipType, codeType, crcType, dumpType, errorType} BinDataType;
// dumpType, errorType must be the last items // dumpType, errorType must be the last items
typedef enum {modbusCrc, kellerCrc, rsportCrc, syconCrc} CrcAlgorithm; typedef enum {modbusCrc, kellerCrc, rsportCrc, syconCrc, chksumCrc} CrcAlgorithm;
typedef struct { typedef struct {
CrcAlgorithm crcAlgorithm; CrcAlgorithm crcAlgorithm;
pDynString inp; pDynString inp;
char *nextFmt; char *nextFmt; // position of next format token
pDynString result; pDynString result;
int expectedChars; int expectedChars;
BinDataType type; BinDataType type;
@@ -135,6 +136,23 @@ static int calc_crc8(char *inp, int inpLen)
} }
return crc; return crc;
} }
/*----------------------------------------------------------------------------*/
static int calc_chksum(char *inp, int inpLen)
{
/** simple 8bit checksum
*/
unsigned char crc = 0;
unsigned char data;
int n;
while (inpLen--) {
data = *(unsigned char *) inp;
crc += data;
inp++;
}
return crc;
}
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
int BinReadItem(Ascon *a) { int BinReadItem(Ascon *a) {
@@ -154,7 +172,11 @@ int BinReadItem(Ascon *a) {
if (strcasecmp(item, "crc") == 0) { if (strcasecmp(item, "crc") == 0) {
p->type = crcType; p->type = crcType;
p->expectedChars = 2; if (p->crcAlgorithm == chksumCrc) {
p->expectedChars = 1;
} else {
p->expectedChars = 2;
}
p->nextFmt += valen; p->nextFmt += valen;
return 1; return 1;
} else if (strncasecmp(item, "int", 3) == 0) { } else if (strncasecmp(item, "int", 3) == 0) {
@@ -281,6 +303,10 @@ int BinHandler(Ascon *a) {
DynStringConcatChar(dyn, crc % 256); DynStringConcatChar(dyn, crc % 256);
DynStringConcatChar(dyn, crc / 256); DynStringConcatChar(dyn, crc / 256);
break; break;
case chksumCrc:
crc = calc_chksum(GetCharArray(dyn), l);
DynStringConcatChar(dyn, crc % 256);
break;
case rsportCrc: case rsportCrc:
crc = calc_crc8(GetCharArray(dyn), l); crc = calc_crc8(GetCharArray(dyn), l);
DynStringConcatChar(dyn, crc); DynStringConcatChar(dyn, crc);
@@ -509,6 +535,11 @@ int BinHandler(Ascon *a) {
DynStringConcat(p->result, "badCRC "); DynStringConcat(p->result, "badCRC ");
} }
break; break;
case chksumCrc:
if (calc_chksum(str, l-1) != (unsigned char)str[l-1]) {
DynStringConcat(p->result, "badCRC ");
}
break;
case kellerCrc: case kellerCrc:
i = str[l-2]; i = str[l-2];
str[l-2] = str[l-1]; str[l-2] = str[l-1];
@@ -560,6 +591,8 @@ static int BinInit(Ascon * a, SConnection * con, int argc, char *argv[])
p->crcAlgorithm = syconCrc; p->crcAlgorithm = syconCrc;
} else if (strcasecmp(argv[2], "rsport-crc") == 0) { } else if (strcasecmp(argv[2], "rsport-crc") == 0) {
p->crcAlgorithm = rsportCrc; p->crcAlgorithm = rsportCrc;
} else if (strcasecmp(argv[2], "chksum-crc") == 0) {
p->crcAlgorithm = chksumCrc;
} else if (strcasecmp(argv[2], "modbus-crc") != 0) { } else if (strcasecmp(argv[2], "modbus-crc") != 0) {
SCPrintf(con, eError, "ERROR: unknown crc-algorithm %s", argv[2]); SCPrintf(con, eError, "ERROR: unknown crc-algorithm %s", argv[2]);
a->private = NULL; a->private = NULL;

View File

@@ -2850,6 +2850,37 @@ static int GetHdbVal(SConnection * pCon, SicsInterp * pSics, void *pData,
return 1; return 1;
} }
/*---------------------------------------------------------------------------*/
static int GetHdbValIgnoreError(
SConnection * pCon, SicsInterp * pSics, void *pData, int argc, char *argv[])
{
pHdb targetNode = NULL;
pDynString parData = NULL;
int protocol, outCode;
if (argc != 2) {
SCWrite(pCon, "ERROR: syntax must be: hvali <path>", eError);
return 0;
}
targetNode = FindHdbNode(NULL, argv[1], pCon);
if (targetNode == NULL) {
return 0;
}
parData = formatValue(targetNode->value, targetNode);
if (parData == NULL) {
SCWrite(pCon, "ERROR: out of memory formatting data", eError);
return 0;
}
if ((protocol = isJSON(pCon, 0)) == 1)
outCode = eHdbEvent;
else
outCode = eValue;
SCWrite(pCon, GetCharArray(parData), outCode);
DeleteDynString(parData);
return 1;
}
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
static int countChildren(pHdb node) static int countChildren(pHdb node)
{ {
@@ -4269,6 +4300,7 @@ int InstallSICSHipadaba(SConnection * pCon, SicsInterp * pSics,
AddCommand(pSics, "hupdate", UpdateHdbNode, NULL, NULL); AddCommand(pSics, "hupdate", UpdateHdbNode, NULL, NULL);
AddCommand(pSics, "hget", GetHdbNode, NULL, NULL); AddCommand(pSics, "hget", GetHdbNode, NULL, NULL);
AddCommand(pSics, "hval", GetHdbVal, NULL, NULL); AddCommand(pSics, "hval", GetHdbVal, NULL, NULL);
AddCommand(pSics, "hvali", GetHdbValIgnoreError, NULL, NULL);
AddCommand(pSics, "hzipget", ZipGetHdbNode, NULL, NULL); AddCommand(pSics, "hzipget", ZipGetHdbNode, NULL, NULL);
AddCommand(pSics, "hzipread", ZipReadHdbNode, NULL, NULL); AddCommand(pSics, "hzipread", ZipReadHdbNode, NULL, NULL);
AddCommand(pSics, "hbinread", BinReadHdbNode, NULL, NULL); AddCommand(pSics, "hbinread", BinReadHdbNode, NULL, NULL);
@@ -4277,7 +4309,6 @@ int InstallSICSHipadaba(SConnection * pCon, SicsInterp * pSics,
AddCommand(pSics, "hdelcb", RemoveHdbCallback, NULL, NULL); AddCommand(pSics, "hdelcb", RemoveHdbCallback, NULL, NULL);
AddCommand(pSics, "hlink", LinkHdbNode, NULL, NULL); AddCommand(pSics, "hlink", LinkHdbNode, NULL, NULL);
AddCommand(pSics, "hinfo", HdbNodeInfo, NULL, NULL); AddCommand(pSics, "hinfo", HdbNodeInfo, NULL, NULL);
/* AddCommand(pSics, "hval", HdbNodeVal, NULL, NULL);*/
AddCommand(pSics, "hchain", ChainHdbNode, NULL, NULL); AddCommand(pSics, "hchain", ChainHdbNode, NULL, NULL);
AddCommand(pSics, "hcommand", SicsCommandNode, NULL, NULL); AddCommand(pSics, "hcommand", SicsCommandNode, NULL, NULL);
AddCommand(pSics, "harray", HdbArrayNode, NULL, NULL); AddCommand(pSics, "harray", HdbArrayNode, NULL, NULL);