-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmiot.cpp
More file actions
242 lines (214 loc) · 7.71 KB
/
miot.cpp
File metadata and controls
242 lines (214 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "esphome/core/log.h"
#include "mbedtls/ccm.h"
#include "inttypes.h"
#include "miot.h"
#include "miot_decrypt.h"
namespace esphome {
namespace miot {
static const char *const TAG = "miot";
static inline const char *get_signal_bars(int rssi) {
if (rssi >= -50) {
return "excellent";
}
if (rssi >= -65) {
return "good";
}
if (rssi >= -85) {
return "normal";
}
return "poor";
}
bool MiBeaconTracker::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
bool processed = false;
for (auto &service_data : device.get_service_datas()) {
if (service_data.uuid.contains(0x95, 0xFE)) {
return this->parse_mibeacon_(device, service_data.data);
}
}
return false;
}
bool MiBeaconTracker::parse_mibeacon_(const esp32_ble_tracker::ESPBTDevice &device,
const std::vector<uint8_t> &raw) const {
if (raw.size() < sizeof(RawMiBeaconHeader)) {
ESP_LOGW(TAG, "Invalid MiBeacon data size: %s", format_hex_pretty(raw).c_str());
return false;
}
auto mib = MiBeacon(reinterpret_cast<const RawMiBeaconHeader *>(raw.data()));
if (mib.frame_control.mesh) {
// ESP_LOGW(TAG, "Device data is a mesh type device: %s", format_hex_pretty(raw).c_str());
return false;
}
if (mib.frame_control.version < 2) {
ESP_LOGW(TAG, "MiBeacon is using old data format %" PRIu8 ": %s", mib.frame_control.version,
format_hex_pretty(raw).c_str());
return false;
}
auto data = raw.data() + sizeof(RawMiBeaconHeader);
if (mib.frame_control.mac_include) {
mib.mac_address =
(*reinterpret_cast<const uint64_t *>(data)) & 0x0000FFFFFFFFFFFFul; // mac address 6 bytes + 2 bytes for uint64
data = data + sizeof(esp_bd_addr_t);
if (mib.mac_address != device.address_uint64()) {
MIOT_LOGW(TAG, "Device MAC address doesn't match mibeacon MAC address %12" PRIX64, device.address_uint64(),
mib.mac_address);
return false;
}
} else {
mib.mac_address = device.address_uint64();
}
if (mib.frame_control.capability_include) {
mib.capability = *reinterpret_cast<const Capability *>(data);
data = data + sizeof(Capability);
if (mib.capability.io) {
mib.io_capability = *reinterpret_cast<const IOCapability *>(data);
data = data + sizeof(IOCapability);
}
}
if (mib.frame_control.object_include) {
if (mib.frame_control.is_encrypted) {
const uint8_t *end = raw.data() + raw.size();
const uint8_t *mic;
if (mib.frame_control.version > 3) {
mic = end - sizeof(uint32_t);
mib.message_integrity_check = *reinterpret_cast<const uint32_t *>(mic);
} else {
mic = end - sizeof(uint8_t);
mib.message_integrity_check = *reinterpret_cast<const uint8_t *>(mic);
}
const uint8_t *rnd = mic - sizeof(uint32_t);
// random_number combined with frame_counter to become a 4-byte Counter for anti-replay
mib.random_number = ((*reinterpret_cast<const uint32_t *>(rnd)) & 0xFFFFFF00) | mib.frame_counter;
rnd++; // random number size is 3 bytes length, so add one.
mib.object = BLEObject(data, rnd);
} else {
mib.object = BLEObject((RawBLEObject *) data);
}
}
// parsing done. now processing...
bool processed = false;
for (auto listener : this->listeners_) {
if (device.address_uint64() != listener->get_address()) {
continue;
}
if (mib.is_encrypted() && mib.object.id == MIID_UNKNOWN) {
if (!listener->has_bindkey()) {
MIOT_LOGW(TAG, "[%04X] Object is encrypted but bindkey is not configured", listener->get_address(),
listener->get_product_id());
continue;
}
if (!decrypt_mibeacon(mib, listener->get_bindkey())) {
continue;
}
}
if (listener->process_mibeacon(mib)) {
processed = true;
}
}
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
if (!processed) {
ESP_LOGD(TAG, "Got MiBeacon: %s", format_hex_pretty(raw).c_str());
const int rssi = device.get_rssi();
ESP_LOGD(TAG, " %s [%04X]%s %s RSSI=%d (%s)", device.get_name().c_str(), mib.product_id,
mib.is_encrypted() ? " (encrypted)" : "", device.address_str().c_str(), rssi, get_signal_bars(rssi));
if (mib.frame_control.solicited) {
ESP_LOGD(TAG, " Requesting for registration and binding");
}
if (mib.has_object()) {
if (mib.is_encrypted() && mib.object.id == MIID_UNKNOWN) {
ESP_LOGD(TAG, " Data: %s", format_hex_pretty(mib.object.data).c_str());
} else {
ESP_LOGD(TAG, " Object[%04X]: %s", mib.object.id, format_hex_pretty(mib.object.data).c_str());
}
}
}
#endif
return true;
}
bool MiotListener::process_mibeacon(const MiBeacon &mib) {
if (this->product_id_ != 0 && this->product_id_ != mib.product_id) {
return false;
}
if (this->frame_counter_ == mib.frame_counter) {
MIOT_LOGV(TAG, "[%04X] Duplicate data packet received: %" PRIu8, this->address_, mib.product_id, mib.frame_counter);
return true;
}
this->frame_counter_ = mib.frame_counter;
if (!mib.has_object()) {
return false;
}
MIOT_LOGV(TAG, "[%04X] Processing Object[%04X]: %s", this->address_, mib.product_id, mib.object.id,
format_hex_pretty(mib.object.data).c_str());
return this->process_object_(mib.object);
}
bool MiotListener::has_bindkey() const {
for (uint8_t b : this->bindkey_) {
if (b != 0) {
return true;
}
}
return false;
}
bool MiotListener::process_unhandled_(const miot::BLEObject &obj) {
std::string s;
if (obj.data.size() == 1) {
s = str_sprintf("%u (0x%x)", obj.data[0], obj.data[0]);
} else {
s = format_hex_pretty(obj.data);
}
MIOT_LOGW(TAG, "[%04X] Unhandled object attribute: %04X, value: %s", this->address_, this->product_id_, obj.id,
s.c_str());
return false;
}
// Convert battery level to voltage.
static float battery_to_voltage(uint32_t battery_level) {
// 2200 mV - 0%
const uint32_t min_voltage = 2200;
// 3100 mV - 100%
const uint32_t max_voltage = 3100;
return (min_voltage + (max_voltage - min_voltage) / 100 * battery_level) * 0.001f;
}
bool MiotComponent::process_default_(const miot::BLEObject &obj) {
switch (obj.id) {
case MIID_BATTERY:
if (this->battery_level_ || this->battery_voltage_) {
auto battery_level = obj.get_battery_level();
if (battery_level.has_value()) {
if (this->battery_level_) {
this->battery_level_->publish_state(*battery_level);
}
if (this->battery_voltage_) {
this->battery_voltage_->publish_state(battery_to_voltage(*battery_level));
}
}
}
break;
case MIID_MIAOMIAOCE_BATTERY_1003:
if (this->battery_level_ || this->battery_voltage_) {
auto battery_level = obj.get_miaomiaoce_battery_level_1003();
if (battery_level.has_value()) {
if (this->battery_level_) {
this->battery_level_->publish_state(*battery_level);
}
if (this->battery_voltage_) {
this->battery_voltage_->publish_state(battery_to_voltage(*battery_level));
}
}
}
break;
default:
return this->process_unhandled_(obj);
}
return true;
}
void MiotComponent::dump_config_(const char *TAG, const char *product_code) const {
ESP_LOGCONFIG(TAG, "Xiaomi %s", product_code);
const uint8_t *mac = mac_reverse(this->address_);
ESP_LOGCONFIG(TAG, " MAC: " MIOT_ADDR_STR, MIOT_ADDR_HEX_REVERSE(mac));
if (this->has_bindkey()) {
ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty(this->bindkey_, sizeof(bindkey_t)).c_str());
}
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
}
} // namespace miot
} // namespace esphome