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:
88
modb_orno3.c
88
modb_orno3.c
@@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <modbus/modbus-rtu.h>
|
#include <modbus/modbus-rtu.h>
|
||||||
#include <errno.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)
|
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;
|
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,
|
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,
|
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.U2 - avg_L2) > avg_L2 * VOLTAGE_FLUCTUATION_THRESHOLD ||
|
||||||
fabs(current_voltage.U3 - avg_L3) > avg_L3 * 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);
|
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");
|
influx_send_post("orno,device=orno,highfluct=start value=true");
|
||||||
high_frequency_mode_end_time = time(NULL) + HIGH_FREQ_MODE_DURATION;
|
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_energy send_energy;
|
||||||
s_frequency send_freq;
|
s_frequency send_freq;
|
||||||
|
|
||||||
|
// VOLTAGE check
|
||||||
int spike_U = 0;
|
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.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;
|
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;
|
prev_voltage = current_voltage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CURRENT check
|
||||||
int spike_I = 0;
|
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.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;
|
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;
|
prev_current = current_current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// POWER check
|
||||||
int spike_P = 0;
|
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;
|
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;
|
prev_power = current_power;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ENERGY check (should only increase)
|
||||||
int spike_W = 0;
|
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;
|
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;
|
prev_energy = current_energy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FREQUENCY check (5% threshold)
|
||||||
int spike_F = 0;
|
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
|
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
|
||||||
|
|
||||||
@@ -708,6 +672,8 @@ int main(int argc, char *argv[])
|
|||||||
send_freq = current_freq;
|
send_freq = current_freq;
|
||||||
prev_freq = current_freq;
|
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;
|
int valid_U = 0, valid_I = 0, valid_P = 0, valid_W = 0, valid_F = 0;
|
||||||
|
|
||||||
@@ -863,81 +829,77 @@ int main(int argc, char *argv[])
|
|||||||
time_t td = reg[0] * 65536 + reg[1];
|
time_t td = reg[0] * 65536 + reg[1];
|
||||||
timeinfo = localtime(&td);
|
timeinfo = localtime(&td);
|
||||||
printf("%s", asctime(timeinfo));
|
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) - 2] = '"';
|
||||||
str_buf[strlen(str_buf) - 1] = '\0';
|
str_buf[strlen(str_buf) - 1] = '\0';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case rgI32:
|
case rgI32:
|
||||||
{
|
{
|
||||||
int dh = reg[0];
|
int32_t d = (int32_t)((uint32_t)reg[0] << 16 | reg[1]);
|
||||||
int dl = reg[1];
|
|
||||||
int d = (dh << 16) + dl;
|
|
||||||
if (mbReg[i].scale == 1)
|
if (mbReg[i].scale == 1)
|
||||||
{
|
{
|
||||||
printf("%i", d);
|
printf("%i", d);
|
||||||
sprintf(str_buf, "%i", d);
|
snprintf(str_buf, sizeof(str_buf), "%i", d);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
double f = d;
|
double f = d;
|
||||||
f /= mbReg[i].scale;
|
f /= mbReg[i].scale;
|
||||||
printf("%lf", f);
|
printf("%lf", f);
|
||||||
sprintf(str_buf, "%lf", f);
|
snprintf(str_buf, sizeof(str_buf), "%lf", f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case rgI16:
|
case rgI16:
|
||||||
{
|
{
|
||||||
int d = reg[0];
|
int16_t d = (int16_t)reg[0];
|
||||||
if (mbReg[i].scale == 1)
|
if (mbReg[i].scale == 1)
|
||||||
{
|
{
|
||||||
printf("%i", d);
|
printf("%i", d);
|
||||||
sprintf(str_buf, "%i", d);
|
snprintf(str_buf, sizeof(str_buf), "%i", d);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
double f = d;
|
double f = d;
|
||||||
f /= mbReg[i].scale;
|
f /= mbReg[i].scale;
|
||||||
printf("%lf", f);
|
printf("%lf", f);
|
||||||
sprintf(str_buf, "%lf", f);
|
snprintf(str_buf, sizeof(str_buf), "%lf", f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case rgU32:
|
case rgU32:
|
||||||
{
|
{
|
||||||
unsigned int dh = reg[0];
|
uint32_t d = (uint32_t)reg[0] << 16 | reg[1];
|
||||||
unsigned int dl = reg[1];
|
|
||||||
unsigned int d = (dh << 16) + dl;
|
|
||||||
|
|
||||||
if (mbReg[i].scale == 1)
|
if (mbReg[i].scale == 1)
|
||||||
{
|
{
|
||||||
printf("%i", d);
|
printf("%u", d);
|
||||||
sprintf(str_buf, "%i", d);
|
snprintf(str_buf, sizeof(str_buf), "%u", d);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
double f = d;
|
double f = d;
|
||||||
f /= mbReg[i].scale;
|
f /= mbReg[i].scale;
|
||||||
printf("%lf", f);
|
printf("%lf", f);
|
||||||
sprintf(str_buf, "%lf", f);
|
snprintf(str_buf, sizeof(str_buf), "%lf", f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case rgU16:
|
case rgU16:
|
||||||
{
|
{
|
||||||
unsigned int d = reg[0];
|
uint16_t d = reg[0];
|
||||||
if (mbReg[i].scale == 1)
|
if (mbReg[i].scale == 1)
|
||||||
{
|
{
|
||||||
printf("%i", d);
|
printf("%u", d);
|
||||||
sprintf(str_buf, "%i", d);
|
snprintf(str_buf, sizeof(str_buf), "%u", d);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
double f = d;
|
double f = d;
|
||||||
f /= mbReg[i].scale;
|
f /= mbReg[i].scale;
|
||||||
printf("%lf", f);
|
printf("%lf", f);
|
||||||
sprintf(str_buf, "%lf", f);
|
snprintf(str_buf, sizeof(str_buf), "%lf", f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -946,7 +908,7 @@ int main(int argc, char *argv[])
|
|||||||
for (int j = 0; j < mbReg[i].num_reg; j++)
|
for (int j = 0; j < mbReg[i].num_reg; j++)
|
||||||
{
|
{
|
||||||
printf("%i:", reg[j]);
|
printf("%i:", reg[j]);
|
||||||
sprintf(str_buf, "%i", reg[j]);
|
snprintf(str_buf, sizeof(str_buf), "%i", reg[j]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user