Skip to content

Commit 1b1060b

Browse files
committed
Cleanup the destruction flow
1 parent eb71566 commit 1b1060b

File tree

11 files changed

+85
-62
lines changed

11 files changed

+85
-62
lines changed

src/ksf/comp/ksDevicePortal.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ namespace ksf::comps
198198
*/
199199
ksDevicePortal();
200200

201+
/*!
202+
@brief Destructs the Device Portal component.
203+
*/
204+
virtual ~ksDevicePortal();
205+
201206
/*!
202207
@brief Constructs the Device Portal component with custom password.
203208
@@ -225,10 +230,5 @@ namespace ksf::comps
225230
@return True on success, false otherwise.
226231
*/
227232
bool loop(ksApplication* app) override;
228-
229-
/*!
230-
@brief Destructs the Device Portal component.
231-
*/
232-
virtual ~ksDevicePortal();
233233
};
234234
}

src/ksf/comp/ksLed.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace ksf::comps
2121
bitflags.driveAsPushPull = driveAsPushPull;
2222
}
2323

24+
ksLed::~ksLed()
25+
{
26+
pinMode(pin, INPUT);
27+
}
28+
2429
bool ksLed::init(ksApplication* owner)
2530
{
2631
if (!bitflags.driveAsPushPull)
@@ -85,9 +90,4 @@ namespace ksf::comps
8590
else
8691
digitalWrite(pin, (bitflags.activeLow ? !enabled : enabled) ? HIGH : LOW);
8792
}
88-
89-
ksLed::~ksLed()
90-
{
91-
pinMode(pin, INPUT);
92-
}
9393
}

src/ksf/comp/ksLed.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ namespace ksf::comps
4646
*/
4747
ksLed(uint8_t pin, bool activeLow = false, bool driveAsPushPull = false);
4848

49+
/*!
50+
@brief Destructs the component and restores INPUT mode on the assigned pin.
51+
*/
52+
virtual ~ksLed();
53+
4954
/*!
5055
@brief Initializes the LED component.
5156
@param owner Pointer to parent ksApplication object.
@@ -95,10 +100,5 @@ namespace ksf::comps
95100
@return Pin number assigned to LED.
96101
*/
97102
uint8_t getPin() const { return pin; }
98-
99-
/*!
100-
@brief Destructs the component and restores INPUT mode on the assigned pin.
101-
*/
102-
virtual ~ksLed();
103103
};
104104
}

src/ksf/comp/ksMqttConnector.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace ksf::comps
3939
bitflags.usePersistentSession = usePersistentSession;
4040
}
4141

