server_register.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_ServerConfig *config = UA_ServerConfig_new_minimal(4841, NULL);;
  57. UA_String_clear(&config->applicationDescription.applicationUri);
  58. config->applicationDescription.applicationUri =
  59. UA_String_fromChars("urn:open62541.example.server_register");
  60. config->mdnsServerName = UA_String_fromChars("Sample Server");
  61. // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
  62. //config.serverCapabilitiesSize = 1;
  63. //UA_String caps = UA_String_fromChars("LDS");
  64. //config.serverCapabilities = &caps;
  65. UA_Server *server = UA_Server_new(config);
  66. /* add a variable node to the address space */
  67. UA_Int32 myInteger = 42;
  68. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  69. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  70. UA_DataSource dateDataSource;
  71. dateDataSource.read = readInteger;
  72. dateDataSource.write = writeInteger;
  73. UA_VariableAttributes attr = UA_VariableAttributes_default;
  74. attr.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  75. attr.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  76. UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
  77. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  78. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  79. myIntegerName, UA_NODEID_NULL, attr, dateDataSource,
  80. &myInteger, NULL);
  81. UA_Client *clientRegister = UA_Client_new();
  82. UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegister));
  83. // periodic server register after 10 Minutes, delay first register for 500ms
  84. UA_StatusCode retval =
  85. UA_Server_addPeriodicServerRegisterCallback(server, clientRegister, DISCOVERY_SERVER_ENDPOINT,
  86. 10 * 60 * 1000, 500, NULL);
  87. // UA_StatusCode retval = UA_Server_addPeriodicServerRegisterJob(server,
  88. // "opc.tcp://localhost:4840", 10*60*1000, 500, NULL);
  89. if(retval != UA_STATUSCODE_GOOD) {
  90. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  91. "Could not create periodic job for server register. StatusCode %s",
  92. UA_StatusCode_name(retval));
  93. UA_Client_disconnect(clientRegister);
  94. UA_Client_delete(clientRegister);
  95. UA_Server_delete(server);
  96. UA_ServerConfig_delete(config);
  97. return EXIT_FAILURE;
  98. }
  99. retval = UA_Server_run(server, &running);
  100. if(retval != UA_STATUSCODE_GOOD) {
  101. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  102. "Could not start the server. StatusCode %s",
  103. UA_StatusCode_name(retval));
  104. UA_Client_disconnect(clientRegister);
  105. UA_Client_delete(clientRegister);
  106. UA_Server_delete(server);
  107. UA_ServerConfig_delete(config);
  108. return EXIT_FAILURE;
  109. }
  110. // Unregister the server from the discovery server.
  111. retval = UA_Server_unregister_discovery(server, clientRegister);
  112. //retval = UA_Server_unregister_discovery(server, "opc.tcp://localhost:4840" );
  113. if(retval != UA_STATUSCODE_GOOD)
  114. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  115. "Could not unregister server from discovery server. StatusCode %s",
  116. UA_StatusCode_name(retval));
  117. UA_Client_disconnect(clientRegister);
  118. UA_Client_delete(clientRegister);
  119. UA_Server_delete(server);
  120. UA_ServerConfig_delete(config);
  121. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;;
  122. }