Settings-Datei fortgesetzt
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <main.h>
|
||||
#include <settings/settings.h>
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
#if !defined(__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
|
||||
{
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user