ua_nodes.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2015-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2015-2016 (c) Sten Grüner
  7. * Copyright 2015 (c) Chris Iatrou
  8. * Copyright 2015, 2017 (c) Florian Palm
  9. * Copyright 2015 (c) Oleksiy Vasylyev
  10. * Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2017 (c) Julian Grothoff
  12. */
  13. #include "ua_server_internal.h"
  14. #include "ua_types_encoding_binary.h"
  15. /* There is no UA_Node_new() method here. Creating nodes is part of the
  16. * NodeStore layer */
  17. void UA_Node_deleteMembers(UA_Node *node) {
  18. /* Delete standard content */
  19. UA_NodeId_deleteMembers(&node->nodeId);
  20. UA_QualifiedName_deleteMembers(&node->browseName);
  21. UA_LocalizedText_deleteMembers(&node->displayName);
  22. UA_LocalizedText_deleteMembers(&node->description);
  23. /* Delete references */
  24. UA_Node_deleteReferences(node);
  25. /* Delete unique content of the nodeclass */
  26. switch(node->nodeClass) {
  27. case UA_NODECLASS_OBJECT:
  28. break;
  29. case UA_NODECLASS_METHOD:
  30. break;
  31. case UA_NODECLASS_OBJECTTYPE:
  32. break;
  33. case UA_NODECLASS_VARIABLE:
  34. case UA_NODECLASS_VARIABLETYPE: {
  35. UA_VariableNode *p = (UA_VariableNode*)node;
  36. UA_NodeId_deleteMembers(&p->dataType);
  37. UA_Array_delete(p->arrayDimensions, p->arrayDimensionsSize,
  38. &UA_TYPES[UA_TYPES_INT32]);
  39. p->arrayDimensions = NULL;
  40. p->arrayDimensionsSize = 0;
  41. if(p->valueSource == UA_VALUESOURCE_DATA)
  42. UA_DataValue_deleteMembers(&p->value.data.value);
  43. break;
  44. }
  45. case UA_NODECLASS_REFERENCETYPE: {
  46. UA_ReferenceTypeNode *p = (UA_ReferenceTypeNode*)node;
  47. UA_LocalizedText_deleteMembers(&p->inverseName);
  48. break;
  49. }
  50. case UA_NODECLASS_DATATYPE:
  51. break;
  52. case UA_NODECLASS_VIEW:
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. static UA_StatusCode
  59. UA_ObjectNode_copy(const UA_ObjectNode *src, UA_ObjectNode *dst) {
  60. dst->eventNotifier = src->eventNotifier;
  61. return UA_STATUSCODE_GOOD;
  62. }
  63. static UA_StatusCode
  64. UA_CommonVariableNode_copy(const UA_VariableNode *src, UA_VariableNode *dst) {
  65. UA_StatusCode retval = UA_Array_copy(src->arrayDimensions,
  66. src->arrayDimensionsSize,
  67. (void**)&dst->arrayDimensions,
  68. &UA_TYPES[UA_TYPES_INT32]);
  69. if(retval != UA_STATUSCODE_GOOD)
  70. return retval;
  71. dst->arrayDimensionsSize = src->arrayDimensionsSize;
  72. retval = UA_NodeId_copy(&src->dataType, &dst->dataType);
  73. dst->valueRank = src->valueRank;
  74. dst->valueSource = src->valueSource;
  75. if(src->valueSource == UA_VALUESOURCE_DATA) {
  76. retval |= UA_DataValue_copy(&src->value.data.value,
  77. &dst->value.data.value);
  78. dst->value.data.callback = src->value.data.callback;
  79. } else
  80. dst->value.dataSource = src->value.dataSource;
  81. return retval;
  82. }
  83. static UA_StatusCode
  84. UA_VariableNode_copy(const UA_VariableNode *src, UA_VariableNode *dst) {
  85. UA_StatusCode retval = UA_CommonVariableNode_copy(src, dst);
  86. dst->accessLevel = src->accessLevel;
  87. dst->minimumSamplingInterval = src->minimumSamplingInterval;
  88. dst->historizing = src->historizing;
  89. return retval;
  90. }
  91. static UA_StatusCode
  92. UA_VariableTypeNode_copy(const UA_VariableTypeNode *src,
  93. UA_VariableTypeNode *dst) {
  94. UA_StatusCode retval = UA_CommonVariableNode_copy((const UA_VariableNode*)src,
  95. (UA_VariableNode*)dst);
  96. dst->isAbstract = src->isAbstract;
  97. return retval;
  98. }
  99. static UA_StatusCode
  100. UA_MethodNode_copy(const UA_MethodNode *src, UA_MethodNode *dst) {
  101. dst->executable = src->executable;
  102. dst->method = src->method;
  103. return UA_STATUSCODE_GOOD;
  104. }
  105. static UA_StatusCode
  106. UA_ObjectTypeNode_copy(const UA_ObjectTypeNode *src, UA_ObjectTypeNode *dst) {
  107. dst->isAbstract = src->isAbstract;
  108. dst->lifecycle = src->lifecycle;
  109. return UA_STATUSCODE_GOOD;
  110. }
  111. static UA_StatusCode
  112. UA_ReferenceTypeNode_copy(const UA_ReferenceTypeNode *src,
  113. UA_ReferenceTypeNode *dst) {
  114. UA_StatusCode retval = UA_LocalizedText_copy(&src->inverseName,
  115. &dst->inverseName);
  116. dst->isAbstract = src->isAbstract;
  117. dst->symmetric = src->symmetric;
  118. return retval;
  119. }
  120. static UA_StatusCode
  121. UA_DataTypeNode_copy(const UA_DataTypeNode *src, UA_DataTypeNode *dst) {
  122. dst->isAbstract = src->isAbstract;
  123. return UA_STATUSCODE_GOOD;
  124. }
  125. static UA_StatusCode
  126. UA_ViewNode_copy(const UA_ViewNode *src, UA_ViewNode *dst) {
  127. dst->containsNoLoops = src->containsNoLoops;
  128. dst->eventNotifier = src->eventNotifier;
  129. return UA_STATUSCODE_GOOD;
  130. }
  131. UA_StatusCode
  132. UA_Node_copy(const UA_Node *src, UA_Node *dst) {
  133. if(src->nodeClass != dst->nodeClass)
  134. return UA_STATUSCODE_BADINTERNALERROR;
  135. /* Copy standard content */
  136. UA_StatusCode retval = UA_NodeId_copy(&src->nodeId, &dst->nodeId);
  137. retval |= UA_QualifiedName_copy(&src->browseName, &dst->browseName);
  138. retval |= UA_LocalizedText_copy(&src->displayName, &dst->displayName);
  139. retval |= UA_LocalizedText_copy(&src->description, &dst->description);
  140. dst->writeMask = src->writeMask;
  141. dst->context = src->context;
  142. if(retval != UA_STATUSCODE_GOOD) {
  143. UA_Node_deleteMembers(dst);
  144. return retval;
  145. }
  146. /* Copy the references */
  147. dst->references = NULL;
  148. if(src->referencesSize > 0) {
  149. dst->references = (UA_NodeReferenceKind*)
  150. UA_calloc(src->referencesSize, sizeof(UA_NodeReferenceKind));
  151. if(!dst->references) {
  152. UA_Node_deleteMembers(dst);
  153. return UA_STATUSCODE_BADOUTOFMEMORY;
  154. }
  155. dst->referencesSize = src->referencesSize;
  156. for(size_t i = 0; i < src->referencesSize; ++i) {
  157. UA_NodeReferenceKind *srefs = &src->references[i];
  158. UA_NodeReferenceKind *drefs = &dst->references[i];
  159. drefs->isInverse = srefs->isInverse;
  160. retval = UA_NodeId_copy(&srefs->referenceTypeId, &drefs->referenceTypeId);
  161. if(retval != UA_STATUSCODE_GOOD)
  162. break;
  163. retval = UA_Array_copy(srefs->targetIds, srefs->targetIdsSize,
  164. (void**)&drefs->targetIds,
  165. &UA_TYPES[UA_TYPES_EXPANDEDNODEID]);
  166. if(retval != UA_STATUSCODE_GOOD)
  167. break;
  168. drefs->targetIdsSize = srefs->targetIdsSize;
  169. }
  170. if(retval != UA_STATUSCODE_GOOD) {
  171. UA_Node_deleteMembers(dst);
  172. return retval;
  173. }
  174. }
  175. /* Copy unique content of the nodeclass */
  176. switch(src->nodeClass) {
  177. case UA_NODECLASS_OBJECT:
  178. retval = UA_ObjectNode_copy((const UA_ObjectNode*)src, (UA_ObjectNode*)dst);
  179. break;
  180. case UA_NODECLASS_VARIABLE:
  181. retval = UA_VariableNode_copy((const UA_VariableNode*)src, (UA_VariableNode*)dst);
  182. break;
  183. case UA_NODECLASS_METHOD:
  184. retval = UA_MethodNode_copy((const UA_MethodNode*)src, (UA_MethodNode*)dst);
  185. break;
  186. case UA_NODECLASS_OBJECTTYPE:
  187. retval = UA_ObjectTypeNode_copy((const UA_ObjectTypeNode*)src, (UA_ObjectTypeNode*)dst);
  188. break;
  189. case UA_NODECLASS_VARIABLETYPE:
  190. retval = UA_VariableTypeNode_copy((const UA_VariableTypeNode*)src, (UA_VariableTypeNode*)dst);
  191. break;
  192. case UA_NODECLASS_REFERENCETYPE:
  193. retval = UA_ReferenceTypeNode_copy((const UA_ReferenceTypeNode*)src, (UA_ReferenceTypeNode*)dst);
  194. break;
  195. case UA_NODECLASS_DATATYPE:
  196. retval = UA_DataTypeNode_copy((const UA_DataTypeNode*)src, (UA_DataTypeNode*)dst);
  197. break;
  198. case UA_NODECLASS_VIEW:
  199. retval = UA_ViewNode_copy((const UA_ViewNode*)src, (UA_ViewNode*)dst);
  200. break;
  201. default:
  202. break;
  203. }
  204. if(retval != UA_STATUSCODE_GOOD)
  205. UA_Node_deleteMembers(dst);
  206. return retval;
  207. }
  208. UA_Node *
  209. UA_Node_copy_alloc(const UA_Node *src) {
  210. /* use dstPtr to trick static code analysis in accepting dirty cast */
  211. size_t nodesize = 0;
  212. switch(src->nodeClass) {
  213. case UA_NODECLASS_OBJECT:
  214. nodesize = sizeof(UA_ObjectNode);
  215. break;
  216. case UA_NODECLASS_VARIABLE:
  217. nodesize = sizeof(UA_VariableNode);
  218. break;
  219. case UA_NODECLASS_METHOD:
  220. nodesize = sizeof(UA_MethodNode);
  221. break;
  222. case UA_NODECLASS_OBJECTTYPE:
  223. nodesize = sizeof(UA_ObjectTypeNode);
  224. break;
  225. case UA_NODECLASS_VARIABLETYPE:
  226. nodesize = sizeof(UA_VariableTypeNode);
  227. break;
  228. case UA_NODECLASS_REFERENCETYPE:
  229. nodesize = sizeof(UA_ReferenceTypeNode);
  230. break;
  231. case UA_NODECLASS_DATATYPE:
  232. nodesize = sizeof(UA_DataTypeNode);
  233. break;
  234. case UA_NODECLASS_VIEW:
  235. nodesize = sizeof(UA_ViewNode);
  236. break;
  237. default:
  238. return NULL;
  239. }
  240. UA_Node *dst = (UA_Node*)UA_calloc(1,nodesize);
  241. if(!dst)
  242. return NULL;
  243. dst->nodeClass = src->nodeClass;
  244. UA_StatusCode retval = UA_Node_copy(src, dst);
  245. if(retval != UA_STATUSCODE_GOOD) {
  246. UA_free(dst);
  247. return NULL;
  248. }
  249. return dst;
  250. }
  251. /******************************/
  252. /* Copy Attributes into Nodes */
  253. /******************************/
  254. static UA_StatusCode
  255. copyStandardAttributes(UA_Node *node, const UA_NodeAttributes *attr) {
  256. /* retval = UA_NodeId_copy(&item->requestedNewNodeId.nodeId, &node->nodeId); */
  257. /* retval |= UA_QualifiedName_copy(&item->browseName, &node->browseName); */
  258. UA_StatusCode retval = UA_LocalizedText_copy(&attr->displayName,
  259. &node->displayName);
  260. retval |= UA_LocalizedText_copy(&attr->description, &node->description);
  261. node->writeMask = attr->writeMask;
  262. return retval;
  263. }
  264. static UA_StatusCode
  265. copyCommonVariableAttributes(UA_VariableNode *node,
  266. const UA_VariableAttributes *attr) {
  267. /* Copy the array dimensions */
  268. UA_StatusCode retval =
  269. UA_Array_copy(attr->arrayDimensions, attr->arrayDimensionsSize,
  270. (void**)&node->arrayDimensions, &UA_TYPES[UA_TYPES_UINT32]);
  271. if(retval != UA_STATUSCODE_GOOD)
  272. return retval;
  273. node->arrayDimensionsSize = attr->arrayDimensionsSize;
  274. /* Data type and value rank */
  275. retval |= UA_NodeId_copy(&attr->dataType, &node->dataType);
  276. node->valueRank = attr->valueRank;
  277. /* Copy the value */
  278. node->valueSource = UA_VALUESOURCE_DATA;
  279. UA_NodeId extensionObject = UA_NODEID_NUMERIC(0, UA_NS0ID_STRUCTURE);
  280. /* if we have an extension object which is still encoded (e.g. from the nodeset compiler)
  281. * we need to decode it and set the decoded value instead of the encoded object */
  282. UA_Boolean valueSet = false;
  283. if(attr->value.type != NULL && UA_NodeId_equal(&attr->value.type->typeId, &extensionObject)) {
  284. if (attr->value.data == UA_EMPTY_ARRAY_SENTINEL) {
  285. /* do nothing since we got an empty array of extension objects */
  286. return UA_STATUSCODE_GOOD;
  287. }
  288. const UA_ExtensionObject *obj = (const UA_ExtensionObject *)attr->value.data;
  289. if(obj && obj->encoding == UA_EXTENSIONOBJECT_ENCODED_BYTESTRING) {
  290. /* TODO: Once we generate type description in the nodeset compiler,
  291. * UA_findDatatypeByBinary can be made internal to the decoding
  292. * layer. */
  293. const UA_DataType *type = UA_findDataTypeByBinary(&obj->content.encoded.typeId);
  294. if(type) {
  295. void *dst = UA_Array_new(attr->value.arrayLength, type);
  296. if (!dst) {
  297. return UA_STATUSCODE_BADOUTOFMEMORY;
  298. }
  299. uint8_t *tmpPos = (uint8_t *)dst;
  300. for(size_t i=0; i<attr->value.arrayLength; i++) {
  301. size_t offset =0;
  302. const UA_ExtensionObject *curr = &((const UA_ExtensionObject *)attr->value.data)[i];
  303. UA_StatusCode ret = UA_decodeBinary(&curr->content.encoded.body, &offset, tmpPos, type, NULL);
  304. if(ret != UA_STATUSCODE_GOOD) {
  305. return ret;
  306. }
  307. tmpPos += type->memSize;
  308. }
  309. UA_Variant_setArray(&node->value.data.value.value, dst, attr->value.arrayLength, type);
  310. valueSet = true;
  311. }
  312. }
  313. }
  314. if(!valueSet)
  315. retval |= UA_Variant_copy(&attr->value, &node->value.data.value.value);
  316. node->value.data.value.hasValue = true;
  317. return retval;
  318. }
  319. static UA_StatusCode
  320. copyVariableNodeAttributes(UA_VariableNode *vnode,
  321. const UA_VariableAttributes *attr) {
  322. vnode->accessLevel = attr->accessLevel;
  323. vnode->historizing = attr->historizing;
  324. vnode->minimumSamplingInterval = attr->minimumSamplingInterval;
  325. return copyCommonVariableAttributes(vnode, attr);
  326. }
  327. static UA_StatusCode
  328. copyVariableTypeNodeAttributes(UA_VariableTypeNode *vtnode,
  329. const UA_VariableTypeAttributes *attr) {
  330. vtnode->isAbstract = attr->isAbstract;
  331. return copyCommonVariableAttributes((UA_VariableNode*)vtnode,
  332. (const UA_VariableAttributes*)attr);
  333. }
  334. static UA_StatusCode
  335. copyObjectNodeAttributes(UA_ObjectNode *onode, const UA_ObjectAttributes *attr) {
  336. onode->eventNotifier = attr->eventNotifier;
  337. return UA_STATUSCODE_GOOD;
  338. }
  339. static UA_StatusCode
  340. copyReferenceTypeNodeAttributes(UA_ReferenceTypeNode *rtnode,
  341. const UA_ReferenceTypeAttributes *attr) {
  342. rtnode->isAbstract = attr->isAbstract;
  343. rtnode->symmetric = attr->symmetric;
  344. return UA_LocalizedText_copy(&attr->inverseName, &rtnode->inverseName);
  345. }
  346. static UA_StatusCode
  347. copyObjectTypeNodeAttributes(UA_ObjectTypeNode *otnode,
  348. const UA_ObjectTypeAttributes *attr) {
  349. otnode->isAbstract = attr->isAbstract;
  350. return UA_STATUSCODE_GOOD;
  351. }
  352. static UA_StatusCode
  353. copyViewNodeAttributes(UA_ViewNode *vnode, const UA_ViewAttributes *attr) {
  354. vnode->containsNoLoops = attr->containsNoLoops;
  355. vnode->eventNotifier = attr->eventNotifier;
  356. return UA_STATUSCODE_GOOD;
  357. }
  358. static UA_StatusCode
  359. copyDataTypeNodeAttributes(UA_DataTypeNode *dtnode,
  360. const UA_DataTypeAttributes *attr) {
  361. dtnode->isAbstract = attr->isAbstract;
  362. return UA_STATUSCODE_GOOD;
  363. }
  364. static UA_StatusCode
  365. copyMethodNodeAttributes(UA_MethodNode *mnode,
  366. const UA_MethodAttributes *attr) {
  367. mnode->executable = attr->executable;
  368. return UA_STATUSCODE_GOOD;
  369. }
  370. #define CHECK_ATTRIBUTES(TYPE) \
  371. if(attributeType != &UA_TYPES[UA_TYPES_##TYPE]) { \
  372. retval = UA_STATUSCODE_BADNODEATTRIBUTESINVALID; \
  373. break; \
  374. }
  375. UA_StatusCode
  376. UA_Node_setAttributes(UA_Node *node, const void *attributes,
  377. const UA_DataType *attributeType) {
  378. /* Copy the attributes into the node */
  379. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  380. switch(node->nodeClass) {
  381. case UA_NODECLASS_OBJECT:
  382. CHECK_ATTRIBUTES(OBJECTATTRIBUTES);
  383. retval = copyObjectNodeAttributes((UA_ObjectNode*)node,
  384. (const UA_ObjectAttributes*)attributes);
  385. break;
  386. case UA_NODECLASS_VARIABLE:
  387. CHECK_ATTRIBUTES(VARIABLEATTRIBUTES);
  388. retval = copyVariableNodeAttributes((UA_VariableNode*)node,
  389. (const UA_VariableAttributes*)attributes);
  390. break;
  391. case UA_NODECLASS_OBJECTTYPE:
  392. CHECK_ATTRIBUTES(OBJECTTYPEATTRIBUTES);
  393. retval = copyObjectTypeNodeAttributes((UA_ObjectTypeNode*)node,
  394. (const UA_ObjectTypeAttributes*)attributes);
  395. break;
  396. case UA_NODECLASS_VARIABLETYPE:
  397. CHECK_ATTRIBUTES(VARIABLETYPEATTRIBUTES);
  398. retval = copyVariableTypeNodeAttributes((UA_VariableTypeNode*)node,
  399. (const UA_VariableTypeAttributes*)attributes);
  400. break;
  401. case UA_NODECLASS_REFERENCETYPE:
  402. CHECK_ATTRIBUTES(REFERENCETYPEATTRIBUTES);
  403. retval = copyReferenceTypeNodeAttributes((UA_ReferenceTypeNode*)node,
  404. (const UA_ReferenceTypeAttributes*)attributes);
  405. break;
  406. case UA_NODECLASS_DATATYPE:
  407. CHECK_ATTRIBUTES(DATATYPEATTRIBUTES);
  408. retval = copyDataTypeNodeAttributes((UA_DataTypeNode*)node,
  409. (const UA_DataTypeAttributes*)attributes);
  410. break;
  411. case UA_NODECLASS_VIEW:
  412. CHECK_ATTRIBUTES(VIEWATTRIBUTES);
  413. retval = copyViewNodeAttributes((UA_ViewNode*)node,
  414. (const UA_ViewAttributes*)attributes);
  415. break;
  416. case UA_NODECLASS_METHOD:
  417. CHECK_ATTRIBUTES(METHODATTRIBUTES);
  418. retval = copyMethodNodeAttributes((UA_MethodNode*)node,
  419. (const UA_MethodAttributes*)attributes);
  420. break;
  421. case UA_NODECLASS_UNSPECIFIED:
  422. default:
  423. retval = UA_STATUSCODE_BADNODECLASSINVALID;
  424. }
  425. if(retval == UA_STATUSCODE_GOOD)
  426. retval = copyStandardAttributes(node, (const UA_NodeAttributes*)attributes);
  427. if(retval != UA_STATUSCODE_GOOD)
  428. UA_Node_deleteMembers(node);
  429. return retval;
  430. }
  431. /*********************/
  432. /* Manage References */
  433. /*********************/
  434. static UA_StatusCode
  435. addReferenceTarget(UA_NodeReferenceKind *refs, const UA_ExpandedNodeId *target) {
  436. UA_ExpandedNodeId *targets =
  437. (UA_ExpandedNodeId*) UA_realloc(refs->targetIds,
  438. sizeof(UA_ExpandedNodeId) * (refs->targetIdsSize+1));
  439. if(!targets)
  440. return UA_STATUSCODE_BADOUTOFMEMORY;
  441. refs->targetIds = targets;
  442. UA_StatusCode retval =
  443. UA_ExpandedNodeId_copy(target, &refs->targetIds[refs->targetIdsSize]);
  444. if(retval == UA_STATUSCODE_GOOD) {
  445. refs->targetIdsSize++;
  446. } else if(refs->targetIdsSize == 0) {
  447. /* We had zero references before (realloc was a malloc) */
  448. UA_free(refs->targetIds);
  449. refs->targetIds = NULL;
  450. }
  451. return retval;
  452. }
  453. static UA_StatusCode
  454. addReferenceKind(UA_Node *node, const UA_AddReferencesItem *item) {
  455. UA_NodeReferenceKind *refs =
  456. (UA_NodeReferenceKind*)UA_realloc(node->references,
  457. sizeof(UA_NodeReferenceKind) * (node->referencesSize+1));
  458. if(!refs)
  459. return UA_STATUSCODE_BADOUTOFMEMORY;
  460. node->references = refs;
  461. UA_NodeReferenceKind *newRef = &refs[node->referencesSize];
  462. memset(newRef, 0, sizeof(UA_NodeReferenceKind));
  463. newRef->isInverse = !item->isForward;
  464. UA_StatusCode retval = UA_NodeId_copy(&item->referenceTypeId, &newRef->referenceTypeId);
  465. retval |= addReferenceTarget(newRef, &item->targetNodeId);
  466. if(retval == UA_STATUSCODE_GOOD) {
  467. node->referencesSize++;
  468. } else {
  469. UA_NodeId_deleteMembers(&newRef->referenceTypeId);
  470. if(node->referencesSize == 0) {
  471. UA_free(node->references);
  472. node->references = NULL;
  473. }
  474. }
  475. return retval;
  476. }
  477. UA_StatusCode
  478. UA_Node_addReference(UA_Node *node, const UA_AddReferencesItem *item) {
  479. UA_NodeReferenceKind *existingRefs = NULL;
  480. for(size_t i = 0; i < node->referencesSize; ++i) {
  481. UA_NodeReferenceKind *refs = &node->references[i];
  482. if(refs->isInverse != item->isForward
  483. && UA_NodeId_equal(&refs->referenceTypeId, &item->referenceTypeId)) {
  484. existingRefs = refs;
  485. break;
  486. }
  487. }
  488. if(existingRefs != NULL) {
  489. for(size_t i = 0; i < existingRefs->targetIdsSize; i++) {
  490. if(UA_ExpandedNodeId_equal(&existingRefs->targetIds[i],
  491. &item->targetNodeId)) {
  492. return UA_STATUSCODE_BADDUPLICATEREFERENCENOTALLOWED;
  493. }
  494. }
  495. return addReferenceTarget(existingRefs, &item->targetNodeId);
  496. }
  497. return addReferenceKind(node, item);
  498. }
  499. UA_StatusCode
  500. UA_Node_deleteReference(UA_Node *node, const UA_DeleteReferencesItem *item) {
  501. for(size_t i = node->referencesSize; i > 0; --i) {
  502. UA_NodeReferenceKind *refs = &node->references[i-1];
  503. if(item->isForward == refs->isInverse)
  504. continue;
  505. if(!UA_NodeId_equal(&item->referenceTypeId, &refs->referenceTypeId))
  506. continue;
  507. for(size_t j = refs->targetIdsSize; j > 0; --j) {
  508. UA_ExpandedNodeId *target = &refs->targetIds[j-1];
  509. if(!UA_NodeId_equal(&item->targetNodeId.nodeId, &target->nodeId))
  510. continue;
  511. /* Ok, delete the reference */
  512. UA_ExpandedNodeId_deleteMembers(target);
  513. refs->targetIdsSize--;
  514. /* One matching target remaining */
  515. if(refs->targetIdsSize > 0) {
  516. if(j-1 != refs->targetIdsSize) // avoid valgrind error: Source
  517. // and destination overlap in
  518. // memcpy
  519. *target = refs->targetIds[refs->targetIdsSize];
  520. return UA_STATUSCODE_GOOD;
  521. }
  522. /* No target for the ReferenceType remaining. Remove entry. */
  523. UA_free(refs->targetIds);
  524. UA_NodeId_deleteMembers(&refs->referenceTypeId);
  525. node->referencesSize--;
  526. if(node->referencesSize > 0) {
  527. if(i-1 != node->referencesSize) // avoid valgrind error: Source
  528. // and destination overlap in
  529. // memcpy
  530. node->references[i-1] = node->references[node->referencesSize];
  531. return UA_STATUSCODE_GOOD;
  532. }
  533. /* No remaining references of any ReferenceType */
  534. UA_free(node->references);
  535. node->references = NULL;
  536. return UA_STATUSCODE_GOOD;
  537. }
  538. }
  539. return UA_STATUSCODE_UNCERTAINREFERENCENOTDELETED;
  540. }
  541. void
  542. UA_Node_deleteReferencesSubset(UA_Node *node, size_t referencesSkipSize,
  543. UA_NodeId* referencesSkip) {
  544. /* Nothing to do */
  545. if(node->referencesSize == 0 || node->references == NULL)
  546. return;
  547. for(size_t i = node->referencesSize; i > 0; --i) {
  548. UA_NodeReferenceKind *refs = &node->references[i-1];
  549. /* Shall we keep the references of this type? */
  550. UA_Boolean skip = false;
  551. for(size_t j = 0; j < referencesSkipSize; j++) {
  552. if(UA_NodeId_equal(&refs->referenceTypeId, &referencesSkip[j])) {
  553. skip = true;
  554. break;
  555. }
  556. }
  557. if(skip)
  558. continue;
  559. /* Remove references */
  560. UA_Array_delete(refs->targetIds, refs->targetIdsSize, &UA_TYPES[UA_TYPES_EXPANDEDNODEID]);
  561. UA_NodeId_deleteMembers(&refs->referenceTypeId);
  562. node->referencesSize--;
  563. /* Move last references-kind entry to this position */
  564. if(i-1 == node->referencesSize) /* Don't memcpy over the same position */
  565. continue;
  566. node->references[i-1] = node->references[node->referencesSize];
  567. }
  568. if(node->referencesSize == 0) {
  569. /* The array is empty. Remove. */
  570. UA_free(node->references);
  571. node->references = NULL;
  572. } else {
  573. /* Realloc to save memory */
  574. UA_NodeReferenceKind *refs = (UA_NodeReferenceKind*)
  575. UA_realloc(node->references, sizeof(UA_NodeReferenceKind) * node->referencesSize);
  576. if(refs) /* Do nothing if realloc fails */
  577. node->references = refs;
  578. }
  579. }
  580. void UA_Node_deleteReferences(UA_Node *node) {
  581. UA_Node_deleteReferencesSubset(node, 0, NULL);
  582. }