-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway-list.cpp
350 lines (327 loc) · 8.54 KB
/
gateway-list.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
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
#include <regex>
#include <sstream>
#include "gateway-list.h"
#include "errlist.h"
#include "utillora.h"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexpansion-to-defined"
#endif
#include "rapidjson/writer.h"
#include "rapidjson/prettywriter.h"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "utilstring.h"
#include "platform.h"
/**
* Create empty gateway list
*/
GatewayList::GatewayList()
: errmessage(""), filename("")
{
}
/**
* Copy gateways
*/
GatewayList::GatewayList(
const GatewayList &value
)
{
errmessage = value.errmessage;
filename = value.filename;
gateways = value.gateways;
}
/**
* Loads gateways from JSON file
*/
GatewayList::GatewayList(
const std::string &aFileName
)
: errmessage(""), filename(aFileName)
{
parse(file2string(filename.c_str()));
}
/**
* Serialize gateway list to JSON
*/
void GatewayList::toJSON(
rapidjson::Value &value,
rapidjson::Document::AllocatorType& allocator
) const
{
for (std::map<uint64_t, GatewayStat>::const_iterator it(gateways.begin()); it != gateways.end(); it++) {
rapidjson::Value v;
v.SetObject();
it->second.toJSON(v, allocator);
value.PushBack(v, allocator);
}
}
/**
* Add gateway to the list frm JSON string
*/
int GatewayList::put(
uint64_t gatewayId,
const std::string &value
)
{
rapidjson::Document doc;
rapidjson::Document::AllocatorType &allocator(doc.GetAllocator());
doc.Parse<rapidjson::kParseCommentsFlag>(value.c_str());
return parse(gatewayId, doc);
}
/**
* Return 'config' gateway address
* It is different than 'current' address extracted from last successful PULL request
* @param gatewayId gateway identifier
*/
std::string GatewayList::getAddress(
uint64_t gatewayId
) {
std::map<uint64_t, GatewayStat>::iterator it(gateways.find(gatewayId));
if (it != gateways.end())
return it->second.addr;
else
return "";
}
/**
* Update list from statistic packet
*/
bool GatewayList::update(
const GatewayStat &value
)
{
std::map<uint64_t, GatewayStat>::iterator it(gateways.find(value.gatewayId));
// save anyway
gateways[value.gatewayId] = value;
// indicate it is new one
return it != gateways.end();
}
/**
* Parse JSON
*/
int GatewayList::parse(
uint64_t gatewayId,
rapidjson::Value &value
)
{
if (!value.IsArray())
return ERR_CODE_INVALID_JSON;
for (rapidjson::SizeType i = 0; i < value.Size(); i++) {
GatewayStat stat;
if (int r = stat.parse(value[i])) {
errmessage = strerror_lorawan_ns(r);
} else {
if (gatewayId)
stat.gatewayId = gatewayId;
gateways[stat.gatewayId] = stat;
}
}
return 0;
}
/**
* Return gateway list subset filters by gateway identifiers
* @param list gateway identifiers. You can use wildcards '*', '?'.
* @param useRegex true- list contains wildcards. Symbols '*', '?' are replaced with '.*' and '.'
* regular expression symbol respectively.
* @return 0- success, < 0- error code
*/
int GatewayList::parseIdentifiers(
std::vector<uint64_t> &retval,
const std::vector<std::string> &list,
bool useRegex
) const
{
for (std::vector<std::string>::const_iterator it(list.begin()); it != list.end(); it++) {
if (isHex(*it)) {
// identifier itself
uint64_t v = str2gatewayId(it->c_str());
if (gateways.find(v) == gateways.end()) {
return false;
} else {
retval.push_back(v);
}
} else {
// can contain regex "*"
try {
std::string re;
if (useRegex)
re = *it;
else
re = replaceAll(replaceAll(*it, "*", ".*"), "?", ".");
std::regex rex(re, std::regex_constants::grep);
for (std::map<uint64_t, GatewayStat>::const_iterator itg(gateways.begin()); itg != gateways.end(); itg++) {
std::string s2 = uint64_t2string(itg->first);
if (std::regex_search(s2, rex))
retval.push_back(itg->first);
}
}
catch (const std::regex_error&) {
return ERR_CODE_INVALID_REGEX;
}
}
}
return 0;
}
/**
* Return gateway list subset filters by gateway name
* @param list gateway names (case sensitive). You can use wildcards '*', '?'.
* @param useRegex true- list contains wildcards. Symbols '*', '?' are replaced with '.*' and '.'
* regular expression symbol respectively.
* @return 0- success, < 0- error code
*/
int GatewayList::parseNames(
std::vector<uint64_t> &retval,
const std::vector<std::string> &list,
bool useRegex
) const
{
for (std::vector<std::string>::const_iterator it(list.begin()); it != list.end(); it++) {
if (isHex(*it)) {
// identifier itself
uint64_t v = str2gatewayId(it->c_str());
if (gateways.find(v) == gateways.end()) {
return false;
} else {
retval.push_back(v);
}
} else {
// can contain regex "*"
try {
std::string re;
if (useRegex)
re = *it;
else
re = replaceAll(replaceAll(*it, "*", ".*"), "?", ".");
std::regex rex(re, std::regex_constants::grep);
for (std::map<uint64_t, GatewayStat>::const_iterator itg(gateways.begin()); itg != gateways.end(); itg++) {
if (std::regex_search(itg->second.name, rex))
retval.push_back(itg->first);
}
}
catch (const std::regex_error&) {
return ERR_CODE_INVALID_REGEX;
}
}
}
return 0;
}
/**
* @return JSON string
*/
std::string GatewayList::toJsonString() const
{
rapidjson::Document doc;
rapidjson::Document::AllocatorType &allocator = doc.GetAllocator();
doc.SetArray();
toJSON(doc, allocator);
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
return std::string(buffer.GetString());
}
/**
* Parse JSON string
*/
void GatewayList::parse(
const std::string &value
)
{
rapidjson::Document doc;
doc.Parse<rapidjson::kParseCommentsFlag>(value.c_str());
parse(0, doc);
}
/**
* Save to JSON file
*/
void GatewayList::save()
{
string2file(filename, toJsonString());
}
/**
* Check does gateway exists
* @param gwId gateway identifier
* @return true is gateway exists
*/
bool GatewayList::has(
const DEVEUI &gwId
) const
{
uint64_t k = *(uint64_t *) &gwId;
return (gateways.find(k) != gateways.end());
}
/**
* Check does gateway exists
* @param gwId gateway identifier
* @return true is gateway exists
*/
bool GatewayList::has(
const uint64_t gwId
) const
{
return (gateways.find(gwId) != gateways.end());
}
/**
* Set gateway socket address
* Gateway send PULL request from random port number each minute.
* This port number is opened for communication until next PULL request.
* Network server must remember this port and then send packet to this port number.
* Please note gateway address can be different from assigned address in configuration file because of NAT.
* @param gwId gateway identifier
* @param gwAddress gateway socket address to be set(update)
* @return true if gateway with gwId identifier found and successfully updated
*/
bool GatewayList::setSocketAddress
(
const DEVEUI &gwId,
int socket,
const struct sockaddr_in *gwAddress
)
{
uint64_t k = *(uint64_t *) &gwId;
std::map<uint64_t, GatewayStat>::iterator it(gateways.find(k));
if (it == gateways.end())
return false;
memmove(&it->second.sockaddr, gwAddress,
(gwAddress->sin_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)));
it->second.socket = socket;
return true;
}
/**
* Set gateway socket address into parameter value from gwAddress parameter and copy name and geolocation
* from config file
* Gateway send PULL request from random port number each minute.
* This port number is opened for communication until next PULL request.
* Network server must remember this port and then send packet to this port number.
* Please note gateway address can be different from assigned address in configuration file because of NAT.
* @param gwAddress gateway socket address to be set(update)
* @param value Set gateway stat socket address, name and geolocation (if gateway found).
* @return true if found
*/
bool GatewayList::copyId
(
GatewayStat &value,
const struct sockaddr *gwAddress
) const
{
uint64_t k = *(uint64_t *) &value.gatewayId;
std::map<uint64_t, GatewayStat>::const_iterator it(gateways.find(k));
if (it == gateways.end())
return false;
value.name = it->second.name;
value.lat = it->second.lat;
value.lon = it->second.lon;
value.addr = UDPSocket::addrString(gwAddress);
return true;
}
std::string GatewayList::toDescriptionTableString() const {
std::stringstream ss;
for (std::map<uint64_t, GatewayStat>::const_iterator it(gateways.begin()); it != gateways.end(); it++) {
ss << "\t" << it->second.name
<< "\t" << gatewayId2str(it->second.gatewayId)
<< "\t" << it->second.addr
<< std::endl;
}
return ss.str();
}