server_register.c 5.7 KB

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