forked from ct-Open-Source/Basecamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WifiControl.cpp
150 lines (129 loc) · 3.97 KB
/
WifiControl.cpp
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
/*
Basecamp - ESP32 library to simplify the basics of IoT projects
Written by Merlin Schumacher ([email protected]) for c't magazin für computer technik (https://www.ct.de)
Licensed under GPLv3. See LICENSE for details.
*/
#include "WifiControl.hpp"
namespace {
// Minumum access point secret length to be generated (8 is min for ESP32)
const constexpr unsigned minApSecretLength = 8;
}
void WifiControl::begin(String essid, String password, String configured,
String hostname, String apSecret)
{
DEBUG_PRINTLN("Connecting to Wifi");
String _wifiConfigured = std::move(configured);
_wifiEssid = std::move(essid);
_wifiPassword = std::move(password);
_wifiAPName = "ESP32_" + getHardwareMacAddress();
WiFi.onEvent(WiFiEvent);
if (_wifiConfigured == "True") {
operationMode_ = Mode::client;
DEBUG_PRINTLN("Wifi is configured");
DEBUG_PRINT("Connecting to ");
DEBUG_PRINTLN(_wifiEssid);
WiFi.begin(_wifiEssid.c_str(), _wifiPassword.c_str());
WiFi.setHostname(hostname.c_str());
//WiFi.setAutoConnect ( true );
//WiFi.setAutoReconnect ( true );
} else {
operationMode_ = Mode::accessPoint;
DEBUG_PRINTLN("Wifi is NOT configured");
DEBUG_PRINTF("Starting Wifi AP '%s'", _wifiAPName);
WiFi.mode(WIFI_AP_STA);
if (apSecret.length() > 0) {
// Start with password protection
DEBUG_PRINTF("Starting AP with password %s\n", apSecret.c_str());
WiFi.softAP(_wifiAPName.c_str(), apSecret.c_str());
} else {
// Start without password protection
WiFi.softAP(_wifiAPName.c_str());
}
}
}
WifiControl::Mode WifiControl::getOperationMode() const
{
return operationMode_;
}
int WifiControl::status() {
return WiFi.status();
}
IPAddress WifiControl::getIP() {
return WiFi.localIP();
}
IPAddress WifiControl::getSoftAPIP() {
return WiFi.softAPIP();
}
String WifiControl::getAPName() {
return _wifiAPName;
}
void WifiControl::WiFiEvent(WiFiEvent_t event)
{
Preferences preferences;
preferences.begin("basecamp", false);
unsigned int bootCounter = preferences.getUInt("bootcounter", 0);
// In case somebody wants to know this..
DEBUG_PRINTF("[WiFi-event] event. Bootcounter is %d\n", bootCounter);
DEBUG_PRINTF("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
DEBUG_PRINT("Wifi IP address: ");
DEBUG_PRINTLN(WiFi.localIP());
preferences.putUInt("bootcounter", 0);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
DEBUG_PRINTLN("WiFi lost connection");
WiFi.reconnect();
break;
default:
// INFO: Default = do nothing
break;
}
}
namespace {
template <typename BYTES>
String format6Bytes(const BYTES &bytes, const String& delimiter)
{
std::ostringstream stream;
for (unsigned int i = 0; i < 6; i++) {
if (i != 0 && delimiter.length() > 0) {
stream << delimiter.c_str();
}
stream << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(bytes[i]);
}
String mac{stream.str().c_str()};
return mac;
}
}
// TODO: This will return the default mac, not a manually set one
// See https://github.com/espressif/esp-idf/blob/master/components/esp32/include/esp_system.h
String WifiControl::getHardwareMacAddress(const String& delimiter)
{
uint8_t rawMac[6];
esp_efuse_mac_get_default(rawMac);
return format6Bytes(rawMac, delimiter);
}
String WifiControl::getSoftwareMacAddress(const String& delimiter)
{
uint8_t rawMac[6];
WiFi.macAddress(rawMac);
return format6Bytes(rawMac, delimiter);
}
unsigned WifiControl::getMinimumSecretLength() const
{
return minApSecretLength;
}
String WifiControl::generateRandomSecret(unsigned length) const
{
// There is no "O" (Oh) to reduce confusion
const String validChars{"abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789.-,:$/"};
String returnValue;
unsigned useLength = (length < minApSecretLength)?minApSecretLength:length;
returnValue.reserve(useLength);
for (unsigned i = 0; i < useLength; i++)
{
auto randomValue = validChars[(esp_random() % validChars.length())];
returnValue += randomValue;
}
return returnValue;
}