42+
ksMqttConnector::~ksMqttConnector() = default;
43+
4244
bool ksMqttConnector::init(ksApplication* app)
4345
{
4446
ksMqttConfigProvider cfgProvider;

src/ksf/comp/ksMqttConnector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ namespace ksf::comps
120120
*/
121121
ksMqttConnector(bool sendConnectionStatus = true, bool usePersistentSession = false);
122122

123+
/*!
124+
@brief Destructs ksMqttConnector object.
125+
*/
126+
virtual ~ksMqttConnector();
127+
123128
/*!
124129
@brief Instantiates the MQTT connector component.
125130

src/ksf/comp/ksWifiConfigurator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ namespace ksf::comps
3838
deviceName += ksf::getDeviceUuidHex();
3939
}
4040

41-
ksWifiConfigurator::~ksWifiConfigurator()
42-
{
43-
WiFi.softAPdisconnect(true);
44-
}
45-
4641
bool ksWifiConfigurator::init(ksApplication* app)
4742
{
4843
WiFi.softAP(deviceName.c_str());
@@ -75,4 +70,9 @@ namespace ksf::comps
7570

7671
return !configTimeout.triggered();
7772
}
73+
74+
ksWifiConfigurator::~ksWifiConfigurator()
75+
{
76+
WiFi.softAPdisconnect(true);
77+
}
7878
}

src/ksf/misc/ksCertUtils.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
* https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
88
*/
99

10+
#include <WiFiClientSecure.h>
11+
1012
#include "ksCertUtils.h"
1113
#include "../ksConstants.h"
12-
1314
namespace ksf::misc
1415
{
1516
/*
@@ -30,6 +31,7 @@ namespace ksf::misc
3031
return 255;
3132
}
3233

34+
ksCertFingerprint::ksCertFingerprint() = default;
3335
ksCertFingerprint::~ksCertFingerprint() = default;
3436

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

5456
#ifdef ESP32
55-
bool ksCertFingerprintESP32::setup(WiFiClientSecure* clientSecure, const std::string& fingerprint)
57+
bool ksCertFingerprintESP32::setup(ksCertUtilsNetCLientSecure_t* clientSecure, const std::string& fingerprint)
5658
{
5759
if (fingerprintToBytes(fingerprint, fingerprintBytes, sizeof(fingerprintBytes)))
5860
{
@@ -64,7 +66,7 @@ namespace ksf::misc
6466
return false;
6567
}
6668

67-
bool ksCertFingerprintESP32::verify(WiFiClientSecure* client) const
69+
bool ksCertFingerprintESP32::verify(ksCertUtilsNetCLientSecure_t* client) const
6870
{
6971
static const char characters[] {"0123456789ABCDEF"};
7072

@@ -82,7 +84,7 @@ namespace ksf::misc
8284
#endif
8385

8486
#ifdef ESP8266
85-
bool ksCertFingerprintESP8266::setup(WiFiClientSecure* clientSecure, const std::string& fingerprint)
87+
bool ksCertFingerprintESP8266::setup(ksCertUtilsNetCLientSecure_t* clientSecure, const std::string& fingerprint)
8688
{
8789
uint8_t fingerprintBytes[20];
8890
if (fingerprintToBytes(fingerprint, fingerprintBytes, sizeof(fingerprintBytes)))
@@ -94,7 +96,7 @@ namespace ksf::misc
9496
return false;
9597
}
9698

97-
bool ksCertFingerprintESP8266::verify(WiFiClientSecure* client) const
99+
bool ksCertFingerprintESP8266::verify(ksCertUtilsNetCLientSecure_t* client) const
98100
{
99101
return true;
100102
}

src/ksf/misc/ksCertUtils.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111

1212
#include <string>
1313
#include <stdint.h>
14-
#include <WiFiClientSecure.h>
14+
15+
#if (defined(ESP32) && ESP_ARDUINO_VERSION_MAJOR >= 3)
16+
#define ksCertUtilsNetCLientSecure_t NetworkClientSecure
17+
#else
18+
#define ksCertUtilsNetCLientSecure_t WiFiClientSecure
19+
#endif
20+
21+
class ksCertUtilsNetCLientSecure_t;
1522

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

3643
public:
44+
/*!
45+
@brief Constructor.
46+
*/
47+
ksCertFingerprint();
48+
3749
/*!
3850
@brief Destructor.
3951
*/
4052
virtual ~ksCertFingerprint();
4153

4254
/*!
4355
@brief Performs certificate fingerprint setup (platform dependent).
44-
@param client Pointer of WiFiClientSecure
56+
@param client Pointer of ksCertUtilsNetCLientSecure_t
4557
@param fingerprint Fingerprint string
4658
@return True if setup has been successfull, otherwise false.
4759
*/
48-
virtual bool setup(WiFiClientSecure* client, const std::string& fingerprint);
60+
virtual bool setup(ksCertUtilsNetCLientSecure_t* client, const std::string& fingerprint) = 0;
4961

5062
/*!
5163
@brief Performs certificate fingerprint validation (platform dependent).
52-
@param client Pointer of WiFiClientSecure
64+
@param client Pointer of ksCertUtilsNetCLientSecure_t
5365
@return True on verification pass, otherwise false.
5466
*/
55-
virtual bool verify(WiFiClientSecure* client) const;
67+
virtual bool verify(ksCertUtilsNetCLientSecure_t* client) const = 0;
5668
};
5769

5870
#ifdef ESP32
@@ -67,18 +79,18 @@ namespace ksf::misc
6779
6880
On ESP32 it will copy fingerprint into fingerprintBytes.
6981
70-
@param client Pointer to WiFiClientSecure
82+
@param client Pointer to ksCertUtilsNetCLientSecure_t
7183
@param fingerprint Fingerprint string
7284
@return True if setup passed, otherwise false.
7385
*/
74-
bool setup(WiFiClientSecure* client, const std::string& fingerprint) override;
86+
bool setup(ksCertUtilsNetCLientSecure_t* client, const std::string& fingerprint) override;
7587

7688
/*!
7789
@brief Converts bytes into a string and performs fingerprint validation.
78-
@param client Pointer to WiFiClientSecure
90+
@param client Pointer to ksCertUtilsNetCLientSecure_t
7991
@return True if verification passed, otherwise false.
8092
*/
81-
bool verify(WiFiClientSecure* client) const override;
93+
bool verify(ksCertUtilsNetCLientSecure_t* client) const override;
8294
};
8395

