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";
46 // extern this semaphore to wait for cmdproc to exit
47 xSemaphoreHandle rpp_sdr_cmdproc_semaphore;
50 /// Memory management variables ------------------------------------------------
51 static xSemaphoreHandle memory_mutex;
52 static uint32_t memory_size = 0;
53 static uint8_t* memory_start = NULL;
54 static uint8_t* memory_end = NULL;
55 static uint8_t* memory_current = NULL;
59 /// Show log task --------------------------------------------------------------
60 // Semaphore to order the show task to start flushing the log
61 static xSemaphoreHandle show_semaphore;
63 // Flag to check if log show task is flushing the log
64 static boolean_t show_flushing = FALSE;
67 void rpp_sdr_showtask(void *p)
74 // Wait semaphore to start
75 xSemaphoreTake(show_semaphore, portMAX_DELAY);
76 current = memory_start;
78 // Iterate until the end of the log
79 while(show_flushing &&
80 (current != memory_current) &&
81 (current < memory_end)) { // Just in case
83 // Print characters in this memory location.
84 // Ignores non-printable characters except \r and \n
86 if((byte == '\r') || (byte == '\n') || isprint(byte)) {
92 // If user waited to finish
94 rpp_sci_printf((const char*)"%s", newline);
95 rpp_sci_printf((const char*)"%s", prompt);
96 show_flushing = FALSE;
103 /// Command processor task -----------------------------------------------------
106 // Buffer to store incomming command
107 static char in_buffer[BUF_SIZE];
109 // SCI log command processor task
110 void rpp_sdr_cmdproc(void *p)
113 (const char*)"Log control: %dkB available.\r\n",
114 (rpp_sdr_available() / 1024)
117 (const char*)"========================================\r\n"
119 rpp_sci_printf((const char*)"%s", prompt);
122 uint8_t buff_index = 0;
123 boolean_t flush = FALSE;
126 // Get one character from the user
127 if(rpp_sci_read_nb(1, &input) != SUCCESS) {
128 vTaskDelay(50 / portTICK_RATE_MS);
132 // Stop flushing if one character is received
135 vTaskDelay(100 / portTICK_RATE_MS);
136 rpp_sci_printf("%s", newline);
137 rpp_sci_printf("%s", prompt);
141 // Backspace and Delete
142 if(input == 8 || input == 127) {
147 // Line feed or Carriage return
148 } else if(input == 10 || input == 13) {
151 // If is any printable character
152 } else if(isprint(input)) {
154 // Store character and increment buffer index
155 in_buffer[buff_index] = input;
158 // Check if buffer is full and force flush
159 if(buff_index == BUF_SIZE - 1) {
163 // All other character are ignored
169 in_buffer[buff_index] = '\0';
173 if(buff_index == 0) {
174 rpp_sci_printf((const char*)"%s", newline);
175 rpp_sci_printf((const char*)"%s", prompt);
178 } else if(strncmp(in_buffer, "help", BUF_SIZE) == 0) {
179 rpp_sci_printf((const char*)
180 "Available commands: \r\n"
187 rpp_sci_printf((const char*)"%s", newline);
188 rpp_sci_printf((const char*)"%s", prompt);
191 } else if(strncmp(in_buffer, "log", BUF_SIZE) == 0) {
195 } else if(strncmp(in_buffer, "clear", BUF_SIZE) == 0) {
197 rpp_sci_printf((const char*)"Done.\r\n");
198 rpp_sci_printf((const char*)"%s", newline);
199 rpp_sci_printf((const char*)"%s", prompt);
202 } else if(strncmp(in_buffer, "available", BUF_SIZE) == 0) {
204 (const char*)"%d kB of %d kB available.\r\n",
205 rpp_sdr_available() / 1024,
208 rpp_sci_printf((const char*)"%s", newline);
209 rpp_sci_printf((const char*)"%s", prompt);
212 } else if(strncmp(in_buffer, "exit", BUF_SIZE) == 0) {
213 xSemaphoreGive(rpp_sdr_cmdproc_semaphore);
216 // Unknown command, print buffer back
219 (const char*)"ERROR: Unknown command \"%s\"\r\n",
221 rpp_sci_printf((const char*)"%s", prompt);
234 /// Public API -----------------------------------------------------------------
235 // Flag to check if SDR module is initialized
236 static boolean_t initialized = FALSE;
238 // Memory for Simulation only
239 #if (rppCONFIG_DRV == 0) && defined(FREERTOS_POSIX)
240 static uint8_t memory_simulation[1024*1024]; // Allocate 1MB for test
243 // Initialize SDR module
244 int8_t rpp_sdr_init()
251 // Create memory write mutex
252 memory_mutex = xSemaphoreCreateMutex();
254 // Create log show semaphore
255 vSemaphoreCreateBinary(show_semaphore);
256 xSemaphoreTake(show_semaphore, 0);
258 // Create semaphore for outer applications to wait cmdproc to exit.
259 // Non static! The symbol should be exported, so use the full prefix.
260 vSemaphoreCreateBinary(rpp_sdr_cmdproc_semaphore);
261 xSemaphoreTake(rpp_sdr_cmdproc_semaphore, 0);
263 // Define memory bounds
264 #if rppCONFIG_DRV == 1
265 memory_size = RPP_SDR_ADDR_END - RPP_SDR_ADDR_START + 1;
266 memory_start = (uint8_t*)RPP_SDR_ADDR_START;
267 memory_end = (uint8_t*)RPP_SDR_ADDR_END;
268 #elif defined(FREERTOS_POSIX)
269 memory_size = sizeof(memory_simulation);
270 memory_start = (uint8_t*)&memory_simulation;
271 memory_end = (uint8_t*)(memory_start + memory_size - 1);
273 memory_current = memory_start;
276 #if rppCONFIG_DRV == 1
284 // General flag to check if logging is enabled
285 static boolean_t log_enabled = FALSE;
287 // Task handle for command processor task
288 static xTaskHandle cmdproc_handle;
290 // Task handle for log show task
291 static xTaskHandle show_handle;
293 // Enable/Disable logging
294 int8_t rpp_sdr_setup(boolean_t enable)
296 // Just in case user ignore everything
302 if(log_enabled == enable) {
306 // Shut down is requested
307 if(log_enabled && !enable) {
310 // Stop show task if running
313 vTaskDelete(cmdproc_handle);
314 vTaskDelete(show_handle);
316 // Startup is requested
318 if(xTaskCreate(rpp_sdr_cmdproc , (const signed char*)"rpp_sdr_cmdproc" ,
319 512, NULL, 2, &cmdproc_handle) != pdPASS) {
322 if(xTaskCreate(rpp_sdr_showtask, (const signed char*)"rpp_sdr_showtask",
323 256, NULL, 2, &show_handle) != pdPASS) {
324 vTaskDelete(cmdproc_handle);
335 uint32_t rpp_sdr_available()
337 return (uint32_t)(memory_end - memory_current + 1);
341 // Store something to the log, if logging is enabled
342 int32_t rpp_sdr_printf(const char* format, ...)
348 // Don't even try if memory is full
349 if(memory_current == memory_end) {
353 /// Format user string
354 char str[MAX_BUFFER_LEN];
358 va_start(argList, format);
360 length = vsnprintf(str, sizeof(str), format, argList);
370 // uint32_t max value is 4294967295 (10 digits) + [] + ' ' + '\0' = 14
373 hdr_length = sprintf(hdr, (const char*)"[%10d] ", xTaskGetTickCount());
383 xSemaphoreTake(memory_mutex, portMAX_DELAY);
384 while((memory_current != memory_end) && (i < hdr_length)) {
386 *memory_current = hdr[i];
394 /// Write user string
396 while((memory_current != memory_end) && (i < length)) {
398 *memory_current = str[i];
406 static const char trl[2] = {'\r', '\n'};
408 while((memory_current != memory_end) && (i < sizeof(trl))) {
410 *memory_current = trl[i];
417 xSemaphoreGive(memory_mutex);
422 // Clears log. Will also stop the show task
423 int8_t rpp_sdr_clear()
429 // Stop log show flushing if running
434 // Check if log is already empty
435 if(memory_current == memory_start) {
439 // Reset memory pointer
440 memory_current = memory_start;
446 // Starts/Stops the task that sends the log to the SCI
447 int8_t rpp_sdr_show(boolean_t start)
454 if(start == show_flushing) {
458 // Log flush stop requested
459 if(show_flushing && !start) {
460 show_flushing = FALSE;
462 // Log flush start requested
464 show_flushing = TRUE;
465 xSemaphoreGive(show_semaphore);
472 #endif /* rppCONFIG_INCLUDE_SDR */