Repository navigation
Expand file tree
/
Copy pathDeviceIdentity.cpp
More file actions
92 lines (74 loc) · 2.26 KB
/
Copy pathDeviceIdentity.cpp
File metadata and controls
92 lines (74 loc) · 2.26 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
#include "DeviceIdentity.h"
#include <esp_mac.h>
namespace {
bool initialized = false;
String macHexValue;
String shortSuffixValue;
String hostnameValue;
String setupSsidValue;
String mqttDeviceIdValue;
String displayNameValue;
String legacyMqttDeviceIdValue;
void initialize() {
if (initialized) return;
uint8_t mac[6] = {};
if (esp_read_mac(mac, ESP_MAC_WIFI_STA) != ESP_OK) {
// A factory-programmed station MAC is present on production ESP32-S3
// devices. This fixed fallback keeps the failure visible instead of
// silently manufacturing an identity that could collide.
memset(mac, 0, sizeof(mac));
}
char full[13];
snprintf(full, sizeof(full), "%02x%02x%02x%02x%02x%02x", mac[0],
mac[1], mac[2], mac[3], mac[4], mac[5]);
macHexValue = full;
char shortSuffix[7];
snprintf(shortSuffix, sizeof(shortSuffix), "%02x%02x%02x", mac[3],
mac[4], mac[5]);
shortSuffixValue = shortSuffix;
hostnameValue = String("gc2-bridge-") + macHexValue;
setupSsidValue = String("GC2-Bridge-") + macHexValue;
setupSsidValue.toUpperCase();
mqttDeviceIdValue = String("gc2_bridge_") + macHexValue;
String displaySuffix = shortSuffixValue;
displaySuffix.toUpperCase();
displayNameValue = String("2GIG GC2 Bridge ") + displaySuffix;
const uint64_t legacyChipId = ESP.getEfuseMac();
char legacySuffix[7];
snprintf(legacySuffix, sizeof(legacySuffix), "%06llx",
static_cast<unsigned long long>(legacyChipId & 0xFFFFFFULL));
legacyMqttDeviceIdValue = String("gc2_bridge_") + legacySuffix;
initialized = true;
}
} // namespace
namespace DeviceIdentity {
void begin() { initialize(); }
const String& macHex() {
initialize();
return macHexValue;
}
const String& shortSuffix() {
initialize();
return shortSuffixValue;
}
const String& hostname() {
initialize();
return hostnameValue;
}
const String& setupSsid() {
initialize();
return setupSsidValue;
}
const String& mqttDeviceId() {
initialize();
return mqttDeviceIdValue;
}
const String& displayName() {
initialize();
return displayNameValue;
}
const String& legacyMqttDeviceId() {
initialize();
return legacyMqttDeviceIdValue;
}
} // namespace DeviceIdentity