From bf0f9e17e5770645c3656b1f1e421952467311b9 Mon Sep 17 00:00:00 2001 From: Ake Hedman Date: Fri, 15 May 2020 16:59:50 +0200 Subject: [PATCH] Fixed problem with driver measurement conversions --- src/vscp/common/controlobject.cpp | 2 +- src/vscp/common/vscphelper.cpp | 39 ++++++++++++++++--- .../test_measurement_coversions.cpp | 19 +++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 tests/vscphelper/test_measurement_coversions.cpp diff --git a/src/vscp/common/controlobject.cpp b/src/vscp/common/controlobject.cpp index fa3e5f3b9..34faff697 100644 --- a/src/vscp/common/controlobject.cpp +++ b/src/vscp/common/controlobject.cpp @@ -1358,7 +1358,7 @@ CControlObject::sendEvent(CClientItem* pClientItem, vscpEvent* peventToSend) syslog(LOG_DEBUG, "Level I event over Level II " "dest = %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:" - "%02X:%02X:%02X:%02X:%v:%02X:%02X:%02X:", + "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:", destguid.getAt(0), destguid.getAt(1), destguid.getAt(2), diff --git a/src/vscp/common/vscphelper.cpp b/src/vscp/common/vscphelper.cpp index 37b46d319..1104f0564 100644 --- a/src/vscp/common/vscphelper.cpp +++ b/src/vscp/common/vscphelper.cpp @@ -2285,7 +2285,6 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) if (vscp_getMeasurementAsDouble(&val64, pEvent)) { - pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_FLOAT; uint8_t* p = new uint8_t[12]; if (NULL != p) { @@ -2304,6 +2303,9 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREMENTX3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREMENTX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_FLOAT; + pEvent->sizeData = 4 + 8; + // Sensor index p[0] = pEvent->pdata[0] & VSCP_MASK_DATACODING_INDEX; @@ -2315,7 +2317,7 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) // Floating point value val64 = VSCP_UINT64_SWAP_ON_LE(val64); - memcpy(p + 4, &val64, sizeof(val64)); + memcpy(p + 4, (uint8_t *)&val64, sizeof(val64)); delete[] pEvent->pdata; @@ -2327,6 +2329,9 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREZONEX3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREZONEX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_FLOAT; + pEvent->sizeData = 4 + 8; + // Index = 0, Unit = 0, Zone = 0, Subzone = 0 // Floating point value val64 = VSCP_UINT64_SWAP_ON_LE(val64); @@ -2342,6 +2347,9 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREMENT32X3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREMENT32X4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_FLOAT; + pEvent->sizeData = 4 + 8; + // Index = 0, Unit = 0, Zone = 0, Subzone = 0 // Floating point value val64 = VSCP_UINT64_SWAP_ON_LE(val64); @@ -2357,6 +2365,9 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREZONEX3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREZONEX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_FLOAT; + pEvent->sizeData = 4 + 8; + // Sensor index p[0] = pEvent->pdata[0]; @@ -2379,6 +2390,9 @@ vscp_convertLevel1MeasuremenToLevel2Double(vscpEvent* pEvent) (VSCP_CLASS1_SETVALUEZONEX3 == pEvent->vscp_class) || (VSCP_CLASS1_SETVALUEZONEX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_FLOAT; + pEvent->sizeData = 4 + 8; + // Sensor index p[0] = pEvent->pdata[0]; @@ -2433,8 +2447,6 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) if (vscp_getMeasurementAsString(strval, pEvent)) { - pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_STR; - // Must be room for the number if ((4 + strval.length() + 1) > VSCP_MAX_DATA) { return false; @@ -2453,6 +2465,9 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREMENTX3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREMENTX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_STR; + pEvent->sizeData = 4 + strval.length(); + // Sensor index p[0] = pEvent->pdata[0] & VSCP_MASK_DATACODING_INDEX; @@ -2462,8 +2477,8 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) // unit p[3] = (pEvent->pdata[0] & VSCP_MASK_DATACODING_UNIT) >> 3; - // Copy in the value string - strcpy(p + 4, (const char*)strval.c_str()); + // Copy in the value string (without terminating zero) + memcpy(p + 4, (const char*)strval.c_str(), strval.length()); delete[] pEvent->pdata; // Delete old data @@ -2475,6 +2490,9 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREMENT64X3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREMENT64X4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_STR; + pEvent->sizeData = 4 + strval.length(); + // Index = 0, Unit = 0, Zone = 0, Subzone = 0 // Floating point value // Copy in the value string @@ -2490,6 +2508,9 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREMENT32X3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREMENT32X4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_STR; + pEvent->sizeData = 4 + strval.length(); + // Index = 0, Unit = 0, Zone = 0, Subzone = 0 // Floating point value // Copy in the value string @@ -2505,6 +2526,9 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) (VSCP_CLASS1_MEASUREZONEX3 == pEvent->vscp_class) || (VSCP_CLASS1_MEASUREZONEX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_STR; + pEvent->sizeData = 4 + strval.length(); + // Sensor index p[0] = pEvent->pdata[0]; @@ -2527,6 +2551,9 @@ vscp_convertLevel1MeasuremenToLevel2String(vscpEvent* pEvent) (VSCP_CLASS1_SETVALUEZONEX3 == pEvent->vscp_class) || (VSCP_CLASS1_SETVALUEZONEX4 == pEvent->vscp_class)) { + pEvent->vscp_class = VSCP_CLASS2_MEASUREMENT_STR; + pEvent->sizeData = 4 + strval.length(); + // Sensor index p[0] = pEvent->pdata[0]; diff --git a/tests/vscphelper/test_measurement_coversions.cpp b/tests/vscphelper/test_measurement_coversions.cpp new file mode 100644 index 000000000..282e0fa85 --- /dev/null +++ b/tests/vscphelper/test_measurement_coversions.cpp @@ -0,0 +1,19 @@ +vscpEvent e; +e.vscp_class = 10; +e.vscp_type = 6; +e.sizeData = 4; +e.pdata = new uint8_t[4]; +e.pdata[0] = 0x80; +e.pdata[1] = 0x02; +e.pdata[2] = 0x1B; +e.pdata[3] = 0x22; +//vscp_convertLevel1MeasuremenToLevel2String(&e); +vscp_convertLevel1MeasuremenToLevel2Double(&e); +double dv; +memcpy( (uint8_t *)&dv, e.pdata+4, 8 ); +fprintf(stderr,"Double: %g\n", dv); +dv = VSCP_UINT64_SWAP_ON_LE(dv); +fprintf(stderr,"Double: %g\n", dv); +for (int i; i<8;i++) { + fprintf(stderr,"%02X \n", e.pdata[i+4]); +} \ No newline at end of file