ua_client_discovery.c 5.2 KB

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