32uint8_t get_bit_msb(
const uint8_t *data, uint16_t bit_pos) {
return (data[bit_pos / 8] >> (7 - (bit_pos % 8))) & 0x01; }
47uint8_t
decode_uart_probe(
const uint8_t *raw, uint8_t raw_len, uint8_t bit_offset, uint8_t *decoded,
48 uint8_t decoded_max_len) {
53 uint16_t bit_pos = bit_offset;
54 uint16_t
const total_bits = raw_len * 8;
55 uint8_t decoded_len = 0;
57 while (bit_pos + 10 <= total_bits && decoded_len < decoded_max_len) {
58 if (get_bit_msb(raw, bit_pos) != 0 || get_bit_msb(raw, bit_pos + 9) != 1)
62 for (uint8_t index = 0; index < 8; index++)
63 value |= get_bit_msb(raw, bit_pos + 1 + index) << index;
65 decoded[decoded_len++] = value;
136bool is_plausible_uart_frame(
const IoFrame &frame, uint8_t candidate_len) {
151 for (uint8_t start = 0; start < decoded_len; start++) {
161 const uint8_t max_candidate_len = std::min<uint8_t>(decoded_len - start,
FRAME_MAX_WIRE_SIZE);
162 for (uint8_t candidate_len = max_candidate_len; candidate_len >=
FRAME_MIN_SIZE; candidate_len--) {
164 if (!
parse(decoded + start, candidate_len, frame))
166 if (!is_plausible_uart_frame(frame, candidate_len))
168 if (start + candidate_len + 2 > decoded_len)
170 const uint16_t computed_crc =
crc_ccitt(decoded + start, candidate_len);
171 const uint16_t received_crc =
172 (uint16_t) decoded[start + candidate_len] | ((uint16_t) decoded[start + candidate_len + 1] << 8);
173 if (computed_crc != received_crc)
175 return {start, candidate_len};
193 uint8_t
const decoded_len =
decode_uart_probe(raw, raw_len, bit_offset, decoded,
sizeof(decoded));
194 if (decoded_len == 0)
200 memcpy(best.
decoded, decoded, decoded_len);
210 memcpy(best.
decoded, decoded, decoded_len);
225 return (uint8_t) ((bits + 7) / 8);
237 best = std::max(frame_len, best);
244uint8_t
uart_encode_packet(
const uint8_t *data, uint8_t len, uint8_t *encoded, uint8_t encoded_max_len) {
245 if (len == 0 || encoded_max_len == 0)
248 memset(encoded, 0, encoded_max_len);
249 uint16_t bit_pos = 0;
250 const uint16_t total_bits = len * 10;
251 if (((total_bits + 7) / 8) > encoded_max_len)
254 auto write_bit = [encoded](uint16_t pos, uint8_t bit) {
256 encoded[pos / 8] |= 1U << (7 - (pos % 8));
259 for (uint8_t byte_index = 0; byte_index < len; byte_index++) {
260 const uint8_t value = data[byte_index];
262 write_bit(bit_pos++, 0);
263 for (uint8_t bit_index = 0; bit_index < 8; bit_index++)
264 write_bit(bit_pos++, (value >> bit_index) & 0x01);
265 write_bit(bit_pos++, 1);
273 const uint8_t encoded_len = (total_bits + 7) / 8;
274 for (uint16_t pad_pos = total_bits; pad_pos < (uint16_t) encoded_len * 8; pad_pos++)
275 write_bit(pad_pos, 1);
static constexpr uint8_t CMD_DISCOVER_REQ
Broadcast discovery request.
static constexpr uint8_t CMD_SET_CONFIG1
Configure device to auto-send status updates.
uint8_t decode_uart_probe(const uint8_t *raw, uint8_t raw_len, uint8_t bit_offset, uint8_t *decoded, uint8_t decoded_max_len)
Decode a raw UART‑encoded bitstream into bytes.
static constexpr uint8_t CMD_KEY_TRANSFER
Send encrypted system key to device.
static constexpr uint8_t FRAME_MIN_SIZE
Minimum frame: CTRL0+CTRL1+DST(3)+SRC(3)+CMD(1).
bool is_known_io_command(uint8_t cmd)
This list missing CMD_GET_GENERAL_INFO3_RESP (0x59) is exactly what turned a real Q2 probe reply into...
static constexpr uint8_t CMD_ERROR_RESP
Error response to any command.
static constexpr uint8_t CMD_GET_NAME_RESP
Device name response.
static constexpr uint8_t CMD_DISCOVER_CONFIRM_ACK
Device acknowledges confirmation.
static constexpr uint8_t CMD_STATUS_UPDATE
Device-initiated status update (needs auth).
uint8_t soft_phy_peek_frame_length(const uint8_t *raw, uint8_t raw_len)
Recover a frame's total length from the very first UART cell of a reception.
uint8_t uart_encode_packet(const uint8_t *data, uint8_t len, uint8_t *encoded, uint8_t encoded_max_len)
UART-encode a buffer of bytes (start bit 0, 8 data bits LSB-first, stop bit 1).
static constexpr uint8_t CMD_PRIVATE2_RESP
Response to CMD_PRIVATE2. See CMD_PRIVATE2's comment.
static constexpr uint8_t CMD_GET_NAME
Request device name.
static constexpr uint8_t CTRL0_PROTOCOL_1W
Bit 5: 1=OneWay protocol, 0=TwoWay protocol.
static constexpr uint8_t CMD_DISCOVER_SPE_RESP
Roll-call reply to CMD_DISCOVER_SPE_REQ, sent only by devices that already hold the requesting contro...
UartProbeResult find_uart_probe(const uint8_t *raw, uint8_t raw_len)
Search raw RX buffer for the best CRC-validated IO-Homecontrol frame.
uint16_t crc_ccitt(const uint8_t *data, uint8_t len)
CRC-CCITT used by the IO-Homecontrol protocol for frame validation.
static constexpr uint8_t CMD_WRITE_PRIVATE
Write private register (climate/heating devices).
static std::pair< uint8_t, uint8_t > find_crc_valid_frame(const uint8_t *decoded, uint8_t decoded_len)
Try to find a CRC-valid IO-Homecontrol frame within a decoded UART byte stream.
static constexpr uint8_t FRAME_MAX_SIZE
Historical name for FRAME_MAX_DECLARED_SIZE, kept as an alias rather than a second literal so the two...
static constexpr uint8_t CMD_KEY_CONFIRM
Device confirms key was received.
static constexpr uint8_t CMD_WRITE_PRIVATE_ACK
Acknowledgment to CMD_WRITE_PRIVATE.
static constexpr uint8_t CMD_KEY_INIT
Initiate key transfer to device.
static constexpr uint8_t CMD_GET_GENERAL_INFO3_RESP
Never captured on our own wire.
static constexpr uint8_t CMD_GET_INFO1_RESP
Device general info 1 response.
static constexpr uint8_t CMD_SET_NAME_RESP
Device-name write response.
static constexpr uint8_t FRAME_MAX_WIRE_SIZE
Largest number of bytes a buffer must hold to receive or transmit any frame this project knows about,...
uint8_t soft_phy_raw_bytes_for_frame(uint8_t frame_len)
Raw on-air bytes needed to carry a whole frame: frame_len protocol bytes plus the two trailing CRC by...
bool parse(const uint8_t *buf, uint8_t buf_len, IoFrame &f)
Parse a wire buffer into a parsed IoFrame (validates length and CTRL0).
static constexpr uint8_t CMD_DISCOVER_SPE_REQ
Broadcast roll-call answered by every device that already holds this controller's system key,...
static constexpr uint8_t CMD_EXECUTE
Set position/open/close/stop — requires authentication.
static constexpr uint8_t CMD_PRIVATE_RESP
Response to 0x00 and 0x03 (contains position data).
static constexpr uint8_t CMD_CHALLENGE_REQ
6-byte random challenge.
static constexpr uint8_t CMD_STATUS_UPDATE_RESP
Acknowledge status update.
static constexpr uint8_t CMD_SET_NAME
Set device name (authenticated).
static constexpr uint8_t CMD_SET_CONFIG1_RESP
Config response, otherwise undocumented.
static constexpr uint8_t CMD_DISCOVER_CONFIRM
Confirm discovery to device.
static constexpr uint8_t FRAME_CRC_SIZE
Size of the on-air CRC-CCITT trailer appended after every frame (declared bytes, plus the out-of-leng...
static constexpr uint8_t CMD_DISCOVER_RESP
Device responds with its ID and type.
static constexpr uint8_t CMD_GET_GENERAL_INFO3
Observed on the wire (tests/corpus/captures/probe/velux_kig300_probe_capability_burst....
static constexpr uint8_t CMD_IDENTIFY
Device physical identification / jog — requires authentication.
static constexpr uint8_t CMD_PRIVATE
Get device status — no authentication needed.
static constexpr uint8_t CTRL0_LENGTH_MASK
Bits [4:0]: frame length - 1.
static constexpr uint8_t CMD_PRIVATE2
Content otherwise undecoded by the wire parser.
static constexpr uint8_t CMD_GET_INFO2_RESP
Device type/model response.
static constexpr uint8_t CMD_CHALLENGE_RESP
HMAC proof answering a 0x3C.
static constexpr uint8_t CMD_GET_INFO2
Request device type/model info.
constexpr uint8_t RADIO_PACKET_BUFFER_SIZE
Scratch buffer size for raw radio packets and recovered frames.
static constexpr uint8_t CMD_GET_INFO1
Request device general info 1.
static constexpr uint8_t UART_CELL_BITS
Bits an on-air UART cell spends per protocol byte: start(1) + data(8) + stop(1).
static constexpr uint8_t CMD_ADDRESS_REQ
"Report your address" request.
static const uint8_t UART_PROBE_MAX_BIT_OFFSET
Maximum bit offset to search for valid UART decode start position.
static constexpr uint8_t CMD_UNKNOWN4A_RESP
Observed on the wire (tests/corpus/captures/probe/velux_kig300_probe_capability_burst....
IO-Homecontrol command IDs, result codes and protocol enumerations.
Software PHY for radios without IoHomeOn hardware framing.
Parsed IO‑Homecontrol frame (CTRL0/1 + addresses + command + data).
Result of the UART probe: best candidate frame within a raw capture.
uint8_t decoded_len
Total number of bytes decoded at that offset.
uint8_t frame_start
Index into decoded buffer where the frame begins.
bool valid
A plausible frame was found.
uint8_t bit_offset
Bit offset where the best decode started.
uint8_t frame_len
Length of the candidate IoFrame (decoded bytes).
uint8_t decoded[RADIO_PACKET_BUFFER_SIZE]
Full decoded UART stream at the chosen offset.