8496
using ksCertFingerprintHolder = ksCertFingerprintESP32;
@@ -90,21 +102,21 @@ namespace ksf::misc
90102
public:
91103
/*!
92104
@brief Performs fingerprint setup (platform dependent).
93-
@param client Pointer of WiFiClientSecure.
105+
@param client Pointer of ksCertUtilsNetCLientSecure_t.
94106
@param fingerprint Fingerprint string.
95107
@return True if setup passed, otherwise false.
96108
*/
97-
bool setup(WiFiClientSecure* client, const std::string& fingerprint) override;
109+
bool setup(ksCertUtilsNetCLientSecure_t* client, const std::string& fingerprint) override;
98110

99111
/*!
100112
@brief Converts bytes into a string and performs fingerprint validation.
101113
102114
Always returns true on ESP8266 as this functionality is handled under the hood.
103115
104-
@param client Pointer to WiFiClientSecure
116+
@param client Pointer to ksCertUtilsNetCLientSecure_t
105117
@return Always true in case of ESP8266.
106118
*/
107-
bool verify(WiFiClientSecure* client) const override;
119+
bool verify(ksCertUtilsNetCLientSecure_t* client) const override;
108120
};
109121

110122
using ksCertFingerprintHolder = ksCertFingerprintESP8266;

src/ksf/misc/ksConfig.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,6 @@ namespace ksf::misc
5353
}
5454
}
5555

56-
void ksConfig::setParam(const std::string& paramName, std::string paramValue)
57-
{
58-
isDirty = true;
59-
configParams.insert_or_assign(paramName, std::move(paramValue));
60-
}
61-
62-
const std::string& ksConfig::getParam(const std::string& paramName, const std::string& defaultValue) const
63-
{
64-
const auto& found{configParams.find(paramName)};
65-
return found == configParams.end() ? defaultValue : found->second;
66-
}
67-
68-
ksConfig::operator bool() const
69-
{
70-
return !configPath.empty();
71-
}
72-
7356
ksConfig::~ksConfig()
7457
{
7558
if (!isDirty)
@@ -87,4 +70,21 @@ namespace ksf::misc
8770
}
8871
fileWriter.close();
8972
}
73+
74+
void ksConfig::setParam(const std::string& paramName, std::string paramValue)
75+
{
76+
isDirty = true;
77+
configParams.insert_or_assign(paramName, std::move(paramValue));
78+
}
79+
80+
const std::string& ksConfig::getParam(const std::string& paramName, const std::string& defaultValue) const
81+
{
82+
const auto& found{configParams.find(paramName)};
83+
return found == configParams.end() ? defaultValue : found->second;
84+
}
85+
86+
ksConfig::operator bool() const
87+
{
88+
return !configPath.empty();
89+
}
9090
}

src/ksf/misc/ksConfig.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ namespace ksf::misc
4141
*/
4242
ksConfig(const std::string& fileName);
4343

44+
/*!
45+
@brief Saves config content on the device filesystem.
46+
In case there is no modification, nothing should actually happen.
47+
*/
48+
virtual ~ksConfig();
49+
4450
/*!
4551
@brief Sets parameter value (creates new parameter if it does not exist).
4652
@param paramName Parameter name.
@@ -61,12 +67,6 @@ namespace ksf::misc
6167
@return True if configFilename is not empty, otherwise false.
6268
*/
6369
operator bool() const;
64-
65-
/*!
66-
@brief Saves config content on the device filesystem.
67-
In case there is no modification, nothing should actually happen.
68-
*/
69-
virtual ~ksConfig();
7070
};
7171
}
7272

0 commit comments

Comments
 (0)