/* Generated IEC 60870-5-104 (CS104) server runtime — instance %(locstr)s */
#include "iec_types_all.h"
#include "iec60870_common.h"
#include "cs101_information_objects.h"
#include "IEC104_%(locstr)s.h"
#if defined(__GNUC__) || defined(__clang__)
#define IEC60870_WEAK __attribute__((weak))
/* Referenced from __init_* before definition; silences -Wimplicit-function-declaration */
int __cleanup_%(locstr)s(void);
%(diag_intrinsic_global_block)s
%(plc_memory_storage_block)s
struct iec60870_binding {
int apci_k, apci_w, t0, t1, t2, t3;
static struct iec60870_binding iec60870_bindings[] = {
static const size_t iec60870_binding_count = IEC60870_NUM_BINDINGS_%(locstr)s;
static void iec_rd_inc(iec60870_srv_t *s) {
static void iec_wr_inc(iec60870_srv_t *s) {
static bool iec_get_bool_var(void *p) {
return *(IEC_BOOL *)p != 0;
static void iec_set_bool_var(void *p, bool v) {
*(IEC_BOOL *)p = v ? (IEC_BOOL)1 : (IEC_BOOL)0;
/* CP24/CP56 helpers for time-tagged ASDUs (wall-clock; PLC does not drive protocol timestamps). */
static struct sCP56Time2a iec_wall_cp56_storage;
static struct sCP24Time2a iec_zero_cp24_storage;
static struct sCP16Time2a iec_zero_cp16_storage;
static CP56Time2a iec_wall_cp56(void) {
CP56Time2a t = (CP56Time2a)&iec_wall_cp56_storage;
CP56Time2a_createFromMsTimestamp(t, (uint64_t)time(NULL) * 1000ULL);
static CP24Time2a iec_zero_cp24(void) {
memset(&iec_zero_cp24_storage, 0, sizeof(iec_zero_cp24_storage));
return (CP24Time2a)&iec_zero_cp24_storage;
static CP16Time2a iec_zero_cp16(void) {
memset(&iec_zero_cp16_storage, 0, sizeof(iec_zero_cp16_storage));
return (CP16Time2a)&iec_zero_cp16_storage;
static InformationObject iec_make_monitor_io(int type_id, int ioa, struct iec60870_binding *b) {
void *mem = malloc((size_t)InformationObject_getMaxSizeInMemory());
QualityDescriptor q = IEC60870_QUALITY_GOOD;
struct sBinaryCounterReading bcr_st;
BinaryCounterReading bcr;
struct sStatusAndStatusChangeDetection scd_st;
StatusAndStatusChangeDetection scd;
return (InformationObject)SinglePointInformation_create(
(SinglePointInformation)mem, ioa, iec_get_bool_var(b->iec_var), q);
return (InformationObject)SinglePointWithCP24Time2a_create(
(SinglePointWithCP24Time2a)mem, ioa, iec_get_bool_var(b->iec_var), q,
return (InformationObject)SinglePointWithCP56Time2a_create(
(SinglePointWithCP56Time2a)mem, ioa, iec_get_bool_var(b->iec_var), q,
DoublePointValue dv = (DoublePointValue)(*(IEC_BYTE *)b->iec_var & (IEC_BYTE)3);
return (InformationObject)DoublePointInformation_create(
(DoublePointInformation)mem, ioa, dv, q);
DoublePointValue dv = (DoublePointValue)(*(IEC_BYTE *)b->iec_var & (IEC_BYTE)3);
return (InformationObject)DoublePointWithCP24Time2a_create(
(DoublePointWithCP24Time2a)mem, ioa, dv, q, iec_zero_cp24());
DoublePointValue dv = (DoublePointValue)(*(IEC_BYTE *)b->iec_var & (IEC_BYTE)3);
return (InformationObject)DoublePointWithCP56Time2a_create(
(DoublePointWithCP56Time2a)mem, ioa, dv, q, iec_wall_cp56());
return (InformationObject)StepPositionInformation_create(
(StepPositionInformation)mem, ioa, (int)*(IEC_INT *)b->iec_var, false, q);
return (InformationObject)StepPositionWithCP24Time2a_create(
(StepPositionWithCP24Time2a)mem, ioa, (int)*(IEC_INT *)b->iec_var, false, q,
return (InformationObject)StepPositionWithCP56Time2a_create(
(StepPositionWithCP56Time2a)mem, ioa, (int)*(IEC_INT *)b->iec_var, false, q,
return (InformationObject)BitString32_createEx(
(BitString32)mem, ioa, (uint32_t)*(UDINT *)b->iec_var, q);
return (InformationObject)Bitstring32WithCP24Time2a_createEx(
(Bitstring32WithCP24Time2a)mem, ioa, (uint32_t)*(UDINT *)b->iec_var, q,
return (InformationObject)Bitstring32WithCP56Time2a_createEx(
(Bitstring32WithCP56Time2a)mem, ioa, (uint32_t)*(UDINT *)b->iec_var, q,
float nv = NormalizedValue_fromScaled((int)(int16_t)*(IEC_UINT *)b->iec_var);
return (InformationObject)MeasuredValueNormalized_create(
(MeasuredValueNormalized)mem, ioa, nv, q);
float nv = NormalizedValue_fromScaled((int)(int16_t)*(IEC_UINT *)b->iec_var);
return (InformationObject)MeasuredValueNormalizedWithCP24Time2a_create(
(MeasuredValueNormalizedWithCP24Time2a)mem, ioa, nv, q, iec_zero_cp24());
float nv = NormalizedValue_fromScaled((int)(int16_t)*(IEC_UINT *)b->iec_var);
return (InformationObject)MeasuredValueNormalizedWithCP56Time2a_create(
(MeasuredValueNormalizedWithCP56Time2a)mem, ioa, nv, q, iec_wall_cp56());
float nv = NormalizedValue_fromScaled((int)(int16_t)*(IEC_UINT *)b->iec_var);
return (InformationObject)MeasuredValueNormalizedWithoutQuality_create(
(MeasuredValueNormalizedWithoutQuality)mem, ioa, nv);
return (InformationObject)MeasuredValueScaled_create(
(MeasuredValueScaled)mem, ioa, (int)*(IEC_INT *)b->iec_var, q);
return (InformationObject)MeasuredValueScaledWithCP24Time2a_create(
(MeasuredValueScaledWithCP24Time2a)mem, ioa, (int)*(IEC_INT *)b->iec_var, q,
return (InformationObject)MeasuredValueScaledWithCP56Time2a_create(
(MeasuredValueScaledWithCP56Time2a)mem, ioa, (int)*(IEC_INT *)b->iec_var, q,
return (InformationObject)MeasuredValueShort_create(
(MeasuredValueShort)mem, ioa, *(IEC_REAL *)b->iec_var, q);
return (InformationObject)MeasuredValueShortWithCP24Time2a_create(
(MeasuredValueShortWithCP24Time2a)mem, ioa, *(IEC_REAL *)b->iec_var, q,
return (InformationObject)MeasuredValueShortWithCP56Time2a_create(
(MeasuredValueShortWithCP56Time2a)mem, ioa, *(IEC_REAL *)b->iec_var, q,
bcr = BinaryCounterReading_create((BinaryCounterReading)&bcr_st,
(int32_t)(*(UDINT *)b->iec_var), 0, false, false, false);
return (InformationObject)IntegratedTotals_create((IntegratedTotals)mem, ioa, bcr);
bcr = BinaryCounterReading_create((BinaryCounterReading)&bcr_st,
(int32_t)(*(UDINT *)b->iec_var), 0, false, false, false);
return (InformationObject)IntegratedTotalsWithCP24Time2a_create(
(IntegratedTotalsWithCP24Time2a)mem, ioa, bcr, iec_zero_cp24());
bcr = BinaryCounterReading_create((BinaryCounterReading)&bcr_st,
(int32_t)(*(UDINT *)b->iec_var), 0, false, false, false);
return (InformationObject)IntegratedTotalsWithCP56Time2a_create(
(IntegratedTotalsWithCP56Time2a)mem, ioa, bcr, iec_wall_cp56());
pack_u32 = (uint32_t)*(UDINT *)b->iec_var;
se = (SingleEvent)&se_byte;
SingleEvent_setEventState(se, (EventState)(pack_u32 & 3u));
SingleEvent_setQDP(se, (QualityDescriptorP)((pack_u32 >> 2) & 0xffu));
return (InformationObject)EventOfProtectionEquipment_create(
(EventOfProtectionEquipment)mem, ioa, se, iec_zero_cp16(), iec_zero_cp24());
pack_u32 = (uint32_t)*(UDINT *)b->iec_var;
ste = (StartEvent)(pack_u32 & 0xffu);
qdp = (QualityDescriptorP)((pack_u32 >> 8) & 0xffu);
return (InformationObject)PackedStartEventsOfProtectionEquipment_create(
(PackedStartEventsOfProtectionEquipment)mem, ioa, ste, qdp, iec_zero_cp16(),
pack_u32 = (uint32_t)*(UDINT *)b->iec_var;
oci = (OutputCircuitInfo)(pack_u32 & 0xffu);
qdp = (QualityDescriptorP)((pack_u32 >> 8) & 0xffu);
return (InformationObject)PackedOutputCircuitInfo_create(
(PackedOutputCircuitInfo)mem, ioa, oci, qdp, iec_zero_cp16(), iec_zero_cp24());
pack_u32 = (uint32_t)*(UDINT *)b->iec_var;
se = (SingleEvent)&se_byte;
SingleEvent_setEventState(se, (EventState)(pack_u32 & 3u));
SingleEvent_setQDP(se, (QualityDescriptorP)((pack_u32 >> 2) & 0xffu));
return (InformationObject)EventOfProtectionEquipmentWithCP56Time2a_create(
(EventOfProtectionEquipmentWithCP56Time2a)mem, ioa, se, iec_zero_cp16(),
pack_u32 = (uint32_t)*(UDINT *)b->iec_var;
ste = (StartEvent)(pack_u32 & 0xffu);
qdp = (QualityDescriptorP)((pack_u32 >> 8) & 0xffu);
return (InformationObject)PackedStartEventsOfProtectionEquipmentWithCP56Time2a_create(
(PackedStartEventsOfProtectionEquipmentWithCP56Time2a)mem, ioa, ste, qdp,
iec_zero_cp16(), iec_wall_cp56());
pack_u32 = (uint32_t)*(UDINT *)b->iec_var;
oci = (OutputCircuitInfo)(pack_u32 & 0xffu);
qdp = (QualityDescriptorP)((pack_u32 >> 8) & 0xffu);
return (InformationObject)PackedOutputCircuitInfoWithCP56Time2a_create(
(PackedOutputCircuitInfoWithCP56Time2a)mem, ioa, oci, qdp, iec_zero_cp16(),
memset(&scd_st, 0, sizeof(scd_st));
scd = (StatusAndStatusChangeDetection)&scd_st;
StatusAndStatusChangeDetection_setSTn(scd, (uint16_t)(*(UDINT *)b->iec_var & 0xffffu));
return (InformationObject)PackedSinglePointWithSCD_create(
(PackedSinglePointWithSCD)mem, ioa, scd, q);
return (InformationObject)EndOfInitialization_create(
(EndOfInitialization)mem, (uint8_t)(iec_get_bool_var(b->iec_var) ? 1 : 0));
/* File transfer indications — PLC holds scalar summaries per IOA (length / qualifiers). */
return (InformationObject)FileReady_create(
(FileReady)mem, ioa, 0, (uint32_t)*(UDINT *)b->iec_var, true);
return (InformationObject)SectionReady_create(
(SectionReady)mem, ioa, 0, 0, (uint32_t)*(UDINT *)b->iec_var, false);
uint32_t v = (uint32_t)*(UDINT *)b->iec_var;
/* Packed: NOF (bits 0–15), NOS (16–23), LSQ (24–31); CHS via extension if needed */
return (InformationObject)FileLastSegmentOrSection_create(
(FileLastSegmentOrSection)mem, ioa,
(uint8_t)((v >> 16) & 0xffu),
(uint8_t)((v >> 24) & 0xffu),
return (InformationObject)FileACK_create(
(FileACK)mem, ioa, 0, 0, (uint8_t)(*(IEC_BYTE *)b->iec_var & 0xffu));
return (InformationObject)FileSegment_create(
(FileSegment)mem, ioa, 0, 0, &seg_dummy, 0);
return (InformationObject)FileDirectory_create(
(FileDirectory)mem, ioa, 0, (uint32_t)*(UDINT *)b->iec_var, 0, iec_wall_cp56());
static bool iec_send_monitor(IMasterConnection conn, iec60870_srv_t *srv, struct iec60870_binding *b) {
CS101_AppLayerParameters al = CS104_Slave_getAppLayerParameters(srv->slave);
int oa = srv->cot_two_byte ? srv->oa : 0;
InformationObject io = iec_make_monitor_io(b->type_id, b->ioa, b);
CS101_ASDU asdu = CS101_ASDU_create(al, false, CS101_COT_INTERROGATED_BY_STATION, oa,
srv->common_address, false, false);
InformationObject_destroy(io);
if (!CS101_ASDU_addInformationObject(asdu, io)) {
InformationObject_destroy(io);
CS101_ASDU_destroy(asdu);
if (!IMasterConnection_sendASDU(conn, asdu)) {
CS101_ASDU_destroy(asdu);
CS101_ASDU_destroy(asdu);
static bool iec_interrogation_handler(void *parameter, IMasterConnection connection, CS101_ASDU asdu, uint8_t qoi) {
iec60870_srv_t *srv = (iec60870_srv_t *)parameter;
size_t idx = (size_t)(srv - iec60870_srv);
if (qoi == IEC60870_QOI_STATION) {
for (i = 0; i < iec60870_binding_count; i++) {
struct iec60870_binding *b = &iec60870_bindings[i];
if ((size_t)b->server_index != idx)
iec_send_monitor(connection, srv, b);
static bool iec_tid_handled_as_command(TypeID tid) {
if (t >= C_SC_NA_1 && t <= C_BO_NA_1)
if (t >= C_SC_TA_1 && t <= C_BO_TA_1)
if (t >= C_IC_NA_1 && t <= C_TS_TA_1)
if (t >= P_ME_NA_1 && t <= P_AC_NA_1)
if (t == F_SC_NA_1 || t == F_SC_NB_1)
static bool iec_handle_command(iec60870_srv_t *srv, IMasterConnection connection, CS101_ASDU asdu) {
TypeID tid = CS101_ASDU_getTypeID(asdu);
InformationObject io = CS101_ASDU_getElement(asdu, 0);
size_t srv_idx = (size_t)(srv - iec60870_srv);
struct iec60870_binding *match = NULL;
int ioa = InformationObject_getObjectAddress(io);
for (i = 0; i < iec60870_binding_count; i++) {
if (iec60870_bindings[i].server_index < 0)
if ((size_t)iec60870_bindings[i].server_index != srv_idx)
if (!iec60870_bindings[i].is_command)
if (iec60870_bindings[i].ioa == ioa && iec60870_bindings[i].type_id == (int)tid) {
match = &iec60870_bindings[i];
SingleCommand sc = (SingleCommand)io;
iec_set_bool_var(match->iec_var, SingleCommand_getState(sc));
DoubleCommand dc = (DoubleCommand)io;
*(IEC_BYTE *)match->iec_var = (IEC_BYTE)(DoubleCommand_getState(dc) & 0xff);
StepCommand sc = (StepCommand)io;
*(IEC_BYTE *)match->iec_var = (IEC_BYTE)((int)StepCommand_getState(sc) & 0xff);
SetpointCommandNormalized sn = (SetpointCommandNormalized)io;
fv = SetpointCommandNormalized_getValue(sn);
scv = NormalizedValue_toScaled(fv);
*(IEC_UINT *)match->iec_var = (uint16_t)scv;
SetpointCommandScaled ss = (SetpointCommandScaled)io;
*(IEC_INT *)match->iec_var = (int16_t)SetpointCommandScaled_getValue(ss);
SetpointCommandShort sf = (SetpointCommandShort)io;
*(IEC_REAL *)match->iec_var = SetpointCommandShort_getValue(sf);
Bitstring32Command bc = (Bitstring32Command)io;
*(UDINT *)match->iec_var = (UDINT)Bitstring32Command_getValue(bc);
iec_set_bool_var(match->iec_var, SingleCommand_getState((SingleCommand)io));
DoubleCommandWithCP56Time2a dc = (DoubleCommandWithCP56Time2a)io;
*(IEC_BYTE *)match->iec_var = (IEC_BYTE)(DoubleCommandWithCP56Time2a_getState(dc) & 0xff);
StepCommandWithCP56Time2a sc = (StepCommandWithCP56Time2a)io;
*(IEC_BYTE *)match->iec_var = (IEC_BYTE)((int)StepCommandWithCP56Time2a_getState(sc) & 0xff);
SetpointCommandNormalizedWithCP56Time2a sn = (SetpointCommandNormalizedWithCP56Time2a)io;
fv = SetpointCommandNormalizedWithCP56Time2a_getValue(sn);
scv = NormalizedValue_toScaled(fv);
*(IEC_UINT *)match->iec_var = (uint16_t)scv;
SetpointCommandScaledWithCP56Time2a ss = (SetpointCommandScaledWithCP56Time2a)io;
*(IEC_INT *)match->iec_var = (int16_t)SetpointCommandScaledWithCP56Time2a_getValue(ss);
SetpointCommandShortWithCP56Time2a sf = (SetpointCommandShortWithCP56Time2a)io;
*(IEC_REAL *)match->iec_var = SetpointCommandShortWithCP56Time2a_getValue(sf);
Bitstring32CommandWithCP56Time2a bc = (Bitstring32CommandWithCP56Time2a)io;
*(UDINT *)match->iec_var = (UDINT)Bitstring32CommandWithCP56Time2a_getValue(bc);
InterrogationCommand ic = (InterrogationCommand)io;
iec_set_bool_var(match->iec_var, InterrogationCommand_getQOI(ic) != 0);
CounterInterrogationCommand cic = (CounterInterrogationCommand)io;
*(UDINT *)match->iec_var = (UDINT)CounterInterrogationCommand_getQCC(cic);
iec_set_bool_var(match->iec_var, true);
ClockSynchronizationCommand cs = (ClockSynchronizationCommand)io;
*(UDINT *)match->iec_var =
(UDINT)(CP56Time2a_toMsTimestamp(ClockSynchronizationCommand_getTime(cs)) & 0xffffffffu);
iec_set_bool_var(match->iec_var, TestCommand_isValid((TestCommand)io));
*(IEC_BYTE *)match->iec_var =
(IEC_BYTE)(ResetProcessCommand_getQRP((ResetProcessCommand)io) & 0xff);
DelayAcquisitionCommand da = (DelayAcquisitionCommand)io;
*(UDINT *)match->iec_var =
(UDINT)CP16Time2a_getEplapsedTimeInMs(DelayAcquisitionCommand_getDelay(da));
*(IEC_UINT *)match->iec_var =
(IEC_UINT)TestCommandWithCP56Time2a_getCounter((TestCommandWithCP56Time2a)io);
ParameterNormalizedValue pn = (ParameterNormalizedValue)io;
fv = ParameterNormalizedValue_getValue(pn);
scv = NormalizedValue_toScaled(fv);
*(IEC_UINT *)match->iec_var = (uint16_t)scv;
ParameterScaledValue ps = (ParameterScaledValue)io;
*(IEC_INT *)match->iec_var = (int16_t)ParameterScaledValue_getValue(ps);
ParameterFloatValue pf = (ParameterFloatValue)io;
*(IEC_REAL *)match->iec_var = ParameterFloatValue_getValue(pf);
*(IEC_BYTE *)match->iec_var =
(IEC_BYTE)(ParameterActivation_getQuality((ParameterActivation)io) & 0xff);
*(IEC_BYTE *)match->iec_var =
(IEC_BYTE)(FileCallOrSelect_getSCQ((FileCallOrSelect)io) & 0xff);
*(IEC_UINT *)match->iec_var = (IEC_UINT)QueryLog_getNOF((QueryLog)io);
IMasterConnection_sendACT_CON(connection, asdu, false);
static bool iec_asdu_handler(void *parameter, IMasterConnection connection, CS101_ASDU asdu) {
iec60870_srv_t *srv = (iec60870_srv_t *)parameter;
TypeID tid = CS101_ASDU_getTypeID(asdu);
if (iec_tid_handled_as_command(tid))
return iec_handle_command(srv, connection, asdu);
static void iec_connection_event(void *parameter, IMasterConnection connection, CS104_PeerConnectionEvent event) {
iec60870_srv_t *srv = (iec60870_srv_t *)parameter;
if (event == CS104_CON_EVENT_ACTIVATED)
iec_set_bool_var(srv->conn_bool, true);
else if (event == CS104_CON_EVENT_DEACTIVATED || event == CS104_CON_EVENT_CONNECTION_CLOSED)
iec_set_bool_var(srv->conn_bool, CS104_Slave_getOpenConnections(srv->slave) > 0);
int __init_%(locstr)s(int argc, char **argv) {
for (si = 0; si < IEC60870_NUM_SERVERS_%(locstr)s; si++) {
iec60870_srv_t *s = &iec60870_srv[si];
s->slave = CS104_Slave_create(200, 200);
fprintf(stderr, "IEC60870 %%s: CS104_Slave_create failed\\n", s->loc_label);
CS104_Slave_setLocalAddress(s->slave, s->ip_str);
CS104_Slave_setLocalPort(s->slave, s->port);
CS104_Slave_setMaxOpenConnections(s->slave, s->max_open);
CS101_AppLayerParameters al = CS104_Slave_getAppLayerParameters(s->slave);
al->sizeOfIOA = s->ioa_sz;
al->sizeOfCOT = s->cot_two_byte ? 2 : 1;
al->originatorAddress = s->oa;
CS104_APCIParameters ap = CS104_Slave_getConnectionParameters(s->slave);
CS104_Slave_setInterrogationHandler(s->slave, iec_interrogation_handler, s);
CS104_Slave_setASDUHandler(s->slave, iec_asdu_handler, s);
CS104_Slave_setConnectionEventHandler(s->slave, iec_connection_event, s);
CS104_Slave_start(s->slave);
void __retrieve_%(locstr)s(void) {
for (si = 0; si < IEC60870_NUM_SERVERS_%(locstr)s; si++) {
iec60870_srv_t *s = &iec60870_srv[si];
if (s->slave && s->conn_bool)
iec_set_bool_var(s->conn_bool, CS104_Slave_getOpenConnections(s->slave) > 0);
void __publish_%(locstr)s(void) {
int __cleanup_%(locstr)s(void) {
for (si = 0; si < IEC60870_NUM_SERVERS_%(locstr)s; si++) {
iec60870_srv_t *s = &iec60870_srv[si];
CS104_Slave_stop(s->slave);
CS104_Slave_destroy(s->slave);