ua_server_discovery.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. */
  8. #include <open62541/client.h>
  9. #include "ua_server_internal.h"
  10. #ifdef UA_ENABLE_DISCOVERY
  11. static UA_StatusCode
  12. register_server_with_discovery_server(UA_Server *server,
  13. UA_Client *client,
  14. const UA_Boolean isUnregister,
  15. const char* semaphoreFilePath) {
  16. /* Prepare the request. Do not cleanup the request after the service call,
  17. * as the members are stack-allocated or point into the server config. */
  18. UA_RegisterServer2Request request;
  19. UA_RegisterServer2Request_init(&request);
  20. request.requestHeader.timestamp = UA_DateTime_now();
  21. request.requestHeader.timeoutHint = 10000;
  22. request.server.isOnline = !isUnregister;
  23. request.server.serverUri = server->config.applicationDescription.applicationUri;
  24. request.server.productUri = server->config.applicationDescription.productUri;
  25. request.server.serverType = server->config.applicationDescription.applicationType;
  26. request.server.gatewayServerUri = server->config.applicationDescription.gatewayServerUri;
  27. if(semaphoreFilePath) {
  28. #ifdef UA_ENABLE_DISCOVERY_SEMAPHORE
  29. request.server.semaphoreFilePath =
  30. UA_STRING((char*)(uintptr_t)semaphoreFilePath); /* dirty cast */
  31. #else
  32. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_CLIENT,
  33. "Ignoring semaphore file path. open62541 not compiled "
  34. "with UA_ENABLE_DISCOVERY_SEMAPHORE=ON");
  35. #endif
  36. }
  37. request.server.serverNames = &server->config.applicationDescription.applicationName;
  38. request.server.serverNamesSize = 1;
  39. /* Copy the discovery urls from the server config and the network layers*/
  40. size_t config_discurls = server->config.applicationDescription.discoveryUrlsSize;
  41. size_t nl_discurls = server->config.networkLayersSize;
  42. size_t total_discurls = config_discurls + nl_discurls;
  43. UA_STACKARRAY(UA_String, urlsBuf, total_discurls);
  44. request.server.discoveryUrls = urlsBuf;
  45. request.server.discoveryUrlsSize = total_discurls;
  46. for(size_t i = 0; i < config_discurls; ++i)
  47. request.server.discoveryUrls[i] = server->config.applicationDescription.discoveryUrls[i];
  48. /* TODO: Add nl only if discoveryUrl not already present */
  49. for(size_t i = 0; i < nl_discurls; ++i) {
  50. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  51. request.server.discoveryUrls[config_discurls + i] = nl->discoveryUrl;
  52. }
  53. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  54. request.discoveryConfigurationSize = 1;
  55. request.discoveryConfiguration = UA_ExtensionObject_new();
  56. UA_ExtensionObject_init(&request.discoveryConfiguration[0]);
  57. // Set to NODELETE so that we can just use a pointer to the mdns config
  58. request.discoveryConfiguration[0].encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  59. request.discoveryConfiguration[0].content.decoded.type = &UA_TYPES[UA_TYPES_MDNSDISCOVERYCONFIGURATION];
  60. request.discoveryConfiguration[0].content.decoded.data = &server->config.discovery.mdns;
  61. #endif
  62. // First try with RegisterServer2, if that isn't implemented, use RegisterServer
  63. UA_RegisterServer2Response response;
  64. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERSERVER2REQUEST],
  65. &response, &UA_TYPES[UA_TYPES_REGISTERSERVER2RESPONSE]);
  66. UA_StatusCode serviceResult = response.responseHeader.serviceResult;
  67. UA_RegisterServer2Response_deleteMembers(&response);
  68. UA_Array_delete(request.discoveryConfiguration,
  69. request.discoveryConfigurationSize,
  70. &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  71. request.discoveryConfiguration = NULL;
  72. request.discoveryConfigurationSize = 0;
  73. if(serviceResult == UA_STATUSCODE_BADNOTIMPLEMENTED ||
  74. serviceResult == UA_STATUSCODE_BADSERVICEUNSUPPORTED) {
  75. /* Try RegisterServer */
  76. UA_RegisterServerRequest request_fallback;
  77. UA_RegisterServerRequest_init(&request_fallback);
  78. /* Copy from RegisterServer2 request */
  79. request_fallback.requestHeader = request.requestHeader;
  80. request_fallback.server = request.server;
  81. UA_RegisterServerResponse response_fallback;
  82. __UA_Client_Service(client, &request_fallback,
  83. &UA_TYPES[UA_TYPES_REGISTERSERVERREQUEST],
  84. &response_fallback,
  85. &UA_TYPES[UA_TYPES_REGISTERSERVERRESPONSE]);
  86. serviceResult = response_fallback.responseHeader.serviceResult;
  87. UA_RegisterServerResponse_deleteMembers(&response_fallback);
  88. }
  89. if(serviceResult != UA_STATUSCODE_GOOD) {
  90. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_CLIENT,
  91. "RegisterServer/RegisterServer2 failed with statuscode %s",
  92. UA_StatusCode_name(serviceResult));
  93. }
  94. return serviceResult;
  95. }
  96. UA_StatusCode
  97. UA_Server_register_discovery(UA_Server *server, UA_Client *client,
  98. const char* semaphoreFilePath) {
  99. return register_server_with_discovery_server(server, client,
  100. false, semaphoreFilePath);
  101. }
  102. UA_StatusCode
  103. UA_Server_unregister_discovery(UA_Server *server, UA_Client *client) {
  104. return register_server_with_discovery_server(server, client,
  105. true, NULL);
  106. }
  107. #endif /* UA_ENABLE_DISCOVERY */