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_device.c
1#include "asc_exception.h"
3#include "externals/unity.h"
4#include <inttypes.h>
5#include <stdio.h>
6
7CEXCEPTION_T e1;
8CEXCEPTION_T e2;
9
10ascii_serial_com *asc_f;
11char ascVersion_f, appVersion_f, command_f;
12char data_f[MAXDATALEN];
13size_t dataLen_f;
14void *state_f;
15
16void testfunc(ascii_serial_com *asc, char ascVersion, char appVersion,
17 char command, char *data, size_t dataLen, void *state);
18
19void testfunc(ascii_serial_com *asc, char ascVersion, char appVersion,
20 char command, char *data, size_t dataLen, void *state) {
21 // fprintf(stderr,
22 // "testfunc: asc: %p ascVersion %c appVersion %c command %c dataLen
23 // "
24 // "%zu state: %p\n data: ",
25 // asc, ascVersion, appVersion, command, dataLen, state);
26 for (size_t i = 0; i < dataLen; i++) {
27 // fprintf(stderr, "%c", data[i]);
28 data_f[i] = data[i];
29 }
30 // fprintf(stderr, "\n");
31 // fflush(stderr);
32 asc_f = asc;
33 ascVersion_f = ascVersion;
34 appVersion_f = appVersion;
35 command_f = command;
36 dataLen_f = dataLen;
37 state_f = state;
38}
39
40int state_rw;
41int state_s;
42int state_nf;
43
44void setUp(void) {
45 // set stuff up here
46 state_rw = 1;
47 state_s = 2;
48}
49
50void tearDown(void) {
51 // clean stuff up here
52}
53
54void test_ascii_serial_com_device_receive_good(void) {
55 Try {
57 ascii_serial_com_device_config config = {testfunc, testfunc, testfunc,
58 &state_rw, &state_s, &state_nf};
59 ascii_serial_com_device_init(&ascd, &config);
60 circular_buffer_uint8 *in_buf =
62 // circular_buffer_uint8* out_buf =
63 // ascii_serial_com_device_get_output_buffer(&ascd);
64
65 circular_buffer_push_back_string_uint8(in_buf, ">00w.23A6\n");
67 TEST_ASSERT_EQUAL_CHAR('0', ascVersion_f);
68 TEST_ASSERT_EQUAL_CHAR('0', appVersion_f);
69 TEST_ASSERT_EQUAL_CHAR('w', command_f);
70 TEST_ASSERT_EQUAL_size_t(0, dataLen_f);
71 TEST_ASSERT_EQUAL(&state_rw, state_f);
72 TEST_ASSERT_EQUAL_INT(state_rw, *((int *)state_f));
73
75 circular_buffer_push_back_string_uint8(in_buf, ">12r.4EBA\n");
77 TEST_ASSERT_EQUAL_CHAR('1', ascVersion_f);
78 TEST_ASSERT_EQUAL_CHAR('2', appVersion_f);
79 TEST_ASSERT_EQUAL_CHAR('r', command_f);
80 TEST_ASSERT_EQUAL_size_t(0, dataLen_f);
81 TEST_ASSERT_EQUAL(&state_rw, state_f);
82 TEST_ASSERT_EQUAL_INT(state_rw, *((int *)state_f));
83
85 circular_buffer_push_back_string_uint8(in_buf, ">00wFFFF.9F3B\n");
87 TEST_ASSERT_EQUAL_CHAR('0', ascVersion_f);
88 TEST_ASSERT_EQUAL_CHAR('0', appVersion_f);
89 TEST_ASSERT_EQUAL_CHAR('w', command_f);
90 TEST_ASSERT_EQUAL_size_t(4, dataLen_f);
91 TEST_ASSERT_EQUAL_MEMORY("FFFF", data_f, 4);
92 TEST_ASSERT_EQUAL(&state_rw, state_f);
93 TEST_ASSERT_EQUAL_INT(state_rw, *((int *)state_f));
94
97 ">FFs111 222 333 444.B049\n");
99 TEST_ASSERT_EQUAL_CHAR('F', ascVersion_f);
100 TEST_ASSERT_EQUAL_CHAR('F', appVersion_f);
101 TEST_ASSERT_EQUAL_CHAR('s', command_f);
102 TEST_ASSERT_EQUAL_size_t(15, dataLen_f);
103 TEST_ASSERT_EQUAL_MEMORY("111 222 333 444", data_f, dataLen_f);
104 TEST_ASSERT_EQUAL(&state_s, state_f);
105 TEST_ASSERT_EQUAL_INT(state_s, *((int *)state_f));
106
107 // Test nonsense, make sure it doesn't write to command (or if it does it
108 // writes null)
109 command_f = '\0';
112 in_buf,
113 ">345666666666666666666666666666666666666666666666666666666.C7FB\n");
115 TEST_ASSERT_EQUAL_CHAR('\0', command_f);
116 }
117 Catch(e1) {
118 printf("Uncaught exception: %u\n", e1);
119 TEST_FAIL_MESSAGE("Uncaught exception!");
120 }
121}
122
123void test_ascii_serial_com_device_receive_null_state(void) {
124 Try {
126 ascii_serial_com_device_config config = {testfunc, testfunc, testfunc,
127 NULL, NULL, NULL};
128 ascii_serial_com_device_init(&ascd, &config);
129 circular_buffer_uint8 *in_buf =
131 // circular_buffer_uint8* out_buf =
132 // ascii_serial_com_device_get_output_buffer(&ascd);
133
134 circular_buffer_push_back_string_uint8(in_buf, ">00w.23A6\n");
136 TEST_ASSERT_EQUAL_CHAR('0', ascVersion_f);
137 TEST_ASSERT_EQUAL_CHAR('0', appVersion_f);
138 TEST_ASSERT_EQUAL_CHAR('w', command_f);
139 TEST_ASSERT_EQUAL_size_t(0, dataLen_f);
140 TEST_ASSERT_EQUAL(NULL, state_f);
141
143 circular_buffer_push_back_string_uint8(in_buf, ">12r.4EBA\n");
145 TEST_ASSERT_EQUAL_CHAR('1', ascVersion_f);
146 TEST_ASSERT_EQUAL_CHAR('2', appVersion_f);
147 TEST_ASSERT_EQUAL_CHAR('r', command_f);
148 TEST_ASSERT_EQUAL_size_t(0, dataLen_f);
149 TEST_ASSERT_EQUAL(NULL, state_f);
150
153 ">FFs111 222 333 444.B049\n");
155 TEST_ASSERT_EQUAL_CHAR('F', ascVersion_f);
156 TEST_ASSERT_EQUAL_CHAR('F', appVersion_f);
157 TEST_ASSERT_EQUAL_CHAR('s', command_f);
158 TEST_ASSERT_EQUAL_size_t(15, dataLen_f);
159 TEST_ASSERT_EQUAL_MEMORY("111 222 333 444", data_f, dataLen_f);
160 TEST_ASSERT_EQUAL(NULL, state_f);
161
162 // Test nonsense, make sure it doesn't write to command (or if it does it
163 // writes null)
164 command_f = '\0';
167 in_buf,
168 ">345666666666666666666666666666666666666666666666666666666.C7FB\n");
170 TEST_ASSERT_EQUAL_CHAR('\0', command_f);
171 TEST_ASSERT_EQUAL(NULL, state_f);
172 }
173 Catch(e1) {
174 printf("Uncaught exception: %u\n", e1);
175 TEST_FAIL_MESSAGE("Uncaught exception!");
176 }
177}
178
179void test_ascii_serial_com_device_receive_bad(void) {
181 ascii_serial_com_device_config config = {testfunc, testfunc, testfunc,
182 &state_rw, &state_s, &state_nf};
183 ascii_serial_com_device_init(&ascd, &config);
184 circular_buffer_uint8 *in_buf =
186
187 Try {
188 circular_buffer_push_back_string_uint8(in_buf, "00w.23A6\n");
190 }
191 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME, e2); }
192
193 Try {
195 circular_buffer_push_back_string_uint8(in_buf, ">00wFFFF\n");
197 }
198 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME_PERIOD, e2); }
199
200 Try {
204 }
205 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME_PERIOD, e2); }
206
207 Try {
210 in_buf,
211 ">345666666666666666666666666666666666666666166666666666666.C7FB\n");
213 }
214 Catch(e2) { TEST_ASSERT_EQUAL(ASC_ERROR_INVALID_FRAME, e2); }
215}
216
217void test_ascii_serial_com_device_receive_null_func(void) {
218 Try {
221 ascii_serial_com_device_init(&ascd, &config);
222 circular_buffer_uint8 *in_buf =
224 circular_buffer_uint8 *out_buf =
226
227 size_t messageLen = 15;
228 const char *message = ">00e15,w,.DF68\n";
229 circular_buffer_push_back_string_uint8(in_buf, ">00w.23A6\n");
231 // circular_buffer_print_uint8(out_buf,stderr,0);
232 TEST_ASSERT_EQUAL_size_t(messageLen,
234 for (size_t i = 0; i < messageLen; i++) {
235 TEST_ASSERT_EQUAL_UINT8(message[i],
237 }
240
241 messageLen = 15;
242 message = ">12e15,r,.52D5\n";
243 circular_buffer_push_back_string_uint8(in_buf, ">12r.4EBA\n");
245 // circular_buffer_print_uint8(out_buf, stderr, 0);
246 TEST_ASSERT_EQUAL_size_t(messageLen,
248 for (size_t i = 0; i < messageLen; i++) {
249 TEST_ASSERT_EQUAL_UINT8(message[i],
251 }
254
255 messageLen = 24;
256 message = ">FFe15,s,111 222 3.7005\n";
258 ">FFs111 222 333 444.B049\n");
260 TEST_ASSERT_EQUAL_size_t(messageLen,
262 for (size_t i = 0; i < messageLen; i++) {
263 TEST_ASSERT_EQUAL_UINT8(message[i],
265 }
268
269 messageLen = 24;
270 message = ">34e15,5,666666666.6441\n";
272 in_buf,
273 ">345666666666666666666666666666666666666666666666666666666.C7FB\n");
275 TEST_ASSERT_EQUAL_size_t(messageLen,
277 for (size_t i = 0; i < messageLen; i++) {
278 TEST_ASSERT_EQUAL_UINT8(message[i],
280 }
281 }
282 Catch(e1) {
283 printf("Uncaught exception: %u\n", e1);
284 TEST_FAIL_MESSAGE("Uncaught exception!");
285 }
286}
287
288int main(void) {
289 UNITY_BEGIN();
290 RUN_TEST(test_ascii_serial_com_device_receive_good);
291 RUN_TEST(test_ascii_serial_com_device_receive_null_state);
292 RUN_TEST(test_ascii_serial_com_device_receive_bad);
293 RUN_TEST(test_ascii_serial_com_device_receive_null_func);
294 return UNITY_END();
295}
circular_buffer_uint8 * ascii_serial_com_device_get_output_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get output buffer.
void ascii_serial_com_device_receive(ascii_serial_com_device *ascd)
ASCII Serial Com Device receive messages.
circular_buffer_uint8 * ascii_serial_com_device_get_input_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get input buffer.
void ascii_serial_com_device_init(ascii_serial_com_device *ascd, ascii_serial_com_device_config *config)
ASCII Serial Com Device init.
ASCII Serial Com Device.
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 Device Config Struct.
ASCII Serial Com Device State struct.
ASCII Serial Com Interface State struct.
circular buffer struct