274 lines
14 KiB
C++
274 lines
14 KiB
C++
/*
|
|
* DTU 远程定值适配,随共享引擎包含;预置、固化、切区回调由事件循环串行执行。
|
|
*/
|
|
static double dtu_setting(const ExampleApp *app, uint32_t ioa, double fallback)
|
|
{
|
|
return Dtu_Settings_Get(&app->dtu_settings, ioa, fallback);
|
|
}
|
|
|
|
static void Example_Remote_Report_Settings(const ExampleApp *app, const char *reason, uint16_t target_area)
|
|
{
|
|
/* target_area 是操作目标,下列运行值取当前生效区。 */
|
|
printf("REMOTE_SETTINGS reason=%s target_area=%u current_area=%u reset_seconds=%.9g x_time=%.9g deadband=%.9g oc1=%.9g oc2=%.9g oc3=%.9g\n",
|
|
reason, (unsigned)target_area, (unsigned)app->dtu_settings.current_area,
|
|
dtu_setting(app, 0x8821U, 5.0), dtu_setting(app, 0x8823U, 7.0),
|
|
dtu_setting(app, 0x8020U, 0.02), dtu_setting(app, 0x8842U, 1.2),
|
|
dtu_setting(app, 0x8846U, 1.0), dtu_setting(app, 0x884aU, 0.8));
|
|
fflush(stdout);
|
|
}
|
|
|
|
static void Example_Reset_Remote_Values(ExampleApp *app)
|
|
{
|
|
Dtu_Settings_Init(&app->dtu_settings);
|
|
memset(&app->dtu_stage, 0, sizeof(app->dtu_stage));
|
|
app->dtu_config_candidate = NULL;
|
|
app->remote_area = 0U;
|
|
app->remote_deadband = 0.0f;
|
|
memset(app->remote_reset_seconds, 0, sizeof(app->remote_reset_seconds));
|
|
memset(app->remote_x_time, 0, sizeof(app->remote_x_time));
|
|
}
|
|
|
|
/* 兼容通用 INI 字段;DTU 值以专用定值区为准。 */
|
|
static ExampleRemoteConfig Example_Remote_Config_Snapshot(const ExampleApp *app)
|
|
{
|
|
ExampleRemoteConfig saved = {0};
|
|
saved.current_area = app->dtu_settings.current_area;
|
|
return saved;
|
|
}
|
|
|
|
static void Example_Remote_Config_Apply(ExampleApp *app, const ExampleRemoteConfig *saved)
|
|
{
|
|
app->remote_area = saved->current_area;
|
|
}
|
|
|
|
static inline bool Example_Init_RemoteParameter(ExampleApp *app)
|
|
{
|
|
IEC104_RemoteParameterConfig_t config;
|
|
if (app->remote_parameter != NULL || sizeof(float) != 4U || FLT_RADIX != 2 ||
|
|
FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) { return false; }
|
|
Example_Reset_Remote_Values(app);
|
|
if (!Dtu_Settings_Valid(&app->dtu_settings)) { return false; }
|
|
IEC104_RemoteParameter_Config_Default(&config);
|
|
app->remote_parameter = IEC104_RemoteParameter_Create(app->slave, &config);
|
|
if (app->remote_parameter != NULL) { Example_Remote_Report_Settings(app, "init", 0U); }
|
|
return app->remote_parameter != NULL;
|
|
}
|
|
|
|
/* 完整身份防止重连或实例复用后接续旧事务;SN 在预置/固化时另查。 */
|
|
static bool dtu_remote_owner(const DtuSettingsStage *stage,
|
|
const IEC104_RemoteParameterRequest_t *request)
|
|
{
|
|
const IEC104_RemoteParameterRequest_t *owner = &stage->owner;
|
|
return stage->active && owner->identity.slave == request->identity.slave &&
|
|
owner->identity.instance_id == request->identity.instance_id &&
|
|
owner->identity.generation == request->identity.generation &&
|
|
owner->instance_id == request->instance_id && owner->generation == request->generation &&
|
|
owner->station_id == request->station_id && owner->common_address == request->common_address &&
|
|
owner->test == request->test;
|
|
}
|
|
|
|
static bool Example_Remote_Station(const ExampleApp *app, const IEC104_RemoteParameterRequest_t *request)
|
|
{
|
|
return request->identity.slave == app->slave &&
|
|
request->station_id == IEC104_Station_Get_Id(&app->station) &&
|
|
request->common_address == IEC104_Station_Get_Common_Address(&app->station);
|
|
}
|
|
|
|
/* 整份校验、可选保存成功后才发布运行值;切区由 candidate.current_area 决定。 */
|
|
static bool dtu_remote_commit(ExampleApp *app, DtuSettings *candidate)
|
|
{
|
|
DtuSettings previous;
|
|
DtuMeasurementInput checked_input;
|
|
DtuMeasurementFrame checked_frame;
|
|
uint64_t applied_ms = IEC104_Net_Now_Ms();
|
|
size_t area, index;
|
|
if (!Dtu_Settings_Valid(candidate)) { return false; }
|
|
Dtu_Measurement_Init(&checked_input);
|
|
if (!Dtu_Measurement_Sample(candidate, 5000U, true, &checked_input) ||
|
|
!Dtu_Measurement_Process(candidate, &checked_input, &checked_frame) ||
|
|
!Example_Dtu_Energy_Settings(app, candidate, applied_ms, false)) { return false; }
|
|
/* 旧倍率只积至提交边界,新倍率不追溯改写已累计电量。 */
|
|
Example_Dtu_Accumulate_Energy(app, IEC104_Net_Now_Ms());
|
|
if (app->config_path != NULL) {
|
|
ExampleConfigResult result;
|
|
/* 仅同步保存期间借用栈候选,返回后清指针。 */
|
|
app->dtu_config_candidate = candidate;
|
|
result = Example_Config_Store(app, NULL, NULL);
|
|
app->dtu_config_candidate = NULL;
|
|
if (result != EXAMPLE_CONFIG_OK) {
|
|
fprintf(stderr, "REMOTE_CONFIG save failed: %s\n", Example_Config_Result_Name(result));
|
|
return false;
|
|
}
|
|
puts("REMOTE_CONFIG SAVED"); fflush(stdout);
|
|
}
|
|
/* 后续日志或应答失败不回滚已提交的配置。 */
|
|
previous = app->dtu_settings;
|
|
app->dtu_settings = *candidate;
|
|
if (!Example_Dtu_Energy_Settings(app, candidate, applied_ms, true)) {
|
|
/* Can/Set串行且使用同一切点;此分支仅防御内部契约错误。 */
|
|
app->energy_failed = true;
|
|
return false;
|
|
}
|
|
app->remote_area = candidate->current_area;
|
|
Example_User_Log_Parameter(app, "REMOTE_AREA", 0U, candidate->current_area,
|
|
previous.current_area, candidate->current_area);
|
|
for (area = 0U; area < DTU_SETTINGS_AREAS; ++area) {
|
|
for (index = 0U; index < DTU_SETTINGS_COUNT; ++index) {
|
|
if (Dtu_Settings_Visible(index, (uint16_t)area) &&
|
|
previous.values[area][index] != candidate->values[area][index]) {
|
|
Example_User_Log_Parameter(app, area == 0U ? "DTU_GLOBAL_OR_AREA0" : "DTU_STORED",
|
|
Dtu_Settings_Ioa(index), (unsigned)area,
|
|
previous.values[area][index], candidate->values[area][index]);
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* 按小端解码,Tag/长度须匹配模板;Float 恢复位模式。 */
|
|
static bool dtu_remote_decode(const IEC104_RemoteParameterElement_t *element, size_t index, double *value)
|
|
{
|
|
uint32_t encoded;
|
|
if (element->tag != Dtu_Settings_Tag(index)) { return false; }
|
|
if (element->tag == 1U && element->length == 1U) {
|
|
*value = element->value[0];
|
|
} else if (element->tag == 45U && element->length == 2U) {
|
|
*value = (uint16_t)((uint16_t)element->value[0] | ((uint16_t)element->value[1] << 8));
|
|
} else if ((element->tag == 35U || element->tag == 38U) && element->length == 4U) {
|
|
encoded = (uint32_t)element->value[0] | ((uint32_t)element->value[1] << 8) |
|
|
((uint32_t)element->value[2] << 16) | ((uint32_t)element->value[3] << 24);
|
|
if (element->tag == 38U) {
|
|
float floating;
|
|
memcpy(&floating, &encoded, sizeof(floating)); *value = floating;
|
|
} else { *value = encoded; }
|
|
} else { return false; }
|
|
return Dtu_Settings_Value_Valid(index, *value);
|
|
}
|
|
|
|
static IEC104_RemoteParameterActionResult_t Example_Remote_Action(void *context,
|
|
const IEC104_RemoteParameterRequest_t *request, IEC104_RemoteParameterArea_t *area)
|
|
{
|
|
ExampleApp *app = context;
|
|
DtuSettingsStage *stage = &app->dtu_stage;
|
|
IEC104_RemoteParameterActionResult_t failure = IEC104_REMOTE_PARAMETER_ACTION_REJECTED;
|
|
size_t index;
|
|
if (request->operation == IEC104_REMOTE_PARAMETER_OP_CANCEL ||
|
|
request->operation == IEC104_REMOTE_PARAMETER_OP_DISCARD) {
|
|
/* 按原完整身份收尾,不依赖当前站点登记,也不能清掉新事务。 */
|
|
if (dtu_remote_owner(stage, request)) { memset(stage, 0, sizeof(*stage)); }
|
|
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
|
}
|
|
if (!Example_Remote_Station(app, request)) { goto reject; }
|
|
if (request->operation == IEC104_REMOTE_PARAMETER_OP_READ_AREA) {
|
|
if (area == NULL) { return failure; }
|
|
area->minimum = 0U; area->maximum = 4U; area->current = app->dtu_settings.current_area;
|
|
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
|
}
|
|
if (request->operation == IEC104_REMOTE_PARAMETER_OP_SWITCH_AREA) {
|
|
DtuSettings candidate;
|
|
if (request->sn >= DTU_SETTINGS_AREAS || stage->active) { return failure; }
|
|
candidate = app->dtu_settings; candidate.current_area = request->sn;
|
|
if (!dtu_remote_commit(app, &candidate)) { return failure; }
|
|
Example_Remote_Report_Settings(app, "switch", request->sn);
|
|
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
|
}
|
|
if (request->operation == IEC104_REMOTE_PARAMETER_OP_PRESET) {
|
|
double candidate[DTU_SETTINGS_COUNT];
|
|
/* 首帧取已提交区,续帧取暂存;整帧校验成功后才替换 stage。 */
|
|
if (request->sn >= DTU_SETTINGS_AREAS) { goto reject; }
|
|
if (request->first_frame) {
|
|
if (stage->active) { goto reject; }
|
|
memcpy(candidate, app->dtu_settings.values[request->sn], sizeof(candidate));
|
|
} else {
|
|
if (!dtu_remote_owner(stage, request)) { return failure; }
|
|
if (stage->owner.sn != request->sn || stage->complete) { goto reject; }
|
|
memcpy(candidate, stage->values, sizeof(candidate));
|
|
}
|
|
for (index = 0U; index < request->count; ++index) {
|
|
IEC104_RemoteParameterElement_t element;
|
|
size_t point;
|
|
if (!IEC104_RemoteParameter_Get_Element(request, index, &element)) { goto reject; }
|
|
point = Dtu_Settings_Index(element.ioa);
|
|
if (point == DTU_SETTINGS_COUNT) { failure = IEC104_REMOTE_PARAMETER_ACTION_UNKNOWN_IOA; goto reject; }
|
|
if (!Dtu_Settings_Visible(point, request->sn) || !Dtu_Settings_Writable(point) ||
|
|
!dtu_remote_decode(&element, point, &candidate[point])) { goto reject; }
|
|
}
|
|
if (request->first_frame) {
|
|
stage->owner = *request;
|
|
/* ASDU 只在本次调用有效,跨帧仅保留身份。 */
|
|
stage->owner._data = NULL; stage->owner._length = 0U; stage->owner.count = 0U;
|
|
stage->active = true;
|
|
}
|
|
memcpy(stage->values, candidate, sizeof(candidate));
|
|
stage->complete = !request->more_follows;
|
|
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
|
}
|
|
if (request->operation == IEC104_REMOTE_PARAMETER_OP_SOLIDIFY) {
|
|
DtuSettings candidate;
|
|
if (!dtu_remote_owner(stage, request)) { return failure; }
|
|
if (request->sn >= DTU_SETTINGS_AREAS || stage->owner.sn != request->sn || !stage->complete) { goto reject; }
|
|
candidate = app->dtu_settings;
|
|
/* 只替换目标区,不隐式切区;commit 校验跨项关系。 */
|
|
memcpy(candidate.values[request->sn], stage->values, sizeof(stage->values));
|
|
if (!dtu_remote_commit(app, &candidate)) { goto reject; }
|
|
memset(stage, 0, sizeof(*stage));
|
|
Example_Remote_Report_Settings(app, "solidify", request->sn);
|
|
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
|
}
|
|
return failure;
|
|
reject:
|
|
/* 错误请求只能丢弃自身事务的暂存。 */
|
|
if (dtu_remote_owner(stage, request)) { memset(stage, 0, sizeof(*stage)); }
|
|
return failure;
|
|
}
|
|
|
|
/* 按 SN 读取已提交区;全召 index 是可见项序号,须跳过模板空项。 */
|
|
static IEC104_RemoteParameterLookupResult_t Example_Remote_Lookup(void *context,
|
|
const IEC104_RemoteParameterRequest_t *request, size_t index, uint32_t ioa,
|
|
IEC104_RemoteParameterFrame_t *frame)
|
|
{
|
|
ExampleApp *app = context;
|
|
IEC104_RemoteParameterAddResult_t added;
|
|
size_t point;
|
|
double value;
|
|
if (!Example_Remote_Station(app, request) || request->sn >= DTU_SETTINGS_AREAS) {
|
|
return IEC104_REMOTE_PARAMETER_LOOKUP_ERROR;
|
|
}
|
|
if (request->count == 0U) {
|
|
size_t visible = 0U;
|
|
for (point = 0U; point < DTU_SETTINGS_COUNT; ++point) {
|
|
uint32_t candidate_ioa = Dtu_Settings_Ioa(point);
|
|
bool image_visible = app->software_upgrade != NULL && request->sn == 0U &&
|
|
(candidate_ioa == 0x8005U || candidate_ioa == 0x8006U);
|
|
if ((Dtu_Settings_Visible(point, request->sn) || image_visible) && visible++ == index) { break; }
|
|
}
|
|
if (point == DTU_SETTINGS_COUNT) { return IEC104_REMOTE_PARAMETER_LOOKUP_END; }
|
|
ioa = Dtu_Settings_Ioa(point);
|
|
} else {
|
|
point = Dtu_Settings_Index(ioa);
|
|
if (point == DTU_SETTINGS_COUNT) { return IEC104_REMOTE_PARAMETER_LOOKUP_UNKNOWN; }
|
|
if (!Dtu_Settings_Visible(point, request->sn) && !(app->software_upgrade != NULL && request->sn == 0U &&
|
|
(ioa == 0x8005U || ioa == 0x8006U))) { return IEC104_REMOTE_PARAMETER_LOOKUP_ERROR; }
|
|
}
|
|
/* 版本/CRC 为升级模块的实时只读值,不是 XML 默认值。 */
|
|
if (app->software_upgrade != NULL && request->sn == 0U && ioa == 0x8005U) {
|
|
char version[11];
|
|
int length = snprintf(version, sizeof(version), "%lu", (unsigned long)example_image.version);
|
|
if (length <= 0 || (size_t)length >= sizeof(version)) { return IEC104_REMOTE_PARAMETER_LOOKUP_ERROR; }
|
|
added = IEC104_RemoteParameter_Add_String(frame, ioa, version, (size_t)length);
|
|
} else if (app->software_upgrade != NULL && request->sn == 0U && ioa == 0x8006U) {
|
|
added = IEC104_RemoteParameter_Add_Uint(frame, ioa, example_image.crc);
|
|
} else {
|
|
value = app->dtu_settings.values[request->sn][point];
|
|
switch (Dtu_Settings_Tag(point)) {
|
|
case 1U: added = IEC104_RemoteParameter_Add_Boolean(frame, ioa, value != 0.0); break;
|
|
case 35U: added = IEC104_RemoteParameter_Add_Uint(frame, ioa, (uint32_t)value); break;
|
|
case 45U: added = IEC104_RemoteParameter_Add_UShort(frame, ioa, (uint16_t)value); break;
|
|
case 38U: added = IEC104_RemoteParameter_Add_Float(frame, ioa, (float)value); break;
|
|
default: return IEC104_REMOTE_PARAMETER_LOOKUP_ERROR;
|
|
}
|
|
}
|
|
return added == IEC104_REMOTE_PARAMETER_ADD_OK ? IEC104_REMOTE_PARAMETER_LOOKUP_ADDED :
|
|
added == IEC104_REMOTE_PARAMETER_ADD_FULL ? IEC104_REMOTE_PARAMETER_LOOKUP_FULL : IEC104_REMOTE_PARAMETER_LOOKUP_ERROR;
|
|
}
|