14static constexpr uint8_t MAX_CHUNK_SIZE = 20;
16static constexpr uint16_t MAX_MESSAGE_SIZE = 255;
18static constexpr uint8_t MAX_CHUNK_PAYLOAD = MAX_CHUNK_SIZE - 1;
19static constexpr uint8_t MAX_CHUNKS = (MAX_MESSAGE_SIZE + MAX_CHUNK_PAYLOAD - 1) / MAX_CHUNK_PAYLOAD;
21static constexpr uint8_t MAX_QUERY_ARGS = 8;
23static constexpr uint8_t MESSAGE_HEADER_SIZE = 4;
25static constexpr uint8_t EMPTY_ARG_LENGTH = 0xFF;
27static constexpr float MIN_TEMP = 16.0f;
28static constexpr float MAX_TEMP = 32.0f;
30static constexpr float SETPOINT_SCALE = 128.0f;
51 const uint8_t chunk_id = chunk[0];
52 const auto payload = chunk.subspan(1);
53 if (chunk_id >= MAX_CHUNKS)
55 if (payload.size() > MAX_CHUNK_PAYLOAD) {
59 std::copy(payload.begin(), payload.end(), this->data_.begin() + chunk_id * MAX_CHUNK_PAYLOAD);
77 for (uint8_t i = 1; i <
count; i++) {
78 std::copy_n(this->
data_.begin() + i * MAX_CHUNK_PAYLOAD, this->chunk_sizes_[i], this->data_.begin() +
pos);
93 std::array<uint8_t, MAX_CHUNKS * MAX_CHUNK_PAYLOAD>
data_;
101 return msg.size() >= MESSAGE_HEADER_SIZE && msg[0] == msg.size();
107inline std::span<const uint8_t>
find_arg(std::span<const uint8_t> msg, uint8_t
id) {
108 size_t i = MESSAGE_HEADER_SIZE;
109 while (i + 2 <= msg.size()) {
110 const uint8_t arg_id = msg[i];
111 const uint8_t
len = msg[i + 1] == EMPTY_ARG_LENGTH ? 0 : msg[i + 1];
113 if (
len > msg.size() - i)
116 return msg.subspan(i,
len);
130 while (i + 2 <= incoming.size()) {
131 const uint8_t
id = incoming[i];
132 const uint8_t
len = incoming[i + 1];
134 if (
len > incoming.size() - i)
136 const auto value = incoming.subspan(i,
len);
139 while (j + 2 <= merged.
size() && merged[j] !=
id)
140 j += 2 + merged[j + 1];
141 if (j + 2 <= merged.
size()) {
142 if (merged[j + 1] !=
len)
151 for (uint8_t
byte :
value)
160 const auto value =
static_cast<uint16_t
>(std::clamp(
temperature, MIN_TEMP, MAX_TEMP) * SETPOINT_SCALE);
void push_back(const T &value)
static constexpr size_t capacity()
Reassembles notifications (one chunk id byte followed by payload) into a message.
ChunkResult feed(std::span< const uint8_t > chunk)
std::array< uint8_t, MAX_CHUNKS *MAX_CHUNK_PAYLOAD > data_
std::span< const uint8_t > message() const
std::array< uint8_t, MAX_CHUNKS > chunk_sizes_
bool is_valid_message(std::span< const uint8_t > msg)
True when the length byte matches and the header (length, reserved, function id) is present.
@ CHUNK_RESULT_INVALID_ID
@ CHUNK_RESULT_INVALID_LENGTH
void append_setpoint(QueryArgs &args, uint8_t id, float temperature)
Appends a setpoint record {id, 2, msb, lsb}, clamped to the supported range.
std::span< const uint8_t > find_arg(std::span< const uint8_t > msg, uint8_t id)
Returns the value of argument id, or an empty span when it is absent, has no value or the records are...
uint16_t message_function_id(std::span< const uint8_t > msg)
bool merge_args(QueryArgs &args, std::span< const uint8_t > incoming)
Merges the records {id, len, value...} of incoming into args: a record whose id is already present is...
float decode_setpoint(std::span< const uint8_t > value)
Decodes a two byte setpoint value; the caller checks the size.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).