@alteriom/painlessmesh 1.6.1 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +435 -144
- package/LICENSE +674 -674
- package/README.md +491 -434
- package/RELEASE_GUIDE.md +504 -418
- package/docs/DOCUMENTATION_MIGRATION_PLAN.md +175 -175
- package/docs/IMPLEMENTATION_PLAN_MESH_TOPOLOGY.md +1062 -0
- package/docs/MESH_TOPOLOGY_GUIDE.md +992 -0
- package/docs/MESH_TOPOLOGY_PROGRESS.md +422 -0
- package/docs/MQTT_BRIDGE_COMMANDS.md +894 -0
- package/docs/MQTT_BRIDGE_IMPLEMENTATION_SUMMARY.md +324 -0
- package/docs/MQTT_COMMAND_SCHEMA_PROPOSAL.md +576 -0
- package/docs/MQTT_SCHEMA_COMPLIANCE.md +285 -0
- package/docs/MQTT_SCHEMA_PROPOSALS.md +446 -0
- package/docs/MQTT_SCHEMA_REVIEW.md +690 -0
- package/docs/OTA_COMMANDS_REFERENCE.md +554 -0
- package/docs/PHASE1_GUIDE.md +349 -0
- package/docs/PHASE2_GUIDE.md +543 -0
- package/docs/README.md +130 -71
- package/docs/SCHEMA_VALIDATION_CHECKLIST.md +222 -0
- package/docs/alteriom/overview.md +507 -507
- package/docs/api/core-api.md +606 -606
- package/docs/architecture/mesh-architecture.md +378 -378
- package/docs/architecture/plugin-system.md +516 -516
- package/docs/archive/DOCUSAURUS_DEPLOYMENT.md +166 -0
- package/docs/archive/FEATURE_PROPOSALS.md +337 -0
- package/docs/archive/LIBRARY_JSON_FIX.md +98 -0
- package/docs/archive/LIBRARY_STRUCTURE_FIX.md +215 -0
- package/docs/archive/PHASE1_IMPLEMENTATION.md +325 -0
- package/docs/archive/PHASE2_IMPLEMENTATION.md +567 -0
- package/docs/archive/RELEASE_SUMMARY.md +173 -0
- package/docs/archive/SCONS_BUILD_FIX.md +313 -0
- package/docs/archive/TRIGGER_RELEASE.md +280 -0
- package/docs/archive/VECTOR_INCLUDE_FIX.md +129 -0
- package/docs/archive/ota-and-status-enhancements.md +911 -0
- package/docs/archive/ota-status-architecture-diagrams.md +658 -0
- package/docs/archive/ota-status-quick-reference.md +284 -0
- package/docs/development/ARDUINO_COMPLIANCE_SUMMARY.md +71 -0
- package/docs/development/CODE_REFACTORING_RECOMMENDATIONS.md +1011 -0
- package/docs/development/DOCKER_TESTING.md +196 -0
- package/docs/development/PLATFORMIO_USAGE.md +180 -0
- package/docs/development/TESTING_SUMMARY.md +126 -0
- package/docs/development/contributing.md +301 -0
- package/docs/development/documentation.md +583 -0
- package/docs/getting-started/first-mesh.md +409 -409
- package/docs/getting-started/installation.md +274 -274
- package/docs/getting-started/quickstart.md +157 -157
- package/docs/improvements/FUTURE_PROPOSALS.md +1016 -0
- package/docs/improvements/IMPLEMENTATION_HISTORY.md +1091 -0
- package/docs/improvements/OTA_STATUS_ENHANCEMENTS.md +709 -0
- package/docs/improvements/README.md +212 -69
- package/docs/platformio-publishing.md +255 -0
- package/docs/platformio-setup-summary.md +121 -0
- package/docs/releases/FEATURE_HISTORY.md +543 -0
- package/docs/releases/PATCH_v1.7.3.md +262 -0
- package/docs/releases/PHASE1_SUMMARY.md +246 -0
- package/docs/releases/PHASE2_SUMMARY.md +499 -0
- package/docs/releases/RELEASE_NOTES_1.7.0.md +539 -0
- package/docs/troubleshooting/common-issues.md +520 -520
- package/docs/troubleshooting/debugging.md +455 -0
- package/docs/troubleshooting/faq.md +472 -472
- package/docs/tutorials/basic-examples.md +717 -717
- package/docs/wiki/API-Reference.md +245 -245
- package/docs/wiki/Complete-Documentation.md +122 -122
- package/examples/alteriom/README.md +139 -81
- package/examples/alteriom/alteriom.ino +186 -185
- package/examples/alteriom/alteriom_sensor_package.hpp +240 -127
- package/examples/alteriom/platformio.ini +24 -24
- package/examples/alteriomImproved/alteriom_sensor_package.hpp +224 -0
- package/examples/{alteriom → alteriomImproved}/improved_sensor_node.ino +245 -245
- package/examples/alteriomImproved/platformio.ini +25 -0
- package/examples/alteriomPhase1/alteriom_sensor_package.hpp +224 -0
- package/examples/alteriomPhase1/phase1_features.ino +242 -0
- package/examples/alteriomPhase1/platformio.ini +25 -0
- package/examples/alteriomPhase2/alteriom_sensor_package.hpp +224 -0
- package/examples/alteriomPhase2/phase2_features.ino +186 -0
- package/examples/alteriomPhase2/platformio.ini +25 -0
- package/examples/{alteriom → alteriomSensorNode}/alteriom_sensor_node.ino +183 -183
- package/examples/alteriomSensorNode/alteriom_sensor_package.hpp +224 -0
- package/examples/alteriomSensorNode/platformio.ini +25 -0
- package/examples/basic/basic.ino +66 -66
- package/examples/basic/platformio.ini +25 -25
- package/examples/bridge/bridge.ino +51 -51
- package/examples/bridge/mesh_event_publisher.hpp +253 -0
- package/examples/bridge/mesh_topology_reporter.hpp +303 -0
- package/examples/bridge/mqtt_command_bridge.hpp +459 -0
- package/examples/bridge/mqtt_status_bridge.hpp +519 -0
- package/examples/bridge/platformio.ini +25 -25
- package/examples/echoNode/echoNode.ino +33 -33
- package/examples/echoNode/platformio.ini +25 -25
- package/examples/logClient/logClient.ino +109 -109
- package/examples/logClient/platformio.ini +25 -25
- package/examples/logServer/logServer.ino +81 -81
- package/examples/logServer/platformio.ini +25 -25
- package/examples/meshCommandNode/alteriom_sensor_package.hpp +235 -0
- package/examples/meshCommandNode/meshCommandNode.ino +263 -0
- package/examples/meshCommandNode/platformio.ini +25 -0
- package/examples/mqttBridge/mqttBridge.ino +118 -118
- package/examples/mqttBridge/platformio.ini +26 -26
- package/examples/mqttCommandBridge/alteriom_sensor_package.hpp +235 -0
- package/examples/mqttCommandBridge/mesh_event_publisher.hpp +253 -0
- package/examples/mqttCommandBridge/mesh_topology_reporter.hpp +303 -0
- package/examples/mqttCommandBridge/mqttCommandBridge.ino +252 -0
- package/examples/mqttCommandBridge/mqtt_command_bridge.hpp +459 -0
- package/examples/mqttCommandBridge/platformio.ini +26 -0
- package/examples/mqttStatusBridge/mqttStatusBridge.ino +216 -0
- package/examples/mqttStatusBridge/mqtt_status_bridge.hpp +522 -0
- package/examples/mqttStatusBridge/platformio.ini +26 -0
- package/examples/mqttTopologyTest/README.md +467 -0
- package/examples/mqttTopologyTest/mqttTopologyTest.ino +748 -0
- package/examples/mqttTopologyTest/platformio.ini +26 -0
- package/examples/namedMesh/namedMesh.ino +97 -97
- package/examples/namedMesh/platformio.ini +25 -25
- package/examples/otaReceiver/otaReceiver.ino +79 -79
- package/examples/otaReceiver/platformio.ini +25 -25
- package/examples/otaSender/otaSender.ino +160 -151
- package/examples/otaSender/platformio.ini +25 -25
- package/examples/startHere/platformio.ini +25 -25
- package/examples/startHere/startHere.ino +159 -159
- package/examples/webServer/platformio.ini +27 -27
- package/examples/webServer/webServer.ino +89 -89
- package/keywords.txt +48 -48
- package/library.json +55 -34
- package/library.properties +10 -10
- package/package.json +86 -78
- package/src/AlteriomPainlessMesh.h +97 -97
- package/src/arduino/wifi.hpp +365 -365
- package/src/boost/asynctcp.hpp +279 -279
- package/src/painlessMesh.h +70 -70
- package/src/painlessMeshSTA.cpp +236 -236
- package/src/painlessMeshSTA.h +58 -58
- package/src/painlessTaskOptions.h +4 -4
- package/src/painlessmesh/base64.hpp +111 -111
- package/src/painlessmesh/buffer.hpp +229 -229
- package/src/painlessmesh/callback.hpp +91 -91
- package/src/painlessmesh/configuration.hpp +77 -77
- package/src/painlessmesh/connection.hpp +192 -192
- package/src/painlessmesh/layout.hpp +188 -188
- package/src/painlessmesh/logger.hpp +158 -158
- package/src/painlessmesh/memory.hpp +119 -119
- package/src/painlessmesh/mesh.hpp +761 -560
- package/src/painlessmesh/metrics.hpp +322 -322
- package/src/painlessmesh/ntp.hpp +263 -263
- package/src/painlessmesh/ota.hpp +582 -553
- package/src/painlessmesh/plugin.hpp +188 -188
- package/src/painlessmesh/protocol.hpp +813 -813
- package/src/painlessmesh/router.hpp +338 -322
- package/src/painlessmesh/tcp.hpp +71 -71
- package/src/painlessmesh/validation.hpp +238 -238
- package/src/plugin/performance.hpp +214 -214
- package/src/plugin/remote.hpp +64 -64
- package/src/scheduler.cpp +10 -10
- package/src/wifi.cpp +2 -2
|
@@ -1,322 +1,338 @@
|
|
|
1
|
-
#ifndef _PAINLESS_MESH_ROUTER_HPP_
|
|
2
|
-
#define _PAINLESS_MESH_ROUTER_HPP_
|
|
3
|
-
|
|
4
|
-
#include <algorithm>
|
|
5
|
-
#include <memory>
|
|
6
|
-
|
|
7
|
-
#include "painlessmesh/callback.hpp"
|
|
8
|
-
#include "painlessmesh/layout.hpp"
|
|
9
|
-
#include "painlessmesh/logger.hpp"
|
|
10
|
-
#include "painlessmesh/protocol.hpp"
|
|
11
|
-
|
|
12
|
-
extern painlessmesh::logger::LogClass Log;
|
|
13
|
-
|
|
14
|
-
namespace painlessmesh {
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Helper functions to route messages
|
|
18
|
-
*/
|
|
19
|
-
namespace router {
|
|
20
|
-
template <class T>
|
|
21
|
-
std::shared_ptr<T> findRoute(layout::Layout<T> tree,
|
|
22
|
-
std::function<bool(std::shared_ptr<T>)> func) {
|
|
23
|
-
auto route = std::find_if(tree.subs.begin(), tree.subs.end(), func);
|
|
24
|
-
if (route == tree.subs.end()) return NULL;
|
|
25
|
-
return (*route);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
template <class T>
|
|
29
|
-
std::shared_ptr<T> findRoute(layout::Layout<T> tree, uint32_t nodeId) {
|
|
30
|
-
return findRoute<T>(tree, [nodeId](std::shared_ptr<T> s) {
|
|
31
|
-
return layout::contains((*s), nodeId);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
template <class T, class U>
|
|
36
|
-
bool send(T& package, std::shared_ptr<U> conn, bool priority = false) {
|
|
37
|
-
painlessmesh::protocol::Variant variant(package);
|
|
38
|
-
TSTRING msg;
|
|
39
|
-
variant.printTo(msg);
|
|
40
|
-
return conn->addMessage(msg, priority);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
template <class T, class U>
|
|
44
|
-
bool send(T&& package, std::shared_ptr<U> conn, bool priority = false) {
|
|
45
|
-
painlessmesh::protocol::Variant variant(package);
|
|
46
|
-
TSTRING msg;
|
|
47
|
-
variant.printTo(msg);
|
|
48
|
-
return conn->addMessage(msg, priority);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
template <class U>
|
|
52
|
-
bool send(protocol::Variant& variant, std::shared_ptr<U> conn,
|
|
53
|
-
bool priority = false) {
|
|
54
|
-
TSTRING msg;
|
|
55
|
-
variant.printTo(msg);
|
|
56
|
-
return conn->addMessage(msg, priority);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
template <class U>
|
|
60
|
-
bool send(protocol::Variant&& variant, std::shared_ptr<U> conn,
|
|
61
|
-
bool priority = false) {
|
|
62
|
-
TSTRING msg;
|
|
63
|
-
variant.printTo(msg);
|
|
64
|
-
return conn->addMessage(msg, priority);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
template <class T, class U>
|
|
68
|
-
bool send(T& package, layout::Layout<U> layout) {
|
|
69
|
-
painlessmesh::protocol::Variant variant(package);
|
|
70
|
-
TSTRING msg;
|
|
71
|
-
variant.printTo(msg);
|
|
72
|
-
auto conn = findRoute<U>(layout, variant.dest());
|
|
73
|
-
if (conn) return conn->addMessage(msg);
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
template <class U>
|
|
78
|
-
bool send(protocol::Variant& variant, layout::Layout<U> layout) {
|
|
79
|
-
TSTRING msg;
|
|
80
|
-
variant.printTo(msg);
|
|
81
|
-
auto conn = findRoute<U>(layout, variant.dest());
|
|
82
|
-
if (conn) return conn->addMessage(msg);
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
template <class T, class U>
|
|
87
|
-
bool send(T&& package, layout::Layout<U> layout) {
|
|
88
|
-
painlessmesh::protocol::Variant variant(package);
|
|
89
|
-
TSTRING msg;
|
|
90
|
-
variant.printTo(msg);
|
|
91
|
-
auto conn = findRoute<U>(layout, variant.dest());
|
|
92
|
-
if (conn) return conn->addMessage(msg);
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
template <class U>
|
|
97
|
-
bool send(protocol::Variant&& variant, layout::Layout<U> layout) {
|
|
98
|
-
TSTRING msg;
|
|
99
|
-
variant.printTo(msg);
|
|
100
|
-
auto conn = findRoute<U>(layout, variant.dest());
|
|
101
|
-
if (conn) return conn->addMessage(msg);
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
template <class T, class U>
|
|
106
|
-
size_t broadcast(T& package, layout::Layout<U> layout, uint32_t exclude) {
|
|
107
|
-
painlessmesh::protocol::Variant variant(package);
|
|
108
|
-
TSTRING msg;
|
|
109
|
-
variant.printTo(msg);
|
|
110
|
-
size_t i = 0;
|
|
111
|
-
for (auto&& conn : layout.subs) {
|
|
112
|
-
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
113
|
-
auto sent = conn->addMessage(msg);
|
|
114
|
-
if (sent) ++i;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return i;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
template <class T, class U>
|
|
121
|
-
size_t broadcast(T&& package, layout::Layout<U> layout, uint32_t exclude) {
|
|
122
|
-
painlessmesh::protocol::Variant variant(package);
|
|
123
|
-
TSTRING msg;
|
|
124
|
-
variant.printTo(msg);
|
|
125
|
-
size_t i = 0;
|
|
126
|
-
for (auto&& conn : layout.subs) {
|
|
127
|
-
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
128
|
-
auto sent = conn->addMessage(msg);
|
|
129
|
-
if (sent) ++i;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return i;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
template <class T>
|
|
136
|
-
size_t broadcast(protocol::Variant& variant, layout::Layout<T> layout,
|
|
137
|
-
uint32_t exclude) {
|
|
138
|
-
TSTRING msg;
|
|
139
|
-
variant.printTo(msg);
|
|
140
|
-
size_t i = 0;
|
|
141
|
-
for (auto&& conn : layout.subs) {
|
|
142
|
-
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
143
|
-
auto sent = conn->addMessage(msg);
|
|
144
|
-
if (sent) ++i;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return i;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
template <class T>
|
|
151
|
-
size_t broadcast(protocol::Variant&& variant, layout::Layout<T> layout,
|
|
152
|
-
uint32_t exclude) {
|
|
153
|
-
TSTRING msg;
|
|
154
|
-
variant.printTo(msg);
|
|
155
|
-
size_t i = 0;
|
|
156
|
-
for (auto&& conn : layout.subs) {
|
|
157
|
-
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
158
|
-
auto sent = conn->addMessage(msg);
|
|
159
|
-
if (sent) ++i;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return i;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
template <class T>
|
|
166
|
-
void routePackage(layout::Layout<T> layout, std::shared_ptr<T> connection,
|
|
167
|
-
const TSTRING& pkg, callback::MeshPackageCallbackList<T> cbl,
|
|
168
|
-
uint32_t receivedAt) {
|
|
169
|
-
using namespace logger;
|
|
170
|
-
Log(COMMUNICATION, "routePackage(): Recvd from %u: %s\n", connection->nodeId,
|
|
171
|
-
pkg.c_str());
|
|
172
|
-
#if ARDUINOJSON_VERSION_MAJOR == 7
|
|
173
|
-
protocol::Variant variant(pkg);
|
|
174
|
-
if (variant.error) {
|
|
175
|
-
Log(ERROR,
|
|
176
|
-
"routePackage(): parsing failed. err=%u, total_length=%d, data=%s<--\n",
|
|
177
|
-
variant.error, pkg.length(), pkg.c_str());
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (variant.routing() == SINGLE && variant.dest() != layout.getNodeId()) {
|
|
182
|
-
// Send on without further processing
|
|
183
|
-
send<T>(variant, layout);
|
|
184
|
-
return;
|
|
185
|
-
} else if (variant.routing() == BROADCAST) {
|
|
186
|
-
broadcast<T>(variant, layout, connection->nodeId);
|
|
187
|
-
}
|
|
188
|
-
auto calls = cbl.execute(variant.type(), variant, connection, receivedAt);
|
|
189
|
-
if (calls == 0)
|
|
190
|
-
Log(DEBUG, "routePackage(): No callbacks executed; %u, %s\n",
|
|
191
|
-
variant.type(), pkg.c_str());
|
|
192
|
-
#else
|
|
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
|
-
mesh.
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
mesh.
|
|
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
|
-
|
|
1
|
+
#ifndef _PAINLESS_MESH_ROUTER_HPP_
|
|
2
|
+
#define _PAINLESS_MESH_ROUTER_HPP_
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <memory>
|
|
6
|
+
|
|
7
|
+
#include "painlessmesh/callback.hpp"
|
|
8
|
+
#include "painlessmesh/layout.hpp"
|
|
9
|
+
#include "painlessmesh/logger.hpp"
|
|
10
|
+
#include "painlessmesh/protocol.hpp"
|
|
11
|
+
|
|
12
|
+
extern painlessmesh::logger::LogClass Log;
|
|
13
|
+
|
|
14
|
+
namespace painlessmesh {
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Helper functions to route messages
|
|
18
|
+
*/
|
|
19
|
+
namespace router {
|
|
20
|
+
template <class T>
|
|
21
|
+
std::shared_ptr<T> findRoute(layout::Layout<T> tree,
|
|
22
|
+
std::function<bool(std::shared_ptr<T>)> func) {
|
|
23
|
+
auto route = std::find_if(tree.subs.begin(), tree.subs.end(), func);
|
|
24
|
+
if (route == tree.subs.end()) return NULL;
|
|
25
|
+
return (*route);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
template <class T>
|
|
29
|
+
std::shared_ptr<T> findRoute(layout::Layout<T> tree, uint32_t nodeId) {
|
|
30
|
+
return findRoute<T>(tree, [nodeId](std::shared_ptr<T> s) {
|
|
31
|
+
return layout::contains((*s), nodeId);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
template <class T, class U>
|
|
36
|
+
bool send(T& package, std::shared_ptr<U> conn, bool priority = false) {
|
|
37
|
+
painlessmesh::protocol::Variant variant(package);
|
|
38
|
+
TSTRING msg;
|
|
39
|
+
variant.printTo(msg);
|
|
40
|
+
return conn->addMessage(msg, priority);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
template <class T, class U>
|
|
44
|
+
bool send(T&& package, std::shared_ptr<U> conn, bool priority = false) {
|
|
45
|
+
painlessmesh::protocol::Variant variant(package);
|
|
46
|
+
TSTRING msg;
|
|
47
|
+
variant.printTo(msg);
|
|
48
|
+
return conn->addMessage(msg, priority);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
template <class U>
|
|
52
|
+
bool send(protocol::Variant& variant, std::shared_ptr<U> conn,
|
|
53
|
+
bool priority = false) {
|
|
54
|
+
TSTRING msg;
|
|
55
|
+
variant.printTo(msg);
|
|
56
|
+
return conn->addMessage(msg, priority);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
template <class U>
|
|
60
|
+
bool send(protocol::Variant&& variant, std::shared_ptr<U> conn,
|
|
61
|
+
bool priority = false) {
|
|
62
|
+
TSTRING msg;
|
|
63
|
+
variant.printTo(msg);
|
|
64
|
+
return conn->addMessage(msg, priority);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
template <class T, class U>
|
|
68
|
+
bool send(T& package, layout::Layout<U> layout) {
|
|
69
|
+
painlessmesh::protocol::Variant variant(package);
|
|
70
|
+
TSTRING msg;
|
|
71
|
+
variant.printTo(msg);
|
|
72
|
+
auto conn = findRoute<U>(layout, variant.dest());
|
|
73
|
+
if (conn) return conn->addMessage(msg);
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
template <class U>
|
|
78
|
+
bool send(protocol::Variant& variant, layout::Layout<U> layout) {
|
|
79
|
+
TSTRING msg;
|
|
80
|
+
variant.printTo(msg);
|
|
81
|
+
auto conn = findRoute<U>(layout, variant.dest());
|
|
82
|
+
if (conn) return conn->addMessage(msg);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
template <class T, class U>
|
|
87
|
+
bool send(T&& package, layout::Layout<U> layout) {
|
|
88
|
+
painlessmesh::protocol::Variant variant(package);
|
|
89
|
+
TSTRING msg;
|
|
90
|
+
variant.printTo(msg);
|
|
91
|
+
auto conn = findRoute<U>(layout, variant.dest());
|
|
92
|
+
if (conn) return conn->addMessage(msg);
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
template <class U>
|
|
97
|
+
bool send(protocol::Variant&& variant, layout::Layout<U> layout) {
|
|
98
|
+
TSTRING msg;
|
|
99
|
+
variant.printTo(msg);
|
|
100
|
+
auto conn = findRoute<U>(layout, variant.dest());
|
|
101
|
+
if (conn) return conn->addMessage(msg);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
template <class T, class U>
|
|
106
|
+
size_t broadcast(T& package, layout::Layout<U> layout, uint32_t exclude) {
|
|
107
|
+
painlessmesh::protocol::Variant variant(package);
|
|
108
|
+
TSTRING msg;
|
|
109
|
+
variant.printTo(msg);
|
|
110
|
+
size_t i = 0;
|
|
111
|
+
for (auto&& conn : layout.subs) {
|
|
112
|
+
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
113
|
+
auto sent = conn->addMessage(msg);
|
|
114
|
+
if (sent) ++i;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return i;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
template <class T, class U>
|
|
121
|
+
size_t broadcast(T&& package, layout::Layout<U> layout, uint32_t exclude) {
|
|
122
|
+
painlessmesh::protocol::Variant variant(package);
|
|
123
|
+
TSTRING msg;
|
|
124
|
+
variant.printTo(msg);
|
|
125
|
+
size_t i = 0;
|
|
126
|
+
for (auto&& conn : layout.subs) {
|
|
127
|
+
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
128
|
+
auto sent = conn->addMessage(msg);
|
|
129
|
+
if (sent) ++i;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return i;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
template <class T>
|
|
136
|
+
size_t broadcast(protocol::Variant& variant, layout::Layout<T> layout,
|
|
137
|
+
uint32_t exclude) {
|
|
138
|
+
TSTRING msg;
|
|
139
|
+
variant.printTo(msg);
|
|
140
|
+
size_t i = 0;
|
|
141
|
+
for (auto&& conn : layout.subs) {
|
|
142
|
+
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
143
|
+
auto sent = conn->addMessage(msg);
|
|
144
|
+
if (sent) ++i;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return i;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
template <class T>
|
|
151
|
+
size_t broadcast(protocol::Variant&& variant, layout::Layout<T> layout,
|
|
152
|
+
uint32_t exclude) {
|
|
153
|
+
TSTRING msg;
|
|
154
|
+
variant.printTo(msg);
|
|
155
|
+
size_t i = 0;
|
|
156
|
+
for (auto&& conn : layout.subs) {
|
|
157
|
+
if (conn->nodeId != 0 && conn->nodeId != exclude) {
|
|
158
|
+
auto sent = conn->addMessage(msg);
|
|
159
|
+
if (sent) ++i;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return i;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
template <class T>
|
|
166
|
+
void routePackage(layout::Layout<T> layout, std::shared_ptr<T> connection,
|
|
167
|
+
const TSTRING& pkg, callback::MeshPackageCallbackList<T> cbl,
|
|
168
|
+
uint32_t receivedAt) {
|
|
169
|
+
using namespace logger;
|
|
170
|
+
Log(COMMUNICATION, "routePackage(): Recvd from %u: %s\n", connection->nodeId,
|
|
171
|
+
pkg.c_str());
|
|
172
|
+
#if ARDUINOJSON_VERSION_MAJOR == 7
|
|
173
|
+
protocol::Variant variant(pkg);
|
|
174
|
+
if (variant.error) {
|
|
175
|
+
Log(ERROR,
|
|
176
|
+
"routePackage(): parsing failed. err=%u, total_length=%d, data=%s<--\n",
|
|
177
|
+
variant.error, pkg.length(), pkg.c_str());
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (variant.routing() == SINGLE && variant.dest() != layout.getNodeId()) {
|
|
182
|
+
// Send on without further processing
|
|
183
|
+
send<T>(variant, layout);
|
|
184
|
+
return;
|
|
185
|
+
} else if (variant.routing() == BROADCAST) {
|
|
186
|
+
broadcast<T>(variant, layout, connection->nodeId);
|
|
187
|
+
}
|
|
188
|
+
auto calls = cbl.execute(variant.type(), variant, connection, receivedAt);
|
|
189
|
+
if (calls == 0)
|
|
190
|
+
Log(DEBUG, "routePackage(): No callbacks executed; %u, %s\n",
|
|
191
|
+
variant.type(), pkg.c_str());
|
|
192
|
+
#else
|
|
193
|
+
// Calculate required capacity based on message size and nesting depth
|
|
194
|
+
// Fixed capacity approach to avoid segmentation fault issues with
|
|
195
|
+
// dynamic reallocation (see issue #521 and CODE_REFACTORING_RECOMMENDATIONS.md)
|
|
196
|
+
size_t nestingDepth = std::count(pkg.begin(), pkg.end(), '{') +
|
|
197
|
+
std::count(pkg.begin(), pkg.end(), '[');
|
|
198
|
+
|
|
199
|
+
#if ARDUINOJSON_VERSION_MAJOR >= 7
|
|
200
|
+
// ArduinoJson v7: automatic capacity management, use generous buffer
|
|
201
|
+
size_t calculatedCapacity = pkg.length() + 1024;
|
|
202
|
+
#else
|
|
203
|
+
// ArduinoJson v6: manual capacity calculation required
|
|
204
|
+
// Base capacity: message length + overhead for JSON structure
|
|
205
|
+
// Each nesting level adds overhead for pointers and metadata
|
|
206
|
+
size_t calculatedCapacity = pkg.length() +
|
|
207
|
+
JSON_OBJECT_SIZE(10) * std::max(nestingDepth, size_t(1)) +
|
|
208
|
+
512; // Additional buffer for strings and padding
|
|
209
|
+
#endif
|
|
210
|
+
|
|
211
|
+
// Cap at 8KB for safety on ESP8266 (which has ~80KB total heap)
|
|
212
|
+
// Messages larger than this should be rejected
|
|
213
|
+
constexpr size_t MAX_MESSAGE_CAPACITY = 8192;
|
|
214
|
+
size_t capacity = std::min(calculatedCapacity, MAX_MESSAGE_CAPACITY);
|
|
215
|
+
|
|
216
|
+
auto variant = std::make_shared<protocol::Variant>(pkg, capacity);
|
|
217
|
+
|
|
218
|
+
if (variant->error) {
|
|
219
|
+
if (variant->error == DeserializationError::NoMemory) {
|
|
220
|
+
Log(ERROR,
|
|
221
|
+
"routePackage(): Message too large. length=%d, calculated_capacity=%u, "
|
|
222
|
+
"nesting_depth=%u. Consider increasing MAX_MESSAGE_CAPACITY if needed.\n",
|
|
223
|
+
pkg.length(), calculatedCapacity, nestingDepth);
|
|
224
|
+
} else {
|
|
225
|
+
Log(ERROR,
|
|
226
|
+
"routePackage(): parsing failed. err=%u, length=%d, data=%s<--\n",
|
|
227
|
+
variant->error, pkg.length(), pkg.c_str());
|
|
228
|
+
}
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (variant->routing() == SINGLE && variant->dest() != layout.getNodeId()) {
|
|
233
|
+
// Send on without further processing
|
|
234
|
+
send<T>((*variant), layout);
|
|
235
|
+
return;
|
|
236
|
+
} else if (variant->routing() == BROADCAST) {
|
|
237
|
+
broadcast<T>((*variant), layout, connection->nodeId);
|
|
238
|
+
}
|
|
239
|
+
auto calls = cbl.execute(variant->type(), (*variant), connection, receivedAt);
|
|
240
|
+
if (calls == 0)
|
|
241
|
+
Log(DEBUG, "routePackage(): No callbacks executed; %u, %s\n",
|
|
242
|
+
variant->type(), pkg.c_str());
|
|
243
|
+
#endif
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
template <class T, class U>
|
|
247
|
+
void handleNodeSync(T& mesh, protocol::NodeTree newTree,
|
|
248
|
+
std::shared_ptr<U> conn) {
|
|
249
|
+
Log(logger::SYNC, "handleNodeSync(): with %u\n", conn->nodeId);
|
|
250
|
+
|
|
251
|
+
if (!conn->validSubs(newTree)) {
|
|
252
|
+
Log(logger::SYNC, "handleNodeSync(): invalid new connection\n");
|
|
253
|
+
Log.remote("Invalid connection to %u\n", conn->nodeId);
|
|
254
|
+
conn->close();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (conn->newConnection) {
|
|
259
|
+
auto oldConnection = router::findRoute<U>(mesh, newTree.nodeId);
|
|
260
|
+
if (oldConnection) {
|
|
261
|
+
Log(logger::SYNC,
|
|
262
|
+
"handleNodeSync(): already connected to %u. Closing the new "
|
|
263
|
+
"connection \n",
|
|
264
|
+
newTree.nodeId);
|
|
265
|
+
Log.remote("Already connected to %u\n", newTree.nodeId);
|
|
266
|
+
conn->close();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
auto remoteNodeId = newTree.nodeId;
|
|
270
|
+
mesh.addTask([&mesh, remoteNodeId]() {
|
|
271
|
+
Log(logger::CONNECTION, "newConnectionTask():\n");
|
|
272
|
+
Log(logger::CONNECTION, "newConnectionTask(): adding %u now= %u\n",
|
|
273
|
+
remoteNodeId, mesh.getNodeTime());
|
|
274
|
+
mesh.newConnectionCallbacks.execute(remoteNodeId);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Initially interval is every 10 seconds,
|
|
278
|
+
// this will slow down to TIME_SYNC_INTERVAL
|
|
279
|
+
// after first succesfull sync
|
|
280
|
+
// TODO move it to a new connection callback and use initTimeSync from
|
|
281
|
+
// ntp.hpp
|
|
282
|
+
conn->timeSyncTask.set(10 * TASK_SECOND, TASK_FOREVER, [conn, &mesh]() {
|
|
283
|
+
Log(logger::S_TIME, "timeSyncTask(): %u\n", conn->nodeId);
|
|
284
|
+
mesh.startTimeSync(conn);
|
|
285
|
+
});
|
|
286
|
+
mesh.mScheduler->addTask(conn->timeSyncTask);
|
|
287
|
+
if (conn->station)
|
|
288
|
+
// We are STA, request time immediately
|
|
289
|
+
conn->timeSyncTask.enable();
|
|
290
|
+
else
|
|
291
|
+
// We are the AP, give STA the change to initiate time sync
|
|
292
|
+
conn->timeSyncTask.enableDelayed();
|
|
293
|
+
conn->newConnection = false;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (conn->updateSubs(newTree)) {
|
|
297
|
+
auto nodeId = newTree.nodeId;
|
|
298
|
+
mesh.addTask(
|
|
299
|
+
[&mesh, nodeId]() { mesh.changedConnectionCallbacks.execute(nodeId); });
|
|
300
|
+
} else {
|
|
301
|
+
conn->nodeSyncTask.delay();
|
|
302
|
+
mesh.stability += std::min(1000 - mesh.stability, (size_t)25);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
template <class T, typename U>
|
|
307
|
+
callback::MeshPackageCallbackList<U> addPackageCallback(
|
|
308
|
+
callback::MeshPackageCallbackList<U>&& callbackList, T& mesh) {
|
|
309
|
+
// REQUEST type,
|
|
310
|
+
callbackList.onPackage(
|
|
311
|
+
protocol::NODE_SYNC_REQUEST,
|
|
312
|
+
[&mesh](protocol::Variant& variant, std::shared_ptr<U> connection,
|
|
313
|
+
uint32_t receivedAt) {
|
|
314
|
+
auto newTree = variant.to<protocol::NodeSyncRequest>();
|
|
315
|
+
handleNodeSync<T, U>(mesh, newTree, connection);
|
|
316
|
+
send<protocol::NodeSyncReply>(
|
|
317
|
+
connection->reply(std::move(mesh.asNodeTree())), connection, true);
|
|
318
|
+
return false;
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// Reply type just handle it
|
|
322
|
+
callbackList.onPackage(
|
|
323
|
+
protocol::NODE_SYNC_REPLY,
|
|
324
|
+
[&mesh](protocol::Variant& variant, std::shared_ptr<U> connection,
|
|
325
|
+
uint32_t receivedAt) {
|
|
326
|
+
auto newTree = variant.to<protocol::NodeSyncReply>();
|
|
327
|
+
handleNodeSync<T, U>(mesh, newTree, connection);
|
|
328
|
+
connection->timeOutTask.disable();
|
|
329
|
+
return false;
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
return callbackList;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
} // namespace router
|
|
336
|
+
} // namespace painlessmesh
|
|
337
|
+
|
|
338
|
+
#endif
|