Skip to content

Commit

Permalink
Cleanup the destruction flow
Browse files Browse the repository at this point in the history
  • Loading branch information
cziter15 committed Feb 15, 2025
1 parent eb71566 commit 1b1060b
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 62 deletions.
10 changes: 5 additions & 5 deletions src/ksf/comp/ksDevicePortal.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ namespace ksf::comps
*/
ksDevicePortal();

/*!
@brief Destructs the Device Portal component.
*/
virtual ~ksDevicePortal();

/*!
@brief Constructs the Device Portal component with custom password.
Expand Down Expand Up @@ -225,10 +230,5 @@ namespace ksf::comps
@return True on success, false otherwise.
*/
bool loop(ksApplication* app) override;

/*!
@brief Destructs the Device Portal component.
*/
virtual ~ksDevicePortal();
};
}
10 changes: 5 additions & 5 deletions src/ksf/comp/ksLed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace ksf::comps
bitflags.driveAsPushPull = driveAsPushPull;
}

ksLed::~ksLed()
{
pinMode(pin, INPUT);
}

bool ksLed::init(ksApplication* owner)
{
if (!bitflags.driveAsPushPull)
Expand Down Expand Up @@ -85,9 +90,4 @@ namespace ksf::comps
else
digitalWrite(pin, (bitflags.activeLow ? !enabled : enabled) ? HIGH : LOW);
}

ksLed::~ksLed()
{
pinMode(pin, INPUT);
}
}
10 changes: 5 additions & 5 deletions src/ksf/comp/ksLed.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ namespace ksf::comps
*/
ksLed(uint8_t pin, bool activeLow = false, bool driveAsPushPull = false);

/*!
@brief Destructs the component and restores INPUT mode on the assigned pin.
*/
virtual ~ksLed();

/*!
@brief Initializes the LED component.
@param owner Pointer to parent ksApplication object.
Expand Down Expand Up @@ -95,10 +100,5 @@ namespace ksf::comps
@return Pin number assigned to LED.
*/
uint8_t getPin() const { return pin; }

/*!
@brief Destructs the component and restores INPUT mode on the assigned pin.
*/
virtual ~ksLed();
};
}
2 changes: 2 additions & 0 deletions src/ksf/comp/ksMqttConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace ksf::comps
bitflags.usePersistentSession = usePersistentSession;
}

ksMqttConnector::~ksMqttConnector() = default;

