Skip to content

Commit

Permalink
Switched API to std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Nov 12, 2024
1 parent 5f2fe85 commit d76ed5b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ espConnect.setIPConfig(ipConfig);
switch (state) {
case Mycila::ESPConnect::State::PORTAL_COMPLETE:
bool apMode = espConnect.hasConfiguredAPMode();
String wifiSSID = espConnect.getConfiguredWiFiSSID();
String wifiPassword = espConnect.getConfiguredWiFiPassword();
std::string wifiSSID = espConnect.getConfiguredWiFiSSID();
std::string wifiPassword = espConnect.getConfiguredWiFiPassword();
if (apMode) {
Serial.println("====> Captive Portal: Access Point configured");
} else {
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ espConnect.setIPConfig(ipConfig);
switch (state) {
case Mycila::ESPConnect::State::PORTAL_COMPLETE:
bool apMode = espConnect.hasConfiguredAPMode();
String wifiSSID = espConnect.getConfiguredWiFiSSID();
String wifiPassword = espConnect.getConfiguredWiFiPassword();
std::string wifiSSID = espConnect.getConfiguredWiFiSSID();
std::string wifiPassword = espConnect.getConfiguredWiFiPassword();
if (apMode) {
Serial.println("====> Captive Portal: Access Point configured");
} else {
Expand Down
4 changes: 2 additions & 2 deletions examples/AdvancedCaptivePortal/AdvancedCaptivePortal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ void setup() {
Preferences preferences;
preferences.begin("app", true);
Mycila::ESPConnect::Config config = {
.wifiSSID = preferences.isKey("ssid") ? preferences.getString("ssid", emptyString) : emptyString,
.wifiPassword = preferences.isKey("password") ? preferences.getString("password", emptyString) : emptyString,
.wifiSSID = (preferences.isKey("ssid") ? preferences.getString("ssid", emptyString) : emptyString).c_str(),
.wifiPassword = (preferences.isKey("password") ? preferences.getString("password", emptyString) : emptyString).c_str(),
.apMode = preferences.isKey("ap") ? preferences.getBool("ap", false) : false};
preferences.end();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
AsyncWebServer server(80);
Mycila::ESPConnect espConnect(server);
uint32_t lastLog = 0;
String hostname = "arduino-1";
const char* hostname = "arduino-1";

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -63,7 +63,7 @@ void setup() {

Serial.println("====> Trying to connect to saved WiFi or will start portal in the background...");

espConnect.begin(hostname.c_str(), "Captive Portal SSID");
espConnect.begin(hostname, "Captive Portal SSID");

Serial.println("====> setup() completed...");
}
Expand Down
4 changes: 2 additions & 2 deletions examples/WiFiStaticIP/WiFiStaticIP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AsyncWebServer server(80);
Mycila::ESPConnect espConnect(server);
uint32_t lastLog = 0;
uint32_t lastChange = 0;
String hostname = "arduino-1";
const char* hostname = "arduino-1";

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -50,7 +50,7 @@ void setup() {

Serial.println("====> Trying to connect to saved WiFi or will start portal in the background...");

espConnect.begin(hostname.c_str(), "Captive Portal SSID");
espConnect.begin(hostname, "Captive Portal SSID");

Serial.println("====> setup() completed...");
}
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
default_envs = arduino-2, arduino-3, arduino-310, esp8266, esp32-poe-arduino-2, esp32-poe-arduino-3, esp32-poe-arduino-310, wt32-eth01-arduino-2, wt32-eth01-arduino-3, wt32-eth01-arduino-310, lilygo-eth-lite-s3-arduino-2, lilygo-eth-lite-s3-arduino-3, lilygo-eth-lite-s3-arduino-310
lib_dir = .
; src_dir = examples/BlockingCaptivePortal
; src_dir = examples/NonBlockingCaptivePortal
src_dir = examples/AdvancedCaptivePortal
src_dir = examples/NonBlockingCaptivePortal
; src_dir = examples/AdvancedCaptivePortal
; src_dir = examples/TestWiFi8266
; src_dir = examples/WiFiStaticIP

Expand Down
75 changes: 42 additions & 33 deletions src/MycilaESPConnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "MycilaESPConnect.h"

#include <cstdio>
#include <string>

#ifdef ESP8266
#ifndef ESPCONNECT_NO_MDNS
Expand Down Expand Up @@ -135,38 +136,38 @@ Mycila::ESPConnect::Mode Mycila::ESPConnect::getMode() const {
}
}

const String Mycila::ESPConnect::getMACAddress(Mycila::ESPConnect::Mode mode) const {
String mac;
std::string Mycila::ESPConnect::getMACAddress(Mycila::ESPConnect::Mode mode) const {
std::string mac;

switch (mode) {
case Mycila::ESPConnect::Mode::AP:
mac = WiFi.softAPmacAddress();
mac = WiFi.softAPmacAddress().c_str();
break;
case Mycila::ESPConnect::Mode::STA:
mac = WiFi.macAddress();
mac = WiFi.macAddress().c_str();
break;
#ifdef ESPCONNECT_ETH_SUPPORT
case Mycila::ESPConnect::Mode::ETH:
mac = ETH.linkUp() ? ETH.macAddress() : emptyString;
if (ETH.linkUp())
mac = ETH.macAddress().c_str();
break;
#endif
default:
mac = emptyString;
break;
}

if (mac != emptyString && mac != "00:00:00:00:00:00")
if (!mac.empty() && mac != "00:00:00:00:00:00")
return mac;

#ifdef ESP8266
switch (mode) {
case Mycila::ESPConnect::Mode::AP:
return WiFi.softAPmacAddress();
return WiFi.softAPmacAddress().c_str();
case Mycila::ESPConnect::Mode::STA:
return WiFi.macAddress();
return WiFi.macAddress().c_str();
default:
// ETH not supported with ESP8266
return emptyString;
return {};
}
#else
// ESP_MAC_IEEE802154 is used to mean "no MAC address" in this context
Expand All @@ -189,19 +190,19 @@ const String Mycila::ESPConnect::getMACAddress(Mycila::ESPConnect::Mode mode) co
}

if (type == esp_mac_type_t::ESP_MAC_IEEE802154)
return emptyString;
return "";

uint8_t bytes[6] = {0, 0, 0, 0, 0, 0};
if (esp_read_mac(bytes, type) != ESP_OK)
return emptyString;
return "";

char buffer[18] = {0};
snprintf(buffer, sizeof(buffer), "%02X:%02X:%02X:%02X:%02X:%02X", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]);
return buffer;
#endif
}

const IPAddress Mycila::ESPConnect::getIPAddress(Mycila::ESPConnect::Mode mode) const {
IPAddress Mycila::ESPConnect::getIPAddress(Mycila::ESPConnect::Mode mode) const {
const wifi_mode_t wifiMode = WiFi.getMode();
switch (mode) {
case Mycila::ESPConnect::Mode::AP:
Expand All @@ -217,27 +218,27 @@ const IPAddress Mycila::ESPConnect::getIPAddress(Mycila::ESPConnect::Mode mode)
}
}

const String& Mycila::ESPConnect::getWiFiSSID() const {
std::string Mycila::ESPConnect::getWiFiSSID() const {
switch (WiFi.getMode()) {
case WIFI_MODE_AP:
case WIFI_MODE_APSTA:
return _apSSID;
case WIFI_MODE_STA:
return _config.wifiSSID;
default:
return emptyString;
return {};
}
}

const String Mycila::ESPConnect::getWiFiBSSID() const {
std::string Mycila::ESPConnect::getWiFiBSSID() const {
switch (WiFi.getMode()) {
case WIFI_MODE_AP:
case WIFI_MODE_APSTA:
return WiFi.softAPmacAddress();
return WiFi.softAPmacAddress().c_str();
case WIFI_MODE_STA:
return WiFi.BSSIDstr();
return WiFi.BSSIDstr().c_str();
default:
return emptyString;
return {};
}
}

Expand All @@ -263,8 +264,12 @@ void Mycila::ESPConnect::begin(const char* hostname, const char* apSSID, const c
LOGD(TAG, "Loading config...");
Preferences preferences;
preferences.begin("espconnect", true);
String ssid = preferences.isKey("ssid") ? preferences.getString("ssid", emptyString) : emptyString;
String password = preferences.isKey("password") ? preferences.getString("password", emptyString) : emptyString;
std::string ssid;
std::string password;
if (preferences.isKey("ssid"))
ssid = preferences.getString("ssid").c_str();
if (preferences.isKey("password"))
password = preferences.getString("password").c_str();
bool ap = preferences.isKey("ap") ? preferences.getBool("ap", false) : false;
preferences.end();
LOGD(TAG, " - AP: %d", ap);
Expand Down Expand Up @@ -338,7 +343,7 @@ void Mycila::ESPConnect::loop() {
// start captive portal when network enabled but not in ap mode and no wifi info and no ethernet
// portal wil be interrupted when network connected
#ifndef ESPCONNECT_ETH_SUPPORT
if (_state == Mycila::ESPConnect::State::NETWORK_ENABLED && _config.wifiSSID.isEmpty()) {
if (_state == Mycila::ESPConnect::State::NETWORK_ENABLED && _config.wifiSSID.empty()) {
_startAP();
}
#endif
Expand All @@ -348,7 +353,7 @@ void Mycila::ESPConnect::loop() {
#ifdef ESPCONNECT_ETH_SUPPORT
_startEthernet();
#endif
if (!_config.wifiSSID.isEmpty())
if (!_config.wifiSSID.empty())
_startSTA();
}

Expand All @@ -368,7 +373,7 @@ void Mycila::ESPConnect::loop() {

// timeout portal if we failed to connect to WiFi (we got a SSID) and portal duration is passed
// in order to restart and try again to connect to the configured WiFi
if (_state == Mycila::ESPConnect::State::PORTAL_STARTED && !_config.wifiSSID.isEmpty() && _durationPassed(_portalTimeout)) {
if (_state == Mycila::ESPConnect::State::PORTAL_STARTED && !_config.wifiSSID.empty() && _durationPassed(_portalTimeout)) {
_setState(Mycila::ESPConnect::State::PORTAL_TIMEOUT);
}

Expand Down Expand Up @@ -522,7 +527,7 @@ void Mycila::ESPConnect::_startSTA() {
#endif

LOGD(TAG, "Connecting to SSID: %s...", _config.wifiSSID.c_str());
WiFi.begin(_config.wifiSSID, _config.wifiPassword);
WiFi.begin(_config.wifiSSID.c_str(), _config.wifiPassword.c_str());

_lastTime = millis();

Expand All @@ -548,11 +553,11 @@ void Mycila::ESPConnect::_startAP() {

WiFi.mode(_config.apMode ? WIFI_AP : WIFI_AP_STA);

if (_apPassword.isEmpty() || _apPassword.length() < 8) {
if (_apPassword.empty() || _apPassword.length() < 8) {
// Disabling invalid Access Point password which must be at least 8 characters long when set
WiFi.softAP(_apSSID, emptyString);
WiFi.softAP(_apSSID.c_str(), "");
} else
WiFi.softAP(_apSSID, _apPassword);
WiFi.softAP(_apSSID.c_str(), _apPassword.c_str());

if (_dnsServer == nullptr) {
_dnsServer = new DNSServer();
Expand Down Expand Up @@ -628,16 +633,20 @@ void Mycila::ESPConnect::_enableCaptivePortal() {

if (_connectHandler == nullptr) {
_connectHandler = &_httpd->on("/espconnect/connect", HTTP_POST, [&](AsyncWebServerRequest* request) {
_config.apMode = (request->hasParam("ap_mode", true) ? request->getParam("ap_mode", true)->value() : emptyString) == "true";
_config.apMode = request->hasParam("ap_mode", true) && request->getParam("ap_mode", true)->value() == "true";
if (_config.apMode) {
request->send(200, "application/json", "{\"message\":\"Configuration Saved.\"}");
_setState(Mycila::ESPConnect::State::PORTAL_COMPLETE);
} else {
String ssid = request->hasParam("ssid", true) ? request->getParam("ssid", true)->value() : emptyString;
String password = request->hasParam("password", true) ? request->getParam("password", true)->value() : emptyString;
if (ssid.isEmpty())
std::string ssid;
std::string password;
if (request->hasParam("ssid", true))
ssid = request->getParam("ssid", true)->value().c_str();
if (request->hasParam("password", true))
password = request->getParam("password", true)->value().c_str();
if (ssid.empty())
return request->send(400, "application/json", "{\"message\":\"Invalid SSID\"}");
if (ssid.length() > 32 || password.length() > 64 || (!password.isEmpty() && password.length() < 8))
if (ssid.length() > 32 || password.length() > 64 || (!password.empty() && password.length() < 8))
return request->send(400, "application/json", "{\"message\":\"Credentials exceed character limit of 32 & 64 respectively, or password lower than 8 characters.\"}");
_config.wifiSSID = ssid;
_config.wifiPassword = password;
Expand Down
36 changes: 19 additions & 17 deletions src/MycilaESPConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <DNSServer.h>
#include <ESPAsyncWebServer.h>

#include <string>

#define ESPCONNECT_VERSION "6.1.1"
#define ESPCONNECT_VERSION_MAJOR 6
#define ESPCONNECT_VERSION_MINOR 1
Expand Down Expand Up @@ -83,9 +85,9 @@ namespace Mycila {

typedef struct {
// SSID name to connect to, loaded from config or set from begin(), or from the captive portal
String wifiSSID;
std::string wifiSSID;
// Password for the WiFi to connect to, loaded from config or set from begin(), or from the captive portal
String wifiPassword;
std::string wifiPassword;
// whether we need to set the ESP to stay in AP mode or not, loaded from config, begin(), or from captive portal
bool apMode;
} Config;
Expand Down Expand Up @@ -133,36 +135,36 @@ namespace Mycila {

bool isConnected() const { return getIPAddress()[0] != 0; }

const String getMACAddress() const { return getMACAddress(getMode()); }
const String getMACAddress(Mode mode) const;
std::string getMACAddress() const { return getMACAddress(getMode()); }
std::string getMACAddress(Mode mode) const;

// Returns the IP address of the current Ethernet, WiFi, or IP address of the AP or captive portal, or empty if not available
const IPAddress getIPAddress() const { return getIPAddress(getMode()); }
const IPAddress getIPAddress(Mode mode) const;
IPAddress getIPAddress() const { return getIPAddress(getMode()); }
IPAddress getIPAddress(Mode mode) const;

// Returns the SSID of the current WiFi, or SSID of the AP or captive portal, or empty if not available
const String& getWiFiSSID() const;
// Returns the configured WiFi SSID or the configured SSID of the AP or captive portal, or empty if not available, depending on the current mode
std::string getWiFiSSID() const;
// Returns the BSSID of the current WiFi, or BSSID of the AP or captive portal, or empty if not available
const String getWiFiBSSID() const;
std::string getWiFiBSSID() const;
// Returns the RSSI of the current WiFi, or -1 if not available
int8_t getWiFiRSSI() const;
// Returns the signal quality (percentage from 0 to 100) of the current WiFi, or -1 if not available
int8_t getWiFiSignalQuality() const;

// the hostname passed from begin()
const String& getHostname() const { return _hostname; }
const std::string& getHostname() const { return _hostname; }

// SSID name used for the captive portal or in AP mode
const String& getAccessPointSSID() const { return _apSSID; }
const std::string& getAccessPointSSID() const { return _apSSID; }
// Password used for the captive portal or in AP mode
const String& getAccessPointPassword() const { return _apPassword; }
const std::string& getAccessPointPassword() const { return _apPassword; }

// Returns the current configuration loaded or passed from begin() or from captive portal
const Config& getConfig() const { return _config; }
// SSID name to connect to, loaded from config or set from begin(), or from the captive portal
const String& getConfiguredWiFiSSID() const { return _config.wifiSSID; }
const std::string& getConfiguredWiFiSSID() const { return _config.wifiSSID; }
// Password for the WiFi to connect to, loaded from config or set from begin(), or from the captive portal
const String& getConfiguredWiFiPassword() const { return _config.wifiPassword; }
const std::string& getConfiguredWiFiPassword() const { return _config.wifiPassword; }
// whether we need to set the ESP to stay in AP mode or not, loaded from config, begin(), or from captive portal
bool hasConfiguredAPMode() const { return _config.apMode; }

Expand Down Expand Up @@ -203,9 +205,9 @@ namespace Mycila {
StateCallback _callback = nullptr;
DNSServer* _dnsServer = nullptr;
int64_t _lastTime = -1;
String _hostname = emptyString;
String _apSSID = emptyString;
String _apPassword = emptyString;
std::string _hostname;
std::string _apSSID;
std::string _apPassword;
uint32_t _connectTimeout = ESPCONNECT_CONNECTION_TIMEOUT;
uint32_t _portalTimeout = ESPCONNECT_PORTAL_TIMEOUT;
Config _config;
Expand Down

0 comments on commit d76ed5b

Please sign in to comment.