ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
daikin_madoka_protocol.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <span>
8
10
11// Wire format helpers kept free of ESP-IDF so they can be unit tested on the host.
12namespace esphome::daikin_madoka {
13
14static constexpr uint8_t MAX_CHUNK_SIZE = 20;
15// A message's leading length byte is a uint8_t, so a full message is at most 255 bytes
16static constexpr uint16_t MAX_MESSAGE_SIZE = 255;
17// Each notification is a chunk id byte followed by up to 19 message bytes
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;
20// Longest args payload is a two-point setpoint: {0x20,0x02,hi,lo,0x21,0x02,hi,lo}
21static constexpr uint8_t MAX_QUERY_ARGS = 8;
22// Length byte, reserved byte and two command bytes precede the args
23static constexpr uint8_t MESSAGE_HEADER_SIZE = 4;
24// A record length of 0xFF marks an argument with no value
25static constexpr uint8_t EMPTY_ARG_LENGTH = 0xFF;
26
27static constexpr float MIN_TEMP = 16.0f;
28static constexpr float MAX_TEMP = 32.0f;
29// Setpoints are sent as fixed point with 7 fractional bits
30static constexpr float SETPOINT_SCALE = 128.0f;
31
39
45 public:
46 ChunkResult feed(std::span<const uint8_t> chunk) {
47 if (chunk.size() < 2)
49 if (this->message_size_ != 0)
50 this->reset();
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) {
56 this->reset();
58 }
59 std::copy(payload.begin(), payload.end(), this->data_.begin() + chunk_id * MAX_CHUNK_PAYLOAD);
60 this->chunk_sizes_[chunk_id] = payload.size();
61 if (this->chunk_sizes_[0] == 0)
63
64 const uint8_t length = this->data_[0];
65 size_t total = this->chunk_sizes_[0];
66 uint8_t count = 1;
68 total += this->chunk_sizes_[count++];
69 if (total > length) {
70 this->reset();
72 }
73 if (total < length)
75 // Close the gaps left by short chunks; each one only moves towards the start
76 size_t pos = this->chunk_sizes_[0];
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);
79 pos += this->chunk_sizes_[i];
80 }
81 this->message_size_ = length;
83 }
84
85 std::span<const uint8_t> message() const { return {this->data_.data(), this->message_size_}; }
86
87 void reset() {
88 this->chunk_sizes_.fill(0);
89 this->message_size_ = 0;
90 }
91
92 protected:
93 std::array<uint8_t, MAX_CHUNKS * MAX_CHUNK_PAYLOAD> data_;
94 // Payload bytes held for each chunk id, 0 while that chunk is missing
95 std::array<uint8_t, MAX_CHUNKS> chunk_sizes_{};
96 uint8_t message_size_{0};
97};
98
100inline bool is_valid_message(std::span<const uint8_t> msg) {
101 return msg.size() >= MESSAGE_HEADER_SIZE && msg[0] == msg.size();
102}
103
104inline uint16_t message_function_id(std::span<const uint8_t> msg) { return encode_uint16(msg[2], msg[3]); }
105
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];
112 i += 2;
113 if (len > msg.size() - i)
114 return {};
115 if (arg_id == id)
116 return msg.subspan(i, len);
117 i += len;
118 }
119 return {};
120}
121
123
127inline bool merge_args(QueryArgs &args, std::span<const uint8_t> incoming) {
128 QueryArgs merged = args;
129 size_t i = 0;
130 while (i + 2 <= incoming.size()) {
131 const uint8_t id = incoming[i];
132 const uint8_t len = incoming[i + 1];
133 i += 2;
134 if (len > incoming.size() - i)
135 return false;
136 const auto value = incoming.subspan(i, len);
137 i += len;
138 size_t j = 0;
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)
143 return false;
144 std::copy(value.begin(), value.end(), merged.begin() + j + 2);
145 continue;
146 }
147 if (merged.size() + 2 + len > QueryArgs::capacity())
148 return false;
149 merged.push_back(id);
150 merged.push_back(len);
151 for (uint8_t byte : value)
152 merged.push_back(byte);
153 }
154 args = merged;
155 return true;
156}
157
159inline void append_setpoint(QueryArgs &args, uint8_t id, float temperature) {
160 const auto value = static_cast<uint16_t>(std::clamp(temperature, MIN_TEMP, MAX_TEMP) * SETPOINT_SCALE);
161 const auto bytes = decode_value<uint16_t>(value);
162 args.push_back(id);
163 args.push_back(2);
164 args.push_back(bytes[0]);
165 args.push_back(bytes[1]);
166}
167
169inline float decode_setpoint(std::span<const uint8_t> value) {
170 return static_cast<float>(encode_uint16(value[0], value[1])) / SETPOINT_SCALE;
171}
172
173} // namespace esphome::daikin_madoka
size_t size() const
Definition helpers.h:373
void push_back(const T &value)
Definition helpers.h:343
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_
unsigned count
Definition gif.cpp:51
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.
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.
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
size_t size_t pos
Definition helpers.h:1333
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
Definition helpers.h:1101
uint16_t temperature
Definition sun_gtil2.cpp:12
uint16_t length
Definition tt21100.cpp:0