forked from mrWheel/DSMRloggerAPI
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelperStuff.ino
207 lines (181 loc) · 7.72 KB
/
helperStuff.ino
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
/*
***************************************************************************
** Program : helperStuff, part of DSMRlogger-Next
** Version : v2.3.0-rc5
**
** Copyright (c) 2020 Willem Aandewiel
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
//===========================================================================================
boolean isValidIP(IPAddress ip)
{
/* Works as follows:
* example:
* 127.0.0.1
* 1 => 127||0||0||1 = 128>0 = true
* 2 => !(false || false) = true
* 3 => !(false || false || false || false ) = true
* 4 => !(true && true && true && true) = false
* 5 => !(false) = true
* true && true & true && false && true = false ==> correct, this is an invalid addres
*
* 0.0.0.0
* 1 => 0||0||0||0 = 0>0 = false
* 2 => !(true || true) = false
* 3 => !(false || false || false || false) = true
* 4 => !(true && true && true && tfalse) = true
* 5 => !(false) = true
* false && false && true && true && true = false ==> correct, this is an invalid addres
*
* 192.168.0.1
* 1 => 192||168||0||1 =233>0 = true
* 2 => !(false || false) = true
* 3 +> !(false || false || false || false) = true
* 4 => !(false && false && true && true) = true
* 5 => !(false) = true
* true & true & true && true && true = true ==> correct, this is a valid address
*
* 255.255.255.255
* 1 => 255||255||255||255 =255>0 = true
* 2 => !(false || false ) = true
* 3 +> !(true || true || true || true) = false
* 4 => !(false && false && false && false) = true
* 5 => !(true) = false
* true && true && false && true && false = false ==> correct, this is an invalid address
*
* 0.123.12.1 => true && false && true && true && true = false ==> correct, this is an invalid address
* 10.0.0.0 => true && false && true && true && true = false ==> correct, this is an invalid address
* 10.255.0.1 => true && true && false && true && true = false ==> correct, this is an invalid address
* 150.150.255.150 => true && true && false && true && true = false ==> correct, this is an invalid address
*
* 123.21.1.99 => true && true && true && true && true = true ==> correct, this is annvalid address
* 1.1.1.1 => true && true && true && true && true = true ==> correct, this is annvalid address
*
* Some references on valid ip addresses:
* - https://www.quora.com/How-do-you-identify-an-invalid-IP-address
*
*/
boolean _isValidIP = false;
_isValidIP = ((ip[0] || ip[1] || ip[2] || ip[3])>0); // if any bits are set, then it is not 0.0.0.0
_isValidIP &= !((ip[0]==0) || (ip[3]==0)); // if either the first or last is a 0, then it is invalid
_isValidIP &= !((ip[0]==255) || (ip[1]==255) || (ip[2]==255) || (ip[3]==255)) ; // if any of the octets is 255, then it is invalid
_isValidIP &= !(ip[0]==127 && ip[1]==0 && ip[2]==0 && ip[3]==1); // if not 127.0.0.0 then it might be valid
_isValidIP &= !(ip[0]>=224); // if ip[0] >=224 then reserved space
DebugTf( "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
if (_isValidIP)
Debugln(F(" = Valid IP"));
else
Debugln(F(" = Invalid IP!"));
return _isValidIP;
} // isValidIP()
//===========================================================================================
bool isNumericp(const char *timeStamp, int8_t len)
{
for (int i=0; (i<len && i<12);i++)
{
if (timeStamp[i] < '0' || timeStamp[i] > '9')
{
return false;
}
}
return true;
} // isNumericp()
//===========================================================================================
int8_t splitString(String inStrng, char delimiter, String wOut[], uint8_t maxWords)
{
int16_t inxS = 0, inxE = 0, wordCount = 0;
inStrng.trim();
while(inxE < inStrng.length() && wordCount < maxWords)
{
inxE = inStrng.indexOf(delimiter, inxS); //finds location of first ,
wOut[wordCount] = inStrng.substring(inxS, inxE); //captures first data String
wOut[wordCount].trim();
//DebugTf("[%d] => [%c] @[%d] found[%s]\r\n", wordCount, delimiter, inxE, wOut[wordCount].c_str());
inxS = inxE;
inxS++;
wordCount++;
}
// zero rest of the words
for(int i=wordCount; i< maxWords; i++)
{
wOut[wordCount][0] = 0;
}
// if not whole string processed place rest in last word
if (inxS < inStrng.length())
{
wOut[maxWords-1] = inStrng.substring(inxS, inStrng.length()); // store rest of String
}
return wordCount;
} // splitString()
//===========================================================================================
String upTime()
{
char calcUptime[20];
snprintf(calcUptime, sizeof(calcUptime), "%d(d)-%02d:%02d(H:m)"
, int((upTimeSeconds / (60 * 60 * 24)) % 365)
, int((upTimeSeconds / (60 * 60)) % 24)
, int((upTimeSeconds / (60)) % 60));
return calcUptime;
} // upTime()
//===========================================================================================
//taken from influxdbclient.cpp == escapeJSONString
static String escapeJSONString(String &value) {
String ret;
int d = 0;
int i,from = 0;
while((i = value.indexOf('"',from)) > -1) {
d++;
if(i == value.length()-1) {
break;
}
from = i+1;
}
ret.reserve(value.length()+d); //most probably we will escape just double quotes
for (char c: value)
{
switch (c)
{
case '"': ret += "\\\""; break;
case '\\': ret += "\\\\"; break;
case '\b': ret += "\\b"; break;
case '\f': ret += "\\f"; break;
case '\n': ret += "\\n"; break;
case '\r': ret += "\\r"; break;
case '\t': ret += "\\t"; break;
default:
if ('\x00' <= c && c <= '\x1f') {
ret += "\\u";
char buf[3 + 8 * sizeof(unsigned int)];
sprintf(buf, "\\u%04u", c);
ret += buf;
} else {
ret += c;
}
}
}
return ret;
}
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/