ua_services_view.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 ns The nodstore where the to-be-browsed node can be found
  128. * @param cp If (*cp) points to a continuationpoint, we continue from there.
  129. * If (*cp) is null, we can set it to a new continuation point
  130. * @param descr If no cp is set, we take the browsedescription from there
  131. * @param maxrefs The maximum number of references the client has requested
  132. * @param result The entry in the request
  133. */
  134. static void browse(UA_NodeStore *ns, struct ContinuationPointEntry **cpp, const UA_BrowseDescription *descr,
  135. UA_UInt32 maxrefs, UA_BrowseResult *result) {
  136. UA_UInt32 continuationIndex = 0;
  137. struct ContinuationPointEntry *cp = UA_NULL;
  138. if(cpp)
  139. cp = *cpp;
  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 count = 0;
  204. size_t skipped = 0;
  205. UA_Int32 i = 0;
  206. for(; i < node->referencesSize && count < real_maxrefs; i++) {
  207. const UA_Node *current = relevant_node(ns, descr, all_refs, &node->references[i],
  208. relevant_refs, relevant_refs_size);
  209. if(!current)
  210. continue;
  211. if(skipped < continuationIndex) {
  212. UA_NodeStore_release(current);
  213. skipped++;
  214. continue;
  215. }
  216. UA_StatusCode retval = fillrefdescr(ns, current, &node->references[i], descr->resultMask,
  217. &result->references[count]);
  218. UA_NodeStore_release(current);
  219. if(retval != UA_STATUSCODE_GOOD) {
  220. UA_Array_delete(result->references, &UA_TYPES[UA_TYPES_REFERENCEDESCRIPTION], count);
  221. result->references = UA_NULL;
  222. result->referencesSize = 0;
  223. result->statusCode = UA_STATUSCODE_UNCERTAINNOTALLNODESAVAILABLE;
  224. goto cleanup;
  225. }
  226. count++;
  227. }
  228. result->referencesSize = count;
  229. if(count == 0) {
  230. UA_free(result->references);
  231. result->references = UA_NULL;
  232. }
  233. cleanup:
  234. UA_NodeStore_release(node);
  235. if(!all_refs && descr->includeSubtypes)
  236. UA_Array_delete(relevant_refs, &UA_TYPES[UA_TYPES_NODEID], relevant_refs_size);
  237. if(result->statusCode != UA_STATUSCODE_GOOD)
  238. return;
  239. /* create, update, delete continuation points */
  240. if(cp) {
  241. if(i == node->referencesSize) {
  242. /* all done, remove a finished continuationPoint */
  243. UA_ByteString_deleteMembers(&cp->identifier);
  244. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  245. LIST_REMOVE(cp, pointers);
  246. UA_free(cp);
  247. cp = UA_NULL;
  248. } else {
  249. /* update the cp and return the cp identifier */
  250. cp->continuationIndex += count;
  251. UA_ByteString_copy(&cp->identifier, &result->continuationPoint);
  252. }
  253. } else if(maxrefs != 0 && count >= maxrefs) {
  254. /* create a continuationPoint, it is added to the session in the calling Service_Browse */
  255. cp = UA_malloc(sizeof(struct ContinuationPointEntry));
  256. if(!cp)
  257. return;
  258. UA_BrowseDescription_copy(descr, &cp->browseDescription);
  259. cp->maxReferences = maxrefs;
  260. cp->continuationIndex = count;
  261. UA_Guid *ident = UA_Guid_new();
  262. UA_UInt32 seed = (uintptr_t)cp;
  263. *ident = UA_Guid_random(&seed);
  264. cp->identifier.data = (UA_Byte*)ident;
  265. cp->identifier.length = sizeof(UA_Guid);
  266. *cpp = cp;
  267. }
  268. }
  269. void Service_Browse(UA_Server *server, UA_Session *session, const UA_BrowseRequest *request,
  270. UA_BrowseResponse *response) {
  271. if(!UA_NodeId_isNull(&request->view.viewId)) {
  272. response->responseHeader.serviceResult = UA_STATUSCODE_BADVIEWIDUNKNOWN;
  273. return;
  274. }
  275. if(request->nodesToBrowseSize <= 0) {
  276. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  277. return;
  278. }
  279. size_t size = request->nodesToBrowseSize;
  280. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  281. if(!response->results) {
  282. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  283. return;
  284. }
  285. /* ### Begin External Namespaces */
  286. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  287. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  288. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  289. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  290. size_t indexSize = 0;
  291. for(size_t i = 0;i < size;i++) {
  292. if(request->nodesToBrowse[i].nodeId.namespaceIndex != server->externalNamespaces[j].index)
  293. continue;
  294. isExternal[i] = UA_TRUE;
  295. indices[indexSize] = i;
  296. indexSize++;
  297. }
  298. if(indexSize == 0)
  299. continue;
  300. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  301. ens->browseNodes(ens->ensHandle, &request->requestHeader, request->nodesToBrowse, indices, indexSize,
  302. request->requestedMaxReferencesPerNode, response->results, response->diagnosticInfos);
  303. }
  304. /* ### End External Namespaces */
  305. response->resultsSize = size;
  306. for(size_t i = 0; i < size; i++) {
  307. if(!isExternal[i]) {
  308. struct ContinuationPointEntry *cp = UA_NULL;
  309. browse(server->nodestore, &cp, &request->nodesToBrowse[i],
  310. request->requestedMaxReferencesPerNode, &response->results[i]);
  311. if(cp) {
  312. LIST_INSERT_HEAD(&session->continuationPoints, cp, pointers);
  313. UA_ByteString_copy(&cp->identifier, &response->results[i].continuationPoint);
  314. }
  315. }
  316. }
  317. }
  318. void Service_BrowseNext(UA_Server *server, UA_Session *session, const UA_BrowseNextRequest *request,
  319. UA_BrowseNextResponse *response) {
  320. if(request->continuationPointsSize <= 0) {
  321. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  322. return;
  323. }
  324. size_t size = request->continuationPointsSize;
  325. if(!request->releaseContinuationPoints) {
  326. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSERESULT], size);
  327. if(!response->results) {
  328. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  329. return;
  330. }
  331. response->resultsSize = size;
  332. for(size_t i = 0; i < size; i++) {
  333. struct ContinuationPointEntry *cp = UA_NULL;
  334. struct ContinuationPointEntry *search_cp;
  335. LIST_FOREACH(search_cp, &session->continuationPoints, pointers) {
  336. if(UA_ByteString_equal(&search_cp->identifier, &request->continuationPoints[i])) {
  337. cp = search_cp;
  338. break;
  339. }
  340. }
  341. if(!cp)
  342. response->results[i].statusCode = UA_STATUSCODE_BADCONTINUATIONPOINTINVALID;
  343. else
  344. browse(server->nodestore, &cp, UA_NULL, 0, &response->results[i]);
  345. }
  346. } else {
  347. for(size_t i = 0; i < size; i++) {
  348. struct ContinuationPointEntry *cp = UA_NULL;
  349. LIST_FOREACH(cp, &session->continuationPoints, pointers) {
  350. if(UA_ByteString_equal(&cp->identifier, &request->continuationPoints[i])) {
  351. UA_ByteString_deleteMembers(&cp->identifier);
  352. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  353. LIST_REMOVE(cp, pointers);
  354. UA_free(cp);
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. /***********************/
  362. /* TranslateBrowsePath */
  363. /***********************/
  364. static UA_StatusCode
  365. walkBrowsePath(UA_Server *server, UA_Session *session, const UA_Node *node, const UA_RelativePath *path,
  366. UA_Int32 pathindex, UA_BrowsePathTarget **targets, UA_Int32 *targets_size,
  367. UA_Int32 *target_count)
  368. {
  369. const UA_RelativePathElement *elem = &path->elements[pathindex];
  370. if(elem->targetName.name.length == -1)
  371. return UA_STATUSCODE_BADBROWSENAMEINVALID;
  372. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  373. UA_NodeId *reftypes;
  374. size_t reftypes_count = 1; // all_refs or no subtypes => 1
  375. UA_Boolean all_refs = UA_FALSE;
  376. if(UA_NodeId_isNull(&elem->referenceTypeId))
  377. all_refs = UA_TRUE;
  378. else if(!elem->includeSubtypes)
  379. reftypes = (UA_NodeId*)(uintptr_t)&elem->referenceTypeId; // ptr magic due to const cast
  380. else {
  381. retval = findsubtypes(server->nodestore, &elem->referenceTypeId, &reftypes, &reftypes_count);
  382. if(retval != UA_STATUSCODE_GOOD)
  383. return retval;
  384. }
  385. for(UA_Int32 i = 0; i < node->referencesSize && retval == UA_STATUSCODE_GOOD; i++) {
  386. UA_Boolean match = all_refs;
  387. for(size_t j = 0; j < reftypes_count && !match; j++) {
  388. if(node->references[i].isInverse == elem->isInverse &&
  389. UA_NodeId_equal(&node->references[i].referenceTypeId, &reftypes[j]))
  390. match = UA_TRUE;
  391. }
  392. if(!match)
  393. continue;
  394. // get the node, todo: expandednodeid
  395. const UA_Node *next = UA_NodeStore_get(server->nodestore, &node->references[i].targetId.nodeId);
  396. if(!next)
  397. continue;
  398. // test the browsename
  399. if(elem->targetName.namespaceIndex != next->browseName.namespaceIndex ||
  400. !UA_String_equal(&elem->targetName.name, &next->browseName.name)) {
  401. UA_NodeStore_release(next);
  402. continue;
  403. }
  404. if(pathindex + 1 < path->elementsSize) {
  405. // recursion if the path is longer
  406. retval = walkBrowsePath(server, session, next, path, pathindex + 1,
  407. targets, targets_size, target_count);
  408. UA_NodeStore_release(next);
  409. } else {
  410. // add the browsetarget
  411. if(*target_count >= *targets_size) {
  412. UA_BrowsePathTarget *newtargets;
  413. newtargets = UA_realloc(targets, sizeof(UA_BrowsePathTarget) * (*targets_size) * 2);
  414. if(!newtargets) {
  415. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  416. UA_NodeStore_release(next);
  417. break;
  418. }
  419. *targets = newtargets;
  420. *targets_size *= 2;
  421. }
  422. UA_BrowsePathTarget *res = *targets;
  423. UA_ExpandedNodeId_init(&res[*target_count].targetId);
  424. retval = UA_NodeId_copy(&next->nodeId, &res[*target_count].targetId.nodeId);
  425. UA_NodeStore_release(next);
  426. if(retval != UA_STATUSCODE_GOOD)
  427. break;
  428. res[*target_count].remainingPathIndex = UA_UINT32_MAX;
  429. *target_count += 1;
  430. }
  431. }
  432. if(!all_refs && elem->includeSubtypes)
  433. UA_Array_delete(reftypes, &UA_TYPES[UA_TYPES_NODEID], (UA_Int32)reftypes_count);
  434. return retval;
  435. }
  436. static void translateBrowsePath(UA_Server *server, UA_Session *session, const UA_BrowsePath *path,
  437. UA_BrowsePathResult *result) {
  438. if(path->relativePath.elementsSize <= 0) {
  439. result->statusCode = UA_STATUSCODE_BADNOTHINGTODO;
  440. return;
  441. }
  442. UA_Int32 arraySize = 10;
  443. result->targets = UA_malloc(sizeof(UA_BrowsePathTarget) * arraySize);
  444. if(!result->targets) {
  445. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  446. return;
  447. }
  448. result->targetsSize = 0;
  449. const UA_Node *firstNode = UA_NodeStore_get(server->nodestore, &path->startingNode);
  450. if(!firstNode) {
  451. result->statusCode = UA_STATUSCODE_BADNODEIDUNKNOWN;
  452. UA_free(result->targets);
  453. result->targets = UA_NULL;
  454. return;
  455. }
  456. result->statusCode = walkBrowsePath(server, session, firstNode, &path->relativePath, 0,
  457. &result->targets, &arraySize, &result->targetsSize);
  458. UA_NodeStore_release(firstNode);
  459. if(result->targetsSize == 0 && result->statusCode == UA_STATUSCODE_GOOD)
  460. result->statusCode = UA_STATUSCODE_BADNOMATCH;
  461. if(result->statusCode != UA_STATUSCODE_GOOD) {
  462. UA_Array_delete(result->targets, &UA_TYPES[UA_TYPES_BROWSEPATHTARGET], result->targetsSize);
  463. result->targets = UA_NULL;
  464. result->targetsSize = -1;
  465. }
  466. }
  467. void Service_TranslateBrowsePathsToNodeIds(UA_Server *server, UA_Session *session,
  468. const UA_TranslateBrowsePathsToNodeIdsRequest *request,
  469. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  470. if(request->browsePathsSize <= 0) {
  471. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  472. return;
  473. }
  474. size_t size = request->browsePathsSize;
  475. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_BROWSEPATHRESULT], size);
  476. if(!response->results) {
  477. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  478. return;
  479. }
  480. /* ### Begin External Namespaces */
  481. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  482. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  483. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  484. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  485. size_t indexSize = 0;
  486. for(size_t i = 0;i < size;i++) {
  487. if(request->browsePaths[i].startingNode.namespaceIndex != server->externalNamespaces[j].index)
  488. continue;
  489. isExternal[i] = UA_TRUE;
  490. indices[indexSize] = i;
  491. indexSize++;
  492. }
  493. if(indexSize == 0)
  494. continue;
  495. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  496. ens->translateBrowsePathsToNodeIds(ens->ensHandle, &request->requestHeader, request->browsePaths,
  497. indices, indexSize, response->results, response->diagnosticInfos);
  498. }
  499. /* ### End External Namespaces */
  500. response->resultsSize = size;
  501. for(size_t i = 0; i < size; i++){
  502. if(!isExternal[i])
  503. translateBrowsePath(server, session, &request->browsePaths[i], &response->results[i]);
  504. }
  505. }
  506. void Service_RegisterNodes(UA_Server *server, UA_Session *session, const UA_RegisterNodesRequest *request,
  507. UA_RegisterNodesResponse *response) {
  508. //TODO: hang the nodeids to the session if really needed
  509. response->responseHeader.timestamp = UA_DateTime_now();
  510. if(request->nodesToRegisterSize <= 0)
  511. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  512. else {
  513. response->responseHeader.serviceResult =
  514. UA_Array_copy(request->nodesToRegister, (void**)&response->registeredNodeIds, &UA_TYPES[UA_TYPES_NODEID],
  515. request->nodesToRegisterSize);
  516. if(response->responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  517. response->registeredNodeIdsSize = request->nodesToRegisterSize;
  518. }
  519. }
  520. void Service_UnregisterNodes(UA_Server *server, UA_Session *session, const UA_UnregisterNodesRequest *request,
  521. UA_UnregisterNodesResponse *response) {
  522. //TODO: remove the nodeids from the session if really needed
  523. response->responseHeader.timestamp = UA_DateTime_now();
  524. if(request->nodesToUnregisterSize==0)
  525. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  526. }