156 lines
3.8 KiB
C
156 lines
3.8 KiB
C
//
|
|
// Created by cfif on 18.02.23.
|
|
//
|
|
|
|
#include <stddef.h>
|
|
#include <SystemDelayInterface.h>
|
|
#include <ModemMain.h>
|
|
#include <CmsisRtosThreadUtils.h>
|
|
#include "SerialPort.h"
|
|
#include "stdlib.h"
|
|
#include "string.h"
|
|
#include "httpd_types.h"
|
|
#include "httpd_base_func.h"
|
|
#include "fs_base_func.h"
|
|
#include "math.h"
|
|
#include "fs_base_func.h"
|
|
#include "file_logger.h"
|
|
#include "ModemLog.h"
|
|
|
|
/*
|
|
void GsmWithGnss_Urc(tModemMainLog *env, tAtBuffer *buff) {
|
|
|
|
if (buff->data[0] == '\r')
|
|
return;
|
|
|
|
LoggerInfoRaw(LOGGER, LOG_SIGN, buff->data, buff->len);
|
|
}
|
|
*/
|
|
void ModemMainLog_Init(
|
|
tModemMainLog *env,
|
|
tDeviceStorageIni *store,
|
|
tSerialPortIO *io,
|
|
tRtcIO *rtcIO,
|
|
tFs *fs,
|
|
osMutexId_t access_log
|
|
) {
|
|
|
|
env->io = io;
|
|
env->fs = fs;
|
|
env->rtcIO = rtcIO;
|
|
env->access = access_log;
|
|
env->store = store;
|
|
|
|
env->timestampWriteFileLogger = 0;
|
|
|
|
// AtCmdInit(
|
|
// &env->modemAt, io,
|
|
// env->mem.modemTx, sizeof(env->mem.modemTx),
|
|
// env->mem.modemRx, sizeof(env->mem.modemRx),
|
|
// 1000, 1000
|
|
// );
|
|
|
|
// AtCmdSetUrcProcessor(&env->modemAt, env, GsmWithGnss_Urc);
|
|
|
|
//Инициализируем главный поток
|
|
InitThreadAtrStatic(&env->thread.attr, "ModemMainLog", env->thread.controlBlock, env->thread.stack, osPriorityNormal);
|
|
env->thread.id = 0;
|
|
}
|
|
|
|
uint32_t getStateWriteLog2(tModemMainLog *env) {
|
|
return env->timestampWriteFileLogger;
|
|
}
|
|
|
|
static uint16_t FileLoggerRaw_Write(tModemMainLog *env, uint32_t size) {
|
|
|
|
FIL file;
|
|
FRESULT fr;
|
|
|
|
if (osMutexAcquire(env->access, 8000) == osOK) {
|
|
|
|
env->filename[0] = '\0';
|
|
strcat(env->filename, dir_logs2);
|
|
|
|
|
|
time_t timestamp;
|
|
env->rtcIO->get(env->rtcIO, ×tamp);
|
|
timestamp += env->store->nvm.Settings_General.GMTcorr_v * 3600;
|
|
|
|
struct tm timestampTM;
|
|
localtime_r(×tamp, ×tampTM);
|
|
|
|
|
|
strftime(env->buf, sizeof(env->buf), "%d-%m-%y.LOG", ×tampTM);
|
|
strcat(env->filename, env->buf);
|
|
|
|
fr = f_open_i(env->fs, &file, (TCHAR *) env->filename, FA_WRITE | FA_OPEN_APPEND);
|
|
|
|
if (fr) {
|
|
osMutexRelease(env->access);
|
|
return 0;
|
|
}
|
|
|
|
UINT bytes_written;
|
|
fr = f_write_i(env->fs, &file, env->bufLog, size, &bytes_written);
|
|
|
|
if (fr) {
|
|
f_close_i(env->fs, &file);
|
|
osMutexRelease(env->access);
|
|
return 0;
|
|
}
|
|
|
|
fr = f_close_i(env->fs, &file);
|
|
if (fr) {
|
|
osMutexRelease(env->access);
|
|
return 0;
|
|
}
|
|
|
|
osMutexRelease(env->access);
|
|
|
|
return bytes_written;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
static _Noreturn void ModemMainLog_Thread(tModemMainLog *env) {
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
if (AtCmdReceiveNextLine(&env->modemAt, 0xffff) == AT_OK) {
|
|
if (AtCmdRxBeginWithStatic(&env->modemAt, "#RSSI")) {
|
|
AtCmdRxClear(&env->modemAt);
|
|
} else {
|
|
AtCmdProcessUnresolvedLine(&env->modemAt);
|
|
AtCmdRxClear(&env->modemAt);
|
|
}
|
|
}
|
|
*/
|
|
|
|
uint16_t len = SerialPortReceive(env->io, env->bufLog, sizeof(env->bufLog), 500);
|
|
|
|
if (len > 0) {
|
|
FileLoggerRaw_Write(env, len);
|
|
env->rtcIO->get(env->rtcIO, &env->timestampWriteFileLogger);
|
|
}
|
|
|
|
//LoggerInfo(LOGGER, LOG_SIGN, (char *)env->bufLog, len);
|
|
|
|
//uint8_t data[512];
|
|
//uint16_t len = SerialPortReceive(env->io, data, sizeof(data), 2000);
|
|
//LoggerInfoRaw(LOGGER, LOG_SIGN, (char *)data, len);
|
|
}
|
|
|
|
}
|
|
|
|
void ModemMainLog_StartThread(tModemMainLog *env) {
|
|
if (!env->thread.id) {
|
|
env->thread.id = osThreadNew((osThreadFunc_t) (ModemMainLog_Thread), (void *) (env), &env->thread.attr);
|
|
} else {
|
|
osThreadResume(env->thread.id);
|
|
}
|
|
}
|
|
|