Skip to content

Commit 16801f3

Browse files
committed
- Rework ExpiringTimeTracker to be based on PolledTimeout.
- Ensure espnowDelay and floodingMeshDelay always performs maintenance. - Rework MutexTracker to use shared_ptr. - Change enums to enum class. - Change typedef to using. - Add HeapMonitor class. - Make _messageIDs be a map instead of an unordered_map to reduce heap usage. - Use the possibly broken wifi_country ESP8266 API to check for legal WiFi channels when setting WiFi channels. - Make MessageData, RequestData and ResponseData contain a TimeTracker rather than inherit from TimeTracker. - Add deprecated attribute to TransmissionResult. - Remove superfluous elses. - Reduce cyclomatic complexity. - Change postfix ++ and -- to prefix. - Generalize getEncryptedConnectionIterator method. - Increase code NRVO compatibility. - Change _connectionAttemptTimeoutMs type from int32_t to uint32_t. - Add deprecated attribute to ESP8266WiFiMesh. - Add some constness to TypeConversionFunctions. - Move base36 arrays to PROGMEM in TypeConversionFunctions.cpp. - Add deprecated atttribute to SHA1 and MD5 hashes. - Remove _warningsEnabled in CryptoInterface since this has been replaced by the deprecated attribute. - Prefix all TypeConversion getters with "get". - Improve comments. - Fix merge conflict.
1 parent a49f047 commit 16801f3

38 files changed

+906
-661
lines changed

libraries/ESP8266WiFiMesh/examples/HelloEspnow/HelloEspnow.ino

Lines changed: 55 additions & 60 deletions
Large diffs are not rendered by default.

libraries/ESP8266WiFiMesh/examples/HelloMesh/HelloMesh.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
That way you will get instant confirmation of the mesh communication without checking the Serial Monitor.
66
*/
77

8-
#define ESP8266WIFIMESH_DISABLE_COMPATIBILITY // Excludes redundant compatibility code. Should be used for new code until the compatibility code is removed with release 3.0.0 of the Arduino core.
8+
#define ESP8266WIFIMESH_DISABLE_COMPATIBILITY // Excludes redundant compatibility code. TODO: Should be used for new code until the compatibility code is removed with release 3.0.0 of the Arduino core.
99

