3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
12
13#include <unity.h>
14
15#include <stdint.h>
16#include <stdio.h>
17#include <string.h>
18
19#include "teleinfo.h" // Real header - pure C++
20
21void setUp(void) {}
22void tearDown(void) {}
23
25struct Values
26{
27 int16_t power;
28 int16_t phasePower[3];
29 uint16_t phaseVoltage_x100[3];
30 int16_t relayAverage;
31 bool relayOn[4];
32 int16_t loadOnPercent[4];
33 int16_t temperature_x100[4];
35 uint16_t absenceCount;
36 bool offPeak;
37 uint16_t sampleSets;
39};
40
42
44template< uint8_t PHASES, uint8_t RELAYS, uint8_t LOADS, uint8_t TEMPS, bool DUAL >
45struct TestSource
46{
47 static constexpr uint8_t PHASE_COUNT{ PHASES };
48 static constexpr bool RELAY_DIVERSION{ RELAYS != 0 };
49 static constexpr uint8_t RELAY_COUNT{ RELAYS };
50 static constexpr uint8_t LOAD_COUNT{ LOADS };
51 static constexpr uint8_t TEMP_COUNT{ TEMPS };
52 static constexpr bool DUAL_TARIFF{ DUAL };
53
54 static int16_t power()
55 {
56 return values.power;
57 }
58 static int16_t phasePower(uint8_t idx)
59 {
60 return values.phasePower[idx];
61 }
62 static uint16_t phaseVoltage(uint8_t idx)
63 {
64 return values.phaseVoltage_x100[idx];
65 }
66 static int16_t relayAverage()
67 {
68 return values.relayAverage;
69 }
70 static bool relayOn(uint8_t idx)
71 {
72 return values.relayOn[idx];
73 }
74 static int16_t loadOnPercent(uint8_t idx)
75 {
76 return values.loadOnPercent[idx];
77 }
78 static bool temperatureValid(uint8_t idx)
79 {
80 return values.temperatureValid[idx];
81 }
82 static int16_t temperature(uint8_t idx)
83 {
84 return values.temperature_x100[idx];
85 }
86 static uint16_t absenceCount()
87 {
88 return values.absenceCount;
89 }
90 static bool offPeak()
91 {
92 return values.offPeak;
93 }
94 static uint16_t sampleSets()
95 {
96 return values.sampleSets;
97 }
98 static uint8_t lowestSampleSetsPerCycle()
99 {
100 return values.lowestSampleSetsPerCycle;
101 }
102};
103
110{
111private:
112 static const char STX{ 0x02 };
113 static const char ETX{ 0x03 };
114 static const char LF{ 0x0A };
115 static const char CR{ 0x0D };
116 static const char TAB{ 0x09 };
117
118 char buffer[512]{};
119 size_t bufferPos{ 0 };
120
121 [[nodiscard]] uint8_t calculateChecksum(size_t startPos, size_t endPos) const
122 {
123 uint8_t sum{ 0 };
124 for (const char* ptr = buffer + startPos; ptr != buffer + endPos; ++ptr)
125 {
126 sum += *ptr;
127 }
128
129 return (sum & 0x3F) + 0x20;
130 }
131
132 void writeTag(const char* tag, uint8_t index)
133 {
134 auto* ptr{ tag };
135 while (*ptr) buffer[bufferPos++] = *ptr++;
136
137 if (index != 0)
138 {
139 buffer[bufferPos++] = static_cast< char >('0' + index);
140 }
141
142 buffer[bufferPos++] = TAB;
143 }
144
145 void writeLine(const char* tag, uint8_t index, uint16_t magnitude, bool negative)
146 {
147 buffer[bufferPos++] = LF;
148
149 const auto startPos{ bufferPos };
150
151 writeTag(tag, index);
152 if (negative)
153 {
154 buffer[bufferPos++] = '-';
155 }
156 char* str{ buffer + bufferPos };
157 snprintf(str, 12, "%u", static_cast< unsigned >(magnitude)); // utoa()
158 bufferPos += strlen(str);
159 buffer[bufferPos++] = TAB;
160
161 const auto crc{ calculateChecksum(startPos, bufferPos) };
162 buffer[bufferPos++] = crc;
163
164 buffer[bufferPos++] = CR;
165 }
166
167public:
169 {
170 bufferPos = 0;
171 buffer[bufferPos++] = STX;
172 }
173
174 void send(const char* tag, int16_t value, uint8_t index = 0)
175 {
176 const auto magnitude{ value < 0 ? static_cast< uint16_t >(0U - static_cast< uint16_t >(value)) : static_cast< uint16_t >(value) };
177 writeLine(tag, index, magnitude, value < 0);
178 }
179
180 void sendUnsigned(const char* tag, uint16_t value, uint8_t index = 0)
181 {
182 writeLine(tag, index, value, false);
183 }
184
185 void endFrame()
186 {
187 buffer[bufferPos++] = ETX;
188 }
189
190 [[nodiscard]] const char* data() const
191 {
192 return buffer;
193 }
194 [[nodiscard]] size_t size() const
195 {
196 return bufferPos;
197 }
198};
199
201template< typename Source >
202static void referenceFrame(Reference& teleInfo)
203{
204 uint8_t idx{ 0 };
205
206 teleInfo.startFrame();
207
208 teleInfo.send("P", Source::power());
209
210 if constexpr (Source::PHASE_COUNT > 1)
211 {
212 idx = Source::PHASE_COUNT;
213 do
214 {
215 --idx;
216 teleInfo.send("P", Source::phasePower(idx), idx + 1);
217 teleInfo.sendUnsigned("V", Source::phaseVoltage(idx), idx + 1); // unsigned, was send() (negative above 327.67 V)
218 } while (idx);
219 }
220
221 if constexpr (Source::RELAY_DIVERSION)
222 {
223 teleInfo.send("R", Source::relayAverage());
224
225 idx = Source::RELAY_COUNT;
226 do
227 {
228 --idx;
229 teleInfo.send("R", Source::relayOn(idx), idx + 1);
230 } while (idx);
231 }
232
233 idx = Source::LOAD_COUNT;
234 do
235 {
236 --idx;
237 teleInfo.send("D", Source::loadOnPercent(idx), idx + 1);
238 } while (idx);
239
240 for (uint8_t idx = 0; idx < Source::TEMP_COUNT; ++idx)
241 {
242 if (!Source::temperatureValid(idx))
243 {
244 continue;
245 }
246 teleInfo.send("T", Source::temperature(idx), idx + 1);
247 }
248
249 teleInfo.sendUnsigned("N", Source::absenceCount()); // unsigned, was send() (negative beyond 32767 s)
250
251 if constexpr (Source::DUAL_TARIFF)
252 {
253 teleInfo.send("TA", static_cast< int16_t >(Source::offPeak() ? 1 : 0));
254 }
255
256 teleInfo.sendUnsigned("S", Source::sampleSets());
257 teleInfo.send("S_MC", Source::lowestSampleSetsPerCycle());
258
259 teleInfo.endFrame();
260}
261
264{
265 char text[512]{};
266 size_t length{ 0 };
267 uint8_t steps{ 0 };
268 uint8_t longestStep{ 0 };
269};
270
271template< typename Source >
273{
274 Streamed frame;
275 bool more{ true };
276 while (more)
277 {
278 char step[TeleInfo::MAX_STEP_LENGTH];
279 uint8_t length{ 0 };
280 more = TeleInfo::writeFrameStep< Source >(frame.steps++, step, length);
281 TEST_ASSERT_LESS_OR_EQUAL_UINT8(TeleInfo::MAX_STEP_LENGTH, length);
282 memcpy(frame.text + frame.length, step, length);
283 frame.length += length;
284 if (length > frame.longestStep)
285 {
286 frame.longestStep = length;
287 }
288 TEST_ASSERT_LESS_THAN_UINT8(100, frame.steps); // never endless
289 }
290 return frame;
291}
292
293template< typename Source >
294static void assertSameAsReference(const char* message)
295{
298 const Streamed streamed{ streamFrame< Source >() };
299
300 TEST_ASSERT_EQUAL_UINT32_MESSAGE(reference.size(), streamed.length, message);
301 TEST_ASSERT_EQUAL_MEMORY_MESSAGE(reference.data(), streamed.text, reference.size(), message);
302}
303
305static void typicalValues()
306{
307 values = Values{ -1234, { 399, 399, -2032 }, { 23045, 23110, 22987 }, -567, { true, false, true, false }, { 100, 37, 0, 5 }, { 2137, -12700, 6500, -5 }, { true, false, true, true }, 731, true, 250, 44 };
308}
309
311static void extremeValues()
312{
313 values = Values{ -32768, { 32767, -32768, 0 }, { 65535, 0, 32768 }, 32767, { false, true, false, true }, { 0, 100, 1, 99 }, { -32768, 32767, 0, 12500 }, { true, true, true, true }, 65535, false, 65535, 255 };
314}
315
316using SinglePhase = TestSource< 1, 0, 1, 0, false >; // NO_OF_PHASES 1: no per-phase lines
323
324/* the streamed frame is the buffered one, byte for byte, for every feature set */
326{
328 assertSameAsReference< SinglePhase >("single phase, typical");
329 assertSameAsReference< Minimal >("minimal, typical");
330 assertSameAsReference< Default >("default, typical");
331 assertSameAsReference< Relays >("relays, typical");
332 assertSameAsReference< Temperatures >("temperatures, typical");
333 assertSameAsReference< DualTariff >("dual tariff, typical");
334 assertSameAsReference< Everything >("every feature, typical");
335
337 assertSameAsReference< SinglePhase >("single phase, extremes");
338 assertSameAsReference< Minimal >("minimal, extremes");
339 assertSameAsReference< Default >("default, extremes");
340 assertSameAsReference< Relays >("relays, extremes");
341 assertSameAsReference< Temperatures >("temperatures, extremes");
342 assertSameAsReference< DualTariff >("dual tariff, extremes");
343 assertSameAsReference< Everything >("every feature, extremes");
344
345 // every temperature invalid: the temperature steps write nothing
346 values.temperatureValid[0] = values.temperatureValid[1] = values.temperatureValid[2] = values.temperatureValid[3] = false;
347 assertSameAsReference< Everything >("every feature, no valid temperature");
348}
349
350/* a step fits the serial output's (SerialOutput::MAX_STEP_LENGTH, 32): one line, STX or ETX */
352{
354 // 32: SerialOutput::MAX_STEP_LENGTH, out of reach here (serial_output.h needs Arduino.h);
355 // utils.h checks it with a static_assert, the embedded test against the constant
356 TEST_ASSERT_LESS_OR_EQUAL_UINT8(32, TeleInfo::MAX_STEP_LENGTH);
357 TEST_ASSERT_LESS_OR_EQUAL_UINT8(TeleInfo::MAX_STEP_LENGTH, streamFrame< Everything >().longestStep);
358}
359
360/* one step per line: invalid temperatures included, they write nothing */
362{
364 // P D1 N S S_MC
365 TEST_ASSERT_EQUAL_UINT8(5, streamFrame< SinglePhase >().steps);
366 // P P3 V3 P2 V2 P1 V1 D1 N S S_MC
367 TEST_ASSERT_EQUAL_UINT8(11, streamFrame< Minimal >().steps);
368 // P (P3 V3 P2 V2 P1 V1) R R2 R1 D4..D1 T1..T4 N TA S S_MC
369 TEST_ASSERT_EQUAL_UINT8(1 + 6 + 3 + 4 + 4 + 1 + 1 + 2, streamFrame< Everything >().steps);
370}
371
372/* a frame written independently of the code, checksums computed by hand */
374{
375 values = Values{};
376 values.power = -25;
377 values.phasePower[0] = 399;
378 values.phasePower[1] = 399;
379 values.phasePower[2] = -823;
380 values.phaseVoltage_x100[0] = 23012;
381 values.phaseVoltage_x100[1] = 23100;
382 values.phaseVoltage_x100[2] = 22950;
383 values.loadOnPercent[0] = 40;
384 values.absenceCount = 7;
385 values.sampleSets = 250;
386 values.lowestSampleSetsPerCycle = 44;
387
388 // checksum: the sum of the bytes from the tag to the second TAB, & 0x3F, + 0x20
389 // P\t-25\t 0x0F6 -> 0x36 + 0x20 = 'V'
390 // P3\t-823\t 0x15F -> 0x1F + 0x20 = '?'
391 // V3\t22950\t 0x19D -> 0x1D + 0x20 = '='
392 // P2\t399\t 0x139 -> 0x39 + 0x20 = 'Y'
393 // V2\t23100\t 0x190 -> 0x10 + 0x20 = '0'
394 // P1\t399\t 0x138 -> 0x38 + 0x20 = 'X'
395 // V1\t23012\t 0x191 -> 0x11 + 0x20 = '1'
396 // D1\t40\t 0x0EB -> 0x2B + 0x20 = 'K'
397 // N\t7\t 0x097 -> 0x17 + 0x20 = '7'
398 // S\t250\t 0x0FC -> 0x3C + 0x20 = '\'
399 // S_MC\t44\t 0x1BC -> 0x3C + 0x20 = '\'
400 static const char golden[]{ "\x02"
401 "\nP\t-25\tV\r"
402 "\nP3\t-823\t?\r"
403 "\nV3\t22950\t=\r"
404 "\nP2\t399\tY\r"
405 "\nV2\t23100\t0\r"
406 "\nP1\t399\tX\r"
407 "\nV1\t23012\t1\r"
408 "\nD1\t40\tK\r"
409 "\nN\t7\t7\r"
410 "\nS\t250\t\\\r"
411 "\nS_MC\t44\t\\\r"
412 "\x03" };
413
414 const Streamed streamed{ streamFrame< Minimal >() };
415 TEST_ASSERT_EQUAL_UINT32(sizeof golden - 1, streamed.length);
416 TEST_ASSERT_EQUAL_MEMORY(golden, streamed.text, sizeof golden - 1);
417}
418
420struct Line
421{
422 char tag[8];
423 char value[16];
424};
425
431static uint8_t parseFrame(const Streamed& frame, Line (&lines)[32])
432{
433 TEST_ASSERT_GREATER_THAN_UINT32(2, frame.length);
434 TEST_ASSERT_EQUAL_CHAR(0x02, frame.text[0]);
435 TEST_ASSERT_EQUAL_CHAR(0x03, frame.text[frame.length - 1]);
436
437 uint8_t count{ 0 };
438 size_t pos{ 1 };
439 while (pos < frame.length - 1)
440 {
441 TEST_ASSERT_EQUAL_CHAR('\n', frame.text[pos++]);
442 const size_t start{ pos };
443 const char* const tab1{ static_cast< const char* >(memchr(frame.text + pos, '\t', frame.length - pos)) };
444 TEST_ASSERT_NOT_NULL(tab1);
445 const char* const tab2{ static_cast< const char* >(memchr(tab1 + 1, '\t', frame.length - (tab1 + 1 - frame.text))) };
446 TEST_ASSERT_NOT_NULL(tab2);
447
448 uint8_t sum{ 0 };
449 for (const char* p = frame.text + start; p != tab2 + 1; ++p)
450 {
451 sum += static_cast< uint8_t >(*p);
452 }
453 TEST_ASSERT_EQUAL_CHAR((sum & 0x3F) + 0x20, tab2[1]);
454 TEST_ASSERT_EQUAL_CHAR('\r', tab2[2]);
455
456 Line& line{ lines[count++] };
457 memset(&line, 0, sizeof line);
458 memcpy(line.tag, frame.text + start, tab1 - (frame.text + start));
459 memcpy(line.value, tab1 + 1, tab2 - (tab1 + 1));
460 pos = (tab2 + 3) - frame.text;
461 }
462 return count;
463}
464
466static uint8_t countTag(const Line (&lines)[32], uint8_t count, const char* tag)
467{
468 uint8_t found{ 0 };
469 for (uint8_t i = 0; i != count; ++i)
470 {
471 found += strcmp(lines[i].tag, tag) == 0 ? 1 : 0;
472 }
473 return found;
474}
475
477static const char* valueOf(const Line (&lines)[32], uint8_t count, const char* tag)
478{
479 for (uint8_t i = 0; i != count; ++i)
480 {
481 if (strcmp(lines[i].tag, tag) == 0)
482 {
483 return lines[i].value;
484 }
485 }
486 return "";
487}
488
489/* every tag of a frame with every feature, once, in order, with its value */
491{
493 Line lines[32];
494 const uint8_t count{ parseFrame(streamFrame< Everything >(), lines) };
495
496 static const char* const order[]{ "P", "P3", "V3", "P2", "V2", "P1", "V1", "R", "R2", "R1", "D4", "D3", "D2", "D1", "T1", "T3", "T4", "N", "TA", "S", "S_MC" };
497 TEST_ASSERT_EQUAL_UINT8(sizeof order / sizeof order[0], count);
498 for (uint8_t i = 0; i != count; ++i)
499 {
500 TEST_ASSERT_EQUAL_STRING(order[i], lines[i].tag);
501 }
502
503 TEST_ASSERT_EQUAL_STRING("-1234", valueOf(lines, count, "P"));
504 TEST_ASSERT_EQUAL_STRING("399", valueOf(lines, count, "P1"));
505 TEST_ASSERT_EQUAL_STRING("399", valueOf(lines, count, "P2"));
506 TEST_ASSERT_EQUAL_STRING("-2032", valueOf(lines, count, "P3"));
507 TEST_ASSERT_EQUAL_STRING("23045", valueOf(lines, count, "V1"));
508 TEST_ASSERT_EQUAL_STRING("23110", valueOf(lines, count, "V2"));
509 TEST_ASSERT_EQUAL_STRING("22987", valueOf(lines, count, "V3"));
510 TEST_ASSERT_EQUAL_STRING("-567", valueOf(lines, count, "R"));
511 TEST_ASSERT_EQUAL_STRING("1", valueOf(lines, count, "R1")); // Rn is relayOn(n - 1)
512 TEST_ASSERT_EQUAL_STRING("0", valueOf(lines, count, "R2"));
513 TEST_ASSERT_EQUAL_STRING("100", valueOf(lines, count, "D1"));
514 TEST_ASSERT_EQUAL_STRING("37", valueOf(lines, count, "D2"));
515 TEST_ASSERT_EQUAL_STRING("0", valueOf(lines, count, "D3"));
516 TEST_ASSERT_EQUAL_STRING("5", valueOf(lines, count, "D4"));
517 TEST_ASSERT_EQUAL_STRING("2137", valueOf(lines, count, "T1"));
518 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "T2")); // invalid: no line
519 TEST_ASSERT_EQUAL_STRING("6500", valueOf(lines, count, "T3"));
520 TEST_ASSERT_EQUAL_STRING("-5", valueOf(lines, count, "T4"));
521 TEST_ASSERT_EQUAL_STRING("731", valueOf(lines, count, "N"));
522 TEST_ASSERT_EQUAL_STRING("1", valueOf(lines, count, "TA"));
523 TEST_ASSERT_EQUAL_STRING("250", valueOf(lines, count, "S"));
524 TEST_ASSERT_EQUAL_STRING("44", valueOf(lines, count, "S_MC"));
525}
526
527/* optional features on and off: their lines are there, or are not */
529{
531 Line lines[32];
532
533 uint8_t count{ parseFrame(streamFrame< Minimal >(), lines) };
534 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "R"));
535 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "R1"));
536 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "T1"));
537 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "TA"));
538 TEST_ASSERT_EQUAL_UINT8(1, countTag(lines, count, "P3"));
539
540 count = parseFrame(streamFrame< Relays >(), lines);
541 TEST_ASSERT_EQUAL_UINT8(1, countTag(lines, count, "R"));
542 TEST_ASSERT_EQUAL_UINT8(1, countTag(lines, count, "R1"));
543 TEST_ASSERT_EQUAL_UINT8(1, countTag(lines, count, "R2"));
544 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "T1"));
545 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "TA"));
546
547 count = parseFrame(streamFrame< Temperatures >(), lines);
548 TEST_ASSERT_EQUAL_UINT8(1, countTag(lines, count, "T1"));
549 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "T2")); // temperature 2 is invalid
550 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "R"));
551
552 count = parseFrame(streamFrame< DualTariff >(), lines);
553 TEST_ASSERT_EQUAL_STRING("1", valueOf(lines, count, "TA"));
554 values.offPeak = false;
555 count = parseFrame(streamFrame< DualTariff >(), lines);
556 TEST_ASSERT_EQUAL_STRING("0", valueOf(lines, count, "TA"));
557
558 // one phase: the total only, no P1/V1
559 count = parseFrame(streamFrame< SinglePhase >(), lines);
560 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "P1"));
561 TEST_ASSERT_EQUAL_UINT8(0, countTag(lines, count, "V1"));
562 TEST_ASSERT_EQUAL_STRING("P", lines[0].tag);
563}
564
565/* N, the seconds without diverted energy, is unsigned: beyond 32767 s (9.1 h) it no longer
566 * reads negative */
568{
570 Line lines[32];
571
572 values.absenceCount = 40000;
573 uint8_t count{ parseFrame(streamFrame< Default >(), lines) };
574 TEST_ASSERT_EQUAL_STRING("40000", valueOf(lines, count, "N"));
575 assertSameAsReference< Everything >("every feature, N = 40000");
576
577 values.absenceCount = 65535;
578 count = parseFrame(streamFrame< Everything >(), lines);
579 TEST_ASSERT_EQUAL_STRING("65535", valueOf(lines, count, "N"));
580}
581
582/* V, the RMS voltage in hundredths of a volt, is unsigned: beyond 327.67 V (a datalog period that
583 * would hold more V^2 than samples) it no longer reads negative */
585{
587 Line lines[32];
588
589 values.phaseVoltage_x100[0] = 46000;
590 values.phaseVoltage_x100[1] = 32768;
591 values.phaseVoltage_x100[2] = 65535;
592 const uint8_t count{ parseFrame(streamFrame< Default >(), lines) };
593 TEST_ASSERT_EQUAL_STRING("46000", valueOf(lines, count, "V1")); // was -19536
594 TEST_ASSERT_EQUAL_STRING("32768", valueOf(lines, count, "V2")); // was -32768
595 TEST_ASSERT_EQUAL_STRING("65535", valueOf(lines, count, "V3")); // was -1
596 assertSameAsReference< Default >("V = 46000, 32768, 65535");
597 assertSameAsReference< Everything >("every feature, V = 46000, 32768, 65535");
598}
599
600/* the sample count above 32767 (long datalog periods) is unsigned too */
602{
604 Line lines[32];
605
606 values.sampleSets = 64000;
607 const uint8_t count{ parseFrame(streamFrame< Default >(), lines) };
608 TEST_ASSERT_EQUAL_STRING("64000", valueOf(lines, count, "S"));
609}
610
611/* a single line: LF, tag and index, TAB, value, TAB, checksum, CR */
612void test_line(void)
613{
614 char text[TeleInfo::MAX_STEP_LENGTH];
615
616 // T1\t-5500\t: 0x18E -> 0x0E + 0x20 = '.'
617 const char* end{ TeleInfo::line(text, "T", -5500, 1) };
618 TEST_ASSERT_EQUAL_UINT32(12, end - text);
619 TEST_ASSERT_EQUAL_MEMORY("\nT1\t-5500\t.\r", text, 12);
620
621 // V1\t65535: unsigned, not -1
622 end = TeleInfo::lineUnsigned(text, "V", 65535, 1);
623 TEST_ASSERT_EQUAL_UINT32(TeleInfo::lineSize(2, 5), end - text);
624 TEST_ASSERT_EQUAL_MEMORY("\nV1\t65535\t", text, 10);
625
626 // the longest: a 4-character tag and a signed 5-digit value
627 end = TeleInfo::line(text, "S_MC", -32768);
628 TEST_ASSERT_EQUAL_UINT32(TeleInfo::lineSize(4, 6), end - text);
629 TEST_ASSERT_EQUAL_UINT8(TeleInfo::MAX_STEP_LENGTH - 1, TeleInfo::lineSize(4, 6));
630
631 // 0 and -32768
632 end = TeleInfo::line(text, "P", 0);
633 TEST_ASSERT_EQUAL_MEMORY("\nP\t0\t", text, 5);
634 end = TeleInfo::line(text, "P", -32768);
635 TEST_ASSERT_EQUAL_MEMORY("\nP\t-32768\t", text, 10);
636}
637
638int main(int /* argc */, char** /* argv */)
639{
640 UNITY_BEGIN();
641
642 RUN_TEST(test_line);
643 RUN_TEST(test_golden_frame);
646 RUN_TEST(test_one_line_per_step);
647 RUN_TEST(test_every_tag);
648 RUN_TEST(test_optional_features);
650 RUN_TEST(test_voltage_unsigned);
652
653 return UNITY_END();
654}
The buffered TeleInfo class of the firmware, as it was (utoa() aside): the reference of the frame....
void send(const char *tag, int16_t value, uint8_t index=0)
void sendUnsigned(const char *tag, uint16_t value, uint8_t index=0)
void writeLine(const char *tag, uint8_t index, uint16_t magnitude, bool negative)
const char * data() const
size_t size() const
size_t bufferPos
static const char STX
void writeTag(const char *tag, uint8_t index)
static const char ETX
void endFrame()
void startFrame()
char buffer[512]
static const char LF
static const char CR
uint8_t calculateChecksum(size_t startPos, size_t endPos) const
static const char TAB
constexpr bool RELAY_DIVERSION
Definition config.h:63
constexpr bool DUAL_TARIFF
Definition config.h:64
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
static void streamFrame(FramePrint &out)
The windows of the scheduled boosts, in milliseconds from the start of the off-peak period.
char * lineUnsigned(char *dst, const char *tag, uint16_t value, uint8_t index=0)
Writes the line of an unsigned value (above 32767, line() would wrap).
Definition teleinfo.h:157
bool writeFrameStep(const uint8_t step, char *const text, uint8_t &length)
Writes a step of the frame: one line, STX before the first one, ETX after the last.
Definition teleinfo.h:304
constexpr uint8_t MAX_STEP_LENGTH
The most a step writes: STX or ETX, and the longest line, a 4-character tag ("S_MC") with a signed 16...
Definition teleinfo.h:60
constexpr uint8_t lineSize(uint8_t tagLength, uint8_t valueLength)
The size of a line: LF, tag, TAB, value, TAB, checksum and CR.
Definition teleinfo.h:51
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.
Definition teleinfo.h:142
int main(void)
static double reference(int32_t averagePower, float cal)
The float arithmetic being replaced, in bucket units.
Definition test_main.cpp:28
void test_optional_features(void)
void test_line(void)
void test_sample_count_unsigned(void)
void test_step_fits_serial_output(void)
TestSource< 3, 0, 3, 0, false > Default
static void typicalValues()
A datalog with every value set, temperature 2 invalid.
TestSource< 3, 2, 4, 4, true > Everything
static uint8_t parseFrame(const Streamed &frame, Line(&lines)[32])
Splits a frame into its lines, as a receiver does, checking STX, ETX and every line's format and chec...
TestSource< 3, 0, 1, 0, false > Minimal
static uint8_t countTag(const Line(&lines)[32], uint8_t count, const char *tag)
How many lines have exactly this tag.
TestSource< 3, 0, 3, 2, false > Temperatures
void test_absence_count_unsigned(void)
static Values values
Definition test_main.cpp:41
static void assertSameAsReference(const char *message)
void test_every_tag(void)
void test_golden_frame(void)
static const char * valueOf(const Line(&lines)[32], uint8_t count, const char *tag)
The value of the line with exactly this tag.
TestSource< 1, 0, 1, 0, false > SinglePhase
static void extremeValues()
The extremes of every value.
void test_voltage_unsigned(void)
TestSource< 3, 2, 3, 0, false > Relays
void test_one_line_per_step(void)
static void referenceFrame(Reference &teleInfo)
The frame of sendTelemetryData() as it was, N and V sent unsigned.
static Streamed streamFrame()
void test_streamed_frame_equals_buffered(void)
A line of a frame: its tag (with the index) and its value.
char tag[8]
char value[16]
A frame as the serial output writes it: every step, one after the other.
uint8_t longestStep
char text[512]
size_t length
uint8_t steps
A data source of writeFrameStep(): 2 loads, 1 relay, 2 temperature sensors (the 2nd invalid).
Definition test_main.cpp:95
static int16_t temperature(uint8_t idx)
Definition test_main.cpp:82
static uint8_t lowestSampleSetsPerCycle()
Definition test_main.cpp:98
static bool relayOn(uint8_t idx)
Definition test_main.cpp:70
static constexpr uint8_t TEMP_COUNT
static int16_t phasePower(uint8_t idx)
Definition test_main.cpp:58
static bool offPeak()
Definition test_main.cpp:90
static constexpr uint8_t PHASE_COUNT
Definition test_main.cpp:96
static uint16_t sampleSets()
Definition test_main.cpp:94
static int16_t relayAverage()
Definition test_main.cpp:66
static bool temperatureValid(uint8_t idx)
Definition test_main.cpp:78
static uint16_t absenceCount()
Definition test_main.cpp:86
static int16_t loadOnPercent(uint8_t idx)
Definition test_main.cpp:74
static int16_t power()
Definition test_main.cpp:54
static constexpr uint8_t LOAD_COUNT
Definition test_main.cpp:99
static constexpr uint8_t RELAY_COUNT
Definition test_main.cpp:98
static uint16_t phaseVoltage(uint8_t idx)
Definition test_main.cpp:62
The values of a datalog, shared by every test source.
Definition test_main.cpp:26
uint16_t absenceCount
Definition test_main.cpp:35
bool relayOn[4]
Definition test_main.cpp:31
int16_t relayAverage
Definition test_main.cpp:30
int16_t temperature_x100[4]
Definition test_main.cpp:33
uint16_t sampleSets
Definition test_main.cpp:37
bool temperatureValid[4]
Definition test_main.cpp:34
int16_t loadOnPercent[4]
Definition test_main.cpp:32
uint8_t lowestSampleSetsPerCycle
Definition test_main.cpp:38
int16_t phasePower[3]
Definition test_main.cpp:28
int16_t power
Definition test_main.cpp:27
bool offPeak
Definition test_main.cpp:36
uint16_t phaseVoltage_x100[3]
Definition test_main.cpp:29
The telemetry frame of the IoT output, written a line at a time.