forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
348 lines (302 loc) · 9.13 KB
/
Copy pathmain.c
File metadata and controls
348 lines (302 loc) · 9.13 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
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
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>
#include "jd_sdk.h"
#include "devicescript/devicescript.h"
#include "storage/jd_storage.h"
#define LOG(fmt, ...) DMESG("main: " fmt, ##__VA_ARGS__)
static bool test_mode;
static bool remote_deploy;
static void frame_cb_post(void);
static void frame_cb(void *userdata, jd_frame_t *frame) {
jd_rx_frame_received(frame);
frame_cb_post();
}
bool ends_with(const char *str, const char *suff) {
if (!str)
return false;
int lstr = strlen(str);
int lsuff = strlen(suff);
if (lstr < lsuff)
return false;
return strcmp(str + lstr - lsuff, suff) == 0;
}
bool starts_with(const char *str, const char *pref) {
if (!str)
return false;
return memcmp(str, pref, strlen(pref)) == 0;
}
void init_devicescript_manager(void);
void app_init_services() {
jd_role_manager_init();
init_devicescript_manager();
wsskhealth_init();
devscloud_init(&wssk_cloud);
tsagg_init(&wssk_cloud);
}
struct {
const void *img;
uint32_t size;
uint32_t offset;
uint8_t isopen;
uint8_t finished;
jd_device_service_t *serv;
jd_opipe_desc_t pipe;
} deploy;
static void process_deploy(void) {
if (!deploy.isopen)
return;
int to_write = deploy.size - deploy.offset;
if (to_write > 224)
to_write = 224;
int r = to_write == 0
? jd_opipe_close(&deploy.pipe)
: jd_opipe_write(&deploy.pipe, (uint8_t *)deploy.img + deploy.offset, to_write);
if (r == 0) {
deploy.offset += to_write;
if (to_write == 0) {
DMESG("closed deploy pipe");
deploy.isopen = false;
deploy.finished = true;
exit(0); // TODO?
}
}
}
static void client_event_handler(void *dummy, int event_id, void *arg0, void *arg1) {
jd_device_t *dev = arg0;
// jd_register_query_t *reg = arg1;
// jd_role_t *role = arg1;
// jd_register_query_t *reg = arg1;
jd_device_service_t *serv = arg0;
jd_packet_t *pkt = arg1;
switch (event_id) {
case JD_CLIENT_EV_PROCESS:
process_deploy();
break;
case JD_CLIENT_EV_SERVICE_PACKET:
if (test_mode && serv->service_class == JD_SERVICE_CLASS_DEVICE_SCRIPT_MANAGER &&
jd_event_code(pkt) == JD_DEVICE_SCRIPT_MANAGER_EV_PROGRAM_PANIC) {
jd_device_script_manager_program_panic_t *ev = (void *)pkt->data;
if (ev->panic_code) {
fprintf(stderr, "test failed\n");
exit(10);
}
}
if (deploy.img && serv && deploy.serv == serv) {
if (jd_is_report(pkt) &&
pkt->service_command == JD_DEVICE_SCRIPT_MANAGER_CMD_DEPLOY_BYTECODE) {
DMESG("opening deploy pipe");
if (jd_opipe_open_report(&deploy.pipe, pkt) == 0)
deploy.isopen = true;
}
// we process deploy on every incoming packet - this include ACKs, so should be as fast
// as possible
process_deploy();
}
break;
case JD_CLIENT_EV_DEVICE_DESTROYED:
if (deploy.serv && jd_service_parent(deploy.serv) == dev) {
fprintf(stderr, "deploy device lost\n");
deploy.serv = NULL;
}
break;
case JD_CLIENT_EV_DEVICE_CREATED:
if (deploy.img && !deploy.serv && dev->device_identifier != jd_device_id()) {
deploy.serv = jd_device_lookup_service(dev, JD_SERVICE_CLASS_DEVICE_SCRIPT_MANAGER);
if (deploy.serv) {
char id[5];
jd_device_short_id(id, dev->device_identifier);
DMESG("asking for deploy: %s", id);
jd_service_send_cmd(deploy.serv, JD_DEVICE_SCRIPT_MANAGER_CMD_DEPLOY_BYTECODE,
&deploy.size, 4);
}
}
break;
}
}
int devs_client_deploy(const void *img, unsigned imgsize) {
deploy.img = img;
deploy.size = imgsize;
#ifdef __EMSCRIPTEN__
jd_client_subscribe(client_event_handler, NULL);
#endif
return 0;
}
int load_image(const char *name) {
FILE *f = name ? fopen(name, "rb") : NULL;
if (!f) {
fprintf(stderr, "can't open image '%s'\n", name);
return -1;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
if (size <= 0) {
fprintf(stderr, "can't determine file size for` '%s'\n", name);
return -2;
}
fseek(f, 0, SEEK_SET);
uint8_t *img = jd_alloc(size + 1);
fread(img, size, 1, f);
fclose(f);
if (memcmp("4a6163530a", img, 10) == 0)
size = jd_from_hex(img, (const char *)img);
int r = devs_verify(img, size);
if (r) {
fprintf(stderr, "verification error for '%s': %d\n", name, r);
jd_free(img);
return r;
}
if (remote_deploy) {
devs_client_deploy(img, size);
// don't free - it is lazy
return 0;
}
if (devicescriptmgr_deploy(img, size)) {
fprintf(stderr, "can't deploy '%s'\n", name);
jd_free(img);
return -3;
}
jd_free(img);
return 0;
}
void jd_tcpsock_process(void);
void app_process(void) {
tx_process();
jd_tcpsock_process();
static uint32_t uptime_cnt;
if (jd_should_sample_ms(&uptime_cnt, 5000))
tsagg_update("uptime", (double)now_ms_long / 1000);
}
static void client_process(void) {
jd_process_everything();
}
static void run_sample(const char *name, int keepgoing) {
test_mode = true;
jd_packet_t ask_ann;
jd_pkt_setup_broadcast(&ask_ann, 0, 0);
jd_send_pkt(&ask_ann); // ask everyone for announce
uint64_t the_end = 0x1000000000;
for (uint64_t iter = 0; iter < the_end; iter++) {
if (iter == 10) { // give it some time to get announce etc
if (load_image(name)) {
exit(9);
}
}
client_process();
target_wait_us(10000);
if (!keepgoing && iter > 15 && !devicescriptmgr_get_ctx() && the_end == 0x1000000000) {
// if the script ended, we shall exit soon, but not exactly now
the_end = iter + 15;
}
}
devicescriptmgr_deploy(NULL, 0);
jd_lstore_force_flush();
jd_services_deinit();
}
static jd_transport_ctx_t *transport_ctx = NULL;
#ifdef __EMSCRIPTEN__
#include <emscripten/eventloop.h>
bool websock_is_connected(jd_transport_ctx_t *ctx);
static void em_process(void *dummy) {
if (!websock_is_connected(transport_ctx))
return;
client_process();
}
static void frame_cb_post(void) {
em_process(NULL);
}
void run_emscripten_loop(void) {
emscripten_set_interval(em_process, 10, NULL);
emscripten_unwind_to_js_event_loop();
}
#else
static void frame_cb_post(void) {}
#endif
#ifndef __EMSCRIPTEN__
int main(int argc, const char **argv) {
const jd_transport_t *transport = NULL;
const char *transport_arg = NULL;
const char *devs_img = NULL;
#if 0
uint64_t devid;
jd_from_hex(&devid, "1989f4ee00000000");
for (;;) {
devid += 0x71000000000;
char s[30];
jd_device_short_id(s, devid);
if (strcmp(s, "WS42") == 0) {
jd_to_hex(s, &devid, sizeof(devid));
printf("%s 0x%llx\n", s, devid);
return 0;
}
}
#endif
int enable_lstore = 0;
int websock = 0;
for (int i = 1; i < argc; ++i) {
const char *arg = argv[i];
if (starts_with(arg, "/dev/")) {
transport_arg = arg;
transport = &hf2_transport;
remote_deploy = 1;
} else if (atoi(arg)) {
transport_arg = arg;
transport = &sock_transport;
} else if (ends_with(arg, ".jacs")) {
devs_img = arg;
} else if (strcmp(arg, "-l") == 0) {
enable_lstore = 1;
} else if (strcmp(arg, "-X") == 0) {
devs_set_global_flags(JACS_FLAG_GC_STRESS);
} else if (strcmp(arg, "-w") == 0) {
websock = 1;
} else {
fprintf(stderr, "unknown arg: %s\n", arg);
return 1;
}
}
if (!transport && !devs_img && !websock) {
fprintf(stderr, "need transport and/or image\n");
return 1;
}
if (transport) {
transport_ctx = transport->alloc();
transport->set_frame_callback(transport_ctx, frame_cb, NULL);
if (transport->connect(transport_ctx, transport_arg) != 0)
return 1;
}
tx_init(transport, transport_ctx);
jd_rx_init();
if (enable_lstore)
jd_lstore_init();
jd_services_init();
{
uint64_t devid = jd_device_id();
char hexbuf[17];
jd_to_hex(hexbuf, &devid, 8);
DMESG("self-device: %-s %s", jd_device_short_id_a(jd_device_id()), hexbuf);
}
if (devs_img && remote_deploy) {
load_image(devs_img);
devs_img = NULL;
}
jd_client_subscribe(client_event_handler, NULL);
if (devs_img) {
run_sample(devs_img, websock);
} else {
#ifdef __EMSCRIPTEN__
run_emscripten_loop();
#else
for (;;) {
client_process();
target_wait_us(10000);
}
#endif
}
return 0;
}
#endif