Settings-Datei fortgesetzt

This commit is contained in:
Bernhard Schräer
2026-01-20 13:24:56 +01:00
parent b5f7b5a45d
commit 874ebf51ba
5 changed files with 78 additions and 6 deletions

View File

@@ -6,8 +6,6 @@
#include <io/io.h> #include <io/io.h>
#include <mqtt/mqtt_client.h> #include <mqtt/mqtt_client.h>
int iCanSimu = 0;
struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT]; struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT];
struct CAN_INTERFACE_DATA intf_data[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_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL);
Can_SetMotorPower(iMotorIndex, 0); Can_SetMotorPower(iMotorIndex, 0);
if (iCanSimu) if (settings.iCanSimu)
{ {
mylog(LOG_INFO, "CAN: Using simulation mode (motor %d).", iMotorIndex); mylog(LOG_INFO, "CAN: Using simulation mode (motor %d).", iMotorIndex);
intf_data[iMotorIndex].socket = -1; intf_data[iMotorIndex].socket = -1;

7
main.c
View File

@@ -166,10 +166,10 @@ void *thread_func(void *data)
while (iThreadControl == 0) while (iThreadControl == 0)
{ {
pinfo.cyclecounter++; pinfo.cyclecounter++;
if (pinfo.cyclecounter >= 86400000) if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
{ {
// Reset cycle counter every 24h // Reset cycle counter every 24h
pinfo.cyclecounter = 0; pinfo.cyclecounter = 1;
} }
do_cyclic_1ms(&pinfo); do_cyclic_1ms(&pinfo);
wait_rest_of_period(&pinfo); wait_rest_of_period(&pinfo);
@@ -219,6 +219,9 @@ int main(int argc, char* argv[])
openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON); openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON);
mylog(LOG_INFO, "Service started. PID: %d", getpid()); mylog(LOG_INFO, "Service started. PID: %d", getpid());
// Read the settings file after opening the log
Settings_ReadConfFile();
// catch signals // catch signals
if (signal(SIGTERM, sig_handler) == SIG_ERR) if (signal(SIGTERM, sig_handler) == SIG_ERR)
{ {

1
main.h
View File

@@ -14,6 +14,7 @@
#include <syslog.h> #include <syslog.h>
#include <stdarg.h> #include <stdarg.h>
#define CYCLE_COUNTER_MAX 86400000
struct period_info struct period_info
{ {

View File

@@ -2,6 +2,7 @@
#include <main.h> #include <main.h>
#include <settings/settings.h> #include <settings/settings.h>
struct APP_SETTINGS settings; struct APP_SETTINGS settings;
void Settings_InitDefaultValues() void Settings_InitDefaultValues()
@@ -15,9 +16,67 @@ void Settings_InitDefaultValues()
// #define LOG_INFO 6 /* informational */ // #define LOG_INFO 6 /* informational */
// #define LOG_DEBUG 7 /* debug-level messages */ // #define LOG_DEBUG 7 /* debug-level messages */
settings.iDebugLevel = LOG_INFO; settings.iDebugLevel = LOG_INFO;
settings.iCanSimu = 0;
settings.iMotorCount = 1; settings.iMotorCount = 1;
settings.iMotorPwrMinRaw = 38; settings.iMotorPwrMinRaw = 38;
settings.iMotorPwrMaxRaw = 250; settings.iMotorPwrMaxRaw = 250;
settings.iMotorPwrStepCount = 7; 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);
} }

View File

@@ -1,9 +1,19 @@
#if !defined(__SETTINGS_H__) #if !defined(__SETTINGS_H__)
#define __SETTINGS_H__ #define __SETTINGS_H__
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 256
struct APP_SETTINGS struct APP_SETTINGS
{ {
int iDebugLevel; // Level of debug messages 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 iMotorCount; // Number of used motors (1 or 2)
int iMotorPwrMinRaw; // Minimum power value for motor (raw value) int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
@@ -13,5 +23,6 @@ struct APP_SETTINGS
extern struct APP_SETTINGS settings; extern struct APP_SETTINGS settings;
void Settings_InitDefaultValues(); void Settings_InitDefaultValues();
void Settings_ReadConfFile();
#endif #endif