You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libiec61850/examples/goose_subscriber/goose_subscriber_example.c

120 lines
3.4 KiB
C

/*
* goose_subscriber_example.c
*
* This is an example for a standalone GOOSE subscriber
*
* Has to be started as root in Linux.
*/
#include "goose_receiver.h"
#include "goose_subscriber.h"
#include "hal_thread.h"
#include "linked_list.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
static int running = 1;
static void
sigint_handler(int signalId)
{
running = 0;
}
static void
gooseListener(GooseSubscriber subscriber, void* parameter)
{
printf("GOOSE event:\n");
printf(" stNum: %u sqNum: %u\n", GooseSubscriber_getStNum(subscriber),
GooseSubscriber_getSqNum(subscriber));
printf(" timeToLive: %u\n", GooseSubscriber_getTimeAllowedToLive(subscriber));
uint64_t timestamp = GooseSubscriber_getTimestamp(subscriber);
printf(" timestamp: %u.%u\n", (uint32_t) (timestamp / 1000), (uint32_t) (timestamp % 1000));
printf(" message is %s\n", GooseSubscriber_isValid(subscriber) ? "valid" : "INVALID");
MmsValue* values = GooseSubscriber_getDataSetValues(subscriber);
char buffer[1024];
MmsValue_printToBuffer(values, buffer, 1024);
printf(" allData: %s\n", buffer);
}
int
main(int argc, char** argv)
{
GooseReceiver receiver = GooseReceiver_create();
if (argc > 1) {
printf("Set interface id: %s\n", argv[1]);
GooseReceiver_setInterfaceId(receiver, argv[1]);
}
else {
printf("Using interface eth0\n");
GooseReceiver_setInterfaceId(receiver, "eth0");
}
GooseSubscriber subscriber = GooseSubscriber_create("simpleIOGenericIO/LLN0$GO$gcbAnalogValues", NULL);
uint8_t dstMac[6] = {0x01,0x0c,0xcd,0x01,0x00,0x01};
GooseSubscriber_setDstMac(subscriber, dstMac);
GooseSubscriber_setAppId(subscriber, 1000);
char* key = "0123456789ABCDEF";
L2Security l2Sec = L2Security_create();
//L2Security_addKey(l2Sec, 0x12345678, (uint8_t*)key, 16, MC_SEC_SEC_ALGO_NONE, MC_SEC_SIG_ALGO_HMAC_SHA256_256);
L2Security_addKey(l2Sec, 1, (uint8_t*)key, 16, MC_SEC_SEC_ALGO_NONE, MC_SEC_SIG_ALGO_AES_GMAC_128);
L2Security_setActiveKey(l2Sec, 1);
GooseSubscriber_setL2Security(subscriber, l2Sec);
GooseSubscriber_setListener(subscriber, gooseListener, NULL);
GooseReceiver_addSubscriber(receiver, subscriber);
GooseSubscriber subscriber2 = GooseSubscriber_create("simpleIOGenericIO/LLN0$GO$gcbAnalogValues2", NULL);
uint8_t dstMac2[6] = {0x01,0x0c,0xcd,0x01,0x00,0x02};
GooseSubscriber_setDstMac(subscriber2, dstMac2);
GooseSubscriber_setAppId(subscriber2, 1001);
char* key2 = "ABCDEF0123456789";
L2Security l2Sec2 = L2Security_create();
//L2Security_addKey(l2Sec, 0x12345678, (uint8_t*)key, 16, MC_SEC_SEC_ALGO_NONE, MC_SEC_SIG_ALGO_HMAC_SHA256_256);
L2Security_addKey(l2Sec2, 1, (uint8_t*)key2, 16, MC_SEC_SEC_ALGO_NONE, MC_SEC_SIG_ALGO_AES_GMAC_128);
L2Security_setActiveKey(l2Sec2, 1);
GooseSubscriber_setL2Security(subscriber2, l2Sec2);
GooseSubscriber_setListener(subscriber2, gooseListener, NULL);
GooseReceiver_addSubscriber(receiver, subscriber2);
GooseReceiver_start(receiver);
if (GooseReceiver_isRunning(receiver)) {
signal(SIGINT, sigint_handler);
while (running) {
Thread_sleep(100);
}
}
else {
printf("Failed to start GOOSE subscriber. Reason can be that the Ethernet interface doesn't exist or root permission are required.\n");
}
GooseReceiver_stop(receiver);
GooseReceiver_destroy(receiver);
return 0;
}