From 77cefdb0ce70f0f4e19bf24d191642d219e2be44 Mon Sep 17 00:00:00 2001 From: cfif Date: Mon, 30 Mar 2026 14:25:13 +0300 Subject: [PATCH] Init --- Inc/LoggerToSerialPortV2.h | 61 ++++++++++++ Src/LoggerToSerialPortV2.c | 190 +++++++++++++++++++++++++++++++++++++ modular.json | 37 ++++++++ 3 files changed, 288 insertions(+) create mode 100644 Inc/LoggerToSerialPortV2.h create mode 100644 Src/LoggerToSerialPortV2.c create mode 100644 modular.json diff --git a/Inc/LoggerToSerialPortV2.h b/Inc/LoggerToSerialPortV2.h new file mode 100644 index 0000000..5e48cfb --- /dev/null +++ b/Inc/LoggerToSerialPortV2.h @@ -0,0 +1,61 @@ +// +// Created by CFIF on 13.11.22. +// + +#ifndef UVEOS_ON_NATION2_LOGGERTOSERIALPORT_H +#define UVEOS_ON_NATION2_LOGGERTOSERIALPORT_H + +#include "SerialPort.h" +#include "LoggerInterface.h" +#include "RtcIO.h" +#include + +typedef struct { + tSerialPortIO *serialPortIo_VIRT; + tSerialPortIO *serialPortIo_PHYSIC; + tRtcIO *rtc; + uint32_t timeout; + uint32_t timeoutTransmittedLog; + uint8_t authorLimit; + bool open; + tLoggerInterface logger; + uint16_t flags; + int32_t greenwichOffset; + + uint8_t buf_LOG[256]; + + uint8_t bufLogger[MAX_COUNT_BUF_LOG][MAX_LEN_LOGGER_BUF_LOG + MAX_LEN_PRINTF_BUF_LOG + 32]; + uint8_t bufLoggerNames[MAX_COUNT_BUF_LOG][MAX_LEN_BUF_NAME_LOG]; + uint8_t countBufLoggerNames; + + struct { + osThreadId_t id; + uint32_t stack[384]; + StaticTask_t controlBlock; + osThreadAttr_t attr; + } thread; + +} tLoggerToSerialPortV2; + +#define SERIAL_LOGGER_SHOW_AUTHOR 0b1 << 0 +#define SERIAL_LOGGER_SHOW_LOG_LEVEL 0b1 << 1 +#define SERIAL_LOGGER_SHOW_TIME 0b1 << 2 +#define SERIAL_LOGGER_SHOW_DATE 0b1 << 3 + +void LoggerToSerialPortV2_Init( + tLoggerToSerialPortV2 *env, + int32_t greenwichOffset, + tSerialPortIO *serialPortIo_VIRT, + tSerialPortIO *serialPortIo_PHYSIC, + tRtcIO *rtc, + uint16_t flags, + uint32_t timeoutTransmittedLog +// bool showDate, +// bool showTime, +// bool showLoglevel, +// bool showAuthor +); + +void LogTransmitter_StartThread(tLoggerToSerialPortV2 *env); + +#endif //UVEOS_ON_NATION2_LOGGERTOSERIALPORT_H diff --git a/Src/LoggerToSerialPortV2.c b/Src/LoggerToSerialPortV2.c new file mode 100644 index 0000000..e6e0017 --- /dev/null +++ b/Src/LoggerToSerialPortV2.c @@ -0,0 +1,190 @@ +// +// Created by CFIF on 13.11.22. +// +#include +#include "LoggerToSerialPortV2.h" +#include "SystemDelayInterface.h" +#include "AsciiStringAssmeblingUtils.h" +#include "Rtc.h" +#include +#include "CmsisRtosThreadUtils.h" +#include "memory.h" +#include CMSIS_device_header + +static uint8_t * +getMemLoggerBufLog(tLoggerToSerialPortV2 *env, char *authorStatic, const uint8_t authorLen, uint16_t offset) { + + for (uint8_t i = 0; i < env->countBufLoggerNames; ++i) { + if (memcmp(env->bufLoggerNames[i], authorStatic, authorLen) == 0) { + return &env->bufLogger[i][offset]; + } + } + + configASSERT(env->countBufLoggerNames < MAX_COUNT_BUF_LOG); + + uint8_t len = authorLen; + if (authorLen > MAX_LEN_BUF_NAME_LOG) { + len = MAX_LEN_BUF_NAME_LOG; + } + + memcpy(env->bufLoggerNames[env->countBufLoggerNames], authorStatic, len); + ++env->countBufLoggerNames; + + return &env->bufLogger[env->countBufLoggerNames - 1][offset]; +} + +static void LoggerToSerialPort_TimeToString(time_t timestamp, char *strbuf) { + struct tm timestampTM; + localtime_r(×tamp, ×tampTM); + asctime_r(×tampTM, strbuf); +} + +static void LoggerToSerialPort_PrintLegend( + tLoggerToSerialPortV2 *env, + char *authorStatic, + const uint8_t authorLen, + eLoggerLevel loglevel +) { + //char str[128]; + char *str = (char *) getMemLoggerBufLog(env, authorStatic, authorLen, 0); + size_t strLen = 0; + + if (env->flags & (SERIAL_LOGGER_SHOW_DATE | SERIAL_LOGGER_SHOW_TIME)) { + + time_t timestamp; + //char timeString[24]; + char *timeString = (char *) getMemLoggerBufLog(env, authorStatic, authorLen, + MAX_LEN_LOGGER_BUF_LOG + MAX_LEN_PRINTF_BUF_LOG); + + if (env->rtc) { + RtcGet(env->rtc, ×tamp); + timestamp += env->greenwichOffset * 3600; + } else { + uint32_t msFromDeviceStart = SystemGetMs() / 1000; + timestamp = 1672531200 + msFromDeviceStart; + } + + LoggerToSerialPort_TimeToString(timestamp, timeString); + + if (env->flags & SERIAL_LOGGER_SHOW_DATE) { + vAsciiStringAdd(str, &strLen, &timeString[20], 4); + vAsciiStringAdd(str, &strLen, &timeString[3], 7); + vAsciiStringAdd(str, &strLen, " ", 1); + } + + if (env->flags & SERIAL_LOGGER_SHOW_TIME) { + vAsciiStringAdd(str, &strLen, &timeString[11], 8); + vAsciiStringAdd(str, &strLen, " ", 1); + } + } + + if (env->flags & SERIAL_LOGGER_SHOW_AUTHOR) { + //выводим автора сообщения + vAsciiStringAdd(str, &strLen, authorStatic, authorLen); + } + + if (env->flags & SERIAL_LOGGER_SHOW_LOG_LEVEL) { + tStringStatic levelName = LOGGER_LEVEL_NAMES[loglevel]; + //выводим уровень сообщения + vAsciiStringAddChar(str, &strLen, '['); + vAsciiStringAdd(str, &strLen, levelName.data, levelName.length); + vAsciiStringAddChar(str, &strLen, ']'); + } + + + //разделитель + if (env->flags) + vAsciiStringAdd(str, &strLen, ": ", 2); + + SerialPortTransmit(env->serialPortIo_VIRT, (uint8_t *) str, strLen, env->timeout); + + env->open = true; +} + +static void LoggerToSerialPort_Logging( + tLoggerToSerialPortV2 *env, + char *authorStatic, + const uint8_t authorLen, + eLoggerLevel loglevel, + char *msg, + uint16_t msgLen, + bool complete +) { + +// uint32_t adr = *(uint32_t *)(env->serialPortIo->env); + +// uint32_t timeoutLocal = env->timeout; + // Если это UART (виртуальный) +// if (adr < PERIPH_BASE) { +// env->timeout = 0; +// } + + if (!env->open) { + LoggerToSerialPort_PrintLegend(env, authorStatic, authorLen, loglevel); + } + + //выводим сообщение + SerialPortTransmit(env->serialPortIo_VIRT, (uint8_t *) msg, msgLen, env->timeout); + + if (complete) { + //переводим строку + SerialPortTransmit(env->serialPortIo_VIRT, (uint8_t *) "\r\n", 2, env->timeout); + env->open = false; + } else { + env->open = true; + } + +// env->timeout = timeoutLocal; +} + + +void LoggerToSerialPortV2_Init( + tLoggerToSerialPortV2 *env, + int32_t greenwichOffset, + tSerialPortIO *serialPortIo_VIRT, + tSerialPortIO *serialPortIo_PHYSIC, + tRtcIO *rtc, + uint16_t flags, + uint32_t timeoutTransmittedLog +) { + env->serialPortIo_VIRT = serialPortIo_VIRT; + env->serialPortIo_PHYSIC = serialPortIo_PHYSIC; + env->open = false; + env->timeout = 0;// не ждем на каждой операции так как если порт исправен то все должно отрабатывать моментально + env->logger.env = env; + env->greenwichOffset = greenwichOffset; + env->logger.logging = (LoggerGenericMethod) LoggerToSerialPort_Logging; + env->logger.getMemPrintfBufLog = (getMemPrintfBufLogMethod) getMemLoggerBufLog; + env->rtc = rtc; + env->flags = flags; + env->timeoutTransmittedLog = timeoutTransmittedLog; +// env->showDate = showDate; +// env->showTime = showTime; +// env->showLoglevel = showLoglevel; +// env->showAuthor = showAuthor; + + InitThreadAtrStatic(&env->thread.attr, "LogTransmitter", env->thread.controlBlock, env->thread.stack, + osPriorityNormal); + + LogTransmitter_StartThread(env); +} + + +static _Noreturn void LogTransmitter_Thread(tLoggerToSerialPortV2 *env) { + for (;;) { + uint16_t len = env->serialPortIo_VIRT->receive(env->serialPortIo_VIRT->env, env->buf_LOG, + sizeof(env->buf_LOG), env->timeoutTransmittedLog); + + if (len > 0) { + len = env->serialPortIo_PHYSIC->transmit(env->serialPortIo_PHYSIC->env, env->buf_LOG, + len, env->timeoutTransmittedLog); + } + } +} + + +void LogTransmitter_StartThread(tLoggerToSerialPortV2 *env) { + if (!env->thread.id) { + env->thread.id = osThreadNew((osThreadFunc_t) (LogTransmitter_Thread), (void *) (env), &env->thread.attr); + } +} \ No newline at end of file diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..f82041d --- /dev/null +++ b/modular.json @@ -0,0 +1,37 @@ +{ + "dep": [ + { + "type": "git", + "provider": "HVAC_DEV", + "repo": "RtcInterface" + }, + { + "type": "git", + "provider": "HVAC_DEV", + "repo": "Rtc" + }, + { + "type": "git", + "provider": "HVAC_DEV", + "repo": "SerialPort" + }, + { + "type": "git", + "provider": "HVAC_DEV", + "repo": "LoggerInterface" + }, + { + "type": "git", + "provider": "HVAC_DEV", + "repo": "AsciiStringAssemblingUtils" + } + ], + "cmake": { + "inc_dirs": [ + "Inc" + ], + "srcs": [ + "Src/**.c" + ] + } +} \ No newline at end of file