ua_server_utils.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "ua_server_internal.h"
  5. /**********************/
  6. /* Parse NumericRange */
  7. /**********************/
  8. static size_t
  9. readDimension(UA_Byte *buf, size_t buflen, UA_NumericRangeDimension *dim) {
  10. size_t progress = UA_readNumber(buf, buflen, &dim->min);
  11. if(progress == 0)
  12. return 0;
  13. if(buflen <= progress + 1 || buf[progress] != ':') {
  14. dim->max = dim->min;
  15. return progress;
  16. }
  17. ++progress;
  18. size_t progress2 = UA_readNumber(&buf[progress], buflen - progress, &dim->max);
  19. if(progress2 == 0)
  20. return 0;
  21. /* invalid range */
  22. if(dim->min >= dim->max)
  23. return 0;
  24. return progress + progress2;
  25. }
  26. UA_StatusCode
  27. UA_NumericRange_parseFromString(UA_NumericRange *range, const UA_String *str) {
  28. size_t idx = 0;
  29. size_t dimensionsMax = 0;
  30. UA_NumericRangeDimension *dimensions = NULL;
  31. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  32. size_t offset = 0;
  33. while(true) {
  34. /* alloc dimensions */
  35. if(idx >= dimensionsMax) {
  36. UA_NumericRangeDimension *newds;
  37. size_t newdssize = sizeof(UA_NumericRangeDimension) * (dimensionsMax + 2);
  38. newds = (UA_NumericRangeDimension*)UA_realloc(dimensions, newdssize);
  39. if(!newds) {
  40. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  41. break;
  42. }
  43. dimensions = newds;
  44. dimensionsMax = dimensionsMax + 2;
  45. }
  46. /* read the dimension */
  47. size_t progress = readDimension(&str->data[offset], str->length - offset,
  48. &dimensions[idx]);
  49. if(progress == 0) {
  50. retval = UA_STATUSCODE_BADINDEXRANGEINVALID;
  51. break;
  52. }
  53. offset += progress;
  54. ++idx;
  55. /* loop into the next dimension */
  56. if(offset >= str->length)
  57. break;
  58. if(str->data[offset] != ',') {
  59. retval = UA_STATUSCODE_BADINDEXRANGEINVALID;
  60. break;
  61. }
  62. ++offset;
  63. }
  64. if(retval == UA_STATUSCODE_GOOD && idx > 0) {
  65. range->dimensions = dimensions;
  66. range->dimensionsSize = idx;
  67. } else
  68. UA_free(dimensions);
  69. return retval;
  70. }
  71. /********************************/
  72. /* Information Model Operations */
  73. /********************************/
  74. UA_StatusCode
  75. getTypeHierarchy(UA_NodeStore *ns, const UA_Node *rootRef, UA_Boolean inverse,
  76. UA_NodeId **typeHierarchy, size_t *typeHierarchySize) {
  77. size_t results_size = 20; // probably too big, but saves mallocs
  78. UA_NodeId *results = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * results_size);
  79. if(!results)
  80. return UA_STATUSCODE_BADOUTOFMEMORY;
  81. UA_StatusCode retval = UA_NodeId_copy(&rootRef->nodeId, &results[0]);
  82. if(retval != UA_STATUSCODE_GOOD) {
  83. UA_free(results);
  84. return retval;
  85. }
  86. const UA_Node *node = rootRef;
  87. size_t idx = 0; /* Current index (contains NodeId of node) */
  88. size_t last = 0; /* Index of the last element in the array */
  89. const UA_NodeId hasSubtypeNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
  90. while(true) {
  91. for(size_t i = 0; i < node->referencesSize; ++i) {
  92. /* is the reference relevant? */
  93. if(node->references[i].isInverse != inverse ||
  94. !UA_NodeId_equal(&hasSubtypeNodeId, &node->references[i].referenceTypeId))
  95. continue;
  96. /* is the target already considered? (multi-inheritance) */
  97. UA_Boolean duplicate = false;
  98. for(size_t j = 0; j <= last; ++j) {
  99. if(UA_NodeId_equal(&node->references[i].targetId.nodeId, &results[j])) {
  100. duplicate = true;
  101. break;
  102. }
  103. }
  104. if(duplicate)
  105. continue;
  106. /* increase array length if necessary */
  107. if(last + 1 >= results_size) {
  108. UA_NodeId *new_results =
  109. (UA_NodeId*)UA_realloc(results, sizeof(UA_NodeId) * results_size * 2);
  110. if(!new_results) {
  111. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  112. break;
  113. }
  114. results = new_results;
  115. results_size *= 2;
  116. }
  117. /* copy new nodeid to the end of the list */
  118. retval = UA_NodeId_copy(&node->references[i].targetId.nodeId, &results[++last]);
  119. if(retval != UA_STATUSCODE_GOOD)
  120. break;
  121. }
  122. /* Get the next node */
  123. next:
  124. ++idx;
  125. if(idx > last || retval != UA_STATUSCODE_GOOD)
  126. break;
  127. node = UA_NodeStore_get(ns, &results[idx]);
  128. if(!node || node->nodeClass != rootRef->nodeClass)
  129. goto next;
  130. }
  131. if(retval != UA_STATUSCODE_GOOD) {
  132. UA_Array_delete(results, last, &UA_TYPES[UA_TYPES_NODEID]);
  133. return retval;
  134. }
  135. *typeHierarchy = results;
  136. *typeHierarchySize = last + 1;
  137. return UA_STATUSCODE_GOOD;
  138. }
  139. UA_Boolean
  140. isNodeInTree(UA_NodeStore *ns, const UA_NodeId *leafNode, const UA_NodeId *nodeToFind,
  141. const UA_NodeId *referenceTypeIds, size_t referenceTypeIdsSize) {
  142. if(UA_NodeId_equal(leafNode, nodeToFind))
  143. return true;
  144. const UA_Node *node = UA_NodeStore_get(ns,leafNode);
  145. if(!node)
  146. return false;
  147. /* Search upwards in the tree */
  148. for(size_t i = 0; i < node->referencesSize; ++i) {
  149. if(!node->references[i].isInverse)
  150. continue;
  151. /* Recurse only for valid reference types */
  152. for(size_t j = 0; j < referenceTypeIdsSize; ++j) {
  153. if(UA_NodeId_equal(&node->references[i].referenceTypeId, &referenceTypeIds[j]) &&
  154. isNodeInTree(ns, &node->references[i].targetId.nodeId, nodeToFind,
  155. referenceTypeIds, referenceTypeIdsSize))
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. void getNodeType(UA_Server *server, const UA_Node *node, UA_NodeId *typeId) {
  162. UA_NodeId_init(typeId);
  163. UA_NodeId parentRef;
  164. UA_Boolean inverse;
  165. /* The reference to the parent is different for variable and variabletype */
  166. if(node->nodeClass == UA_NODECLASS_VARIABLE ||
  167. node->nodeClass == UA_NODECLASS_OBJECT) {
  168. parentRef = UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION);
  169. inverse = false;
  170. } else if(node->nodeClass == UA_NODECLASS_VARIABLETYPE ||
  171. node->nodeClass == UA_NODECLASS_OBJECTTYPE ||
  172. node->nodeClass == UA_NODECLASS_REFERENCETYPE ||
  173. node->nodeClass == UA_NODECLASS_DATATYPE) {
  174. parentRef = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
  175. inverse = true;
  176. } else {
  177. return;
  178. }
  179. /* Stop at the first matching candidate */
  180. for(size_t i = 0; i < node->referencesSize; ++i) {
  181. if(node->references[i].isInverse == inverse &&
  182. UA_NodeId_equal(&node->references[i].referenceTypeId, &parentRef)) {
  183. UA_NodeId_copy(&node->references[i].targetId.nodeId, typeId);
  184. break;
  185. }
  186. }
  187. }
  188. const UA_VariableTypeNode *
  189. getVariableNodeType(UA_Server *server, const UA_VariableNode *node) {
  190. UA_NodeId vtId;
  191. getNodeType(server, (const UA_Node*)node, &vtId);
  192. const UA_Node *vt = UA_NodeStore_get(server->nodestore, &vtId);
  193. if(!vt || vt->nodeClass != UA_NODECLASS_VARIABLETYPE) {
  194. vt = NULL;
  195. UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER,
  196. "No VariableType for the node found");
  197. }
  198. return (const UA_VariableTypeNode*)vt;
  199. }
  200. const UA_ObjectTypeNode *
  201. getObjectNodeType(UA_Server *server, const UA_ObjectNode *node) {
  202. UA_NodeId otId;
  203. getNodeType(server, (const UA_Node*)node, &otId);
  204. const UA_Node *ot = UA_NodeStore_get(server->nodestore, &otId);
  205. if(!ot || ot->nodeClass != UA_NODECLASS_OBJECTTYPE) {
  206. ot = NULL;
  207. UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER,
  208. "No ObjectType for the node found");
  209. }
  210. return (const UA_ObjectTypeNode*)ot;
  211. }
  212. UA_Boolean
  213. UA_Node_hasSubTypeOrInstances(const UA_Node *node) {
  214. const UA_NodeId hasSubType = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
  215. const UA_NodeId hasTypeDefinition = UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION);
  216. for(size_t i = 0; i < node->referencesSize; ++i) {
  217. if(node->references[i].isInverse == false &&
  218. UA_NodeId_equal(&node->references[i].referenceTypeId, &hasSubType))
  219. return true;
  220. if(node->references[i].isInverse == true &&
  221. UA_NodeId_equal(&node->references[i].referenceTypeId, &hasTypeDefinition))
  222. return true;
  223. }
  224. return false;
  225. }
  226. /* For mulithreading: make a copy of the node, edit and replace.
  227. * For singletrheading: edit the original */
  228. UA_StatusCode
  229. UA_Server_editNode(UA_Server *server, UA_Session *session,
  230. const UA_NodeId *nodeId, UA_EditNodeCallback callback,
  231. const void *data) {
  232. #ifndef UA_ENABLE_MULTITHREADING
  233. const UA_Node *node = UA_NodeStore_get(server->nodestore, nodeId);
  234. if(!node)
  235. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  236. UA_Node *editNode = (UA_Node*)(uintptr_t)node; // dirty cast
  237. return callback(server, session, editNode, data);
  238. #else
  239. UA_StatusCode retval;
  240. do {
  241. UA_RCU_LOCK();
  242. UA_Node *copy = UA_NodeStore_getCopy(server->nodestore, nodeId);
  243. UA_RCU_UNLOCK();
  244. if(!copy)
  245. return UA_STATUSCODE_BADOUTOFMEMORY;
  246. retval = callback(server, session, copy, data);
  247. if(retval != UA_STATUSCODE_GOOD) {
  248. UA_NodeStore_deleteNode(copy);
  249. return retval;
  250. }
  251. UA_RCU_LOCK();
  252. retval = UA_NodeStore_replace(server->nodestore, copy);
  253. UA_RCU_UNLOCK();
  254. } while(retval != UA_STATUSCODE_GOOD);
  255. return UA_STATUSCODE_GOOD;
  256. #endif
  257. }