ua_services_view.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #include "ua_server_internal.h"
  5. #include "ua_services.h"
  6. static UA_StatusCode
  7. fillReferenceDescription(UA_Server *server, const UA_Node *curr,
  8. const UA_NodeReferenceKind *ref, UA_UInt32 mask,
  9. UA_ReferenceDescription *descr) {
  10. UA_ReferenceDescription_init(descr);
  11. UA_StatusCode retval = UA_NodeId_copy(&curr->nodeId, &descr->nodeId.nodeId);
  12. if(mask & UA_BROWSERESULTMASK_REFERENCETYPEID)
  13. retval |= UA_NodeId_copy(&ref->referenceTypeId, &descr->referenceTypeId);
  14. if(mask & UA_BROWSERESULTMASK_ISFORWARD)
  15. descr->isForward = !ref->isInverse;
  16. if(mask & UA_BROWSERESULTMASK_NODECLASS)
  17. retval |= UA_NodeClass_copy(&curr->nodeClass, &descr->nodeClass);
  18. if(mask & UA_BROWSERESULTMASK_BROWSENAME)
  19. retval |= UA_QualifiedName_copy(&curr->browseName, &descr->browseName);
  20. if(mask & UA_BROWSERESULTMASK_DISPLAYNAME)
  21. retval |= UA_LocalizedText_copy(&curr->displayName, &descr->displayName);
  22. if(mask & UA_BROWSERESULTMASK_TYPEDEFINITION) {
  23. if(curr->nodeClass == UA_NODECLASS_OBJECT ||
  24. curr->nodeClass == UA_NODECLASS_VARIABLE) {
  25. UA_NodeId type;
  26. getNodeType(server, curr , &type);
  27. retval |= UA_NodeId_copy(&type, &descr->typeDefinition.nodeId);
  28. }
  29. }
  30. return retval;
  31. }
  32. static void
  33. removeCp(ContinuationPointEntry *cp, UA_Session* session) {
  34. LIST_REMOVE(cp, pointers);
  35. UA_ByteString_deleteMembers(&cp->identifier);
  36. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  37. UA_free(cp);
  38. ++session->availableContinuationPoints;
  39. }
  40. static UA_Boolean
  41. relevantReference(UA_Server *server, UA_Boolean includeSubtypes,
  42. const UA_NodeId *rootRef, const UA_NodeId *testRef) {
  43. if(!includeSubtypes)
  44. return UA_NodeId_equal(rootRef, testRef);
  45. const UA_NodeId hasSubType = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
  46. return isNodeInTree(server->nodestore, testRef, rootRef, &hasSubType, 1);
  47. }
  48. /* Returns whether the node / continuationpoint is done */
  49. static UA_Boolean
  50. browseReferences(UA_Server *server, const UA_BrowseDescription *descr,
  51. UA_BrowseResult *result, ContinuationPointEntry *cp) {
  52. UA_assert(cp != NULL);
  53. /* Get the node */
  54. const UA_Node *node = UA_NodeStore_get(server->nodestore, &descr->nodeId);
  55. if(!node) {
  56. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  57. return true;;
  58. }
  59. /* If the node has no references, just return */
  60. if(node->referencesSize == 0) {
  61. result->referencesSize = 0;
  62. return true;;
  63. }
  64. /* Follow all references? */
  65. UA_Boolean browseAll = UA_NodeId_isNull(&descr->referenceTypeId);
  66. /* How many references can we return at most? */
  67. size_t maxrefs = cp->maxReferences;
  68. if(maxrefs == 0)
  69. maxrefs = UA_INT32_MAX;
  70. /* Allocate the results array */
  71. size_t refs_size = 2; /* True size of the array */
  72. result->references =
  73. (UA_ReferenceDescription*)UA_Array_new(refs_size,
  74. &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION]);
  75. if(!result->references) {
  76. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  77. return false;
  78. }
  79. size_t referenceKindIndex = cp->referenceKindIndex;
  80. size_t targetIndex = cp->targetIndex;
  81. /* Loop over the node's references */
  82. for(; referenceKindIndex < node->referencesSize; ++referenceKindIndex) {
  83. UA_NodeReferenceKind *rk = &node->references[referenceKindIndex];
  84. /* Reference in the right direction? */
  85. if(rk->isInverse && descr->browseDirection == UA_BROWSEDIRECTION_FORWARD)
  86. continue;
  87. if(!rk->isInverse && descr->browseDirection == UA_BROWSEDIRECTION_INVERSE)
  88. continue;
  89. /* Is the reference part of the hierarchy of references we look for? */
  90. if(!browseAll && !relevantReference(server, descr->includeSubtypes,
  91. &descr->referenceTypeId, &rk->referenceTypeId))
  92. continue;
  93. /* Loop over the targets */
  94. for(; targetIndex < rk->targetIdsSize; ++targetIndex) {
  95. /* Get the node */
  96. const UA_Node *target = UA_NodeStore_get(server->nodestore,
  97. &rk->targetIds[targetIndex].nodeId);
  98. /* Test if the node class matches */
  99. if(!target || (descr->nodeClassMask != 0 &&
  100. (target->nodeClass & descr->nodeClassMask) == 0))
  101. continue;
  102. /* A match! Can we return it? */
  103. if(result->referencesSize >= maxrefs) {
  104. /* There are references we could not return */
  105. cp->referenceKindIndex = referenceKindIndex;
  106. cp->targetIndex = targetIndex;
  107. return false;
  108. }
  109. /* Make enough space in the array */
  110. if(result->referencesSize >= refs_size) {
  111. refs_size *= 2;
  112. UA_ReferenceDescription *rd =
  113. (UA_ReferenceDescription*)UA_realloc(result->references,
  114. sizeof(UA_ReferenceDescription) * refs_size);
  115. if(!rd) {
  116. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  117. goto error_recovery;
  118. }
  119. result->references = rd;
  120. }
  121. /* Copy the node description */
  122. result->statusCode =
  123. fillReferenceDescription(server, target, rk, descr->resultMask,
  124. &result->references[result->referencesSize]);
  125. if(result->statusCode != UA_STATUSCODE_GOOD)
  126. goto error_recovery;
  127. /* Increase the counter */
  128. result->referencesSize++;
  129. }
  130. targetIndex = 0; /* Start at index 0 for the next reference kind */
  131. }
  132. /* No relevant references, return array of length zero */
  133. if(result->referencesSize == 0) {
  134. UA_free(result->references);
  135. result->references = (UA_ReferenceDescription*)UA_EMPTY_ARRAY_SENTINEL;
  136. }
  137. /* The node is done */
  138. return true;
  139. error_recovery:
  140. if(result->referencesSize == 0)
  141. UA_free(result->references);
  142. else
  143. UA_Array_delete(result->references, result->referencesSize,
  144. &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION]);
  145. result->references = NULL;
  146. result->referencesSize = 0;
  147. return false;
  148. }
  149. /* Results for a single browsedescription. This is the inner loop for both
  150. * Browse and BrowseNext
  151. *
  152. * @param session Session to save continuationpoints
  153. * @param ns The nodstore where the to-be-browsed node can be found
  154. * @param cp If cp is not null, we continue from here If cp is null, we can add
  155. * a new continuation point if possible and necessary.
  156. * @param descr If no cp is set, we take the browsedescription from there
  157. * @param maxrefs The maximum number of references the client has requested. If 0,
  158. * all matching references are returned at once.
  159. * @param result The entry in the request */
  160. void
  161. Service_Browse_single(UA_Server *server, UA_Session *session,
  162. ContinuationPointEntry *cp,
  163. const UA_BrowseDescription *descr,
  164. UA_UInt32 maxrefs, UA_BrowseResult *result) {
  165. ContinuationPointEntry *internal_cp = cp;
  166. if(!internal_cp) {
  167. /* If there is no continuation point, stack-allocate one. It gets copied
  168. * on the heap when this is required at a later point. */
  169. internal_cp = (ContinuationPointEntry*)UA_alloca(sizeof(ContinuationPointEntry));
  170. memset(internal_cp, 0, sizeof(ContinuationPointEntry));
  171. internal_cp->maxReferences = maxrefs;
  172. } else {
  173. /* Set the browsedescription if a cp is given */
  174. descr = &cp->browseDescription;
  175. }
  176. /* Is the browsedirection valid? */
  177. if(descr->browseDirection != UA_BROWSEDIRECTION_BOTH &&
  178. descr->browseDirection != UA_BROWSEDIRECTION_FORWARD &&
  179. descr->browseDirection != UA_BROWSEDIRECTION_INVERSE) {
  180. result->statusCode = UA_STATUSCODE_BADBROWSEDIRECTIONINVALID;
  181. return;
  182. }
  183. /* Is the reference type valid? */
  184. if(!UA_NodeId_isNull(&descr->referenceTypeId)) {
  185. const UA_Node *reftype = UA_NodeStore_get(server->nodestore, &descr->referenceTypeId);
  186. if(!reftype || reftype->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  187. result->statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  188. return;
  189. }
  190. }
  191. /* Browse the references */
  192. UA_Boolean done = browseReferences(server, descr, result, internal_cp);
  193. /* Exit early if an error occured */
  194. if(result->statusCode != UA_STATUSCODE_GOOD)
  195. return;
  196. /* A continuation point exists already */
  197. if(cp) {
  198. if(done) {
  199. /* All done, remove a finished continuationPoint */
  200. removeCp(cp, session);
  201. } else {
  202. /* Return the cp identifier */
  203. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  204. }
  205. return;
  206. }
  207. /* Create a new continuation point */
  208. if(!done) {
  209. if(session->availableContinuationPoints <= 0 ||
  210. !(cp = (ContinuationPointEntry *)UA_malloc(sizeof(ContinuationPointEntry)))) {
  211. result->statusCode = UA_STATUSCODE_BADNOCONTINUATIONPOINTS;
  212. return;
  213. }
  214. UA_BrowseDescription_copy(descr, &cp->browseDescription);
  215. cp->referenceKindIndex = internal_cp->referenceKindIndex;
  216. cp->targetIndex = internal_cp->targetIndex;
  217. cp->maxReferences = internal_cp->maxReferences;
  218. /* Create a random bytestring via a Guid */
  219. UA_Guid *ident = UA_Guid_new();
  220. *ident = UA_Guid_random();
  221. cp->identifier.data = (UA_Byte*)ident;
  222. cp->identifier.length = sizeof(UA_Guid);
  223. /* Return the cp identifier */
  224. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  225. /* Attach the cp to the session */
  226. LIST_INSERT_HEAD(&session->continuationPoints, cp, pointers);
  227. --session->availableContinuationPoints;
  228. }
  229. }
  230. void Service_Browse(UA_Server *server, UA_Session *session,
  231. const UA_BrowseRequest *request,
  232. UA_BrowseResponse *response) {
  233. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  234. "Processing BrowseRequest");
  235. if(!UA_NodeId_isNull(&request->view.viewId)) {
  236. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  237. return;
  238. }
  239. if(request->nodesToBrowseSize <= 0) {
  240. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  241. return;
  242. }
  243. size_t size = request->nodesToBrowseSize;
  244. response->results =
  245. (UA_BrowseResult*)UA_Array_new(size, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  246. if(!response->results) {
  247. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  248. return;
  249. }
  250. response->resultsSize = size;
  251. for(size_t i = 0; i < size; ++i)
  252. Service_Browse_single(server, session, NULL, &request->nodesToBrowse[i],
  253. request->requestedMaxReferencesPerNode,
  254. &response->results[i]);
  255. }
  256. UA_BrowseResult
  257. UA_Server_browse(UA_Server *server, UA_UInt32 maxrefs,
  258. const UA_BrowseDescription *descr) {
  259. UA_BrowseResult result;
  260. UA_BrowseResult_init(&result);
  261. UA_RCU_LOCK();
  262. Service_Browse_single(server, &adminSession, NULL,
  263. descr, maxrefs, &result);
  264. UA_RCU_UNLOCK();
  265. return result;
  266. }
  267. static void
  268. browseNext(UA_Server *server, UA_Session *session,
  269. UA_Boolean releaseContinuationPoint,
  270. const UA_ByteString *continuationPoint,
  271. UA_BrowseResult *result) {
  272. /* Find the continuation point */
  273. ContinuationPointEntry *cp;
  274. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  275. if(UA_ByteString_equal(&cp->identifier, continuationPoint))
  276. break;
  277. }
  278. if(!cp) {
  279. result->statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  280. return;
  281. }
  282. /* Do the work */
  283. if(!releaseContinuationPoint)
  284. Service_Browse_single(server, session, cp, NULL, 0, result);
  285. else
  286. removeCp(cp, session);
  287. }
  288. void
  289. Service_BrowseNext(UA_Server *server, UA_Session *session,
  290. const UA_BrowseNextRequest *request,
  291. UA_BrowseNextResponse *response) {
  292. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  293. "Processing BrowseNextRequest");
  294. if(request->continuationPointsSize == 0) {
  295. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  296. return;
  297. }
  298. /* Allocate the result array */
  299. size_t size = request->continuationPointsSize;
  300. response->results =
  301. (UA_BrowseResult*)UA_Array_new(size, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  302. if(!response->results) {
  303. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  304. return;
  305. }
  306. response->resultsSize = size;
  307. for(size_t i = 0; i < size; ++i)
  308. browseNext(server, session, request->releaseContinuationPoints,
  309. &request->continuationPoints[i], &response->results[i]);
  310. }
  311. UA_BrowseResult
  312. UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint,
  313. const UA_ByteString *continuationPoint) {
  314. UA_BrowseResult result;
  315. UA_BrowseResult_init(&result);
  316. UA_RCU_LOCK();
  317. browseNext(server, &adminSession, releaseContinuationPoint,
  318. continuationPoint, &result);
  319. UA_RCU_UNLOCK();
  320. return result;
  321. }
  322. /***********************/
  323. /* TranslateBrowsePath */
  324. /***********************/
  325. static void
  326. walkBrowsePathElementReferenceTargets(UA_BrowsePathResult *result, size_t *targetsSize,
  327. UA_NodeId **next, size_t *nextSize, size_t *nextCount,
  328. UA_UInt32 elemDepth, const UA_NodeReferenceKind *rk) {
  329. /* Loop over the targets */
  330. for(size_t i = 0; i < rk->targetIdsSize; i++) {
  331. UA_ExpandedNodeId *targetId = &rk->targetIds[i];
  332. /* Does the reference point to an external server? Then add to the
  333. * targets with the right path depth. */
  334. if(targetId->serverIndex != 0) {
  335. UA_BrowsePathTarget *tempTargets =
  336. (UA_BrowsePathTarget*)UA_realloc(result->targets,
  337. sizeof(UA_BrowsePathTarget) * (*targetsSize) * 2);
  338. if(!tempTargets) {
  339. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  340. return;
  341. }
  342. result->targets = tempTargets;
  343. (*targetsSize) *= 2;
  344. result->statusCode = UA_ExpandedNodeId_copy(targetId,
  345. &result->targets[result->targetsSize].targetId);
  346. result->targets[result->targetsSize].remainingPathIndex = elemDepth;
  347. continue;
  348. }
  349. /* Can we store the node in the array of candidates for deep-search? */
  350. if(*nextSize <= *nextCount) {
  351. UA_NodeId *tempNext =
  352. (UA_NodeId*)UA_realloc(*next, sizeof(UA_NodeId) * (*nextSize) * 2);
  353. if(!tempNext) {
  354. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  355. return;
  356. }
  357. *next = tempNext;
  358. (*nextSize) *= 2;
  359. }
  360. /* Add the node to the next array for the following path element */
  361. result->statusCode = UA_NodeId_copy(&targetId->nodeId,
  362. &(*next)[*nextCount]);
  363. if(result->statusCode != UA_STATUSCODE_GOOD)
  364. return;
  365. ++(*nextCount);
  366. }
  367. }
  368. static void
  369. walkBrowsePathElement(UA_Server *server, UA_Session *session,
  370. UA_BrowsePathResult *result, size_t *targetsSize,
  371. const UA_RelativePathElement *elem, UA_UInt32 elemDepth,
  372. const UA_QualifiedName *targetName,
  373. const UA_NodeId *current, const size_t currentCount,
  374. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  375. /* Return all references? */
  376. UA_Boolean all_refs = UA_NodeId_isNull(&elem->referenceTypeId);
  377. if(!all_refs) {
  378. const UA_Node *rootRef = UA_NodeStore_get(server->nodestore, &elem->referenceTypeId);
  379. if(!rootRef || rootRef->nodeClass != UA_NODECLASS_REFERENCETYPE)
  380. return;
  381. }
  382. /* Iterate over all nodes at the current depth-level */
  383. for(size_t i = 0; i < currentCount; ++i) {
  384. /* Get the node */
  385. const UA_Node *node = UA_NodeStore_get(server->nodestore, &current[i]);
  386. if(!node) {
  387. /* If we cannot find the node at depth 0, the starting node does not exist */
  388. if(elemDepth == 0)
  389. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  390. continue;
  391. }
  392. /* Test whether the current node has the target name required in the
  393. * previous path element */
  394. if(targetName && (targetName->namespaceIndex != node->browseName.namespaceIndex ||
  395. !UA_String_equal(&targetName->name, &node->browseName.name)))
  396. continue;
  397. /* Loop over the nodes references */
  398. for(size_t r = 0; r < node->referencesSize &&
  399. result->statusCode == UA_STATUSCODE_GOOD; ++r) {
  400. UA_NodeReferenceKind *rk = &node->references[r];
  401. /* Does the direction of the reference match? */
  402. if(rk->isInverse != elem->isInverse)
  403. continue;
  404. /* Is the node relevant? */
  405. if(!all_refs && !relevantReference(server, elem->includeSubtypes,
  406. &elem->referenceTypeId, &rk->referenceTypeId))
  407. continue;
  408. /* Walk over the reference targets */
  409. walkBrowsePathElementReferenceTargets(result, targetsSize, next, nextSize,
  410. nextCount, elemDepth, rk);
  411. }
  412. }
  413. }
  414. /* This assumes that result->targets has enough room for all currentCount elements */
  415. static void
  416. addBrowsePathTargets(UA_Server *server, UA_Session *session,
  417. UA_BrowsePathResult *result, const UA_QualifiedName *targetName,
  418. UA_NodeId *current, size_t currentCount) {
  419. for(size_t i = 0; i < currentCount; i++) {
  420. /* Get the node */
  421. const UA_Node *node = UA_NodeStore_get(server->nodestore, &current[i]);
  422. if(!node) {
  423. UA_NodeId_deleteMembers(&current[i]);
  424. continue;
  425. }
  426. /* Test whether the current node has the target name required in the
  427. * previous path element */
  428. if(targetName->namespaceIndex != node->browseName.namespaceIndex ||
  429. !UA_String_equal(&targetName->name, &node->browseName.name)) {
  430. UA_NodeId_deleteMembers(&current[i]);
  431. continue;
  432. }
  433. /* Move the nodeid to the target array */
  434. UA_BrowsePathTarget_init(&result->targets[result->targetsSize]);
  435. result->targets[result->targetsSize].targetId.nodeId = current[i];
  436. result->targets[result->targetsSize].remainingPathIndex = UA_UINT32_MAX;
  437. ++result->targetsSize;
  438. }
  439. }
  440. static void
  441. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  442. UA_BrowsePathResult *result, size_t targetsSize,
  443. UA_NodeId **current, size_t *currentSize, size_t *currentCount,
  444. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  445. UA_assert(*currentCount == 1);
  446. UA_assert(*nextCount == 0);
  447. /* Points to the targetName of the _previous_ path element */
  448. const UA_QualifiedName *targetName = NULL;
  449. /* Iterate over path elements */
  450. UA_assert(path->relativePath.elementsSize > 0);
  451. for(UA_UInt32 i = 0; i < path->relativePath.elementsSize; ++i) {
  452. walkBrowsePathElement(server, session, result, &targetsSize,
  453. &path->relativePath.elements[i], i, targetName,
  454. *current, *currentCount, next, nextSize, nextCount);
  455. /* Clean members of current */
  456. for(size_t j = 0; j < *currentCount; j++)
  457. UA_NodeId_deleteMembers(&(*current)[j]);
  458. *currentCount = 0;
  459. /* When no targets are left or an error occurred. None of next's
  460. * elements will be copied to result->targets */
  461. if(*nextCount == 0 || result->statusCode != UA_STATUSCODE_GOOD) {
  462. UA_assert(*currentCount == 0);
  463. UA_assert(*nextCount == 0);
  464. return;
  465. }
  466. /* Exchange current and next for the next depth */
  467. size_t tSize = *currentSize; size_t tCount = *currentCount; UA_NodeId *tT = *current;
  468. *currentSize = *nextSize; *currentCount = *nextCount; *current = *next;
  469. *nextSize = tSize; *nextCount = tCount; *next = tT;
  470. /* Store the target name of the previous path element */
  471. targetName = &path->relativePath.elements[i].targetName;
  472. }
  473. UA_assert(targetName != NULL);
  474. UA_assert(*nextCount == 0);
  475. /* After the last BrowsePathElement, move members from current to the
  476. * result targets */
  477. /* Realloc if more space is needed */
  478. if(targetsSize < result->targetsSize + (*currentCount)) {
  479. UA_BrowsePathTarget *newTargets =
  480. (UA_BrowsePathTarget*)UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) *
  481. (result->targetsSize + (*currentCount)));
  482. if(!newTargets) {
  483. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  484. for(size_t i = 0; i < *currentCount; ++i)
  485. UA_NodeId_deleteMembers(&(*current)[i]);
  486. *currentCount = 0;
  487. return;
  488. }
  489. result->targets = newTargets;
  490. }
  491. /* Move the elements of current to the targets */
  492. addBrowsePathTargets(server, session, result, targetName, *current, *currentCount);
  493. *currentCount = 0;
  494. }
  495. static void
  496. translateBrowsePathToNodeIds(UA_Server *server, UA_Session *session,
  497. const UA_BrowsePath *path, UA_BrowsePathResult *result) {
  498. if(path->relativePath.elementsSize <= 0) {
  499. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  500. return;
  501. }
  502. /* RelativePath elements must not have an empty targetName */
  503. for(size_t i = 0; i < path->relativePath.elementsSize; ++i) {
  504. if(UA_QualifiedName_isNull(&path->relativePath.elements[i].targetName)) {
  505. result->statusCode = UA_STATUSCODE_BADBROWSENAMEINVALID;
  506. return;
  507. }
  508. }
  509. /* Allocate memory for the targets */
  510. size_t targetsSize = 10; /* When to realloc; the member count is stored in
  511. * result->targetsSize */
  512. result->targets =
  513. (UA_BrowsePathTarget*)UA_malloc(sizeof(UA_BrowsePathTarget) * targetsSize);
  514. if(!result->targets) {
  515. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  516. return;
  517. }
  518. /* Allocate memory for two temporary arrays. One with the results for the
  519. * previous depth of the path. The other for the new results at the current
  520. * depth. The two arrays alternate as we descend down the tree. */
  521. size_t currentSize = 10; /* When to realloc */
  522. size_t currentCount = 0; /* Current elements */
  523. UA_NodeId *current = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * currentSize);
  524. if(!current) {
  525. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  526. UA_free(result->targets);
  527. return;
  528. }
  529. size_t nextSize = 10; /* When to realloc */
  530. size_t nextCount = 0; /* Current elements */
  531. UA_NodeId *next = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * nextSize);
  532. if(!next) {
  533. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  534. UA_free(result->targets);
  535. UA_free(current);
  536. return;
  537. }
  538. /* Copy the starting node into current */
  539. result->statusCode = UA_NodeId_copy(&path->startingNode, &current[0]);
  540. if(result->statusCode != UA_STATUSCODE_GOOD) {
  541. UA_free(result->targets);
  542. UA_free(current);
  543. UA_free(next);
  544. return;
  545. }
  546. currentCount = 1;
  547. /* Walk the path elements */
  548. walkBrowsePath(server, session, path, result, targetsSize,
  549. &current, &currentSize, &currentCount,
  550. &next, &nextSize, &nextCount);
  551. UA_assert(currentCount == 0);
  552. UA_assert(nextCount == 0);
  553. /* No results => BadNoMatch status code */
  554. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  555. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  556. /* Clean up the temporary arrays and the targets */
  557. UA_free(current);
  558. UA_free(next);
  559. if(result->statusCode != UA_STATUSCODE_GOOD) {
  560. for(size_t i = 0; i < result->targetsSize; ++i)
  561. UA_BrowsePathTarget_deleteMembers(&result->targets[i]);
  562. UA_free(result->targets);
  563. result->targets = NULL;
  564. result->targetsSize = 0;
  565. }
  566. }
  567. UA_BrowsePathResult
  568. UA_Server_translateBrowsePathToNodeIds(UA_Server *server,
  569. const UA_BrowsePath *browsePath) {
  570. UA_BrowsePathResult result;
  571. UA_BrowsePathResult_init(&result);
  572. UA_RCU_LOCK();
  573. translateBrowsePathToNodeIds(server, &adminSession, browsePath, &result);
  574. UA_RCU_UNLOCK();
  575. return result;
  576. }
  577. void
  578. Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  579. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  580. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  581. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  582. "Processing TranslateBrowsePathsToNodeIdsRequest");
  583. if(request->browsePathsSize <= 0) {
  584. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  585. return;
  586. }
  587. size_t size = request->browsePathsSize;
  588. response->results =
  589. (UA_BrowsePathResult*)UA_Array_new(size, &UA_TYPES[UA_TYPES_BROWSEPATHRESULT]);
  590. if(!response->results) {
  591. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  592. return;
  593. }
  594. response->resultsSize = size;
  595. for(size_t i = 0; i < size; ++i)
  596. translateBrowsePathToNodeIds(server, session, &request->browsePaths[i],
  597. &response->results[i]);
  598. }
  599. void Service_RegisterNodes(UA_Server *server, UA_Session *session,
  600. const UA_RegisterNodesRequest *request,
  601. UA_RegisterNodesResponse *response) {
  602. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  603. "Processing RegisterNodesRequest");
  604. //TODO: hang the nodeids to the session if really needed
  605. if(request->nodesToRegisterSize == 0) {
  606. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  607. return;
  608. }
  609. response->responseHeader.serviceResult =
  610. UA_Array_copy(request->nodesToRegister, request->nodesToRegisterSize,
  611. (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID]);
  612. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  613. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  614. }
  615. void Service_UnregisterNodes(UA_Server *server, UA_Session *session,
  616. const UA_UnregisterNodesRequest *request,
  617. UA_UnregisterNodesResponse *response) {
  618. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  619. "Processing UnRegisterNodesRequest");
  620. //TODO: remove the nodeids from the session if really needed
  621. if(request->nodesToUnregisterSize == 0)
  622. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  623. }