forked from toniebox-reverse-engineering/hackiebox_cfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxCLI.cpp
460 lines (406 loc) · 14.4 KB
/
BoxCLI.cpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "BoxCLI.h"
#include "Hackiebox.h"
#include "BoxEvents.h"
#include <ESP8266SAM.h>
void BoxCLI::begin() {
setInterval(50);
cmdI2C = cli.addCmd("i2c");
cmdI2C.setDescription(" Access I2C");
cmdI2C.addFlagArg("re/ad");
cmdI2C.addFlagArg("wr/ite");
cmdI2C.addArg("a/ddress");
cmdI2C.addArg("r/egister");
cmdI2C.addArg("v/alue", "");
cmdI2C.addArg("l/ength", "1");
cmdI2C.addArg("o/utput", "B");
cmdSpiRFID = cli.addCmd("spi-rfid");
cmdSpiRFID.setDescription(" Access RFID SPI");
cmdSpiRFID.addFlagArg("re/ad");
cmdSpiRFID.addFlagArg("wr/ite");
cmdSpiRFID.addFlagArg("c/md,co/mmand");
cmdSpiRFID.addArg("r/egister", "0");
cmdSpiRFID.addArg("v/alue", "0");
cmdBeep = cli.addCmd("beep");
cmdBeep.setDescription(" Beep with build-in DAC synthesizer");
cmdBeep.addArg("m/idi-id", "60");
cmdBeep.addArg("l/ength", "200");
cmdRFID = cli.addCmd("rfid");
cmdRFID.setDescription(" Access RFID");
cmdRFID.addFlagArg("u/id");
cmdRFID.addFlagArg("r/ead");
cmdRFID.addFlagArg("m/emory");
cmdRFID.addFlagArg("d/ump");
cmdRFID.addFlagArg("o/verwrite");
cmdLoad = cli.addCmd("load");
cmdLoad.setDescription(" Shows the load of all threads");
cmdLoad.addArg("n/ame", "");
cmdLoad.addArg("p/ointer", "0");
cmdLoad.addFlagArg("r/eset");
cmdHelp = cli.addSingleArgumentCommand("help");
cmdHelp.setDescription(" Show this screen");
cmdI2S = cli.addCmd("i2s");
cmdI2S.setDescription(" I2S debug information");
cmdI2S.addFlagArg("l/og");
cmdI2S.addArg("t/est", 0);
cmdI2S.addArg("f/requency", "440");
cmdSay = cli.addCmd("say");
cmdSay.setDescription(" Generate speech with SAM");
cmdSay.addPosArg("t/ext");
cmdSay.addArg("v/oice", "0");
cmdSay.addArg("s/peed", "0");
cmdSay.addArg("p/itch", "0");
cmdSay.addArg("t/hroat", "0");
cmdSay.addArg("m/outh", "0");
cmdSay.addFlagArg("sing");
cmdSay.addFlagArg("p/hoentic");
cmdAudio = cli.addCmd("audio");
cmdAudio.setDescription(" Play/Pause audio files");
cmdAudio.addFlagArg("p/lay,pause");
cmdAudio.addArg("f/ile/name", "");
cmdAudio.addArg("g/en-limit", "0");
cmdAudio.addArg("b/uffer", "-1");
cmdAudio.addArg("m/axSampleRate", "0");
}
void BoxCLI::loop() {
parse();
}
void BoxCLI::parse() {
if (cli.available()) {
lastCmd = cli.getCmd();
if (lastCmd == cmdHelp) {
Log.println("Help:");
Log.println();
if (lastCmd.getArg(0).isSet()) {
String arg = lastCmd.getArg(0).getValue();
Command cmd = cli.getCmd(arg);
if (cmd.getName() == arg) {
Log.println(cmd.toString().c_str());
return;
}
}
Log.println(cli.toString().c_str());
} else if (lastCmd == cmdI2C) {
execI2C();
} else if (lastCmd == cmdSpiRFID) {
execSpiRFID();
} else if (lastCmd == cmdBeep) {
execBeep();
} else if (lastCmd == cmdRFID) {
execRFID();
} else if (lastCmd == cmdLoad) {
execLoad();
} else if (lastCmd == cmdI2S) {
execI2S();
} else if (lastCmd == cmdSay) {
execSay();
} else if (lastCmd == cmdAudio) {
execAudio();
}
}
if (cli.errored()) {
CommandError cmdError = cli.getError();
Log.print("ERROR: ");
Log.println(cmdError.toString().c_str());
if (cmdError.hasCommand()) {
Log.print("Did you mean \"");
Log.print(cmdError.getCommand().toString().c_str());
Log.println("\"?");
}
}
}
void BoxCLI::execI2C() {
Command c = lastCmd;
int argNum = c.countArgs();
unsigned long int tmpNum;
uint8_t addr, regi;
bool read = c.getArg("read").isSet();
bool write = c.getArg("write").isSet();
if (read == write) {
Log.error("Either read or write has to be set");
return;
}
String saddress = c.getArg("address").getValue();
String sregister = c.getArg("register").getValue();
tmpNum = parseNumber(saddress);
if (tmpNum > 127) {
Log.error("address must be <128");
return;
}
addr = (uint8_t)tmpNum;
tmpNum = parseNumber(sregister);
if (tmpNum > 255) {
Log.error("register must be <256");
return;
}
regi = (uint8_t)tmpNum;
//Log.printfln("I2C command read=%T, write=%T, addr=%X, regi=%X", read, write, addr, regi);
String slength = c.getArg("length").getValue();
String svalue = c.getArg("value").getValue();
if (read) {
String soutput = c.getArg("output").getValue();
if (!(soutput == "x" || soutput == "X" || soutput == "i" || soutput == "b" || soutput == "B")) {
Log.error("Allowed output values are: x, X, i, b, B");
return;
}
if (slength.length() == 0) {
Log.error("length must be specified");
return;
}
tmpNum = parseNumber(slength);
unsigned long len = tmpNum;
Log.printfln("Read %i bytes", len);
for (uint16_t i=0; i<len; i++) {
String format = String(" %") + soutput;
uint8_t result = Box.boxI2C.readByte(addr, regi+i);
Log.printf(format.c_str(), result);
}
} else if (write) {
char* value = (char*)svalue.c_str();
char* newVal;
while (strlen(value)>0) {
while(isspace((unsigned char)*value)) value++;
tmpNum = parseNumber(value, &newVal);
if (tmpNum > 255) {
Log.error("value must be <256");
return;
} else if (value == newVal) {
Log.error("Couldn't parse part \"%s\" of \"%s\"", value, svalue.c_str());
return;
}
uint8_t data = (uint8_t)tmpNum;
bool result = Box.boxI2C.send(addr, regi++, data);
if (!result) {
Log.error(" %X not written, abort", data);
break;
}
Log.info(" %X successful written", data);
value = newVal;
}
}
}
void BoxCLI::execSpiRFID() {
Command c = lastCmd;
unsigned long tmpNum;
String sregister = c.getArg("register").getValue();
tmpNum = parseNumber(sregister);
if (tmpNum > 255) {
Log.error("register must be <256");
return;
}
uint8_t regi = (uint8_t)tmpNum;
String svalue = c.getArg("value").getValue();
tmpNum = parseNumber(svalue);
if (tmpNum > 255) {
Log.error("value/command must be <256");
return;
}
uint8_t value = (uint8_t)tmpNum;
bool read = c.getArg("read").isSet();
bool write = c.getArg("write").isSet();
bool cmd = c.getArg("command").isSet();
//TODO Exclusive check
if (read) {
uint8_t result = Box.boxRFID.readRegister(regi);
Log.info("Read from register=%X value=%X", regi, result);
} else if (write) {
Box.boxRFID.writeRegister(regi, value);
Log.info("Write to register=%X value=%X", regi, value);
} else if (cmd) {
Box.boxRFID.sendCommand(value);
Log.info("Send command %X", value);
}
}
void BoxCLI::execBeep() {
Command c = lastCmd;
unsigned long tmpNum;
String sid = c.getArg("midi-id").getValue();
tmpNum = parseNumber(sid);
if (tmpNum > 127) {
Log.error("midi-id must be <128");
return;
}
uint8_t id = (uint8_t)tmpNum;
String slength = c.getArg("length").getValue();
tmpNum = parseNumber(slength);
if (tmpNum > 65535) {
Log.error("length must be <65.536");
return;
}
uint16_t length = (uint16_t)tmpNum;
Box.boxDAC.beepMidi(id, length);
}
void BoxCLI::execRFID() {
Command c = lastCmd;
if (c.getArg("read").isSet()) {
if (Box.boxRFID.tagActive) {
Box.boxRFID.tagActive = false;
Events.handleTagEvent(BoxRFID::TAG_EVENT::TAG_REMOVED);
}
Box.boxRFID.loop();
if (!Box.boxRFID.tagActive) {
Log.error("No tag in place");
}
} else if (c.getArg("uid").isSet()) {
if (!Box.boxRFID.tagActive) {
Log.error("No tag in place, last known is shown");
}
Box.boxRFID.logUID();
} else if (c.getArg("memory").isSet()) {
Box.boxRFID.loop();
if (!Box.boxRFID.tagActive) {
Log.error("No tag in place");
}
Box.boxRFID.logTagMemory();
} else if (c.getArg("dump").isSet()) {
Box.boxRFID.loop();
if (!Box.boxRFID.tagActive) {
Log.error("No tag in place");
}
Box.boxRFID.dumpTagMemory(c.getArg("overwrite").isSet());
} else {
Log.error("Nothing to do...");
}
}
void BoxCLI::execLoad() {
Command c = lastCmd;
String name = c.getArg("name").getValue();
String pointerStr = c.getArg("pointer").getValue();
bool reset = c.getArg("reset").isSet();
unsigned long pointer = parseNumber(pointerStr);
if (name != "") {
Log.info("Statistics for Threads starting with \"%s\"", name.c_str());
Log.println("---");
for (uint8_t i = 0; i < Box.threadController.size(); i++) {
EnhancedThread* thread = (EnhancedThread*)Box.threadController.get(i);
if (strncasecmp(name.c_str(), thread->ThreadName, name.length()) == 0) {
thread->logStats();
if (reset) thread->resetStats();
Log.println();
}
Box.delayTask(1);
}
} else if (pointer > 0) {
Log.info("Statistics for Threads with pointer=%i", pointer);
Log.println("---");
for (uint8_t i = 0; i < Box.threadController.size(); i++) {
EnhancedThread* thread = (EnhancedThread*)Box.threadController.get(i);
if (pointer == thread->ThreadID) {
thread->logStats();
if (reset) thread->resetStats();
Log.println();
}
}
Box.delayTask(1);
} else {
Log.info("Statistics for all %i Threads", Box.threadController.size(false)); //TODO ThreadController
Log.println("---");
for (uint8_t i = 0; i < Box.threadController.size(); i++) {
EnhancedThread* thread = (EnhancedThread*)Box.threadController.get(i);
thread->logStats();
if (reset) thread->resetStats();
Log.println();
Box.delayTask(1);
}
}
if (reset) Log.info("All stats of selected threads reset.");
}
void BoxCLI::execI2S() {
Command c = lastCmd;
unsigned long freq = parseNumber(c.getArg("frequency").getValue());
unsigned long testTime = parseNumber(c.getArg("test").getValue());
if (c.getArg("log").isSet()) {
Box.boxDAC.audioBuffer.logState();
Box.boxDAC.logDmaIrqChanges();
}
if (testTime > 0 && freq > 0) {
Log.info("Run frequency test for %ims with %iHz", testTime, freq);
BoxTimer timer;
timer.setTimer(testTime);
while (timer.isRunning()) {
Box.boxDAC.generateFrequency(freq, timer.getTimeTillEnd());
timer.tick();
}
Box.boxDAC.audioOutput->flush();
if (c.getArg("log").isSet()) {
Box.boxDAC.audioBuffer.logState();
Box.boxDAC.logDmaIrqChanges();
}
}
}
void BoxCLI::execSay() {
Command c = lastCmd;
String text = c.getArg("text").getValue();
ESP8266SAM::SAMVoice voice = (ESP8266SAM::SAMVoice)parseNumber(c.getArg("voice").getValue());
uint8_t speed = parseNumber(c.getArg("speed").getValue());
uint8_t pitch = parseNumber(c.getArg("pitch").getValue());
uint8_t throat = parseNumber(c.getArg("throat").getValue());
uint8_t mouth = parseNumber(c.getArg("mouth").getValue());
bool sing = c.getArg("sing").isSet();
bool phoentic = c.getArg("phoentic").isSet();
if (text != "") {
Box.boxDAC.samSay(
text.c_str(),
voice,
speed,
pitch,
throat,
mouth,
sing,
phoentic
);
}
}
void BoxCLI::execAudio() {
Command c = lastCmd;
String filenameStr = c.getArg("file").getValue();
uint16_t genLimit = (uint16_t)parseNumber(c.getArg("gen-limit").getValue());
uint32_t resampleRate = (uint16_t)parseNumber(c.getArg("maxSampleRate").getValue());
String bufferStr = c.getArg("buffer").getValue();
int32_t buffer = -1;
if (bufferStr != "-1")
buffer = parseNumber(bufferStr);
if (bufferStr != "0" && buffer == 0)
buffer = -1;
if (c.getArg("file").isSet() && filenameStr != "") {
Box.boxDAC.playFile(filenameStr.c_str());
} else if (c.getArg("play").isSet()) {
Box.boxDAC.audioPlaying = !Box.boxDAC.audioPlaying;
} else {
if (genLimit > 0)
Box.boxDAC.audioTimeoutMs = genLimit;
Log.info("Generator time limit is set to %ims", Box.boxDAC.audioTimeoutMs);
if (resampleRate > 0) {
Box.boxDAC.audioOutputResample->SetMaxRate(resampleRate);
Log.info("Max Samplerate is set to %ihz", Box.boxDAC.audioOutputResample->GetMaxRate());
}
if (buffer == 0) {
Log.info("Additional buffering off");
Box.boxDAC.audioOutput = Box.boxDAC.audioOutputResample;
} else if (buffer > 0) {
if (buffer <= freeHeapMemory() / 2) {
Box.boxDAC.audioOutput = Box.boxDAC.audioOutputBuffer;
free(Box.boxDAC.audioOutputBuffer);
Box.boxDAC.audioOutputBuffer = new AudioOutputBuffer(buffer, Box.boxDAC.audioOutputResample);
Box.boxDAC.audioOutput = Box.boxDAC.audioOutputBuffer;
Log.info("Additional buffer set to %ib", buffer);
} else {
Log.error("Buffer should not use more than half of the HEAP (%ib)", freeHeapMemory());
}
}
}
}
unsigned long BoxCLI::parseNumber(String numberString) {
const char* num = numberString.c_str();
return parseNumber((char*)num);
}
unsigned long BoxCLI::parseNumber(char* number) {
return parseNumber(number, NULL);
}
unsigned long BoxCLI::parseNumber(char* number, char** rest) {
if (strncmp(number, "0x", 2) == 0) {
return strtoul(number, rest, 16);
} else if (strncmp(number, "0b", 2) == 0) {
return strtoul(number+2, rest, 2);
}
return strtoul(number, rest, 10);
}