mirror of
https://github.com/esphome/esphome.git
synced 2024-12-12 14:57:34 +01:00
fix modbus crashing when bad data returned (#7810)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
80fedbc1a5
commit
e9851e7eb2
@ -38,8 +38,9 @@ void Modbus::loop() {
|
|||||||
|
|
||||||
// stop blocking new send commands after sent_wait_time_ ms after response received
|
// stop blocking new send commands after sent_wait_time_ ms after response received
|
||||||
if (now - this->last_send_ > send_wait_time_) {
|
if (now - this->last_send_ > send_wait_time_) {
|
||||||
if (waiting_for_response > 0)
|
if (waiting_for_response > 0) {
|
||||||
ESP_LOGV(TAG, "Stop waiting for response from %d", waiting_for_response);
|
ESP_LOGV(TAG, "Stop waiting for response from %d", waiting_for_response);
|
||||||
|
}
|
||||||
waiting_for_response = 0;
|
waiting_for_response = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -622,51 +622,87 @@ int64_t payload_to_number(const std::vector<uint8_t> &data, SensorValueType sens
|
|||||||
uint32_t bitmask) {
|
uint32_t bitmask) {
|
||||||
int64_t value = 0; // int64_t because it can hold signed and unsigned 32 bits
|
int64_t value = 0; // int64_t because it can hold signed and unsigned 32 bits
|
||||||
|
|
||||||
|
size_t size = data.size() - offset;
|
||||||
|
bool error = false;
|
||||||
switch (sensor_value_type) {
|
switch (sensor_value_type) {
|
||||||
case SensorValueType::U_WORD:
|
case SensorValueType::U_WORD:
|
||||||
|
if (size >= 2) {
|
||||||
value = mask_and_shift_by_rightbit(get_data<uint16_t>(data, offset), bitmask); // default is 0xFFFF ;
|
value = mask_and_shift_by_rightbit(get_data<uint16_t>(data, offset), bitmask); // default is 0xFFFF ;
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SensorValueType::U_DWORD:
|
case SensorValueType::U_DWORD:
|
||||||
case SensorValueType::FP32:
|
case SensorValueType::FP32:
|
||||||
|
if (size >= 4) {
|
||||||
value = get_data<uint32_t>(data, offset);
|
value = get_data<uint32_t>(data, offset);
|
||||||
value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
|
value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SensorValueType::U_DWORD_R:
|
case SensorValueType::U_DWORD_R:
|
||||||
case SensorValueType::FP32_R:
|
case SensorValueType::FP32_R:
|
||||||
|
if (size >= 4) {
|
||||||
value = get_data<uint32_t>(data, offset);
|
value = get_data<uint32_t>(data, offset);
|
||||||
value = static_cast<uint32_t>(value & 0xFFFF) << 16 | (value & 0xFFFF0000) >> 16;
|
value = static_cast<uint32_t>(value & 0xFFFF) << 16 | (value & 0xFFFF0000) >> 16;
|
||||||
value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
|
value = mask_and_shift_by_rightbit((uint32_t) value, bitmask);
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SensorValueType::S_WORD:
|
case SensorValueType::S_WORD:
|
||||||
|
if (size >= 2) {
|
||||||
value = mask_and_shift_by_rightbit(get_data<int16_t>(data, offset),
|
value = mask_and_shift_by_rightbit(get_data<int16_t>(data, offset),
|
||||||
bitmask); // default is 0xFFFF ;
|
bitmask); // default is 0xFFFF ;
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SensorValueType::S_DWORD:
|
case SensorValueType::S_DWORD:
|
||||||
|
if (size >= 4) {
|
||||||
value = mask_and_shift_by_rightbit(get_data<int32_t>(data, offset), bitmask);
|
value = mask_and_shift_by_rightbit(get_data<int32_t>(data, offset), bitmask);
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SensorValueType::S_DWORD_R: {
|
case SensorValueType::S_DWORD_R: {
|
||||||
|
if (size >= 4) {
|
||||||
value = get_data<uint32_t>(data, offset);
|
value = get_data<uint32_t>(data, offset);
|
||||||
// Currently the high word is at the low position
|
// Currently the high word is at the low position
|
||||||
// the sign bit is therefore at low before the switch
|
// the sign bit is therefore at low before the switch
|
||||||
uint32_t sign_bit = (value & 0x8000) << 16;
|
uint32_t sign_bit = (value & 0x8000) << 16;
|
||||||
value = mask_and_shift_by_rightbit(
|
value = mask_and_shift_by_rightbit(
|
||||||
static_cast<int32_t>(((value & 0x7FFF) << 16 | (value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
|
static_cast<int32_t>(((value & 0x7FFF) << 16 | (value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
} break;
|
} break;
|
||||||
case SensorValueType::U_QWORD:
|
case SensorValueType::U_QWORD:
|
||||||
case SensorValueType::S_QWORD:
|
case SensorValueType::S_QWORD:
|
||||||
// Ignore bitmask for QWORD
|
// Ignore bitmask for QWORD
|
||||||
|
if (size >= 8) {
|
||||||
value = get_data<uint64_t>(data, offset);
|
value = get_data<uint64_t>(data, offset);
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SensorValueType::U_QWORD_R:
|
case SensorValueType::U_QWORD_R:
|
||||||
case SensorValueType::S_QWORD_R: {
|
case SensorValueType::S_QWORD_R: {
|
||||||
// Ignore bitmask for QWORD
|
// Ignore bitmask for QWORD
|
||||||
|
if (size >= 8) {
|
||||||
uint64_t tmp = get_data<uint64_t>(data, offset);
|
uint64_t tmp = get_data<uint64_t>(data, offset);
|
||||||
value = (tmp << 48) | (tmp >> 48) | ((tmp & 0xFFFF0000) << 16) | ((tmp >> 16) & 0xFFFF0000);
|
value = (tmp << 48) | (tmp >> 48) | ((tmp & 0xFFFF0000) << 16) | ((tmp >> 16) & 0xFFFF0000);
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
} break;
|
} break;
|
||||||
case SensorValueType::RAW:
|
case SensorValueType::RAW:
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (error)
|
||||||
|
ESP_LOGE(TAG, "not enough data for value");
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user