]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - apps/rpp-test-suite/main.c
Add Makefiles for rpp-test-suite
[pes-rpp/rpp-lib.git] / apps / rpp-test-suite / main.c
1 /* Copyright (C) 2013, 2014 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : main.c
12  * Abstract:
13  *     RPP test suite command processor.
14  *
15  * References:
16  *     test.h
17  */
18
19
20 #include <ctype.h>  // isprint()
21 #include <string.h> // strncmp()
22
23 #include "rpp/rpp.h"
24 #include "test.h"
25
26
27 /// Command processor task -----------------------------------------------------
28 #define INPUT_IDLE 50
29 #define BUF_SIZE   80
30 static char in_buffer[BUF_SIZE];
31 static const char *prompt  = "--> ";
32 static const char *newline = "\r\n";
33
34
35 /**
36  * Compare user string and execute command.
37  */
38 void execute_cmd(char *string)
39 {
40 #ifndef TARGET_POSIX
41         if (       strncmp(string, "adc", BUF_SIZE) == 0)
42                 test_adc();
43         else if (strncmp(string, "can", BUF_SIZE) == 0)
44 #else /* TARGET_POSIX */
45         if (strncmp(string, "can", BUF_SIZE) == 0)
46 #endif /* TARGET_POSIX */
47                 test_can();
48 #ifdef TARGET_POSIX
49         else if (strncmp(string, "eth", BUF_SIZE) == 0)
50                 test_eth();
51         else if (strncmp(string, "fr", BUF_SIZE) == 0)
52                 test_fr();
53         else if (strncmp(string, "lin", BUF_SIZE) == 0)
54                 test_lin();
55 #endif /* TARGET_POSIX */
56         else if (strncmp(string, "sci", BUF_SIZE) == 0)
57                 test_sci();
58         else if (strncmp(string, "help", BUF_SIZE) == 0) {
59                 rpp_sci_printf((const char *)"Available commands:\r\n");
60                 rpp_sci_printf((const char *)"\thelp - Display this help.\r\n");
61 #ifndef TARGET_POSIX
62                 rpp_sci_printf((const char *)"\tain  - Test Analog Input.\r\n");
63 #endif /* ! TARGET_POSIX */
64                 rpp_sci_printf((const char *)"\tcan  - Test CAN communication.\r\n");
65 #ifdef TARGET_POSIX
66                 rpp_sci_printf((const char *)"\teth  - Test Ethernet communication.\r\n");
67                 rpp_sci_printf((const char *)"\tfr   - Test FlexRay communication.\r\n");
68                 rpp_sci_printf((const char *)"\tlin  - Test LIN communication.\r\n");
69 #endif /* TARGET_POSIX */
70                 rpp_sci_printf((const char *)"\tsci  - Test Serial Communication Interface.\r\n");
71         }
72         else
73                 // Unknown command, print buffer back
74                 rpp_sci_printf((const char *)
75                                            "ERROR: Unknown command \"%s\"\r\n", string
76                                            );
77 }
78
79
80 /**
81  * Test suite command processor.
82  */
83 void test_cmdproc(void *par)
84 {
85         // Print header
86         rpp_sci_printf((const char *)
87                                    "RPP Library Test Suite.\r\n"
88                                    );
89         rpp_sci_printf((const char *)
90                                    "===========================================================\r\n"
91                                    );
92         rpp_sci_printf((const char *)
93                                    "[Type a module to test or 'help']\r\n"
94                                    );
95
96         rpp_sci_printf((const char *)"%s", prompt);
97
98         uint8_t input = 0;
99         uint8_t buff_index = 0;
100         boolean_t flush = FALSE;
101         while (TRUE) {
102
103                 // Get one character from the user
104                 if (rpp_sci_read_nb(1, &input) != SUCCESS) {
105                         vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
106                         continue;
107                 }
108
109                 // Backspace and Delete
110                 if (input == 8 || input == 127) {
111                         if (buff_index > 0) {
112                                 buff_index--;
113                                 echo('\b');
114                                 echo(' ' );
115                                 echo('\b');
116                         }
117                         continue;
118                 }
119                 else if (input == 10 || input == 13) {
120                         // Line feed or Carriage return
121                         flush = TRUE;
122                         echo('\r');
123                         echo('\n');
124                 }
125                 else if (isprint(input)) {
126                         // If is any printable character
127
128                         // Store character and increment buffer index
129                         in_buffer[buff_index] = input;
130                         buff_index++;
131                         echo(input);
132
133                         // Check if buffer is full and force flush
134                         if (buff_index == BUF_SIZE - 1)
135                                 flush = TRUE;
136                 }
137                 // All other character are ignored
138
139
140                 // Flush buffer
141                 if (flush) {
142
143                         // Terminate string
144                         in_buffer[buff_index] = '\0';
145
146                         // Execute command
147                         if (buff_index != 0)
148                                 execute_cmd((char *)&in_buffer);
149                         rpp_sci_printf((const char *)"%s", newline);
150                         rpp_sci_printf((const char *)"%s", prompt);
151
152                         // Reset variables
153                         rpp_sci_flush(TRUE);
154                         buff_index = 0;
155                         flush = FALSE;
156                 }
157         }
158 }
159
160
161
162 /// Utilities ------------------------------------------------------------------
163 /**
164  * Wait for user input "q" to quit the loop.
165  */
166 void wait_for_quit()
167 {
168         while (rpp_sci_getc() < 0)
169                 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
170 }
171
172
173 /**
174  * Infinite busy loop.
175  *
176  * This function is called after an error has being detected to "stop"
177  * processing.
178  */
179 void busy_infinite_loop()
180 {
181         while (TRUE)
182                 asm (" nop");
183 }
184
185
186
187 /// Main functions -------------------------------------------------------------
188 /**
189  * Application main function
190  *
191  * This function is called after startup.
192  */
193 int main(void)
194 {
195 #ifndef TARGET_POSIX
196         // Initialize library
197         rpp_init();
198
199         // Speed up the SCI
200         rpp_sci_setup(115200);
201 #endif /* ! TARGET_POSIX */
202         // Spawn tasks
203         portBASE_TYPE task_created =
204                 xTaskCreate(test_cmdproc,
205                                         (const signed char *)"test_cmdproc",
206                                         1024, NULL, TEST_TASK_PRIORITY, NULL
207                                         );
208
209         if (task_created != pdPASS) {
210                 rpp_sci_printf((const char *)
211                                            "ERROR: Problem allocating memory for command processor to "
212                                            "start. Error code: %d\r\n", (uint32_t)task_created
213                                            );
214                 busy_infinite_loop();
215         }
216
217         // Start Scheduler
218         vTaskStartScheduler();
219
220         // Catch memory problems
221         rpp_sci_printf((const char *)
222                                    "ERROR: Problem allocating memory for scheduler to start.\r\n"
223                                    );
224         busy_infinite_loop();
225
226         return 0;
227 }
228
229
230 /**
231  * FreeRTOS malloc() failed hook.
232  */
233 void vApplicationMallocFailedHook(void)
234 {
235         rpp_sci_printf((const char *)
236                                    "ERROR: Manual memory allocation failed.\r\n"
237                                    );
238 }
239
240
241 /**
242  * FreeRTOS stack overflow hook.
243  */
244 void vApplicationStackOverflowHook(xTaskHandle xTask,
245                                                                    signed portCHAR *pcTaskName)
246 {
247         rpp_sci_printf((const char *)
248                                    "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
249                                    );
250 }