embox
229 строк · 7.8 Кб
1/*
2* server_example_basic_io.c
3*
4* - How to use simple control models
5* - How to serve analog measurement data
6* - Using the IedServerConfig object to configure stack features
7*/
8
9#include "iec61850_server.h"10#include "hal_thread.h"11#include <signal.h>12#include <stdlib.h>13#include <stdio.h>14#include <math.h>15
16#include "static_model.h"17
18/* import IEC 61850 device model created from SCL-File */
19extern IedModel iedModel;20
21static int running = 0;22static IedServer iedServer = NULL;23
24void
25sigint_handler(int signalId)26{
27running = 0;28}
29
30static ControlHandlerResult31controlHandlerForBinaryOutput(ControlAction action, void* parameter, MmsValue* value, bool test)32{
33if (test)34return CONTROL_RESULT_FAILED;35
36if (MmsValue_getType(value) == MMS_BOOLEAN) {37printf("received binary control command: ");38
39if (MmsValue_getBoolean(value))40printf("on\n");41else42printf("off\n");43}44else45return CONTROL_RESULT_FAILED;46
47uint64_t timeStamp = Hal_getTimeInMs();48
49if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO1) {50IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1_t, timeStamp);51IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1_stVal, value);52}53
54if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO2) {55IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO2_t, timeStamp);56IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO2_stVal, value);57}58
59if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO3) {60IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO3_t, timeStamp);61IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO3_stVal, value);62}63
64if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO4) {65IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO4_t, timeStamp);66IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO4_stVal, value);67}68
69return CONTROL_RESULT_OK;70}
71
72
73static void74connectionHandler (IedServer self, ClientConnection connection, bool connected, void* parameter)75{
76if (connected)77printf("Connection opened\n");78else79printf("Connection closed\n");80}
81
82static void83rcbEventHandler(void* parameter, ReportControlBlock* rcb, ClientConnection connection, IedServer_RCBEventType event, const char* parameterName, MmsDataAccessError serviceError)84{
85printf("RCB: %s event: %i\n", ReportControlBlock_getName(rcb), event);86
87if ((event == RCB_EVENT_SET_PARAMETER) || (event == RCB_EVENT_GET_PARAMETER)) {88printf(" param: %s\n", parameterName);89printf(" result: %i\n", serviceError);90}91
92if (event == RCB_EVENT_ENABLE) {93char* rptId = ReportControlBlock_getRptID(rcb);94printf(" rptID: %s\n", rptId);95char* dataSet = ReportControlBlock_getDataSet(rcb);96printf(" datSet: %s\n", dataSet);97
98free(rptId);99free(dataSet);100}101}
102
103int
104main(int argc, char** argv)105{
106printf("Using libIEC61850 version %s\n", LibIEC61850_getVersionString());107
108/* Create new server configuration object */109IedServerConfig config = IedServerConfig_create();110
111/* Set buffer size for buffered report control blocks to 200000 bytes */112IedServerConfig_setReportBufferSize(config, 200000);113
114/* Set stack compliance to a specific edition of the standard (WARNING: data model has also to be checked for compliance) */115IedServerConfig_setEdition(config, IEC_61850_EDITION_2);116
117/* Set the base path for the MMS file services */118IedServerConfig_setFileServiceBasePath(config, "./vmd-filestore/");119
120/* disable MMS file service */121IedServerConfig_enableFileService(config, false);122
123/* enable dynamic data set service */124IedServerConfig_enableDynamicDataSetService(config, true);125
126/* disable log service */127IedServerConfig_enableLogService(config, false);128
129/* set maximum number of clients */130IedServerConfig_setMaxMmsConnections(config, 2);131
132/* Create a new IEC 61850 server instance */133iedServer = IedServer_createWithConfig(&iedModel, NULL, config);134
135/* configuration object is no longer required */136IedServerConfig_destroy(config);137
138/* set the identity values for MMS identify service */139IedServer_setServerIdentity(iedServer, "MZ", "basic io", "1.4.2");140
141/* Install handler for operate command */142IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1,143(ControlHandler) controlHandlerForBinaryOutput,144IEDMODEL_GenericIO_GGIO1_SPCSO1);145
146IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO2,147(ControlHandler) controlHandlerForBinaryOutput,148IEDMODEL_GenericIO_GGIO1_SPCSO2);149
150IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO3,151(ControlHandler) controlHandlerForBinaryOutput,152IEDMODEL_GenericIO_GGIO1_SPCSO3);153
154IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO4,155(ControlHandler) controlHandlerForBinaryOutput,156IEDMODEL_GenericIO_GGIO1_SPCSO4);157
158IedServer_setConnectionIndicationHandler(iedServer, (IedConnectionIndicationHandler) connectionHandler, NULL);159
160IedServer_setRCBEventHandler(iedServer, rcbEventHandler, NULL);161
162/* By default access to variables with FC=DC and FC=CF is not allowed.163* This allow to write to simpleIOGenericIO/GGIO1.NamPlt.vendor variable used
164* by iec61850_client_example1.
165*/
166IedServer_setWriteAccessPolicy(iedServer, IEC61850_FC_DC, ACCESS_POLICY_ALLOW);167
168/* MMS server will be instructed to start listening for client connections. */169IedServer_start(iedServer, 102);170
171if (!IedServer_isRunning(iedServer)) {172printf("Starting server failed (maybe need root permissions or another server is already using the port)! Exit.\n");173IedServer_destroy(iedServer);174exit(-1);175}176
177running = 1;178
179signal(SIGINT, sigint_handler);180
181float t = 0.f;182
183while (running) {184uint64_t timestamp = Hal_getTimeInMs();185
186t += 0.1f;187
188float an1 = t;//sinf(t);189float an2 = t + 1;//sinf(t + 1.f);190float an3 = t + 2;//sinf(t + 2.f);191float an4 = t + 3;//sinf(t + 3.f);192
193Timestamp iecTimestamp;194
195Timestamp_clearFlags(&iecTimestamp);196Timestamp_setTimeInMilliseconds(&iecTimestamp, timestamp);197Timestamp_setLeapSecondKnown(&iecTimestamp, true);198
199/* toggle clock-not-synchronized flag in timestamp */200if (((int) t % 2) == 0)201Timestamp_setClockNotSynchronized(&iecTimestamp, true);202
203IedServer_lockDataModel(iedServer);204
205IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn1_t, &iecTimestamp);206IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn1_mag_f, an1);207
208IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn2_t, &iecTimestamp);209IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn2_mag_f, an2);210
211IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn3_t, &iecTimestamp);212IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn3_mag_f, an3);213
214IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn4_t, &iecTimestamp);215IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn4_mag_f, an4);216
217IedServer_unlockDataModel(iedServer);218
219Thread_sleep(100);220}221
222/* stop MMS server - close TCP server socket and all client sockets */223IedServer_stop(iedServer);224
225/* Cleanup - free all resources */226IedServer_destroy(iedServer);227
228return 0;229} /* main() */230