forked from dastcvi/StrateoleXML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLWriter_v5.cpp
641 lines (557 loc) · 14.2 KB
/
XMLWriter_v5.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* XMLWriter_v5.cpp
* XML writer specifically designed for communicating with
* Zephyr Iridium Modem
* Author: Marika Schubert
* January 2017
*
* Adapted: Alex St. Clair (v4, v5)
* August 2019
*
* This library will allow devices on the Strateole project
* to write specific message types in XML to the Zephyr Gondola.
* This file will write the Tag->Tag contents of a message to a
* character output buffer, run the CRC check on this buffer, then
* send the buffer with the proper CRC.
*
* * Version 3 adds DIB/MCB Communications
* * Version 4 updates the interface for StratoCore
* * Version 5 removes DIB/MCB Communications, cleans the interface, and
* adds bigger, safer, more efficient bufferering
*
* This code has the following dependencies:
*
* "Arduino.h"
*/
#include <XMLWriter_v5.h>
const uint16_t reset_crc = 0x1021;
// Constant Device Information
char swDate[] = "20170901,000000";
char swVer[] = "0.1";
char Zproto[] = "1.0";
// --------------------------------------------------------
// Constructor and reset
// --------------------------------------------------------
#ifdef XMLDEBUG
XMLWriter::XMLWriter(Print* stream, Instrument_t inst, Stream* log)
{
_log = log;
_log->println("Written messages with be logged.");
#else
XMLWriter::XMLWriter(Print* stream, Instrument_t inst)
{
#endif
instrument = inst;
_stream = stream;
reset();
}
void XMLWriter::reset()
{
tx_crc = reset_crc;
clearTm();
}
// --------------------------------------------------------
// Telemetry state fields functions
// --------------------------------------------------------
void XMLWriter::setStateFlags(uint8_t num, String flag)
{
if (num == 1) {
StateFlag1 = flag;
} else if (num == 2) {
StateFlag2 = flag;
} else if (num == 3) {
StateFlag3 = flag;
}
}
void XMLWriter::setStateFlagValue(uint8_t num, StateFlag_t stat)
{
if (num == 1) {
flag1 = stat;
} else if (num == 2) {
flag2 = stat;
} else if (num == 3) {
flag3 = stat;
}
}
void XMLWriter::setStateDetails(uint8_t num, String details)
{
if (num == 1) {
details1 = details;
} else if (num == 2) {
details2 = details;
} else if (num == 3) {
details3 = details;
}
}
// --------------------------------------------------------
// Tag functions
// --------------------------------------------------------
void XMLWriter::tagOpen(const char* tag)
{
writeAndUpdateCRC('<');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC('\n');
}
void XMLWriter::tagClose(const char* tag)
{
writeAndUpdateCRC('<');
writeAndUpdateCRC('/');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC('\n');
}
// --------------------------------------------------------
// Overloaded node write functions
// --------------------------------------------------------
void XMLWriter::writeNode(const char* tag, const char* value)
{
writeAndUpdateCRC('\t');
writeAndUpdateCRC('<');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC(value);
writeAndUpdateCRC('<');
writeAndUpdateCRC('/');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC('\n');
}
void XMLWriter::writeNode(const char* tag, String value)
{
writeNode(tag, value.c_str());
}
void XMLWriter::writeNode(const char* tag, char* value, uint8_t length)
{
writeAndUpdateCRC('\t');
writeAndUpdateCRC('<');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
for (uint8_t i = 0; i < length; i++) {
if (value[i] != 0) {
writeAndUpdateCRC(value[i]);
} else {
break;
}
}
writeAndUpdateCRC('<');
writeAndUpdateCRC('/');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC('\n');
}
void XMLWriter::writeNode(const char* tag, uint8_t value)
{
writeAndUpdateCRC('\t');
writeAndUpdateCRC('<');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC(value);
writeAndUpdateCRC('<');
writeAndUpdateCRC('/');
writeAndUpdateCRC(tag);
writeAndUpdateCRC('>');
writeAndUpdateCRC('\n');
}
void XMLWriter::writeNode(String tag, String value)
{
writeNode(tag.c_str(), value.c_str());
}
void XMLWriter::writeNode(String tag, const char* value)
{
writeNode(tag.c_str(), value);
}
// --------------------------------------------------------
// CRC control functions
// --------------------------------------------------------
void XMLWriter::crcReset()
{
tx_crc = reset_crc;
}
uint16_t XMLWriter::crcValue()
{
return tx_crc;
}
void XMLWriter::writeCRC()
{
_stream->print("<CRC>");
_stream->print(tx_crc, DEC);
_stream->print("</CRC>\n");
#ifdef XMLDEBUG
_log->print("<CRC>");
_log->print(tx_crc, DEC);
_log->print("</CRC>\n");
#endif
crcReset();
}
// --------------------------------------------------------
// Send over serial and update the CRC
// --------------------------------------------------------
void XMLWriter::writeAndUpdateCRC(uint8_t* data, uint8_t length)
{
if (length == 0 || data == NULL) {
return;
}
uint16_t c;
uint8_t x;
for (c = 0; c < length; c++) {
x = data[c];
if ((x == 0) & (c != 0)) {
break;
}
writeAndUpdateCRC(x);
}
return;
}
void XMLWriter::writeAndUpdateCRC(const char* data)
{
uint16_t c;
uint8_t x;
for (c = 0; c < 100; c++) {
x = data[c];
if ((x == 0) & (c != 0)) {
break;
}
writeAndUpdateCRC(x);
}
return;
}
inline void XMLWriter::writeAndUpdateCRC(uint8_t data)
{
uint8_t msb = tx_crc >> 8;
uint8_t lsb = tx_crc & 255;
uint16_t c;
_stream->write(data);
#ifdef XMLDEBUG
// print regular characters if alphanumeric or newline or tab, else print in hex
if ((data >= 32 && data <= 126) || data == 10 || data == 9) {
_log->write(data);
} else {
_log->print(data, HEX);
}
#endif
c = data ^ msb;
c ^= (c >> 4);
msb = (lsb ^ (c >> 3) ^ (c << 4)) & 255;
lsb = (c ^ (c << 5)) & 255;
tx_crc = (msb << 8) + lsb;
return;
}
// --------------------------------------------------------
// Write specific fields
// --------------------------------------------------------
uint16_t XMLWriter::msgNode()
{
String buf = String(messCount);
writeNode("Msg", buf.c_str());
messCount++;
if (messCount == 65534) {
messCount = 1;
}
return messCount;
}
void XMLWriter::instNode()
{
writeNode("Inst", inst_ids[instrument]);
}
// --------------------------------------------------------
// Send specific Zephyr messages
// --------------------------------------------------------
void XMLWriter::IMR()
{
tagOpen("IMR");
msgNode();
instNode();
writeNode("SWDate", swDate);
writeNode("SWVersion", swVer);
writeNode("ZProtocolVersion", Zproto);
tagClose("IMR");
writeCRC();
}
void XMLWriter::S()
{
tagOpen("S");
msgNode();
instNode();
tagClose("S");
writeCRC();
}
void XMLWriter::RA()
{
if (RACHUTS != instrument) { //Writer does not have inst enum
#ifdef XMLDEBUG
_log->print("Invalid devId: ");
_log->println(inst_ids[instrument]);
#endif
return;
}
tagOpen("RA");
msgNode();
writeNode("Inst", "RACHUTS");
tagClose("RA");
writeCRC();
}
void XMLWriter::IMAck(bool ackval)
{
tagOpen("IMAck");
msgNode();
instNode();
if (ackval) {
writeNode("Ack", "ACK");
} else {
writeNode("Ack", "NACK");
}
tagClose("IMAck");
writeCRC();
}
void XMLWriter::TCAck(bool ackval)
{
tagOpen("TCAck");
msgNode();
instNode();
if (ackval) {
writeNode("Ack", "ACK");
} else {
writeNode("Ack", "NACK");
}
tagClose("TCAck");
writeCRC();
}
// --------------------------------------------------------
// Send telemetry messages
// --------------------------------------------------------
void XMLWriter::TM()
{
tagOpen("TM");
msgNode();
instNode();
sendTMBody();
String buf = String(num_tm_elements);
writeNode("Length", buf.c_str());
tagClose("TM");
#ifdef XMLDEBUG
_log->print("Number of items in telemetry buffer: ");
_log->println(num_tm_elements);
#endif
writeCRC();
sendBin();
}
void XMLWriter::TM_String(StateFlag_t state_flag, const char * message)
{
tagOpen("TM");
msgNode();
instNode();
// write the state flag
if (state_flag == FINE) {
writeNode("StateFlag1", "FINE");
} else if (state_flag == WARN) {
writeNode("StateFlag1", "WARN");
} else if (state_flag == CRIT) {
writeNode("StateFlag1", "CRIT");
} else {
writeNode("StateFlag1", "UNKN");
}
// write the actual message of up to 100 chars
writeNode("StateMess1", message);
writeNode("Length", "0"); // no binary
tagClose("TM");
writeCRC();
sendEmptyBin(); // expected, even if empty
}
void XMLWriter::sendBin()
{
// Calling function does proper input check
crcReset();
_stream->print("START");
#ifdef XMLDEBUG
_log->print("START");
#endif
for (uint16_t i = 0; i < num_tm_elements; i++) {
writeAndUpdateCRC(tm_buffer[i]);
}
uint16_t binCrc = tx_crc;
uint8_t send;
crcReset();
send = binCrc >> 8;
_stream->write((byte)send);
send = (binCrc & (0x00FF));
_stream->write((byte)send);
_stream->print("END");
#ifdef XMLDEBUG
_log->print(binCrc, HEX);
_log->print("END\n");
#endif
tm_buff_sent = true;
return;
}
void XMLWriter::sendEmptyBin()
{
uint16_t binCrc = tx_crc;
uint8_t send;
// Calling function does proper input check
crcReset();
_stream->print("START");
#ifdef XMLDEBUG
_log->print("START");
#endif
send = binCrc >> 8;
_stream->write(send);
send = (binCrc & (0x00FF));
_stream->write(send);
//end
_stream->print("END");
#ifdef XMLDEBUG
_log->print(binCrc, HEX);
_log->print("END\n");
#endif
}
void XMLWriter::sendTMBody()
{
switch (flag1) {
case (FINE):
writeNode(StateFlag1, "FINE");
break;
case (CRIT):
writeNode(StateFlag1, "CRIT");
break;
case (WARN):
writeNode(StateFlag1, "WARN");
break;
case (UNKN):
writeNode(StateFlag1, "UNKN");
break;
default:
writeNode("StateMess1", "UNKN");
}
if (details1.length() != 0) {
writeNode("StateMess1", details1);
}
switch (flag2) {
case (FINE):
writeNode(StateFlag2, "FINE");
break;
case (CRIT):
writeNode(StateFlag2, "CRIT");
break;
case (WARN):
writeNode(StateFlag2, "WARN");
break;
case (UNKN):
writeNode(StateFlag2, "UNKN");
break;
case (NOMESS):
break;
default:
writeNode(StateFlag2, "UNKN");
}
if (details2.length() != 0) {
writeNode("StateMess2", details2);
}
switch (flag3) {
case (FINE):
writeNode(StateFlag3, "FINE");
break;
case (CRIT):
writeNode(StateFlag3, "CRIT");
break;
case (WARN):
writeNode(StateFlag3, "WARN");
break;
case (UNKN):
writeNode(StateFlag3, "UNKN");
break;
case (NOMESS):
break;
default:
writeNode(StateFlag3, "UNKN");
}
if (details3.length() != 0) {
writeNode("StateMess3", details3);
}
}
// --------------------------------------------------------
// Telemetry buffer interface functions
// --------------------------------------------------------
bool XMLWriter::addTm(uint8_t inChar)
{
return addTMByte(inChar);
}
bool XMLWriter::addTm(uint16_t inWord)
{
uint8_t outChar;
outChar = inWord >> 8;
if (!addTMByte(outChar)) return false;
outChar = (inWord & 0xFF);
return addTMByte(outChar);
}
bool XMLWriter::addTm(uint32_t inDouble)
{
uint8_t outChar;
outChar = inDouble >> 24;
if (!addTMByte(outChar)) return false;
outChar = ((inDouble >> 16) & 0x000000FF);
if (!addTMByte(outChar)) return false;
outChar = ((inDouble >> 8) & 0x000000FF);
if (!addTMByte(outChar)) return false;
outChar = ((inDouble) & 0x000000FF);
return addTMByte(outChar);
}
bool XMLWriter::addTm(String inStr)
{
bool err = false;
for (uint8_t i = 0; i < inStr.length(); i++) {
err = addTm((uint8_t)inStr.charAt(i));
#ifdef TM_DEBUG
_stream->print((uint8_t)inStr.charAt(i));
#endif
if (err)
break;
}
return err;
}
bool XMLWriter::addTm(const uint8_t * buffer, uint16_t size)
{
if (NULL == buffer) return false;
for (uint16_t i = 0; i < size; i++) {
if (!addTMByte(buffer[i])) return false;
}
return true;
}
bool XMLWriter::addTm(const uint16_t * buffer, uint16_t size)
{
uint8_t outChar;
if (NULL == buffer) return false;
for (uint16_t i = 0; i < size; i++) {
outChar = buffer[i] >> 8;
if (!addTMByte(outChar)) return false;
outChar = (buffer[i] & 0xFF);
if (!addTMByte(outChar)) return false;
}
return true;
}
void XMLWriter::clearTm()
{
num_tm_elements = 0;
tm_buff_sent = false;
}
uint16_t XMLWriter::getTmLen()
{
return num_tm_elements;
}
uint16_t XMLWriter::getTmBuffer(uint8_t ** buffer)
{
*buffer = tm_buffer;
return num_tm_elements;
}
inline bool XMLWriter::addTMByte(uint8_t in_byte)
{
// if we're adding to the buffer after it's been sent, reset it
if (tm_buff_sent) {
tm_buff_sent = false;
num_tm_elements = 0;
}
if (TMBUF_MAXSIZE == num_tm_elements) return false;
tm_buffer[num_tm_elements++] = in_byte;
return true;
}
// END OF FILE