ua_services_view.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #include "ua_server_internal.h"
  2. #include "ua_services.h"
  3. #include "ua_statuscodes.h"
  4. #include "ua_nodestore.h"
  5. #include "ua_util.h"
  6. static UA_StatusCode fillrefdescr(UA_NodeStore *ns, const UA_Node *curr, UA_ReferenceNode *ref,
  7. UA_UInt32 mask, UA_ReferenceDescription *descr)
  8. {
  9. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  10. UA_ReferenceDescription_init(descr);
  11. retval |= UA_NodeId_copy(&curr->nodeId, &descr->nodeId.nodeId);
  12. //TODO: ExpandedNodeId is mocked up
  13. descr->nodeId.serverIndex = 0;
  14. descr->nodeId.namespaceUri.length = -1;
  15. if(mask & UA_BROWSERESULTMASK_REFERENCETYPEID)
  16. retval |= UA_NodeId_copy(&ref->referenceTypeId, &descr->referenceTypeId);
  17. if(mask & UA_BROWSERESULTMASK_ISFORWARD)
  18. descr->isForward = !ref->isInverse;
  19. if(mask & UA_BROWSERESULTMASK_NODECLASS)
  20. retval |= UA_NodeClass_copy(&curr->nodeClass, &descr->nodeClass);
  21. if(mask & UA_BROWSERESULTMASK_BROWSENAME)
  22. retval |= UA_QualifiedName_copy(&curr->browseName, &descr->browseName);
  23. if(mask & UA_BROWSERESULTMASK_DISPLAYNAME)
  24. retval |= UA_LocalizedText_copy(&curr->displayName, &descr->displayName);
  25. if(mask & UA_BROWSERESULTMASK_TYPEDEFINITION){
  26. if(curr->nodeClass == UA_NODECLASS_OBJECT || curr->nodeClass == UA_NODECLASS_VARIABLE) {
  27. for(UA_Int32 i = 0; i < curr->referencesSize; i++) {
  28. UA_ReferenceNode *refnode = &curr->references[i];
  29. if(refnode->referenceTypeId.identifier.numeric != UA_NS0ID_HASTYPEDEFINITION)
  30. continue;
  31. retval |= UA_ExpandedNodeId_copy(&refnode->targetId, &descr->typeDefinition);
  32. break;
  33. }
  34. }
  35. }
  36. if(retval)
  37. UA_ReferenceDescription_deleteMembers(descr);
  38. return retval;
  39. }
  40. /* Tests if the node is relevant to the browse request and shall be returned. If
  41. so, it is retrieved from the Nodestore. If not, null is returned. */
  42. static const UA_Node *relevant_node(UA_NodeStore *ns, const UA_BrowseDescription *descr,
  43. UA_Boolean return_all, UA_ReferenceNode *reference,
  44. UA_NodeId *relevant, size_t relevant_count)
  45. {
  46. if(reference->isInverse == UA_TRUE && descr->browseDirection == UA_BROWSEDIRECTION_FORWARD)
  47. return UA_NULL;
  48. else if(reference->isInverse == UA_FALSE && descr->browseDirection == UA_BROWSEDIRECTION_INVERSE)
  49. return UA_NULL;
  50. if(!return_all) {
  51. for(size_t i = 0; i < relevant_count; i++) {
  52. if(UA_NodeId_equal(&reference->referenceTypeId, &relevant[i]))
  53. goto is_relevant;
  54. }
  55. return UA_NULL;
  56. }
  57. is_relevant: ;
  58. const UA_Node *node = UA_NodeStore_get(ns, &reference->targetId.nodeId);
  59. if(node && descr->nodeClassMask != 0 && (node->nodeClass & descr->nodeClassMask) == 0) {
  60. UA_NodeStore_release(node);
  61. return UA_NULL;
  62. }
  63. return node;
  64. }
  65. /**
  66. * We find all subtypes by a single iteration over the array. We start with an array with a single
  67. * root nodeid at the beginning. When we find relevant references, we add the nodeids to the back of
  68. * the array and increase the size. Since the hierarchy is not cyclic, we can safely progress in the
  69. * array to process the newly found referencetype nodeids (emulated recursion).
  70. */
  71. static UA_StatusCode findsubtypes(UA_NodeStore *ns, const UA_NodeId *root, UA_NodeId **reftypes,
  72. size_t *reftypes_count) {
  73. const UA_Node *node = UA_NodeStore_get(ns, root);
  74. if(!node)
  75. return UA_STATUSCODE_BADNOMATCH;
  76. if(node->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  77. UA_NodeStore_release(node);
  78. return UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  79. }
  80. UA_NodeStore_release(node);
  81. size_t results_size = 20; // probably too big, but saves mallocs
  82. UA_NodeId *results = UA_malloc(sizeof(UA_NodeId) * results_size);
  83. if(!results)
  84. return UA_STATUSCODE_BADOUTOFMEMORY;
  85. UA_StatusCode retval = UA_NodeId_copy(root, &results[0]);
  86. if(retval != UA_STATUSCODE_GOOD) {
  87. UA_free(results);
  88. return retval;
  89. }
  90. size_t index = 0; // where are we currently in the array?
  91. size_t last = 0; // where is the last element in the array?
  92. do {
  93. node = UA_NodeStore_get(ns, &results[index]);
  94. if(!node || node->nodeClass != UA_NODECLASS_REFERENCETYPE)
  95. continue;
  96. for(UA_Int32 i = 0; i < node->referencesSize; i++) {
  97. if(node->references[i].referenceTypeId.identifier.numeric != UA_NS0ID_HASSUBTYPE ||
  98. node->references[i].isInverse == UA_TRUE)
  99. continue;
  100. if(++last >= results_size) { // is the array big enough?
  101. UA_NodeId *new_results = UA_realloc(results, sizeof(UA_NodeId) * results_size * 2);
  102. if(!new_results) {
  103. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  104. break;
  105. }
  106. results = new_results;
  107. results_size *= 2;
  108. }
  109. retval = UA_NodeId_copy(&node->references[i].targetId.nodeId, &results[last]);
  110. if(retval != UA_STATUSCODE_GOOD) {
  111. last--; // for array_delete
  112. break;
  113. }
  114. }
  115. UA_NodeStore_release(node);
  116. } while(++index <= last && retval == UA_STATUSCODE_GOOD);
  117. if(retval) {
  118. UA_Array_delete(results, &UA_TYPES[UA_TYPES_NODEID], last);
  119. return retval;
  120. }
  121. *reftypes = results;
  122. *reftypes_count = last + 1;
  123. return UA_STATUSCODE_GOOD;
  124. }
  125. /**
  126. * Results for a single browsedescription. This is the inner loop for both Browse and BrowseNext
  127. * @param session Session to save continuationpoints
  128. * @param ns The nodstore where the to-be-browsed node can be found
  129. * @param cp If cp points to a continuationpoint, we continue from there.
  130. * If cp is null, we can add a new continuation point if possible and necessary.
  131. * @param descr If no cp is set, we take the browsedescription from there
  132. * @param maxrefs The maximum number of references the client has requested
  133. * @param result The entry in the request
  134. */
  135. static void browse(UA_Session *session, UA_NodeStore *ns, struct ContinuationPointEntry *cp,
  136. const UA_BrowseDescription *descr, UA_UInt32 maxrefs, UA_BrowseResult *result) {
  137. UA_UInt32 continuationIndex = 0;
  138. size_t referencesCount = 0;
  139. UA_Int32 referencesIndex = 0;
  140. /* set the browsedescription if a cp is given */
  141. if(cp) {
  142. descr = &cp->browseDescription;
  143. maxrefs = cp->maxReferences;
  144. continuationIndex = cp->continuationIndex;
  145. }
  146. /* is the browsedirection valid? */
  147. if(descr->browseDirection != UA_BROWSEDIRECTION_BOTH &&
  148. descr->browseDirection != UA_BROWSEDIRECTION_FORWARD &&
  149. descr->browseDirection != UA_BROWSEDIRECTION_INVERSE) {
  150. result->statusCode = UA_STATUSCODE_BADBROWSEDIRECTIONINVALID;
  151. return;
  152. }
  153. /* get the references that match the browsedescription */
  154. size_t relevant_refs_size = 0;
  155. UA_NodeId *relevant_refs = UA_NULL;
  156. UA_Boolean all_refs = UA_NodeId_isNull(&descr->referenceTypeId);
  157. if(!all_refs) {
  158. if(descr->includeSubtypes) {
  159. result->statusCode = findsubtypes(ns, &descr->referenceTypeId, &relevant_refs, &relevant_refs_size);
  160. if(result->statusCode != UA_STATUSCODE_GOOD)
  161. return;
  162. } else {
  163. const UA_Node *rootRef = UA_NodeStore_get(ns, &descr->referenceTypeId);
  164. if(!rootRef) {
  165. result->statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  166. return;
  167. } else if(rootRef->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  168. UA_NodeStore_release(rootRef);
  169. result->statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  170. return;
  171. }
  172. UA_NodeStore_release(rootRef);
  173. relevant_refs = (UA_NodeId*)(uintptr_t)&descr->referenceTypeId;
  174. relevant_refs_size = 1;
  175. }
  176. }
  177. /* get the node */
  178. const UA_Node *node = UA_NodeStore_get(ns, &descr->nodeId);
  179. if(!node) {
  180. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  181. if(!all_refs && descr->includeSubtypes)
  182. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  183. return;
  184. }
  185. /* if the node has no references, just return */
  186. if(node->referencesSize <= 0) {
  187. result->referencesSize = 0;
  188. UA_NodeStore_release(node);
  189. if(!all_refs && descr->includeSubtypes)
  190. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  191. return;
  192. }
  193. /* how many references can we return at most? */
  194. UA_UInt32 real_maxrefs = maxrefs;
  195. if(real_maxrefs == 0)
  196. real_maxrefs = node->referencesSize;
  197. result->references = UA_malloc(sizeof(UA_ReferenceDescription) * real_maxrefs);
  198. if(!result->references) {
  199. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  200. goto cleanup;
  201. }
  202. /* loop over the node's references */
  203. size_t skipped = 0;
  204. for(; referencesIndex < node->referencesSize && referencesCount < real_maxrefs; referencesIndex++) {
  205. const UA_Node *current = relevant_node(ns, descr, all_refs, &node->references[referencesIndex],
  206. relevant_refs, relevant_refs_size);
  207. if(!current)
  208. continue;
  209. if(skipped < continuationIndex) {
  210. UA_NodeStore_release(current);
  211. skipped++;
  212. continue;
  213. }
  214. UA_StatusCode retval = fillrefdescr(ns, current, &node->references[referencesIndex], descr->resultMask,
  215. &result->references[referencesCount]);
  216. UA_NodeStore_release(current);
  217. if(retval != UA_STATUSCODE_GOOD) {
  218. UA_Array_delete(result->references, &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION], referencesCount);
  219. result->references = UA_NULL;
  220. result->referencesSize = 0;
  221. result->statusCode = UA_STATUSCODE_UNCERTAINNOTALLNODESAVAILABLE;
  222. goto cleanup;
  223. }
  224. referencesCount++;
  225. }
  226. result->referencesSize = referencesCount;
  227. if(referencesCount == 0) {
  228. UA_free(result->references);
  229. result->references = UA_NULL;
  230. }
  231. cleanup:
  232. UA_NodeStore_release(node);
  233. if(!all_refs && descr->includeSubtypes)
  234. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  235. if(result->statusCode != UA_STATUSCODE_GOOD)
  236. return;
  237. /* create, update, delete continuation points */
  238. if(cp) {
  239. if(referencesIndex == node->referencesSize) {
  240. /* all done, remove a finished continuationPoint */
  241. session->availableContinuationPoints++;
  242. UA_ByteString_deleteMembers(&cp->identifier);
  243. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  244. LIST_REMOVE(cp, pointers);
  245. UA_free(cp);
  246. } else {
  247. /* update the cp and return the cp identifier */
  248. cp->continuationIndex += referencesCount;
  249. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  250. }
  251. } else if(maxrefs != 0 && referencesCount >= maxrefs) {
  252. /* create a cp */
  253. if(session->availableContinuationPoints <= 0 || !(cp = UA_malloc(sizeof(struct ContinuationPointEntry)))) {
  254. result->statusCode = UA_STATUSCODE_BADNOCONTINUATIONPOINTS;
  255. return;
  256. }
  257. UA_BrowseDescription_copy(descr, &cp->browseDescription);
  258. cp->maxReferences = maxrefs;
  259. cp->continuationIndex = referencesCount;
  260. UA_Guid *ident = UA_Guid_new();
  261. UA_UInt32 seed = (uintptr_t)cp;
  262. *ident = UA_Guid_random(&seed);
  263. cp->identifier.data = (UA_Byte*)ident;
  264. cp->identifier.length = sizeof(UA_Guid);
  265. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  266. /* store the cp */
  267. LIST_INSERT_HEAD(&session->continuationPoints, cp, pointers);
  268. session->availableContinuationPoints--;
  269. }
  270. }
  271. void Service_Browse(UA_Server *server, UA_Session *session, const UA_BrowseRequest *request,
  272. UA_BrowseResponse *response) {
  273. if(!UA_NodeId_isNull(&request->view.viewId)) {
  274. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  275. return;
  276. }
  277. if(request->nodesToBrowseSize <= 0) {
  278. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  279. return;
  280. }
  281. size_t size = request->nodesToBrowseSize;
  282. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  283. if(!response->results) {
  284. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  285. return;
  286. }
  287. /* ### Begin External Namespaces */
  288. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  289. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  290. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  291. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  292. size_t indexSize = 0;
  293. for(size_t i = 0; i < size; i++) {
  294. if(request->nodesToBrowse[i].nodeId.namespaceIndex != server->externalNamespaces[j].index)
  295. continue;
  296. isExternal[i] = UA_TRUE;
  297. indices[indexSize] = i;
  298. indexSize++;
  299. }
  300. if(indexSize == 0)
  301. continue;
  302. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  303. ens->browseNodes(ens->ensHandle, &request->requestHeader, request->nodesToBrowse, indices, indexSize,
  304. request->requestedMaxReferencesPerNode, response->results, response->diagnosticInfos);
  305. }
  306. /* ### End External Namespaces */
  307. response->resultsSize = size;
  308. for(size_t i = 0; i < size; i++) {
  309. if(!isExternal[i]) {
  310. browse(session, server->nodestore, UA_NULL, &request->nodesToBrowse[i],
  311. request->requestedMaxReferencesPerNode, &response->results[i]);
  312. }
  313. }
  314. }
  315. void Service_BrowseNext(UA_Server *server, UA_Session *session, const UA_BrowseNextRequest *request,
  316. UA_BrowseNextResponse *response) {
  317. if(request->continuationPointsSize <= 0) {
  318. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  319. return;
  320. }
  321. size_t size = request->continuationPointsSize;
  322. if(!request->releaseContinuationPoints) {
  323. /* continue with the cp */
  324. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  325. if(!response->results) {
  326. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  327. return;
  328. }
  329. response->resultsSize = size;
  330. for(size_t i = 0; i < size; i++) {
  331. struct ContinuationPointEntry *cp;
  332. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  333. if(UA_ByteString_equal(&cp->identifier, &request->continuationPoints[i])) {
  334. browse(session, server->nodestore, cp, UA_NULL, 0, &response->results[i]);
  335. break;
  336. }
  337. }
  338. if(!cp)
  339. response->results[i].statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  340. }
  341. } else {
  342. /* remove the cp */
  343. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  344. if(!response->results) {
  345. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  346. return;
  347. }
  348. response->resultsSize = size;
  349. for(size_t i = 0; i < size; i++) {
  350. struct ContinuationPointEntry *cp = UA_NULL;
  351. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  352. if(UA_ByteString_equal(&cp->identifier, &request->continuationPoints[i])) {
  353. LIST_REMOVE(cp, pointers);
  354. UA_ByteString_deleteMembers(&cp->identifier);
  355. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  356. UA_free(cp);
  357. session->availableContinuationPoints++;
  358. break;
  359. }
  360. }
  361. if(!cp)
  362. response->results[i].statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  363. }
  364. }
  365. }
  366. /***********************/
  367. /* TranslateBrowsePath */
  368. /***********************/
  369. static UA_StatusCode
  370. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_Node *node, const UA_RelativePath *path,
  371. UA_Int32 pathindex, UA_BrowsePathTarget **targets, UA_Int32 *targets_size,
  372. UA_Int32 *target_count)
  373. {
  374. const UA_RelativePathElement *elem = &path->elements[pathindex];
  375. if(elem->targetName.name.length == -1)
  376. return UA_STATUSCODE_BADBROWSENAMEINVALID;
  377. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  378. UA_NodeId *reftypes;
  379. size_t reftypes_count = 1; // all_refs or no subtypes => 1
  380. UA_Boolean all_refs = UA_FALSE;
  381. if(UA_NodeId_isNull(&elem->referenceTypeId))
  382. all_refs = UA_TRUE;
  383. else if(!elem->includeSubtypes)
  384. reftypes = (UA_NodeId*)(uintptr_t)&elem->referenceTypeId; // ptr magic due to const cast
  385. else {
  386. retval = findsubtypes(server->nodestore, &elem->referenceTypeId, &reftypes, &reftypes_count);
  387. if(retval != UA_STATUSCODE_GOOD)
  388. return retval;
  389. }
  390. for(UA_Int32 i = 0; i < node->referencesSize && retval == UA_STATUSCODE_GOOD; i++) {
  391. UA_Boolean match = all_refs;
  392. for(size_t j = 0; j < reftypes_count && !match; j++) {
  393. if(node->references[i].isInverse == elem->isInverse &&
  394. UA_NodeId_equal(&node->references[i].referenceTypeId, &reftypes[j]))
  395. match = UA_TRUE;
  396. }
  397. if(!match)
  398. continue;
  399. // get the node, todo: expandednodeid
  400. const UA_Node *next = UA_NodeStore_get(server->nodestore, &node->references[i].targetId.nodeId);
  401. if(!next)
  402. continue;
  403. // test the browsename
  404. if(elem->targetName.namespaceIndex != next->browseName.namespaceIndex ||
  405. !UA_String_equal(&elem->targetName.name, &next->browseName.name)) {
  406. UA_NodeStore_release(next);
  407. continue;
  408. }
  409. if(pathindex + 1 < path->elementsSize) {
  410. // recursion if the path is longer
  411. retval = walkBrowsePath(server, session, next, path, pathindex + 1,
  412. targets, targets_size, target_count);
  413. UA_NodeStore_release(next);
  414. } else {
  415. // add the browsetarget
  416. if(*target_count >= *targets_size) {
  417. UA_BrowsePathTarget *newtargets;
  418. newtargets = UA_realloc(targets, sizeof(UA_BrowsePathTarget) * (*targets_size) * 2);
  419. if(!newtargets) {
  420. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  421. UA_NodeStore_release(next);
  422. break;
  423. }
  424. *targets = newtargets;
  425. *targets_size *= 2;
  426. }
  427. UA_BrowsePathTarget *res = *targets;
  428. UA_ExpandedNodeId_init(&res[*target_count].targetId);
  429. retval = UA_NodeId_copy(&next->nodeId, &res[*target_count].targetId.nodeId);
  430. UA_NodeStore_release(next);
  431. if(retval != UA_STATUSCODE_GOOD)
  432. break;
  433. res[*target_count].remainingPathIndex = UA_UINT32_MAX;
  434. *target_count += 1;
  435. }
  436. }
  437. if(!all_refs && elem->includeSubtypes)
  438. UA_Array_delete(reftypes, &UA_TYPES[UA_TYPES_NODEID], (UA_Int32)reftypes_count);
  439. return retval;
  440. }
  441. static void translateBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  442. UA_BrowsePathResult *result) {
  443. if(path->relativePath.elementsSize <= 0) {
  444. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  445. return;
  446. }
  447. UA_Int32 arraySize = 10;
  448. result->targets = UA_malloc(sizeof(UA_BrowsePathTarget) * arraySize);
  449. if(!result->targets) {
  450. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  451. return;
  452. }
  453. result->targetsSize = 0;
  454. const UA_Node *firstNode = UA_NodeStore_get(server->nodestore, &path->startingNode);
  455. if(!firstNode) {
  456. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  457. UA_free(result->targets);
  458. result->targets = UA_NULL;
  459. return;
  460. }
  461. result->statusCode = walkBrowsePath(server, session, firstNode, &path->relativePath, 0,
  462. &result->targets, &arraySize, &result->targetsSize);
  463. UA_NodeStore_release(firstNode);
  464. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  465. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  466. if(result->statusCode != UA_STATUSCODE_GOOD) {
  467. UA_Array_delete(result->targets, &UA_TYPES[UA_TYPES_BROWSEPATHTARGET], result->targetsSize);
  468. result->targets = UA_NULL;
  469. result->targetsSize = -1;
  470. }
  471. }
  472. void Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  473. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  474. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  475. if(request->browsePathsSize <= 0) {
  476. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  477. return;
  478. }
  479. size_t size = request->browsePathsSize;
  480. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSEPATHRESULT], size);
  481. if(!response->results) {
  482. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  483. return;
  484. }
  485. /* ### Begin External Namespaces */
  486. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  487. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  488. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  489. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  490. size_t indexSize = 0;
  491. for(size_t i = 0;i < size;i++) {
  492. if(request->browsePaths[i].startingNode.namespaceIndex != server->externalNamespaces[j].index)
  493. continue;
  494. isExternal[i] = UA_TRUE;
  495. indices[indexSize] = i;
  496. indexSize++;
  497. }
  498. if(indexSize == 0)
  499. continue;
  500. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  501. ens->translateBrowsePathsToNodeIds(ens->ensHandle, &request->requestHeader, request->browsePaths,
  502. indices, indexSize, response->results, response->diagnosticInfos);
  503. }
  504. /* ### End External Namespaces */
  505. response->resultsSize = size;
  506. for(size_t i = 0; i < size; i++){
  507. if(!isExternal[i])
  508. translateBrowsePath(server, session, &request->browsePaths[i], &response->results[i]);
  509. }
  510. }
  511. void Service_RegisterNodes(UA_Server *server, UA_Session *session, const UA_RegisterNodesRequest *request,
  512. UA_RegisterNodesResponse *response) {
  513. //TODO: hang the nodeids to the session if really needed
  514. response->responseHeader.timestamp = UA_DateTime_now();
  515. if(request->nodesToRegisterSize <= 0)
  516. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  517. else {
  518. response->responseHeader.serviceResult =
  519. UA_Array_copy(request->nodesToRegister, (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID],
  520. request->nodesToRegisterSize);
  521. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  522. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  523. }
  524. }
  525. void Service_UnregisterNodes(UA_Server *server, UA_Session *session, const UA_UnregisterNodesRequest *request,
  526. UA_UnregisterNodesResponse *response) {
  527. //TODO: remove the nodeids from the session if really needed
  528. response->responseHeader.timestamp = UA_DateTime_now();
  529. if(request->nodesToUnregisterSize==0)
  530. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  531. }