Skip to content

Commit 7d303d3

Browse files
author
Frank Zosso
committed
Fix OneWire current conversion and harden protocol parsing
1 parent c37161d commit 7d303d3

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/helpers/sensors/OneWireSensorHub.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ void OneWireSensorHub::handleEvent(uint8_t pid, uint8_t sid, SNHUBAPI_EVT_E eid,
104104
uint8_t* msg, uint16_t len) {
105105
switch (eid) {
106106
case SNHUBAPI_EVT_QSEND:
107-
oneWireSerial.write(msg, len);
107+
if (oneWireSerial.write(msg, len) != len) {
108+
MESH_DEBUG_PRINTLN("OneWire: write failed");
109+
}
108110
break;
109111

110112
case SNHUBAPI_EVT_ADD_PID:
@@ -116,10 +118,12 @@ void OneWireSensorHub::handleEvent(uint8_t pid, uint8_t sid, SNHUBAPI_EVT_E eid,
116118
break;
117119

118120
case SNHUBAPI_EVT_SDATA_REQ: {
121+
if (len < 2) break;
119122
uint8_t ipso_type = msg[0];
120123
uint16_t val_len = len - 1;
121124

122125
uint8_t ordered[256];
126+
if (val_len > 256) val_len = 256;
123127
for (uint16_t i = 0; i < val_len; i += 2) {
124128
if (i + 1 < val_len) {
125129
ordered[i] = msg[1 + i + 1];
@@ -134,6 +138,7 @@ void OneWireSensorHub::handleEvent(uint8_t pid, uint8_t sid, SNHUBAPI_EVT_E eid,
134138
}
135139

136140
case SNHUBAPI_EVT_REPORT: {
141+
if (len < 2) break;
137142
uint8_t ipso_type = msg[0];
138143
uint16_t val_len = len - 1;
139144
MESH_DEBUG_PRINTLN("OneWire: REPORT SID=0x%02X IPSO=%d len=%d", sid, ipso_type, val_len);
@@ -185,10 +190,10 @@ void OneWireSensorHub::parseSensorData(uint8_t sid, uint8_t ipso_type, uint8_t*
185190
break;
186191
}
187192

188-
case RAK_IPSO_DC_CURRENT: { // 185 (3385-3200): DC current, 2 bytes, mA
193+
case RAK_IPSO_DC_CURRENT: { // 185 (3385-3200): DC current, 2 bytes, raw * 0.01A
189194
if (data_len >= 2) {
190195
int16_t raw = ((int16_t)data[0] << 8) | (int16_t)data[1];
191-
_cached_current_ma = raw;
196+
_cached_current_ma = raw * 10; // convert raw (centiamps) to mA
192197
_has_current = true;
193198
MESH_DEBUG_PRINTLN("OneWire: Battery Current = %dmA (IPSO %d, raw=%d)", _cached_current_ma, ipso_type, raw);
194199
}
@@ -251,7 +256,7 @@ void OneWireSensorHub::parseSensorData(uint8_t sid, uint8_t ipso_type, uint8_t*
251256
bool OneWireSensorHub::hasVoltage() const { return _has_voltage; }
252257
float OneWireSensorHub::getVoltage() const { return _cached_voltage; }
253258
bool OneWireSensorHub::hasCurrent() const { return _has_current; }
254-
float OneWireSensorHub::getCurrent() const { return (float)_cached_current_ma / 100.0f; }
259+
float OneWireSensorHub::getCurrent() const { return (float)_cached_current_ma / 1000.0f; }
255260
bool OneWireSensorHub::hasBatteryPercent() const { return _has_battery_pct; }
256261
uint8_t OneWireSensorHub::getBatteryPercent() const { return _cached_battery_pct; }
257262
bool OneWireSensorHub::hasTemperature() const { return _has_temperature; }

0 commit comments

Comments
 (0)