Repository navigation
Expand file tree
/
Copy pathEspnowDatabase.cpp
More file actions
383 lines (316 loc) · 14.3 KB
/
Copy pathEspnowDatabase.cpp
File metadata and controls
383 lines (316 loc) · 14.3 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Copyright (C) 2020 Anders Löfgren
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "EspnowDatabase.h"
#include "EspnowMeshBackend.h"
#include "UtilityFunctions.h"
namespace
{
namespace TypeCast = MeshTypeConversionFunctions;
// _logEntryLifetimeMs is based on someone storing 40 responses of 750 bytes each = 30 000 bytes (roughly full memory),
// which takes 2000 ms + some margin to send. Also, we want to avoid old entries taking up memory if they cannot be sent,
// so storage duration should not be too long.
uint32_t _logEntryLifetimeMs = 2500;
uint32_t _broadcastResponseTimeoutMs = 1000; // This is shorter than _logEntryLifetimeMs to preserve RAM since broadcasts are not deleted from sentRequests until they expire.
ExpiringTimeTracker _logClearingCooldown(500);
uint32_t _encryptionRequestTimeoutMs = 300;
uint32_t _criticalHeapLevel = 6000; // In bytes
uint32_t _criticalHeapLevelBuffer = 6000; // In bytes
using EspnowProtocolInterpreter::macAndType_td;
using EspnowProtocolInterpreter::messageID_td;
using EspnowProtocolInterpreter::peerMac_td;
std::list<ResponseData> _responsesToSend = {};
std::list<PeerRequestLog> _peerRequestConfirmationsToSend = {};
std::map<std::pair<macAndType_td, messageID_td>, MessageData> _receivedEspnowTransmissions = {};
std::map<std::pair<peerMac_td, messageID_td>, RequestData> _sentRequests = {};
std::map<std::pair<peerMac_td, messageID_td>, TimeTracker> _receivedRequests = {};
std::shared_ptr<bool> _espnowConnectionQueueMutex = std::make_shared<bool>(false);
std::shared_ptr<bool> _responsesToSendMutex = std::make_shared<bool>(false);
}
std::vector<EspnowNetworkInfo> EspnowDatabase::_connectionQueue = {};
std::vector<TransmissionOutcome> EspnowDatabase::_latestTransmissionOutcomes = {};
EspnowDatabase::EspnowDatabase(ConditionalPrinter &conditionalPrinterInstance, const uint8 espnowWiFiChannel) : _conditionalPrinter(conditionalPrinterInstance), _espnowWiFiChannel(espnowWiFiChannel)
{
}
std::vector<EspnowNetworkInfo> & EspnowDatabase::connectionQueue()
{
MutexTracker connectionQueueMutexTracker(_espnowConnectionQueueMutex);
if(!connectionQueueMutexTracker.mutexCaptured())
{
assert(false && String(F("ERROR! connectionQueue locked. Don't call connectionQueue() from callbacks other than NetworkFilter as this may corrupt program state!")));
}
return _connectionQueue;
}
const std::vector<EspnowNetworkInfo> & EspnowDatabase::constConnectionQueue()
{
return _connectionQueue;
}
std::vector<TransmissionOutcome> & EspnowDatabase::latestTransmissionOutcomes()
{
return _latestTransmissionOutcomes;
}
void EspnowDatabase::setCriticalHeapLevelBuffer(const uint32_t bufferInBytes)
{
_criticalHeapLevelBuffer = bufferInBytes;
}
uint32_t EspnowDatabase::criticalHeapLevelBuffer()
{
return _criticalHeapLevelBuffer;
}
uint32_t EspnowDatabase::criticalHeapLevel()
{
return _criticalHeapLevel;
}
template <typename T, typename U>
void EspnowDatabase::deleteExpiredLogEntries(std::map<std::pair<U, uint64_t>, T> &logEntries, const uint32_t maxEntryLifetimeMs)
{
for(typename std::map<std::pair<U, uint64_t>, T>::iterator entryIterator = logEntries.begin();
entryIterator != logEntries.end(); )
{
if(entryIterator->second.getTimeTracker().timeSinceCreation() > maxEntryLifetimeMs)
{
entryIterator = logEntries.erase(entryIterator);
}
else
++entryIterator;
}
}
template <typename U>
void EspnowDatabase::deleteExpiredLogEntries(std::map<std::pair<U, uint64_t>, TimeTracker> &logEntries, const uint32_t maxEntryLifetimeMs)
{
for(typename std::map<std::pair<U, uint64_t>, TimeTracker>::iterator entryIterator = logEntries.begin();
entryIterator != logEntries.end(); )
{
if(entryIterator->second.timeSinceCreation() > maxEntryLifetimeMs)
{
entryIterator = logEntries.erase(entryIterator);
}
else
++entryIterator;
}
}
void EspnowDatabase::deleteExpiredLogEntries(std::map<std::pair<peerMac_td, messageID_td>, RequestData> &logEntries, const uint32_t requestLifetimeMs, const uint32_t broadcastLifetimeMs)
{
for(typename std::map<std::pair<peerMac_td, messageID_td>, RequestData>::iterator entryIterator = logEntries.begin();
entryIterator != logEntries.end(); )
{
bool broadcast = entryIterator->first.first == EspnowProtocolInterpreter::uint64BroadcastMac;
uint32_t timeSinceCreation = entryIterator->second.getTimeTracker().timeSinceCreation();
if((!broadcast && timeSinceCreation > requestLifetimeMs)
|| (broadcast && timeSinceCreation > broadcastLifetimeMs))
{
entryIterator = logEntries.erase(entryIterator);
}
else
++entryIterator;
}
}
template <typename T>
void EspnowDatabase::deleteExpiredLogEntries(std::list<T> &logEntries, const uint32_t maxEntryLifetimeMs)
{
for(typename std::list<T>::iterator entryIterator = logEntries.begin();
entryIterator != logEntries.end(); )
{
if(entryIterator->getTimeTracker().timeSinceCreation() > maxEntryLifetimeMs)
{
entryIterator = logEntries.erase(entryIterator);
}
else
++entryIterator;
}
}
template <>
void EspnowDatabase::deleteExpiredLogEntries(std::list<PeerRequestLog> &logEntries, const uint32_t maxEntryLifetimeMs)
{
for(typename std::list<PeerRequestLog>::iterator entryIterator = logEntries.begin();
entryIterator != logEntries.end(); )
{
auto timeTrackerPointer = entryIterator->temporary();
if(timeTrackerPointer && timeTrackerPointer->elapsedTime() > maxEntryLifetimeMs)
{
entryIterator = logEntries.erase(entryIterator);
}
else
++entryIterator;
}
}
void EspnowDatabase::setLogEntryLifetimeMs(const uint32_t logEntryLifetimeMs)
{
_logEntryLifetimeMs = logEntryLifetimeMs;
}
uint32_t EspnowDatabase::logEntryLifetimeMs() { return _logEntryLifetimeMs; }
void EspnowDatabase::setBroadcastResponseTimeoutMs(const uint32_t broadcastResponseTimeoutMs)
{
_broadcastResponseTimeoutMs = broadcastResponseTimeoutMs;
}
uint32_t EspnowDatabase::broadcastResponseTimeoutMs() { return _broadcastResponseTimeoutMs; }
String EspnowDatabase::getScheduledResponseMessage(const uint32_t responseIndex)
{
return getScheduledResponse(responseIndex)->getMessage();
}
const uint8_t *EspnowDatabase::getScheduledResponseRecipient(const uint32_t responseIndex)
{
return getScheduledResponse(responseIndex)->getRecipientMac();
}
uint32_t EspnowDatabase::numberOfScheduledResponses() {return responsesToSend().size();}
void EspnowDatabase::clearAllScheduledResponses()
{
MutexTracker responsesToSendMutexTracker(_responsesToSendMutex);
if(!responsesToSendMutexTracker.mutexCaptured())
{
assert(false && String(F("ERROR! responsesToSend locked. Don't call clearAllScheduledResponses from callbacks as this may corrupt program state! Aborting.")));
}
responsesToSend().clear();
}
void EspnowDatabase::deleteScheduledResponsesByRecipient(const uint8_t *recipientMac, const bool encryptedOnly)
{
MutexTracker responsesToSendMutexTracker(_responsesToSendMutex);
if(!responsesToSendMutexTracker.mutexCaptured())
{
assert(false && String(F("ERROR! responsesToSend locked. Don't call deleteScheduledResponsesByRecipient from callbacks as this may corrupt program state! Aborting.")));
}
for(auto responseIterator = responsesToSend().begin(); responseIterator != responsesToSend().end(); )
{
if(MeshUtilityFunctions::macEqual(responseIterator->getRecipientMac(), recipientMac) &&
(!encryptedOnly || EspnowProtocolInterpreter::usesEncryption(responseIterator->getRequestID())))
{
responseIterator = responsesToSend().erase(responseIterator);
}
else
++responseIterator;
}
}
void EspnowDatabase::setEncryptionRequestTimeout(const uint32_t timeoutMs)
{
_encryptionRequestTimeoutMs = timeoutMs;
}
uint32_t EspnowDatabase::getEncryptionRequestTimeout() {return _encryptionRequestTimeoutMs;}
void EspnowDatabase::setAutoEncryptionDuration(const uint32_t duration)
{
_autoEncryptionDuration = duration;
}
uint32_t EspnowDatabase::getAutoEncryptionDuration() const {return _autoEncryptionDuration;}
String EspnowDatabase::getSenderMac() const {return TypeCast::macToString(_senderMac);}
uint8_t *EspnowDatabase::getSenderMac(uint8_t *macArray) const
{
std::copy_n(_senderMac, 6, macArray);
return macArray;
}
String EspnowDatabase::getSenderAPMac() const {return TypeCast::macToString(_senderAPMac);}
uint8_t *EspnowDatabase::getSenderAPMac(uint8_t *macArray) const
{
std::copy_n(_senderAPMac, 6, macArray);
return macArray;
}
void EspnowDatabase::clearOldLogEntries(bool forced)
{
// Clearing all old log entries at the same time should help minimize heap fragmentation.
// uint32_t startTime = millis();
if(!forced && !_logClearingCooldown) // Clearing too frequently will cause a lot of unnecessary container iterations.
{
return;
}
_logClearingCooldown.reset();
deleteExpiredLogEntries(receivedEspnowTransmissions(), logEntryLifetimeMs());
deleteExpiredLogEntries(receivedRequests(), logEntryLifetimeMs()); // Just needs to be long enough to not accept repeated transmissions by mistake.
deleteExpiredLogEntries(sentRequests(), logEntryLifetimeMs(), broadcastResponseTimeoutMs());
deleteExpiredLogEntries(responsesToSend(), logEntryLifetimeMs());
deleteExpiredLogEntries(peerRequestConfirmationsToSend(), getEncryptionRequestTimeout());
}
std::list<ResponseData>::const_iterator EspnowDatabase::getScheduledResponse(const uint32_t responseIndex)
{
assert(responseIndex < numberOfScheduledResponses());
bool startFromBeginning = responseIndex < numberOfScheduledResponses()/2;
auto responseIterator = startFromBeginning ? responsesToSend().cbegin() : responsesToSend().cend();
uint32_t stepsToTarget = startFromBeginning ? responseIndex : numberOfScheduledResponses() - responseIndex; // cend is one element beyond the last
while(stepsToTarget > 0)
{
startFromBeginning ? ++responseIterator : --responseIterator;
--stepsToTarget;
}
return responseIterator;
}
void EspnowDatabase::setSenderMac(const uint8_t *macArray)
{
std::copy_n(macArray, 6, _senderMac);
}
void EspnowDatabase::setSenderAPMac(const uint8_t *macArray)
{
std::copy_n(macArray, 6, _senderAPMac);
}
void EspnowDatabase::setWiFiChannel(const uint8 newWiFiChannel)
{
wifi_country_t wifiCountry;
wifi_get_country(&wifiCountry); // Note: Should return 0 on success and -1 on failure, but always seems to return 1. Possibly broken API. Channels 1 to 13 are the default limits.
assert(wifiCountry.schan <= newWiFiChannel && newWiFiChannel <= wifiCountry.schan + wifiCountry.nchan - 1);
_espnowWiFiChannel = newWiFiChannel;
}
uint8 EspnowDatabase::getWiFiChannel() const
{
return _espnowWiFiChannel;
}
bool EspnowDatabase::requestReceived(const uint64_t requestMac, const uint64_t requestID)
{
return receivedRequests().count(std::make_pair(requestMac, requestID));
}
MutexTracker EspnowDatabase::captureEspnowConnectionQueueMutex()
{
// Syntax like this will move the resulting value into its new position (similar to NRVO): https://stackoverflow.com/a/11540204
return MutexTracker(_espnowConnectionQueueMutex);
}
MutexTracker EspnowDatabase::captureEspnowConnectionQueueMutex(const std::function<void()> destructorHook) { return MutexTracker(_espnowConnectionQueueMutex, destructorHook); }
MutexTracker EspnowDatabase::captureResponsesToSendMutex(){ return MutexTracker(_responsesToSendMutex); }
MutexTracker EspnowDatabase::captureResponsesToSendMutex(const std::function<void()> destructorHook) { return MutexTracker(_responsesToSendMutex, destructorHook); }
void EspnowDatabase::storeSentRequest(const uint64_t targetBSSID, const uint64_t messageID, const RequestData &requestData)
{
sentRequests().insert(std::make_pair(std::make_pair(targetBSSID, messageID), requestData));
}
void EspnowDatabase::storeReceivedRequest(const uint64_t senderBSSID, const uint64_t messageID, const TimeTracker &timeTracker)
{
receivedRequests().insert(std::make_pair(std::make_pair(senderBSSID, messageID), timeTracker));
}
EspnowMeshBackend *EspnowDatabase::getOwnerOfSentRequest(const uint64_t requestMac, const uint64_t requestID)
{
std::map<std::pair<peerMac_td, messageID_td>, RequestData>::iterator sentRequest = sentRequests().find(std::make_pair(requestMac, requestID));
if(sentRequest != sentRequests().end())
{
return &sentRequest->second.getMeshInstance();
}
return nullptr;
}
size_t EspnowDatabase::deleteSentRequest(const uint64_t requestMac, const uint64_t requestID)
{
return sentRequests().erase(std::make_pair(requestMac, requestID));
}
size_t EspnowDatabase::deleteSentRequestsByOwner(const EspnowMeshBackend *instancePointer)
{
size_t numberDeleted = 0;
for(std::map<std::pair<peerMac_td, messageID_td>, RequestData>::iterator requestIterator = sentRequests().begin();
requestIterator != sentRequests().end(); )
{
if(&requestIterator->second.getMeshInstance() == instancePointer) // If instance at instancePointer made the request
{
requestIterator = sentRequests().erase(requestIterator);
numberDeleted++;
}
else
++requestIterator;
}
return numberDeleted;
}
std::list<ResponseData> & EspnowDatabase::responsesToSend() { return _responsesToSend; }
std::list<PeerRequestLog> & EspnowDatabase::peerRequestConfirmationsToSend() { return _peerRequestConfirmationsToSend; }
std::map<std::pair<macAndType_td, messageID_td>, MessageData> & EspnowDatabase::receivedEspnowTransmissions() { return _receivedEspnowTransmissions; }
std::map<std::pair<peerMac_td, messageID_td>, RequestData> & EspnowDatabase::sentRequests() { return _sentRequests; }
std::map<std::pair<peerMac_td, messageID_td>, TimeTracker> & EspnowDatabase::receivedRequests() { return _receivedRequests; }