1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * SD-RAN logging RPP API implementation file.
25 * RPP API documentation.
31 #if rppCONFIG_INCLUDE_SDR == 1
32 #include <stdio.h> // vsnprintf()
33 #include <ctype.h> // isprint()
34 #include <string.h> // strncmp()
35 #include <stdarg.h> // va_start, va_end
37 #if rppCONFIG_DRV == 1
39 #elif defined(FREERTOS_POSIX)
40 #include <stdlib.h> // malloc()
43 static const char* prompt = "--> ";
44 static const char* newline = "\r\n";
47 /// Memory management variables ------------------------------------------------
48 static xSemaphoreHandle memory_mutex;
49 static uint32_t memory_size = 0;
50 static uint8_t* memory_start = NULL;
51 static uint8_t* memory_end = NULL;
52 static uint8_t* memory_current = NULL;
56 /// Show log task --------------------------------------------------------------
57 // Semaphore to order the show task to start flushing the log
58 static xSemaphoreHandle show_semaphore;
60 // Flag to check if log show task is flushing the log
61 static boolean_t show_flushing = FALSE;
64 void rpp_sdr_showtask(void *p)
71 // Wait semaphore to start
72 xSemaphoreTake(show_semaphore, portMAX_DELAY);
73 current = memory_start;
75 // Iterate until the end of the log
76 while(show_flushing &&
77 (current != memory_current) &&
78 (current < memory_end)) { // Just in case
80 // Print characters in this memory location.
81 // Ignores non-printable characters except \r and \n
83 if((byte == '\r') || (byte == '\n') || isprint(byte)) {
89 // If user waited to finish
91 rpp_sci_printf((const char*)"%s", newline);
92 rpp_sci_printf((const char*)"%s", prompt);
93 show_flushing = FALSE;
100 /// Command processor task -----------------------------------------------------
103 // Buffer to store incomming command
104 static char in_buffer[BUF_SIZE];
106 // SCI log command processor task
107 void rpp_sdr_cmdproc(void *p)
110 (const char*)"Log control: %dkB available.\r\n",
111 (rpp_sdr_available() / 1024)
114 (const char*)"========================================\r\n"
116 rpp_sci_printf((const char*)"%s", prompt);
119 uint8_t buff_index = 0;
120 boolean_t flush = FALSE;
123 // Get one character from the user
124 if(rpp_sci_read_nb(1, &input) != SUCCESS) {
125 vTaskDelay(50 / portTICK_RATE_MS);
129 // Stop flushing if one character is received
132 vTaskDelay(100 / portTICK_RATE_MS);
133 rpp_sci_printf("%s", newline);
134 rpp_sci_printf("%s", prompt);
138 // Backspace and Delete
139 if(input == 8 || input == 127) {
144 // Line feed or Carriage return
145 } else if(input == 10 || input == 13) {
148 // If is any printable character
149 } else if(isprint(input)) {
151 // Store character and increment buffer index
152 in_buffer[buff_index] = input;
155 // Check if buffer is full and force flush
156 if(buff_index == BUF_SIZE - 1) {
160 // All other character are ignored
166 in_buffer[buff_index] = '\0';
170 if(buff_index == 0) {
171 rpp_sci_printf((const char*)"%s", newline);
172 rpp_sci_printf((const char*)"%s", prompt);
175 } else if(strncmp(in_buffer, "help", BUF_SIZE) == 0) {
176 rpp_sci_printf((const char*)
177 "Available commands: \r\n"
184 } else if(strncmp(in_buffer, "log", BUF_SIZE) == 0) {
188 } else if(strncmp(in_buffer, "clear", BUF_SIZE) == 0) {
190 rpp_sci_printf((const char*)"Done.\r\n");
191 rpp_sci_printf((const char*)"%s", prompt);
194 } else if(strncmp(in_buffer, "available", BUF_SIZE) == 0) {
196 (const char*)"%d kB of %d kB available.\r\n",
197 rpp_sdr_available() / 1024,
200 rpp_sci_printf((const char*)"%s", newline);
201 rpp_sci_printf((const char*)"%s", prompt);
203 // Unknown command, print buffer back
206 (const char*)"ERROR: Unknown command \"%s\"\r\n",
208 rpp_sci_printf((const char*)"%s", prompt);
221 /// Public API -----------------------------------------------------------------
222 // Flag to check if SDR module is initialized
223 static boolean_t initialized = FALSE;
225 // Initialize SDR module
226 int8_t rpp_sdr_init()
233 // Create memory write mutex
234 memory_mutex = xSemaphoreCreateMutex();
236 // Create log show semaphore
237 vSemaphoreCreateBinary(show_semaphore);
239 // Define memory bounds
240 #if rppCONFIG_DRV == 1
241 memory_size = RPP_SDR_ADDR_END - RPP_SDR_ADDR_START + 1;
242 memory_start = (uint8_t*)RPP_SDR_ADDR_START;
243 memory_end = (uint8_t*)RPP_SDR_ADDR_END;
244 #elif defined(FREERTOS_POSIX)
245 memory_size = 1024*1024; // Allocate 1MB for test
246 memory_start = (uint8_t*)malloc(memory_size);
247 memory_end = (uint8_t*)(memory_start + memory_size - 1);
249 memory_current = memory_start;
252 #if rppCONFIG_DRV == 1
260 // General flag to check if logging is enabled
261 static boolean_t log_enabled = FALSE;
263 // Task handle for command processor task
264 static xTaskHandle cmdproc_handle;
266 // Task handle for log show task
267 static xTaskHandle show_handle;
269 // Enable/Disable logging
270 int8_t rpp_sdr_setup(boolean_t enable)
272 // Just in case user ignore everything
278 if(log_enabled == enable) {
282 // Shut down is requested
283 if(log_enabled && !enable) {
286 // Stop show task if running
289 vTaskDelete(cmdproc_handle);
290 vTaskDelete(show_handle);
292 // Startup is requested
294 xTaskCreate(rpp_sdr_cmdproc , (const signed char*)"rpp_sdr_cmdproc" ,
295 512, NULL, 2, &cmdproc_handle);
296 xTaskCreate(rpp_sdr_showtask, (const signed char*)"rpp_sdr_showtask",
297 512, NULL, 2, &show_handle);
306 uint32_t rpp_sdr_available()
311 return (uint32_t)(memory_end - memory_current + 1);
315 // Store something to the log, if logging is enabled
316 int32_t rpp_sdr_printf(const char* format, ...)
322 // Don't even try if memory is full
323 if(memory_current == memory_end) {
327 /// Format user string
328 char str[MAX_BUFFER_LEN];
332 va_start(argList, format);
334 length = vsnprintf(str, sizeof(str), format, argList);
344 // uint32_t max value is 4294967295 (10 digits) + [] + ' ' + '\0' = 14
347 hdr_length = sprintf(hdr, (const char*)"[%10d] ", xTaskGetTickCount());
357 xSemaphoreTake(memory_mutex, portMAX_DELAY);
358 while((memory_current != memory_end) && (i < hdr_length)) {
360 *memory_current = hdr[i];
368 /// Write user string
370 while((memory_current != memory_end) && (i < length)) {
372 *memory_current = str[i];
380 static const char trl[2] = {'\r', '\n'};
382 while((memory_current != memory_end) && (i < sizeof(trl))) {
384 *memory_current = trl[i];
391 xSemaphoreGive(memory_mutex);
396 // Clears log. Will also stop the show task
397 int8_t rpp_sdr_clear()
403 // Stop log show flushing if running
408 // Check if log is already empty
409 if(memory_current == memory_start) {
413 // Reset memory pointer
414 memory_current = memory_start;
420 // Starts/Stops the task that sends the log to the SCI
421 int8_t rpp_sdr_show(boolean_t start)
428 if(start == show_flushing) {
432 // Log flush stop requested
433 if(show_flushing && !start) {
434 show_flushing = FALSE;
436 // Log flush start requested
438 show_flushing = TRUE;
439 xSemaphoreGive(show_semaphore);
446 #endif /* rppCONFIG_INCLUDE_SDR */