refactored xiaomi ble data parsing (#823)

This commit is contained in:
Alexander Leisentritt 2019-11-02 18:55:10 +01:00 committed by Otto Winter
parent 3aaa92fdff
commit 7adaeacd0b
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E

View File

@ -91,17 +91,6 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
return {}; return {};
} }
uint8_t raw_offset = is_lywsdcgq || is_cgg1 ? 11 : 12;
const uint8_t raw_type = raw[raw_offset];
const uint8_t data_length = raw[raw_offset + 2];
const uint8_t *data = &raw[raw_offset + 3];
const uint8_t expected_length = data_length + raw_offset + 3;
const uint8_t actual_length = device.get_service_data().size();
if (expected_length != actual_length) {
// ESP_LOGV(TAG, "Xiaomi %s data length mismatch (%u != %d)", type, expected_length, actual_length);
return {};
}
XiaomiParseResult result; XiaomiParseResult result;
result.type = XiaomiParseResult::TYPE_HHCCJCY01; result.type = XiaomiParseResult::TYPE_HHCCJCY01;
if (is_lywsdcgq) { if (is_lywsdcgq) {
@ -111,7 +100,42 @@ optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &d
} else if (is_cgg1) { } else if (is_cgg1) {
result.type = XiaomiParseResult::TYPE_CGG1; result.type = XiaomiParseResult::TYPE_CGG1;
} }
bool success = parse_xiaomi_data_byte(raw_type, data, data_length, result);
uint8_t raw_offset = is_lywsdcgq || is_cgg1 ? 11 : 12;
// Data point specs
// Byte 0: type
// Byte 1: fixed 0x10
// Byte 2: length
// Byte 3..3+len-1: data point value
const uint8_t *raw_data = &raw[raw_offset];
uint8_t data_offset = 0;
uint8_t data_length = device.get_service_data().size() - raw_offset;
bool success = false;
while (true) {
if (data_length < 4)
// at least 4 bytes required
// type, fixed 0x10, length, 1 byte value
break;
const uint8_t datapoint_type = raw_data[data_offset + 0];
const uint8_t datapoint_length = raw_data[data_offset + 2];
if (data_length < 3 + datapoint_length)
// 3 fixed bytes plus value length
break;
const uint8_t *datapoint_data = &raw_data[data_offset + 3];
if (parse_xiaomi_data_byte(datapoint_type, datapoint_data, datapoint_length, result))
success = true;
data_length -= data_offset + 3 + datapoint_length;
data_offset += 3 + datapoint_length;
}
if (!success) if (!success)
return {}; return {};
return result; return result;