-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoant.c
More file actions
375 lines (295 loc) · 10.3 KB
/
autoant.c
File metadata and controls
375 lines (295 loc) · 10.3 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
auto-ant script parser / interpreter
Copyright (c) 2013, Quarq Technology / SRAM LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of Quarq Technology nor SRAM LLC nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "autoant.h"
#include <ctype.h> // for isspace() macro
#include <string.h> // for strlen(), memcmp()
//#include <stdio.h> // debugging
#define AUTOANT_MAX_RECURSION_DEPTH 5
#define AUTOANT_MAX_LINE_LEN 240
static int autoant_delay;
typedef struct {
int position; // bytes into file
int linenumber;
int iterations_left;
} autoant_stack_frame_t;
static autoant_stack_frame_t autoant_stack[AUTOANT_MAX_RECURSION_DEPTH];
static int stack_depth;
static autoant_ops_t *op;
static void autoant_init_parser(void) {
autoant_delay=3000;
stack_depth=0;
autoant_stack[stack_depth].position=0;
autoant_stack[stack_depth].linenumber=0;
autoant_stack[stack_depth].iterations_left=0;
}
char *autoant_interpret_warning(int warnno) {
switch (warnno) {
case AUTOANT_WARN_SIZE_EXCEEDED:
return "Buffer size exceeded";
case AUTOANT_WARN_LOOP_NESTING:
return "loopend statement without previous loop() statement";
case AUTOANT_WARN_NO_ITERATION:
return "No iterations specified in loop() statement";
case AUTOANT_WARN_STACK_OVERFLOW:
return "Too many nested loop() statements";
case AUTOANT_WARN_TRUNCATED_LINE:
return "Line exceeded compiled-in limit";
case AUTOANT_WARN_GARBAGE_IN_BRACKETS:
return "Found unknown (non-hexadecimal) character within []";
case AUTOANT_WARN_HARD_TIMEOUT:
return "Failed hard read timeout";
default:
return "Unknown warning";
}
}
#define WARN(warnno) do { \
if (op->warn) op->warn(warnno, \
autoant_stack[stack_depth].linenumber, \
autoant_stack[stack_depth].position); \
} while (0);
#define OUTPUT(msg) do { \
if (op->write) op->write(msg); \
} while (0);
#define LINE_STARTSWITH(val) 0==memcmp(line,val,strlen(val))
// define the overall length of Ant message based on length byte
int autoant_antlen(uint8_t *c) {
// header+length byte + id + data + checksum
return 1+2+c[1]+1;
}
static int get_pos_integer(char *line) {
int ret=0;
int found_num=0;
char *c=line;
while ((*c) && isspace(*c))
c++;
while (1) {
switch (*c) {
case '0'...'9':
found_num = 1;
ret *= 10;
ret += (*c-'0');
break;
default:
if (0==found_num) ret=-1;
return ret;
}
c++;
}
}
#define MAX_BUFFER_LEN 32
static uint8_t char_array_readout[MAX_BUFFER_LEN];
static uint8_t *get_hex_char_array(char *line) {
uint8_t *c=(uint8_t *)line;
uint16_t current;
int len=0;
uint8_t checksum=0;
char_array_readout[len++]=0xa4; checksum ^= 0xa4;
len++; // skip length byte, fill in later
while (1) {
while ((*c) && (*c != '[')) {
c++;
}
if (*c=='\0') goto done;
// now *c is '[' and we're ready to parse hex.
current=0;
while (*c != ']') {
//printf("char '%c' current %02x\n",*c,current);
switch (*c) {
case 0:
goto done;
case 'a'...'f':
current <<= 4;
current += 10+(*c-'a');
break;
case 'A'...'F':
current <<= 4;
current += 10+(*c-'A');
break;
case '0'...'9':
current <<= 4;
current += (*c-'0');
break;
case '[':
break;
default:
//printf("Skipping char '%c'\n",*c);
WARN(AUTOANT_WARN_GARBAGE_IN_BRACKETS);
break;
}
c++;
}
//printf("Got char %02x\n",current);
char_array_readout[len++]=current; checksum ^= current;
if (len+1==MAX_BUFFER_LEN) {
WARN(AUTOANT_WARN_SIZE_EXCEEDED);
return NULL;
}
}
done:
if (len==0) {
return NULL;
} else {
char_array_readout[1]=(len-3); checksum ^= (len-3);
char_array_readout[len++]=checksum;
return char_array_readout;
}
}
// returns -1 for end processing, 0 for continue.
static int autoant_run_line(char *line, int len) {
//printf("Processing: %s\n", line);
/* skip whitespace */
char *end_char=line+len; // actually, one past last character
while (line < end_char) {
if (isspace(*line)) {
line++;
} else {
break;
}
}
/* remove comments off the end */
char *x=line;
while (x<end_char) {
if (*x=='#')
end_char=x;
x++;
}
*end_char=0; // null term, just in case
//printf("actual line %d (iteration %d): '%s'\n",
//autoant_stack[stack_depth].linenumber,
// autoant_stack[stack_depth].iterations_left, line);
if (line==end_char)
return 0; // blank line, skip
if (LINE_STARTSWITH("o")) {
OUTPUT(line+1);
} else if (LINE_STARTSWITH("d")) {
int d=get_pos_integer(line+1);
uint16_t timeout = op->get_milliseconds()+d;
int16_t timeremain;
while ((timeremain = timeout - op->get_milliseconds())>0) {
if (op->delay_ms) op->delay_ms();
}
if (op->flush_ant) op->flush_ant();
} else if (LINE_STARTSWITH("w")) {
uint8_t *c=get_hex_char_array(line+1);
op->send_ant_message(c);
} else if (LINE_STARTSWITH("r")) {
line++;
int hard_fail=(*line =='!');
if (hard_fail) line++;
int d=get_pos_integer(line);
if (d<0) d=autoant_delay;
uint8_t *c=get_hex_char_array(line);
if (c==NULL) {
autoant_delay=d;
return 0;
}
uint8_t *c2;
uint16_t timeout = op->get_milliseconds()+d;
int16_t timeremain;
while ((timeremain = timeout - op->get_milliseconds())>0) {
c2=op->receive_ant_message(timeremain);
if (memcmp(c2,c,autoant_antlen(c))) c2 = NULL;
if (c2) break;
}
if (c2==NULL) {
if (hard_fail) {
WARN(AUTOANT_WARN_HARD_TIMEOUT);
return -1;
}
}
} else if (LINE_STARTSWITH("pauseBreak")) {
if (op->pause) op->pause();
} else if (LINE_STARTSWITH("loopend")) {
if (stack_depth==0) {
WARN(AUTOANT_WARN_LOOP_NESTING);
return -1;
}
if (autoant_stack[stack_depth].iterations_left) {
op->seek(autoant_stack[stack_depth-1].position);
autoant_stack[stack_depth].position=autoant_stack[stack_depth-1].position;
autoant_stack[stack_depth].linenumber=autoant_stack[stack_depth-1].linenumber;
autoant_stack[stack_depth].iterations_left -= 1;
} else {
autoant_stack[stack_depth-1].position=autoant_stack[stack_depth].position;
autoant_stack[stack_depth-1].linenumber=autoant_stack[stack_depth].linenumber;
stack_depth-=1;
}
} else if (LINE_STARTSWITH("loop")) {
while (*line)
if (*line++=='(') break;
int iterations=get_pos_integer(line);
if (iterations<1) {
WARN(AUTOANT_WARN_NO_ITERATION);
return -1;
}
if ((stack_depth+1) >=AUTOANT_MAX_RECURSION_DEPTH) {
WARN(AUTOANT_WARN_STACK_OVERFLOW);
return -1;
}
autoant_stack[stack_depth+1].position=autoant_stack[stack_depth].position;
autoant_stack[stack_depth+1].linenumber=autoant_stack[stack_depth].linenumber;
autoant_stack[stack_depth+1].iterations_left=iterations-1;
stack_depth++;
}
return 0;
}
static char linebuf[AUTOANT_MAX_LINE_LEN];
// reads a line and runs it
/* returns: -1 for runtime error
-2 for parse error
0 for EOF, line was OK.
1 for OK, not EOF */
static int autoant_read_line(void) {
int len=0;
int c;
do {
c=op->get_char();
autoant_stack[stack_depth].position++;
if (c<0) break;
if (len<AUTOANT_MAX_LINE_LEN) {
linebuf[len++]=c;
}
} while (c != '\n');
autoant_stack[stack_depth].linenumber++;
if (len == AUTOANT_MAX_LINE_LEN) { WARN(AUTOANT_WARN_TRUNCATED_LINE); len--; }
linebuf[len]='\0'; // overwrite \n with \0 for c-friendly null terminator
int ret=autoant_run_line(linebuf, len);
if ((c >= 0) && (ret==0)) ret=1;
return ret;
}
int autoant_exec(autoant_ops_t *ops) {
op=ops;
op->seek(0);
int ret;
autoant_init_parser();
do {
ret=autoant_read_line();
} while (ret == 1);
return ret;
}