embox

Форк
0
/
server_example_basic_io.c 
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 */
19
extern IedModel iedModel;
20

21
static int running = 0;
22
static IedServer iedServer = NULL;
23

24
void
25
sigint_handler(int signalId)
26
{
27
    running = 0;
28
}
29

30
static ControlHandlerResult
31
controlHandlerForBinaryOutput(ControlAction action, void* parameter, MmsValue* value, bool test)
32
{
33
    if (test)
34
        return CONTROL_RESULT_FAILED;
35

36
    if (MmsValue_getType(value) == MMS_BOOLEAN) {
37
        printf("received binary control command: ");
38

39
        if (MmsValue_getBoolean(value))
40
            printf("on\n");
41
        else
42
            printf("off\n");
43
    }
44
    else
45
        return CONTROL_RESULT_FAILED;
46

47
    uint64_t timeStamp = Hal_getTimeInMs();
48

49
    if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO1) {
50
        IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1_t, timeStamp);
51
        IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1_stVal, value);
52
    }
53

54
    if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO2) {
55
        IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO2_t, timeStamp);
56
        IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO2_stVal, value);
57
    }
58

59
    if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO3) {
60
        IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO3_t, timeStamp);
61
        IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO3_stVal, value);
62
    }
63

64
    if (parameter == IEDMODEL_GenericIO_GGIO1_SPCSO4) {
65
        IedServer_updateUTCTimeAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO4_t, timeStamp);
66
        IedServer_updateAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO4_stVal, value);
67
    }
68

69
    return CONTROL_RESULT_OK;
70
}
71

72

73
static void
74
connectionHandler (IedServer self, ClientConnection connection, bool connected, void* parameter)
75
{
76
    if (connected)
77
        printf("Connection opened\n");
78
    else
79
        printf("Connection closed\n");
80
}
81

82
static void
83
rcbEventHandler(void* parameter, ReportControlBlock* rcb, ClientConnection connection, IedServer_RCBEventType event, const char* parameterName, MmsDataAccessError serviceError)
84
{
85
    printf("RCB: %s event: %i\n", ReportControlBlock_getName(rcb), event);
86

87
    if ((event == RCB_EVENT_SET_PARAMETER) || (event == RCB_EVENT_GET_PARAMETER)) {
88
        printf("  param:  %s\n", parameterName);
89
        printf("  result: %i\n", serviceError);
90
    }
91

92
    if (event == RCB_EVENT_ENABLE) {
93
        char* rptId = ReportControlBlock_getRptID(rcb);
94
        printf("   rptID:  %s\n", rptId);
95
        char* dataSet = ReportControlBlock_getDataSet(rcb);
96
        printf("   datSet: %s\n", dataSet);
97

98
        free(rptId);
99
        free(dataSet);
100
    }
101
}
102

103
int
104
main(int argc, char** argv)
105
{
106
    printf("Using libIEC61850 version %s\n", LibIEC61850_getVersionString());
107

108
    /* Create new server configuration object */
109
    IedServerConfig config = IedServerConfig_create();
110

111
    /* Set buffer size for buffered report control blocks to 200000 bytes */
112
    IedServerConfig_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) */
115
    IedServerConfig_setEdition(config, IEC_61850_EDITION_2);
116

117
    /* Set the base path for the MMS file services */
118
    IedServerConfig_setFileServiceBasePath(config, "./vmd-filestore/");
119

120
    /* disable MMS file service */
121
    IedServerConfig_enableFileService(config, false);
122

123
    /* enable dynamic data set service */
124
    IedServerConfig_enableDynamicDataSetService(config, true);
125

126
    /* disable log service */
127
    IedServerConfig_enableLogService(config, false);
128

129
    /* set maximum number of clients */
130
    IedServerConfig_setMaxMmsConnections(config, 2);
131

132
    /* Create a new IEC 61850 server instance */
133
    iedServer = IedServer_createWithConfig(&iedModel, NULL, config);
134

135
    /* configuration object is no longer required */
136
    IedServerConfig_destroy(config);
137

138
    /* set the identity values for MMS identify service */
139
    IedServer_setServerIdentity(iedServer, "MZ", "basic io", "1.4.2");
140

141
    /* Install handler for operate command */
142
    IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1,
143
            (ControlHandler) controlHandlerForBinaryOutput,
144
            IEDMODEL_GenericIO_GGIO1_SPCSO1);
145

146
    IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO2,
147
            (ControlHandler) controlHandlerForBinaryOutput,
148
            IEDMODEL_GenericIO_GGIO1_SPCSO2);
149

150
    IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO3,
151
            (ControlHandler) controlHandlerForBinaryOutput,
152
            IEDMODEL_GenericIO_GGIO1_SPCSO3);
153

154
    IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO4,
155
            (ControlHandler) controlHandlerForBinaryOutput,
156
            IEDMODEL_GenericIO_GGIO1_SPCSO4);
157

158
    IedServer_setConnectionIndicationHandler(iedServer, (IedConnectionIndicationHandler) connectionHandler, NULL);
159

160
    IedServer_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
     */
166
    IedServer_setWriteAccessPolicy(iedServer, IEC61850_FC_DC, ACCESS_POLICY_ALLOW);
167

168
    /* MMS server will be instructed to start listening for client connections. */
169
    IedServer_start(iedServer, 102);
170

171
    if (!IedServer_isRunning(iedServer)) {
172
        printf("Starting server failed (maybe need root permissions or another server is already using the port)! Exit.\n");
173
        IedServer_destroy(iedServer);
174
        exit(-1);
175
    }
176

177
    running = 1;
178

179
    signal(SIGINT, sigint_handler);
180

181
    float t = 0.f;
182

183
    while (running) {
184
        uint64_t timestamp = Hal_getTimeInMs();
185

186
        t += 0.1f;
187

188
        float an1 = t;//sinf(t);
189
        float an2 = t + 1;//sinf(t + 1.f);
190
        float an3 = t + 2;//sinf(t + 2.f);
191
        float an4 = t + 3;//sinf(t + 3.f);
192

193
        Timestamp iecTimestamp;
194

195
        Timestamp_clearFlags(&iecTimestamp);
196
        Timestamp_setTimeInMilliseconds(&iecTimestamp, timestamp);
197
        Timestamp_setLeapSecondKnown(&iecTimestamp, true);
198

199
        /* toggle clock-not-synchronized flag in timestamp */
200
        if (((int) t % 2) == 0)
201
            Timestamp_setClockNotSynchronized(&iecTimestamp, true);
202

203
        IedServer_lockDataModel(iedServer);
204

205
        IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn1_t, &iecTimestamp);
206
        IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn1_mag_f, an1);
207

208
        IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn2_t, &iecTimestamp);
209
        IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn2_mag_f, an2);
210

211
        IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn3_t, &iecTimestamp);
212
        IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn3_mag_f, an3);
213

214
        IedServer_updateTimestampAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn4_t, &iecTimestamp);
215
        IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn4_mag_f, an4);
216

217
        IedServer_unlockDataModel(iedServer);
218

219
        Thread_sleep(100);
220
    }
221

222
    /* stop MMS server - close TCP server socket and all client sockets */
223
    IedServer_stop(iedServer);
224

225
    /* Cleanup - free all resources */
226
    IedServer_destroy(iedServer);
227

228
    return 0;
229
} /* main() */
230

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.