@alteriom/painlessmesh 1.6.1
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 +144 -0
- package/LICENSE +674 -0
- package/README.md +434 -0
- package/RELEASE_GUIDE.md +419 -0
- package/docs/DOCUMENTATION_MIGRATION_PLAN.md +176 -0
- package/docs/README.md +71 -0
- package/docs/alteriom/overview.md +508 -0
- package/docs/api/core-api.md +607 -0
- package/docs/architecture/mesh-architecture.md +379 -0
- package/docs/architecture/plugin-system.md +517 -0
- package/docs/getting-started/first-mesh.md +410 -0
- package/docs/getting-started/installation.md +275 -0
- package/docs/getting-started/quickstart.md +158 -0
- package/docs/improvements/README.md +69 -0
- package/docs/troubleshooting/common-issues.md +521 -0
- package/docs/troubleshooting/faq.md +473 -0
- package/docs/tutorials/basic-examples.md +718 -0
- package/docs/wiki/API-Reference.md +246 -0
- package/docs/wiki/Complete-Documentation.md +123 -0
- package/examples/alteriom/README.md +82 -0
- package/examples/alteriom/alteriom.ino +186 -0
- package/examples/alteriom/alteriom_sensor_node.ino +184 -0
- package/examples/alteriom/alteriom_sensor_package.hpp +128 -0
- package/examples/alteriom/improved_sensor_node.ino +246 -0
- package/examples/alteriom/platformio.ini +25 -0
- package/examples/basic/basic.ino +66 -0
- package/examples/basic/platformio.ini +25 -0
- package/examples/bridge/bridge.ino +51 -0
- package/examples/bridge/platformio.ini +25 -0
- package/examples/echoNode/echoNode.ino +33 -0
- package/examples/echoNode/platformio.ini +25 -0
- package/examples/logClient/logClient.ino +109 -0
- package/examples/logClient/platformio.ini +25 -0
- package/examples/logServer/logServer.ino +81 -0
- package/examples/logServer/platformio.ini +25 -0
- package/examples/mqttBridge/mqttBridge.ino +118 -0
- package/examples/mqttBridge/platformio.ini +26 -0
- package/examples/namedMesh/namedMesh.ino +97 -0
- package/examples/namedMesh/platformio.ini +25 -0
- package/examples/otaReceiver/otaReceiver.ino +79 -0
- package/examples/otaReceiver/platformio.ini +25 -0
- package/examples/otaSender/nodemcu32s_connections.JPG +0 -0
- package/examples/otaSender/otaSender.ino +151 -0
- package/examples/otaSender/platformio.ini +25 -0
- package/examples/startHere/platformio.ini +25 -0
- package/examples/startHere/startHere.ino +159 -0
- package/examples/webServer/platformio.ini +27 -0
- package/examples/webServer/webServer.ino +89 -0
- package/keywords.txt +49 -0
- package/library.json +34 -0
- package/library.properties +11 -0
- package/package.json +78 -0
- package/src/AlteriomPainlessMesh.h +98 -0
- package/src/arduino/wifi.hpp +365 -0
- package/src/boost/asynctcp.hpp +279 -0
- package/src/painlessMesh.h +70 -0
- package/src/painlessMeshSTA.cpp +236 -0
- package/src/painlessMeshSTA.h +58 -0
- package/src/painlessTaskOptions.h +4 -0
- package/src/painlessmesh/base64.hpp +111 -0
- package/src/painlessmesh/buffer.hpp +229 -0
- package/src/painlessmesh/callback.hpp +91 -0
- package/src/painlessmesh/configuration.hpp +77 -0
- package/src/painlessmesh/connection.hpp +192 -0
- package/src/painlessmesh/layout.hpp +188 -0
- package/src/painlessmesh/logger.hpp +158 -0
- package/src/painlessmesh/memory.hpp +120 -0
- package/src/painlessmesh/mesh.hpp +560 -0
- package/src/painlessmesh/metrics.hpp +323 -0
- package/src/painlessmesh/ntp.hpp +263 -0
- package/src/painlessmesh/ota.hpp +553 -0
- package/src/painlessmesh/plugin.hpp +188 -0
- package/src/painlessmesh/protocol.hpp +813 -0
- package/src/painlessmesh/router.hpp +322 -0
- package/src/painlessmesh/tcp.hpp +71 -0
- package/src/painlessmesh/validation.hpp +239 -0
- package/src/plugin/performance.hpp +214 -0
- package/src/plugin/remote.hpp +64 -0
- package/src/scheduler.cpp +10 -0
- package/src/wifi.cpp +2 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#ifndef _PAINLESS_MESH_BASE64_HPP_
|
|
2
|
+
#define _PAINLESS_MESH_BASE64_HPP_
|
|
3
|
+
|
|
4
|
+
#include <string>
|
|
5
|
+
|
|
6
|
+
#include "painlessmesh/configuration.hpp"
|
|
7
|
+
|
|
8
|
+
namespace painlessmesh {
|
|
9
|
+
namespace base64 {
|
|
10
|
+
|
|
11
|
+
static inline bool is_base64(unsigned char c) {
|
|
12
|
+
return (isalnum(c) || (c == '+') || (c == '/'));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static const TSTRING chars =
|
|
16
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
17
|
+
"abcdefghijklmnopqrstuvwxyz"
|
|
18
|
+
"0123456789+/";
|
|
19
|
+
|
|
20
|
+
inline TSTRING encode(unsigned char const* bytes_to_encode,
|
|
21
|
+
unsigned int in_len) {
|
|
22
|
+
TSTRING ret;
|
|
23
|
+
int i = 0;
|
|
24
|
+
int j = 0;
|
|
25
|
+
unsigned char char_array_3[3];
|
|
26
|
+
unsigned char char_array_4[4];
|
|
27
|
+
|
|
28
|
+
while (in_len--) {
|
|
29
|
+
char_array_3[i++] = *(bytes_to_encode++);
|
|
30
|
+
if (i == 3) {
|
|
31
|
+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
|
32
|
+
char_array_4[1] =
|
|
33
|
+
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
|
34
|
+
char_array_4[2] =
|
|
35
|
+
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
|
36
|
+
char_array_4[3] = char_array_3[2] & 0x3f;
|
|
37
|
+
|
|
38
|
+
for (i = 0; (i < 4); i++) ret += chars[char_array_4[i]];
|
|
39
|
+
i = 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (i) {
|
|
44
|
+
for (j = i; j < 3; j++) char_array_3[j] = '\0';
|
|
45
|
+
|
|
46
|
+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
|
47
|
+
char_array_4[1] =
|
|
48
|
+
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
|
49
|
+
char_array_4[2] =
|
|
50
|
+
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
|
51
|
+
char_array_4[3] = char_array_3[2] & 0x3f;
|
|
52
|
+
for (j = 0; (j < i + 1); j++) ret += chars[char_array_4[j]];
|
|
53
|
+
while ((i++ < 3)) ret += '=';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return ret;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
inline TSTRING encode(const TSTRING& str64) {
|
|
60
|
+
return encode((unsigned char*)str64.c_str(), str64.length());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static const int B64index[256] = {
|
|
64
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
65
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
66
|
+
0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57,
|
|
67
|
+
58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
|
|
68
|
+
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
|
69
|
+
25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
|
|
70
|
+
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
|
|
71
|
+
|
|
72
|
+
inline const TSTRING decode(const void* data, const size_t& len) {
|
|
73
|
+
if (len == 0) return "";
|
|
74
|
+
|
|
75
|
+
unsigned char* p = (unsigned char*)data;
|
|
76
|
+
size_t j = 0, pad1 = len % 4 || p[len - 1] == '=',
|
|
77
|
+
pad2 = pad1 && (len % 4 > 2 || p[len - 2] != '=');
|
|
78
|
+
const size_t last = (len - pad1) / 4 << 2;
|
|
79
|
+
#ifdef PAINLESSMESH_ENABLE_STD_STRING
|
|
80
|
+
TSTRING result(last / 4 * 3 + pad1 + pad2, '\0');
|
|
81
|
+
#else
|
|
82
|
+
TSTRING result;
|
|
83
|
+
result.reserve(last / 4 * 3 + pad1 + pad2);
|
|
84
|
+
for (size_t i = 0; i < last / 4 * 3 + pad1 + pad2; ++i) result.concat('\0');
|
|
85
|
+
#endif
|
|
86
|
+
unsigned char* str = (unsigned char*)&result[0];
|
|
87
|
+
|
|
88
|
+
for (size_t i = 0; i < last; i += 4) {
|
|
89
|
+
int n = B64index[p[i]] << 18 | B64index[p[i + 1]] << 12 |
|
|
90
|
+
B64index[p[i + 2]] << 6 | B64index[p[i + 3]];
|
|
91
|
+
str[j++] = n >> 16;
|
|
92
|
+
str[j++] = n >> 8 & 0xFF;
|
|
93
|
+
str[j++] = n & 0xFF;
|
|
94
|
+
}
|
|
95
|
+
if (pad1) {
|
|
96
|
+
int n = B64index[p[last]] << 18 | B64index[p[last + 1]] << 12;
|
|
97
|
+
str[j++] = n >> 16;
|
|
98
|
+
if (pad2) {
|
|
99
|
+
n |= B64index[p[last + 2]] << 6;
|
|
100
|
+
str[j++] = n >> 8 & 0xFF;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
inline TSTRING decode(const TSTRING& str64) {
|
|
107
|
+
return decode(str64.c_str(), str64.length());
|
|
108
|
+
}
|
|
109
|
+
} // namespace base64
|
|
110
|
+
} // namespace painlessmesh
|
|
111
|
+
#endif
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
#ifndef _PAINLESS_MESH_BUFFER_HPP_
|
|
2
|
+
#define _PAINLESS_MESH_BUFFER_HPP_
|
|
3
|
+
|
|
4
|
+
#include <list>
|
|
5
|
+
|
|
6
|
+
#include "Arduino.h"
|
|
7
|
+
#include "painlessmesh/configuration.hpp"
|
|
8
|
+
|
|
9
|
+
#ifndef TCP_MSS
|
|
10
|
+
#define TCP_MSS 1024
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
namespace painlessmesh {
|
|
14
|
+
namespace buffer {
|
|
15
|
+
|
|
16
|
+
// Temporary buffer used by ReceiveBuffer and SentBuffer
|
|
17
|
+
struct temp_buffer_t {
|
|
18
|
+
size_t length = TCP_MSS;
|
|
19
|
+
char buffer[TCP_MSS];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* \brief ReceivedBuffer handles cstrings and stores them as strings
|
|
24
|
+
*/
|
|
25
|
+
template <class T>
|
|
26
|
+
class ReceiveBuffer {
|
|
27
|
+
public:
|
|
28
|
+
ReceiveBuffer() { buffer = T(); }
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Push a message into the buffer
|
|
32
|
+
*/
|
|
33
|
+
void push(const char *cstr, size_t length, temp_buffer_t &buf) {
|
|
34
|
+
auto data_ptr = cstr;
|
|
35
|
+
do {
|
|
36
|
+
auto len = strnlen(data_ptr, length);
|
|
37
|
+
do {
|
|
38
|
+
auto read_len = std::min(len, buf.length);
|
|
39
|
+
memcpy(buf.buffer, data_ptr, read_len);
|
|
40
|
+
buf.buffer[read_len] = '\0';
|
|
41
|
+
auto newBuffer = T(buf.buffer);
|
|
42
|
+
stringAppend(buffer, newBuffer);
|
|
43
|
+
len -= newBuffer.length();
|
|
44
|
+
length -= newBuffer.length();
|
|
45
|
+
data_ptr += newBuffer.length() * sizeof(char);
|
|
46
|
+
} while (len > 0);
|
|
47
|
+
if (length > 0) {
|
|
48
|
+
// Skip/remove the '\0' between the messages
|
|
49
|
+
length -= 1;
|
|
50
|
+
data_ptr += 1 * sizeof(char);
|
|
51
|
+
if (buffer.length() > 0) { // skip empty buffers
|
|
52
|
+
jsonStrings.push_back(buffer);
|
|
53
|
+
buffer = T();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} while (length > 0);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the oldest message from the buffer
|
|
61
|
+
*/
|
|
62
|
+
T front() {
|
|
63
|
+
if (!empty()) return jsonStrings.front();
|
|
64
|
+
return T();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Remove the oldest message from the buffer
|
|
69
|
+
*/
|
|
70
|
+
void pop_front() { jsonStrings.pop_front(); }
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Is the buffer empty
|
|
74
|
+
*/
|
|
75
|
+
bool empty() { return jsonStrings.empty(); }
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Clear the buffer
|
|
79
|
+
*/
|
|
80
|
+
void clear() {
|
|
81
|
+
jsonStrings.clear();
|
|
82
|
+
buffer = T();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private:
|
|
86
|
+
T buffer;
|
|
87
|
+
std::list<T> jsonStrings;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Helper function to deal with difference Arduino String
|
|
91
|
+
* and std::string
|
|
92
|
+
*/
|
|
93
|
+
inline void stringAppend(T &buffer, T &newBuffer) {
|
|
94
|
+
buffer.concat(newBuffer);
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
#ifdef PAINLESSMESH_ENABLE_STD_STRING
|
|
99
|
+
template <>
|
|
100
|
+
inline void ReceiveBuffer<std::string>::stringAppend(std::string &buffer,
|
|
101
|
+
std::string &newBuffer) {
|
|
102
|
+
buffer.append(newBuffer);
|
|
103
|
+
}
|
|
104
|
+
#endif
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* \brief SentBuffer stores messages (strings) and allows them to be read in any
|
|
108
|
+
* length
|
|
109
|
+
*/
|
|
110
|
+
template <class T>
|
|
111
|
+
class SentBuffer {
|
|
112
|
+
public:
|
|
113
|
+
SentBuffer() {};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* push a message into the buffer.
|
|
117
|
+
*
|
|
118
|
+
* \param priority Whether this is a high priority message.
|
|
119
|
+
*
|
|
120
|
+
* High priority messages will be sent to the front of the buffer
|
|
121
|
+
*/
|
|
122
|
+
void push(const T &message, bool priority = false) {
|
|
123
|
+
if (priority) {
|
|
124
|
+
if (clean)
|
|
125
|
+
jsonStrings.push_front(message);
|
|
126
|
+
else
|
|
127
|
+
jsonStrings.insert((++jsonStrings.begin()), message);
|
|
128
|
+
} else
|
|
129
|
+
jsonStrings.push_back(message);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Request whether the passed length is readable
|
|
134
|
+
*
|
|
135
|
+
* Returns the actual length available (<= the requested length
|
|
136
|
+
*/
|
|
137
|
+
size_t requestLength(size_t buffer_length) {
|
|
138
|
+
if (jsonStrings.empty())
|
|
139
|
+
return 0;
|
|
140
|
+
else
|
|
141
|
+
// String.toCharArray automatically turns the last character into
|
|
142
|
+
// a \0, we need the extra space to deal with that annoyance
|
|
143
|
+
return std::min(buffer_length - 1, jsonStrings.begin()->length() + 1);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Read the given length into the passed buffer (copy mode)
|
|
148
|
+
*
|
|
149
|
+
* Note the user should first make sure the requested length is available
|
|
150
|
+
* using `SentBuffer.requestLength()`, otherwise this function might fail.
|
|
151
|
+
* Note that if multiple messages are read then they are separated using '\0'.
|
|
152
|
+
*
|
|
153
|
+
* Performance note: Consider using readPtr() for zero-copy access when possible.
|
|
154
|
+
*/
|
|
155
|
+
void read(size_t length, temp_buffer_t &buf) {
|
|
156
|
+
// Note that toCharrArray always null terminates
|
|
157
|
+
// independent of whether the whole string was read so we use one extra
|
|
158
|
+
// space
|
|
159
|
+
jsonStrings.front().toCharArray(buf.buffer, length + 1);
|
|
160
|
+
last_read_size = length;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Returns a pointer directly to the oldest message
|
|
165
|
+
*
|
|
166
|
+
* Note the user should first make sure the requested length is available
|
|
167
|
+
* using `SentBuffer.requestLength()`, otherwise this function might fail.
|
|
168
|
+
* Note that if multiple messages are read then they are separated using '\0'.
|
|
169
|
+
*/
|
|
170
|
+
const char *readPtr(size_t length) {
|
|
171
|
+
last_read_size = length;
|
|
172
|
+
return jsonStrings.front().c_str();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Clear the previously read messages from the buffer.
|
|
177
|
+
*
|
|
178
|
+
* Should be called after a call of read() to clear the buffer.
|
|
179
|
+
*/
|
|
180
|
+
void freeRead() {
|
|
181
|
+
if (last_read_size == jsonStrings.begin()->length() + 1) {
|
|
182
|
+
jsonStrings.pop_front();
|
|
183
|
+
clean = true;
|
|
184
|
+
} else {
|
|
185
|
+
// jsonStrings.begin()->remove(0, last_read_size);
|
|
186
|
+
stringEraseFront((*jsonStrings.begin()), last_read_size);
|
|
187
|
+
clean = false;
|
|
188
|
+
}
|
|
189
|
+
last_read_size = 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
bool empty() { return jsonStrings.empty(); }
|
|
193
|
+
|
|
194
|
+
void clear() { jsonStrings.clear(); }
|
|
195
|
+
|
|
196
|
+
size_t size() { return jsonStrings.size(); }
|
|
197
|
+
|
|
198
|
+
private:
|
|
199
|
+
size_t last_read_size = 0;
|
|
200
|
+
bool clean = true;
|
|
201
|
+
std::list<T> jsonStrings;
|
|
202
|
+
|
|
203
|
+
inline void stringEraseFront(T &string, size_t length) {
|
|
204
|
+
string.remove(0, length);
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
#ifdef PAINLESSMESH_ENABLE_STD_STRING
|
|
209
|
+
template <>
|
|
210
|
+
inline void SentBuffer<std::string>::read(size_t length, temp_buffer_t &buf) {
|
|
211
|
+
jsonStrings.front().copy(buf.buffer, length);
|
|
212
|
+
// Mimic String.toCharArray behaviour, which will insert
|
|
213
|
+
// null termination at the end of original string and the last
|
|
214
|
+
// character
|
|
215
|
+
if (length == jsonStrings.front().length() + 1) buf.buffer[length - 1] = '\0';
|
|
216
|
+
buf.buffer[length] = '\0';
|
|
217
|
+
last_read_size = length;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
template <>
|
|
221
|
+
inline void SentBuffer<std::string>::stringEraseFront(std::string &string,
|
|
222
|
+
size_t length) {
|
|
223
|
+
string.erase(0, length);
|
|
224
|
+
};
|
|
225
|
+
#endif
|
|
226
|
+
|
|
227
|
+
} // namespace buffer
|
|
228
|
+
} // namespace painlessmesh
|
|
229
|
+
#endif
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
#ifndef _PAINLESS_MESH_CALLBACK_HPP_
|
|
2
|
+
#define _PAINLESS_MESH_CALLBACK_HPP_
|
|
3
|
+
|
|
4
|
+
#include <map>
|
|
5
|
+
#include <memory>
|
|
6
|
+
|
|
7
|
+
#include "painlessmesh/configuration.hpp"
|
|
8
|
+
|
|
9
|
+
#include "painlessmesh/protocol.hpp"
|
|
10
|
+
|
|
11
|
+
namespace painlessmesh {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Helper functions to work with multiple callbacks
|
|
15
|
+
*/
|
|
16
|
+
namespace callback {
|
|
17
|
+
template <typename... Args>
|
|
18
|
+
class List {
|
|
19
|
+
public:
|
|
20
|
+
int execute(Args... args) {
|
|
21
|
+
for (auto&& f : callbacks) {
|
|
22
|
+
f(args...);
|
|
23
|
+
}
|
|
24
|
+
return callbacks.size();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
size_t size() { return callbacks.size(); }
|
|
28
|
+
|
|
29
|
+
void clear() { return callbacks.clear(); }
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
* Needs to be wrapped into semaphore
|
|
33
|
+
*
|
|
34
|
+
int executeWithScheduler(Scheduler& scheduler, Args... args) {
|
|
35
|
+
scheduler.execute();
|
|
36
|
+
for (auto&& f : callbacks) {
|
|
37
|
+
f(args...);
|
|
38
|
+
scheduler.execute();
|
|
39
|
+
}
|
|
40
|
+
return callbacks.size();
|
|
41
|
+
}*/
|
|
42
|
+
|
|
43
|
+
/** Add callbacks to the end of the list.
|
|
44
|
+
*/
|
|
45
|
+
void push_back(std::function<void(Args...)> func) {
|
|
46
|
+
callbacks.push_back(func);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected:
|
|
50
|
+
std::list<std::function<void(Args...)>> callbacks;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Manage callbacks for receiving packages
|
|
55
|
+
*/
|
|
56
|
+
template <typename... Args>
|
|
57
|
+
class PackageCallbackList {
|
|
58
|
+
public:
|
|
59
|
+
/**
|
|
60
|
+
* Add a callback for specific package id
|
|
61
|
+
*/
|
|
62
|
+
void onPackage(int id, std::function<void(Args...)> func) {
|
|
63
|
+
callbackMap[id].push_back(func);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
size_t size() {
|
|
67
|
+
size_t size = 0;
|
|
68
|
+
for (auto&& key_value : callbackMap) {
|
|
69
|
+
size += key_value.second.size();
|
|
70
|
+
}
|
|
71
|
+
return size;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
void clear() { callbackMap.clear(); }
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Execute all the callbacks associated with a certain package
|
|
78
|
+
*/
|
|
79
|
+
int execute(int id, Args... args) { return callbackMap[id].execute(args...); }
|
|
80
|
+
|
|
81
|
+
protected:
|
|
82
|
+
std::map<int, List<Args...>> callbackMap;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
template <typename T>
|
|
86
|
+
using MeshPackageCallbackList =
|
|
87
|
+
PackageCallbackList<protocol::Variant&, std::shared_ptr<T>, uint32_t>;
|
|
88
|
+
} // namespace callback
|
|
89
|
+
} // namespace painlessmesh
|
|
90
|
+
|
|
91
|
+
#endif
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#ifndef _PAINLESS_MESH_CONFIGURATION_HPP_
|
|
2
|
+
#define _PAINLESS_MESH_CONFIGURATION_HPP_
|
|
3
|
+
|
|
4
|
+
// Include Arduino.h in test environment to ensure TSTRING is defined
|
|
5
|
+
#if defined(PAINLESSMESH_BOOST) || defined(ARDUINO_ARCH_ESP8266) || \
|
|
6
|
+
defined(ARDUINO_ARCH_ESP32)
|
|
7
|
+
#ifndef ARDUINO
|
|
8
|
+
#include "Arduino.h"
|
|
9
|
+
#endif
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
#include <list>
|
|
13
|
+
|
|
14
|
+
#include "painlessTaskOptions.h"
|
|
15
|
+
|
|
16
|
+
#include <TaskSchedulerDeclarations.h>
|
|
17
|
+
|
|
18
|
+
#define ARDUINOJSON_USE_LONG_LONG 1
|
|
19
|
+
#undef ARDUINOJSON_ENABLE_STD_STRING
|
|
20
|
+
#include <ArduinoJson.h>
|
|
21
|
+
#undef ARDUINOJSON_ENABLE_STD_STRING
|
|
22
|
+
|
|
23
|
+
// Enable (arduino) wifi support
|
|
24
|
+
#define PAINLESSMESH_ENABLE_ARDUINO_WIFI
|
|
25
|
+
|
|
26
|
+
// Enable OTA support
|
|
27
|
+
#define PAINLESSMESH_ENABLE_OTA
|
|
28
|
+
|
|
29
|
+
// Minimum free memory, besides here all packets in queue are discarded.
|
|
30
|
+
#define MIN_FREE_MEMORY 4000
|
|
31
|
+
// MAX number of unsent messages in queue. Newer messages are discarded
|
|
32
|
+
#define MAX_MESSAGE_QUEUE 50
|
|
33
|
+
|
|
34
|
+
#define NODE_TIMEOUT 10 * TASK_SECOND
|
|
35
|
+
#define SCAN_INTERVAL 30 * TASK_SECOND // AP scan period in ms
|
|
36
|
+
|
|
37
|
+
#ifdef ESP32
|
|
38
|
+
#include <AsyncTCP.h>
|
|
39
|
+
#include <WiFi.h>
|
|
40
|
+
#if ESP_ARDUINO_VERSION_MAJOR >= 3
|
|
41
|
+
#include "esp_mac.h" // required for core 3.x - exposes esp_mac_type_t values
|
|
42
|
+
#endif
|
|
43
|
+
#elif defined(ESP8266)
|
|
44
|
+
#include <ESP8266WiFi.h>
|
|
45
|
+
#include <ESPAsyncTCP.h>
|
|
46
|
+
#endif // ESP32
|
|
47
|
+
|
|
48
|
+
// Define TSTRING - use std::string in test environment, Arduino String
|
|
49
|
+
// otherwise
|
|
50
|
+
#if defined(PAINLESSMESH_BOOST)
|
|
51
|
+
// Test environment - TSTRING already defined in test Arduino.h as std::string
|
|
52
|
+
#else
|
|
53
|
+
typedef String TSTRING;
|
|
54
|
+
#endif
|
|
55
|
+
|
|
56
|
+
// backward compatibility
|
|
57
|
+
template <typename T>
|
|
58
|
+
using SimpleList = std::list<T>;
|
|
59
|
+
|
|
60
|
+
namespace painlessmesh {
|
|
61
|
+
namespace wifi {
|
|
62
|
+
class Mesh;
|
|
63
|
+
};
|
|
64
|
+
}; // namespace painlessmesh
|
|
65
|
+
|
|
66
|
+
/** A convenience typedef to access the mesh class*/
|
|
67
|
+
#ifdef PAINLESSMESH_ENABLE_ARDUINO_WIFI
|
|
68
|
+
using painlessMesh = painlessmesh::wifi::Mesh;
|
|
69
|
+
#endif
|
|
70
|
+
|
|
71
|
+
#ifdef ESP32
|
|
72
|
+
#define MAX_CONN 10
|
|
73
|
+
#else
|
|
74
|
+
#define MAX_CONN 4
|
|
75
|
+
#endif // DEBUG
|
|
76
|
+
|
|
77
|
+
#endif
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#ifndef _PAINLESS_MESH_CONNECTION_HPP_
|
|
2
|
+
#define _PAINLESS_MESH_CONNECTION_HPP_
|
|
3
|
+
|
|
4
|
+
#include <memory>
|
|
5
|
+
|
|
6
|
+
#include "Arduino.h"
|
|
7
|
+
#include "painlessmesh/configuration.hpp"
|
|
8
|
+
|
|
9
|
+
#include "painlessmesh/buffer.hpp"
|
|
10
|
+
#include "painlessmesh/logger.hpp"
|
|
11
|
+
|
|
12
|
+
extern painlessmesh::logger::LogClass Log;
|
|
13
|
+
|
|
14
|
+
namespace painlessmesh {
|
|
15
|
+
namespace tcp {
|
|
16
|
+
|
|
17
|
+
// Shared buffer for reading/writing to the buffer
|
|
18
|
+
static painlessmesh::buffer::temp_buffer_t shared_buffer;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Class that performs buffered read and write to the tcp connection
|
|
22
|
+
* (asyncclient)
|
|
23
|
+
*
|
|
24
|
+
* Note that the class expects to be wrapped in a shared_ptr to ensure proper
|
|
25
|
+
* lifetime management. Objects generally will only be destroyed after close is
|
|
26
|
+
* called (and the reading and writing tasks are stopped).
|
|
27
|
+
*/
|
|
28
|
+
class BufferedConnection
|
|
29
|
+
: public std::enable_shared_from_this<BufferedConnection> {
|
|
30
|
+
public:
|
|
31
|
+
/**
|
|
32
|
+
* Create a buffered connection around the client
|
|
33
|
+
*
|
|
34
|
+
*
|
|
35
|
+
* BufferedConnection takes ownership of the client and will delete the
|
|
36
|
+
* pointer at the end.
|
|
37
|
+
*
|
|
38
|
+
* One should always call initialize after construction
|
|
39
|
+
*/
|
|
40
|
+
BufferedConnection(AsyncClient *client) : client(client) {}
|
|
41
|
+
|
|
42
|
+
~BufferedConnection() {
|
|
43
|
+
Log.remote("~BufferedConnection");
|
|
44
|
+
this->close();
|
|
45
|
+
if (!client->freeable()) {
|
|
46
|
+
client->close(true);
|
|
47
|
+
}
|
|
48
|
+
client->abort();
|
|
49
|
+
delete client;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
void initialize(Scheduler *scheduler) {
|
|
53
|
+
auto self = this->shared_from_this();
|
|
54
|
+
sentBufferTask.set(TASK_SECOND, TASK_FOREVER, [self]() {
|
|
55
|
+
if (!self->sentBuffer.empty() && self->client->canSend()) {
|
|
56
|
+
auto ret = self->writeNext();
|
|
57
|
+
if (ret)
|
|
58
|
+
self->sentBufferTask.forceNextIteration();
|
|
59
|
+
else
|
|
60
|
+
self->sentBufferTask.delay(100 * TASK_MILLISECOND);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
scheduler->addTask(sentBufferTask);
|
|
64
|
+
sentBufferTask.enableDelayed();
|
|
65
|
+
|
|
66
|
+
readBufferTask.set(TASK_SECOND, TASK_FOREVER, [self]() {
|
|
67
|
+
if (!self->receiveBuffer.empty()) {
|
|
68
|
+
TSTRING frnt = self->receiveBuffer.front();
|
|
69
|
+
self->receiveBuffer.pop_front();
|
|
70
|
+
if (!self->receiveBuffer.empty())
|
|
71
|
+
self->readBufferTask.forceNextIteration();
|
|
72
|
+
if (self->receiveCallback) self->receiveCallback(frnt);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
scheduler->addTask(readBufferTask);
|
|
76
|
+
readBufferTask.enableDelayed();
|
|
77
|
+
|
|
78
|
+
client->onAck(
|
|
79
|
+
[self](void *arg, AsyncClient *client, size_t len, uint32_t time) {
|
|
80
|
+
self->sentBufferTask.forceNextIteration();
|
|
81
|
+
},
|
|
82
|
+
NULL);
|
|
83
|
+
|
|
84
|
+
client->onData(
|
|
85
|
+
[self](void *arg, AsyncClient *client, void *data, size_t len) {
|
|
86
|
+
self->receiveBuffer.push(static_cast<const char *>(data), len,
|
|
87
|
+
shared_buffer);
|
|
88
|
+
// Signal that we are done
|
|
89
|
+
self->client->ack(len);
|
|
90
|
+
self->readBufferTask.forceNextIteration();
|
|
91
|
+
},
|
|
92
|
+
NULL);
|
|
93
|
+
|
|
94
|
+
client->onDisconnect(
|
|
95
|
+
[self](void *arg, AsyncClient *client) { self->close(); }, NULL);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
void close() {
|
|
99
|
+
if (!mConnected) return;
|
|
100
|
+
|
|
101
|
+
// Disable tasks and callbacks
|
|
102
|
+
this->sentBufferTask.setCallback(NULL);
|
|
103
|
+
this->sentBufferTask.disable();
|
|
104
|
+
this->readBufferTask.setCallback(NULL);
|
|
105
|
+
this->readBufferTask.disable();
|
|
106
|
+
|
|
107
|
+
this->client->onData(NULL, NULL);
|
|
108
|
+
this->client->onAck(NULL, NULL);
|
|
109
|
+
this->client->onDisconnect(NULL, NULL);
|
|
110
|
+
this->client->onError(NULL, NULL);
|
|
111
|
+
|
|
112
|
+
if (client->connected()) {
|
|
113
|
+
client->close();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
receiveBuffer.clear();
|
|
117
|
+
sentBuffer.clear();
|
|
118
|
+
|
|
119
|
+
if (disconnectCallback) disconnectCallback();
|
|
120
|
+
|
|
121
|
+
receiveCallback = NULL;
|
|
122
|
+
disconnectCallback = NULL;
|
|
123
|
+
|
|
124
|
+
mConnected = false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
bool write(const TSTRING &data, bool priority = false) {
|
|
128
|
+
sentBuffer.push(data, priority);
|
|
129
|
+
sentBufferTask.forceNextIteration();
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
void onDisconnect(std::function<void()> callback) {
|
|
134
|
+
disconnectCallback = callback;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
void onReceive(std::function<void(TSTRING)> callback) {
|
|
138
|
+
receiveCallback = callback;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
bool connected() { return mConnected; }
|
|
142
|
+
|
|
143
|
+
protected:
|
|
144
|
+
bool mConnected = true;
|
|
145
|
+
|
|
146
|
+
AsyncClient *client;
|
|
147
|
+
|
|
148
|
+
std::function<void(TSTRING)> receiveCallback;
|
|
149
|
+
std::function<void()> disconnectCallback;
|
|
150
|
+
|
|
151
|
+
painlessmesh::buffer::ReceiveBuffer<TSTRING> receiveBuffer;
|
|
152
|
+
painlessmesh::buffer::SentBuffer<TSTRING> sentBuffer;
|
|
153
|
+
|
|
154
|
+
bool writeNext() {
|
|
155
|
+
if (sentBuffer.empty()) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
auto len = sentBuffer.requestLength(shared_buffer.length);
|
|
159
|
+
auto snd_len = client->space();
|
|
160
|
+
if (len > snd_len) len = snd_len;
|
|
161
|
+
if (len > 0) {
|
|
162
|
+
// sentBuffer.read(len, shared_buffer);
|
|
163
|
+
// auto written = client->write(shared_buffer.buffer, len, 1);
|
|
164
|
+
auto data_ptr = sentBuffer.readPtr(len);
|
|
165
|
+
auto written = client->write(data_ptr, len, 1);
|
|
166
|
+
if (written == len) {
|
|
167
|
+
client->send(); // TODO only do this for priority messages
|
|
168
|
+
sentBuffer.freeRead();
|
|
169
|
+
sentBufferTask.forceNextIteration();
|
|
170
|
+
return true;
|
|
171
|
+
} else if (written == 0) {
|
|
172
|
+
return false;
|
|
173
|
+
} else {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
} else {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
Task sentBufferTask;
|
|
182
|
+
Task readBufferTask;
|
|
183
|
+
|
|
184
|
+
template <typename T>
|
|
185
|
+
std::shared_ptr<T> shared_from(T *derived) {
|
|
186
|
+
return std::static_pointer_cast<T>(shared_from_this());
|
|
187
|
+
}
|
|
188
|
+
}; // namespace tcp
|
|
189
|
+
}; // namespace tcp
|
|
190
|
+
}; // namespace painlessmesh
|
|
191
|
+
|
|
192
|
+
#endif
|