1010
#include <ESP8266WiFi.h>
1111
#include <TypeConversionFunctions.h>
@@ -175,7 +175,7 @@ void loop() {
175175
floodingMeshDelay(1);
176176

177177
// If you wish to transmit only to a single node, try using one of the following methods (requires the node to be within range and know the MAC of the recipient):
178-
// Unencrypted: transmission_status_t floodingMesh.getEspnowMeshBackend().attemptTransmission(message, EspnowNetworkInfo(recipientMac));
178+
// Unencrypted: TransmissionStatusType floodingMesh.getEspnowMeshBackend().attemptTransmission(message, EspnowNetworkInfo(recipientMac));
179179
// Encrypted (slow): floodingMesh.getEspnowMeshBackend().attemptAutoEncryptingTransmission(message, EspnowNetworkInfo(recipientMac));
180180

181181
if (theOne) {

libraries/ESP8266WiFiMesh/examples/HelloTcpIp/HelloTcpIp.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define ESP8266WIFIMESH_DISABLE_COMPATIBILITY // Excludes redundant compatibility code. Should be used for new code until the compatibility code is removed with release 3.0.0 of the Arduino core.
1+
#define ESP8266WIFIMESH_DISABLE_COMPATIBILITY // Excludes redundant compatibility code. TODO: Should be used for new code until the compatibility code is removed with release 3.0.0 of the Arduino core.
22

33
#include <ESP8266WiFi.h>
44
#include <TcpIpMeshBackend.h>
@@ -25,7 +25,7 @@ unsigned int requestNumber = 0;
2525
unsigned int responseNumber = 0;
2626

2727
String manageRequest(const String &request, MeshBackendBase &meshInstance);
28-
transmission_status_t manageResponse(const String &response, MeshBackendBase &meshInstance);
28+
TransmissionStatusType manageResponse(const String &response, MeshBackendBase &meshInstance);
2929
void networkFilter(int numberOfNetworks, MeshBackendBase &meshInstance);
3030

3131
/* Create the mesh node object */
@@ -68,8 +68,8 @@ String manageRequest(const String &request, MeshBackendBase &meshInstance) {
6868
@param meshInstance The MeshBackendBase instance that called the function.
6969
@return The status code resulting from the response, as an int
7070
*/
71-
transmission_status_t manageResponse(const String &response, MeshBackendBase &meshInstance) {
72-
transmission_status_t statusCode = TS_TRANSMISSION_COMPLETE;
71+
TransmissionStatusType manageResponse(const String &response, MeshBackendBase &meshInstance) {
72+
TransmissionStatusType statusCode = TransmissionStatusType::TRANSMISSION_COMPLETE;
7373

7474
// To get the actual class of the polymorphic meshInstance, do as follows (meshBackendCast replaces dynamic_cast since RTTI is disabled)
7575
if (EspnowMeshBackend *espnowInstance = TypeCast::meshBackendCast<EspnowMeshBackend *>(&meshInstance)) {
@@ -142,7 +142,7 @@ bool exampleTransmissionOutcomesUpdateHook(MeshBackendBase &meshInstance) {
142142
// The default hook only returns true and does nothing else.
143143

144144
if (TcpIpMeshBackend *tcpIpInstance = TypeCast::meshBackendCast<TcpIpMeshBackend *>(&meshInstance)) {
145-
if (tcpIpInstance->latestTransmissionOutcomes().back().transmissionStatus() == TS_TRANSMISSION_COMPLETE) {
145+
if (tcpIpInstance->latestTransmissionOutcomes().back().transmissionStatus() == TransmissionStatusType::TRANSMISSION_COMPLETE) {
146146
// Our last request got a response, so time to create a new request.
147147
meshInstance.setMessage(String(F("Hello world request #")) + String(++requestNumber) + F(" from ")
148148
+ meshInstance.getMeshName() + meshInstance.getNodeID() + String('.'));
@@ -209,11 +209,11 @@ void loop() {
209209
Serial.println(F("No mesh AP found."));
210210
} else {
211211
for (TransmissionOutcome &transmissionOutcome : tcpIpNode.latestTransmissionOutcomes()) {
212-
if (transmissionOutcome.transmissionStatus() == TS_TRANSMISSION_FAILED) {
212+
if (transmissionOutcome.transmissionStatus() == TransmissionStatusType::TRANSMISSION_FAILED) {
213213
Serial.println(String(F("Transmission failed to mesh AP ")) + transmissionOutcome.SSID());
214-
} else if (transmissionOutcome.transmissionStatus() == TS_CONNECTION_FAILED) {
214+
} else if (transmissionOutcome.transmissionStatus() == TransmissionStatusType::CONNECTION_FAILED) {
215215
Serial.println(String(F("Connection failed to mesh AP ")) + transmissionOutcome.SSID());
216-
} else if (transmissionOutcome.transmissionStatus() == TS_TRANSMISSION_COMPLETE) {
216+
} else if (transmissionOutcome.transmissionStatus() == TransmissionStatusType::TRANSMISSION_COMPLETE) {
217217
// No need to do anything, transmission was successful.
218218
} else {
219219
Serial.println(String(F("Invalid transmission status for ")) + transmissionOutcome.SSID() + String('!'));

libraries/ESP8266WiFiMesh/keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ESP8266WiFiMesh KEYWORD3
1212
# Datatypes (KEYWORD1)
1313
#######################################
1414

15-
transmission_status_t KEYWORD1
15+
TransmissionStatusType KEYWORD1
1616

1717
#######################################
1818
# Methods and Functions (KEYWORD2)

libraries/ESP8266WiFiMesh/src/CryptoInterface.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ namespace
3434
{
3535
size_t _ctMinDataLength = 0;
3636
size_t _ctMaxDataLength = 1024;
37-
38-
bool _warningsEnabled = true;
3937

4038
br_hkdf_context _storedHkdfContext;
4139
bool _hkdfContextStored = false;
@@ -186,52 +184,61 @@ namespace
186184
createBearsslHmacCT(hashType, message.c_str(), message.length(), hashKey, hashKeyLength, hmac, hmacLength);
187185
return TypeCast::uint8ArrayToHexString(hmac, hmacLength);
188186
}
187+
188+
189+
// Helper function to avoid deprecated warnings.
190+
void *md5HashHelper(const void *data, const size_t dataLength, void *resultArray)
191+
{
192+
br_md5_context context;
193+
br_md5_init(&context);
194+
br_md5_update(&context, data, dataLength);
195+
br_md5_out(&context, resultArray);
196+
return resultArray;
197+
}
198+
199+
// Helper function to avoid deprecated warnings.
200+
void *sha1HashHelper(const void *data, const size_t dataLength, void *resultArray)
201+
{
202+
br_sha1_context context;
203+
br_sha1_init(&context);
204+
br_sha1_update(&context, data, dataLength);
205+
br_sha1_out(&context, resultArray);
206+
return resultArray;
207+
}
189208
}
190209

191210
namespace CryptoInterface
192211
{
193212
void setCtMinDataLength(const size_t ctMinDataLength)
194213
{
195-
assert(ctMaxDataLength() - ctMinDataLength <= CT_MAX_DIFF);
214+
assert(getCtMaxDataLength() - ctMinDataLength <= CT_MAX_DIFF);
196215
_ctMinDataLength = ctMinDataLength;
197216
}
198-
size_t ctMinDataLength() {return _ctMinDataLength;}
217+
size_t getCtMinDataLength() {return _ctMinDataLength;}
199218

200219
void setCtMaxDataLength(const size_t ctMaxDataLength)
201220
{
202-
assert(ctMaxDataLength - ctMinDataLength() <= CT_MAX_DIFF);
221+
assert(ctMaxDataLength - getCtMinDataLength() <= CT_MAX_DIFF);
203222
_ctMaxDataLength = ctMaxDataLength;
204223
}
205-
size_t ctMaxDataLength() {return _ctMaxDataLength;}
206-
207-
void setWarningsEnabled(bool warningsEnabled) { _warningsEnabled = warningsEnabled; }
208-
bool warningsEnabled() { return _warningsEnabled; }
224+
size_t getCtMaxDataLength() {return _ctMaxDataLength;}
209225

210226
void setNonceGenerator(nonceGeneratorType nonceGenerator) { _nonceGenerator = nonceGenerator; }
211227
nonceGeneratorType getNonceGenerator() { return _nonceGenerator; }
212228

213229

214230
// #################### MD5 ####################
215-
231+
216232
// resultArray must have size MD5_NATURAL_LENGTH or greater
217233
void *md5Hash(const void *data, const size_t dataLength, void *resultArray)
218234
{
219-
if(warningsEnabled())
220-
Serial.println(F("\nWARNING! The MD5 hash is broken in terms of attacker resistance.\n"
221-
"Only use it in those cases where attacker resistance is not important. Prefer SHA-256 or higher otherwise.\n"
222-
"Use CryptoInterface::setWarningsEnabled(false) to turn off this warning.\n"));
223-
224-
br_md5_context context;
225-
br_md5_init(&context);
226-
br_md5_update(&context, data, dataLength);
227-
br_md5_out(&context, resultArray);
228-
return resultArray;
235+
return md5HashHelper(data, dataLength, resultArray);
229236
}
230237

231238
String md5Hash(const String &message)
232239
{
233240
uint8_t hash[MD5_NATURAL_LENGTH];
234-
md5Hash(message.c_str(), message.length(), hash);
241+
md5HashHelper(message.c_str(), message.length(), hash);
235242
return TypeCast::uint8ArrayToHexString(hash, MD5_NATURAL_LENGTH);
236243
}
237244

@@ -260,23 +267,14 @@ namespace CryptoInterface
260267

261268
// resultArray must have size SHA1_NATURAL_LENGTH or greater
262269
void *sha1Hash(const void *data, const size_t dataLength, void *resultArray)
263-
{
264-
if(warningsEnabled())
265-
Serial.println(F("\nWARNING! The SHA-1 hash is broken in terms of attacker resistance.\n"
266-
"Only use it in those cases where attacker resistance is not important. Prefer SHA-256 or higher otherwise.\n"
267-
"Use CryptoInterface::setWarningsEnabled(false) to turn off this warning.\n"));
268-
269-
br_sha1_context context;
270-
br_sha1_init(&context);
271-
br_sha1_update(&context, data, dataLength);
272-
br_sha1_out(&context, resultArray);
273-
return resultArray;
270+
{
271+
return sha1HashHelper(data, dataLength, resultArray);
274272
}
275273

276274
String sha1Hash(const String &message)
277275
{
278276
uint8_t hash[SHA1_NATURAL_LENGTH];
279-
sha1Hash(message.c_str(), message.length(), hash);
277+
sha1HashHelper(message.c_str(), message.length(), hash);
280278
return TypeCast::uint8ArrayToHexString(hash, SHA1_NATURAL_LENGTH);
281279
}
282280

0 commit comments

Comments
 (0)