153 lines
4.6 KiB
C
Executable File
153 lines
4.6 KiB
C
Executable File
|
|
#include <main.h>
|
|
#include <settings/settings.h>
|
|
#include <ctype.h>
|
|
|
|
struct APP_SETTINGS settings;
|
|
|
|
void Settings_InitDefaultValues()
|
|
{
|
|
// #define LOG_EMERG 0 /* system is unusable */
|
|
// #define LOG_ALERT 1 /* action must be taken immediately */
|
|
// #define LOG_CRIT 2 /* critical conditions */
|
|
// #define LOG_ERR 3 /* error conditions */
|
|
// #define LOG_WARNING 4 /* warning conditions */
|
|
// #define LOG_NOTICE 5 /* normal but significant condition */
|
|
// #define LOG_INFO 6 /* informational */
|
|
// #define LOG_DEBUG 7 /* debug-level messages */
|
|
settings.iDebugLevel = LOG_INFO;
|
|
settings.iCanSimu = 1;
|
|
|
|
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!");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
char filename[MAX_PATH + 50];
|
|
sprintf(filename, "%s.conf", settings.sExePath);
|
|
FILE *file = fopen(filename, "r");
|
|
if (file == NULL)
|
|
{
|
|
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))
|
|
{
|
|
// 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 = trim_str(strtok(line, "="));
|
|
char *value = trim_str(strtok(NULL, "="));
|
|
|
|
if ((key != NULL) && (value != NULL))
|
|
{
|
|
//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)
|
|
// {
|
|
// int port = atoi(value);
|
|
// printf(" [System] Port auf %d gesetzt.\n", port);
|
|
// }
|
|
}
|
|
}
|
|
|
|
fclose(file);
|
|
}
|