bool ksMqttConnector::init(ksApplication* app)
{
ksMqttConfigProvider cfgProvider;
Expand Down
5 changes: 5 additions & 0 deletions src/ksf/comp/ksMqttConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ namespace ksf::comps
*/
ksMqttConnector(bool sendConnectionStatus = true, bool usePersistentSession = false);

/*!
@brief Destructs ksMqttConnector object.
*/
virtual ~ksMqttConnector();

/*!
@brief Instantiates the MQTT connector component.
Expand Down
10 changes: 5 additions & 5 deletions src/ksf/comp/ksWifiConfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ namespace ksf::comps
deviceName += ksf::getDeviceUuidHex();
}

ksWifiConfigurator::~ksWifiConfigurator()
{
WiFi.softAPdisconnect(true);
}

bool ksWifiConfigurator::init(ksApplication* app)
{
WiFi.softAP(deviceName.c_str());
Expand Down Expand Up @@ -75,4 +70,9 @@ namespace ksf::comps

return !configTimeout.triggered();
}

ksWifiConfigurator::~ksWifiConfigurator()
{
WiFi.softAPdisconnect(true);
}
}
12 changes: 7 additions & 5 deletions src/ksf/misc/ksCertUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
*/

#include <WiFiClientSecure.h>

#include "ksCertUtils.h"
#include "../ksConstants.h"

namespace ksf::misc
{
/*
Expand All @@ -30,6 +31,7 @@ namespace ksf::misc
return 255;
}

ksCertFingerprint::ksCertFingerprint() = default;
ksCertFingerprint::~ksCertFingerprint() = default;

bool ksCertFingerprint::fingerprintToBytes(const std::string& fingerprint, uint8_t * bytes, uint8_t bytesLen) const
Expand All @@ -52,7 +54,7 @@ namespace ksf::misc
}

#ifdef ESP32
bool ksCertFingerprintESP32::setup(WiFiClientSecure* clientSecure, const std::string& fingerprint)
bool ksCertFingerprintESP32::setup(ksCertUtilsNetCLientSecure_t* clientSecure, const std::string& fingerprint)
{
if (fingerprintToBytes(fingerprint, fingerprintBytes, sizeof(fingerprintBytes)))
{
Expand All @@ -64,7 +66,7 @@ namespace ksf::misc
return false;
}

bool ksCertFingerprintESP32::verify(WiFiClientSecure* client) const
bool ksCertFingerprintESP32::verify(ksCertUtilsNetCLientSecure_t* client) const
{
static const char characters[] {"0123456789ABCDEF"};

Expand All @@ -82,7 +84,7 @@ namespace ksf::misc
#endif

#ifdef ESP8266
bool ksCertFingerprintESP8266::setup(WiFiClientSecure* clientSecure, const std::string& fingerprint)
bool ksCertFingerprintESP8266::setup(ksCertUtilsNetCLientSecure_t* clientSecure, const std::string& fingerprint)
{
uint8_t fingerprintBytes[20];
if (fingerprintToBytes(fingerprint, fingerprintBytes, sizeof(fingerprintBytes)))
Expand All @@ -94,7 +96,7 @@ namespace ksf::misc
return false;
}

bool ksCertFingerprintESP8266::verify(WiFiClientSecure* client) const
bool ksCertFingerprintESP8266::verify(ksCertUtilsNetCLientSecure_t* client) const
{
return true;
}
Expand Down
38 changes: 25 additions & 13 deletions src/ksf/misc/ksCertUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

#include <string>
#include <stdint.h>
#include <WiFiClientSecure.h>

#if (defined(ESP32) && ESP_ARDUINO_VERSION_MAJOR >= 3)
#define ksCertUtilsNetCLientSecure_t NetworkClientSecure
#else
#define ksCertUtilsNetCLientSecure_t WiFiClientSecure
#endif

class ksCertUtilsNetCLientSecure_t;

namespace ksf::misc
{
Expand All @@ -34,25 +41,30 @@ namespace ksf::misc
bool fingerprintToBytes(const std::string& fingerprint, uint8_t* bytes, uint8_t bytesLen) const;

public:
/*!
@brief Constructor.
*/
ksCertFingerprint();

/*!
@brief Destructor.
*/
virtual ~ksCertFingerprint();

/*!
@brief Performs certificate fingerprint setup (platform dependent).
@param client Pointer of WiFiClientSecure
@param client Pointer of ksCertUtilsNetCLientSecure_t
@param fingerprint Fingerprint string
@return True if setup has been successfull, otherwise false.
*/
virtual bool setup(WiFiClientSecure* client, const std::string& fingerprint);
virtual bool setup(ksCertUtilsNetCLientSecure_t* client, const std::string& fingerprint) = 0;

/*!
@brief Performs certificate fingerprint validation (platform dependent).
@param client Pointer of WiFiClientSecure
@param client Pointer of ksCertUtilsNetCLientSecure_t
@return True on verification pass, otherwise false.
*/
virtual bool verify(WiFiClientSecure* client) const;
virtual bool verify(ksCertUtilsNetCLientSecure_t* client) const = 0;
};

#ifdef ESP32
Expand All @@ -67,18 +79,18 @@ namespace ksf::misc
On ESP32 it will copy fingerprint into fingerprintBytes.
@param client Pointer to WiFiClientSecure
@param client Pointer to ksCertUtilsNetCLientSecure_t
@param fingerprint Fingerprint string
@return True if setup passed, otherwise false.
*/
bool setup(WiFiClientSecure* client, const std::string& fingerprint) override;
bool setup(ksCertUtilsNetCLientSecure_t* client, const std::string& fingerprint) override;

/*!
@brief Converts bytes into a string and performs fingerprint validation.
@param client Pointer to WiFiClientSecure
@param client Pointer to ksCertUtilsNetCLientSecure_t
@return True if verification passed, otherwise false.
*/
bool verify(WiFiClientSecure* client) const override;
bool verify(ksCertUtilsNetCLientSecure_t* client) const override;
};

