15static bool quantity_in_range(uint16_t start_address, uint16_t quantity, uint16_t max_quantity) {
16 return quantity != 0 && quantity <= max_quantity &&
address_range_fits(start_address, quantity);
21static bool is_canonical_coil_value(uint8_t high_byte, uint8_t low_byte) {
22 return (high_byte == 0xFF || high_byte == 0x00) && low_byte == 0x00;
26 if (
size < MIN_PDU_SIZE)
35 return 2 + (
size > 1 ? std::min(frame[1], uint8_t(
packed_bit_bytes(MAX_NUM_OF_COILS_TO_READ))) : 0);
39 return 2 + (
size > 1 ? std::min(frame[1], uint8_t(MAX_NUM_OF_REGISTERS_TO_READ * 2)) : 0);
49 return 2 + (
size > 1 ? std::min(frame[1], uint8_t(MAX_PDU_SIZE - 2)) : 0);
54 return 2 + (
size > 1 ? std::min(frame[1], uint8_t(MAX_NUM_OF_REGISTERS_TO_READ * 2)) : 0);
64 if (
size < MIN_PDU_SIZE)
80 return 6 + (
size > 5 ? std::min(frame[5], uint8_t(
packed_bit_bytes(MAX_NUM_OF_COILS_TO_WRITE))) : 0);
83 return 6 + (
size > 5 ? std::min(frame[5], uint8_t(MAX_NUM_OF_REGISTERS_TO_WRITE * 2)) : 0);
88 return 2 + (
size > 1 ? std::min(frame[1], uint8_t(MAX_PDU_SIZE - 2)) : 0);
94 return 10 + (
size > 9 ? std::min(frame[9], uint8_t(MAX_NUM_OF_REGISTERS_TO_WRITE_RW * 2)) : 0);
107 const auto function_code =
static_cast<FunctionCode>(pdu[0]);
108 switch (function_code) {
112 return pdu[1] != 0 && pdu[1] <= uint8_t(
packed_bit_bytes(MAX_NUM_OF_COILS_TO_READ));
116 return pdu[1] != 0 && pdu[1] % 2 == 0 && pdu[1] <= uint8_t(MAX_NUM_OF_REGISTERS_TO_READ * 2);
119 return pdu[1] <= uint8_t(MAX_PDU_SIZE - 2);
121 return pdu[1] != 0 && pdu[1] % 2 == 0 && pdu[1] <= uint8_t(MAX_NUM_OF_REGISTERS_TO_READ * 2);
128 const uint16_t max_quantity = bits ? MAX_NUM_OF_COILS_TO_WRITE : MAX_NUM_OF_REGISTERS_TO_WRITE;
129 return quantity_in_range(start_address, quantity, max_quantity);
133 return is_canonical_coil_value(pdu[3], pdu[4]);
143 const auto function_code =
static_cast<FunctionCode>(pdu[0]);
144 switch (function_code) {
153 const uint16_t max_quantity = bits ? MAX_NUM_OF_COILS_TO_READ : MAX_NUM_OF_REGISTERS_TO_READ;
154 return quantity_in_range(start_address, quantity, max_quantity);
161 const uint16_t max_quantity = bits ? MAX_NUM_OF_COILS_TO_WRITE : MAX_NUM_OF_REGISTERS_TO_WRITE;
163 const size_t expected_data_bytes = bits ?
packed_bit_bytes(quantity) : quantity * 2;
164 return quantity_in_range(start_address, quantity, max_quantity) && pdu[5] == expected_data_bytes;
168 return pdu[1] <= MAX_PDU_SIZE - 2;
174 return quantity_in_range(start_address_read, quantity_read, MAX_NUM_OF_REGISTERS_TO_READ) &&
175 quantity_in_range(start_address_write, quantity_write, MAX_NUM_OF_REGISTERS_TO_WRITE_RW) &&
176 pdu[9] == quantity_write * 2;
180 return is_canonical_coil_value(pdu[3], pdu[4]);
186static size_t required_payload_size(
SensorValueType sensor_value_type) {
187 switch (sensor_value_type) {
212 ESP_LOGE(TAG,
"Invalid data type for modbus number to payload conversion: %d",
static_cast<uint16_t
>(value_type));
221 if (
static_cast<size_t>(offset) >
size) {
222 ESP_LOGE(TAG,
"not enough data for value type=%u offset=%u size=%zu",
static_cast<unsigned int>(sensor_value_type),
223 static_cast<unsigned int>(offset),
size);
227 const size_t required_size = required_payload_size(sensor_value_type);
228 if (required_size == 0) {
232 if (
size - offset < required_size) {
233 ESP_LOGE(TAG,
"not enough data for value type=%u offset=%u size=%zu required=%zu",
234 static_cast<unsigned int>(sensor_value_type),
static_cast<unsigned int>(offset),
size, required_size);
238 switch (sensor_value_type) {
275 static_cast<int32_t
>(((
value & 0x7FFF) << 16 | (
value & 0xFFFF0000) >> 16) | sign_bit), bitmask);
286 value = (tmp << 48) | (tmp >> 48) | ((tmp & 0xFFFF0000) << 16) | ((tmp >> 16) & 0xFFFF0000);
302 if (required_words >
count) {
303 ESP_LOGE(TAG,
"not enough registers for value type=%u count=%zu required=%u",
304 static_cast<unsigned int>(sensor_value_type),
count,
static_cast<unsigned int>(required_words));
311 switch (sensor_value_type) {
355 pdu.
push_back(
static_cast<uint8_t
>(function_code));
356 append_pdu_word(pdu, first);
357 append_pdu_word(pdu,
second);
364static void mask_trailing_pad_bits(std::span<uint8_t>
data, uint16_t bit_count) {
365 if (
data.empty() || bit_count % 8 == 0)
367 data.back() &=
static_cast<uint8_t
>((1 << (bit_count % 8)) - 1);
372 if (number_of_entities == 0) {
373 ESP_LOGE(TAG,
"Number of entities is zero for function code %02X",
static_cast<uint8_t
>(function_code));
377 ESP_LOGE(TAG,
"Read of %u entities at %u runs past the 16-bit address space, dropping request", number_of_entities,
382 switch (function_code) {
384 if (number_of_entities > MAX_NUM_OF_COILS_TO_READ) {
385 ESP_LOGE(TAG,
"number_of_entities %u exceeds maximum coils to read %u for function code %02X",
386 number_of_entities, MAX_NUM_OF_COILS_TO_READ,
static_cast<uint8_t
>(function_code));
391 if (number_of_entities > MAX_NUM_OF_DISCRETE_INPUTS_TO_READ) {
392 ESP_LOGE(TAG,
"number_of_entities %u exceeds maximum discrete inputs to read %u for function code %02X",
393 number_of_entities, MAX_NUM_OF_DISCRETE_INPUTS_TO_READ,
static_cast<uint8_t
>(function_code));
399 if (number_of_entities > MAX_NUM_OF_REGISTERS_TO_READ) {
400 ESP_LOGE(TAG,
"number_of_entities %u exceeds maximum registers to read %u for function code %02X",
401 number_of_entities, MAX_NUM_OF_REGISTERS_TO_READ,
static_cast<uint8_t
>(function_code));
406 ESP_LOGE(TAG,
"Unsupported function code %02X for read PDU creation",
static_cast<uint8_t
>(function_code));
410 append_pdu_header(pdu, function_code, start_address, number_of_entities);
415 const uint8_t *values,
size_t values_len) {
418 if (values !=
nullptr || values_len > 0) {
419 ESP_LOGW(TAG,
"Values provided for read function code %02X, but will be ignored",
420 static_cast<uint8_t
>(function_code));
422 auto read_pdu =
create_read_pdu(function_code, start_address, number_of_entities);
423 pdu.
assign(read_pdu.begin(), read_pdu.end());
428 const bool is_single =
430 const bool is_multi =
432 if (!is_single && !is_multi) {
433 ESP_LOGE(TAG,
"Unsupported function code %02X for client PDU creation",
static_cast<uint8_t
>(function_code));
438 if (values ==
nullptr || values_len == 0) {
439 ESP_LOGE(TAG,
"No values provided for write function code %02X",
static_cast<uint8_t
>(function_code));
442 if (number_of_entities == 0) {
443 ESP_LOGE(TAG,
"Number of entities is zero for function code %02X",
static_cast<uint8_t
>(function_code));
449 const uint16_t max_entities =
451 if (!is_single && number_of_entities > max_entities) {
452 ESP_LOGE(TAG,
"number_of_entities %u exceeds maximum %u for function code %02X", number_of_entities, max_entities,
453 static_cast<uint8_t
>(function_code));
457 ESP_LOGE(TAG,
"Write of %u entities at %u runs past the 16-bit address space, dropping request", number_of_entities,
464 if (values_len < 2) {
465 ESP_LOGE(TAG,
"values_len %zu too small for write-single command (need 2), dropping request", values_len);
471 ESP_LOGE(TAG,
"Invalid single-coil value %02X%02X (must be FF00 or 0000), dropping request", values[0],
475 append_pdu_header(pdu, function_code, start_address,
encode_uint16(values[0], values[1]));
482 const size_t expected_len = bits ?
packed_bit_bytes(number_of_entities) :
static_cast<size_t>(number_of_entities) * 2;
483 if (values_len != expected_len) {
484 ESP_LOGE(TAG,
"values_len %zu does not match %u entities (expected %zu) for function code %02X, dropping request",
485 values_len, number_of_entities, expected_len,
static_cast<uint8_t
>(function_code));
488 append_pdu_header(pdu, function_code, start_address, number_of_entities);
490 for (
size_t i = 0; i < values_len; i++)
493 mask_trailing_pad_bits(pdu, number_of_entities);
502static bool register_block_in_range(
const LogString *role, uint16_t start_address,
size_t quantity,
503 uint16_t max_quantity) {
504 if (quantity == 0 || quantity > max_quantity) {
505 ESP_LOGE(TAG,
"%s count %zu out of range [1, %u], dropping request", LOG_STR_ARG(role), quantity, max_quantity);
509 ESP_LOGE(TAG,
"%s of %zu registers at %u runs past the 16-bit address space, dropping request", LOG_STR_ARG(role),
510 quantity, start_address);
518template<
typename Pdu>
static Pdu build_write_registers_pdu(uint16_t start_address, std::span<const uint16_t> values) {
519 constexpr auto max_registers =
static_cast<uint16_t
>((Pdu::capacity() - WRITE_MULTIPLE_HEADER_SIZE) / 2);
521 if (!register_block_in_range(LOG_STR(
"Write"), start_address, values.size(), max_registers)) {
525 pdu.push_back(
static_cast<uint8_t
>(values.size() * 2));
526 for (
auto v : values) {
527 append_pdu_word(pdu, v);
532static_assert((
PduBuffer::capacity() - WRITE_MULTIPLE_HEADER_SIZE) / 2 == MAX_NUM_OF_REGISTERS_TO_WRITE,
533 "a full-frame PDU must hold exactly MAX_NUM_OF_REGISTERS_TO_WRITE registers");
535 "the small write buffer must hold exactly MAX_FEW_REGISTERS registers");
538 return build_write_registers_pdu<PduBuffer>(start_address, values);
542 return build_write_registers_pdu<WriteFewRegistersPdu>(start_address, values);
546 uint16_t write_start_address,
547 std::span<const uint16_t> write_values) {
549 if (!register_block_in_range(LOG_STR(
"Read"), read_start_address, read_count, MAX_NUM_OF_REGISTERS_TO_READ)) {
552 if (!register_block_in_range(LOG_STR(
"Write"), write_start_address, write_values.size(),
553 MAX_NUM_OF_REGISTERS_TO_WRITE_RW)) {
557 const auto write_count =
static_cast<uint16_t
>(write_values.size());
559 append_pdu_word(pdu, read_start_address);
560 append_pdu_word(pdu, read_count);
561 append_pdu_word(pdu, write_start_address);
562 append_pdu_word(pdu, write_count);
563 pdu.
push_back(
static_cast<uint8_t
>(write_count * 2));
564 for (
auto v : write_values) {
565 append_pdu_word(pdu, v);
584static void build_write_coils_pdu(
PduBuffer &pdu, uint16_t start_address,
PackedBits bits) {
586 const std::span<const uint8_t> packed_bits = bits.
bytes();
588 ESP_LOGE(TAG,
"No coils requested for write multiple coils, dropping request");
591 if (
count > MAX_NUM_OF_COILS_TO_WRITE) {
592 ESP_LOGE(TAG,
"count %u exceeds maximum coils to write %u, dropping request",
count, MAX_NUM_OF_COILS_TO_WRITE);
596 ESP_LOGE(TAG,
"Write of %u coils at %u runs past the 16-bit address space, dropping request",
count, start_address);
600 if (packed_bits.size() < byte_count) {
601 ESP_LOGE(TAG,
"packed_bits (%zu bytes) does not cover %u coils (%zu bytes), dropping request", packed_bits.size(),
606 pdu.push_back(
static_cast<uint8_t
>(byte_count));
607 for (
size_t i = 0; i != byte_count; i++) {
608 pdu.push_back(packed_bits[i]);
610 mask_trailing_pad_bits(pdu,
count);
615 build_write_coils_pdu(pdu, start_address, bits);
620template<
typename BoolContainer>
621static PduBuffer create_write_coils_pdu_from_bools(uint16_t start_address,
const BoolContainer &values) {
625 if (
count > MAX_NUM_OF_COILS_TO_WRITE) {
626 ESP_LOGE(TAG,
"values.size() %zu exceeds maximum coils to write %u, dropping request",
count,
627 MAX_NUM_OF_COILS_TO_WRITE);
632 build_write_coils_pdu(pdu, start_address, PackedBits(std::span<const uint8_t>(packed.data(), packed.size()),
count));
637 return create_write_coils_pdu_from_bools(start_address, values);
641 return create_write_coils_pdu_from_bools(start_address, values);
Minimal static vector - saves memory by avoiding std::vector overhead.
void push_back(const T &value)
static constexpr size_t capacity()
void assign(InputIt first, InputIt last)
Read-only view of Modbus-packed bits: bit 0 of byte 0 is the first bit (LSB first),...
std::span< const uint8_t > bytes() const
The underlying packed bytes: exactly ceil(size() / 8) bytes, even when the view was constructed over ...
uint16_t size() const
Number of bits in the view.
bool address_range_fits(uint16_t start_address, size_t count)
PduBuffer create_write_coils_pdu(uint16_t start_address, PackedBits bits)
Create modbus write multiple coils command (function 0x0F) from bits packed as on the wire.
StaticVector< uint8_t, MAX_PDU_SIZE > PduBuffer
WriteFewRegistersPdu create_write_few_registers_pdu(uint16_t start_address, std::span< const uint16_t > values)
Create modbus write multiple registers command (function 0x10) on a right-sized stack buffer.
bool is_function_code_read_only(uint8_t function_code)
WriteSinglePdu create_write_single_coil_pdu(uint16_t address, bool value)
Create modbus write single coil command Function 0x05 Write Single Coil.
std::span< const uint8_t > data
void pack_bits(Out &out, const Bits &bits)
Append packed bytes (LSB first) for the given bits onto a growable byte container.
constexpr uint32_t registers_to_uint32(uint16_t high_word, uint16_t low_word)
Combine two register words into a 32-bit value.
ESPHOME_LOG_TAG(TAG, "modbus_helpers")
T get_data(const uint8_t *data, size_t buffer_offset)
Extract data from modbus response buffer.
constexpr auto registers_to_value(const uint16_t *registers)
Decode one value whose type is known at compile time, from registers in host byte order.
ReadPdu create_read_pdu(FunctionCode function_code, uint16_t start_address, uint16_t number_of_entities)
Create a modbus read request PDU.
constexpr uint16_t register_width_for(SensorValueType v)
Number of 16-bit registers a value of this type occupies (RAW counts as one register).
WriteSinglePdu create_write_single_register_pdu(uint16_t start_address, uint16_t value)
Create modbus write single register command Function 0x06 Write Single Register.
PduBuffer create_client_pdu(FunctionCode function_code, uint16_t start_address, uint16_t number_of_entities, const uint8_t *values, size_t values_len)
Create a modbus client pdu for reading/writing single/multiple coils/register/inputs.
uint16_t server_pdu_length(const uint8_t *frame, size_t size)
N mask_and_shift_by_rightbit(N data, uint32_t mask)
Extract bits from value and shift right according to the bitmask if the bitmask is 0x00F0 we want the...
bool is_server_pdu_standard(const uint8_t *pdu, size_t size)
void log_unsupported_value_type(SensorValueType value_type)
bool is_client_pdu_standard(const uint8_t *pdu, size_t size)
PduBuffer create_write_registers_pdu(uint16_t start_address, std::span< const uint16_t > values)
Create modbus write multiple registers command Function 0x10 Write Multiple Registers.
StaticVector< uint8_t, packed_bit_bytes(MAX_NUM_OF_COILS_TO_WRITE)> CoilPackBuffer
Scratch space for packing coils into wire layout: one bit per coil, sized for the spec maximum.
PduBuffer create_read_write_multiple_registers_pdu(uint16_t read_start_address, uint16_t read_count, uint16_t write_start_address, std::span< const uint16_t > write_values)
Create modbus read/write multiple registers command Function 0x17 Read/Write Multiple Registers Write...
std::optional< int64_t > registers_to_number(const uint16_t *registers, size_t count, SensorValueType sensor_value_type)
Reconstruct a number from register words (host byte order).
uint16_t client_pdu_length(const uint8_t *frame, size_t size)
bool is_function_code_exception(uint8_t function_code)
std::optional< int64_t > payload_to_number(const uint8_t *data, size_t size, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
Convert a raw response payload to a number.
@ WRITE_MULTIPLE_REGISTERS
@ READ_WRITE_MULTIPLE_REGISTERS
constexpr size_t packed_bit_bytes(size_t bits)
Bits pack 8 per data byte, rounded up to whole bytes.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.