123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include <open62541/plugin/log_stdout.h>
- #include <open62541/server.h>
- #include <open62541/server_config_default.h>
- #include <signal.h>
- #include <stdlib.h>
- static void
- addVariable(UA_Server *server) {
-
- UA_VariableAttributes attr = UA_VariableAttributes_default;
- UA_Int32 myInteger = 42;
- UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
- attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
- attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
- attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
- attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
-
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
- UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
- parentReferenceNodeId, myIntegerName,
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
- }
- static void
- addMatrixVariable(UA_Server *server) {
- UA_VariableAttributes attr = UA_VariableAttributes_default;
- attr.displayName = UA_LOCALIZEDTEXT("en-US", "Double Matrix");
- attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
-
- attr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
- attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
- UA_UInt32 arrayDims[2] = {2,2};
- attr.arrayDimensions = arrayDims;
- attr.arrayDimensionsSize = 2;
-
- UA_Double zero[4] = {0.0, 0.0, 0.0, 0.0};
- UA_Variant_setArray(&attr.value, zero, 4, &UA_TYPES[UA_TYPES_DOUBLE]);
- attr.value.arrayDimensions = arrayDims;
- attr.value.arrayDimensionsSize = 2;
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "double.matrix");
- UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "double matrix");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
- parentReferenceNodeId, myIntegerName,
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
- attr, NULL, NULL);
- }
- static void
- writeVariable(UA_Server *server) {
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
-
- UA_Int32 myInteger = 43;
- UA_Variant myVar;
- UA_Variant_init(&myVar);
- UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
- UA_Server_writeValue(server, myIntegerNodeId, myVar);
-
- UA_WriteValue wv;
- UA_WriteValue_init(&wv);
- wv.nodeId = myIntegerNodeId;
- wv.attributeId = UA_ATTRIBUTEID_VALUE;
- wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
- wv.value.hasStatus = true;
- UA_Server_write(server, &wv);
-
- wv.value.hasStatus = false;
- wv.value.value = myVar;
- wv.value.hasValue = true;
- UA_Server_write(server, &wv);
- }
- static void
- writeWrongVariable(UA_Server *server) {
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
-
- UA_String myString = UA_STRING("test");
- UA_Variant myVar;
- UA_Variant_init(&myVar);
- UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
- UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
- printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
- }
- 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));
- UA_ServerConfig* config = UA_Server_getConfig(server);
- config->verifyRequestTimestamp = UA_RULEHANDLING_ACCEPT;
- #ifdef UA_ENABLE_WEBSOCKET_SERVER
- UA_ServerConfig_addNetworkLayerWS(UA_Server_getConfig(server), 7681, 0, 0);
- #endif
- addVariable(server);
- addMatrixVariable(server);
- writeVariable(server);
- writeWrongVariable(server);
- UA_StatusCode retval = UA_Server_run(server, &running);
- UA_Server_delete(server);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|