ua_server_addressspace.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include "ua_server.h"
  2. #include "ua_server_internal.h"
  3. #define UA_SERVER_DELETENODEALIAS(TYPE) \
  4. UA_StatusCode UA_Server_delete##TYPE##Node(UA_Server *server, UA_NodeId nodeId) { \
  5. return UA_Server_deleteNode(server, nodeId); \
  6. }
  7. UA_StatusCode
  8. UA_Server_deleteNode(UA_Server *server, UA_NodeId nodeId) {
  9. union nodeUnion {
  10. const UA_Node *delNodeConst;
  11. UA_Node *delNode;
  12. } ptrs;
  13. ptrs.delNodeConst = UA_NodeStore_get(server->nodestore, &nodeId);
  14. if (!ptrs.delNodeConst)
  15. return UA_STATUSCODE_BADNODEIDINVALID;
  16. UA_NodeStore_release(ptrs.delNodeConst);
  17. // Remove the node from the hashmap/slot
  18. UA_NodeStore_remove(server->nodestore, &nodeId);
  19. /* Note: this is done by deleteEntry() in _remove()!
  20. * FIXME: Delete unreachable child nodes???
  21. switch(ptrs.delNode->nodeClass) {
  22. case UA_NODECLASS_DATATYPE:
  23. UA_DataTypeNode_delete((UA_DataTypeNode *) ptrs.delNode);
  24. break;
  25. case UA_NODECLASS_METHOD:
  26. UA_MethodNode_delete((UA_MethodNode *) ptrs.delNode);
  27. break;
  28. case UA_NODECLASS_OBJECT:
  29. UA_ObjectNode_delete((UA_ObjectNode *) ptrs.delNode);
  30. break;
  31. case UA_NODECLASS_OBJECTTYPE:
  32. UA_ObjectTypeNode_delete((UA_ObjectTypeNode *) ptrs.delNode);
  33. break;
  34. case UA_NODECLASS_REFERENCETYPE:
  35. UA_ReferenceTypeNode_delete((UA_ReferenceTypeNode *) ptrs.delNode);
  36. break;
  37. case UA_NODECLASS_VARIABLE:
  38. UA_VariableNode_delete((UA_VariableNode *) ptrs.delNode);
  39. break;
  40. case UA_NODECLASS_VARIABLETYPE:
  41. UA_VariableTypeNode_delete((UA_VariableTypeNode *) ptrs.delNode);
  42. break;
  43. case UA_NODECLASS_VIEW:
  44. UA_ViewNode_delete((UA_ViewNode *) ptrs.delNode);
  45. break;
  46. default:
  47. break;
  48. } */
  49. return UA_STATUSCODE_GOOD;
  50. }
  51. UA_SERVER_DELETENODEALIAS(Object)
  52. UA_SERVER_DELETENODEALIAS(Variable)
  53. #ifdef ENABLE_METHODCALLS
  54. UA_SERVER_DELETENODEALIAS(Method)
  55. #endif
  56. UA_StatusCode
  57. UA_Server_addVariableNode(UA_Server *server, UA_Variant *value, const UA_QualifiedName browseName,
  58. UA_NodeId nodeId, const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId,
  59. UA_NodeId *createdNodeId) {
  60. UA_VariableNode *node = UA_VariableNode_new();
  61. node->value.variant = *value; // copy content
  62. UA_NodeId_copy(&nodeId, &node->nodeId);
  63. UA_QualifiedName_copy(&browseName, &node->browseName);
  64. UA_String_copy(&browseName.name, &node->displayName.text);
  65. UA_ExpandedNodeId parentId;
  66. UA_ExpandedNodeId_init(&parentId);
  67. UA_NodeId_copy(&parentNodeId, &parentId.nodeId);
  68. UA_AddNodesResult res = UA_Server_addNodeWithSession(server, &adminSession, (UA_Node*)node,
  69. parentId, referenceTypeId);
  70. UA_Server_addReference(server, res.addedNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION),
  71. UA_EXPANDEDNODEID_NUMERIC(0, value->type->typeId.identifier.numeric));
  72. if(res.statusCode != UA_STATUSCODE_GOOD) {
  73. UA_Variant_init(&node->value.variant);
  74. UA_VariableNode_delete(node);
  75. } else
  76. UA_free(value);
  77. if (createdNodeId != UA_NULL)
  78. UA_NodeId_copy(&res.addedNodeId, createdNodeId);
  79. UA_AddNodesResult_deleteMembers(&res);
  80. return res.statusCode;
  81. }
  82. UA_StatusCode
  83. UA_Server_addObjectNode(UA_Server *server, const UA_QualifiedName browseName, UA_NodeId nodeId,
  84. const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId,
  85. const UA_NodeId typeDefinition,
  86. UA_NodeId *createdNodeId) {
  87. UA_ObjectNode *node = UA_ObjectNode_new();
  88. UA_NodeId_copy(&nodeId, &node->nodeId);
  89. UA_QualifiedName_copy(&browseName, &node->browseName);
  90. UA_String_copy(&browseName.name, &node->displayName.text);
  91. UA_ExpandedNodeId parentId; // we need an expandednodeid
  92. UA_ExpandedNodeId_init(&parentId);
  93. UA_NodeId_copy(&parentNodeId, &parentId.nodeId);
  94. UA_AddNodesResult res = UA_Server_addNodeWithSession(server, &adminSession, (UA_Node*)node,
  95. parentId, referenceTypeId);
  96. if(res.statusCode != UA_STATUSCODE_GOOD)
  97. UA_ObjectNode_delete(node);
  98. if (createdNodeId != UA_NULL)
  99. UA_NodeId_copy(&res.addedNodeId, createdNodeId);
  100. UA_AddNodesResult_deleteMembers(&res);
  101. if(!UA_NodeId_isNull(&typeDefinition)) {
  102. UA_ExpandedNodeId typeDefid; // we need an expandednodeid
  103. UA_ExpandedNodeId_init(&typeDefid);
  104. UA_NodeId_copy(&typeDefinition, &typeDefid.nodeId);
  105. UA_Server_addReference(server, res.addedNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION),
  106. typeDefid);
  107. }
  108. return res.statusCode;
  109. }
  110. UA_StatusCode
  111. UA_Server_addDataSourceVariableNode(UA_Server *server, UA_DataSource dataSource,
  112. const UA_QualifiedName browseName, UA_NodeId nodeId,
  113. const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId,
  114. UA_NodeId *createdNodeId) {
  115. UA_VariableNode *node = UA_VariableNode_new();
  116. node->valueSource = UA_VALUESOURCE_DATASOURCE;
  117. node->value.dataSource = dataSource;
  118. UA_NodeId_copy(&nodeId, &node->nodeId);
  119. UA_QualifiedName_copy(&browseName, &node->browseName);
  120. UA_String_copy(&browseName.name, &node->displayName.text);
  121. UA_ExpandedNodeId parentId; // dummy exapndednodeid
  122. UA_ExpandedNodeId_init(&parentId);
  123. UA_NodeId_copy(&parentNodeId, &parentId.nodeId);
  124. UA_AddNodesResult res = UA_Server_addNodeWithSession(server, &adminSession, (UA_Node*)node,
  125. parentId, referenceTypeId);
  126. UA_Server_addReference(server, res.addedNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION),
  127. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE));
  128. if(res.statusCode != UA_STATUSCODE_GOOD)
  129. UA_VariableNode_delete(node);
  130. if (createdNodeId != UA_NULL)
  131. UA_NodeId_copy(&res.addedNodeId, createdNodeId);
  132. UA_AddNodesResult_deleteMembers(&res);
  133. return res.statusCode;
  134. }
  135. /* Userspace Version of addOneWayReferenceWithSession*/
  136. UA_StatusCode
  137. UA_Server_AddMonodirectionalReference(UA_Server *server, UA_NodeId sourceNodeId, UA_ExpandedNodeId targetNodeId,
  138. UA_NodeId referenceTypeId, UA_Boolean isforward) {
  139. UA_AddReferencesItem ref;
  140. UA_AddReferencesItem_init(&ref);
  141. UA_StatusCode retval = UA_NodeId_copy(&sourceNodeId, &ref.sourceNodeId);
  142. retval |= UA_ExpandedNodeId_copy(&targetNodeId, &ref.targetNodeId);
  143. retval |= UA_NodeId_copy(&referenceTypeId, &ref.referenceTypeId);
  144. if(retval != UA_STATUSCODE_GOOD)
  145. goto cleanup;
  146. const UA_Node *target = UA_NodeStore_get(server->nodestore, &ref.targetNodeId.nodeId);
  147. if(!target) {
  148. retval = UA_STATUSCODE_BADNODEIDINVALID;
  149. goto cleanup;
  150. }
  151. if(isforward == UA_TRUE)
  152. ref.isForward = UA_TRUE;
  153. ref.targetNodeClass = target->nodeClass;
  154. UA_NodeStore_release(target);
  155. retval = addOneWayReferenceWithSession(server, (UA_Session *) UA_NULL, &ref);
  156. cleanup:
  157. UA_AddReferencesItem_deleteMembers(&ref);
  158. return retval;
  159. }
  160. /* Adds a one-way reference to the local nodestore */
  161. UA_StatusCode
  162. addOneWayReferenceWithSession(UA_Server *server, UA_Session *session, const UA_AddReferencesItem *item) {
  163. const UA_Node *node = UA_NodeStore_get(server->nodestore, &item->sourceNodeId);
  164. if(!node)
  165. return UA_STATUSCODE_BADINTERNALERROR;
  166. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  167. #ifndef UA_MULTITHREADING
  168. size_t i = node->referencesSize;
  169. if(node->referencesSize < 0)
  170. i = 0;
  171. size_t refssize = (i+1) | 3; // so the realloc is not necessary every time
  172. UA_ReferenceNode *new_refs = UA_realloc(node->references, sizeof(UA_ReferenceNode) * refssize);
  173. if(!new_refs)
  174. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  175. else {
  176. UA_ReferenceNode_init(&new_refs[i]);
  177. retval = UA_NodeId_copy(&item->referenceTypeId, &new_refs[i].referenceTypeId);
  178. new_refs[i].isInverse = !item->isForward;
  179. retval |= UA_ExpandedNodeId_copy(&item->targetNodeId, &new_refs[i].targetId);
  180. /* hack. be careful! possible only in the single-threaded case. */
  181. UA_Node *mutable_node = (UA_Node*)(uintptr_t)node;
  182. mutable_node->references = new_refs;
  183. if(retval != UA_STATUSCODE_GOOD) {
  184. UA_NodeId_deleteMembers(&new_refs[node->referencesSize].referenceTypeId);
  185. UA_ExpandedNodeId_deleteMembers(&new_refs[node->referencesSize].targetId);
  186. } else
  187. mutable_node->referencesSize = i+1;
  188. }
  189. UA_NodeStore_release(node);
  190. return retval;
  191. #else
  192. UA_Node *newNode = UA_NULL;
  193. void (*deleteNode)(UA_Node*) = UA_NULL;
  194. switch(node->nodeClass) {
  195. case UA_NODECLASS_OBJECT:
  196. newNode = (UA_Node*)UA_ObjectNode_new();
  197. UA_ObjectNode_copy((const UA_ObjectNode*)node, (UA_ObjectNode*)newNode);
  198. deleteNode = (void (*)(UA_Node*))UA_ObjectNode_delete;
  199. break;
  200. case UA_NODECLASS_VARIABLE:
  201. newNode = (UA_Node*)UA_VariableNode_new();
  202. UA_VariableNode_copy((const UA_VariableNode*)node, (UA_VariableNode*)newNode);
  203. deleteNode = (void (*)(UA_Node*))UA_VariableNode_delete;
  204. break;
  205. case UA_NODECLASS_METHOD:
  206. newNode = (UA_Node*)UA_MethodNode_new();
  207. UA_MethodNode_copy((const UA_MethodNode*)node, (UA_MethodNode*)newNode);
  208. deleteNode = (void (*)(UA_Node*))UA_MethodNode_delete;
  209. break;
  210. case UA_NODECLASS_OBJECTTYPE:
  211. newNode = (UA_Node*)UA_ObjectTypeNode_new();
  212. UA_ObjectTypeNode_copy((const UA_ObjectTypeNode*)node, (UA_ObjectTypeNode*)newNode);
  213. deleteNode = (void (*)(UA_Node*))UA_ObjectTypeNode_delete;
  214. break;
  215. case UA_NODECLASS_VARIABLETYPE:
  216. newNode = (UA_Node*)UA_VariableTypeNode_new();
  217. UA_VariableTypeNode_copy((const UA_VariableTypeNode*)node, (UA_VariableTypeNode*)newNode);
  218. deleteNode = (void (*)(UA_Node*))UA_VariableTypeNode_delete;
  219. break;
  220. case UA_NODECLASS_REFERENCETYPE:
  221. newNode = (UA_Node*)UA_ReferenceTypeNode_new();
  222. UA_ReferenceTypeNode_copy((const UA_ReferenceTypeNode*)node, (UA_ReferenceTypeNode*)newNode);
  223. deleteNode = (void (*)(UA_Node*))UA_ReferenceTypeNode_delete;
  224. break;
  225. case UA_NODECLASS_DATATYPE:
  226. newNode = (UA_Node*)UA_DataTypeNode_new();
  227. UA_DataTypeNode_copy((const UA_DataTypeNode*)node, (UA_DataTypeNode*)newNode);
  228. deleteNode = (void (*)(UA_Node*))UA_DataTypeNode_delete;
  229. break;
  230. case UA_NODECLASS_VIEW:
  231. newNode = (UA_Node*)UA_ViewNode_new();
  232. UA_ViewNode_copy((const UA_ViewNode*)node, (UA_ViewNode*)newNode);
  233. deleteNode = (void (*)(UA_Node*))UA_ViewNode_delete;
  234. break;
  235. default:
  236. return UA_STATUSCODE_BADINTERNALERROR;
  237. }
  238. UA_Int32 count = node->referencesSize;
  239. if(count < 0)
  240. count = 0;
  241. UA_ReferenceNode *old_refs = newNode->references;
  242. UA_ReferenceNode *new_refs = UA_malloc(sizeof(UA_ReferenceNode)*(count+1));
  243. if(!new_refs) {
  244. deleteNode(newNode);
  245. UA_NodeStore_release(node);
  246. return UA_STATUSCODE_BADOUTOFMEMORY;
  247. }
  248. // insert the new reference
  249. UA_memcpy(new_refs, old_refs, sizeof(UA_ReferenceNode)*count);
  250. UA_ReferenceNode_init(&new_refs[count]);
  251. retval = UA_NodeId_copy(&item->referenceTypeId, &new_refs[count].referenceTypeId);
  252. new_refs[count].isInverse = !item->isForward;
  253. retval |= UA_ExpandedNodeId_copy(&item->targetNodeId, &new_refs[count].targetId);
  254. if(retval != UA_STATUSCODE_GOOD) {
  255. UA_Array_delete(new_refs, &UA_TYPES[UA_TYPES_REFERENCENODE], ++count);
  256. newNode->references = UA_NULL;
  257. newNode->referencesSize = 0;
  258. deleteNode(newNode);
  259. UA_NodeStore_release(node);
  260. return UA_STATUSCODE_BADOUTOFMEMORY;
  261. }
  262. UA_free(old_refs);
  263. newNode->references = new_refs;
  264. newNode->referencesSize = ++count;
  265. retval = UA_NodeStore_replace(server->nodestore, node, newNode, UA_NULL);
  266. UA_NodeStore_release(node);
  267. if(retval == UA_STATUSCODE_BADINTERNALERROR) {
  268. /* presumably because the node was replaced and an old version was updated at the same time.
  269. just try again */
  270. deleteNode(newNode);
  271. return addOneWayReferenceWithSession(server, session, item);
  272. }
  273. return retval;
  274. #endif
  275. }
  276. /* userland version of addReferenceWithSession */
  277. UA_StatusCode UA_Server_addReference(UA_Server *server, const UA_NodeId sourceId, const UA_NodeId refTypeId,
  278. const UA_ExpandedNodeId targetId) {
  279. UA_AddReferencesItem item;
  280. UA_AddReferencesItem_init(&item);
  281. item.sourceNodeId = sourceId;
  282. item.referenceTypeId = refTypeId;
  283. item.isForward = UA_TRUE;
  284. item.targetNodeId = targetId;
  285. return UA_Server_addReferenceWithSession(server, &adminSession, &item);
  286. }
  287. UA_StatusCode UA_Server_addReferenceWithSession(UA_Server *server, UA_Session *session,
  288. const UA_AddReferencesItem *item) {
  289. if(item->targetServerUri.length > 0)
  290. return UA_STATUSCODE_BADNOTIMPLEMENTED; // currently no expandednodeids are allowed
  291. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  292. #ifdef UA_EXTERNAL_NAMESPACES
  293. UA_ExternalNodeStore *ensFirst = UA_NULL;
  294. UA_ExternalNodeStore *ensSecond = UA_NULL;
  295. for(size_t j = 0;j<server->externalNamespacesSize && (!ensFirst || !ensSecond);j++) {
  296. if(item->sourceNodeId.namespaceIndex == server->externalNamespaces[j].index)
  297. ensFirst = &server->externalNamespaces[j].externalNodeStore;
  298. if(item->targetNodeId.nodeId.namespaceIndex == server->externalNamespaces[j].index)
  299. ensSecond = &server->externalNamespaces[j].externalNodeStore;
  300. }
  301. if(ensFirst) {
  302. // todo: use external nodestore
  303. } else
  304. #endif
  305. retval = addOneWayReferenceWithSession(server, session, item);
  306. if(retval)
  307. return retval;
  308. UA_AddReferencesItem secondItem;
  309. secondItem = *item;
  310. secondItem.targetNodeId.nodeId = item->sourceNodeId;
  311. secondItem.sourceNodeId = item->targetNodeId.nodeId;
  312. secondItem.isForward = !item->isForward;
  313. #ifdef UA_EXTERNAL_NAMESPACES
  314. if(ensSecond) {
  315. // todo: use external nodestore
  316. } else
  317. #endif
  318. retval = addOneWayReferenceWithSession (server, session, &secondItem);
  319. // todo: remove reference if the second direction failed
  320. return retval;
  321. }
  322. /* userland version of addNodeWithSession */
  323. UA_AddNodesResult
  324. UA_Server_addNode(UA_Server *server, UA_Node *node, const UA_ExpandedNodeId parentNodeId,
  325. const UA_NodeId referenceTypeId) {
  326. return UA_Server_addNodeWithSession(server, &adminSession, node, parentNodeId, referenceTypeId);
  327. }
  328. UA_AddNodesResult
  329. UA_Server_addNodeWithSession(UA_Server *server, UA_Session *session, UA_Node *node,
  330. const UA_ExpandedNodeId parentNodeId, const UA_NodeId referenceTypeId) {
  331. UA_AddNodesResult result;
  332. UA_AddNodesResult_init(&result);
  333. if(node->nodeId.namespaceIndex >= server->namespacesSize) {
  334. result.statusCode = UA_STATUSCODE_BADNODEIDINVALID;
  335. return result;
  336. }
  337. const UA_Node *parent = UA_NodeStore_get(server->nodestore, &parentNodeId.nodeId);
  338. if(!parent) {
  339. result.statusCode = UA_STATUSCODE_BADPARENTNODEIDINVALID;
  340. return result;
  341. }
  342. const UA_ReferenceTypeNode *referenceType =
  343. (const UA_ReferenceTypeNode *)UA_NodeStore_get(server->nodestore, &referenceTypeId);
  344. if(!referenceType) {
  345. result.statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  346. goto ret;
  347. }
  348. if(referenceType->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  349. result.statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  350. goto ret2;
  351. }
  352. if(referenceType->isAbstract == UA_TRUE) {
  353. result.statusCode = UA_STATUSCODE_BADREFERENCENOTALLOWED;
  354. goto ret2;
  355. }
  356. // todo: test if the referencetype is hierarchical
  357. //FIXME: a bit dirty workaround of preserving namespace
  358. //namespace index is assumed to be valid
  359. const UA_Node *managed = UA_NULL;
  360. UA_NodeId tempNodeid;
  361. UA_NodeId_init(&tempNodeid);
  362. UA_NodeId_copy(&node->nodeId, &tempNodeid);
  363. tempNodeid.namespaceIndex = 0;
  364. if(UA_NodeId_isNull(&tempNodeid)) {
  365. if(UA_NodeStore_insert(server->nodestore, node, &managed) != UA_STATUSCODE_GOOD) {
  366. result.statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  367. goto ret2;
  368. }
  369. result.addedNodeId = managed->nodeId; // cannot fail as unique nodeids are numeric
  370. } else {
  371. if(UA_NodeId_copy(&node->nodeId, &result.addedNodeId) != UA_STATUSCODE_GOOD) {
  372. result.statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  373. goto ret2;
  374. }
  375. if(UA_NodeStore_insert(server->nodestore, node, &managed) != UA_STATUSCODE_GOOD) {
  376. result.statusCode = UA_STATUSCODE_BADNODEIDEXISTS; // todo: differentiate out of memory
  377. UA_NodeId_deleteMembers(&result.addedNodeId);
  378. goto ret2;
  379. }
  380. }
  381. // reference back to the parent
  382. UA_AddReferencesItem item;
  383. UA_AddReferencesItem_init(&item);
  384. item.sourceNodeId = managed->nodeId;
  385. item.referenceTypeId = referenceType->nodeId;
  386. item.isForward = UA_FALSE;
  387. item.targetNodeId.nodeId = parent->nodeId;
  388. UA_Server_addReferenceWithSession(server, session, &item);
  389. // todo: error handling. remove new node from nodestore
  390. UA_NodeStore_release(managed);
  391. ret2:
  392. UA_NodeId_deleteMembers(&tempNodeid);
  393. UA_NodeStore_release((const UA_Node*)referenceType);
  394. ret:
  395. UA_NodeStore_release(parent);
  396. return result;
  397. }
  398. #ifdef ENABLE_METHODCALLS
  399. UA_StatusCode
  400. UA_Server_addMethodNode(UA_Server *server, const UA_QualifiedName browseName, UA_NodeId nodeId,
  401. const UA_ExpandedNodeId parentNodeId, const UA_NodeId referenceTypeId,
  402. UA_MethodCallback method, UA_Int32 inputArgumentsSize,
  403. const UA_Argument *inputArguments, UA_Int32 outputArgumentsSize,
  404. const UA_Argument *outputArguments,
  405. UA_NodeId *createdNodeId) {
  406. UA_MethodNode *newMethod = UA_MethodNode_new();
  407. UA_NodeId_copy(&nodeId, &newMethod->nodeId);
  408. UA_QualifiedName_copy(&browseName, &newMethod->browseName);
  409. UA_String_copy(&browseName.name, &newMethod->displayName.text);
  410. newMethod->attachedMethod = method;
  411. newMethod->executable = UA_TRUE;
  412. newMethod->userExecutable = UA_TRUE;
  413. UA_AddNodesResult addRes = UA_Server_addNode(server, (UA_Node*)newMethod, parentNodeId, referenceTypeId);
  414. if(addRes.statusCode != UA_STATUSCODE_GOOD) {
  415. UA_MethodNode_delete(newMethod);
  416. return addRes.statusCode;
  417. }
  418. UA_ExpandedNodeId methodExpandedNodeId;
  419. UA_ExpandedNodeId_init(&methodExpandedNodeId);
  420. UA_NodeId_copy(&addRes.addedNodeId, &methodExpandedNodeId.nodeId);
  421. if (createdNodeId != UA_NULL)
  422. UA_NodeId_copy(&addRes.addedNodeId, createdNodeId);
  423. /* create InputArguments */
  424. UA_NodeId argId = UA_NODEID_NUMERIC(nodeId.namespaceIndex, 0);
  425. UA_VariableNode *inputArgumentsVariableNode = UA_VariableNode_new();
  426. UA_StatusCode retval = UA_NodeId_copy(&argId, &inputArgumentsVariableNode->nodeId);
  427. inputArgumentsVariableNode->browseName = UA_QUALIFIEDNAME_ALLOC(0,"InputArguments");
  428. inputArgumentsVariableNode->displayName = UA_LOCALIZEDTEXT_ALLOC("en_US", "InputArguments");
  429. inputArgumentsVariableNode->description = UA_LOCALIZEDTEXT_ALLOC("en_US", "InputArguments");
  430. inputArgumentsVariableNode->valueRank = 1;
  431. UA_Variant_setArrayCopy(&inputArgumentsVariableNode->value.variant, inputArguments,
  432. inputArgumentsSize, &UA_TYPES[UA_TYPES_ARGUMENT]);
  433. addRes = UA_Server_addNode(server, (UA_Node*)inputArgumentsVariableNode, methodExpandedNodeId,
  434. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY));
  435. if(addRes.statusCode != UA_STATUSCODE_GOOD) {
  436. UA_ExpandedNodeId_deleteMembers(&methodExpandedNodeId);
  437. // TODO Remove node
  438. return addRes.statusCode;
  439. }
  440. if (createdNodeId != UA_NULL)
  441. UA_NodeId_copy(&addRes.addedNodeId, createdNodeId);
  442. /* create OutputArguments */
  443. argId = UA_NODEID_NUMERIC(nodeId.namespaceIndex, 0);
  444. UA_VariableNode *outputArgumentsVariableNode = UA_VariableNode_new();
  445. retval |= UA_NodeId_copy(&argId, &outputArgumentsVariableNode->nodeId);
  446. outputArgumentsVariableNode->browseName = UA_QUALIFIEDNAME_ALLOC(0,"OutputArguments");
  447. outputArgumentsVariableNode->displayName = UA_LOCALIZEDTEXT_ALLOC("en_US", "OutputArguments");
  448. outputArgumentsVariableNode->description = UA_LOCALIZEDTEXT_ALLOC("en_US", "OutputArguments");
  449. outputArgumentsVariableNode->valueRank = 1;
  450. UA_Variant_setArrayCopy(&outputArgumentsVariableNode->value.variant, outputArguments,
  451. outputArgumentsSize, &UA_TYPES[UA_TYPES_ARGUMENT]);
  452. addRes = UA_Server_addNode(server, (UA_Node*)outputArgumentsVariableNode, methodExpandedNodeId,
  453. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY));
  454. UA_ExpandedNodeId_deleteMembers(&methodExpandedNodeId);
  455. if(addRes.statusCode != UA_STATUSCODE_GOOD)
  456. // TODO Remove node
  457. retval = addRes.statusCode;
  458. return retval;
  459. }
  460. #endif