ua_server_addressspace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "ua_server.h"
  2. #include "ua_server_internal.h"
  3. UA_StatusCode
  4. UA_Server_addVariableNode(UA_Server *server, UA_Variant *value, const UA_QualifiedName browseName,
  5. UA_NodeId nodeId, const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId)
  6. {
  7. UA_VariableNode *node = UA_VariableNode_new();
  8. node->value.variant = *value; // copy content
  9. UA_NodeId_copy(&nodeId, &node->nodeId);
  10. UA_QualifiedName_copy(&browseName, &node->browseName);
  11. UA_String_copy(&browseName.name, &node->displayName.text);
  12. UA_ExpandedNodeId parentId; // we need an expandednodeid
  13. UA_ExpandedNodeId_init(&parentId);
  14. UA_NodeId_copy(&parentNodeId, &parentId.nodeId);
  15. UA_AddNodesResult res =
  16. UA_Server_addNodeWithSession(server, &adminSession, (UA_Node*)node, parentId, referenceTypeId);
  17. ADDREFERENCE(res.addedNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION),
  18. UA_EXPANDEDNODEID_NUMERIC(0, value->type->typeId.identifier.numeric));
  19. if(res.statusCode != UA_STATUSCODE_GOOD) {
  20. UA_Variant_init(&node->value.variant);
  21. UA_VariableNode_delete(node);
  22. } else
  23. UA_free(value);
  24. UA_AddNodesResult_deleteMembers(&res);
  25. return res.statusCode;
  26. }
  27. UA_StatusCode
  28. UA_Server_addObjectNode(UA_Server *server, const UA_QualifiedName browseName,
  29. UA_NodeId nodeId, const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId, const UA_NodeId typeDefinition)
  30. {
  31. UA_ObjectNode *node = UA_ObjectNode_new();
  32. UA_NodeId_copy(&nodeId, &node->nodeId);
  33. UA_QualifiedName_copy(&browseName, &node->browseName);
  34. UA_String_copy(&browseName.name, &node->displayName.text);
  35. UA_ExpandedNodeId parentId; // we need an expandednodeid
  36. UA_ExpandedNodeId_init(&parentId);
  37. UA_NodeId_copy(&parentNodeId, &parentId.nodeId);
  38. UA_AddNodesResult res =
  39. UA_Server_addNodeWithSession(server, &adminSession, (UA_Node*)node, parentId, referenceTypeId);
  40. if(res.statusCode != UA_STATUSCODE_GOOD) {
  41. UA_ObjectNode_delete(node);
  42. }
  43. UA_AddNodesResult_deleteMembers(&res);
  44. if(!(UA_NodeId_isNull(&typeDefinition))){
  45. UA_ExpandedNodeId typeDefid; // we need an expandednodeid
  46. UA_ExpandedNodeId_init(&typeDefid);
  47. UA_NodeId_copy(&typeDefinition, &typeDefid.nodeId);
  48. ADDREFERENCE(res.addedNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION), typeDefid);
  49. }
  50. return res.statusCode;
  51. }
  52. UA_StatusCode
  53. UA_Server_addDataSourceVariableNode(UA_Server *server, UA_DataSource dataSource,
  54. const UA_QualifiedName browseName, UA_NodeId nodeId,
  55. const UA_NodeId parentNodeId, const UA_NodeId referenceTypeId)
  56. {
  57. UA_VariableNode *node = UA_VariableNode_new();
  58. node->valueSource = UA_VALUESOURCE_DATASOURCE;
  59. node->value.dataSource = dataSource;
  60. UA_NodeId_copy(&nodeId, &node->nodeId);
  61. UA_QualifiedName_copy(&browseName, &node->browseName);
  62. UA_String_copy(&browseName.name, &node->displayName.text);
  63. UA_ExpandedNodeId parentId; // dummy exapndednodeid
  64. UA_ExpandedNodeId_init(&parentId);
  65. UA_NodeId_copy(&parentNodeId, &parentId.nodeId);
  66. UA_AddNodesResult res =
  67. UA_Server_addNodeWithSession(server, &adminSession, (UA_Node*)node, parentId, referenceTypeId);
  68. ADDREFERENCE(res.addedNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION),
  69. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE));
  70. if(res.statusCode != UA_STATUSCODE_GOOD)
  71. UA_VariableNode_delete(node);
  72. UA_AddNodesResult_deleteMembers(&res);
  73. return res.statusCode;
  74. }
  75. /* Adds a one-way reference to the local nodestore */
  76. static UA_StatusCode
  77. addOneWayReferenceWithSession(UA_Server *server, UA_Session *session, const UA_AddReferencesItem *item) {
  78. const UA_Node *node = UA_NodeStore_get(server->nodestore, &item->sourceNodeId);
  79. if(!node)
  80. return UA_STATUSCODE_BADINTERNALERROR;
  81. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  82. #ifndef UA_MULTITHREADING
  83. size_t i = node->referencesSize;
  84. if(node->referencesSize < 0)
  85. i = 0;
  86. size_t refssize = (i+1) | 3; // so the realloc is not necessary every time
  87. UA_ReferenceNode *new_refs = UA_realloc(node->references, sizeof(UA_ReferenceNode) * refssize);
  88. if(!new_refs)
  89. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  90. else {
  91. UA_ReferenceNode_init(&new_refs[i]);
  92. retval = UA_NodeId_copy(&item->referenceTypeId, &new_refs[i].referenceTypeId);
  93. new_refs[i].isInverse = !item->isForward;
  94. retval |= UA_ExpandedNodeId_copy(&item->targetNodeId, &new_refs[i].targetId);
  95. /* hack. be careful! possible only in the single-threaded case. */
  96. UA_Node *mutable_node = (UA_Node*)(uintptr_t)node;
  97. mutable_node->references = new_refs;
  98. if(retval != UA_STATUSCODE_GOOD) {
  99. UA_NodeId_deleteMembers(&new_refs[node->referencesSize].referenceTypeId);
  100. UA_ExpandedNodeId_deleteMembers(&new_refs[node->referencesSize].targetId);
  101. } else
  102. mutable_node->referencesSize = i+1;
  103. }
  104. UA_NodeStore_release(node);
  105. return retval;
  106. #else
  107. UA_Node *newNode = UA_NULL;
  108. void (*deleteNode)(UA_Node*) = UA_NULL;
  109. switch(node->nodeClass) {
  110. case UA_NODECLASS_OBJECT:
  111. newNode = (UA_Node*)UA_ObjectNode_new();
  112. UA_ObjectNode_copy((const UA_ObjectNode*)node, (UA_ObjectNode*)newNode);
  113. deleteNode = (void (*)(UA_Node*))UA_ObjectNode_delete;
  114. break;
  115. case UA_NODECLASS_VARIABLE:
  116. newNode = (UA_Node*)UA_VariableNode_new();
  117. UA_VariableNode_copy((const UA_VariableNode*)node, (UA_VariableNode*)newNode);
  118. deleteNode = (void (*)(UA_Node*))UA_VariableNode_delete;
  119. break;
  120. case UA_NODECLASS_METHOD:
  121. newNode = (UA_Node*)UA_MethodNode_new();
  122. UA_MethodNode_copy((const UA_MethodNode*)node, (UA_MethodNode*)newNode);
  123. deleteNode = (void (*)(UA_Node*))UA_MethodNode_delete;
  124. break;
  125. case UA_NODECLASS_OBJECTTYPE:
  126. newNode = (UA_Node*)UA_ObjectTypeNode_new();
  127. UA_ObjectTypeNode_copy((const UA_ObjectTypeNode*)node, (UA_ObjectTypeNode*)newNode);
  128. deleteNode = (void (*)(UA_Node*))UA_ObjectTypeNode_delete;
  129. break;
  130. case UA_NODECLASS_VARIABLETYPE:
  131. newNode = (UA_Node*)UA_VariableTypeNode_new();
  132. UA_VariableTypeNode_copy((const UA_VariableTypeNode*)node, (UA_VariableTypeNode*)newNode);
  133. deleteNode = (void (*)(UA_Node*))UA_VariableTypeNode_delete;
  134. break;
  135. case UA_NODECLASS_REFERENCETYPE:
  136. newNode = (UA_Node*)UA_ReferenceTypeNode_new();
  137. UA_ReferenceTypeNode_copy((const UA_ReferenceTypeNode*)node, (UA_ReferenceTypeNode*)newNode);
  138. deleteNode = (void (*)(UA_Node*))UA_ReferenceTypeNode_delete;
  139. break;
  140. case UA_NODECLASS_DATATYPE:
  141. newNode = (UA_Node*)UA_DataTypeNode_new();
  142. UA_DataTypeNode_copy((const UA_DataTypeNode*)node, (UA_DataTypeNode*)newNode);
  143. deleteNode = (void (*)(UA_Node*))UA_DataTypeNode_delete;
  144. break;
  145. case UA_NODECLASS_VIEW:
  146. newNode = (UA_Node*)UA_ViewNode_new();
  147. UA_ViewNode_copy((const UA_ViewNode*)node, (UA_ViewNode*)newNode);
  148. deleteNode = (void (*)(UA_Node*))UA_ViewNode_delete;
  149. break;
  150. default:
  151. return UA_STATUSCODE_BADINTERNALERROR;
  152. }
  153. UA_Int32 count = node->referencesSize;
  154. if(count < 0)
  155. count = 0;
  156. UA_ReferenceNode *old_refs = newNode->references;
  157. UA_ReferenceNode *new_refs = UA_malloc(sizeof(UA_ReferenceNode)*(count+1));
  158. if(!new_refs) {
  159. deleteNode(newNode);
  160. UA_NodeStore_release(node);
  161. return UA_STATUSCODE_BADOUTOFMEMORY;
  162. }
  163. // insert the new reference
  164. UA_memcpy(new_refs, old_refs, sizeof(UA_ReferenceNode)*count);
  165. UA_ReferenceNode_init(&new_refs[count]);
  166. retval = UA_NodeId_copy(&item->referenceTypeId, &new_refs[count].referenceTypeId);
  167. new_refs[count].isInverse = !item->isForward;
  168. retval |= UA_ExpandedNodeId_copy(&item->targetNodeId, &new_refs[count].targetId);
  169. if(retval != UA_STATUSCODE_GOOD) {
  170. UA_Array_delete(new_refs, &UA_TYPES[UA_TYPES_REFERENCENODE], ++count);
  171. newNode->references = UA_NULL;
  172. newNode->referencesSize = 0;
  173. deleteNode(newNode);
  174. UA_NodeStore_release(node);
  175. return UA_STATUSCODE_BADOUTOFMEMORY;
  176. }
  177. UA_free(old_refs);
  178. newNode->references = new_refs;
  179. newNode->referencesSize = ++count;
  180. retval = UA_NodeStore_replace(server->nodestore, node, newNode, UA_NULL);
  181. UA_NodeStore_release(node);
  182. if (retval == UA_STATUSCODE_BADINTERNALERROR) {
  183. /* presumably because the node was replaced and an old version was updated at the same time. just try again */
  184. deleteNode(newNode);
  185. return addOneWayReferenceWithSession(server, session, item);
  186. }
  187. return retval;
  188. #endif
  189. }
  190. /* userland version of addReferenceWithSession */
  191. UA_StatusCode
  192. UA_Server_addReference(UA_Server *server, const UA_AddReferencesItem *item)
  193. {
  194. return UA_Server_addReferenceWithSession(server, &adminSession, item);
  195. }
  196. UA_StatusCode
  197. UA_Server_addReferenceWithSession(UA_Server *server, UA_Session *session, const UA_AddReferencesItem *item)
  198. {
  199. if(item->targetServerUri.length > 0)
  200. return UA_STATUSCODE_BADNOTIMPLEMENTED; // currently no expandednodeids are allowed
  201. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  202. #ifdef UA_EXTERNAL_NAMESPACES
  203. UA_ExternalNodeStore *ensFirst = UA_NULL;
  204. UA_ExternalNodeStore *ensSecond = UA_NULL;
  205. for(size_t j = 0;j<server->externalNamespacesSize && (!ensFirst || !ensSecond);j++) {
  206. if(item->sourceNodeId.namespaceIndex == server->externalNamespaces[j].index)
  207. ensFirst = &server->externalNamespaces[j].externalNodeStore;
  208. if(item->targetNodeId.nodeId.namespaceIndex == server->externalNamespaces[j].index)
  209. ensSecond = &server->externalNamespaces[j].externalNodeStore;
  210. }
  211. if(ensFirst) {
  212. // todo: use external nodestore
  213. } else
  214. #endif
  215. retval = addOneWayReferenceWithSession(server, session, item);
  216. if(retval) return retval;
  217. UA_AddReferencesItem secondItem;
  218. secondItem = *item;
  219. secondItem.targetNodeId.nodeId = item->sourceNodeId;
  220. secondItem.sourceNodeId = item->targetNodeId.nodeId;
  221. secondItem.isForward = !item->isForward;
  222. #ifdef UA_EXTERNAL_NAMESPACES
  223. if(ensSecond) {
  224. // todo: use external nodestore
  225. } else
  226. #endif
  227. retval = addOneWayReferenceWithSession (server, session, &secondItem);
  228. // todo: remove reference if the second direction failed
  229. return retval;
  230. }
  231. /* userland version of addNodeWithSession */
  232. UA_AddNodesResult
  233. UA_Server_addNode(UA_Server *server, UA_Node *node, const UA_ExpandedNodeId parentNodeId,
  234. const UA_NodeId referenceTypeId)
  235. {
  236. return UA_Server_addNodeWithSession(server, &adminSession, node, parentNodeId, referenceTypeId);
  237. }
  238. UA_AddNodesResult
  239. UA_Server_addNodeWithSession(UA_Server *server, UA_Session *session, UA_Node *node,
  240. const UA_ExpandedNodeId parentNodeId, const UA_NodeId referenceTypeId)
  241. {
  242. UA_AddNodesResult result;
  243. UA_AddNodesResult_init(&result);
  244. if(node->nodeId.namespaceIndex >= server->namespacesSize) {
  245. result.statusCode = UA_STATUSCODE_BADNODEIDINVALID;
  246. return result;
  247. }
  248. const UA_Node *parent = UA_NodeStore_get(server->nodestore, &parentNodeId.nodeId);
  249. if(!parent) {
  250. result.statusCode = UA_STATUSCODE_BADPARENTNODEIDINVALID;
  251. return result;
  252. }
  253. const UA_ReferenceTypeNode *referenceType =
  254. (const UA_ReferenceTypeNode *)UA_NodeStore_get(server->nodestore, &referenceTypeId);
  255. if(!referenceType) {
  256. result.statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  257. goto ret;
  258. }
  259. if(referenceType->nodeClass != UA_NODECLASS_REFERENCETYPE) {
  260. result.statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
  261. goto ret2;
  262. }
  263. if(referenceType->isAbstract == UA_TRUE) {
  264. result.statusCode = UA_STATUSCODE_BADREFERENCENOTALLOWED;
  265. goto ret2;
  266. }
  267. // todo: test if the referencetype is hierarchical
  268. const UA_Node *managed = UA_NULL;
  269. if(UA_NodeId_isNull(&node->nodeId)) {
  270. if(UA_NodeStore_insert(server->nodestore, node, &managed) != UA_STATUSCODE_GOOD) {
  271. result.statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  272. goto ret2;
  273. }
  274. result.addedNodeId = managed->nodeId; // cannot fail as unique nodeids are numeric
  275. } else {
  276. if(UA_NodeId_copy(&node->nodeId, &result.addedNodeId) != UA_STATUSCODE_GOOD) {
  277. result.statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  278. goto ret2;
  279. }
  280. if(UA_NodeStore_insert(server->nodestore, node, &managed) != UA_STATUSCODE_GOOD) {
  281. result.statusCode = UA_STATUSCODE_BADNODEIDEXISTS; // todo: differentiate out of memory
  282. UA_NodeId_deleteMembers(&result.addedNodeId);
  283. goto ret2;
  284. }
  285. }
  286. // reference back to the parent
  287. UA_AddReferencesItem item;
  288. UA_AddReferencesItem_init(&item);
  289. item.sourceNodeId = managed->nodeId;
  290. item.referenceTypeId = referenceType->nodeId;
  291. item.isForward = UA_FALSE;
  292. item.targetNodeId.nodeId = parent->nodeId;
  293. UA_Server_addReference(server, &item);
  294. // todo: error handling. remove new node from nodestore
  295. UA_NodeStore_release(managed);
  296. ret2:
  297. UA_NodeStore_release((const UA_Node*)referenceType);
  298. ret:
  299. UA_NodeStore_release(parent);
  300. return result;
  301. }