using ksCertFingerprintHolder = ksCertFingerprintESP32;
Expand All @@ -90,21 +102,21 @@ namespace ksf::misc
public:
/*!
@brief Performs fingerprint setup (platform dependent).
@param client Pointer of WiFiClientSecure.
@param client Pointer of ksCertUtilsNetCLientSecure_t.
@param fingerprint Fingerprint string.
@return True if setup passed, otherwise false.
*/
bool setup(WiFiClientSecure* client, const std::string& fingerprint) override;
bool setup(ksCertUtilsNetCLientSecure_t* client, const std::string& fingerprint) override;

/*!
@brief Converts bytes into a string and performs fingerprint validation.
Always returns true on ESP8266 as this functionality is handled under the hood.
@param client Pointer to WiFiClientSecure
@param client Pointer to ksCertUtilsNetCLientSecure_t
@return Always true in case of ESP8266.
*/
bool verify(WiFiClientSecure* client) const override;
bool verify(ksCertUtilsNetCLientSecure_t* client) const override;
};

using ksCertFingerprintHolder = ksCertFingerprintESP8266;
Expand Down
34 changes: 17 additions & 17 deletions src/ksf/misc/ksConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@ namespace ksf::misc
}
}

void ksConfig::setParam(const std::string& paramName, std::string paramValue)
{
isDirty = true;
configParams.insert_or_assign(paramName, std::move(paramValue));
}

const std::string& ksConfig::getParam(const std::string& paramName, const std::string& defaultValue) const
{
const auto& found{configParams.find(paramName)};
return found == configParams.end() ? defaultValue : found->second;
}

ksConfig::operator bool() const
{
return !configPath.empty();
}

ksConfig::~ksConfig()
{
if (!isDirty)
Expand All @@ -87,4 +70,21 @@ namespace ksf::misc
}
fileWriter.close();
}

void ksConfig::setParam(const std::string& paramName, std::string paramValue)
{
isDirty = true;
configParams.insert_or_assign(paramName, std::move(paramValue));
}

const std::string& ksConfig::getParam(const std::string& paramName, const std::string& defaultValue) const
{
const auto& found{configParams.find(paramName)};
return found == configParams.end() ? defaultValue : found->second;
}

ksConfig::operator bool() const
{
return !configPath.empty();
}
}
12 changes: 6 additions & 6 deletions src/ksf/misc/ksConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ namespace ksf::misc
*/
ksConfig(const std::string& fileName);

/*!
@brief Saves config content on the device filesystem.
In case there is no modification, nothing should actually happen.
*/
virtual ~ksConfig();

/*!
@brief Sets parameter value (creates new parameter if it does not exist).
@param paramName Parameter name.
Expand All @@ -61,12 +67,6 @@ namespace ksf::misc
@return True if configFilename is not empty, otherwise false.
*/
operator bool() const;

/*!
@brief Saves config content on the device filesystem.
In case there is no modification, nothing should actually happen.
*/
virtual ~ksConfig();
};
}

4 changes: 3 additions & 1 deletion src/ksf/misc/ksDomainQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ksf::misc
udp->write(static_cast<uint8_t>(value & 0xFF));
}

ksDomainQuery::~ksDomainQuery() = default;


ksDomainQuery::ksDomainQuery() : ksDomainQuery(KSF_DOMAIN_QUERY_DNS_SERVER) {}

Expand All @@ -37,6 +37,8 @@ namespace ksf::misc
udp->begin(0);
}

ksDomainQuery::~ksDomainQuery() = default;

void ksDomainQuery::invalidate()
{
resolvedIP = IPAddress(0, 0, 0, 0);
Expand Down

0 comments on commit 1b1060b

Please sign in to comment.