diff --git a/can/can_client.c b/can/can_client.c index 4f23f0c..5ac8bbc 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -55,7 +55,7 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename) if (settings.iCanSimu) { - mylog(LOG_INFO, "CAN: Using simulation mode (motor %d).", iMotorIndex); + mylog(LOG_WARNING, "CAN: Using simulation mode (motor %d).", iMotorIndex); intf_data[iMotorIndex].socket = -1; motctrl[iMotorIndex].nSwitchState = 0xF5; motctrl[iMotorIndex].nDriveConnected = 1; diff --git a/io/io.c b/io/io.c index 24b3df2..a2db865 100755 --- a/io/io.c +++ b/io/io.c @@ -51,7 +51,7 @@ void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin) if ((pdata->iKeyPin > 0) && nInitialized) { - //mylog(LOG_INFO, "IO: Config Pin %d as input", iOutPin); + mylog(LOG_DEBUG, "IO: Config Pin %d as input", pdata->iKeyPin); pinMode(pdata->iKeyPin, INPUT); pullUpDnControl(pdata->iKeyPin, PUD_UP); } @@ -154,7 +154,7 @@ void SetupOutputPin(int iOutPin) { if ((iOutPin > 0) && nInitialized) { - //mylog(LOG_INFO, "IO: Config Pin %d as output", iOutPin); + mylog(LOG_DEBUG, "IO: Config Pin %d as output", iOutPin); pinMode(iOutPin, OUTPUT); digitalWrite(iOutPin, LOW); } diff --git a/main.c b/main.c index 8804e8f..19c9a45 100755 --- a/main.c +++ b/main.c @@ -17,7 +17,7 @@ int iLogToConsole = 1; /// @param void mylog(int prio, const char *format, ...) { - if (prio >= settings.iDebugLevel) + if (prio <= settings.iDebugLevel) { va_list args; diff --git a/mqtt/mqtt_client.c b/mqtt/mqtt_client.c index 5cd3e1b..0493af8 100755 --- a/mqtt/mqtt_client.c +++ b/mqtt/mqtt_client.c @@ -55,7 +55,7 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo int val = 9999; if (sscanf(topic_value, "%d", &val)) { - mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_gear: %d", val); + mylog(LOG_DEBUG, "MQTT: Received value for mqtt_topic_motor1_gear: %d", val); iMqttMotor1Gear = val; Can_SetMotorGear(0, val); } @@ -69,7 +69,7 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo int val = 9999; if (sscanf(topic_value, "%d", &val)) { - mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_power: %d", val); + mylog(LOG_DEBUG, "MQTT: Received value for mqtt_topic_motor1_power: %d", val); iMqttMotor1Power = val; Can_SetMotorPower(0, val); } @@ -83,7 +83,7 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo int val = 9999; if (sscanf(topic_value, "%d", &val)) { - mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor2_gear: %d", val); + mylog(LOG_DEBUG, "MQTT: Received value for mqtt_topic_motor2_gear: %d", val); iMqttMotor2Gear = val; Can_SetMotorGear(1, val); } @@ -97,7 +97,7 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo int val = 9999; if (sscanf(topic_value, "%d", &val)) { - mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor2_power: %d", val); + mylog(LOG_DEBUG, "MQTT: Received value for mqtt_topic_motor2_power: %d", val); iMqttMotor2Power = val; Can_SetMotorPower(1, val); } @@ -152,7 +152,7 @@ int MqttClient_Connect() { /* Wenn wir Verbindungsfehler hatten, dann befinden wir uns wohl in Boot-Prozess und der Mosquitto ist gerade erst gestartet. Wir müssen hier etwas warten, sonst funktioniert das Subscriben nicht */ - sleep(10); + sleep(500); } // publish all topics we want to subscribe @@ -226,7 +226,7 @@ int MqttClient_Connect() iMqttMotor2ActualPowerW = -1; MqttClient_Publish_MotorActualPowerW(1, 0); - mylog(LOG_INFO, "MQTT: Connected successfull!"); + mylog(LOG_INFO, "MQTT: Connected successfull after %d errors!", iHadConnectError); return 0; } diff --git a/settings/settings.c b/settings/settings.c old mode 100644 new mode 100755 index 3708564..81f896f --- a/settings/settings.c +++ b/settings/settings.c @@ -1,7 +1,7 @@ #include #include - +#include 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) // { diff --git a/settings/settings.h b/settings/settings.h old mode 100644 new mode 100755 index 700b8d3..0506593 --- a/settings/settings.h +++ b/settings/settings.h @@ -8,12 +8,13 @@ #include #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)