ua_client_discovery.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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) Mark Giraud, Fraunhofer IOSB
  7. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  8. */
  9. #include "ua_client_internal.h"
  10. UA_StatusCode
  11. UA_Client_getEndpoints(UA_Client *client, const char *serverUrl,
  12. size_t* endpointDescriptionsSize,
  13. UA_EndpointDescription** endpointDescriptions) {
  14. UA_Boolean connected = (client->state > UA_CLIENTSTATE_DISCONNECTED);
  15. /* Client is already connected to a different server */
  16. if(connected && strncmp((const char*)client->config.endpoint.endpointUrl.data, serverUrl,
  17. client->config.endpoint.endpointUrl.length) != 0) {
  18. return UA_STATUSCODE_BADINVALIDARGUMENT;
  19. }
  20. UA_StatusCode retval;
  21. const UA_String url = UA_STRING((char*)(uintptr_t)serverUrl);
  22. if(!connected) {
  23. retval = UA_Client_connectTCPSecureChannel(client, url);
  24. if(retval != UA_STATUSCODE_GOOD)
  25. return retval;
  26. }
  27. retval = UA_Client_getEndpointsInternal(client, url, endpointDescriptionsSize,
  28. endpointDescriptions);
  29. if(!connected)
  30. UA_Client_disconnect(client);
  31. return retval;
  32. }
  33. UA_StatusCode
  34. UA_Client_findServers(UA_Client *client, const char *serverUrl,
  35. size_t serverUrisSize, UA_String *serverUris,
  36. size_t localeIdsSize, UA_String *localeIds,
  37. size_t *registeredServersSize,
  38. UA_ApplicationDescription **registeredServers) {
  39. UA_Boolean connected = (client->state > UA_CLIENTSTATE_DISCONNECTED);
  40. /* Client is already connected to a different server */
  41. if(connected && strncmp((const char*)client->config.endpoint.endpointUrl.data, serverUrl,
  42. client->config.endpoint.endpointUrl.length) != 0) {
  43. return UA_STATUSCODE_BADINVALIDARGUMENT;
  44. }
  45. UA_StatusCode retval;
  46. const UA_String url = UA_STRING((char*)(uintptr_t)serverUrl);
  47. if(!connected) {
  48. retval = UA_Client_connectTCPSecureChannel(client, url);
  49. if(retval != UA_STATUSCODE_GOOD)
  50. return retval;
  51. }
  52. /* Prepare the request */
  53. UA_FindServersRequest request;
  54. UA_FindServersRequest_init(&request);
  55. request.serverUrisSize = serverUrisSize;
  56. request.serverUris = serverUris;
  57. request.localeIdsSize = localeIdsSize;
  58. request.localeIds = localeIds;
  59. /* Send the request */
  60. UA_FindServersResponse response;
  61. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_FINDSERVERSREQUEST],
  62. &response, &UA_TYPES[UA_TYPES_FINDSERVERSRESPONSE]);
  63. /* Process the response */
  64. retval = response.responseHeader.serviceResult;
  65. if(retval == UA_STATUSCODE_GOOD) {
  66. *registeredServersSize = response.serversSize;
  67. *registeredServers = response.servers;
  68. response.serversSize = 0;
  69. response.servers = NULL;
  70. } else {
  71. *registeredServersSize = 0;
  72. *registeredServers = NULL;
  73. }
  74. /* Clean up */
  75. UA_FindServersResponse_deleteMembers(&response);
  76. if(!connected)
  77. UA_Client_disconnect(client);
  78. return retval;
  79. }
  80. #ifdef UA_ENABLE_DISCOVERY
  81. UA_StatusCode
  82. UA_Client_findServersOnNetwork(UA_Client *client, const char *serverUrl,
  83. UA_UInt32 startingRecordId, UA_UInt32 maxRecordsToReturn,
  84. size_t serverCapabilityFilterSize, UA_String *serverCapabilityFilter,
  85. size_t *serverOnNetworkSize, UA_ServerOnNetwork **serverOnNetwork) {
  86. UA_Boolean connected = (client->state > UA_CLIENTSTATE_DISCONNECTED);
  87. /* Client is already connected to a different server */
  88. if(connected && strncmp((const char*)client->config.endpoint.endpointUrl.data, serverUrl,
  89. client->config.endpoint.endpointUrl.length) != 0) {
  90. return UA_STATUSCODE_BADINVALIDARGUMENT;
  91. }
  92. UA_StatusCode retval;
  93. const UA_String url = UA_STRING((char*)(uintptr_t)serverUrl);
  94. if(!connected) {
  95. retval = UA_Client_connectTCPSecureChannel(client, url);
  96. if(retval != UA_STATUSCODE_GOOD)
  97. return retval;
  98. }
  99. /* Prepare the request */
  100. UA_FindServersOnNetworkRequest request;
  101. UA_FindServersOnNetworkRequest_init(&request);
  102. request.startingRecordId = startingRecordId;
  103. request.maxRecordsToReturn = maxRecordsToReturn;
  104. request.serverCapabilityFilterSize = serverCapabilityFilterSize;
  105. request.serverCapabilityFilter = serverCapabilityFilter;
  106. /* Send the request */
  107. UA_FindServersOnNetworkResponse response;
  108. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_FINDSERVERSONNETWORKREQUEST],
  109. &response, &UA_TYPES[UA_TYPES_FINDSERVERSONNETWORKRESPONSE]);
  110. /* Process the response */
  111. retval = response.responseHeader.serviceResult;
  112. if(retval == UA_STATUSCODE_GOOD) {
  113. *serverOnNetworkSize = response.serversSize;
  114. *serverOnNetwork = response.servers;
  115. response.serversSize = 0;
  116. response.servers = NULL;
  117. } else {
  118. *serverOnNetworkSize = 0;
  119. *serverOnNetwork = NULL;
  120. }
  121. /* Clean up */
  122. UA_FindServersOnNetworkResponse_deleteMembers(&response);
  123. if(!connected)
  124. UA_Client_disconnect(client);
  125. return retval;
  126. }
  127. #endif