-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.cpp
More file actions
207 lines (168 loc) · 5.87 KB
/
Copy pathprocessor.cpp
File metadata and controls
207 lines (168 loc) · 5.87 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
#include <M5Unified.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <mbedtls/ccm.h>
#include "processor.hpp"
template <typename T>
bool AdvertisementProcessor::verify_header(const T *p) const
{
return p->uid == BLE_HS_ADV_TYPE_SVC_DATA_UUID16 &&
p->UUID == ADV_CUSTOM_UUID16;
}
template <typename T_in, typename T_out>
bool AdvertisementProcessor::decrypt(T_out *target, const T_in *adv, const uint64_t &mac, const uint8_t *key) const
{
enc_beacon_nonce_t cbn;
// reverse MAC address
cbn.MAC[0] = (uint8_t)(mac >> 0);
cbn.MAC[1] = (uint8_t)(mac >> 8);
cbn.MAC[2] = (uint8_t)(mac >> 16);
cbn.MAC[3] = (uint8_t)(mac >> 24);
cbn.MAC[4] = (uint8_t)(mac >> 32);
cbn.MAC[5] = (uint8_t)(mac >> 40);
memcpy(&cbn.head, &adv->head, sizeof(cbn.head));
uint8_t aad = 0x11;
mbedtls_ccm_context ctx;
mbedtls_ccm_init(&ctx);
int ret = mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, 16 * 8);
if (ret)
{
M5_LOGE("mbedtls_ccm_setkey() failed: ret = %d", ret);
mbedtls_ccm_free(&ctx);
return false;
}
ret = mbedtls_ccm_auth_decrypt(&ctx, sizeof(adv->data),
(uint8_t *)&cbn, sizeof(cbn),
&aad, sizeof(aad),
(uint8_t *)&adv->data, (uint8_t *)target,
adv->mic, 4);
if (ret)
{
M5_LOGE("mbedtls_ccm_auth_decrypt() failed: ret = %d", ret);
mbedtls_ccm_free(&ctx);
return false;
}
mbedtls_ccm_free(&ctx);
return true;
}
bool AdvertisementProcessor::decode_adv_cust_enc_t(JsonDocument &doc, const padv_cust_enc_t payload, const uint64_t &mac, const uint8_t *key) const
{
if (!key)
{
M5_LOGE("no key configured to decrypt the payload for MAC %012llX", mac);
return false;
}
if (!verify_header(&payload->head))
return false;
M5_LOGV("decoding custom pvvx encrypted format");
adv_cust_data_t d;
if (!decrypt(&d, payload, mac, key))
{
M5_LOGE("could not decrypt the payload for MAC %012llX", mac);
return false;
}
doc[key_temp] = ((JsonFloat)d.temp) / 100;
doc[key_humi] = ((JsonFloat)d.humi) / 100;
doc[key_batt] = d.bat;
return true;
}
bool AdvertisementProcessor::decode_adv_atc_enc_t(JsonDocument &doc, const padv_atc_enc_t payload, uint64_t mac, const uint8_t *key) const
{
if (!key)
{
M5_LOGE("no key configured to decrypt the payload for MAC %012llX", mac);
return false;
}
if (!verify_header(&payload->head))
return false;
M5_LOGV("decoding atc1441 encrypted format");
adv_atc_data_t d;
if (!decrypt(&d, payload, mac, key))
{
M5_LOGE("could not decrypt the payload for MAC %012llX", mac);
return false;
}
doc[key_temp] = ((JsonFloat)d.temp) / 2 - 40;
doc[key_humi] = ((JsonFloat)d.humi) / 2;
doc[key_batt] = d.bat & 0x7f;
return true;
}
bool AdvertisementProcessor::decode_adv_custom_t(JsonDocument &doc, const padv_custom_t payload) const
{
if (!verify_header(payload))
return false;
M5_LOGV("decoding custom pvvx format");
doc[key_temp] = ((JsonFloat)payload->temperature) / 100;
doc[key_humi] = ((JsonFloat)payload->humidity) / 100;
doc[key_volt] = payload->battery_mv;
doc[key_batt] = payload->battery_level;
return true;
}
bool AdvertisementProcessor::decode_adv_atc1441_t(JsonDocument &doc, const padv_atc1441_t payload) const
{
if (!verify_header(payload))
return false;
M5_LOGV("decoding atc1441 format");
int16_t temp;
uint16_t volt;
std::reverse_copy(payload->temperature, payload->temperature + 2, (uint8_t *)&temp);
std::reverse_copy(payload->battery_mv, payload->battery_mv + 2, (uint8_t *)&volt);
doc[key_temp] = ((JsonFloat)temp) / 10;
doc[key_humi] = payload->humidity;
doc[key_volt] = volt;
doc[key_batt] = payload->battery_level;
return true;
}
AdvertisementProcessor::AdvertisementProcessor(PubSubClient &mqtt_client)
: mqtt_client(&mqtt_client), last_success(0) {}
void AdvertisementProcessor::onResult(NimBLEAdvertisedDevice *adv)
{
uint64_t mac = adv->getAddress();
auto entry = settings.ble.devices.find(mac);
if (entry == settings.ble.devices.end())
return;
size_t payloadLength = adv->getPayloadLength();
const uint8_t *payload = adv->getPayload();
// expecting exactly one advertisement data element
if (payloadLength == 0 || payload[0] != payloadLength - 1)
return;
bool success = false;
StaticJsonDocument<json_capacity> doc;
switch (payloadLength)
{
case sizeof(adv_cust_enc_t):
success = decode_adv_cust_enc_t(doc, (padv_cust_enc_t)payload, mac, entry->second.key);
break;
case sizeof(adv_atc_enc_t):
success = decode_adv_atc_enc_t(doc, (padv_atc_enc_t)payload, mac, entry->second.key);
break;
case sizeof(adv_custom_t):
success = decode_adv_custom_t(doc, (padv_custom_t)payload);
break;
case sizeof(adv_atc1441_t):
success = decode_adv_atc1441_t(doc, (padv_atc1441_t)payload);
break;
}
if (!success)
return;
time_t now;
time(&now);
doc[key_sensor] = entry->second.name;
doc[key_time] = now;
doc[key_rssi] = adv->getRSSI();
char json[512];
size_t json_length = serializeJson(doc, json);
if (mqtt_client->beginPublish(entry->second.mqtt_topic, json_length, false))
{
mqtt_client->write((uint8_t *)json, json_length);
mqtt_client->endPublish();
M5_LOGI("successfully published %s to MQTT topic %s", json, entry->second.mqtt_topic);
last_success = millis();
}
else
M5_LOGE("could not publish %s to MQTT topic %s", json, entry->second.mqtt_topic);
}
unsigned long AdvertisementProcessor::lastSuccess() const
{
return last_success;
}