123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include <open62541/plugin/log_stdout.h>
- #include <open62541/server.h>
- #include <open62541/server_config_default.h>
- #include <signal.h>
- #include <stdlib.h>
- static void
- updateCurrentTime(UA_Server *server) {
- UA_DateTime now = UA_DateTime_now();
- UA_Variant value;
- UA_Variant_setScalar(&value, &now, &UA_TYPES[UA_TYPES_DATETIME]);
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time-value-callback");
- UA_Server_writeValue(server, currentNodeId, value);
- }
- static void
- addCurrentTimeVariable(UA_Server *server) {
- UA_DateTime now = 0;
- UA_VariableAttributes attr = UA_VariableAttributes_default;
- attr.displayName = UA_LOCALIZEDTEXT("en-US", "Current time - value callback");
- attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
- UA_Variant_setScalar(&attr.value, &now, &UA_TYPES[UA_TYPES_DATETIME]);
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time-value-callback");
- UA_QualifiedName currentName = UA_QUALIFIEDNAME(1, "current-time-value-callback");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_NodeId variableTypeNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE);
- UA_Server_addVariableNode(server, currentNodeId, parentNodeId,
- parentReferenceNodeId, currentName,
- variableTypeNodeId, attr, NULL, NULL);
- updateCurrentTime(server);
- }
- static void
- beforeReadTime(UA_Server *server,
- const UA_NodeId *sessionId, void *sessionContext,
- const UA_NodeId *nodeid, void *nodeContext,
- const UA_NumericRange *range, const UA_DataValue *data) {
- updateCurrentTime(server);
- }
- static void
- afterWriteTime(UA_Server *server,
- const UA_NodeId *sessionId, void *sessionContext,
- const UA_NodeId *nodeId, void *nodeContext,
- const UA_NumericRange *range, const UA_DataValue *data) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "The variable was updated");
- }
- static void
- addValueCallbackToCurrentTimeVariable(UA_Server *server) {
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time-value-callback");
- UA_ValueCallback callback ;
- callback.onRead = beforeReadTime;
- callback.onWrite = afterWriteTime;
- UA_Server_setVariableNode_valueCallback(server, currentNodeId, callback);
- }
- static UA_StatusCode
- readCurrentTime(UA_Server *server,
- const UA_NodeId *sessionId, void *sessionContext,
- const UA_NodeId *nodeId, void *nodeContext,
- UA_Boolean sourceTimeStamp, const UA_NumericRange *range,
- UA_DataValue *dataValue) {
- UA_DateTime now = UA_DateTime_now();
- UA_Variant_setScalarCopy(&dataValue->value, &now,
- &UA_TYPES[UA_TYPES_DATETIME]);
- dataValue->hasValue = true;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- writeCurrentTime(UA_Server *server,
- const UA_NodeId *sessionId, void *sessionContext,
- const UA_NodeId *nodeId, void *nodeContext,
- const UA_NumericRange *range, const UA_DataValue *data) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Changing the system time is not implemented");
- return UA_STATUSCODE_BADINTERNALERROR;
- }
- static void
- addCurrentTimeDataSourceVariable(UA_Server *server) {
- UA_VariableAttributes attr = UA_VariableAttributes_default;
- attr.displayName = UA_LOCALIZEDTEXT("en-US", "Current time - data source");
- attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
- UA_NodeId currentNodeId = UA_NODEID_STRING(1, "current-time-datasource");
- UA_QualifiedName currentName = UA_QUALIFIEDNAME(1, "current-time-datasource");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_NodeId variableTypeNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE);
- UA_DataSource timeDataSource;
- timeDataSource.read = readCurrentTime;
- timeDataSource.write = writeCurrentTime;
- UA_Server_addDataSourceVariableNode(server, currentNodeId, parentNodeId,
- parentReferenceNodeId, currentName,
- variableTypeNodeId, attr,
- timeDataSource, NULL, NULL);
- }
- static volatile UA_Boolean running = true;
- static void stopHandler(int sign) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
- running = false;
- }
- int main(void) {
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- UA_Server *server = UA_Server_new();
- UA_ServerConfig_setDefault(UA_Server_getConfig(server));
- addCurrentTimeVariable(server);
- addValueCallbackToCurrentTimeVariable(server);
- addCurrentTimeDataSourceVariable(server);
- UA_StatusCode retval = UA_Server_run(server, &running);
- UA_Server_delete(server);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|