3#include "externals/unity.h"
19void test_convert_uint8_to_hex(
void) {
22 char outstr[3] =
"\0\0\0";
23 for (
size_t i = 0; i < 0x100; i++) {
26 snprintf(stdiostr, 4,
"%02hhX", j);
28 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 2);
31 snprintf(stdiostr, 4,
"%02hhx", j);
33 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 2);
37 printf(
"Uncaught exception: %u\n", e1);
38 TEST_FAIL_MESSAGE(
"Uncaught exception!");
42void test_convert_uint16_to_hex(
void) {
45 char outstr[5] =
"\0\0\0\0\0";
46 for (
size_t i = 0; i < 0x10000; i++) {
49 snprintf(stdiostr, 6,
"%04" PRIX16, j);
51 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 4);
54 snprintf(stdiostr, 6,
"%04" PRIx16, j);
56 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 4);
60 printf(
"Uncaught exception: %u\n", e1);
61 TEST_FAIL_MESSAGE(
"Uncaught exception!");
65void test_convert_uint32_to_hex(
void) {
69 for (
size_t i = 0; i < 9; i++) {
72 for (uint64_t i = 0; i < 0x100000000; i += 0x100000) {
75 snprintf(stdiostr, 10,
"%08" PRIX32, j);
77 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
80 snprintf(stdiostr, 10,
"%08" PRIx32, j);
82 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
84 for (uint64_t i = 0; i < 0x1F000; i++) {
87 snprintf(stdiostr, 10,
"%08" PRIX32, j);
89 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
92 snprintf(stdiostr, 10,
"%08" PRIx32, j);
94 TEST_ASSERT_EQUAL_MEMORY(stdiostr, outstr, 8);
98 printf(
"Uncaught exception: %u\n", e1);
99 TEST_FAIL_MESSAGE(
"Uncaught exception!");
103void test_convert_hex_to_uint8(
void) {
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);
111 snprintf(instr, 4,
"%02" PRIx8, (uint8_t)i);
118 printf(
"Uncaught exception: %u\n", e1);
119 TEST_FAIL_MESSAGE(
"Uncaught exception!");
123void test_convert_hex_to_uint16(
void) {
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);
129 snprintf(instr, 6,
"%04" PRIx16, (uint16_t)i);
134 printf(
"Uncaught exception: %u\n", e1);
135 TEST_FAIL_MESSAGE(
"Uncaught exception!");
139void test_convert_hex_to_uint32(
void) {
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);
145 snprintf(instr, 10,
"%08" PRIx32, (uint32_t)i);
148 for (uint64_t i = 0; i < 0x1F000; i++) {
149 snprintf(instr, 10,
"%08" PRIX32, (uint32_t)i);
151 snprintf(instr, 10,
"%08" PRIx32, (uint32_t)i);
156 printf(
"Uncaught exception: %u\n", e1);
157 TEST_FAIL_MESSAGE(
"Uncaught exception!");
161void test_ascii_serial_com_compute_checksum(
void) {
166 checksumOut[4] =
'\0';
170 const char *strings[][2] = {
174 {
">1234567890ABCDEF.",
"9411"},
175 {
">FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.",
"39DE"},
176 {
">00000000000000000000000000000000000000000.",
"1DC1"},
178 for (iString = 0; iString < 6; iString++) {
187 for (
size_t iByte = 0; iByte < 4; iByte++) {
188 TEST_ASSERT_EQUAL_UINT8(strings[iString][1][iByte], checksumOut[iByte]);
193 for (
size_t iByte = 0; iByte < 4; iByte++) {
194 TEST_ASSERT_EQUAL_UINT8(strings[iString][1][iByte], checksumOut[iByte]);
199 printf(
"Uncaught exception: %u\n", e1);
200 TEST_FAIL_MESSAGE(
"Uncaught exception!");
203 const char *stringsInvalid[] = {
".",
"...."};
204 for (iString = 0; iString < 2; iString++) {
208 stringsInvalid[iString]);
210 stringsInvalid[iString]);
212 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME, e2); }
214 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME, e2); }
217 const char *stringsInvalidPeriod[] = {
">.",
223 ">asdgkjhq23kjhgqwerkjhg1234g\n"};
224 for (iString = 0; iString < 7; iString++) {
228 stringsInvalidPeriod[iString]);
230 stringsInvalidPeriod[iString]);
232 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME_PERIOD, e2); }
234 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME_PERIOD, e2); }
238void test_ascii_serial_com_put_message_in_output_buffer(
void) {
244 const char *message1 =
">00w.23A6\n";
245 size_t messageLen = 10;
247 TEST_ASSERT_EQUAL_size_t(messageLen,
249 for (
size_t i = 0; i < messageLen; i++) {
250 TEST_ASSERT_EQUAL_UINT8(message1[i],
255 const char *message2 =
">00wFFFF.9F3B\n";
259 TEST_ASSERT_EQUAL_size_t(messageLen,
261 for (
size_t i = 0; i < messageLen; i++) {
262 TEST_ASSERT_EQUAL_UINT8(message2[i],
267 const char *message3 =
268 ">345666666666666666666666666666666666666666666666666666666.C7FB\n";
269 const char *databuf = message3 + 4;
273 TEST_ASSERT_EQUAL_size_t(messageLen,
275 for (
size_t i = 0; i < messageLen; i++) {
276 TEST_ASSERT_EQUAL_UINT8(message3[i],
282 printf(
"Uncaught exception: %u\n", e1);
283 TEST_FAIL_MESSAGE(
"Uncaught exception!");
287void test_ascii_serial_com_get_message_from_input_buffer(
void) {
293 char ascVersion =
'\0';
294 char appVersion =
'\0';
296 char data[MAXDATALEN + 1];
297 for (
size_t i = 0; i < MAXDATALEN + 1; i++) {
302 const char *message =
">abc.C103\n";
305 &asc, &ascVersion, &appVersion, &command, data, &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);
314 const char *message2 =
315 ">defxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.350F\n";
318 &asc, &ascVersion, &appVersion, &command, data, &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]);
330 const char *message3 =
">AFw0123456789.A86F\n";
333 &asc, &ascVersion, &appVersion, &command, data, &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]);
346 printf(
"Uncaught exception: %u\n", e1);
347 TEST_FAIL_MESSAGE(
"Uncaught exception!");
351void test_ascii_serial_com_put_s_message_in_output_buffer(
void) {
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],
368 const char *message2 =
">00s01,0000.91AE\n";
369 size_t message2Len = 17;
371 TEST_ASSERT_EQUAL_size_t(message2Len,
373 for (
size_t i = 0; i < message2Len; i++) {
374 TEST_ASSERT_EQUAL_UINT8(message2[i],
379 const char *message3 =
380 ">00s02,000000000000000000000000000000000000000000000000000.7448\n";
381 size_t message3Len = 64;
383 &asc,
'0',
'0',
"000000000000000000000000000000000000000000000000000",
385 TEST_ASSERT_EQUAL_size_t(message3Len,
387 for (
size_t i = 0; i < message3Len; i++) {
388 TEST_ASSERT_EQUAL_UINT8(message3[i],
394 printf(
"Uncaught exception: %u\n", e1);
395 TEST_FAIL_MESSAGE(
"Uncaught exception!");
399void test_ascii_serial_com_put_error_in_output_buffer(
void) {
405 const char *message1 =
">00eFF,w,.C117\n";
406 size_t messageLen = 15;
410 TEST_ASSERT_EQUAL_size_t(messageLen,
412 for (
size_t i = 0; i < messageLen; i++) {
413 TEST_ASSERT_EQUAL_UINT8(message1[i],
418 const char *message2 =
">1Fe05,e,0123.5368\n";
423 TEST_ASSERT_EQUAL_size_t(messageLen,
425 for (
size_t i = 0; i < messageLen; i++) {
426 TEST_ASSERT_EQUAL_UINT8(message2[i],
431 const char *message3 =
">1FeFF,s,012345678.2FA0\n";
434 "0123456789ABCDEF", 16, 0xFF);
436 TEST_ASSERT_EQUAL_size_t(messageLen,
438 for (
size_t i = 0; i < messageLen; i++) {
439 TEST_ASSERT_EQUAL_UINT8(message3[i],
445 printf(
"Uncaught exception: %u\n", e1);
446 TEST_FAIL_MESSAGE(
"Uncaught exception!");
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);
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