Commit 19633145 authored by Ivan Tyagov's avatar Ivan Tyagov

Move to a dedicated file.

parent 211bcc7a
/*
Keep alive implementation for couplers based on OPC UA's pub/sub mechanism
*/
// global HEART BEATs of coupler$
static unsigned int HEART_BEATS = 0;
// the heart beat interval$
static int HEART_BEAT_INTERVAL = 250;
// both publisher and subscriber should use same publisher id
const int PUBLISHER_ID = 2234;
......@@ -137,3 +144,60 @@ static void addPubSubDataSetField(UA_Server *server, PublishedVariable varDetail
UA_Server_addDataSetField(server, publishedDataSetIdent,
&dataSetFieldConfig, &dataSetFieldIdent);
}
void callbackTicHeartBeat()
{
/* Increase periodically heart beats of the server */
HEART_BEATS += 1;
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "heart_beat %d", HEART_BEATS);
// set OPC UA's heat_beat node value
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "heart_beat");
UA_UInt32 myInteger = HEART_BEATS;
UA_Variant myVar;
UA_Variant_init(&myVar);
UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_UINT32]);
UA_Server_writeValue(server, myIntegerNodeId, myVar);
}
static void enablePublishHeartBeat(UA_Server *erver, UA_ServerConfig *config){
int i;
// add a callback which will increment heart beat tics
UA_UInt64 callbackId = 1;
UA_Server_addRepeatedCallback(server, callbackTicHeartBeat, NULL, HEART_BEAT_INTERVAL, &callbackId);
UA_UInt32 defaultUInt32 = 0;
UA_UInt32 couplerID = COUPLER_ID;
const PublishedVariable publishedVariableArray[] = {
// representing time in millis since start of process
{
.name = "heart_beat",
.description = "Heart beat",
.pdefaultValue = &defaultUInt32,
.type = UA_TYPES_UINT32
},
// representing the ID of the coupler
{
.name = "id",
.description = "ID",
.pdefaultValue = &couplerID,
.type = UA_TYPES_UINT32
}
};
UA_String transportProfile =
UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
UA_NetworkAddressUrlDataType networkAddressUrl =
{UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
UA_ServerConfig_addPubSubTransportLayer(config, UA_PubSubTransportLayerUDPMP());
addPubSubConnection(server, &transportProfile, &networkAddressUrl);
addPublishedDataSet(server);
for(i = 0; i < countof(publishedVariableArray); i++) {
addPubSubVariable(server, publishedVariableArray[i]);
addPubSubDataSetField(server, publishedVariableArray[i]);
}
addWriterGroup(server);
addDataSetWriter(server);
}
......@@ -25,7 +25,6 @@
#include "common.h"
#include "mod_io_i2c.h"
#include "mod_io_opc_ua.h"
#include "keep_alive.h"
#include <time.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
......@@ -37,15 +36,11 @@
// global Id of coupler
static int COUPLER_ID = 0;
// global HEART BEATs of coupler
static unsigned int HEART_BEATS = 0;
// the heart beat interval
static int HEART_BEAT_INTERVAL = 250;
// global server
UA_Server *server;
#include "keep_alive.h"
// The default port of OPC-UA server
const int DEFAULT_OPC_UA_PORT = 4840;
const int DEFAULT_MODE = 0;
......@@ -131,20 +126,6 @@ static void stopHandler(int sign)
running = false;
}
void callbackTicHeartBeat()
{
/* Increase periodically heart beats of the server */
HEART_BEATS += 1;
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "heart_beat %d", HEART_BEATS);
// set OPC UA's heat_beat node value
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "heart_beat");
UA_UInt32 myInteger = HEART_BEATS;
UA_Variant myVar;
UA_Variant_init(&myVar);
UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_UINT32]);
UA_Server_writeValue(server, myIntegerNodeId, myVar);
}
int main(int argc, char **argv)
{
......@@ -254,43 +235,7 @@ int main(int argc, char **argv)
#endif
// enable keep-alive
// add a callback which will increment heart beat tics
UA_UInt64 callbackId = 1;
UA_Server_addRepeatedCallback(server, callbackTicHeartBeat, NULL, HEART_BEAT_INTERVAL, &callbackId);
UA_UInt32 defaultUInt32 = 0;
UA_UInt32 couplerID = COUPLER_ID;
const PublishedVariable publishedVariableArray[] = {
// representing time in millis since start of process
{
.name = "heart_beat",
.description = "Heart beat",
.pdefaultValue = &defaultUInt32,
.type = UA_TYPES_UINT32
},
// representing the ID of the coupler
{
.name = "id",
.description = "ID",
.pdefaultValue = &couplerID,
.type = UA_TYPES_UINT32
}
};
UA_String transportProfile =
UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
UA_NetworkAddressUrlDataType networkAddressUrl =
{UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
UA_ServerConfig_addPubSubTransportLayer(config, UA_PubSubTransportLayerUDPMP());
addPubSubConnection(server, &transportProfile, &networkAddressUrl);
addPublishedDataSet(server);
for(i = 0; i < countof(publishedVariableArray); i++) {
addPubSubVariable(server, publishedVariableArray[i]);
addPubSubDataSetField(server, publishedVariableArray[i]);
}
addWriterGroup(server);
addDataSetWriter(server);
enablePublishHeartBeat(server, config);
// run server
UA_StatusCode retval = UA_Server_run(server, &running);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment