12static constexpr uint16_t PRODUCT_ID = 0x098b;
15static constexpr uint8_t FC_ENCRYPTED = 0x08;
16static constexpr uint8_t FC_MAC_INCLUDED = 0x10;
17static constexpr uint8_t FC_CAPABILITY = 0x20;
18static constexpr uint8_t FC_OBJECT = 0x40;
21static constexpr size_t MIC_SIZE = 4;
22static constexpr size_t ENCRYPTED_TRAILER = 3 + MIC_SIZE;
24static constexpr size_t MAX_PAYLOAD_SIZE = 16;
28static constexpr uint16_t OBJ_LIGHT = 0x0018;
29static constexpr uint16_t OBJ_LIGHT_ALT = 0x1018;
30static constexpr uint16_t OBJ_DOOR = 0x0019;
31static constexpr uint16_t OBJ_DOOR_ALT = 0x1019;
32static constexpr uint16_t OBJ_BATTERY = 0x100A;
33static constexpr uint16_t OBJ_BATTERY_ALT = 0x4803;
35void XiaomiMCCGQ02HL::dump_config() {
36 uint8_t mac[MAC_ADDRESS_SIZE];
38 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
43 LOG_BINARY_SENSOR(
" ",
"Opening",
this);
44 LOG_BINARY_SENSOR(
" ",
"Light", this->light_binary_sensor_);
45 LOG_SENSOR(
" ",
"Battery Level", this->battery_level_sensor_);
50bool XiaomiMCCGQ02HL::parse_device(
const ble_device_base::ESPBTDevice &device) {
51 if (device.address_uint64() != this->address_)
55 for (
auto &service_data : device.get_service_datas()) {
56 if (!service_data.uuid.contains(0x95, 0xFE))
63 ESP_LOGD(TAG,
"%s: open=%s light=%s battery=%s", LOG_STR_ARG(this->
get_log_name()),
64 reading.open.has_value() ? (*reading.open ? LOG_STR_LITERAL(
"yes") : LOG_STR_LITERAL(
"no"))
65 : LOG_STR_LITERAL(
"-"),
66 reading.light.has_value() ? (*reading.light ? LOG_STR_LITERAL(
"yes") : LOG_STR_LITERAL(
"no"))
67 : LOG_STR_LITERAL(
"-"),
68 reading.battery_level.has_value() ? LOG_STR_LITERAL(
"updated") : LOG_STR_LITERAL(
"-"));
70 if (reading.open.has_value())
80 uint8_t nonce[MAC_ADDRESS_SIZE + 6];
81 for (
size_t i = 0; i < MAC_ADDRESS_SIZE; i++)
82 nonce[i] =
static_cast<uint8_t
>(this->
address_ >> (i * 8));
83 memcpy(nonce + MAC_ADDRESS_SIZE, frame + 2, 3);
84 memcpy(nonce + MAC_ADDRESS_SIZE + 3, frame +
size - ENCRYPTED_TRAILER, 3);
85 static constexpr uint8_t AUTH_DATA[1] = {0x11};
87 frame + offset,
size - offset - ENCRYPTED_TRAILER, plaintext,
88 frame +
size - MIC_SIZE, MIC_SIZE);
92 const size_t size = data.size();
95 const uint8_t *
raw = data.data();
97 const uint8_t fc =
raw[0];
98 if (!(fc & FC_OBJECT))
101 ESP_LOGVV(TAG,
"Not an MCCGQ02HL frame (product id %02X%02X).",
raw[3],
raw[2]);
105 const uint8_t frame_count =
raw[4];
106 if (this->
last_frame_count_.has_value() && *this->last_frame_count_ == frame_count) {
107 ESP_LOGVV(TAG,
"Duplicate frame %u.", frame_count);
112 if (!(fc & FC_ENCRYPTED)) {
113 ESP_LOGVV(TAG,
"Ignoring unencrypted frame %u.", frame_count);
117 const size_t offset = 5 + ((fc & FC_MAC_INCLUDED) ? 6 : 0) + ((fc & FC_CAPABILITY) ? 1 : 0);
119 ESP_LOGW(TAG,
"Unsupported encrypted frame layout (fc=0x%02X, %u bytes).", fc, (
unsigned)
size);
122 uint8_t plaintext[MAX_PAYLOAD_SIZE];
124 ESP_LOGW(TAG,
"Decryption failed (%u-byte frame) -- check the bindkey.", (
unsigned)
size);
139 const uint8_t
len = payload[2];
142 const uint8_t *
value = payload + 3;
144 if ((
id == OBJ_DOOR ||
id == OBJ_DOOR_ALT) &&
len == 1) {
150 }
else if ((
id == OBJ_LIGHT ||
id == OBJ_LIGHT_ALT) &&
len == 1) {
153 }
else if ((
id == OBJ_BATTERY ||
id == OBJ_BATTERY_ALT) &&
len == 1) {
157 ESP_LOGVV(TAG,
"Ignoring object 0x%04X (%u bytes).",
id,
len);
bool aes_ccm_auth_decrypt(const uint8_t key[16], const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *ciphertext, size_t ct_len, uint8_t *plaintext, const uint8_t *tag, size_t tag_len)
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.