]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/sdr.c
Finished the documentation on the SDR module and minor bug fixed.
[pes-rpp/rpp-lib.git] / rpp / src / rpp / sdr.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  * File : sdr.c
20  * Abstract:
21  *     SD-RAN logging RPP API implementation file.
22  *
23  * References:
24  *     sdr.h
25  *     RPP API documentation.
26  */
27
28
29 #include "rpp/rpp.h"
30
31 #if rppCONFIG_INCLUDE_SDR == 1
32
33 #if rppCONFIG_DRV == 1
34 #include "drv/drv.h"
35 #else
36 #include <stdio.h>
37 #include <string.h>
38 #define rpp_sci_putc(x) putchar(x)
39 #define rpp_sci_printf(x) printf(x)
40 #endif
41
42
43 /// Memory management variables ------------------------------------------------
44 static xSemaphoreHandle memory_mutex;
45 static uint32_t memory_size    = 0;
46 static uint8_t* memory_start   = NULL;
47 static uint8_t* memory_end     = NULL;
48 static uint8_t* memory_current = NULL;
49
50
51
52 /// Command processor task -----------------------------------------------------
53 #define BUF_SIZE 80
54
55 // Buffer to store incomming command
56 static char in_buffer[BUF_SIZE];
57
58 // SCI log command processor task
59 void rpp_sdr_cmdproc(void *p)
60 {
61     static const char* prompt = "--> ";
62     rpp_sci_printf(
63             "Log control: %dkB available.\r\n",
64             (rpp_sdr_available() / 1024)
65         );
66     rpp_sci_printf("========================================\r\n");
67     rpp_sci_printf(prompt);
68
69     uint8_t input = 0;
70     uint8_t buff_index = 0;
71     boolean_t flush = FALSE;
72     while(TRUE) {
73
74         // Get one character from the user
75         if(rpp_sci_read_nb(1, &input) != SUCCESS) {
76             vTaskDelay(10);
77             continue;
78         }
79
80         // Stop flushing if one character is received
81         if(show_flushing) {
82             rpp_sdr_show(FALSE);
83             vTaskDelay(10);
84             rpp_sci_printf("\r\n");
85             rpp_sci_printf(prompt);
86             continue;
87         }
88
89         // Backspace and Delete
90         if(input == 8 || input == 127) {
91             if(buff_index > 0) {
92                 buff_index--;
93             }
94
95         // Line feed or Carriage return
96         } else if(input == 10 || input == 13) {
97             flush = TRUE;
98
99         // If is any printable character
100         } else if(isprint(input)) {
101
102             // Store character and increment buffer index
103             in_buffer[buff_index] = input;
104             buff_index++;
105
106             // Check if buffer is full and force flush
107             if(buff_index == BUF_SIZE - 1) {
108                 flush = TRUE;
109             }
110         }
111         // All other character are ignored
112
113         // Flush buffer
114         if(flush) {
115
116             // Terminate string
117             in_buffer[buff_index] = '\0';
118
119
120             // Re-prompt
121             if(buff_index == 0) {
122                 rpp_sci_printf("\r\n");
123                 rpp_sci_printf(prompt);
124
125             // Help command
126             } else if(strncmp(in_buffer, "help", BUF_SIZE) == 0) {
127                 rpp_sci_printf(
128                         "Available commands: \r\n"
129                                       "    log\r\n"
130                                       "    clear\r\n"
131                                       "    available\r\n"
132                     );
133
134             // Log command
135             } else if(strncmp(in_buffer, "log", BUF_SIZE) == 0) {
136                 rpp_sdr_show(TRUE);
137
138             // Clear command
139             } else if(strncmp(in_buffer, "clear", BUF_SIZE) == 0) {
140                 rpp_sdr_clear();
141                 rpp_sci_printf("Done.\r\n");
142                 rpp_sci_printf(prompt);
143
144             // Available command
145             } else if(strncmp(in_buffer, "available", BUF_SIZE) == 0) {
146                 rpp_sci_printf(
147                         "%d kB of %d kB available.\r\n",
148                         rpp_sdr_available() / 1024,
149                         memory_size / 1024
150                     );
151
152             // Unknow command, print buffer back
153             } else {
154                 rpp_sci_printf(
155                         "ERROR: Unknown command \"%s\"\r\n",
156                         (char*)&in_buffer);
157                 rpp_sci_printf(prompt);
158             }
159
160             // Reset variables
161             rpp_sci_flush(TRUE);
162             buff_index = 0;
163             flush = FALSE;
164         }
165     }
166 }
167
168
169
170 /// Show log task --------------------------------------------------------------
171 // Semaphore to order the show task to start flushing the log
172 static xSemaphoreHandle show_semaphore;
173
174 // Flag to check if log show task is flushing the log
175 static boolean_t show_flushing = FALSE;
176
177 // Show log task
178 void rpp_sdr_showtask(void *p)
179 {
180     uint8_t* current;
181     uint8_t byte;
182
183     while(TRUE) {
184
185         // Wait semaphore to start
186         xSemaphoreTake(show_semaphore, portMAX_DELAY);
187         current = memory_start;
188
189         // Iterate until the end of the log
190         while(show_flushing &&
191               (current != memory_current) &&
192               (current < memory_end)) { // Just in case
193
194             // Print characters in this memory location.
195             // Ignores non-printable characters except \r and \n
196             byte = *current;
197             if((byte == '\r') || (byte == '\n') || isprint(byte)) {
198                 rpp_sci_putc(byte);
199             }
200             current++;
201         }
202
203         // If user waited to finish
204         if(show_flushing) {
205             rpp_sci_printf("\r\n");
206             rpp_sci_printf(prompt);
207             show_flushing = FALSE;
208         }
209     }
210 }
211
212
213
214 /// Public API -----------------------------------------------------------------
215 // Flag to check if SDR module is initialized
216 static boolean_t initialized = FALSE;
217
218 // Initialize SDR module
219 int8_t rpp_sdr_init()
220 {
221     if(initialized) {
222         return FAILURE;
223     }
224     initialized = TRUE;
225
226     // Create memory write mutex
227     memory_mutex = xSemaphoreCreateMutex();
228
229     // Create log show semaphore
230     vSemaphoreCreateBinary(show_semaphore);
231
232     // Define memory bounds
233     #if rppCONFIG_DRV == 1
234     memory_size    = RPP_SDR_ADDR_END - RPP_SDR_ADDR_START + 1;
235     memory_start   = (uint8_t*)RPP_SDR_ADDR_START;
236     memory_end     = (uint8_t*)RPP_SDR_ADDR_END;
237     #else
238     // Allocate 1MB for test
239     memory_size    = 1024*1024;
240     memory_start   = (uint8_t*)malloc(memory_size);
241     memory_end     = (uint8_t*)((uint32_t)memory_start + memory_size - 1);
242     #endif
243     memory_current = memory_start;
244
245     return SUCCESS;
246 }
247
248
249 // General flag to check if logging is enabled
250 static boolean_t log_enabled = FALSE;
251
252 // Task handle for command processor task
253 static xTaskHandle cmdproc_handle;
254
255 // Task handle for log show task
256 static xTaskHandle show_handle;
257
258 // Enable/Disable logging
259 int8_t rpp_sdr_setup(boolean_t enable)
260 {
261     // Just in case user ignore everything
262     if(!initialized) {
263         return FAILURE;
264     }
265
266     // No change, ignore
267     if(log_enabled == enable) {
268         return FAILURE;
269     }
270
271     // Shut down is requested
272     if(log_enabled && !enable) {
273         // Disable logging
274         log_enabled = FALSE;
275         // Stop show task if running
276         rpp_sdr_show(FALSE);
277         // Delete tasks
278         vTaskDelete(cmdproc_handle);
279         vTaskDelete(show_handle);
280
281     // Startup is requested
282     } else {
283         log_enabled = TRUE;
284         xTaskCreate(rpp_sdr_cmdproc,
285                    "rpp_sdr_cmdproc", 512, NULL, 2, &cmdproc_handle);
286         xTaskCreate(rpp_sdr_showtask,
287                    "rpp_sdr_showtask", 512, NULL, 2, &show_handle);
288     }
289
290     return SUCCESS;
291 }
292
293
294 // Memory available
295 uint32_t rpp_sdr_available()
296 {
297     if(!log_enabled) {
298         return 0;
299     }
300     return ((uint32_t)memory_end - (uint32_t)memory_current) + 1;
301 }
302
303
304 // Store something to the log, if logging is enabled
305 int32_t rpp_sdr_printf(const char* format, ...)
306 {
307     if(!log_enabled) {
308         return FAILURE;
309     }
310
311     /// Format user string
312     char str[MAX_BUFFER_LEN];
313     int length = -1;
314
315     va_list argList;
316     va_start(argList, format);
317
318     length = vsnprintf(str, sizeof(str), format, argList);
319
320     va_end(argList);
321
322     if(length < 1) {
323         return length;
324     }
325
326
327     /// Format header
328     // uint32_t max value is 4294967295 (10 digits) + [] + ' ' + '\0' = 14
329     char hdr[14];
330     int hdr_length = -1;
331     hdr_length = vsnprintf(hdr, sizeof(hdr), "[%10d] ", xTaskGetTickCount());
332
333     if(hdr_length < 1) {
334         return hdr_length;
335     }
336
337
338     /// Write header
339     uint32_t cnt = 0;
340     int i = 0;
341     xSemaphoreTake(memory_mutex, portMAX_DELAY);
342     while((memory_current != memory_end) && (i < hdr_length)) {
343
344         *memory_current = hdr[i];
345
346         memory_current++;
347         cnt++;
348         i++;
349     }
350
351
352     /// Write user string
353     i = 0;
354     while((memory_current != memory_end) && (i < length)) {
355
356         *memory_current = str[i];
357
358         memory_current++;
359         cnt++;
360         i++;
361     }
362
363     /// Write trailer
364     const static char trl[2] = {'\r', '\n'};
365     i = 0;
366     while((memory_current != memory_end) && (i < sizeof(trl))) {
367
368         *memory_current = trl[i];
369
370         memory_current++;
371         cnt++;
372         i++;
373     }
374
375     xSemaphoreGive(memory_mutex);
376     return cnt;
377 }
378
379
380 // Clears log. Will also stop the show task
381 int8_t rpp_sdr_clear();
382 {
383     if(!log_enabled) {
384         return FAILURE;
385     }
386
387     // Stop log show flushing if running
388     if(show_flushing) {
389         rpp_sdr_show(FALSE);
390     }
391
392     // Check if log is already empty
393     if(memory_current == memory_start) {
394         return FAILURE;
395     }
396
397     // Reset memory pointer
398     memory_current = memory_start;
399
400     return SUCCESS;
401 }
402
403
404 // Starts/Stops the task that sends the log to the SCI
405 int8_t rpp_sdr_show(boolean_t start)
406 {
407     if(!log_enabled) {
408         return FAILURE;
409     }
410
411     // No change, ignore
412     if(start == show_flushing) {
413         return FAILURE;
414     }
415
416     // Log flush stop requested
417     if(show_flushing && !start) {
418         show_flushing = FALSE;
419
420     // Log flush start requested
421     } else {
422         show_flushing = TRUE;
423         xSemaphoreGive(show_semaphore);
424     }
425
426     return SUCCESS;
427 }
428
429
430 #endif /* rppCONFIG_INCLUDE_SDR */
431