-
Notifications
You must be signed in to change notification settings - Fork 5
/
misc.h
225 lines (186 loc) · 5.3 KB
/
misc.h
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
#ifndef MISC_H
#define MISC_H
#include "Ext/typedefs.h"
#include "macros.h"
#include "file_system.h"
#include "ttime.h"
#include <ArduinoJson.h>
#include <ctime>
#define to_string_c(val) #val
namespace gpos
{
namespace misc
{
ArduinoJson::StaticJsonBuffer<KB> json_buffer_KB;
ArduinoJson::StaticJsonBuffer<_100KB> json_buffer_50KB;
ArduinoJson::StaticJsonBuffer<_100KB> json_out_buffer_50KB;
void set_bufs_to_null(uchar buf1[], uchar buf2[])
{
estr::set_null(buf1, sizeof(buf1));
estr::set_null(buf2, sizeof(buf2));
}
typedef struct _JSON_BODY
{
uchar body_arr[_50KB];
uint32 sz;
} json_body_t;
json_body_t json_body = {{NULL}, 0};
enum JsonMode { Array, Object};
int extract_json(char *http_response, uchar* dest, JsonMode mode = Object)
{
char *pos = NULL, *pos_end = NULL;
if(mode == Object)
pos = strstr(http_response, "{\"");
else if(mode == Array)
pos = strstr(http_response, "[");
if (pos)
{
if(mode == Object)
pos_end = strrchr(http_response, '}') + 1;
else if(mode == Array)
pos_end = strrchr(http_response, ']') + 1;
if (pos_end)
{
estr::set_null(dest, strlen((char*)dest));
for (uint32 i= 0; i < (pos_end - pos); ++i)
dest[i] = pos[i];
}
}
return 0;
}
int extract_json(char *http_response, json_body_t& jbody, JsonMode mode = Object)
{
char *pos = NULL, *pos_end = NULL;
if(mode == Object)
pos = strstr(http_response, "{\"");
else if(mode == Array)
pos = strstr(http_response, "[");
if (pos)
{
if(mode == Object)
pos_end = strrchr(http_response, '}') + 1;
else if(mode == Array)
pos_end = strrchr(http_response, ']') + 1;
if (pos_end)
{
//jbody.sz = strlen(pos) - strlen(pos_end);
jbody.sz = 0;
estr::set_null(jbody.body_arr, strlen((char*)jbody.body_arr));
for (uint32 &i= json_body.sz; i < (pos_end - pos); ++i)
jbody.body_arr[i] = pos[i];
}
}
return 0;
}
int extract_json(uchar *http_response, json_body_t& jbody, JsonMode mode = Object)
{
return extract_json((char*)http_response, jbody, mode);
}
bool response_body(char *http_response, json_body_t& jbody)
{
int16 index = estr::str_index<int16>(http_response, "\r\n\r\n");
if (index != -1)
{
estr::set_null(jbody.body_arr, sizeof jbody.body_arr);
index += 4;
jbody.sz = strlen(http_response) - index;
estr::copy_from_n(http_response, index, jbody.body_arr, jbody.sz);
return true;
}
return false;
}
bool response_body(char *http_response, char* dest)
{
int16 index = estr::str_index<int16>(http_response, "\r\n\r\n");
if (index != -1)
{
index += 4;
estr::copy_from_n(http_response, index, dest, strlen(http_response) - index);
return true;
}
return false;
}
bool response_body(uchar *http_response, uchar* dest)
{
return response_body((char*)http_response, (char*)dest);
}
void create_config_file()
{
file_system_t fl;
fl.set_file_name("config.cfg");
bool b = fl.exists();
if(!b)
{
fl.set_max_size(_5KB);
fl.create_overwrite();
}
}
template <class T>
void clear_json_buffer(T& instance)
{
instance = T();
}
void reset_screen_pos_display()
{
TP_SetDisplayArea( 0, 0, 127, 63);
TP_ScrGotoxyEx(0, 0);
}
int32& last_error() { return error_code; }
void wait_till_key_press()
{
while (1)
{
if (TP_Kbhit() == 0xFF)
{
TP_GetKey();
TP_Kbflush();
break;
}
}
}
}
namespace random
{
char arr[16], tmp[16];
uint16 len;
double unifRand()
{
return rand() / double(RAND_MAX);
}
double unifRand(double a, double b)
{
return (b - a) * unifRand() + a;
}
int unifRand(int n)
{
if (n < 0) n = -n;
if (n==0) return 0;
int guard = (int) (unifRand() * n) +1;
return (guard > n)? n : guard;
}
// Reset the random number generator with the system clock.
void seed()
{
srand(date_time::get_time());
}
void randomise()
{
seed();
int32 j = 0, k = 0;
for (int i = 0; i < 5; ++i)
{
j += unifRand() * 795849;
k += unifRand() * 981872;
}
sprintf(tmp, "%d", j);
estr::assign(arr, 15, tmp);
sprintf(tmp, "%d", k);
estr::append(arr, 15, tmp);
len = strlen(arr) - 1;
char c = arr[0];
arr[0] = arr[len];
arr[len] = c;
}
}
}
#endif//MISC_H