working Communication, Database, Logger (w/out CDC)
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2024 KPI Rover Project
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "ulUlog.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "queue.h"
|
||||
#include "cmsis_os2.h"
|
||||
//#include "usbd_cdc_if.h"
|
||||
//#include "usbd_def.h"
|
||||
#include "main.h"
|
||||
#include "portable.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LOG_QUEUE_LENGTH 20
|
||||
#define MAX_LOG_MESSAGE_SIZE 256
|
||||
|
||||
static void ulogTask(void *argument);
|
||||
|
||||
static osThreadId_t ulogTaskHandle;
|
||||
static StaticQueue_t xUlogQueueBuffer;
|
||||
static uint8_t ucUlogQueueStorage[LOG_QUEUE_LENGTH * MAX_LOG_MESSAGE_SIZE];
|
||||
static QueueHandle_t xULogQueue = NULL;
|
||||
|
||||
volatile uint32_t ulogUsbIsEstablished;
|
||||
osEventFlagsId_t ulogFlags;
|
||||
#define ULOG_CDC_TRANSMIT_FINISH_FLAG 0x1
|
||||
#define ULOG_DEV_CONNECTED_WAKEUP_FLAG 0x2
|
||||
|
||||
static StaticSemaphore_t ulUlog_mutex_storage;
|
||||
#define ULOG_LOCK() // osMutexAcquire(&ulUlog_mutex_storage, osWaitForever)
|
||||
#define ULOG_UNLOCK() // osMutexRelease(&ulUlog_mutex_storage)
|
||||
|
||||
static const osThreadAttr_t ulogTask_attributes = {
|
||||
.name = "ulogTask",
|
||||
.stack_size = 128 * 4,
|
||||
.priority = (osPriority_t) osPriorityNormal,
|
||||
};
|
||||
|
||||
|
||||
void ul_ulog_init()
|
||||
{
|
||||
xULogQueue = xQueueCreateStatic(LOG_QUEUE_LENGTH, MAX_LOG_MESSAGE_SIZE,
|
||||
ucUlogQueueStorage, &xUlogQueueBuffer);
|
||||
if (xULogQueue == NULL)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
ulogTaskHandle = osThreadNew(ulogTask, NULL, &ulogTask_attributes);
|
||||
if (ulogTaskHandle == NULL)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
ulogFlags = osEventFlagsNew(NULL);
|
||||
ulogUsbIsEstablished = 0;
|
||||
|
||||
{
|
||||
const static osMutexAttr_t mutex_attrs = {NULL, 0, &ulUlog_mutex_storage, sizeof(ulUlog_mutex_storage)};
|
||||
(void) osMutexNew(&mutex_attrs);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ul_ulog_send_critical(ulog_level_t level, const char *filename, char *msg)
|
||||
{
|
||||
if (xULogQueue == NULL || msg == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
static char logBuffer[MAX_LOG_MESSAGE_SIZE];
|
||||
int len;
|
||||
size_t msg_len = strlen(msg);
|
||||
|
||||
// Extract just the filename from the full path
|
||||
const char *fbasename = strrchr(filename, '/');
|
||||
if (fbasename) {
|
||||
fbasename++; // Skip the '/'
|
||||
} else {
|
||||
fbasename = filename; // No path separator found
|
||||
}
|
||||
|
||||
// Truncate message if too long, leaving space for timestamp, level, and filename
|
||||
if (msg_len > (MAX_LOG_MESSAGE_SIZE - 60))
|
||||
{
|
||||
msg_len = MAX_LOG_MESSAGE_SIZE - 60;
|
||||
}
|
||||
|
||||
TickType_t currentTime = xTaskGetTickCount();
|
||||
len = snprintf(logBuffer, sizeof(logBuffer), "[%lu][%s][%s] %.*s\r\n",
|
||||
(unsigned long)currentTime,
|
||||
ulog_level_name(level),
|
||||
fbasename,
|
||||
(int)msg_len, msg);
|
||||
|
||||
// Ensure buffer is always null-terminated
|
||||
if (len < 0)
|
||||
{
|
||||
// snprintf failed
|
||||
return;
|
||||
}
|
||||
if (len >= sizeof(logBuffer))
|
||||
{
|
||||
logBuffer[sizeof(logBuffer) - 1] = '\0';
|
||||
}
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
if (uxQueueSpacesAvailable(xULogQueue))
|
||||
{
|
||||
if (xPortIsInsideInterrupt())
|
||||
{
|
||||
xQueueSendFromISR(xULogQueue, logBuffer, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
else
|
||||
{
|
||||
xQueueSend(xULogQueue, logBuffer, pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ul_ulog_send(ulog_level_t level, const char *filename, char *msg)
|
||||
{
|
||||
ULOG_LOCK();
|
||||
ul_ulog_send_critical(level, filename, msg);
|
||||
ULOG_UNLOCK();
|
||||
}
|
||||
|
||||
static void ulogTask(void *argument)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
static char logMessage[MAX_LOG_MESSAGE_SIZE];
|
||||
BaseType_t result = xQueueReceive(xULogQueue, logMessage, portMAX_DELAY);
|
||||
|
||||
if (result == pdTRUE)
|
||||
{
|
||||
logMessage[MAX_LOG_MESSAGE_SIZE - 1] = '\0';
|
||||
|
||||
#if 0
|
||||
while (ulogUsbIsEstablished != 1)
|
||||
{
|
||||
osEventFlagsWait(ulogFlags, ULOG_DEV_CONNECTED_WAKEUP_FLAG, 0, osWaitForever);
|
||||
osDelay(2000);
|
||||
}
|
||||
|
||||
CDC_Transmit_FS((uint8_t *)logMessage, strlen(logMessage));
|
||||
osEventFlagsWait(ulogFlags, ULOG_CDC_TRANSMIT_FINISH_FLAG, 0, 100);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user