Settings-Datei lesen fertig
This commit is contained in:
86
settings/settings.c
Normal file → Executable file
86
settings/settings.c
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
|
||||
#include <main.h>
|
||||
#include <settings/settings.h>
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
struct APP_SETTINGS settings;
|
||||
|
||||
@@ -16,7 +16,7 @@ void Settings_InitDefaultValues()
|
||||
// #define LOG_INFO 6 /* informational */
|
||||
// #define LOG_DEBUG 7 /* debug-level messages */
|
||||
settings.iDebugLevel = LOG_INFO;
|
||||
settings.iCanSimu = 0;
|
||||
settings.iCanSimu = 1;
|
||||
|
||||
settings.iMotorCount = 1;
|
||||
settings.iMotorPwrMinRaw = 38;
|
||||
@@ -38,17 +38,58 @@ void Settings_InitDefaultValues()
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *trim_str(const char *s)
|
||||
{
|
||||
// Führende Leerzeichen finden
|
||||
while (isspace((unsigned char)*s))
|
||||
s++;
|
||||
|
||||
// Falls der String leer ist
|
||||
if (*s == 0)
|
||||
return strdup("");
|
||||
|
||||
// Letztes Zeichen finden
|
||||
const char *end = s + strlen(s) - 1;
|
||||
while (end > s && isspace((unsigned char)*end))
|
||||
end--;
|
||||
|
||||
// Länge des neuen Strings berechnen
|
||||
size_t len = (end - s) + 1;
|
||||
|
||||
// Speicher reservieren (+1 für das Null-Byte)
|
||||
char *new_str = malloc(len + 1);
|
||||
if (new_str)
|
||||
{
|
||||
memcpy(new_str, s, len);
|
||||
new_str[len] = '\0';
|
||||
}
|
||||
|
||||
return new_str;
|
||||
}
|
||||
|
||||
void Settings_ReadConfFile()
|
||||
{
|
||||
const char *filename = "/etc/CanRtDriver.conf";
|
||||
|
||||
//const char *filename = "/etc/CanRtDriver.conf";
|
||||
|
||||
char filename[MAX_PATH + 50];
|
||||
sprintf(filename, "%s.conf", settings.sExePath);
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
mylog(LOG_ERR, "Failed to open settings file %s", filename);
|
||||
return;
|
||||
mylog(LOG_INFO, "SETTINGS: File %s noch found", filename);
|
||||
sprintf(filename, "/etc/CanRtDriver.conf");
|
||||
file = fopen(filename, "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
mylog(LOG_ERR, "SETTINGS: No conf file found!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mylog(LOG_INFO, "SETTINGS: Reading %s", filename);
|
||||
|
||||
char line[MAX_LINE_LENGTH];
|
||||
while (fgets(line, sizeof(line), file))
|
||||
{
|
||||
@@ -62,13 +103,42 @@ void Settings_ReadConfFile()
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
|
||||
// 3. Split line to key and value
|
||||
char *key = strtok(line, "=");
|
||||
char *value = strtok(NULL, "=");
|
||||
char *key = trim_str(strtok(line, "="));
|
||||
char *value = trim_str(strtok(NULL, "="));
|
||||
|
||||
if ((key != NULL) && (value != NULL))
|
||||
{
|
||||
mylog(LOG_DEBUG, "SETTINGS: Found key: '%s' | value: '%s'\n", key, value);
|
||||
//mylog(LOG_INFO, "SETTINGS: Found key: '%s' | value: '%s'", key, value);
|
||||
|
||||
if (strcmp(key, "DebugLevel") == 0)
|
||||
{
|
||||
settings.iDebugLevel = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel);
|
||||
}
|
||||
else if (strcmp(key, "CanSimu") == 0)
|
||||
{
|
||||
settings.iCanSimu = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iCanSimu);
|
||||
}
|
||||
else if (strcmp(key, "MotorPowerMinRaw") == 0)
|
||||
{
|
||||
settings.iMotorPwrMinRaw = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMinRaw);
|
||||
}
|
||||
else if (strcmp(key, "MotorPowerMaxRaw") == 0)
|
||||
{
|
||||
settings.iMotorPwrMaxRaw = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMaxRaw);
|
||||
}
|
||||
else if (strcmp(key, "MotorPowerStepCount") == 0)
|
||||
{
|
||||
settings.iMotorPwrStepCount = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrStepCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "SETTING: Unknown key: %s", key);
|
||||
}
|
||||
// // Beispiel: Wert verarbeiten
|
||||
// if (strcmp(key, "port") == 0)
|
||||
// {
|
||||
|
||||
3
settings/settings.h
Normal file → Executable file
3
settings/settings.h
Normal file → Executable file
@@ -8,12 +8,13 @@
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_LINE_LENGTH 256
|
||||
#define MAX_PATH 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 iCanSimu; // Simulate CAN if 1
|
||||
|
||||
int iMotorCount; // Number of used motors (1 or 2)
|
||||
int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
|
||||
|
||||
Reference in New Issue
Block a user