open62541_nodestore_nodemanagement.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * open62541_nodestore_nodemanagement.c
  3. *
  4. * Created on: Oct 27, 2014
  5. * Author: opcua
  6. */
  7. #include "ua_nodestoreExample.h"
  8. #include "../ua_services.h"
  9. #include "open62541_nodestore.h"
  10. #include "ua_namespace_0.h"
  11. #include "ua_util.h"
  12. /*
  13. // ReferenceType Ids
  14. UA_ExpandedNodeId RefTypeId_References; NS0EXPANDEDNODEID(RefTypeId_References, 31);
  15. UA_ExpandedNodeId RefTypeId_NonHierarchicalReferences; NS0EXPANDEDNODEID(RefTypeId_NonHierarchicalReferences, 32);
  16. UA_ExpandedNodeId RefTypeId_HierarchicalReferences; NS0EXPANDEDNODEID(RefTypeId_HierarchicalReferences, 33);
  17. UA_ExpandedNodeId RefTypeId_HasChild; NS0EXPANDEDNODEID(RefTypeId_HasChild, 34);
  18. UA_ExpandedNodeId RefTypeId_Organizes; NS0EXPANDEDNODEID(RefTypeId_Organizes, 35);
  19. UA_ExpandedNodeId RefTypeId_HasEventSource; NS0EXPANDEDNODEID(RefTypeId_HasEventSource, 36);
  20. UA_ExpandedNodeId RefTypeId_HasModellingRule; NS0EXPANDEDNODEID(RefTypeId_HasModellingRule, 37);
  21. UA_ExpandedNodeId RefTypeId_HasEncoding; NS0EXPANDEDNODEID(RefTypeId_HasEncoding, 38);
  22. UA_ExpandedNodeId RefTypeId_HasDescription; NS0EXPANDEDNODEID(RefTypeId_HasDescription, 39);
  23. UA_ExpandedNodeId RefTypeId_HasTypeDefinition; NS0EXPANDEDNODEID(RefTypeId_HasTypeDefinition, 40);
  24. UA_ExpandedNodeId RefTypeId_GeneratesEvent; NS0EXPANDEDNODEID(RefTypeId_GeneratesEvent, 41);
  25. UA_ExpandedNodeId RefTypeId_Aggregates; NS0EXPANDEDNODEID(RefTypeId_Aggregates, 44);
  26. UA_ExpandedNodeId RefTypeId_HasSubtype; NS0EXPANDEDNODEID(RefTypeId_HasSubtype, 45);
  27. UA_ExpandedNodeId RefTypeId_HasProperty; NS0EXPANDEDNODEID(RefTypeId_HasProperty, 46);
  28. UA_ExpandedNodeId RefTypeId_HasComponent; NS0EXPANDEDNODEID(RefTypeId_HasComponent, 47);
  29. UA_ExpandedNodeId RefTypeId_HasNotifier; NS0EXPANDEDNODEID(RefTypeId_HasNotifier, 48);
  30. UA_ExpandedNodeId RefTypeId_HasOrderedComponent; NS0EXPANDEDNODEID(RefTypeId_HasOrderedComponent, 49);
  31. UA_ExpandedNodeId RefTypeId_HasModelParent; NS0EXPANDEDNODEID(RefTypeId_HasModelParent, 50);
  32. UA_ExpandedNodeId RefTypeId_FromState; NS0EXPANDEDNODEID(RefTypeId_FromState, 51);
  33. UA_ExpandedNodeId RefTypeId_ToState; NS0EXPANDEDNODEID(RefTypeId_ToState, 52);
  34. UA_ExpandedNodeId RefTypeId_HasCause; NS0EXPANDEDNODEID(RefTypeId_HasCause, 53);
  35. UA_ExpandedNodeId RefTypeId_HasEffect; NS0EXPANDEDNODEID(RefTypeId_HasEffect, 54);
  36. UA_ExpandedNodeId RefTypeId_HasHistoricalConfiguration; NS0EXPANDEDNODEID(RefTypeId_HasHistoricalConfiguration, 56);
  37. */
  38. UA_Int32 open62541NodeStore_addReferences(UA_AddReferencesItem* referencesToAdd,
  39. UA_UInt32 *indices,UA_UInt32 indicesSize, UA_StatusCode *addReferencesResults,
  40. UA_DiagnosticInfo *diagnosticInfos)
  41. {
  42. for(UA_UInt32 i = 0;i<indicesSize;i++){
  43. UA_Node *node = UA_NULL;
  44. UA_NodeStoreExample *ns = Nodestore_get();
  45. UA_NodeStoreExample_get((const UA_NodeStoreExample*)ns,(const UA_NodeId*)&referencesToAdd[indices[i]].sourceNodeId, (const UA_Node**)&node);
  46. if(node == UA_NULL){
  47. addReferencesResults[indices[i]] = UA_STATUSCODE_BADSOURCENODEIDINVALID;
  48. continue;
  49. }
  50. // TODO: Check if reference already exists
  51. UA_Int32 count = node->referencesSize;
  52. UA_ReferenceNode *old_refs = node->references;
  53. UA_ReferenceNode *new_refs;
  54. if(count < 0) count = 0;
  55. if(!(new_refs = UA_alloc(sizeof(UA_ReferenceNode)*(count+1))))
  56. return UA_STATUSCODE_BADOUTOFMEMORY;
  57. UA_memcpy(new_refs, old_refs, sizeof(UA_ReferenceNode)*count);
  58. UA_ReferenceNode *reference;
  59. UA_ReferenceNode_new(&reference);
  60. reference->isInverse = !referencesToAdd[indices[i]].isForward;
  61. UA_NodeId_copy(&referencesToAdd[indices[i]].referenceTypeId,&reference->referenceTypeId);
  62. UA_ExpandedNodeId_copy(&referencesToAdd[indices[i]].targetNodeId,&reference->targetId);
  63. if(UA_ReferenceNode_copy(reference, &new_refs[count]) != UA_STATUSCODE_GOOD) {
  64. UA_free(new_refs);
  65. addReferencesResults[indices[i]] = UA_STATUSCODE_BADOUTOFMEMORY;
  66. }
  67. node->references = new_refs;
  68. node->referencesSize = count+1;
  69. UA_free(old_refs);
  70. addReferencesResults[indices[i]] = UA_STATUSCODE_GOOD;
  71. UA_ReferenceNode_delete(reference); //FIXME to be removed
  72. //TODO fill diagnostic info if needed
  73. }
  74. return UA_STATUSCODE_GOOD;
  75. }
  76. UA_Int32 open62541Nodestore_addNodes(UA_AddNodesItem *nodesToAdd,UA_UInt32 *indices,
  77. UA_UInt32 indicesSize, UA_AddNodesResult* addNodesResults,
  78. UA_DiagnosticInfo *diagnosticInfos){
  79. UA_Node *node = UA_NULL;
  80. for(UA_UInt32 i=0;i<indicesSize;i++){
  81. UA_NodeStoreExample *ns = Nodestore_get();
  82. UA_NodeStoreExample_get((const UA_NodeStoreExample*)ns, (const UA_NodeId*)&nodesToAdd[indices[i]].requestedNewNodeId.nodeId , (const UA_Node**)&node);
  83. if(node==UA_NULL){
  84. switch(nodesToAdd[indices[i]].nodeClass){
  85. case UA_NODECLASS_DATATYPE:
  86. {
  87. break;
  88. }
  89. case UA_NODECLASS_METHOD:
  90. {
  91. break;
  92. }
  93. case UA_NODECLASS_OBJECT:
  94. {
  95. UA_ObjectNode *newNode;
  96. UA_ObjectNode_new(&newNode);
  97. newNode->nodeId = nodesToAdd[indices[i]].requestedNewNodeId.nodeId;
  98. newNode->nodeClass = nodesToAdd[indices[i]].nodeClass;
  99. UA_QualifiedName_copy(&nodesToAdd[indices[i]].browseName, &newNode->browseName);
  100. UA_UInt32 offset = 0;
  101. UA_ObjectAttributes objType;
  102. UA_ObjectAttributes_decodeBinary(&nodesToAdd[indices[i]].nodeAttributes.body,&offset,&objType);
  103. if(objType.specifiedAttributes & UA_ATTRIBUTEID_DISPLAYNAME){
  104. UA_LocalizedText_copy(&objType.displayName, &newNode->displayName);
  105. }
  106. if(objType.specifiedAttributes & UA_ATTRIBUTEID_DESCRIPTION){
  107. UA_LocalizedText_copy(&objType.description, &newNode->description);
  108. }
  109. if(objType.specifiedAttributes & UA_ATTRIBUTEID_EVENTNOTIFIER){
  110. newNode->eventNotifier = objType.eventNotifier;
  111. }
  112. if(objType.specifiedAttributes & UA_ATTRIBUTEID_WRITEMASK){
  113. newNode->writeMask = objType.writeMask;
  114. }
  115. UA_AddReferencesItem addRefItem;
  116. addRefItem.isForward = UA_TRUE;
  117. addRefItem.referenceTypeId = nodesToAdd[indices[i]].referenceTypeId;
  118. addRefItem.sourceNodeId = nodesToAdd[indices[i]].parentNodeId.nodeId;
  119. addRefItem.targetNodeId.nodeId = newNode->nodeId;
  120. addRefItem.targetNodeId.namespaceUri.length = 0;
  121. addRefItem.targetServerUri.length = 0;
  122. addRefItem.targetNodeClass = newNode->nodeClass;
  123. UA_UInt32 ind = 0;
  124. UA_UInt32 indSize = 1;
  125. UA_StatusCode result;
  126. UA_DiagnosticInfo diagnosticInfo;
  127. UA_NodeStoreExample_insert(ns, (UA_Node**)&newNode, UA_NODESTORE_INSERT_UNIQUE);
  128. if(!(
  129. nodesToAdd[indices[i]].requestedNewNodeId.nodeId.identifier.numeric == 84 &&
  130. nodesToAdd[indices[i]].requestedNewNodeId.nodeId.namespaceIndex == 0)){
  131. open62541NodeStore_addReferences(&addRefItem, &ind, indSize, &result, &diagnosticInfo);
  132. }
  133. break;
  134. }
  135. case UA_NODECLASS_OBJECTTYPE:
  136. {
  137. break;
  138. }
  139. case UA_NODECLASS_REFERENCETYPE:
  140. {
  141. UA_ReferenceTypeNode *newNode;
  142. UA_ReferenceTypeNode_new(&newNode);
  143. newNode->nodeId = nodesToAdd[indices[i]].requestedNewNodeId.nodeId;
  144. newNode->nodeClass = nodesToAdd[indices[i]].nodeClass;
  145. UA_QualifiedName_copy(&nodesToAdd[indices[i]].browseName, &newNode->browseName);
  146. UA_UInt32 offset = 0;
  147. UA_ReferenceTypeAttributes refType;
  148. UA_ReferenceTypeAttributes_decodeBinary(&nodesToAdd[indices[i]].nodeAttributes.body,&offset,&refType);
  149. if(refType.specifiedAttributes & UA_ATTRIBUTEID_DISPLAYNAME){
  150. UA_LocalizedText_copy(&refType.displayName, &newNode->displayName);
  151. }
  152. if(refType.specifiedAttributes & UA_ATTRIBUTEID_DESCRIPTION){
  153. UA_LocalizedText_copy(&refType.description, &newNode->description);
  154. }
  155. if(refType.specifiedAttributes & UA_ATTRIBUTEID_ISABSTRACT){
  156. newNode->isAbstract = refType.isAbstract;
  157. }
  158. if(refType.specifiedAttributes & UA_ATTRIBUTEID_SYMMETRIC){
  159. newNode->symmetric = refType.symmetric;
  160. }
  161. //ADDREFERENCE(haschild, RefTypeId_HasSubtype, UA_TRUE, RefTypeId_HierarchicalReferences);
  162. UA_AddReferencesItem addRefItem;
  163. addRefItem.isForward = UA_TRUE;
  164. addRefItem.referenceTypeId = nodesToAdd[indices[i]].referenceTypeId;
  165. addRefItem.sourceNodeId = nodesToAdd[indices[i]].parentNodeId.nodeId;
  166. addRefItem.targetNodeId.nodeId = newNode->nodeId;
  167. addRefItem.targetNodeId.namespaceUri.length = 0;
  168. addRefItem.targetServerUri.length = 0;
  169. addRefItem.targetNodeClass = newNode->nodeClass;
  170. UA_UInt32 ind = 0;
  171. UA_UInt32 indSize = 1;
  172. UA_StatusCode result;
  173. UA_DiagnosticInfo diagnosticInfo;
  174. UA_NodeStoreExample_insert(ns, (UA_Node**)&newNode, UA_NODESTORE_INSERT_UNIQUE);
  175. open62541NodeStore_addReferences(&addRefItem, &ind, indSize, &result, &diagnosticInfo);
  176. break;
  177. }
  178. case UA_NODECLASS_VARIABLE:
  179. {
  180. break;
  181. }
  182. case UA_NODECLASS_VARIABLETYPE:
  183. {
  184. break;
  185. }
  186. default:
  187. {
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. return UA_STATUSCODE_GOOD;
  194. }