server_register.c 5.7 KB

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