forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl_ds.c
More file actions
235 lines (202 loc) · 7 KB
/
impl_ds.c
File metadata and controls
235 lines (202 loc) · 7 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
#include "devs_internal.h"
#include <math.h>
uint32_t devs_compute_timeout(devs_ctx_t *ctx, value_t t) {
uint32_t max = 0xffffffff;
if (devs_is_null_or_undefined(t))
return max;
if (devs_is_tagged_int(t))
return t.val_int32 < 0 ? 0 : t.val_int32;
if (devs_is_special(t) && devs_handle_value(t) == DEVS_SPECIAL_INF)
return max;
double v = devs_value_to_double(ctx, t);
if (v > max)
return max;
if (v < 0)
return 0;
return (uint32_t)v;
}
void fun1_DeviceScript_sleep(devs_ctx_t *ctx) {
unsigned time = devs_compute_timeout(ctx, devs_arg(ctx, 0));
devs_fiber_sleep(ctx->curr_fiber, time);
}
void fun1_DeviceScript_delay(devs_ctx_t *ctx) {
fun1_DeviceScript_sleep(ctx);
}
void fun1_DeviceScript__panic(devs_ctx_t *ctx) {
unsigned code = devs_arg_int(ctx, 0);
if (code == 0xab04711) {
DMESG("! User-requested JD_PANIC()");
JD_PANIC();
}
if (code == 0 || code >= 60000)
code = 59999;
devs_panic(ctx, code);
}
void fun0_DeviceScript_reboot(devs_ctx_t *ctx) {
target_reset();
}
void fun0_DeviceScript_restart(devs_ctx_t *ctx) {
devs_panic(ctx, 0);
}
void funX_DeviceScript_format(devs_ctx_t *ctx) {
if (ctx->stack_top_for_gc < 2)
return;
value_t fmtv = devs_arg(ctx, 0);
unsigned len;
const char *fmt = devs_string_get_utf8(ctx, fmtv, &len);
if (fmt == NULL)
return;
unsigned numargs = ctx->stack_top_for_gc - 2;
value_t *argp = ctx->the_stack + 2;
char tmp[64];
size_t length;
unsigned sz = devs_strformat(ctx, fmt, len, tmp, sizeof(tmp), argp, numargs, &length);
sz--;
length--;
value_t r;
char *d = devs_string_prep(ctx, &r, sz, length);
if (d) {
if (sz < sizeof(tmp) - 1) {
memcpy(d, tmp, sz);
} else {
sz = devs_strformat(ctx, fmt, len, d, sz + 1, argp, numargs, &length);
length--;
sz--;
}
devs_string_finish(ctx, &r, sz, length);
}
devs_ret(ctx, r);
}
void fun2_DeviceScript_print(devs_ctx_t *ctx) {
int lev = devs_arg_int(ctx, 0);
if (lev <= 0 || lev > 0x80)
lev = '>';
value_t s = devs_arg(ctx, 1);
s = devs_value_to_string(ctx, s);
devs_jd_send_logmsg(ctx, lev, s);
}
void fun1_DeviceScript_parseFloat(devs_ctx_t *ctx) {
devs_ret_double(ctx, devs_arg_double(ctx, 0));
}
void fun1_DeviceScript_parseInt(devs_ctx_t *ctx) {
devs_ret_double(ctx, trunc(devs_arg_double(ctx, 0)));
}
void fun2_DeviceScript__logRepr(devs_ctx_t *ctx) {
value_t v = devs_arg(ctx, 0);
value_t lbl = devs_arg(ctx, 1);
if (devs_is_nullish(lbl)) {
DMESG("> %s", devs_show_value(ctx, v));
} else {
lbl = devs_value_to_string(ctx, lbl);
devs_value_pin(ctx, lbl);
const char *p = devs_string_get_utf8(ctx, lbl, NULL);
DMESG("> %s: %s", p, devs_show_value(ctx, v));
devs_value_unpin(ctx, lbl);
}
}
void fun1_DeviceScript__dcfgString(devs_ctx_t *ctx) {
value_t lbl = devs_arg(ctx, 0);
lbl = devs_value_to_string(ctx, lbl);
const char *key = devs_string_get_utf8(ctx, lbl, NULL);
if (key) {
unsigned sz;
const char *v = dcfg_get_string(key, &sz);
if (v)
devs_ret(ctx, devs_value_from_gc_obj(ctx, devs_string_try_alloc_init(ctx, v, sz)));
}
}
void fun0_DeviceScript_millis(devs_ctx_t *ctx) {
devs_ret(ctx, devs_value_from_double((double)ctx->_now_long));
}
void fun1_DeviceScript_deviceIdentifier(devs_ctx_t *ctx) {
value_t tp = devs_arg(ctx, 0);
uint64_t id;
if (devs_value_eq_builtin_string(ctx, tp, DEVS_BUILTIN_STRING_SELF))
id = jd_device_id();
else if (devs_value_eq_builtin_string(ctx, tp, DEVS_BUILTIN_STRING_SERVER))
id = devs_jd_server_device_id();
else
return;
devs_ret(ctx, devs_string_sprintf(ctx, "%*p", 8, &id));
}
void fun2_DeviceScript__serverSend(devs_ctx_t *ctx) {
unsigned service_idx = devs_arg_int(ctx, 0);
devs_packet_t *pkt = devs_value_to_packet_or_throw(ctx, devs_arg(ctx, 1));
if (pkt == NULL)
return;
if (service_idx == 0x100) {
static uint8_t event_cnt;
++event_cnt;
if ((pkt->service_command & ~JD_CMD_EVENT_CODE_MASK) != JD_CMD_EVENT_MASK)
devs_throw_expecting_error(ctx, DEVS_BUILTIN_STRING_EVENT, devs_arg(ctx, 1));
else
pkt->service_command |= (event_cnt & JD_CMD_EVENT_COUNTER_MASK)
<< JD_CMD_EVENT_COUNTER_SHIFT;
return;
}
if (service_idx > 0x30) {
devs_throw_too_big_error(ctx, DEVS_BUILTIN_STRING_SERVICEINDEX);
return;
}
pkt->service_index = service_idx;
unsigned sz = pkt->payload->length;
if (sz > JD_SERIAL_PAYLOAD_SIZE) {
devs_throw_too_big_error(ctx, DEVS_BUILTIN_STRING_PACKET);
return;
}
ctx->packet.service_index = pkt->service_index;
ctx->packet.service_command = pkt->service_command;
ctx->packet.device_identifier = pkt->device_id;
ctx->packet.flags = pkt->flags;
ctx->packet.service_size = sz;
memcpy(ctx->packet.data, pkt->payload->data, sz);
devs_jd_send_raw(ctx);
}
void fun2_DeviceScript__allocRole(devs_ctx_t *ctx) {
unsigned service_cls = devs_arg_int(ctx, 0);
value_t name = devs_arg(ctx, 1);
if (!devs_is_undefined(name) && !devs_is_string(ctx, name)) {
devs_throw_expecting_error(ctx, DEVS_BUILTIN_STRING_STRING, name);
return;
}
if (service_cls && (service_cls & 0xF0000000) != 0x10000000) {
devs_throw_range_error(ctx, "0x1xxxxxxx expected for service class");
return;
}
int r = devs_jd_alloc_role(ctx, name, service_cls);
if (r == -2) {
devs_throw_type_error(ctx, "spec missing: %x", service_cls);
return;
}
if (r == -3) {
devs_throw_range_error(ctx, "role name '%s' already used",
devs_string_get_utf8(ctx, name, NULL));
return;
}
if (r < 0)
return; // OOM
devs_ret(ctx, devs_value_from_handle(DEVS_HANDLE_TYPE_ROLE, r));
}
void fun0_DeviceScript_notImplemented(devs_ctx_t *ctx) {
devs_throw_generic_error(ctx, "body missing");
}
__attribute__((weak)) void devs_send_large_frame(const void *data, unsigned size) {}
void fun2_DeviceScript__twinMessage(devs_ctx_t *ctx) {
unsigned topic_size, data_size;
const void *topic = devs_string_get_utf8(ctx, devs_arg(ctx, 0), &topic_size);
const void *data = devs_bufferish_data(ctx, devs_arg(ctx, 1), &data_size);
if (topic == NULL || data == NULL) {
devs_throw_type_error(ctx, "expecting topic and data");
return;
}
devs_buffer_t *buf = devs_buffer_try_alloc(ctx, 4 + 4 + 8 + topic_size + 1 + data_size);
if (buf == NULL)
return;
devs_ret_gc_ptr(ctx, buf);
buf->data[2] = 0xff;
uint64_t id = devs_jd_server_device_id();
memcpy(buf->data + 8, &id, 8);
memcpy(buf->data + 16, topic, topic_size);
memcpy(buf->data + 16 + topic_size + 1, data, data_size);
devs_send_large_frame(buf->data, buf->length);
}