server_register.c 5.9 KB

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