Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
proto_commands.cpp
Go to the documentation of this file.
1/// @file proto_commands.cpp
2/// @brief Command builders for the IO-Homecontrol protocol.
3
4#include "proto_commands.h"
5
6#include "proto_crypto.h"
7
8#include <cstring>
9
10namespace esphome {
11namespace home_io_control {
12
13namespace {
14
15// === Command payload templates ===
16
17/// The protocol uses 0-100 for percentage-style position inputs before encoding them on wire.
18constexpr uint8_t POSITION_PERCENT_MAX = 100;
19/// Byte 0 in execute-family payloads identifies a user-originated remote action.
20constexpr uint8_t EXECUTE_USER_ORIGINATOR = 0x01;
21/// ACEI/profile byte observed on normal execute commands.
22constexpr uint8_t EXECUTE_POSITION_ACEI = 0x67;
23/// Standard payload length for full execute-family commands.
24constexpr size_t EXECUTE_PAYLOAD_SIZE = 8;
25/// Bit flag that marks the standard position payload layout after the encoded position byte.
26constexpr uint8_t EXECUTE_POSITION_LAYOUT_FLAG = 0x80;
27/// Controller-capture matched helper byte used in normal execute payloads.
28constexpr uint8_t EXECUTE_POSITION_PROFILE = 0x06;
29/// ACEI/profile byte observed on tilt execute commands.
30constexpr uint8_t EXECUTE_TILT_ACEI = 0xE7;
31/// Short payload length for special execute commands such as stop/favorite.
32constexpr size_t EXECUTE_SPECIAL_PAYLOAD_SIZE = 6;
33/// Private sub-command for position status requests.
34constexpr uint8_t PRIVATE_GET_POSITION_STATUS = 0x03;
35/// Status-update acknowledgement payload matched from controller traffic.
36constexpr uint8_t STATUS_UPDATE_ACK_PAYLOAD[] = {0x05, 0x00};
37/// Set-config payload that enables automatic status updates from the device.
38constexpr uint8_t SET_CONFIG1_STATUS_BROADCAST_PAYLOAD[] = {0xE0, 0x10, 0x0A, 0x08, 0x00};
39
40} // namespace
41
42/// Build an execute command (0x00) to control a device.
43/// For real positions (0-100), the value is doubled in the frame (0x00=0%, 0xC8=100%).
44/// For special commands (stop/favorite), a shorter 6-byte payload is used.
45bool create_execute(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t position) {
46 init_frame(f, true, true, false, low_power);
47 set_dst(f, dst);
48 set_src(f, own);
49 if (position <= POSITION_PERCENT_MAX) {
50 // Real position: doubled value (0-200 maps to 0-100%).
51 const uint8_t payload[EXECUTE_PAYLOAD_SIZE] = {
52 EXECUTE_USER_ORIGINATOR, EXECUTE_POSITION_ACEI, static_cast<uint8_t>(2 * position), 0x00,
53 EXECUTE_POSITION_LAYOUT_FLAG, POS_FAVORITE, EXECUTE_POSITION_PROFILE, 0x00};
54 return set_cmd(f, CMD_EXECUTE, payload, sizeof(payload));
55 }
56
57 // Special command (stop=0xD2, favorite=0xD8).
58 const uint8_t payload[EXECUTE_SPECIAL_PAYLOAD_SIZE] = {
59 EXECUTE_USER_ORIGINATOR, EXECUTE_POSITION_ACEI, position, 0x00, 0x00, 0x00};
60 return set_cmd(f, CMD_EXECUTE, payload, sizeof(payload));
61}
62
63/// Build a get-status request (0x03). The device responds with its current position.
64bool create_get_status(IoFrame &f, const uint8_t *own, const uint8_t *dst) {
65 // low_power=true for solar devices.
66 init_frame(f, true, true, false, true);
67 set_dst(f, dst);
68 set_src(f, own);
69 // Private sub-command = get position status.
70 uint8_t d[3] = {PRIVATE_GET_POSITION_STATUS, 0x00, 0x00};
71 return set_cmd(f, CMD_PRIVATE, d, sizeof(d));
72}
73
74/// Build a tilt execute command (0x00) for devices that support slat angle control.
75bool create_execute_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t tilt_percent) {
76 init_frame(f, true, true, false, low_power);
77 set_dst(f, dst);
78 set_src(f, own);
79
80 auto const tilt_value =
81 static_cast<uint16_t>((POSITION_PERCENT_MAX - tilt_percent) * STATUS_POS_MAX / POSITION_PERCENT_MAX);
82 uint8_t d[EXECUTE_PAYLOAD_SIZE] = {EXECUTE_USER_ORIGINATOR,
83 EXECUTE_TILT_ACEI,
85 0x00,
87 static_cast<uint8_t>(tilt_value >> BITS_PER_BYTE),
88 static_cast<uint8_t>(tilt_value),
89 0x00};
90 return set_cmd(f, CMD_EXECUTE, d, sizeof(d));
91}
92
93/// Build a tilt-aware get-status request (0x03) that returns the extended 16-byte tilt payload.
94bool create_get_status_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst) {
95 init_frame(f, true, true, false, true);
96 set_dst(f, dst);
97 set_src(f, own);
98 // The selector byte switches the private status response to the extended tilt layout.
99 uint8_t d[4] = {PRIVATE_GET_POSITION_STATUS, STATUS_TILT_SELECTOR, 0x01, 0x00};
100 return set_cmd(f, CMD_PRIVATE, d, sizeof(d));
101}
102
103/// Build a discovery broadcast (0x28). Sent to the broadcast address 0x00003B.
104/// Only devices in pairing mode (PROG button pressed) will respond.
105bool create_discover(IoFrame &f, const uint8_t *own) {
106 // start+end: single broadcast frame.
107 init_frame(f, true, true, true, false);
109 set_src(f, own);
110 return set_cmd(f, CMD_DISCOVER_REQ);
111}
112
113/// Build a key-init request (0x31) to start the pairing key exchange with a discovered device.
114bool create_key_init(IoFrame &f, const uint8_t *own, const uint8_t *dst) {
115 init_frame(f, true, true, false, true);
116 set_dst(f, dst);
117 set_src(f, own);
118 return set_cmd(f, CMD_KEY_INIT);
119}
120
121/// Build a key-transfer frame (0x32) containing the system key encrypted with the transfer key.
122bool create_key_transfer(IoFrame &f, IoFrame &old_frame, const uint8_t *dst, const uint8_t *src,
123 const uint8_t key[AES_KEY_SIZE], const uint8_t challenge[HMAC_SIZE]) {
124 init_frame(f, true, false, false, false);
125 set_dst(f, dst);
126 set_src(f, src);
127 // The pairing capture we matched derives the IV from the previous command byte only. Treating
128 // the key-init frame that narrowly keeps our key transfer aligned with real controllers.
129 uint8_t enc_key[AES_KEY_SIZE];
130 if (!crypto::crypt_key(&old_frame.cmd, 1, challenge, key, enc_key))
131 return false;
132 return set_cmd(f, CMD_KEY_TRANSFER, enc_key, AES_KEY_SIZE);
133}
134
135/// Build a challenge request (0x3C) containing 6 random bytes.
136/// Used when WE need to authenticate an incoming request from a device.
137bool create_challenge_req(IoFrame &f, const uint8_t *dst, const uint8_t *src) {
138 init_frame(f, true, true, false, false); // start=true, end=false
139 set_dst(f, dst);
140 set_src(f, src);
141 uint8_t challenge[HMAC_SIZE];
143 return set_cmd(f, CMD_CHALLENGE_REQ, challenge, HMAC_SIZE);
144}
145
146/// Build a challenge response (0x3D) proving we know the system key.
147/// The HMAC is computed over [original_command_id + original_data] using the challenge.
148bool create_challenge_resp(IoFrame &f, const uint8_t *dst, const uint8_t *src, const uint8_t challenge[HMAC_SIZE],
149 const IoFrame &origin, const uint8_t *key) {
150 init_frame(f);
151 set_dst(f, dst);
152 set_src(f, src);
153 // The authenticated transcript covers the original request, not the 0x3D wrapper. Using the
154 // origin command byte and payload here was one of the key interoperability findings.
155 uint8_t frame_data[FRAME_MAX_SIZE];
156 frame_data[0] = origin.cmd;
157 memcpy(frame_data + 1, origin.data, origin.data_len);
158 uint8_t hmac[HMAC_SIZE];
159 if (!crypto::create_hmac(frame_data, origin.data_len + 1, challenge, key, hmac))
160 return false;
161 return set_cmd(f, CMD_CHALLENGE_RESP, hmac, HMAC_SIZE);
162}
163
164/// Build a status-update acknowledgment (0x72). Sent after authenticating a device's status update.
165/// The response is sent on all 3 channels to ensure the device receives it.
166bool create_status_update_resp(IoFrame &f, const uint8_t *own, const uint8_t *dst) {
167 // end=true: final frame.
168 init_frame(f, true, false, true, false);
169 set_dst(f, dst);
170 set_src(f, own);
171 // Status update acknowledgment payload matched from working controller captures.
172 return set_cmd(f, CMD_STATUS_UPDATE_RESP, STATUS_UPDATE_ACK_PAYLOAD, sizeof(STATUS_UPDATE_ACK_PAYLOAD));
173}
174
175/// Build a set-config command (0x6F) to tell the device to automatically send status updates
176/// when controlled by any remote (not just us). Not all devices support this.
177bool create_set_config1(IoFrame &f, const uint8_t *own, const uint8_t *dst) {
178 init_frame(f, true, true, false, false);
179 set_dst(f, dst);
180 set_src(f, own);
181 // Set-config payload matched from working controller captures.
182 return set_cmd(f, CMD_SET_CONFIG1, SET_CONFIG1_STATUS_BROADCAST_PAYLOAD,
183 sizeof(SET_CONFIG1_STATUS_BROADCAST_PAYLOAD));
184}
185
186} // namespace home_io_control
187} // namespace esphome
void generate_challenge(uint8_t out[HMAC_SIZE])
Generate 6 random bytes for a challenge using the ESP32 hardware RNG.
bool create_hmac(const uint8_t *data, uint8_t len, const uint8_t challenge[HMAC_SIZE], const uint8_t key[AES_KEY_SIZE], uint8_t hmac[HMAC_SIZE])
Create a 6-byte HMAC for authentication (proprietary IO-Homecontrol scheme).
bool crypt_key(const uint8_t *data, uint8_t len, const uint8_t challenge[HMAC_SIZE], const uint8_t in[AES_KEY_SIZE], uint8_t out[AES_KEY_SIZE])
Encrypt or decrypt a system key during pairing.
bool set_cmd(IoFrame &f, uint8_t cmd, const uint8_t *params, uint8_t params_len)
Set command and payload.
static constexpr uint8_t BITS_PER_BYTE
Number of bits in one protocol byte.
Definition proto_frame.h:87
static constexpr uint8_t CMD_DISCOVER_REQ
Broadcast discovery request.
static constexpr uint8_t CMD_SET_CONFIG1
Configure device to auto-send status updates.
static constexpr uint16_t STATUS_POS_MAX
In status responses, position is encoded as a 16-bit value where 0x0000 = fully open (0%) and 0xC800 ...
static constexpr uint8_t CMD_KEY_TRANSFER
Send encrypted system key to device.
bool create_get_status(IoFrame &f, const uint8_t *own, const uint8_t *dst)
Build a get-status request (0x03). The device responds with its current position.
static constexpr uint8_t POS_UNKNOWN
Position unknown.
bool create_execute(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t position)
Build an execute command (0x00) to control a device.
static constexpr uint8_t HMAC_SIZE
Authentication HMAC is 6 bytes (truncated AES output).
Definition proto_frame.h:82
void init_frame(IoFrame &f, bool is_2w, bool start, bool end, bool low_power)
Initialize an IoFrame header (ctrl0/ctrl1) with flags.
static constexpr uint8_t FRAME_MAX_SIZE
Maximum frame size (9 header + 23 data).
Definition proto_frame.h:90
static constexpr uint8_t CMD_KEY_INIT
Initiate key transfer to device.
bool create_execute_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t tilt_percent)
Build a tilt execute command (0x00) for devices that support slat angle control.
bool create_set_config1(IoFrame &f, const uint8_t *own, const uint8_t *dst)
Build a set-config command (0x6F) to tell the device to automatically send status updates when contro...
bool create_challenge_resp(IoFrame &f, const uint8_t *dst, const uint8_t *src, const uint8_t challenge[HMAC_SIZE], const IoFrame &origin, const uint8_t *key)
Build a challenge response (0x3D) proving we know the system key.
void set_dst(IoFrame &f, const uint8_t id[NODE_ID_SIZE])
Set destination node ID.
bool create_key_init(IoFrame &f, const uint8_t *own, const uint8_t *dst)
Build a key-init request (0x31) to start the pairing key exchange with a discovered device.
static constexpr uint8_t CMD_EXECUTE
Set position/open/close/stop — requires authentication.
static constexpr uint8_t CMD_CHALLENGE_REQ
Device sends 6-byte random challenge.
static constexpr uint8_t CMD_STATUS_UPDATE_RESP
Acknowledge status update.
bool create_challenge_req(IoFrame &f, const uint8_t *dst, const uint8_t *src)
Build a challenge request (0x3C) containing 6 random bytes.
bool create_discover(IoFrame &f, const uint8_t *own)
Build a discovery broadcast (0x28).
static constexpr uint8_t POS_FAVORITE
Move to favorite/"My" position.
static constexpr uint8_t BROADCAST_DISCOVER[NODE_ID_SIZE]
Broadcast address for device discovery (0x00003B).
static constexpr uint8_t STATUS_TILT_SELECTOR
Extended status payload marker for tilt-capable devices.
static constexpr uint8_t CMD_PRIVATE
Get device status — no authentication needed.
static constexpr uint8_t AES_KEY_SIZE
AES-128 key size.
Definition proto_frame.h:83
static constexpr uint8_t CMD_CHALLENGE_RESP
Controller responds with HMAC proof.
bool create_get_status_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst)
Build a tilt-aware get-status request (0x03) that returns the extended 16-byte tilt payload.
bool create_status_update_resp(IoFrame &f, const uint8_t *own, const uint8_t *dst)
Build a status-update acknowledgment (0x72).
bool create_key_transfer(IoFrame &f, IoFrame &old_frame, const uint8_t *dst, const uint8_t *src, const uint8_t key[AES_KEY_SIZE], const uint8_t challenge[HMAC_SIZE])
Build a key-transfer frame (0x32) containing the system key encrypted with the transfer key.
void set_src(IoFrame &f, const uint8_t id[NODE_ID_SIZE])
Set source node ID.
Command builders for the IO‑Homecontrol protocol.
Cryptographic helpers for the IO‑Homecontrol protocol.
Parsed IO‑Homecontrol frame (CTRL0/1 + addresses + command + data).
uint8_t data[FRAME_MAX_DATA_SIZE]
Command parameters (0–23 bytes).
uint8_t data_len
Actual length of data.