From 679d428e897a685bd79a45b166d5188def152390 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Fri, 25 Oct 2019 17:31:41 +0200 Subject: [PATCH] - updated slave plugin interface --- .../examples/cs101_slave/slave_example.c | 2 +- lib60870-C/src/iec60870/cs101/cs101_slave.c | 72 +++++++++++++------ lib60870-C/src/iec60870/cs104/cs104_slave.c | 4 +- lib60870-C/src/inc/api/iec60870_slave.h | 11 ++- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/lib60870-C/examples/cs101_slave/slave_example.c b/lib60870-C/examples/cs101_slave/slave_example.c index f9d61b18..f594bb4e 100644 --- a/lib60870-C/examples/cs101_slave/slave_example.c +++ b/lib60870-C/examples/cs101_slave/slave_example.c @@ -206,7 +206,7 @@ main(int argc, char** argv) /* Add Ctrl-C handler */ signal(SIGINT, sigint_handler); - const char* serialPort = "/dev/ttyUSB1"; + const char* serialPort = "/dev/ttyUSB2"; if (argc > 1) serialPort = argv[1]; diff --git a/lib60870-C/src/iec60870/cs101/cs101_slave.c b/lib60870-C/src/iec60870/cs101/cs101_slave.c index 6aed4f94..f2f557f9 100644 --- a/lib60870-C/src/iec60870/cs101/cs101_slave.c +++ b/lib60870-C/src/iec60870/cs101/cs101_slave.c @@ -636,8 +636,14 @@ handleASDU(CS101_Slave self, CS101_ASDU asdu) CS101_SlavePlugin plugin = (CS101_SlavePlugin) LinkedList_getData(pluginElem); - if (plugin->handleAsdu(plugin->parameter, &(self->iMasterConnection), asdu)) + CS101_SlavePlugin_Result result = plugin->handleAsdu(plugin->parameter, &(self->iMasterConnection), asdu); + + if (result == CS101_PLUGIN_RESULT_HANDLED) { return; + } + else if (result == CS101_PLUGIN_RESULT_INVALID_ASDU) { + DEBUG_PRINT("Invalid message"); + } pluginElem = LinkedList_getNext(pluginElem); } @@ -658,9 +664,14 @@ handleASDU(CS101_Slave self, CS101_ASDU asdu) InterrogationCommand irc = (InterrogationCommand) CS101_ASDU_getElementEx(asdu, (InformationObject) &_io, 0); - if (self->interrogationHandler(self->interrogationHandlerParameter, - &(self->iMasterConnection), asdu, InterrogationCommand_getQOI(irc))) - messageHandled = true; + if (irc) { + if (self->interrogationHandler(self->interrogationHandlerParameter, + &(self->iMasterConnection), asdu, InterrogationCommand_getQOI(irc))) + messageHandled = true; + } + else { + DEBUG_PRINT("Invalid message"); + } } } else @@ -680,9 +691,15 @@ handleASDU(CS101_Slave self, CS101_ASDU asdu) CounterInterrogationCommand cic = (CounterInterrogationCommand) CS101_ASDU_getElementEx(asdu, (InformationObject) &_io, 0); - if (self->counterInterrogationHandler(self->counterInterrogationHandlerParameter, - &(self->iMasterConnection), asdu, CounterInterrogationCommand_getQCC(cic))) - messageHandled = true; + if (cic) { + if (self->counterInterrogationHandler(self->counterInterrogationHandlerParameter, + &(self->iMasterConnection), asdu, CounterInterrogationCommand_getQCC(cic))) + messageHandled = true; + } + else { + DEBUG_PRINT("Invalid message"); + return; + } } } else @@ -701,9 +718,14 @@ handleASDU(CS101_Slave self, CS101_ASDU asdu) ReadCommand rc = (ReadCommand) CS101_ASDU_getElementEx(asdu, (InformationObject) &_io, 0); - if (self->readHandler(self->readHandlerParameter, - &(self->iMasterConnection), asdu, InformationObject_getObjectAddress((InformationObject) rc))) - messageHandled = true; + if (rc) { + if (self->readHandler(self->readHandlerParameter, + &(self->iMasterConnection), asdu, InformationObject_getObjectAddress((InformationObject) rc))) + messageHandled = true; + } + else { + DEBUG_PRINT("Invalid message"); + } } } else @@ -723,15 +745,20 @@ handleASDU(CS101_Slave self, CS101_ASDU asdu) ClockSynchronizationCommand csc = (ClockSynchronizationCommand) CS101_ASDU_getElementEx(asdu, (InformationObject) &_io, 0); - if (self->clockSyncHandler(self->clockSyncHandlerParameter, - &(self->iMasterConnection), asdu, ClockSynchronizationCommand_getTime(csc))) - messageHandled = true; + if (csc) { + if (self->clockSyncHandler(self->clockSyncHandlerParameter, + &(self->iMasterConnection), asdu, ClockSynchronizationCommand_getTime(csc))) + messageHandled = true; - if (messageHandled) { - /* send ACT-CON message */ - CS101_ASDU_setCOT(asdu, CS101_COT_ACTIVATION_CON); + if (messageHandled) { + /* send ACT-CON message */ + CS101_ASDU_setCOT(asdu, CS101_COT_ACTIVATION_CON); - CS101_Slave_enqueueUserDataClass1(self, asdu); + CS101_Slave_enqueueUserDataClass1(self, asdu); + } + } + else { + DEBUG_PRINT("Invalid message"); } } } @@ -792,9 +819,14 @@ handleASDU(CS101_Slave self, CS101_ASDU asdu) DelayAcquisitionCommand dac = (DelayAcquisitionCommand) CS101_ASDU_getElementEx(asdu, (InformationObject) &_io, 0); - if (self->delayAcquisitionHandler(self->delayAcquisitionHandlerParameter, - &(self->iMasterConnection), asdu, DelayAcquisitionCommand_getDelay(dac))) - messageHandled = true; + if (dac) { + if (self->delayAcquisitionHandler(self->delayAcquisitionHandlerParameter, + &(self->iMasterConnection), asdu, DelayAcquisitionCommand_getDelay(dac))) + messageHandled = true; + } + else { + DEBUG_PRINT("Invalid message"); + } } } else diff --git a/lib60870-C/src/iec60870/cs104/cs104_slave.c b/lib60870-C/src/iec60870/cs104/cs104_slave.c index 2ceafc90..49460c48 100644 --- a/lib60870-C/src/iec60870/cs104/cs104_slave.c +++ b/lib60870-C/src/iec60870/cs104/cs104_slave.c @@ -1701,7 +1701,9 @@ handleASDU(MasterConnection self, CS101_ASDU asdu) CS101_SlavePlugin plugin = (CS101_SlavePlugin) LinkedList_getData(pluginElem); - if (plugin->handleAsdu(plugin->parameter, &(self->iMasterConnection), asdu)) + CS101_SlavePlugin_Result result = plugin->handleAsdu(plugin->parameter, &(self->iMasterConnection), asdu); + + if (result == CS101_PLUGIN_RESULT_HANDLED) return true; pluginElem = LinkedList_getNext(pluginElem); diff --git a/lib60870-C/src/inc/api/iec60870_slave.h b/lib60870-C/src/inc/api/iec60870_slave.h index 16d73084..6e77dd48 100644 --- a/lib60870-C/src/inc/api/iec60870_slave.h +++ b/lib60870-C/src/inc/api/iec60870_slave.h @@ -163,12 +163,21 @@ IMasterConnection_getApplicationLayerParameters(IMasterConnection self); * Plugin interface to add functionality to the slave (e.g. file server) */ +typedef enum +{ + CS101_PLUGIN_RESULT_NOT_HANDLED = 0, + CS101_PLUGIN_RESULT_HANDLED = 1, + CS101_PLUGIN_RESULT_INVALID_ASDU = 2 +} CS101_SlavePlugin_Result; +/** + * \brief Plugin interface for CS101 or CS104 slaves + */ typedef struct sCS101_SlavePlugin* CS101_SlavePlugin; struct sCS101_SlavePlugin { - bool (*handleAsdu) (void* parameter, IMasterConnection connection, CS101_ASDU asdu); + CS101_SlavePlugin_Result (*handleAsdu) (void* parameter, IMasterConnection connection, CS101_ASDU asdu); void (*runTask) (void* parameter, IMasterConnection connection); void* parameter;