server_register.c 5.8 KB

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