From 874ebf51ba28a978c21eb4153d04f89ab0c03572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20Schr=C3=A4er?= Date: Tue, 20 Jan 2026 13:24:56 +0100 Subject: [PATCH] Settings-Datei fortgesetzt --- can/can_client.c | 4 +-- main.c | 7 ++++-- main.h | 1 + settings/settings.c | 61 ++++++++++++++++++++++++++++++++++++++++++++- settings/settings.h | 11 ++++++++ 5 files changed, 78 insertions(+), 6 deletions(-) diff --git a/can/can_client.c b/can/can_client.c index d14e7fb..4f23f0c 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -6,8 +6,6 @@ #include #include -int iCanSimu = 0; - struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT]; struct CAN_INTERFACE_DATA intf_data[MAX_MOTOR_COUNT]; @@ -55,7 +53,7 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename) Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL); Can_SetMotorPower(iMotorIndex, 0); - if (iCanSimu) + if (settings.iCanSimu) { mylog(LOG_INFO, "CAN: Using simulation mode (motor %d).", iMotorIndex); intf_data[iMotorIndex].socket = -1; diff --git a/main.c b/main.c index fa6c88a..8804e8f 100755 --- a/main.c +++ b/main.c @@ -166,10 +166,10 @@ void *thread_func(void *data) while (iThreadControl == 0) { pinfo.cyclecounter++; - if (pinfo.cyclecounter >= 86400000) + if (pinfo.cyclecounter > CYCLE_COUNTER_MAX) { // Reset cycle counter every 24h - pinfo.cyclecounter = 0; + pinfo.cyclecounter = 1; } do_cyclic_1ms(&pinfo); wait_rest_of_period(&pinfo); @@ -219,6 +219,9 @@ int main(int argc, char* argv[]) openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON); mylog(LOG_INFO, "Service started. PID: %d", getpid()); + // Read the settings file after opening the log + Settings_ReadConfFile(); + // catch signals if (signal(SIGTERM, sig_handler) == SIG_ERR) { diff --git a/main.h b/main.h index 0aead00..6df4fa6 100755 --- a/main.h +++ b/main.h @@ -14,6 +14,7 @@ #include #include +#define CYCLE_COUNTER_MAX 86400000 struct period_info { diff --git a/settings/settings.c b/settings/settings.c index e428524..3708564 100644 --- a/settings/settings.c +++ b/settings/settings.c @@ -2,6 +2,7 @@ #include #include + struct APP_SETTINGS settings; void Settings_InitDefaultValues() @@ -15,9 +16,67 @@ void Settings_InitDefaultValues() // #define LOG_INFO 6 /* informational */ // #define LOG_DEBUG 7 /* debug-level messages */ settings.iDebugLevel = LOG_INFO; - + settings.iCanSimu = 0; + settings.iMotorCount = 1; settings.iMotorPwrMinRaw = 38; settings.iMotorPwrMaxRaw = 250; settings.iMotorPwrStepCount = 7; + + // Get path of the executable itself + ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1); + if (length >= 0) + { + settings.sExePath[length] = '\0'; + mylog(LOG_INFO, "SETTINGS: Executable path: %s", settings.sExePath); + } + else + { + settings.sExePath[0] = '\0'; + mylog(LOG_ERR, "SETTINGS: Executable path not found!"); + } +} + + +void Settings_ReadConfFile() +{ + const char *filename = "/etc/CanRtDriver.conf"; + + FILE *file = fopen(filename, "r"); + if (file == NULL) + { + mylog(LOG_ERR, "Failed to open settings file %s", filename); + return; + } + + char line[MAX_LINE_LENGTH]; + while (fgets(line, sizeof(line), file)) + { + // 1. Ignore comments and empty lines + if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') + { + continue; + } + + // 2. Remove CRLF at end of line + line[strcspn(line, "\r\n")] = 0; + + // 3. Split line to key and value + char *key = strtok(line, "="); + char *value = strtok(NULL, "="); + + if ((key != NULL) && (value != NULL)) + { + mylog(LOG_DEBUG, "SETTINGS: Found key: '%s' | value: '%s'\n", key, value); + + // // Beispiel: Wert verarbeiten + // if (strcmp(key, "port") == 0) + // { + // int port = atoi(value); + // printf(" [System] Port auf %d gesetzt.\n", port); + // } + } + } + + fclose(file); } diff --git a/settings/settings.h b/settings/settings.h index 664b7f2..700b8d3 100644 --- a/settings/settings.h +++ b/settings/settings.h @@ -1,9 +1,19 @@ #if !defined(__SETTINGS_H__) #define __SETTINGS_H__ +#include +#include +#include +#include +#include + +#define MAX_LINE_LENGTH 256 + struct APP_SETTINGS { int iDebugLevel; // Level of debug messages + char sExePath[MAX_PATH]; // Path of the executable + int iCanSimu = 0; // Simulate CAN if 1 int iMotorCount; // Number of used motors (1 or 2) int iMotorPwrMinRaw; // Minimum power value for motor (raw value) @@ -13,5 +23,6 @@ struct APP_SETTINGS extern struct APP_SETTINGS settings; void Settings_InitDefaultValues(); +void Settings_ReadConfFile(); #endif