server_register.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /*
  4. * A simple server instance which registers with the discovery server (see server_lds.c).
  5. * Before shutdown it has to unregister itself.
  6. */
  7. #include <open62541/client.h>
  8. #include <open62541/client_config_default.h>
  9. #include <open62541/plugin/log_stdout.h>
  10. #include <open62541/server.h>
  11. #include <open62541/server_config_default.h>
  12. #include <signal.h>
  13. #include <stdlib.h>
  14. #define DISCOVERY_SERVER_ENDPOINT "opc.tcp://localhost:4840"
  15. UA_Boolean running = true;
  16. static void stopHandler(int sign) {
  17. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  18. running = false;
  19. }
  20. static UA_StatusCode
  21. readInteger(UA_Server *server, const UA_NodeId *sessionId,
  22. void *sessionContext, const UA_NodeId *nodeId,
  23. void *nodeContext, UA_Boolean includeSourceTimeStamp,
  24. const UA_NumericRange *range, UA_DataValue *value) {
  25. UA_Int32 *myInteger = (UA_Int32*)nodeContext;
  26. value->hasValue = true;
  27. UA_Variant_setScalarCopy(&value->value, myInteger, &UA_TYPES[UA_TYPES_INT32]);
  28. // we know the nodeid is a string
  29. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Node read %.*s",
  30. (int)nodeId->identifier.string.length,
  31. nodeId->identifier.string.data);
  32. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  33. "read value %i", *(UA_UInt32 *)myInteger);
  34. return UA_STATUSCODE_GOOD;
  35. }
  36. static UA_StatusCode
  37. writeInteger(UA_Server *server, const UA_NodeId *sessionId,
  38. void *sessionContext, const UA_NodeId *nodeId,
  39. void *nodeContext, const UA_NumericRange *range,
  40. const UA_DataValue *value) {
  41. UA_Int32 *myInteger = (UA_Int32*)nodeContext;
  42. if(value->hasValue && UA_Variant_isScalar(&value->value) &&
  43. value->value.type == &UA_TYPES[UA_TYPES_INT32] && value->value.data)
  44. *myInteger = *(UA_Int32 *)value->value.data;
  45. // we know the nodeid is a string
  46. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Node written %.*s",
  47. (int)nodeId->identifier.string.length,
  48. nodeId->identifier.string.data);
  49. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  50. "written value %i", *(UA_UInt32 *)myInteger);
  51. return UA_STATUSCODE_GOOD;
  52. }
  53. int main(int argc, char **argv) {
  54. signal(SIGINT, stopHandler); /* catches ctrl-c */
  55. signal(SIGTERM, stopHandler);
  56. UA_Server *server = UA_Server_new();
  57. UA_ServerConfig *config = UA_Server_getConfig(server);
  58. // use port 0 to dynamically assign port
  59. UA_ServerConfig_setMinimal(config, 0, NULL);
  60. UA_String_clear(&config->applicationDescription.applicationUri);
  61. config->applicationDescription.applicationUri =
  62. UA_String_fromChars("urn:open62541.example.server_register");
  63. config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Server");
  64. // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
  65. //config.serverCapabilitiesSize = 1;
  66. //UA_String caps = UA_String_fromChars("LDS");
  67. //config.serverCapabilities = &caps;
  68. /* add a variable node to the address space */
  69. UA_Int32 myInteger = 42;
  70. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  71. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  72. UA_DataSource dateDataSource;
  73. dateDataSource.read = readInteger;
  74. dateDataSource.write = writeInteger;
  75. UA_VariableAttributes attr = UA_VariableAttributes_default;
  76. attr.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  77. attr.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  78. UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
  79. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  80. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  81. myIntegerName, UA_NODEID_NULL, attr, dateDataSource,
  82. &myInteger, NULL);
  83. UA_Client *clientRegister = UA_Client_new();
  84. UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegister));
  85. // periodic server register after 10 Minutes, delay first register for 500ms
  86. UA_StatusCode retval =
  87. UA_Server_addPeriodicServerRegisterCallback(server, clientRegister, DISCOVERY_SERVER_ENDPOINT,
  88. 10 * 60 * 1000, 500, NULL);
  89. // UA_StatusCode retval = UA_Server_addPeriodicServerRegisterJob(server,
  90. // "opc.tcp://localhost:4840", 10*60*1000, 500, NULL);
  91. if(retval != UA_STATUSCODE_GOOD) {
  92. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  93. "Could not create periodic job for server register. StatusCode %s",
  94. UA_StatusCode_name(retval));
  95. UA_Client_disconnect(clientRegister);
  96. UA_Client_delete(clientRegister);
  97. UA_Server_delete(server);
  98. UA_Server_delete(server);
  99. return EXIT_FAILURE;
  100. }
  101. retval = UA_Server_run(server, &running);
  102. if(retval != UA_STATUSCODE_GOOD) {
  103. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  104. "Could not start the server. StatusCode %s",
  105. UA_StatusCode_name(retval));
  106. UA_Client_disconnect(clientRegister);
  107. UA_Client_delete(clientRegister);
  108. UA_Server_delete(server);
  109. return EXIT_FAILURE;
  110. }
  111. // Unregister the server from the discovery server.
  112. retval = UA_Server_unregister_discovery(server, clientRegister);
  113. //retval = UA_Server_unregister_discovery(server, "opc.tcp://localhost:4840" );
  114. if(retval != UA_STATUSCODE_GOOD)
  115. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  116. "Could not unregister server from discovery server. StatusCode %s",
  117. UA_StatusCode_name(retval));
  118. UA_Client_disconnect(clientRegister);
  119. UA_Client_delete(clientRegister);
  120. UA_Server_delete(server);
  121. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;;
  122. }