-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoJetPeer.cpp
More file actions
255 lines (229 loc) · 8.33 KB
/
ArduinoJetPeer.cpp
File metadata and controls
255 lines (229 loc) · 8.33 KB
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
#include <avr/pgmspace.h>
#include "ArduinoJetPeer.h"
#define htonl(x) \
(((x) << 24 & 0xFF000000UL) | ((x) << 8 & 0x00FF0000UL) | \
((x) >> 8 & 0x0000FF00UL) | ((x) >> 24 & 0x000000FFUL))
#define ntohl(x) htonl(x)
char string_buf[JET_MESSAGE_RAM];
char json_buf[JET_JSON_RAM];
int json_ptr = 0;
void *malloc_json(size_t len) {
if ((len + json_ptr) > JET_JSON_RAM) {
Serial.println(F("jet out of mem"));
return NULL;
}
void *ptr = (void *)(json_buf + json_ptr);
json_ptr += len;
return ptr;
}
void free_json(void *ptr) {}
JetPeer::JetPeer() : _sock(0), _req_cnt(0), _state_cnt(0), _fetch_cnt(0) {
aJson.setMemFuncs(malloc_json, free_json);
}
void JetPeer::init(Client &sock) { _sock = &sock; }
void JetPeer::loop(void) {
json_ptr = 0;
if (_sock->available()) {
_sock->read();
while (!_sock->available())
;
_sock->read();
while (!_sock->available())
;
_sock->read();
while (!_sock->available())
;
_sock->read();
dispatch_message();
json_ptr = 0;
}
}
prog_char jet_method[] PROGMEM = "method";
prog_char jet_params[] PROGMEM = "params";
prog_char jet_id[] PROGMEM = "id";
prog_char jet_value[] PROGMEM = "value";
prog_char jet_path[] PROGMEM = "path";
prog_char jet_event[] PROGMEM = "event";
prog_char jet_value_cat[] PROGMEM = "\",\"value\":";
prog_char jet_resp_result[] PROGMEM = "{\"result\":true,\"id\":\"";
prog_char jet_resp_error[] PROGMEM =
"{\"error\":{\"message\":\"Invalid params\",\"code\":-32602},\"id\":\"";
prog_char jet_req_add[] PROGMEM = "{\"method\":\"add\",\"params\":{\"path\":\"";
prog_char jet_req_change[] PROGMEM =
"{\"method\":\"change\",\"params\":{\"path\":\"";
prog_char jet_req_fetch[] PROGMEM = "{\"method\":\"fetch\",\"params\":";
prog_char jet_req_set[] PROGMEM = "{\"method\":\"set\",\"params\":";
prog_char jet_req_call[] PROGMEM = "{\"method\":\"call\",\"params\":";
PROGMEM const char *jet_strings[] = // change "string_table" name to suit
{jet_req_add, jet_req_change, jet_resp_result, jet_resp_error,
jet_method, jet_params, jet_id, jet_value,
jet_value_cat, jet_req_fetch, jet_path, jet_event,
jet_req_set, jet_req_call};
enum jet_string_names {
JET_REQ_ADD,
JET_REQ_CHANGE,
JET_RESP_RESULT,
JET_RESP_ERROR,
JET_METHOD,
JET_PARAMS,
JET_ID,
JET_VALUE,
JET_VALUE_CAT,
JET_REQ_FETCH,
JET_PATH,
JET_EVENT,
JET_REQ_SET,
JET_REQ_CALL
};
void JetPeer::dispatch_message() {
aJsonStream input(_sock);
// reset json_buf, aJson.parse will create dyn objects ->malloc
json_ptr = 0;
aJsonObject *msg = aJson.parse(&input);
if (msg == NULL) {
Serial.println(F("Parse failed"));
return;
}
char *buf = (char *)string_buf;
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_METHOD])));
aJsonObject *method = aJson.getObjectItem(msg, buf);
if (method && method->type == aJson_String) {
String method_str(method->valuestring);
for (int i = 0; i < _state_cnt; ++i) {
JetState &state = _states[i];
if (method_str.equals(state._path)) {
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_PARAMS])));
aJsonObject *params = aJson.getObjectItem(msg, buf);
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_ID])));
aJsonObject *id = aJson.getObjectItem(msg, buf);
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_VALUE])));
aJsonObject *value = aJson.getObjectItem(params, buf);
bool ok = state._handler(value, state._context);
if (id) {
if (ok) {
strcpy_P(buf,
(char *)pgm_read_word(&(jet_strings[JET_RESP_RESULT])));
} else {
strcpy_P(buf,
(char *)pgm_read_word(&(jet_strings[JET_RESP_ERROR])));
}
strcat(buf, id->valuestring);
strcat(buf, "\"}");
send(buf);
}
if (ok) {
change(state._path, value);
}
return;
}
}
} else if (method && method->type == aJson_Int) {
JetFetcher &fetcher = _fetchers[method->valueint];
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_PARAMS])));
aJsonObject *params = aJson.getObjectItem(msg, buf);
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_VALUE])));
aJsonObject *value = aJson.getObjectItem(params, buf);
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_PATH])));
aJsonObject *path = aJson.getObjectItem(params, buf);
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_EVENT])));
aJsonObject *event = aJson.getObjectItem(params, buf);
fetcher._handler(path->valuestring, event->valuestring, value,
fetcher._context);
}
}
void JetPeer::send(const char *msg) {
uint32_t len = strlen(msg);
uint32_t len_net = htonl(len);
// Serial.println(msg);
_sock->write((const uint8_t *)&len_net, sizeof(len_net));
_sock->write((const uint8_t *)msg, len);
}
void JetPeer::value_request(const char *path, aJsonObject *val, int req_id) {
char *buf = string_buf;
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[req_id])));
strcat(buf, path);
strcat_P(buf, (char *)pgm_read_word(&(jet_strings[JET_VALUE_CAT])));
int len_so_far = strlen(buf);
aJsonStringStream stringStream(NULL, buf + len_so_far,
JET_MESSAGE_RAM - len_so_far);
aJson.print(val, &stringStream);
strcat(buf, "}}");
send(buf);
}
void JetPeer::add(const char *path, aJsonObject *val) {
value_request(path, val, JET_REQ_ADD);
}
void JetPeer::change(const char *path, aJsonObject *val) {
value_request(path, val, JET_REQ_CHANGE);
}
void JetPeer::fetch_request(int fetch_id, aJsonObject *fetch_expr) {
char *buf = string_buf;
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_REQ_FETCH])));
int len_so_far = strlen(buf);
aJson.addItemToObject(fetch_expr, "id", aJson.createItem(fetch_id));
aJsonStringStream stringStream(NULL, buf + len_so_far,
JET_MESSAGE_RAM - len_so_far);
aJson.print(fetch_expr, &stringStream);
strcat(buf, "}");
send(buf);
}
void JetPeer::set(const char *path, aJsonObject *value) {
char *buf = string_buf;
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_REQ_SET])));
int len_so_far = strlen(buf);
aJsonObject *params = aJson.createObject();
aJson.addItemToObject(params, "path", aJson.createItem(path));
aJson.addItemToObject(params, "value", value);
aJsonStringStream stringStream(NULL, buf + len_so_far,
JET_MESSAGE_RAM - len_so_far);
aJson.print(params, &stringStream);
strcat(buf, "}");
send(buf);
}
void JetPeer::call(const char *path, aJsonObject *args) {
char *buf = string_buf;
strcpy_P(buf, (char *)pgm_read_word(&(jet_strings[JET_REQ_CALL])));
int len_so_far = strlen(buf);
aJsonObject *params = aJson.createObject();
aJson.addItemToObject(params, "path", aJson.createItem(path));
aJson.addItemToObject(params, "args", args);
aJsonStringStream stringStream(NULL, buf + len_so_far,
JET_MESSAGE_RAM - len_so_far);
aJson.print(params, &stringStream);
strcat(buf, "}");
send(buf);
}
JetFetcher *JetPeer::fetch(aJsonObject *expr, fetch_handler_t handler,
void *context) {
JetFetcher &fetcher = _fetchers[_fetch_cnt];
fetcher._handler = handler;
fetcher._context = context;
fetch_request(_fetch_cnt, expr);
_fetch_cnt++;
return &fetcher;
}
JetFetcher *JetPeer::fetch(const char *path, fetch_handler_t handler,
void *context) {
aJsonObject *fetch_expr = aJson.createObject();
aJsonObject *path_obj = aJson.createObject();
aJson.addItemToObject(path_obj, "equals", aJson.createItem(path));
aJson.addItemToObject(fetch_expr, "path", path_obj);
return fetch(fetch_expr, handler, context);
}
bool default_set_handler(aJsonObject *value, void *context) { return false; }
JetState *JetPeer::state(const char *path, aJsonObject *val,
set_handler_t handler, void *context) {
JetState &state = _states[_state_cnt];
state._path = path;
if (handler) {
state._handler = handler;
state._context = context;
} else {
state._handler = default_set_handler;
}
state._peer = this;
add(path, val);
_state_cnt++;
return &state;
}
void JetState::value(aJsonObject *val) { _peer->change(_path, val); }