ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
test_ascii_serial_com.c
1#include "asc_exception.h"
2#include "ascii_serial_com.h"
3#include "externals/unity.h"
4#include <inttypes.h>
5#include <stdio.h>
6
7CEXCEPTION_T e1;
8CEXCEPTION_T e2;
9size_t iString;
10
11void setUp(void) {
12 // set stuff up here
13}
14
15void tearDown(void) {
16 // clean stuff up here
17}
18
19void test_convert_uint8_to_hex(void) {
20 Try {
21 char stdiostr[4];
22 char outstr[3] = "\0\0\0";
23 for (size_t i = 0; i < 0x100; i++) {
24 char j = i;
25 convert_uint8_to_hex(j, outstr, true);
26 snprintf(stdiostr, 4, "%02hhX", j);
27 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
28 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 2);
29
30 convert_uint8_to_hex(j, outstr, false);
31 snprintf(stdiostr, 4, "%02hhx", j);
32 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
33 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 2);
34 }
35 }
36 Catch(e1) {
37 printf("Uncaught exception: %u\n", e1);
38 TEST_FAIL_MESSAGE("Uncaught exception!");
39 }
40}
41
42void test_convert_uint16_to_hex(void) {
43 Try {
44 char stdiostr[6];
45 char outstr[5] = "\0\0\0\0\0";
46 for (size_t i = 0; i < 0x10000; i++) {
47 uint16_t j = i;
48 convert_uint16_to_hex(j, outstr, true);
49 snprintf(stdiostr, 6, "%04" PRIX16, j);
50 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
51 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 4);
52
53 convert_uint16_to_hex(j, outstr, false);
54 snprintf(stdiostr, 6, "%04" PRIx16, j);
55 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
56 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 4);
57 }
58 }
59 Catch(e1) {
60 printf("Uncaught exception: %u\n", e1);
61 TEST_FAIL_MESSAGE("Uncaught exception!");
62 }
63}
64
65void test_convert_uint32_to_hex(void) {
66 Try {
67 char stdiostr[10];
68 char outstr[9];
69 for (size_t i = 0; i < 9; i++) {
70 outstr[i] = '\0';
71 }
72 for (uint64_t i = 0; i < 0x100000000; i += 0x100000) {
73 uint32_t j = i;
74 convert_uint32_to_hex(j, outstr, true);
75 snprintf(stdiostr, 10, "%08" PRIX32, j);
76 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
77 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
78
79 convert_uint32_to_hex(j, outstr, false);
80 snprintf(stdiostr, 10, "%08" PRIx32, j);
81 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
82 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
83 }
84 for (uint64_t i = 0; i < 0x1F000; i++) {
85 uint32_t j = i;
86 convert_uint32_to_hex(j, outstr, true);
87 snprintf(stdiostr, 10, "%08" PRIX32, j);
88 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
89 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
90
91 convert_uint32_to_hex(j, outstr, false);
92 snprintf(stdiostr, 10, "%08" PRIx32, j);
93 // printf("%zu: %s = %s\n",i,stdiostr,outstr);
94 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
95 }
96 }
97 Catch(e1) {
98 printf("Uncaught exception: %u\n", e1);
99 TEST_FAIL_MESSAGE("Uncaught exception!");
100 }
101}
102
103void test_convert_hex_to_uint8(void) {
104 Try {
105 char instr[5] = "\0\0\0\0\0";
106 for (size_t i = 0; i < 0x100; i++) {
107 snprintf(instr, 4, "%02" PRIX8, (uint8_t)i);
108 // printf("%s %02" PRIX8 " %02"PRIX8"\n",instr, (uint8_t) instr[1],
109 // (uint8_t) instr[0]);
110 TEST_ASSERT_EQUAL_UINT8(i, convert_hex_to_uint8(instr));
111 snprintf(instr, 4, "%02" PRIx8, (uint8_t)i);
112 // printf("%s %02" PRIX8 " %02"PRIX8"\n",instr, (uint8_t) instr[1],
113 // (uint8_t) instr[0]);
114 TEST_ASSERT_EQUAL_UINT8(i, convert_hex_to_uint8(instr));
115 }
116 }
117 Catch(e1) {
118 printf("Uncaught exception: %u\n", e1);
119 TEST_FAIL_MESSAGE("Uncaught exception!");
120 }
121}
122
123void test_convert_hex_to_uint16(void) {
124 Try {
125 char instr[6] = "\0\0\0\0\0";
126 for (size_t i = 0; i < 0x10000; i++) {
127 snprintf(instr, 6, "%04" PRIX16, (uint16_t)i);
128 TEST_ASSERT_EQUAL_UINT16(i, convert_hex_to_uint16(instr));
129 snprintf(instr, 6, "%04" PRIx16, (uint16_t)i);
130 TEST_ASSERT_EQUAL_UINT16(i, convert_hex_to_uint16(instr));
131 }
132 }
133 Catch(e1) {
134 printf("Uncaught exception: %u\n", e1);
135 TEST_FAIL_MESSAGE("Uncaught exception!");
136 }
137}
138
139void test_convert_hex_to_uint32(void) {
140 Try {
141 char instr[10] = "\0\0\0\0\0\0\0\0\0\0";
142 for (uint64_t i = 0; i < 0x100000000; i += 0x100000) {
143 snprintf(instr, 10, "%08" PRIX32, (uint32_t)i);
144 TEST_ASSERT_EQUAL_UINT32(i, convert_hex_to_uint32(instr));
145 snprintf(instr, 10, "%08" PRIx32, (uint32_t)i);
146 TEST_ASSERT_EQUAL_UINT32(i, convert_hex_to_uint32(instr));
147 }
148 for (uint64_t i = 0; i < 0x1F000; i++) {
149 snprintf(instr, 10, "%08" PRIX32, (uint32_t)i);
150 TEST_ASSERT_EQUAL_UINT32(i, convert_hex_to_uint32(instr));
151 snprintf(instr, 10, "%08" PRIx32, (uint32_t)i);
152 TEST_ASSERT_EQUAL_UINT32(i, convert_hex_to_uint32(instr));
153 }
154 }
155 Catch(e1) {
156 printf("Uncaught exception: %u\n", e1);
157 TEST_FAIL_MESSAGE("Uncaught exception!");
158 }
159}
160
161void test_ascii_serial_com_compute_checksum(void) {
164
165 char checksumOut[5];
166 checksumOut[4] = '\0'; // for easy printing
167
168 Try {
169
170 const char *strings[][2] = {
171 {">xxx.", "79BD"},
172 {">000.", "0FEC"},
173 {">FFF.", "FD98"},
174 {">1234567890ABCDEF.", "9411"},
175 {">FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.", "39DE"},
176 {">00000000000000000000000000000000000000000.", "1DC1"},
177 };
178 for (iString = 0; iString < 6; iString++) {
181 circular_buffer_push_back_string_uint8(&asc.in_buf, strings[iString][0]);
182 circular_buffer_push_back_string_uint8(&asc.out_buf, strings[iString][0]);
183 ascii_serial_com_compute_checksum(&asc, checksumOut, true);
184
185 // printf("Message: \"%s\" checksums: Expected: %s, computed:
186 // %s\n",strings[iString][0],strings[iString][1],checksumOut);
187 for (size_t iByte = 0; iByte < 4; iByte++) {
188 TEST_ASSERT_EQUAL_UINT8(strings[iString][1][iByte], checksumOut[iByte]);
189 }
190 ascii_serial_com_compute_checksum(&asc, checksumOut, false);
191 // printf("Message: \"%s\" checksums: Expected: %s, computed:
192 // %s\n",strings[iString][0],strings[iString][1],checksumOut);
193 for (size_t iByte = 0; iByte < 4; iByte++) {
194 TEST_ASSERT_EQUAL_UINT8(strings[iString][1][iByte], checksumOut[iByte]);
195 }
196 }
197 }
198 Catch(e1) {
199 printf("Uncaught exception: %u\n", e1);
200 TEST_FAIL_MESSAGE("Uncaught exception!");
201 }
202
203 const char *stringsInvalid[] = {".", "...."};
204 for (iString = 0; iString < 2; iString++) {
208 stringsInvalid[iString]);
210 stringsInvalid[iString]);
211 Try { ascii_serial_com_compute_checksum(&asc, checksumOut, true); }
212 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME, e2); }
213 Try { ascii_serial_com_compute_checksum(&asc, checksumOut, false); }
214 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME, e2); }
215 }
216
217 const char *stringsInvalidPeriod[] = {">.",
218 ">xx.",
219 ".>",
220 ".xxxxxx>",
221 ">",
222 ">>>>",
223 ">asdgkjhq23kjhgqwerkjhg1234g\n"};
224 for (iString = 0; iString < 7; iString++) {
228 stringsInvalidPeriod[iString]);
230 stringsInvalidPeriod[iString]);
231 Try { ascii_serial_com_compute_checksum(&asc, checksumOut, true); }
232 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME_PERIOD, e2); }
233 Try { ascii_serial_com_compute_checksum(&asc, checksumOut, false); }
234 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME_PERIOD, e2); }
235 }
236}
237
238void test_ascii_serial_com_put_message_in_output_buffer(void) {
239 Try {
243
244 const char *message1 = ">00w.23A6\n";
245 size_t messageLen = 10;
246 ascii_serial_com_put_message_in_output_buffer(&asc, '0', '0', 'w', "", 0);
247 TEST_ASSERT_EQUAL_size_t(messageLen,
249 for (size_t i = 0; i < messageLen; i++) {
250 TEST_ASSERT_EQUAL_UINT8(message1[i],
252 }
254
255 const char *message2 = ">00wFFFF.9F3B\n";
256 messageLen = 14;
257 ascii_serial_com_put_message_in_output_buffer(&asc, '0', '0', 'w', "FFFF",
258 4);
259 TEST_ASSERT_EQUAL_size_t(messageLen,
261 for (size_t i = 0; i < messageLen; i++) {
262 TEST_ASSERT_EQUAL_UINT8(message2[i],
264 }
266
267 const char *message3 =
268 ">345666666666666666666666666666666666666666666666666666666.C7FB\n";
269 const char *databuf = message3 + 4;
270 messageLen = 64;
271 ascii_serial_com_put_message_in_output_buffer(&asc, '3', '4', '5', databuf,
272 MAXDATALEN);
273 TEST_ASSERT_EQUAL_size_t(messageLen,
275 for (size_t i = 0; i < messageLen; i++) {
276 TEST_ASSERT_EQUAL_UINT8(message3[i],
278 }
280 }
281 Catch(e1) {
282 printf("Uncaught exception: %u\n", e1);
283 TEST_FAIL_MESSAGE("Uncaught exception!");
284 }
285}
286
287void test_ascii_serial_com_get_message_from_input_buffer(void) {
288 Try {
292
293 char ascVersion = '\0';
294 char appVersion = '\0';
295 char command = '\0';
296 char data[MAXDATALEN + 1];
297 for (size_t i = 0; i < MAXDATALEN + 1; i++) {
298 data[i] = '\0';
299 }
300 size_t dataLen = 0;
301
302 const char *message = ">abc.C103\n";
305 &asc, &ascVersion, &appVersion, &command, data, &dataLen);
306 // printf("Message: \"%s\" ascVersion: %c appVersion: %c command: %c data:
307 // %s dataLen: %zu\n", message, ascVersion, appVersion, command, data,
308 // dataLen);
309 TEST_ASSERT_EQUAL_UINT8('a', ascVersion);
310 TEST_ASSERT_EQUAL_UINT8('b', appVersion);
311 TEST_ASSERT_EQUAL_UINT8('c', command);
312 TEST_ASSERT_EQUAL(0, dataLen);
313
314 const char *message2 =
315 ">defxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.350F\n";
318 &asc, &ascVersion, &appVersion, &command, data, &dataLen);
319 // printf("Message: \"%s\" ascVersion: %c appVersion: %c command: %c data:
320 // %s dataLen: %zu\n", message2, ascVersion, appVersion, command, data,
321 // dataLen);
322 TEST_ASSERT_EQUAL_UINT8('d', ascVersion);
323 TEST_ASSERT_EQUAL_UINT8('e', appVersion);
324 TEST_ASSERT_EQUAL_UINT8('f', command);
325 TEST_ASSERT_EQUAL(54, dataLen);
326 for (size_t i = 0; i < dataLen; i++) {
327 TEST_ASSERT_EQUAL_UINT8('x', data[i]);
328 }
329
330 const char *message3 = ">AFw0123456789.A86F\n";
333 &asc, &ascVersion, &appVersion, &command, data, &dataLen);
334 // printf("Message: \"%s\" ascVersion: %c appVersion: %c command: %c data:
335 // %s dataLen: %zu\n", message3, ascVersion, appVersion, command, data,
336 // dataLen);
337 TEST_ASSERT_EQUAL_UINT8('A', ascVersion);
338 TEST_ASSERT_EQUAL_UINT8('F', appVersion);
339 TEST_ASSERT_EQUAL_UINT8('w', command);
340 TEST_ASSERT_EQUAL(10, dataLen);
341 for (size_t i = 0; i < dataLen; i++) {
342 TEST_ASSERT_EQUAL_UINT8(i + 0x30, data[i]);
343 }
344 }
345 Catch(e1) {
346 printf("Uncaught exception: %u\n", e1);
347 TEST_FAIL_MESSAGE("Uncaught exception!");
348 }
349}
350
351void test_ascii_serial_com_put_s_message_in_output_buffer(void) {
352 Try {
356
357 const char *message1 = ">00s00,.7216\n";
358 size_t messageLen = 13;
360 TEST_ASSERT_EQUAL_size_t(messageLen,
362 for (size_t i = 0; i < messageLen; i++) {
363 TEST_ASSERT_EQUAL_UINT8(message1[i],
365 }
367
368 const char *message2 = ">00s01,0000.91AE\n";
369 size_t message2Len = 17;
370 ascii_serial_com_put_s_message_in_output_buffer(&asc, '0', '0', "0000", 4);
371 TEST_ASSERT_EQUAL_size_t(message2Len,
373 for (size_t i = 0; i < message2Len; i++) {
374 TEST_ASSERT_EQUAL_UINT8(message2[i],
376 }
378
379 const char *message3 =
380 ">00s02,000000000000000000000000000000000000000000000000000.7448\n";
381 size_t message3Len = 64;
383 &asc, '0', '0', "000000000000000000000000000000000000000000000000000",
384 51);
385 TEST_ASSERT_EQUAL_size_t(message3Len,
387 for (size_t i = 0; i < message3Len; i++) {
388 TEST_ASSERT_EQUAL_UINT8(message3[i],
390 }
392 }
393 Catch(e1) {
394 printf("Uncaught exception: %u\n", e1);
395 TEST_FAIL_MESSAGE("Uncaught exception!");
396 }
397}
398
399void test_ascii_serial_com_put_error_in_output_buffer(void) {
400 Try {
404
405 const char *message1 = ">00eFF,w,.C117\n";
406 size_t messageLen = 15;
407 ascii_serial_com_put_error_in_output_buffer(&asc, '0', '0', 'w', "", 0,
408 0xFF);
409 // circular_buffer_print_uint8(out_buf,stderr,0);
410 TEST_ASSERT_EQUAL_size_t(messageLen,
412 for (size_t i = 0; i < messageLen; i++) {
413 TEST_ASSERT_EQUAL_UINT8(message1[i],
415 }
417
418 const char *message2 = ">1Fe05,e,0123.5368\n";
419 messageLen = 19;
420 ascii_serial_com_put_error_in_output_buffer(&asc, '1', 'F', 'e', "0123", 4,
421 5);
422 // circular_buffer_print_uint8(out_buf, stderr);
423 TEST_ASSERT_EQUAL_size_t(messageLen,
425 for (size_t i = 0; i < messageLen; i++) {
426 TEST_ASSERT_EQUAL_UINT8(message2[i],
428 }
430
431 const char *message3 = ">1FeFF,s,012345678.2FA0\n";
432 messageLen = 24;
434 "0123456789ABCDEF", 16, 0xFF);
435 // circular_buffer_print_uint8(out_buf, stderr);
436 TEST_ASSERT_EQUAL_size_t(messageLen,
438 for (size_t i = 0; i < messageLen; i++) {
439 TEST_ASSERT_EQUAL_UINT8(message3[i],
441 }
443 }
444 Catch(e1) {
445 printf("Uncaught exception: %u\n", e1);
446 TEST_FAIL_MESSAGE("Uncaught exception!");
447 }
448}
449
450int main(void) {
451 UNITY_BEGIN();
452 RUN_TEST(test_convert_uint8_to_hex);
453 RUN_TEST(test_convert_uint16_to_hex);
454 RUN_TEST(test_convert_uint32_to_hex);
455 RUN_TEST(test_convert_hex_to_uint8);
456 RUN_TEST(test_convert_hex_to_uint16);
457 RUN_TEST(test_convert_hex_to_uint32);
458 RUN_TEST(test_ascii_serial_com_compute_checksum);
459 RUN_TEST(test_ascii_serial_com_put_message_in_output_buffer);
460 RUN_TEST(test_ascii_serial_com_get_message_from_input_buffer);
461 RUN_TEST(test_ascii_serial_com_put_s_message_in_output_buffer);
462 RUN_TEST(test_ascii_serial_com_put_error_in_output_buffer);
463 return UNITY_END();
464}
void convert_uint8_to_hex(uint8_t num, char *outstr, bool caps)
convert uint8 to hex string
circular_buffer_uint8 * ascii_serial_com_get_output_buffer(ascii_serial_com *asc)
ASCII Serial Com get output buffer.
void convert_uint16_to_hex(uint16_t num, char *outstr, bool caps)
convert uint16 to hex string
void ascii_serial_com_put_s_message_in_output_buffer(ascii_serial_com *asc, char ascVersion, char appVersion, const char *data, size_t dataLen)
ASCII Serial Com Pack and put 's' message in output buffer.
void ascii_serial_com_init(ascii_serial_com *asc)
ASCII Serial Com Interface init method.
uint16_t convert_hex_to_uint16(const char *instr)
convert hex string to uint16
void ascii_serial_com_put_message_in_output_buffer(ascii_serial_com *asc, char ascVersion, char appVersion, char command, const char *data, size_t dataLen)
ASCII Serial Com Pack and put message in output buffer.
void ascii_serial_com_get_message_from_input_buffer(ascii_serial_com *asc, char *ascVersion, char *appVersion, char *command, char *data, size_t *dataLen)
ASCII Serial Com pop message from input buffer and unpack.
uint8_t convert_hex_to_uint8(const char *instr)
convert hex string to uint8
uint32_t convert_hex_to_uint32(const char *instr)
convert hex string to uint32
void ascii_serial_com_put_error_in_output_buffer(ascii_serial_com *asc, char ascVersion, char appVersion, char command, char *data, size_t dataLen, enum asc_exception errorCode)
ASCII Serial Com put error message in out buffer.
void ascii_serial_com_compute_checksum(ascii_serial_com *asc, char *checksumOut, bool outputBuffer)
ASCII Serial Com compute checksum of message.
circular_buffer_uint8 * ascii_serial_com_get_input_buffer(ascii_serial_com *asc)
ASCII Serial Com get input buffer.
void convert_uint32_to_hex(uint32_t num, char *outstr, bool caps)
convert uint32 to hex string
size_t circular_buffer_get_size_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get number of elements
uint8_t circular_buffer_get_element_uint8(const circular_buffer_uint8 *circ_buf, const size_t iElement)
circular buffer get element
size_t circular_buffer_push_back_string_uint8(circular_buffer_uint8 *circ_buf, const char *string)
circular buffer push back string (null terminated)
void circular_buffer_clear_uint8(circular_buffer_uint8 *circ_buf)
circular buffer clear
ASCII Serial Com Interface State struct.
circular_buffer_uint8 in_buf
circular_buffer_uint8 out_buf
circular buffer struct