server_register.c 5.6 KB

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