155 lines
7.4 KiB
C
155 lines
7.4 KiB
C
/* DTU profile: physical COMTRADE samples and consistent energy address order,
|
|
* including real durable FRZ/FLOWREV storage and restart recovery. */
|
|
#include "iec104_example_recording.h"
|
|
#include "iec104_example_energy_meter.h"
|
|
#include "iec104_example_energy_freeze.h"
|
|
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
#ifndef IEC104_EXAMPLE_DTU_PROFILE
|
|
#error This test requires IEC104_EXAMPLE_DTU_PROFILE for every translation unit
|
|
#endif
|
|
|
|
#define TEST_UTC UINT64_C(1767196800000)
|
|
|
|
static ExampleRecording recording;
|
|
static ExampleEnergyFreeze energy;
|
|
|
|
static void Read_Energy_Xml(const char *directory, char *xml, size_t capacity)
|
|
{
|
|
ExampleEnergyFreezeEntry entry;
|
|
uint32_t size;
|
|
assert(Example_Energy_Freeze_Directory_Open(&energy, (const uint8_t *)directory, strlen(directory)));
|
|
assert(Example_Energy_Freeze_Directory_Count(&energy) == 1U);
|
|
assert(Example_Energy_Freeze_Directory_Get(&energy, 0U, &entry));
|
|
Example_Energy_Freeze_Directory_Close(&energy);
|
|
assert(Example_Energy_Freeze_Read_Open(&energy, (const uint8_t *)entry.name, strlen(entry.name), &size));
|
|
assert(size == entry.size && size < capacity);
|
|
assert(Example_Energy_Freeze_Read(&energy, 0U, (uint8_t *)xml, size));
|
|
xml[size] = '\0';
|
|
Example_Energy_Freeze_Read_Close(&energy);
|
|
}
|
|
|
|
static void Test_Recording(const char *parent)
|
|
{
|
|
IEC60870_CP56Time2a_t time;
|
|
IEC60870_FaultRecorderFrame_t frame;
|
|
double voltage_sum = 0.0, normal_sum = 0.0, fault_sum = 0.0;
|
|
unsigned index;
|
|
memset(&time, 0, sizeof(time));
|
|
time.time.year = 2026U; time.time.month = 1U; time.time.day = 1U;
|
|
time.time.hour = 8U; time.time.second = 1U;
|
|
assert(Example_Recording_Demo(&recording, &time, 1U, 1U));
|
|
assert(strstr(recording.cfg, "SIMULATION,DTU_100V1A,1999\r\n") != NULL);
|
|
assert(strstr(recording.cfg, ",100,100,S\r\n") != NULL);
|
|
assert(strstr(recording.cfg, ",1,1,S\r\n") != NULL);
|
|
assert(strstr(recording.cfg, "SW00_RECORD_START") != NULL);
|
|
for (index = 0U; index < 80U; ++index) {
|
|
assert(IEC60870_FaultRecorder_Get_Frame(&recording.recorder, index, &frame));
|
|
voltage_sum += frame.analog[0] * (double)frame.analog[0] / 10000.0;
|
|
normal_sum += frame.analog[4] * (double)frame.analog[4] / 1000000.0;
|
|
assert(frame.digital[0] == 0x0080U);
|
|
assert(IEC60870_FaultRecorder_Get_Frame(&recording.recorder, 320U + index, &frame));
|
|
fault_sum += frame.analog[4] * (double)frame.analog[4] / 1000000.0;
|
|
assert(frame.digital[0] == 0x0082U);
|
|
}
|
|
assert(fabs(sqrt(voltage_sum / 80.0) - 57.735) < 0.02);
|
|
assert(fabs(sqrt(normal_sum / 80.0) - 0.4) < 0.002);
|
|
assert(fabs(sqrt(fault_sum / 80.0) - 1.3) < 0.002);
|
|
assert(recording.dat_size == 24986U);
|
|
assert(Example_Recording_Save(&recording, parent));
|
|
for (index = 0U; index < 2U; ++index) {
|
|
char path[512];
|
|
uint8_t disk[137], expected[137];
|
|
size_t offset = 0U, size = index == 0U ? recording.cfg_size : recording.dat_size;
|
|
FILE *file;
|
|
int count = snprintf(path, sizeof(path), "%s/%s", parent, recording.paths[index]);
|
|
assert(count > 0 && (size_t)count < sizeof(path));
|
|
file = fopen(path, "rb"); assert(file != NULL);
|
|
while (offset < size) {
|
|
size_t length = size - offset < sizeof(disk) ? size - offset : sizeof(disk);
|
|
assert(fread(disk, 1U, length, file) == length);
|
|
assert(Example_Recording_Read(&recording, index, (uint32_t)offset, expected, length));
|
|
assert(memcmp(disk, expected, length) == 0);
|
|
offset += length;
|
|
}
|
|
assert(fgetc(file) == EOF && !ferror(file));
|
|
assert(fclose(file) == 0);
|
|
assert(remove(path) == 0); /* Only files successfully created by this test. */
|
|
}
|
|
}
|
|
|
|
static void Test_Energy(const char *parent)
|
|
{
|
|
static const uint32_t interval_ioas[8] = {0x6409U, 0x640AU, 0x640BU, 0x640CU,
|
|
0x640DU, 0x640EU, 0x640FU, 0x6410U};
|
|
static const uint32_t day_ioas[8] = {0x6411U, 0x6412U, 0x6413U, 0x6414U,
|
|
0x6415U, 0x6416U, 0x6417U, 0x6418U};
|
|
static const uint32_t flow_ioas[8] = {0x6419U, 0x641AU, 0x641BU, 0x641CU,
|
|
0x641DU, 0x641EU, 0x641FU, 0x6420U};
|
|
static const char *const units[8] = {"kWh", "kVarh", "kWh", "kVarh",
|
|
"kVarh", "kVarh", "kVarh", "kVarh"};
|
|
static const uint64_t expected[8] = {11U, 1U, 5U, 2U, 1U, 1U, 0U, 1U};
|
|
IEC60870_EnergyFreezeConfig_t config;
|
|
IEC60870_EnergyFreezeFlowReverse_t flow;
|
|
IEC60870_EnergyFreezeRecord_t interval;
|
|
ExampleEnergyMeter meter, restarted;
|
|
uint64_t registers[8], restored[8];
|
|
float values[8];
|
|
uint8_t quality[8];
|
|
char path[512];
|
|
char xml[4096];
|
|
struct stat entry;
|
|
size_t index;
|
|
int count = snprintf(path, sizeof(path), "%s/energy.store", parent);
|
|
assert(count > 0 && (size_t)count < sizeof(path));
|
|
assert(stat(path, &entry) != 0 && errno == ENOENT);
|
|
IEC60870_EnergyFreeze_Config_Default(&config);
|
|
config.interval_ioas = interval_ioas; config.day_ioas = day_ioas;
|
|
config.flowrev_ioas = flow_ioas; config.point_count = config.flowrev_count = 8U;
|
|
config.value_semantics = 1U;
|
|
assert(Example_Energy_Freeze_Open(&energy, parent, &config, "DTU 100V1A", units));
|
|
assert(Example_Energy_Meter_Init(&meter, 1200000U, 0U, TEST_UTC));
|
|
assert(Example_Energy_Meter_Restore(&meter, &energy.core));
|
|
assert(Example_Energy_Meter_Evaluate_At(&meter, 900000U, registers));
|
|
assert(memcmp(registers, expected, sizeof(registers)) == 0);
|
|
assert(Example_Energy_Meter_Snapshot(&meter, 900000U, values, quality));
|
|
assert(Example_Energy_Meter_Restore_From_Values(values, restored));
|
|
assert(memcmp(registers, restored, sizeof(registers)) == 0);
|
|
assert(Example_Energy_Meter_Step(&meter, &energy.core, 900000U));
|
|
assert(IEC60870_EnergyFreeze_Get_FlowReverse(&energy.core, 0U, &flow) == IEC60870_ENERGY_FREEZE_OK);
|
|
assert(flow.count == 8U && flow.values[0] == 0.011f && flow.values[2] == 0.0f);
|
|
assert(IEC60870_EnergyFreeze_Read_Interval(&energy.core,
|
|
IEC60870_EnergyFreeze_Window_Day(TEST_UTC, config.day_start_minutes), 2U, &interval) == IEC60870_ENERGY_FREEZE_OK);
|
|
for (index = 0U; index < 8U; ++index) { assert(interval.values[index] == values[index]); }
|
|
Read_Energy_Xml("HISTORY/FLOWREV", xml, sizeof(xml));
|
|
assert(strstr(xml, "ioa=\"25627\"") != NULL);
|
|
Example_Energy_Freeze_Close(&energy);
|
|
assert(Example_Energy_Freeze_Open(&energy, parent, &config, "DTU 100V1A", units));
|
|
assert(Example_Energy_Meter_Init(&restarted, 1200000U, 100U, TEST_UTC + 900000U));
|
|
assert(Example_Energy_Meter_Restore(&restarted, &energy.core));
|
|
assert(Example_Energy_Meter_Evaluate_At(&restarted, 100U, restored));
|
|
assert(memcmp(registers, restored, sizeof(registers)) == 0);
|
|
assert(IEC60870_EnergyFreeze_Advance(&energy.core, TEST_UTC + UINT64_C(86400000) +
|
|
config.max_delay_ms) == IEC60870_ENERGY_FREEZE_OK);
|
|
Read_Energy_Xml("HISTORY/FRZ", xml, sizeof(xml));
|
|
assert(strstr(xml, "ioa=\"25611\" type=\"float\" unit=\"kWh\"") != NULL);
|
|
assert(strstr(xml, "ioa=\"25612\" type=\"float\" unit=\"kVarh\"") != NULL);
|
|
Example_Energy_Freeze_Close(&energy);
|
|
assert(remove(path) == 0); /* The path was absent before this test opened it. */
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
assert(argc == 2);
|
|
Test_Recording(argv[1]);
|
|
Test_Energy(argv[1]);
|
|
puts("PASS: DTU physical recording and energy freeze/restart");
|
|
return 0;
|
|
}
|