Fix sprintf overflows, integer sign handling, and clean up dead code

- Replace all sprintf with snprintf for buffer safety
- Fix rgI32/rgI16 sign extension using int32_t/int16_t casts
- Fix rgU32/rgU16 to use proper uint types and %u format
- Remove unused mbReg_Orno[] array and individual influx_send_* functions
- Fix MQTT topic for voltage fluctuation to match /energy/orno/ convention

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ms
2026-02-14 20:38:45 +01:00
parent 44aceebae6
commit 2b16211ef2

View File

@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <modbus/modbus-rtu.h>
#include <errno.h>
@@ -125,19 +126,6 @@ t_mb_reg mbReg[] = {
};
t_mb_reg mbReg_Orno[] = {
{"L1 Volt", 0x000e, 2, "L1 Volt", 1, rgU32, "V", "info", "hold", ""},
{"L2 Volt", 0x0010, 2, "L2 Volt", 1, rgU32, "V", "info", "hold", ""},
{"L3 Volt", 0x0012, 2, "L3 Volt", 10, rgU32, "V", "info", "hold", ""},
{"Tot Power", 0x001c, 2, "Tot Power", 1, rgU32, "kW", "info", "hold", ""},
{"L1 Tot Power", 0x001e, 2, "L1 Tot Power", 1, rgU32, "kW", "info", "hold", ""},
{"L2 Tot Power", 0x0020, 2, "L2 Tot Power", 1, rgU32, "kW", "info", "hold", ""},
{"L3 Tot Power", 0x0022, 2, "L3 Tot Power", 1, rgU32, "kW", "info", "hold", ""},
{"Tot Energ", 0x0100, 2, "Tot Energ", 1, rgU32, "kWh", "info", "hold", ""},
{"L1 Tot Energ", 0x0102, 2, "L1 Tot Energ", 1, rgU32, "kWh", "info", "hold", ""},
{"L2 Tot Energ", 0x0104, 2, "L2 Tot Energ", 1, rgU32, "kWh", "info", "hold", ""},
{"L3 Tot Energ", 0x0106, 2, "L3 Tot Energ", 1, rgU32, "kWh", "info", "hold", ""}};
void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
@@ -337,35 +325,6 @@ int influx_send_post(char *data)
return 0;
}
int influx_send_U(float U1, float U2, float U3) {
char line[256];
snprintf(line, sizeof(line), "orno,device=orno,pomiar=voltage L1=%.2f,L2=%.2f,L3=%.2f", U1, U2, U3);
return influx_send_post(line);
}
int influx_send_I(float I1, float I2, float I3) {
char line[256];
snprintf(line, sizeof(line), "orno,device=orno,pomiar=current L1=%.2f,L2=%.2f,L3=%.2f", I1, I2, I3);
return influx_send_post(line);
}
int influx_send_P(float P_Tot, float P1, float P2, float P3) {
char line[256];
snprintf(line, sizeof(line), "orno,device=orno,pomiar=power total=%.3f,L1=%.3f,L2=%.3f,L3=%.3f", P_Tot, P1, P2, P3);
return influx_send_post(line);
}
int influx_send_W(float W_Tot, float W1, float W2, float W3) {
char line[256];
snprintf(line, sizeof(line), "orno,device=orno,pomiar=energy total=%.3f,L1=%.3f,L2=%.3f,L3=%.3f", W_Tot, W1, W2, W3);
return influx_send_post(line);
}
int influx_send_Hz(float Hz) {
char line[256];
snprintf(line, sizeof(line), "orno,device=orno,pomiar=frequency value=%.4f", Hz);
return influx_send_post(line);
}
int influx_send_orno_batch(s_voltage *v, int v_ok, s_current *i, int i_ok,
s_power *p, int p_ok, s_energy *w, int w_ok,
@@ -627,7 +586,7 @@ int main(int argc, char *argv[])
fabs(current_voltage.U2 - avg_L2) > avg_L2 * VOLTAGE_FLUCTUATION_THRESHOLD ||
fabs(current_voltage.U3 - avg_L3) > avg_L3 * VOLTAGE_FLUCTUATION_THRESHOLD) {
printf("ORNO: Voltage fluctuation detected! Switching to high frequency polling for %d seconds.\n", HIGH_FREQ_MODE_DURATION);
mqtt_send("fluct/start", "on");
mqtt_send("/energy/orno/fluct", "on");
influx_send_post("orno,device=orno,highfluct=start value=true");
high_frequency_mode_end_time = time(NULL) + HIGH_FREQ_MODE_DURATION;
}
@@ -650,6 +609,7 @@ int main(int argc, char *argv[])
s_energy send_energy;
s_frequency send_freq;
// VOLTAGE check
int spike_U = 0;
if (prev_voltage.U1 != 0 && (current_voltage.U1 > prev_voltage.U1 * 1.8 || current_voltage.U1 < prev_voltage.U1 * 0.2)) spike_U = 1;
if (prev_voltage.U2 != 0 && (current_voltage.U2 > prev_voltage.U2 * 1.8 || current_voltage.U2 < prev_voltage.U2 * 0.2)) spike_U = 1;
@@ -663,6 +623,7 @@ int main(int argc, char *argv[])
prev_voltage = current_voltage;
}
// CURRENT check
int spike_I = 0;
if (prev_current.I1 != 0 && (current_current.I1 > prev_current.I1 * 1.8 || current_current.I1 < prev_current.I1 * 0.2)) spike_I = 1;
if (prev_current.I2 != 0 && (current_current.I2 > prev_current.I2 * 1.8 || current_current.I2 < prev_current.I2 * 0.2)) spike_I = 1;
@@ -676,6 +637,7 @@ int main(int argc, char *argv[])
prev_current = current_current;
}
// POWER check
int spike_P = 0;
if (prev_power.P_Tot != 0 && (fabs(current_power.P_Tot - prev_power.P_Tot) > fabs(prev_power.P_Tot) * 0.8)) spike_P = 1;
@@ -687,6 +649,7 @@ int main(int argc, char *argv[])
prev_power = current_power;
}
// ENERGY check (should only increase)
int spike_W = 0;
if (prev_energy.W_Tot != 0 && (current_energy.W_Tot > prev_energy.W_Tot * 1.8 || current_energy.W_Tot < prev_energy.W_Tot)) spike_W = 1;
@@ -698,6 +661,7 @@ int main(int argc, char *argv[])
prev_energy = current_energy;
}
// FREQUENCY check (5% threshold)
int spike_F = 0;
if (prev_freq.Freq != 0 && (current_freq.Freq > prev_freq.Freq * 1.05 || current_freq.Freq < prev_freq.Freq * 0.95)) spike_F = 1; // 5% for frequency
@@ -709,6 +673,8 @@ int main(int argc, char *argv[])
prev_freq = current_freq;
}
// --- Now send the selected values ---
int valid_U = 0, valid_I = 0, valid_P = 0, valid_W = 0, valid_F = 0;
printf("ORNO: Voltages: L1=%.1f V, L2=%.1f V, L3=%.1f V\n", send_voltage.U1, send_voltage.U2, send_voltage.U3);
@@ -863,81 +829,77 @@ int main(int argc, char *argv[])
time_t td = reg[0] * 65536 + reg[1];
timeinfo = localtime(&td);
printf("%s", asctime(timeinfo));
sprintf(str_buf, "\"%s\"", asctime(timeinfo));
snprintf(str_buf, sizeof(str_buf), "\"%s\"", asctime(timeinfo));
str_buf[strlen(str_buf) - 2] = '"';
str_buf[strlen(str_buf) - 1] = '\0';
break;
}
case rgI32:
{
int dh = reg[0];
int dl = reg[1];
int d = (dh << 16) + dl;
int32_t d = (int32_t)((uint32_t)reg[0] << 16 | reg[1]);
if (mbReg[i].scale == 1)
{
printf("%i", d);
sprintf(str_buf, "%i", d);
snprintf(str_buf, sizeof(str_buf), "%i", d);
}
else
{
double f = d;
f /= mbReg[i].scale;
printf("%lf", f);
sprintf(str_buf, "%lf", f);
snprintf(str_buf, sizeof(str_buf), "%lf", f);
}
break;
}
case rgI16:
{
int d = reg[0];
int16_t d = (int16_t)reg[0];
if (mbReg[i].scale == 1)
{
printf("%i", d);
sprintf(str_buf, "%i", d);
snprintf(str_buf, sizeof(str_buf), "%i", d);
}
else
{
double f = d;
f /= mbReg[i].scale;
printf("%lf", f);
sprintf(str_buf, "%lf", f);
snprintf(str_buf, sizeof(str_buf), "%lf", f);
}
break;
}
case rgU32:
{
unsigned int dh = reg[0];
unsigned int dl = reg[1];
unsigned int d = (dh << 16) + dl;
uint32_t d = (uint32_t)reg[0] << 16 | reg[1];
if (mbReg[i].scale == 1)
{
printf("%i", d);
sprintf(str_buf, "%i", d);
printf("%u", d);
snprintf(str_buf, sizeof(str_buf), "%u", d);
}
else
{
double f = d;
f /= mbReg[i].scale;
printf("%lf", f);
sprintf(str_buf, "%lf", f);
snprintf(str_buf, sizeof(str_buf), "%lf", f);
}
break;
}
case rgU16:
{
unsigned int d = reg[0];
uint16_t d = reg[0];
if (mbReg[i].scale == 1)
{
printf("%i", d);
sprintf(str_buf, "%i", d);
printf("%u", d);
snprintf(str_buf, sizeof(str_buf), "%u", d);
}
else
{
double f = d;
f /= mbReg[i].scale;
printf("%lf", f);
sprintf(str_buf, "%lf", f);
snprintf(str_buf, sizeof(str_buf), "%lf", f);
}
break;
}
@@ -946,7 +908,7 @@ int main(int argc, char *argv[])
for (int j = 0; j < mbReg[i].num_reg; j++)
{
printf("%i:", reg[j]);
sprintf(str_buf, "%i", reg[j]);
snprintf(str_buf, sizeof(str_buf), "%i", reg[j]);
}
break;
}