ua_services_view.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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", NULL);
  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. /* Thread-local variables to pass additional arguments into the operation */
  268. static UA_THREAD_LOCAL UA_Boolean op_releaseContinuationPoint;
  269. static void
  270. Operation_BrowseNext(UA_Server *server, UA_Session *session,
  271. const UA_ByteString *continuationPoint, 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(!op_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", NULL);
  294. op_releaseContinuationPoint = request->releaseContinuationPoints;
  295. response->responseHeader.serviceResult =
  296. UA_Server_processServiceOperations(server, session,
  297. (UA_ServiceOperation)Operation_BrowseNext,
  298. &request->continuationPointsSize, &UA_TYPES[UA_TYPES_BYTESTRING],
  299. &response->resultsSize, &UA_TYPES[UA_TYPES_BROWSERESULT]);
  300. }
  301. UA_BrowseResult
  302. UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint,
  303. const UA_ByteString *continuationPoint) {
  304. UA_BrowseResult result;
  305. UA_BrowseResult_init(&result);
  306. op_releaseContinuationPoint = releaseContinuationPoint;
  307. UA_RCU_LOCK();
  308. Operation_BrowseNext(server, &adminSession,
  309. continuationPoint, &result);
  310. UA_RCU_UNLOCK();
  311. return result;
  312. }
  313. /***********************/
  314. /* TranslateBrowsePath */
  315. /***********************/
  316. static void
  317. walkBrowsePathElementReferenceTargets(UA_BrowsePathResult *result, size_t *targetsSize,
  318. UA_NodeId **next, size_t *nextSize, size_t *nextCount,
  319. UA_UInt32 elemDepth, const UA_NodeReferenceKind *rk) {
  320. /* Loop over the targets */
  321. for(size_t i = 0; i < rk->targetIdsSize; i++) {
  322. UA_ExpandedNodeId *targetId = &rk->targetIds[i];
  323. /* Does the reference point to an external server? Then add to the
  324. * targets with the right path depth. */
  325. if(targetId->serverIndex != 0) {
  326. UA_BrowsePathTarget *tempTargets =
  327. (UA_BrowsePathTarget*)UA_realloc(result->targets,
  328. sizeof(UA_BrowsePathTarget) * (*targetsSize) * 2);
  329. if(!tempTargets) {
  330. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  331. return;
  332. }
  333. result->targets = tempTargets;
  334. (*targetsSize) *= 2;
  335. result->statusCode = UA_ExpandedNodeId_copy(targetId,
  336. &result->targets[result->targetsSize].targetId);
  337. result->targets[result->targetsSize].remainingPathIndex = elemDepth;
  338. continue;
  339. }
  340. /* Can we store the node in the array of candidates for deep-search? */
  341. if(*nextSize <= *nextCount) {
  342. UA_NodeId *tempNext =
  343. (UA_NodeId*)UA_realloc(*next, sizeof(UA_NodeId) * (*nextSize) * 2);
  344. if(!tempNext) {
  345. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  346. return;
  347. }
  348. *next = tempNext;
  349. (*nextSize) *= 2;
  350. }
  351. /* Add the node to the next array for the following path element */
  352. result->statusCode = UA_NodeId_copy(&targetId->nodeId,
  353. &(*next)[*nextCount]);
  354. if(result->statusCode != UA_STATUSCODE_GOOD)
  355. return;
  356. ++(*nextCount);
  357. }
  358. }
  359. static void
  360. walkBrowsePathElement(UA_Server *server, UA_Session *session,
  361. UA_BrowsePathResult *result, size_t *targetsSize,
  362. const UA_RelativePathElement *elem, UA_UInt32 elemDepth,
  363. const UA_QualifiedName *targetName,
  364. const UA_NodeId *current, const size_t currentCount,
  365. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  366. /* Return all references? */
  367. UA_Boolean all_refs = UA_NodeId_isNull(&elem->referenceTypeId);
  368. if(!all_refs) {
  369. const UA_Node *rootRef = UA_NodeStore_get(server->nodestore, &elem->referenceTypeId);
  370. if(!rootRef || rootRef->nodeClass != UA_NODECLASS_REFERENCETYPE)
  371. return;
  372. }
  373. /* Iterate over all nodes at the current depth-level */
  374. for(size_t i = 0; i < currentCount; ++i) {
  375. /* Get the node */
  376. const UA_Node *node = UA_NodeStore_get(server->nodestore, &current[i]);
  377. if(!node) {
  378. /* If we cannot find the node at depth 0, the starting node does not exist */
  379. if(elemDepth == 0)
  380. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  381. continue;
  382. }
  383. /* Test whether the current node has the target name required in the
  384. * previous path element */
  385. if(targetName && (targetName->namespaceIndex != node->browseName.namespaceIndex ||
  386. !UA_String_equal(&targetName->name, &node->browseName.name)))
  387. continue;
  388. /* Loop over the nodes references */
  389. for(size_t r = 0; r < node->referencesSize &&
  390. result->statusCode == UA_STATUSCODE_GOOD; ++r) {
  391. UA_NodeReferenceKind *rk = &node->references[r];
  392. /* Does the direction of the reference match? */
  393. if(rk->isInverse != elem->isInverse)
  394. continue;
  395. /* Is the node relevant? */
  396. if(!all_refs && !relevantReference(server, elem->includeSubtypes,
  397. &elem->referenceTypeId, &rk->referenceTypeId))
  398. continue;
  399. /* Walk over the reference targets */
  400. walkBrowsePathElementReferenceTargets(result, targetsSize, next, nextSize,
  401. nextCount, elemDepth, rk);
  402. }
  403. }
  404. }
  405. /* This assumes that result->targets has enough room for all currentCount elements */
  406. static void
  407. addBrowsePathTargets(UA_Server *server, UA_Session *session,
  408. UA_BrowsePathResult *result, const UA_QualifiedName *targetName,
  409. UA_NodeId *current, size_t currentCount) {
  410. for(size_t i = 0; i < currentCount; i++) {
  411. /* Get the node */
  412. const UA_Node *node = UA_NodeStore_get(server->nodestore, &current[i]);
  413. if(!node) {
  414. UA_NodeId_deleteMembers(&current[i]);
  415. continue;
  416. }
  417. /* Test whether the current node has the target name required in the
  418. * previous path element */
  419. if(targetName->namespaceIndex != node->browseName.namespaceIndex ||
  420. !UA_String_equal(&targetName->name, &node->browseName.name)) {
  421. UA_NodeId_deleteMembers(&current[i]);
  422. continue;
  423. }
  424. /* Move the nodeid to the target array */
  425. UA_BrowsePathTarget_init(&result->targets[result->targetsSize]);
  426. result->targets[result->targetsSize].targetId.nodeId = current[i];
  427. result->targets[result->targetsSize].remainingPathIndex = UA_UINT32_MAX;
  428. ++result->targetsSize;
  429. }
  430. }
  431. static void
  432. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  433. UA_BrowsePathResult *result, size_t targetsSize,
  434. UA_NodeId **current, size_t *currentSize, size_t *currentCount,
  435. UA_NodeId **next, size_t *nextSize, size_t *nextCount) {
  436. UA_assert(*currentCount == 1);
  437. UA_assert(*nextCount == 0);
  438. /* Points to the targetName of the _previous_ path element */
  439. const UA_QualifiedName *targetName = NULL;
  440. /* Iterate over path elements */
  441. UA_assert(path->relativePath.elementsSize > 0);
  442. for(UA_UInt32 i = 0; i < path->relativePath.elementsSize; ++i) {
  443. walkBrowsePathElement(server, session, result, &targetsSize,
  444. &path->relativePath.elements[i], i, targetName,
  445. *current, *currentCount, next, nextSize, nextCount);
  446. /* Clean members of current */
  447. for(size_t j = 0; j < *currentCount; j++)
  448. UA_NodeId_deleteMembers(&(*current)[j]);
  449. *currentCount = 0;
  450. /* When no targets are left or an error occurred. None of next's
  451. * elements will be copied to result->targets */
  452. if(*nextCount == 0 || result->statusCode != UA_STATUSCODE_GOOD) {
  453. UA_assert(*currentCount == 0);
  454. UA_assert(*nextCount == 0);
  455. return;
  456. }
  457. /* Exchange current and next for the next depth */
  458. size_t tSize = *currentSize; size_t tCount = *currentCount; UA_NodeId *tT = *current;
  459. *currentSize = *nextSize; *currentCount = *nextCount; *current = *next;
  460. *nextSize = tSize; *nextCount = tCount; *next = tT;
  461. /* Store the target name of the previous path element */
  462. targetName = &path->relativePath.elements[i].targetName;
  463. }
  464. UA_assert(targetName != NULL);
  465. UA_assert(*nextCount == 0);
  466. /* After the last BrowsePathElement, move members from current to the
  467. * result targets */
  468. /* Realloc if more space is needed */
  469. if(targetsSize < result->targetsSize + (*currentCount)) {
  470. UA_BrowsePathTarget *newTargets =
  471. (UA_BrowsePathTarget*)UA_realloc(result->targets, sizeof(UA_BrowsePathTarget) *
  472. (result->targetsSize + (*currentCount)));
  473. if(!newTargets) {
  474. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  475. for(size_t i = 0; i < *currentCount; ++i)
  476. UA_NodeId_deleteMembers(&(*current)[i]);
  477. *currentCount = 0;
  478. return;
  479. }
  480. result->targets = newTargets;
  481. }
  482. /* Move the elements of current to the targets */
  483. addBrowsePathTargets(server, session, result, targetName, *current, *currentCount);
  484. *currentCount = 0;
  485. }
  486. static void
  487. Operation_TranslateBrowsePathToNodeIds(UA_Server *server, UA_Session *session,
  488. const UA_BrowsePath *path,
  489. UA_BrowsePathResult *result) {
  490. if(path->relativePath.elementsSize <= 0) {
  491. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  492. return;
  493. }
  494. /* RelativePath elements must not have an empty targetName */
  495. for(size_t i = 0; i < path->relativePath.elementsSize; ++i) {
  496. if(UA_QualifiedName_isNull(&path->relativePath.elements[i].targetName)) {
  497. result->statusCode = UA_STATUSCODE_BADBROWSENAMEINVALID;
  498. return;
  499. }
  500. }
  501. /* Allocate memory for the targets */
  502. size_t targetsSize = 10; /* When to realloc; the member count is stored in
  503. * result->targetsSize */
  504. result->targets =
  505. (UA_BrowsePathTarget*)UA_malloc(sizeof(UA_BrowsePathTarget) * targetsSize);
  506. if(!result->targets) {
  507. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  508. return;
  509. }
  510. /* Allocate memory for two temporary arrays. One with the results for the
  511. * previous depth of the path. The other for the new results at the current
  512. * depth. The two arrays alternate as we descend down the tree. */
  513. size_t currentSize = 10; /* When to realloc */
  514. size_t currentCount = 0; /* Current elements */
  515. UA_NodeId *current = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * currentSize);
  516. if(!current) {
  517. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  518. UA_free(result->targets);
  519. return;
  520. }
  521. size_t nextSize = 10; /* When to realloc */
  522. size_t nextCount = 0; /* Current elements */
  523. UA_NodeId *next = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * nextSize);
  524. if(!next) {
  525. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  526. UA_free(result->targets);
  527. UA_free(current);
  528. return;
  529. }
  530. /* Copy the starting node into current */
  531. result->statusCode = UA_NodeId_copy(&path->startingNode, &current[0]);
  532. if(result->statusCode != UA_STATUSCODE_GOOD) {
  533. UA_free(result->targets);
  534. UA_free(current);
  535. UA_free(next);
  536. return;
  537. }
  538. currentCount = 1;
  539. /* Walk the path elements */
  540. walkBrowsePath(server, session, path, result, targetsSize,
  541. &current, &currentSize, &currentCount,
  542. &next, &nextSize, &nextCount);
  543. UA_assert(currentCount == 0);
  544. UA_assert(nextCount == 0);
  545. /* No results => BadNoMatch status code */
  546. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  547. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  548. /* Clean up the temporary arrays and the targets */
  549. UA_free(current);
  550. UA_free(next);
  551. if(result->statusCode != UA_STATUSCODE_GOOD) {
  552. for(size_t i = 0; i < result->targetsSize; ++i)
  553. UA_BrowsePathTarget_deleteMembers(&result->targets[i]);
  554. UA_free(result->targets);
  555. result->targets = NULL;
  556. result->targetsSize = 0;
  557. }
  558. }
  559. UA_BrowsePathResult
  560. UA_Server_translateBrowsePathToNodeIds(UA_Server *server,
  561. const UA_BrowsePath *browsePath) {
  562. UA_BrowsePathResult result;
  563. UA_BrowsePathResult_init(&result);
  564. UA_RCU_LOCK();
  565. Operation_TranslateBrowsePathToNodeIds(server, &adminSession, browsePath, &result);
  566. UA_RCU_UNLOCK();
  567. return result;
  568. }
  569. void
  570. Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  571. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  572. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  573. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  574. "Processing TranslateBrowsePathsToNodeIdsRequest", NULL);
  575. response->responseHeader.serviceResult =
  576. UA_Server_processServiceOperations(server, session,
  577. (UA_ServiceOperation)Operation_TranslateBrowsePathToNodeIds,
  578. &request->browsePathsSize, &UA_TYPES[UA_TYPES_BROWSEPATH],
  579. &response->resultsSize, &UA_TYPES[UA_TYPES_BROWSEPATHRESULT]);
  580. }
  581. void Service_RegisterNodes(UA_Server *server, UA_Session *session,
  582. const UA_RegisterNodesRequest *request,
  583. UA_RegisterNodesResponse *response) {
  584. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  585. "Processing RegisterNodesRequest", NULL);
  586. //TODO: hang the nodeids to the session if really needed
  587. if(request->nodesToRegisterSize == 0) {
  588. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  589. return;
  590. }
  591. response->responseHeader.serviceResult =
  592. UA_Array_copy(request->nodesToRegister, request->nodesToRegisterSize,
  593. (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID]);
  594. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  595. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  596. }
  597. void Service_UnregisterNodes(UA_Server *server, UA_Session *session,
  598. const UA_UnregisterNodesRequest *request,
  599. UA_UnregisterNodesResponse *response) {
  600. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  601. "Processing UnRegisterNodesRequest", NULL);
  602. //TODO: remove the nodeids from the session if really needed
  603. if(request->nodesToUnregisterSize == 0)
  604. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  605. }