1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include "common_def.h"
20 #include "ctype.h"
21 #include "securec.h"
22 #include "soc_osal.h"
23 #include "panic.h"
24 #include "test_suite_log.h"
25 #include "test_suite_errors.h"
26 #include "test_suite.h"
27 #include "test_suite_channel.h"
28 #include "test_suite_console.h"
29
30 /*
31 * -- Configuration parameters
32 */
33 #ifdef CONFIG_TEST_CONSOLE_HISTORY_LEN
34 #define TEST_CONSOLE_HISTORY_LEN CONFIG_TEST_CONSOLE_HISTORY_LEN
35 #else
36 #define TEST_CONSOLE_HISTORY_LEN 10 /* max number of lines to add to the console command history */
37 #endif
38
39 #define COLOR_S_LEN 6
40 #define CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE 300
41
42 #define test_suite_console_sendf(fmt, arg...) g_test_suite_console_channel_funcs->sendf(fmt, ##arg)
43
44 /*
45 * -- Private variables
46 */
47 static const char *g_logo[] = {
48 "Welcome to Test Suite",
49 };
50
51 static char g_test_suite_console_history_commands[TEST_CONSOLE_HISTORY_LEN][CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1];
52 static uint8_t g_test_suite_console_history_pos;
53 static uint8_t g_test_suite_console_history_begin;
54 static uint8_t g_test_suite_console_history_end;
55 static uint8_t g_h_index;
56 static uint8_t g_h_full;
57 static int8_t g_h_pos = 0;
58 static uint16_t g_cmd_len = 0;
59 static uint16_t g_cmd_pos = 0;
60 static char *g_test_suite_console_command_buffer;
61 static volatile bool g_consle_enable = false;
62 static test_suite_channel_funcs_t *g_test_suite_console_channel_funcs = NULL;
63
64 static void test_suite_console_history_add_current_command(void);
65 static uint8_t test_suite_console_history_put_previous_in_command_buffer(void);
66 static uint8_t test_suite_console_history_put_next_in_command_buffer(void);
67
68 /*
69 * -- Cursor write
70 */
test_suite_console_write_string(const char * s)71 static void test_suite_console_write_string(const char *s)
72 {
73 g_test_suite_console_channel_funcs->send(s);
74 }
75
test_suite_console_write_char(const char c)76 static void test_suite_console_write_char(const char c)
77 {
78 g_test_suite_console_channel_funcs->send_char(c);
79 }
80
test_suite_console_write_line(const char * line)81 static void test_suite_console_write_line(const char *line)
82 {
83 g_test_suite_console_channel_funcs->send_line(line);
84 }
85
test_suite_console_helper_repeated_write(char c,uint8_t times)86 static void test_suite_console_helper_repeated_write(char c, uint8_t times)
87 {
88 uint8_t i;
89 for (i = 0; i < times; i++) {
90 test_suite_console_write_char(c);
91 }
92 }
93
94 /*
95 * -- console public funtions
96 */
test_suite_console_cursor_left(void)97 static void test_suite_console_cursor_left(void)
98 {
99 if (g_cmd_pos > 0) {
100 g_cmd_pos--;
101 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE);
102 }
103 }
104
test_suite_console_cursor_right(void)105 static void test_suite_console_cursor_right(void)
106 {
107 if (g_cmd_pos < g_cmd_len) {
108 g_cmd_pos++;
109 test_suite_console_write_char(CONSOLE_CHAR_ESC);
110 test_suite_console_write_char('[');
111 test_suite_console_write_char('C');
112 }
113 }
114
test_suite_console_cursor_up(void)115 static void test_suite_console_cursor_up(void)
116 {
117 if (g_test_suite_console_history_begin != g_test_suite_console_history_end) {
118 /* if there was something written, delete it */
119 test_suite_console_helper_repeated_write(CONSOLE_CHAR_BACKSPACE, (uint8_t)g_cmd_pos);
120 test_suite_console_helper_repeated_write(' ', (uint8_t)g_cmd_len);
121 test_suite_console_helper_repeated_write(CONSOLE_CHAR_BACKSPACE, (uint8_t)g_cmd_len);
122 g_cmd_len = test_suite_console_history_put_previous_in_command_buffer();
123 test_suite_console_write_string((const char *)g_test_suite_console_command_buffer);
124 g_cmd_pos = (uint8_t)g_cmd_len;
125 }
126 }
127
test_suite_console_cursor_down(void)128 static void test_suite_console_cursor_down(void)
129 {
130 if (g_test_suite_console_history_begin != g_test_suite_console_history_end) {
131 test_suite_console_helper_repeated_write(CONSOLE_CHAR_BACKSPACE, (uint8_t)g_cmd_pos);
132 test_suite_console_helper_repeated_write(' ', (uint8_t)g_cmd_len);
133 test_suite_console_helper_repeated_write(CONSOLE_CHAR_BACKSPACE, (uint8_t)g_cmd_len);
134 g_cmd_len = test_suite_console_history_put_next_in_command_buffer();
135 test_suite_console_write_string((const char *)g_test_suite_console_command_buffer);
136 g_cmd_pos = g_cmd_len;
137 }
138 }
139
test_suite_console_home(void)140 static void test_suite_console_home(void)
141 {
142 test_suite_console_helper_repeated_write(CONSOLE_CHAR_BACKSPACE, (uint8_t)g_cmd_pos);
143 g_cmd_pos = 0;
144 }
145
test_suite_console_end(void)146 static void test_suite_console_end(void)
147 {
148 while (g_cmd_pos < g_cmd_len) {
149 g_cmd_pos++;
150 test_suite_console_write_char(CONSOLE_CHAR_ESC);
151 test_suite_console_write_char('[');
152 test_suite_console_write_char('C');
153 }
154 }
155
test_suite_console_pageup(void)156 static void test_suite_console_pageup(void)
157 {
158 if (g_h_full != 0) {
159 return;
160 }
161
162 if (g_h_pos < (g_h_index - 1)) {
163 g_h_pos = (signed char)(g_h_index - 1);//lint !e734
164 for (int i = 0; i < g_cmd_pos; i++) {
165 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE); // Rewind cursor
166 }
167 char *i = g_test_suite_console_history_commands[g_h_index - g_h_pos - 1];
168 while ((*i) != 0) {
169 test_suite_console_write_char(*i++);
170 }
171 int l = i - g_test_suite_console_history_commands[g_h_index - g_h_pos - 1];
172 int n = g_cmd_pos - l;
173 for (int j = 0; j < n; j++) { //lint !e578
174 test_suite_console_write_char(' ');
175 }
176 for (int k = 0; k < n; k++) { //lint !e578
177 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE);
178 }
179 //lint -e{732}
180 g_cmd_pos = g_cmd_len = l; //lint !e734
181 }
182 }
183
test_suite_console_pagedown(void)184 static void test_suite_console_pagedown(void)
185 {
186 if (g_h_full != 0) {
187 return;
188 }
189
190 if (g_h_pos >= 0) {
191 g_h_pos = -1;
192 for (int i = 0; i < g_cmd_pos; i++) {
193 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE); // Rewind cursor
194 }
195 char *i = g_test_suite_console_command_buffer;
196 while ((*i) != 0) {
197 test_suite_console_write_char(*i++);
198 }
199 int l = i - g_test_suite_console_command_buffer;
200 int n = g_cmd_pos - l;
201 for (int j = 0; j < n; j++) { //lint !e578
202 test_suite_console_write_char(' ');
203 }
204 for (int k = 0; k < n; k++) { //lint !e578
205 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE);
206 }
207 //lint -e{732}
208 g_cmd_pos = g_cmd_len = l; //lint !e734
209 }
210 }
211
212 /*
213 * -- cursor init
214 */
test_suite_console_get_channel(void)215 static void test_suite_console_get_channel(void)
216 {
217 g_test_suite_console_channel_funcs = test_suite_channel_get_funcs();
218 }
219
test_suite_console_clear_screen(void)220 static void test_suite_console_clear_screen(void)
221 {
222 test_suite_console_write_char(CONSOLE_CHAR_ESC);
223 test_suite_console_write_string(CONSOLE_COMMAND_CLEAR);
224
225 /* ESC [ 1 1 H (move to 1,1) */
226 test_suite_console_write_char(CONSOLE_CHAR_ESC);
227 test_suite_console_write_string(CONSOLE_COMMAND_MOVE_1_1);
228 }
229
test_suite_console_print_logo(void)230 static void test_suite_console_print_logo(void)
231 {
232 uint8_t i = 0;
233 while (i < (sizeof(g_logo) / sizeof(g_logo[0]))) {
234 test_suite_console_write_line(g_logo[i++]);
235 }
236 }
237
test_suite_console_monitor_init(void)238 static void test_suite_console_monitor_init(void)
239 {
240 test_suite_console_clear_screen();
241 test_suite_console_set_color(TERM_COLOR_RED);
242
243 test_suite_console_print_logo();
244 }
245
test_suite_console_init(void)246 void test_suite_console_init(void)
247 {
248 test_suite_console_get_channel();
249 test_suite_console_monitor_init();
250
251 g_cmd_pos = 0;
252 g_h_full = false;
253 g_h_index = 0;
254 g_cmd_len = 0;
255 g_h_pos = -1;
256 g_test_suite_console_history_begin = 0;
257 g_test_suite_console_history_end = 0;
258 g_test_suite_console_history_pos = TEST_CONSOLE_HISTORY_LEN;
259 }
260
test_suite_console_register_command_buffer(char * buffer_to_register)261 void test_suite_console_register_command_buffer(char *buffer_to_register)
262 {
263 g_test_suite_console_command_buffer = buffer_to_register;
264 }
265
test_suite_console_print_ps(void)266 static void test_suite_console_print_ps(void)
267 {
268 test_suite_console_set_color(TERM_COLOR_BLUE);
269 test_suite_log_string("\nEnter Command >>> ");
270 }
271
test_suite_console_enable(void)272 void test_suite_console_enable(void)
273 {
274 #ifndef SUPPORT_AUDIO_LIEYIN_TOOL
275 test_suite_console_print_ps();
276 g_cmd_pos = 0;
277 g_test_suite_console_command_buffer[g_cmd_pos] = '\0';
278 #endif
279 g_cmd_len = 0;
280 g_consle_enable = true;
281 }
282
test_suite_console_disable(void)283 void test_suite_console_disable(void)
284 {
285 g_consle_enable = false;
286 test_suite_console_write_line("\n");
287 }
288
test_suite_console_is_enabled(void)289 bool test_suite_console_is_enabled(void)
290 {
291 return g_consle_enable;
292 }
293
test_suite_console_history_helper_get_next_index(uint8_t index)294 static inline uint8_t test_suite_console_history_helper_get_next_index(uint8_t index)
295 {
296 uint8_t next_index = index;
297 next_index++;
298 if (next_index >= TEST_CONSOLE_HISTORY_LEN) {
299 next_index = 0;
300 }
301 return next_index;
302 }
303
test_suite_console_history_helper_get_previous_index(uint8_t index)304 static inline uint8_t test_suite_console_history_helper_get_previous_index(uint8_t index)
305 {
306 uint8_t pre_index = index;
307 if (pre_index == 0) {
308 pre_index = TEST_CONSOLE_HISTORY_LEN - 1;
309 } else {
310 pre_index--;
311 }
312 return pre_index;
313 }
314
315 /*
316 * -- Test suite add history command
317 */
test_suite_console_history_add_current_command(void)318 static void test_suite_console_history_add_current_command(void)
319 {
320 uint8_t last_command_index;
321 bool put_in_history = true;
322 int32_t ret;
323 /* if there is something on the history */
324 if (g_test_suite_console_history_end != g_test_suite_console_history_begin) {
325 /* check last command executed */
326 last_command_index = test_suite_console_history_helper_get_previous_index(g_test_suite_console_history_end);
327 if (strcmp((char *)g_test_suite_console_history_commands[last_command_index],
328 (char *)g_test_suite_console_command_buffer) == 0) {
329 put_in_history = false;
330 }
331 }
332 if (put_in_history) {
333 ret = strcpy_s((char *)g_test_suite_console_history_commands[g_test_suite_console_history_end],
334 CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1, (char *)g_test_suite_console_command_buffer);
335 if (ret != EOK) {
336 return;
337 }
338 g_test_suite_console_history_end =
339 test_suite_console_history_helper_get_next_index(g_test_suite_console_history_end);
340 if (g_test_suite_console_history_end == g_test_suite_console_history_begin) {
341 g_test_suite_console_history_begin =
342 test_suite_console_history_helper_get_next_index(g_test_suite_console_history_begin);
343 }
344 }
345 /* reset the history position to the "not in use" value */
346 g_test_suite_console_history_pos = TEST_CONSOLE_HISTORY_LEN;
347 }
348
test_suite_console_history_put_previous_in_command_buffer(void)349 static uint8_t test_suite_console_history_put_previous_in_command_buffer(void)
350 {
351 int32_t ret;
352 /* Move the position index only if it is not at the beginning
353 * write the command in the position index
354 * check it has been called with something in the buffer history */
355 if (g_test_suite_console_history_end == g_test_suite_console_history_begin) {
356 panic(PANIC_TESTSUIT, __LINE__);
357 }
358 /* if it is the first time being called set position to the end */
359 if (g_test_suite_console_history_pos == TEST_CONSOLE_HISTORY_LEN) {
360 g_test_suite_console_history_pos =
361 test_suite_console_history_helper_get_previous_index(g_test_suite_console_history_end);
362 /* otherwise if we have not reached the first command decrement the position */
363 } else if (g_test_suite_console_history_pos != g_test_suite_console_history_begin) {
364 g_test_suite_console_history_pos =
365 test_suite_console_history_helper_get_previous_index(g_test_suite_console_history_pos);
366 }
367 /* copy it into the buffer */
368 ret = strcpy_s(g_test_suite_console_command_buffer, CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1,
369 g_test_suite_console_history_commands[g_test_suite_console_history_pos]);
370 if (ret != EOK) {
371 return 0;
372 }
373 return (uint8_t)strlen(g_test_suite_console_command_buffer);
374 }
375
test_suite_console_history_put_next_in_command_buffer(void)376 static uint8_t test_suite_console_history_put_next_in_command_buffer(void)
377 {
378 uint8_t next;
379 int32_t ret;
380 /* check it has been called with something in the buffer history */
381 if (g_test_suite_console_history_end == g_test_suite_console_history_begin) {
382 panic(PANIC_TESTSUIT, __LINE__);
383 }
384 /* if it is the first time being called set position to the end */
385 if (g_test_suite_console_history_pos == TEST_CONSOLE_HISTORY_LEN) {
386 g_test_suite_console_history_pos =
387 test_suite_console_history_helper_get_previous_index(g_test_suite_console_history_end);
388 }
389 next = test_suite_console_history_helper_get_next_index(g_test_suite_console_history_pos);
390 /* otherwise if we have not reached the first command decrement the position */
391 if (next != g_test_suite_console_history_end) {
392 g_test_suite_console_history_pos = next;
393 }
394 /* copy it into the buffer */
395 ret = strcpy_s(g_test_suite_console_command_buffer, CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1,
396 g_test_suite_console_history_commands[g_test_suite_console_history_pos]);
397 if (ret != EOK) {
398 return 0;
399 }
400 return (uint8_t)strlen(g_test_suite_console_command_buffer);
401 }
402
test_suite_console_backspace_char(char c)403 static void test_suite_console_backspace_char(char c)
404 {
405 if (g_h_pos >= 0) {
406 if (strcpy_s(g_test_suite_console_command_buffer, CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1,
407 g_test_suite_console_history_commands[g_h_index - g_h_pos - 1]) != EOK) {
408 return;
409 }
410 g_h_pos = -1;
411 }
412 if (g_cmd_pos < g_cmd_len) {
413 /* Delete char: move chars from g_cmd_pos+ to the left */
414 test_suite_console_write_char(c);
415 for (uint8_t i = (uint8_t)g_cmd_pos; i < g_cmd_len; i++) {
416 test_suite_console_write_char(g_test_suite_console_command_buffer[i]);
417 g_test_suite_console_command_buffer[i - 1] = g_test_suite_console_command_buffer[i];
418 }
419 test_suite_console_write_char(' ');
420 for (uint8_t i = (uint8_t)g_cmd_pos; i < g_cmd_len; i++) {
421 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE); /* cursor back */
422 }
423 --g_cmd_pos;
424 } else {
425 test_suite_console_write_char(c);
426 test_suite_console_write_char(' ');
427 g_test_suite_console_command_buffer[--g_cmd_pos] = '\0';
428 }
429 test_suite_console_write_char(c);
430 --g_cmd_len;
431 }
432
test_suite_console_delete_char(void)433 static void test_suite_console_delete_char(void)
434 {
435 if (g_h_pos >= 0) {
436 if (strcpy_s(g_test_suite_console_command_buffer, CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1,
437 g_test_suite_console_history_commands[g_h_index - g_h_pos - 1]) != EOK) {
438 return;
439 }
440 g_h_pos = -1;
441 }
442 /* Delete char: move chars from g_cmd_pos+ to the left */
443 for (uint8_t i = (uint8_t)g_cmd_pos; i < g_cmd_len - 1; i++) {
444 g_test_suite_console_command_buffer[i] = g_test_suite_console_command_buffer[i + 1];
445 test_suite_console_write_char(g_test_suite_console_command_buffer[i]);
446 }
447 test_suite_console_write_char(' ');
448 for (uint8_t i = (uint8_t)g_cmd_pos; i < g_cmd_len; i++) {
449 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE); /* cursor back */
450 }
451 g_cmd_len--;
452 }
453
test_suite_console_enter_char(void)454 static void test_suite_console_enter_char(void)
455 {
456 g_test_suite_console_command_buffer[g_cmd_len] = '\0'; /* NULL terminate it */
457 if (g_cmd_len > 0) {
458 test_suite_console_history_add_current_command();
459 }
460 test_suite_console_disable();
461 }
462
test_suite_console_save_char(char c)463 static void test_suite_console_save_char(char c)
464 {
465 if (g_cmd_pos == 0 && c == ' ') {
466 return; /* Eat leading spaces */
467 }
468
469 if (g_h_pos >= 0) {
470 if (strcpy_s(g_test_suite_console_command_buffer, CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE + 1,
471 g_test_suite_console_history_commands[g_h_index - g_h_pos - 1]) != EOK) {
472 return;
473 }
474 g_h_pos = -1;
475 }
476
477 if (g_cmd_len < CONFIG_TEST_SUITE_COMMAND_BUFFER_SIZE) {
478 if (g_cmd_pos < g_cmd_len) {
479 /* Insert char: move chars from g_cmd_pos to the right */
480 for (int32_t i = g_cmd_len - 1; i >= g_cmd_pos; i--) {
481 g_test_suite_console_command_buffer[i + 1] = g_test_suite_console_command_buffer[i];
482 }
483 g_test_suite_console_command_buffer[g_cmd_pos] = c;
484 for (uint8_t i = (uint8_t)g_cmd_pos; i <= g_cmd_len; i++) {
485 test_suite_console_write_char(g_test_suite_console_command_buffer[i]);
486 }
487 for (uint8_t i = (uint8_t)g_cmd_pos; i < g_cmd_len; i++) {
488 test_suite_console_write_char(CONSOLE_CHAR_BACKSPACE); /* cursor back */
489 }
490 } else {
491 g_test_suite_console_command_buffer[g_cmd_pos] = c;
492 test_suite_console_write_char(c);
493 }
494 g_cmd_len++;
495 g_cmd_pos++;
496 }
497 }
498
499 /**
500 * Test suite process csi char from console
501 * @param c The char to process.
502 * @return true Need do more process or false no need do more process;
503 */
test_suite_console_process_char_csi(char c,char * escape_code)504 static bool test_suite_console_process_char_csi(char c, char *escape_code)
505 {
506 switch (c) {
507 case CONSOLE_CHAR_CSI:
508 break;
509 case CONSOLE_CHAR_LEFT:
510 test_suite_console_cursor_left();
511 *escape_code = 0;
512 break;
513 case CONSOLE_CHAR_RIGHT:
514 test_suite_console_cursor_right();
515 *escape_code = 0;
516 break;
517 case CONSOLE_CHAR_UP:
518 test_suite_console_cursor_up();
519 *escape_code = 0;
520 break;
521 case CONSOLE_CHAR_DOWN:
522 test_suite_console_cursor_down();
523 *escape_code = 0;
524 break;
525 case CONSOLE_CHAR_PAGE_HOME:
526 case CONSOLE_CHAR_PAGE_END:
527 case CONSOLE_CHAR_PAGE_UP:
528 case CONSOLE_CHAR_PAGE_DOWN:
529 *escape_code = c;
530 break;
531 default:
532 return true;
533 }
534 return false;
535 }
536
537 /**
538 * Test suite process 7e char from console
539 * @param c The char to process.
540 * @return true Need do more process or false no need do more process;
541 */
test_suite_console_process_char_7e(char * escape_code)542 static bool test_suite_console_process_char_7e(char *escape_code)
543 {
544 switch (*escape_code) {
545 case CONSOLE_CHAR_PAGE_HOME:
546 test_suite_console_home();
547 *escape_code = 0;
548 break;
549 case CONSOLE_CHAR_PAGE_END:
550 test_suite_console_end();
551 *escape_code = 0;
552 break;
553 case CONSOLE_CHAR_PAGE_UP:
554 test_suite_console_pageup();
555 *escape_code = 0;
556 break;
557 case CONSOLE_CHAR_PAGE_DOWN:
558 test_suite_console_pagedown();
559 *escape_code = 0;
560 break;
561 default:
562 return true;
563 }
564 return false;
565 }
566
test_suite_console_process_char(char c)567 void test_suite_console_process_char(char c)
568 {
569 static char escape_code = 0;
570
571 /* if the console is disabled just discard the data */
572 if (!test_suite_console_is_enabled()) { return; }
573
574 /* --------------- *
575 * left = [0x1b][0x5b][0x44]
576 * right = [0x1b][0x5b][0x43]
577 * up = [0x1b][0x5b][0x41]
578 * down = [0x1b][0x5b][0x42]
579 * home = [0x1b][0x5b][0x31][0x7e]
580 * end = [0x1b][0x5b][0x34][0x7e]
581 * pg up = [0x1b][0x5b][0x35][0x7e]
582 * pg dn = [0x1b][0x5b][0x36][0x7e]
583 * tab = [0x09]
584 * dtab = [0x09][0x09]
585 * cancel = [0x03]
586 * reset = [0x12]
587 * --------------- */
588 if (escape_code != 0) {
589 if (escape_code == CONSOLE_CHAR_ESC && (c == CONSOLE_CHAR_CSI || c == CONSOLE_CHAR_NONE)) {
590 escape_code = c;
591 return;
592 } else if (escape_code == CONSOLE_CHAR_CSI) {
593 if (!test_suite_console_process_char_csi(c, &escape_code)) {
594 return;
595 }
596 } else if (c == CONSOLE_CHAR_PAGE_7E) {
597 if (!test_suite_console_process_char_7e(&escape_code)) {
598 return;
599 }
600 }
601
602 /* process cancel */
603 escape_code = 0;
604 }
605
606 if (isprint((int32_t)c)) {
607 test_suite_console_save_char(c);
608 } else if (c == CONSOLE_CHAR_BACKSPACE && (g_cmd_pos > 0)) {
609 test_suite_console_backspace_char(c);
610 } else if (c == CONSOLE_CHAR_DELETE && g_cmd_pos < g_cmd_len) {
611 test_suite_console_delete_char();
612 } else if (c == CONSOLE_CHAR_ENTER) {
613 test_suite_console_enter_char();
614 } else if (c == CONSOLE_CHAR_ESC) {
615 escape_code = c;
616 }
617 }
618
test_suite_console_set_color(term_color_t color)619 void test_suite_console_set_color(term_color_t color)
620 {
621 static term_color_t current_color = TERM_COLOR_RESET;
622 char s[COLOR_S_LEN] = "\x1b[ m";
623 if (color != current_color) {
624 /* ESC [ termColor m */
625 s[2] = (char)((int8_t)'0' + ((int8_t)color / 10)); /* (color / 10) Convert to characters reserved in s[2] */
626 s[3] = (char)((int8_t)'0' + ((int8_t)color % 10)); /* (color % 10) Convert to characters reserved in s[3] */
627 test_suite_console_write_string((const char *)s);
628 current_color = color;
629 }
630 }
631
test_suite_console_display_test_status(int status)632 void test_suite_console_display_test_status(int status)
633 {
634 if (status < 0) { /* Error codes in TEST_SUITE_ERRORS */
635 test_suite_console_set_color(TERM_COLOR_RED);
636 test_suite_console_write_string("[TEST_FAILED : ");
637 if (status == TEST_SUITE_ERROR_TIMED_OUT) {
638 test_suite_console_write_string("MAXIMUM_TIMEOUT_EXCEEDED]");
639 } else if (status == TEST_SUITE_UNKNOWN_FUNCTION) {
640 test_suite_console_write_string("UNKNOWN_FUNCTION]");
641 } else {
642 test_suite_console_write_string("ERROR_CODE_RETURNED]");
643 }
644 test_suite_console_write_string(" [ERROR = ");
645 test_suite_console_sendf("0x%x]\n", status);
646 } else if (status == 0) { /* OK in TEST_SUITE_ERRORS */
647 test_suite_console_set_color(TERM_COLOR_GREEN);
648 test_suite_console_write_line("[TEST_PASSED!]");
649 } else {
650 if (status == 1) {
651 test_suite_console_set_color(TERM_COLOR_GREEN);
652 test_suite_console_write_line("[CONTROL RETURNED]");
653 }
654 }
655 test_suite_console_set_color(TERM_COLOR_WHITE);
656 }
657