Skip to content

Commit

Permalink
Fixed problem with driver measurement conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
grodansparadis committed May 15, 2020
1 parent 1e71cde commit bf0f9e1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/vscp/common/controlobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 33 additions & 6 deletions src/vscp/common/vscphelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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];

Expand All @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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];

Expand All @@ -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];

Expand Down
19 changes: 19 additions & 0 deletions tests/vscphelper/test_measurement_coversions.cpp
Original file line number Diff line number Diff line change
@@ -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]);
}

0 comments on commit bf0f9e1

Please sign in to comment.