-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.cpp
311 lines (248 loc) · 5.26 KB
/
utils.cpp
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
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
#include <Arduino.h>
#include <LittleFS.h>
#include "rp2040.h"
#include <sys/socket.h>
#elif defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#endif
#include <ArduinoJson.h>
#include <errno.h>
#include <optional>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <string>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <sys/time.h>
#if defined(_WIN32)
#include "win32.h"
#endif
void setBit(uint16_t & v, const int bit, const bool vb)
{
const uint16_t mask = 1 << bit;
v &= ~mask;
if (vb)
v |= mask;
}
std::string format(const char *const fmt, ...)
{
#if defined(BUILD_FOR_RP2040) || defined(ESP32)
char buffer[256];
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof buffer, fmt, ap);
va_end(ap);
return buffer;
#else
char *buffer = nullptr;
va_list ap;
va_start(ap, fmt);
(void)vasprintf(&buffer, fmt, ap);
va_end(ap);
std::string result = buffer;
free(buffer);
return result;
#endif
}
unsigned long get_ms()
{
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
return millis();
#else
timeval tv;
// TODO replace gettimeofday by clock_gettime
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}
uint64_t get_us()
{
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
return micros();
#else
timeval tv;
// TODO replace gettimeofday by clock_gettime
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000l + tv.tv_usec;
#endif
}
int parity(int v)
{
return __builtin_parity(v); // TODO
}
void myusleep(uint64_t us)
{
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
for(;;) {
uint64_t n_ms = us / 1000;
if (n_ms >= portTICK_PERIOD_MS) {
vTaskDelay(n_ms / portTICK_PERIOD_MS);
us -= n_ms * 1000;
}
else {
delayMicroseconds(us);
break;
}
}
#else
timespec req;
req.tv_sec = us / 1000000l;
req.tv_nsec = (us % 1000000l) * 1000l;
for(;;) {
timespec rem { 0, 0 };
int rc = nanosleep(&req, &rem);
if (rc == 0 || (rc == -1 && errno != EINTR))
break;
memcpy(&req, &rem, sizeof(timespec));
}
#endif
}
std::vector<std::string> split(std::string in, std::string splitter)
{
std::vector<std::string> out;
size_t splitter_size = splitter.size();
for(;;)
{
size_t pos = in.find(splitter);
if (pos == std::string::npos)
break;
std::string before = in.substr(0, pos);
if (!before.empty())
out.push_back(before);
size_t bytes_left = in.size() - (pos + splitter_size);
if (bytes_left == 0)
return out;
in = in.substr(pos + splitter_size);
}
if (in.size() > 0)
out.push_back(in);
return out;
}
void set_thread_name(std::string name)
{
#if !defined(ESP32) && !defined(BUILD_FOR_RP2040)
if (name.length() > 15)
name = name.substr(0, 15);
pthread_setname_np(pthread_self(), name.c_str());
#endif
}
std::string get_thread_name()
{
#if IS_POSIX || defined(_WIN32)
char buffer[16 + 1] { };
pthread_getname_np(pthread_self(), buffer, sizeof buffer);
return buffer;
#else
return pcTaskGetName(xTaskGetCurrentTaskHandle());
#endif
}
ssize_t WRITE(int fd, const char *whereto, size_t len)
{
ssize_t cnt=0;
while(len > 0)
{
ssize_t rc = write(fd, whereto, len);
if (rc == -1)
return -1;
else if (rc == 0)
return -1;
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
ssize_t READ(int fd, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len > 0)
{
ssize_t rc = read(fd, whereto, len);
if (rc == -1)
return -1;
else if (rc == 0)
break;
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
void update_word(uint16_t *const w, const bool msb, const uint8_t v)
{
if (msb) {
(*w) &= 0x00ff;
(*w) |= v << 8;
}
else {
(*w) &= 0xff00;
(*w) |= v;
}
}
bool set_nodelay(const int fd)
{
int flags = 1;
#if defined(__FreeBSD__) || defined(ESP32) || defined(_WIN32)
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&flags), sizeof(flags)) == -1)
return false;
#else
if (setsockopt(fd, SOL_TCP, TCP_NODELAY, reinterpret_cast<void *>(&flags), sizeof(flags)) == -1)
return false;
#endif
return true;
}
std::string get_endpoint_name(const int fd)
{
sockaddr_in addr { };
socklen_t addr_len = sizeof addr;
if (getpeername(fd, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1)
return format("FAILED TO FIND NAME OF %d: %s", fd, strerror(errno));
return std::string(inet_ntoa(addr.sin_addr)) + ":" + format("%d", ntohs(addr.sin_port));
}
std::optional<JsonDocument> deserialize_file(const std::string & filename)
{
JsonDocument j;
#if defined(ESP32)
File data_file = LittleFS.open(filename.c_str(), "r");
if (!data_file)
return { };
deserializeJson(j, data_file);
data_file.close();
#else
FILE *fh = fopen(filename.c_str(), "r");
if (!fh)
return { };
std::string j_in;
char buffer[4096];
for(;;) {
char *rc = fgets(buffer, sizeof buffer, fh);
if (!rc)
break;
j_in += buffer;
}
fclose(fh);
DeserializationError error = deserializeJson(j, j_in);
if (error)
return { };
#endif
return j;
}