diff --git a/cnvrt.c b/cnvrt.c new file mode 100644 index 00000000..0a1f040c --- /dev/null +++ b/cnvrt.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include "sics.h" + +/* + * conversions: + * hex2float + * float2hex + * + * Markus Zolliker Aug 2010 + */ + +/*-------------------------------------------------------------------------*/ +void double2ieee(double input, char ieee[4]) +{ + +/* convert double to IEEE 32 bit floating number (denormalized numbers are considered as zero) */ + + long mantissa; + int exponent; + + if (input == 0) { + ieee[0] = 0; + ieee[1] = 0; + ieee[2] = 0; + ieee[3] = 0; + } else { + mantissa = 0x1000000 * (frexp(fabs(input), &exponent)); + exponent = exponent - 1 + 127; + if (exponent < 0) { + exponent = 0; + } else if (exponent > 0xFE) { + exponent = 0xFE; + } + if (input < 0) { + ieee[0] = 0x80 | (exponent >> 1); + } else { + ieee[0] = exponent >> 1; + } + ieee[1] = (exponent & 1) << 7 | ((mantissa & 0x7F0000) >> 16); + ieee[2] = (mantissa & 0xFF00) >> 8; + ieee[3] = mantissa & 0xFF; + } + return; +} + +/*-------------------------------------------------------------------------*/ +double ieee2double(char ieee[4]) +{ + +/* IEEE 32 bit floating number to double (denormalized numbers are considered as zero) */ + + long mantissa; + double output; + int exponent; + + mantissa = ((ieee[1] << 16) & 0x7FFFFF) + | ((ieee[2] << 8) & 0xFF00) + | ((ieee[3]) & 0xFF); + + exponent = (ieee[0] & 0x7F) * 2 + ((ieee[1] >> 7) & 1); /* raw exponent */ + if (exponent == 0 && mantissa == 0) { + return 0.0; + } + output = ldexp(mantissa, -23) + 1.0; + if (ieee[0] & 0x80) { + output = -output; + } + return output * ldexp(1, exponent - 127); +} + +/*-------------------------------------------------------------------------*/ +int CnvrtAction(SConnection *con, SicsInterp *sics, void *data, + int argc, char *argv[]) { + double f; + char ieee[4]; + char result[32]; + + if (argc != 3) goto Usage; + if (strcasecmp(argv[1], "float2xieee") == 0) { + double2ieee(atof(argv[2]), ieee); + SCPrintf(con, eValue, "%2.2x%2.2x%2.2x%2.2x", + ieee[0] & 0xFF, ieee[1] & 0xFF, ieee[2] & 0xFF, ieee[3] & 0xFF); + return 1; + } else if (strcasecmp(argv[1], "xieee2float") == 0) { + ieee[0]=0; + ieee[1]=0; + ieee[2]=0; + ieee[3]=0; + sscanf(argv[2], "%2hhx%2hhx%2hhx%2hhx", + ieee, ieee+1, ieee+2, ieee+3); + snprintf(result, sizeof result, "%.7g", ieee2double(ieee)); + if (strchr(result, '.') == NULL && strchr(result, 'e') == NULL) { + SCPrintf(con, eValue, "%s.", result); + } else { + SCWrite(con, result, eValue); + } + return 1; + } + Usage: + SCPrintf(con, eError, "ERROR: Usage: cnvrt float2xieee "); + SCPrintf(con, eError, " cnvrt xieee2float "); + return 0; +} diff --git a/cnvrt.h b/cnvrt.h new file mode 100644 index 00000000..e1f6df27 --- /dev/null +++ b/cnvrt.h @@ -0,0 +1,6 @@ +/* convert double to IEEE 32 bit floating number and vice versa + (denormalized numbers are considered as zero) */ + +void double2ieee(double input, char ieee[4]); +double ieee2double(char ieee[4]); + diff --git a/make_gen b/make_gen index e7191dac..172a3a7f 100644 --- a/make_gen +++ b/make_gen @@ -46,7 +46,8 @@ SOBJ = network.o ifile.o conman.o SCinter.o splitter.o passwd.o \ rwpuffer.o asynnet.o background.o countersec.o hdbtable.o velosec.o \ histmemsec.o sansbc.o sicsutil.o strlutil.o genbinprot.o trace.o\ singlebinb.o taskobj.o sctcomtask.o tasmono.o multicountersec.o \ - messagepipe.o sicsget.o remoteobject.o + messagepipe.o sicsget.o remoteobject.o pmacprot.o charbychar.o binprot.o \ + cnvrt.o MOTOROBJ = motor.o simdriv.o COUNTEROBJ = countdriv.o simcter.o counter.o diff --git a/protocol.c b/protocol.c index 73ddbc71..9891913d 100644 --- a/protocol.c +++ b/protocol.c @@ -21,7 +21,7 @@ #define MAXMSG 1024 #define INIT_STR_SIZE 256 #define STR_RESIZE_LENGTH 256 -#define NUMPROS 7 +#define NUMPROS 6 #define PROLISTLEN 8 typedef struct __Protocol { pObjectDescriptor pDes; /* required as first field */ @@ -97,7 +97,6 @@ pProtocol CreateProtocol(void) char *pPros[] = { "default", "normal", "withcode", - "sycamore", "json", "act", "all", @@ -282,19 +281,15 @@ static int ProtocolSet(SConnection * pCon, Protocol * pPro, char *pProName) SCSetWriteFunc(pCon, SCWriteWithOutcode); break; - case 3: /* sycamore */ - SCSetWriteFunc(pMaster, SCWriteSycamore); - SCSetWriteFunc(pCon, SCWriteSycamore); - break; - case 4: /* json */ + case 3: /* json */ SCSetWriteFunc(pCon, SCWriteJSON_String); SCSetWriteFunc(pMaster, SCWriteJSON_String); break; - case 5: /* ACT */ + case 4: /* ACT */ SCSetWriteFunc(pMaster, SCACTWrite); SCSetWriteFunc(pCon, SCACTWrite); break; - case 6: + case 5: SCSetWriteFunc(pMaster, SCAllWrite); SCSetWriteFunc(pCon, SCAllWrite); break; @@ -340,9 +335,8 @@ int ProtocolGet(SConnection * pCon, void *pData, char *pProName, int len) case 0: /* default = psi_sics */ case 1: /* normal (connection start default) */ case 2: /* outcodes */ - case 3: /* sycamore */ - case 4: /* json */ - case 5: /* act */ + case 3: /* json */ + case 4: /* act */ pProName = pPro->pProList[Index]; return 1; break; @@ -451,133 +445,7 @@ static int InitDefaultProtocol(SConnection * pCon, Protocol * pPro) } return pPro->isDefaultSet; } - -/*---------------------------------------------------------------------*/ -void sycformat(char *tag, OutCode msgFlag, pDynString msgString, - pDynString msgOut) -{ - DynStringConcat(msgOut, " "); - switch (msgFlag) { - eEvent: - break; - eFinish: - break; - default: - DynStringConcat(msgOut, tag); - DynStringConcat(msgOut, "={"); - DynStringConcat(msgOut, GetCharArray(msgString)); - DynStringConcat(msgOut, "}"); - break; - } -} -int SCWriteSycamore(SConnection * pCon, char *pBuffer, int iOut) -{ - int iRet; - char pBueffel[MAXMSG]; - long taskID = 0; -/* char pPrefix[40];*/ - pDynString pMsg = NULL; - pDynString pMsgString = NULL; - commandContext comCon; - - - if (strlen(pBuffer) == 0) { - return 0; - } - - if (!SCVerifyConnection(pCon)) { - return 0; - } - - /* log it for any case */ - if (pCon->pSock) { - iRet = pCon->pSock->sockid; - } else { - iRet = 0; - } - snprintf(pBueffel,sizeof(pBueffel)-1, "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) { - WriteToCommandLogId(NULL, iRet, pBuffer); - } - - /* put it into the interpreter if present */ - if (SCinMacro(pCon)) { - InterpWrite(pServ->pSics, pBuffer); - /* print it to client if error message */ - /* FIXME should report errors via sycamore - if((iOut== eError) || (iOut == eWarning) ) - iRet = SCDoSockWrite(pCon,GetCharArray(pMsgOut)); */ - } else { /* not in interpreter, normal logic */ - - comCon = SCGetContext(pCon); - - /* Return 0 without dying if no message data */ - if (pBuffer == NULL) { - return 0; - } - - taskID = comCon.transID; - - pMsg = CreateDynString(INIT_STR_SIZE, STR_RESIZE_LENGTH); - pMsgString = CreateDynString(INIT_STR_SIZE, STR_RESIZE_LENGTH); - pBueffel[0] = '\0'; - - snprintf(pBueffel,sizeof(pBueffel)-1, "[con%4.4d:", (int) pCon->ident); /* field 1: connID */ - DynStringConcat(pMsg, pBueffel); - snprintf(pBueffel,sizeof(pBueffel)-1, "t%6.6d:", (int) taskID); /* field 2: taskID */ - DynStringConcat(pMsg, pBueffel); - /* deviceID */ - DynStringConcat(pMsg, comCon.deviceID); - DynStringConcatChar(pMsg, ':'); - - /* msgFlag */ - switch (iOut) { - case 5: /* eValue */ - DynStringConcat(pMsg, "out"); - break; - default: - DynStringConcat(pMsg, pCode[iOut]); - break; - } - DynStringConcatChar(pMsg, ']'); - if (iOut == eStart) { - DynStringConcat(pMsgString, comCon.deviceID); - } - - if (iOut == eEvent) { - DynStringConcat(pMsgString, " type="); - /* 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, pStatus[pCon->conStatus]);*/ - DynStringConcat(pMsgString, ","); - } - DynStringConcat(pMsgString, " "); - DynStringConcat(pMsgString, pBuffer); - sycformat(comCon.deviceID, iOut, pMsgString, pMsg); - - /* is this really to be printed ? */ - if (iOut < pCon->iOutput) { - if (pMsg != NULL) - DeleteDynString(pMsg); - return 0; - } - /* first the socket */ - /*strcat(pMsg, pBueffel); */ - iRet = SCDoSockWrite(pCon, GetCharArray(pMsg)); - } - if (pMsg != NULL) - DeleteDynString(pMsg); - 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 @@ -763,8 +631,7 @@ char *GetProtocolName(SConnection * pCon) case 0: /* default = psi_sics */ case 1: /* normal (connection start default) */ case 2: /* outcodes */ - case 3: /* sycamore */ - case 4: /* json */ + case 3: /* json */ return strdup(pPro->pProList[pCon->iProtocolID]); break; default: @@ -790,13 +657,10 @@ writeFunc GetProtocolWriteFunc(SConnection * pCon) case 2: /* outcodes */ return SCWriteWithOutcode; break; - case 3: /* sycamore */ - return SCWriteSycamore; - break; - case 4: /* json */ + case 3: /* json */ return SCWriteJSON_String; break; - case 5: + case 4: return SCACTWrite; break; default: diff --git a/protocol.h b/protocol.h index 53537abc..4151b4f7 100644 --- a/protocol.h +++ b/protocol.h @@ -24,9 +24,6 @@ void MakeProtocol(void); /*--------------------- operations --------------------------------------*/ int ProtocolAction(SConnection * pCon, SicsInterp * pSics, void *pData, int argc, char *argv[]); -/*--------------------- implement protocol sycamore ---------------------*/ -int SCWriteSycamore(SConnection * pCon, char *pBuffer, int iOut); - /*--------------------- implement protocol API -----------------------*/ char *GetProtocolName(SConnection * pCon); int GetProtocolID(SConnection * pCon);