-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.cpp
More file actions
126 lines (113 loc) · 3.32 KB
/
Copy pathsettings.cpp
File metadata and controls
126 lines (113 loc) · 3.32 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
#if __has_include("settings-private.hpp")
#include "settings-private.hpp"
#endif
#include <cstring>
#include <M5Unified.h>
#include <NimBLEAddress.h>
#include <esp_mac.h>
#include "settings.hpp"
#ifndef WIFI_SSID
#define WIFI_SSID NULL
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD NULL
#endif
#ifndef MQTT_SERVER
#define MQTT_SERVER NULL
#endif
#ifndef MQTT_PORT
#define MQTT_PORT 1883
#endif
#ifndef MQTT_USER
#define MQTT_USER NULL
#endif
#ifndef MQTT_PASSWORD
#define MQTT_PASSWORD NULL
#endif
#ifndef MQTT_CLIENT_NAME_PREFIX
#define MQTT_CLIENT_NAME_PREFIX "M5Stack AtomS3 Lite"
#endif
#ifndef MQTT_TOPIC_PREFIX
#define MQTT_TOPIC_PREFIX "m5stack_atoms3lite_mitemperature"
#endif
#ifndef DEVICES
#define DEVICES \
{ \
}
#endif
Settings::Settings()
: watchdog_timer_s(60) {}
Settings::Time::Time()
: ntpServer("pool.ntp.org") {}
Settings::Wifi::Wifi()
: ssid(WIFI_SSID),
password(WIFI_PASSWORD) {}
Settings::Mqtt::Mqtt()
: server(MQTT_SERVER),
port(MQTT_PORT),
user(MQTT_USER),
password(MQTT_PASSWORD),
client_name(getClientName(MQTT_CLIENT_NAME_PREFIX)),
topic_prefix(MQTT_TOPIC_PREFIX),
status_interval(60),
status_topic_name(getStatusTopicName(MQTT_TOPIC_PREFIX)),
reconnect(3) {}
char *Settings::Mqtt::getClientName(const char *prefix) const
{
uint8_t mac[6];
char *name;
esp_read_mac(mac, ESP_MAC_WIFI_STA);
asprintf(&name, "%s %02X:%02X:%02X:%02X:%02X:%02X",
prefix, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return name;
}
char *Settings::Mqtt::getStatusTopicName(const char *prefix) const
{
char *name;
asprintf(&name, "%s/status", prefix);
return name;
}
Settings::Led::Led()
: wifi_off(0x800000u),
wifi_on_mqtt_off(0xC08000u),
mqtt_on(0x008000u),
ble(0x0000ffu) {}
Settings::Ble::Ble()
: devices(convert_devices(DEVICES)) {}
std::map<uint64_t, Settings::Ble::device_t> Settings::Ble::convert_devices(const std::vector<Settings::Ble::device_def_t> devices) const
{
std::map<uint64_t, device_t> m;
for (auto d : devices)
{
uint64_t mac = NimBLEAddress(d.mac);
if (mac == 0)
{
M5_LOGE("invalid MAC address: %s", d.mac);
continue;
}
uint8_t *device_key = nullptr;
char *device_name,
*device_mqtt_topic;
if (d.key != nullptr)
{
uint8_t key[16];
if (d.key != nullptr &&
std::strlen(d.key) == 32 &&
sscanf(d.key,
"%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
&key[0], &key[1], &key[2], &key[3], &key[4], &key[5], &key[6], &key[7],
&key[8], &key[9], &key[10], &key[11], &key[12], &key[13], &key[14], &key[15]) == 16)
memcpy(device_key = (uint8_t *)malloc(16), key, 16);
else
M5_LOGI("invalid decryption key: %s", d.key);
}
if (d.name != nullptr)
asprintf(&device_name, "%s", d.name);
else
asprintf(&device_name, "%06X", mac & 0xffffffu);
asprintf(&device_mqtt_topic, "%s/%s", settings.mqtt.topic_prefix, device_name);
device_t device{device_key, device_name, device_mqtt_topic};
m[mac] = device;
}
return m;
}