ua_services_attribute.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2015-2016 (c) Sten Grüner
  7. * Copyright 2014-2017 (c) Florian Palm
  8. * Copyright 2015 (c) Christian Fimmers
  9. * Copyright 2015-2016 (c) Chris Iatrou
  10. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  11. * Copyright 2015 (c) wuyangtang
  12. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  13. * Copyright 2016 (c) Lorenz Haas
  14. * Copyright 2017 (c) frax2222
  15. * Copyright 2017 (c) Thomas Bender
  16. * Copyright 2017 (c) Julian Grothoff
  17. * Copyright 2017 (c) Jonas Green
  18. * Copyright 2017 (c) Henrik Norrman
  19. */
  20. #include "ua_server_internal.h"
  21. #include "ua_types_encoding_binary.h"
  22. #include "ua_services.h"
  23. /******************/
  24. /* Access Control */
  25. /******************/
  26. static UA_UInt32
  27. getUserWriteMask(UA_Server *server, const UA_Session *session,
  28. const UA_Node *node) {
  29. if(session == &adminSession)
  30. return 0xFFFFFFFF; /* the local admin user has all rights */
  31. return node->writeMask &
  32. server->config.accessControl.getUserRightsMask(server, &server->config.accessControl,
  33. &session->sessionId, session->sessionHandle,
  34. &node->nodeId, node->context);
  35. }
  36. static UA_Byte
  37. getAccessLevel(UA_Server *server, const UA_Session *session,
  38. const UA_VariableNode *node) {
  39. if(session == &adminSession)
  40. return 0xFF; /* the local admin user has all rights */
  41. return node->accessLevel;
  42. }
  43. static UA_Byte
  44. getUserAccessLevel(UA_Server *server, const UA_Session *session,
  45. const UA_VariableNode *node) {
  46. if(session == &adminSession)
  47. return 0xFF; /* the local admin user has all rights */
  48. return node->accessLevel &
  49. server->config.accessControl.getUserAccessLevel(server, &server->config.accessControl,
  50. &session->sessionId, session->sessionHandle,
  51. &node->nodeId, node->context);
  52. }
  53. static UA_Boolean
  54. getUserExecutable(UA_Server *server, const UA_Session *session,
  55. const UA_MethodNode *node) {
  56. if(session == &adminSession)
  57. return true; /* the local admin user has all rights */
  58. return node->executable &
  59. server->config.accessControl.getUserExecutable(server, &server->config.accessControl,
  60. &session->sessionId, session->sessionHandle,
  61. &node->nodeId, node->context);
  62. }
  63. /****************/
  64. /* Read Service */
  65. /****************/
  66. static UA_StatusCode
  67. readArrayDimensionsAttribute(const UA_VariableNode *vn, UA_DataValue *v) {
  68. UA_Variant_setArray(&v->value, vn->arrayDimensions,
  69. vn->arrayDimensionsSize, &UA_TYPES[UA_TYPES_UINT32]);
  70. v->value.storageType = UA_VARIANT_DATA_NODELETE;
  71. return UA_STATUSCODE_GOOD;
  72. }
  73. static void
  74. setScalarNoDelete(UA_Variant *v, const void * UA_RESTRICT p,
  75. const UA_DataType *type) {
  76. UA_Variant_setScalar(v, (void*)(uintptr_t)p, type);
  77. v->storageType = UA_VARIANT_DATA_NODELETE;
  78. }
  79. static UA_StatusCode
  80. readIsAbstractAttribute(const UA_Node *node, UA_Variant *v) {
  81. const UA_Boolean *isAbstract;
  82. switch(node->nodeClass) {
  83. case UA_NODECLASS_REFERENCETYPE:
  84. isAbstract = &((const UA_ReferenceTypeNode*)node)->isAbstract;
  85. break;
  86. case UA_NODECLASS_OBJECTTYPE:
  87. isAbstract = &((const UA_ObjectTypeNode*)node)->isAbstract;
  88. break;
  89. case UA_NODECLASS_VARIABLETYPE:
  90. isAbstract = &((const UA_VariableTypeNode*)node)->isAbstract;
  91. break;
  92. case UA_NODECLASS_DATATYPE:
  93. isAbstract = &((const UA_DataTypeNode*)node)->isAbstract;
  94. break;
  95. default:
  96. return UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  97. }
  98. setScalarNoDelete(v, isAbstract, &UA_TYPES[UA_TYPES_BOOLEAN]);
  99. v->storageType = UA_VARIANT_DATA_NODELETE;
  100. return UA_STATUSCODE_GOOD;
  101. }
  102. static UA_StatusCode
  103. readValueAttributeFromNode(UA_Server *server, UA_Session *session,
  104. const UA_VariableNode *vn, UA_DataValue *v,
  105. UA_NumericRange *rangeptr) {
  106. if(vn->value.data.callback.onRead) {
  107. vn->value.data.callback.onRead(server, &session->sessionId,
  108. session->sessionHandle, &vn->nodeId,
  109. vn->context, rangeptr, &vn->value.data.value);
  110. const UA_Node *old = (const UA_Node *)vn;
  111. /* Reopen the node to see the changes from onRead */
  112. vn = (const UA_VariableNode*)UA_Nodestore_get(server, &vn->nodeId);
  113. UA_Nodestore_release(server, old);
  114. }
  115. if(rangeptr)
  116. return UA_Variant_copyRange(&vn->value.data.value.value, &v->value, *rangeptr);
  117. *v = vn->value.data.value;
  118. v->value.storageType = UA_VARIANT_DATA_NODELETE;
  119. return UA_STATUSCODE_GOOD;
  120. }
  121. static UA_StatusCode
  122. readValueAttributeFromDataSource(UA_Server *server, UA_Session *session,
  123. const UA_VariableNode *vn, UA_DataValue *v,
  124. UA_TimestampsToReturn timestamps,
  125. UA_NumericRange *rangeptr) {
  126. if(!vn->value.dataSource.read)
  127. return UA_STATUSCODE_BADINTERNALERROR;
  128. UA_Boolean sourceTimeStamp = (timestamps == UA_TIMESTAMPSTORETURN_SOURCE ||
  129. timestamps == UA_TIMESTAMPSTORETURN_BOTH);
  130. return vn->value.dataSource.read(server, &session->sessionId, session->sessionHandle,
  131. &vn->nodeId, vn->context, sourceTimeStamp, rangeptr, v);
  132. }
  133. static UA_StatusCode
  134. readValueAttributeComplete(UA_Server *server, UA_Session *session,
  135. const UA_VariableNode *vn, UA_TimestampsToReturn timestamps,
  136. const UA_String *indexRange, UA_DataValue *v) {
  137. /* Compute the index range */
  138. UA_NumericRange range;
  139. UA_NumericRange *rangeptr = NULL;
  140. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  141. if(indexRange && indexRange->length > 0) {
  142. retval = UA_NumericRange_parseFromString(&range, indexRange);
  143. if(retval != UA_STATUSCODE_GOOD)
  144. return retval;
  145. rangeptr = ⦥
  146. }
  147. /* Read the value */
  148. if(vn->valueSource == UA_VALUESOURCE_DATA)
  149. retval = readValueAttributeFromNode(server, session, vn, v, rangeptr);
  150. else
  151. retval = readValueAttributeFromDataSource(server, session, vn, v, timestamps, rangeptr);
  152. /* Clean up */
  153. if(rangeptr)
  154. UA_free(range.dimensions);
  155. return retval;
  156. }
  157. UA_StatusCode
  158. readValueAttribute(UA_Server *server, UA_Session *session,
  159. const UA_VariableNode *vn, UA_DataValue *v) {
  160. return readValueAttributeComplete(server, session, vn, UA_TIMESTAMPSTORETURN_NEITHER, NULL, v);
  161. }
  162. static const UA_String binEncoding = {sizeof("Default Binary")-1, (UA_Byte*)"Default Binary"};
  163. static const UA_String xmlEncoding = {sizeof("Default XML")-1, (UA_Byte*)"Default XML"};
  164. static const UA_String jsonEncoding = {sizeof("Default JSON")-1, (UA_Byte*)"Default JSON"};
  165. #define CHECK_NODECLASS(CLASS) \
  166. if(!(node->nodeClass & (CLASS))) { \
  167. retval = UA_STATUSCODE_BADATTRIBUTEIDINVALID; \
  168. break; \
  169. }
  170. /* Returns a datavalue that may point into the node via the
  171. * UA_VARIANT_DATA_NODELETE tag. Don't access the returned DataValue once the
  172. * node has been released! */
  173. static void
  174. Read(const UA_Node *node, UA_Server *server, UA_Session *session,
  175. UA_TimestampsToReturn timestampsToReturn,
  176. const UA_ReadValueId *id, UA_DataValue *v) {
  177. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  178. "Read the attribute %i", id->attributeId);
  179. /* Only Binary Encoding is supported */
  180. if(id->dataEncoding.name.length > 0 &&
  181. !UA_String_equal(&binEncoding, &id->dataEncoding.name)) {
  182. if(UA_String_equal(&xmlEncoding, &id->dataEncoding.name) ||
  183. UA_String_equal(&jsonEncoding, &id->dataEncoding.name))
  184. v->status = UA_STATUSCODE_BADDATAENCODINGUNSUPPORTED;
  185. else
  186. v->status = UA_STATUSCODE_BADDATAENCODINGINVALID;
  187. v->hasStatus = true;
  188. return;
  189. }
  190. /* Index range for an attribute other than value */
  191. if(id->indexRange.length > 0 && id->attributeId != UA_ATTRIBUTEID_VALUE) {
  192. v->hasStatus = true;
  193. v->status = UA_STATUSCODE_BADINDEXRANGENODATA;
  194. return;
  195. }
  196. /* Read the attribute */
  197. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  198. switch(id->attributeId) {
  199. case UA_ATTRIBUTEID_NODEID:
  200. setScalarNoDelete(&v->value, &node->nodeId, &UA_TYPES[UA_TYPES_NODEID]);
  201. break;
  202. case UA_ATTRIBUTEID_NODECLASS:
  203. setScalarNoDelete(&v->value, &node->nodeClass, &UA_TYPES[UA_TYPES_NODECLASS]);
  204. break;
  205. case UA_ATTRIBUTEID_BROWSENAME:
  206. setScalarNoDelete(&v->value, &node->browseName, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  207. break;
  208. case UA_ATTRIBUTEID_DISPLAYNAME:
  209. setScalarNoDelete(&v->value, &node->displayName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  210. break;
  211. case UA_ATTRIBUTEID_DESCRIPTION:
  212. setScalarNoDelete(&v->value, &node->description, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  213. break;
  214. case UA_ATTRIBUTEID_WRITEMASK:
  215. setScalarNoDelete(&v->value, &node->writeMask, &UA_TYPES[UA_TYPES_UINT32]);
  216. break;
  217. case UA_ATTRIBUTEID_USERWRITEMASK: {
  218. UA_UInt32 userWriteMask = getUserWriteMask(server, session, node);
  219. retval = UA_Variant_setScalarCopy(&v->value, &userWriteMask, &UA_TYPES[UA_TYPES_UINT32]);
  220. break; }
  221. case UA_ATTRIBUTEID_ISABSTRACT:
  222. retval = readIsAbstractAttribute(node, &v->value);
  223. break;
  224. case UA_ATTRIBUTEID_SYMMETRIC:
  225. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  226. setScalarNoDelete(&v->value, &((const UA_ReferenceTypeNode*)node)->symmetric,
  227. &UA_TYPES[UA_TYPES_BOOLEAN]);
  228. break;
  229. case UA_ATTRIBUTEID_INVERSENAME:
  230. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  231. setScalarNoDelete(&v->value, &((const UA_ReferenceTypeNode*)node)->inverseName,
  232. &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  233. break;
  234. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  235. CHECK_NODECLASS(UA_NODECLASS_VIEW);
  236. setScalarNoDelete(&v->value, &((const UA_ViewNode*)node)->containsNoLoops,
  237. &UA_TYPES[UA_TYPES_BOOLEAN]);
  238. break;
  239. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  240. CHECK_NODECLASS(UA_NODECLASS_VIEW | UA_NODECLASS_OBJECT);
  241. setScalarNoDelete(&v->value, &((const UA_ViewNode*)node)->eventNotifier,
  242. &UA_TYPES[UA_TYPES_BYTE]);
  243. break;
  244. case UA_ATTRIBUTEID_VALUE: {
  245. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  246. /* VariableTypes don't have the AccessLevel concept. Always allow reading the value. */
  247. if(node->nodeClass == UA_NODECLASS_VARIABLE) {
  248. /* The access to a value variable is granted via the AccessLevel
  249. * and UserAccessLevel attributes */
  250. UA_Byte accessLevel = getAccessLevel(server, session, (const UA_VariableNode*)node);
  251. if(!(accessLevel & (UA_ACCESSLEVELMASK_READ))) {
  252. retval = UA_STATUSCODE_BADNOTREADABLE;
  253. break;
  254. }
  255. accessLevel = getUserAccessLevel(server, session,
  256. (const UA_VariableNode*)node);
  257. if(!(accessLevel & (UA_ACCESSLEVELMASK_READ))) {
  258. retval = UA_STATUSCODE_BADUSERACCESSDENIED;
  259. break;
  260. }
  261. }
  262. retval = readValueAttributeComplete(server, session, (const UA_VariableNode*)node,
  263. timestampsToReturn, &id->indexRange, v);
  264. break;
  265. }
  266. case UA_ATTRIBUTEID_DATATYPE:
  267. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  268. setScalarNoDelete(&v->value, &((const UA_VariableTypeNode*)node)->dataType,
  269. &UA_TYPES[UA_TYPES_NODEID]);
  270. break;
  271. case UA_ATTRIBUTEID_VALUERANK:
  272. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  273. setScalarNoDelete(&v->value, &((const UA_VariableTypeNode*)node)->valueRank,
  274. &UA_TYPES[UA_TYPES_INT32]);
  275. break;
  276. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  277. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  278. retval = readArrayDimensionsAttribute((const UA_VariableNode*)node, v);
  279. break;
  280. case UA_ATTRIBUTEID_ACCESSLEVEL:
  281. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  282. setScalarNoDelete(&v->value, &((const UA_VariableNode*)node)->accessLevel,
  283. &UA_TYPES[UA_TYPES_BYTE]);
  284. break;
  285. case UA_ATTRIBUTEID_USERACCESSLEVEL: {
  286. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  287. UA_Byte userAccessLevel = getUserAccessLevel(server, session,
  288. (const UA_VariableNode*)node);
  289. retval = UA_Variant_setScalarCopy(&v->value, &userAccessLevel, &UA_TYPES[UA_TYPES_BYTE]);
  290. break; }
  291. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  292. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  293. setScalarNoDelete(&v->value, &((const UA_VariableNode*)node)->minimumSamplingInterval,
  294. &UA_TYPES[UA_TYPES_DOUBLE]);
  295. break;
  296. case UA_ATTRIBUTEID_HISTORIZING:
  297. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  298. setScalarNoDelete(&v->value, &((const UA_VariableNode*)node)->historizing,
  299. &UA_TYPES[UA_TYPES_BOOLEAN]);
  300. break;
  301. case UA_ATTRIBUTEID_EXECUTABLE:
  302. CHECK_NODECLASS(UA_NODECLASS_METHOD);
  303. setScalarNoDelete(&v->value, &((const UA_MethodNode*)node)->executable,
  304. &UA_TYPES[UA_TYPES_BOOLEAN]);
  305. break;
  306. case UA_ATTRIBUTEID_USEREXECUTABLE: {
  307. CHECK_NODECLASS(UA_NODECLASS_METHOD);
  308. UA_Boolean userExecutable = getUserExecutable(server, session,
  309. (const UA_MethodNode*)node);
  310. retval = UA_Variant_setScalarCopy(&v->value, &userExecutable, &UA_TYPES[UA_TYPES_BOOLEAN]);
  311. break; }
  312. default:
  313. retval = UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  314. }
  315. /* Return error code when reading has failed */
  316. if(retval != UA_STATUSCODE_GOOD) {
  317. v->hasStatus = true;
  318. v->status = retval;
  319. return;
  320. }
  321. v->hasValue = true;
  322. /* Create server timestamp */
  323. if(timestampsToReturn == UA_TIMESTAMPSTORETURN_SERVER ||
  324. timestampsToReturn == UA_TIMESTAMPSTORETURN_BOTH) {
  325. v->serverTimestamp = UA_DateTime_now();
  326. v->hasServerTimestamp = true;
  327. }
  328. /* Handle source time stamp */
  329. if(id->attributeId == UA_ATTRIBUTEID_VALUE) {
  330. if(timestampsToReturn == UA_TIMESTAMPSTORETURN_SERVER ||
  331. timestampsToReturn == UA_TIMESTAMPSTORETURN_NEITHER) {
  332. v->hasSourceTimestamp = false;
  333. v->hasSourcePicoseconds = false;
  334. } else if(!v->hasSourceTimestamp) {
  335. v->sourceTimestamp = UA_DateTime_now();
  336. v->hasSourceTimestamp = true;
  337. }
  338. }
  339. }
  340. static UA_StatusCode
  341. Operation_Read(UA_Server *server, UA_Session *session, UA_MessageContext *mc,
  342. UA_TimestampsToReturn timestampsToReturn, const UA_ReadValueId *id) {
  343. UA_DataValue dv;
  344. UA_DataValue_init(&dv);
  345. /* Get the node */
  346. const UA_Node *node = UA_Nodestore_get(server, &id->nodeId);
  347. /* Perform the read operation */
  348. if(node) {
  349. Read(node, server, session, timestampsToReturn, id, &dv);
  350. } else {
  351. dv.hasStatus = true;
  352. dv.status = UA_STATUSCODE_BADNODEIDUNKNOWN;
  353. }
  354. /* Encode (and send) the results */
  355. UA_StatusCode retval = UA_MessageContext_encode(mc, &dv, &UA_TYPES[UA_TYPES_DATAVALUE]);
  356. /* Free copied data and release the node */
  357. UA_Variant_deleteMembers(&dv.value);
  358. UA_Nodestore_release(server, node);
  359. return retval;
  360. }
  361. UA_StatusCode Service_Read(UA_Server *server, UA_Session *session, UA_MessageContext *mc,
  362. const UA_ReadRequest *request, UA_ResponseHeader *responseHeader) {
  363. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  364. "Processing ReadRequest");
  365. /* Check if the timestampstoreturn is valid */
  366. if(request->timestampsToReturn > UA_TIMESTAMPSTORETURN_NEITHER)
  367. responseHeader->serviceResult = UA_STATUSCODE_BADTIMESTAMPSTORETURNINVALID;
  368. if(request->nodesToReadSize == 0)
  369. responseHeader->serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  370. /* Check if maxAge is valid */
  371. if(request->maxAge < 0)
  372. responseHeader->serviceResult = UA_STATUSCODE_BADMAXAGEINVALID;
  373. /* Check if there are too many operations */
  374. if(server->config.maxNodesPerRead != 0 &&
  375. request->nodesToReadSize > server->config.maxNodesPerRead)
  376. responseHeader->serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  377. /* Encode the response header */
  378. UA_StatusCode retval =
  379. UA_MessageContext_encode(mc, responseHeader, &UA_TYPES[UA_TYPES_RESPONSEHEADER]);
  380. if(retval != UA_STATUSCODE_GOOD)
  381. return retval;
  382. /* Process nothing if we return an error code for the entire service */
  383. UA_Int32 arraySize = (UA_Int32)request->nodesToReadSize;
  384. if(responseHeader->serviceResult != UA_STATUSCODE_GOOD)
  385. arraySize = 0;
  386. /* Process all ReadValueIds */
  387. retval = UA_MessageContext_encode(mc, &arraySize, &UA_TYPES[UA_TYPES_INT32]);
  388. if(retval != UA_STATUSCODE_GOOD)
  389. return retval;
  390. for(UA_Int32 i = 0; i < arraySize; i++) {
  391. retval = Operation_Read(server, session, mc, request->timestampsToReturn,
  392. &request->nodesToRead[i]);
  393. if(retval != UA_STATUSCODE_GOOD)
  394. return retval;
  395. }
  396. /* Don't return any DiagnosticInfo */
  397. arraySize = -1;
  398. return UA_MessageContext_encode(mc, &arraySize, &UA_TYPES[UA_TYPES_INT32]);
  399. }
  400. UA_DataValue
  401. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  402. const UA_ReadValueId *item,
  403. UA_TimestampsToReturn timestampsToReturn) {
  404. UA_DataValue dv;
  405. UA_DataValue_init(&dv);
  406. /* Get the node */
  407. const UA_Node *node = UA_Nodestore_get(server, &item->nodeId);
  408. if(!node) {
  409. dv.hasStatus = true;
  410. dv.status = UA_STATUSCODE_BADNODEIDUNKNOWN;
  411. return dv;
  412. }
  413. /* Perform the read operation */
  414. Read(node, server, session, timestampsToReturn, item, &dv);
  415. /* Do we have to copy the result before releasing the node? */
  416. if(dv.hasValue && dv.value.storageType == UA_VARIANT_DATA_NODELETE) {
  417. UA_DataValue dv2;
  418. UA_StatusCode retval = UA_DataValue_copy(&dv, &dv2);
  419. if(retval == UA_STATUSCODE_GOOD) {
  420. dv = dv2;
  421. } else {
  422. UA_DataValue_init(&dv);
  423. dv.hasStatus = true;
  424. dv.status = retval;
  425. }
  426. }
  427. /* Release the node and return */
  428. UA_Nodestore_release(server, node);
  429. return dv;
  430. }
  431. /* Exposes the Read service to local users */
  432. UA_DataValue
  433. UA_Server_read(UA_Server *server, const UA_ReadValueId *item,
  434. UA_TimestampsToReturn timestamps) {
  435. return UA_Server_readWithSession(server, &adminSession, item, timestamps);
  436. }
  437. /* Used in inline functions exposing the Read service with more syntactic sugar
  438. * for individual attributes */
  439. UA_StatusCode
  440. __UA_Server_read(UA_Server *server, const UA_NodeId *nodeId,
  441. const UA_AttributeId attributeId, void *v) {
  442. /* Call the read service */
  443. UA_ReadValueId item;
  444. UA_ReadValueId_init(&item);
  445. item.nodeId = *nodeId;
  446. item.attributeId = attributeId;
  447. UA_DataValue dv = UA_Server_read(server, &item, UA_TIMESTAMPSTORETURN_NEITHER);
  448. /* Check the return value */
  449. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  450. if(dv.hasStatus)
  451. retval = dv.status;
  452. else if(!dv.hasValue)
  453. retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
  454. if(retval != UA_STATUSCODE_GOOD) {
  455. UA_DataValue_deleteMembers(&dv);
  456. return retval;
  457. }
  458. if(attributeId == UA_ATTRIBUTEID_VALUE ||
  459. attributeId == UA_ATTRIBUTEID_ARRAYDIMENSIONS) {
  460. /* Return the entire variant */
  461. memcpy(v, &dv.value, sizeof(UA_Variant));
  462. } else {
  463. /* Return the variant content only */
  464. memcpy(v, dv.value.data, dv.value.type->memSize);
  465. UA_free(dv.value.data);
  466. }
  467. return retval;
  468. }
  469. /*****************/
  470. /* Type Checking */
  471. /*****************/
  472. enum type_equivalence {
  473. TYPE_EQUIVALENCE_NONE,
  474. TYPE_EQUIVALENCE_ENUM,
  475. TYPE_EQUIVALENCE_OPAQUE
  476. };
  477. static enum type_equivalence
  478. typeEquivalence(const UA_DataType *t) {
  479. if(t->membersSize != 1 || !t->members[0].namespaceZero)
  480. return TYPE_EQUIVALENCE_NONE;
  481. if(t->members[0].memberTypeIndex == UA_TYPES_INT32)
  482. return TYPE_EQUIVALENCE_ENUM;
  483. if(t->members[0].memberTypeIndex == UA_TYPES_BYTE && t->members[0].isArray)
  484. return TYPE_EQUIVALENCE_OPAQUE;
  485. return TYPE_EQUIVALENCE_NONE;
  486. }
  487. const UA_NodeId subtypeId = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_HASSUBTYPE}};
  488. static const UA_NodeId enumNodeId = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_ENUMERATION}};
  489. UA_Boolean
  490. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  491. const UA_NodeId *constraintDataType, UA_Boolean isValue) {
  492. /* Do not allow empty datatypes */
  493. if(UA_NodeId_isNull(dataType))
  494. return false;
  495. /* No constraint (TODO: use variant instead) */
  496. if(UA_NodeId_isNull(constraintDataType))
  497. return true;
  498. /* Same datatypes */
  499. if (UA_NodeId_equal(dataType, constraintDataType))
  500. return true;
  501. /* Variant allows any subtype */
  502. if(UA_NodeId_equal(constraintDataType, &UA_TYPES[UA_TYPES_VARIANT].typeId))
  503. return true;
  504. /* Is the value-type a subtype of the required type? */
  505. if(isNodeInTree(&server->config.nodestore, dataType, constraintDataType, &subtypeId, 1))
  506. return true;
  507. /* Enum allows Int32 (only) */
  508. if(UA_NodeId_equal(dataType, &UA_TYPES[UA_TYPES_INT32].typeId) &&
  509. isNodeInTree(&server->config.nodestore, constraintDataType, &enumNodeId, &subtypeId, 1))
  510. return true;
  511. /* More checks for the data type of real values (variants) */
  512. if(isValue) {
  513. /* If value is a built-in type: The target data type may be a sub type of
  514. * the built-in type. (e.g. UtcTime is sub-type of DateTime and has a
  515. * DateTime value). A type is builtin if its NodeId is in Namespace 0 and
  516. * has a numeric identifier <= 25 (DiagnosticInfo) */
  517. if(dataType->namespaceIndex == 0 &&
  518. dataType->identifierType == UA_NODEIDTYPE_NUMERIC &&
  519. dataType->identifier.numeric <= 25 &&
  520. isNodeInTree(&server->config.nodestore, constraintDataType,
  521. dataType, &subtypeId, 1))
  522. return true;
  523. }
  524. return false;
  525. }
  526. /* Test whether a valurank and the given arraydimensions are compatible. zero
  527. * array dimensions indicate a scalar */
  528. UA_Boolean
  529. compatibleValueRankArrayDimensions(UA_Int32 valueRank, size_t arrayDimensionsSize) {
  530. switch(valueRank) {
  531. case -3: /* the value can be a scalar or a one dimensional array */
  532. if(arrayDimensionsSize > 1)
  533. return false;
  534. break;
  535. case -2: /* the value can be a scalar or an array with any number of dimensions */
  536. break;
  537. case -1: /* the value is a scalar */
  538. if(arrayDimensionsSize > 0)
  539. return false;
  540. break;
  541. case 0: /* the value is an array with one or more dimensions */
  542. if(arrayDimensionsSize < 1)
  543. return false;
  544. break;
  545. default: /* >= 1: the value is an array with the specified number of dimensions */
  546. if(valueRank < (UA_Int32) 0)
  547. return false;
  548. /* Must hold if the array has a defined length. Null arrays (length -1)
  549. * need to be caught before. */
  550. if(arrayDimensionsSize != (size_t)valueRank)
  551. return false;
  552. }
  553. return true;
  554. }
  555. UA_Boolean
  556. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank) {
  557. /* Check if the valuerank of the variabletype allows the change. */
  558. switch(constraintValueRank) {
  559. case -3: /* the value can be a scalar or a one dimensional array */
  560. if(valueRank != -1 && valueRank != 1)
  561. return false;
  562. break;
  563. case -2: /* the value can be a scalar or an array with any number of dimensions */
  564. break;
  565. case -1: /* the value is a scalar */
  566. if(valueRank != -1)
  567. return false;
  568. break;
  569. case 0: /* the value is an array with one or more dimensions */
  570. if(valueRank < (UA_Int32) 0)
  571. return false;
  572. break;
  573. default: /* >= 1: the value is an array with the specified number of dimensions */
  574. if(valueRank != constraintValueRank)
  575. return false;
  576. break;
  577. }
  578. return true;
  579. }
  580. /* Check if the valuerank allows for the value dimension */
  581. static UA_Boolean
  582. compatibleValueRankValue(UA_Int32 valueRank, const UA_Variant *value) {
  583. /* empty arrays (-1) always match */
  584. if(!value->data)
  585. return false;
  586. size_t arrayDims = value->arrayDimensionsSize;
  587. if(!UA_Variant_isScalar(value))
  588. arrayDims = 1; /* array but no arraydimensions -> implicit array dimension 1 */
  589. return compatibleValueRankArrayDimensions(valueRank, arrayDims);
  590. }
  591. UA_Boolean
  592. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  593. const UA_UInt32 *constraintArrayDimensions,
  594. size_t testArrayDimensionsSize,
  595. const UA_UInt32 *testArrayDimensions) {
  596. /* No array dimensions defined -> everything is permitted if the value rank fits */
  597. if(constraintArrayDimensionsSize == 0)
  598. return true;
  599. /* Dimension count must match */
  600. if(testArrayDimensionsSize != constraintArrayDimensionsSize)
  601. return false;
  602. /* Dimension lengths must match; zero in the constraint is a wildcard */
  603. for(size_t i = 0; i < constraintArrayDimensionsSize; ++i) {
  604. if(constraintArrayDimensions[i] != testArrayDimensions[i] &&
  605. constraintArrayDimensions[i] != 0)
  606. return false;
  607. }
  608. return true;
  609. }
  610. UA_Boolean
  611. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  612. const UA_UInt32 *targetArrayDimensions) {
  613. size_t valueArrayDimensionsSize = value->arrayDimensionsSize;
  614. UA_UInt32 *valueArrayDimensions = value->arrayDimensions;
  615. UA_UInt32 tempArrayDimensions;
  616. if(valueArrayDimensions == 0 && !UA_Variant_isScalar(value)) {
  617. valueArrayDimensionsSize = 1;
  618. tempArrayDimensions = (UA_UInt32)value->arrayLength;
  619. valueArrayDimensions = &tempArrayDimensions;
  620. }
  621. return compatibleArrayDimensions(targetArrayDimensionsSize, targetArrayDimensions,
  622. valueArrayDimensionsSize, valueArrayDimensions);
  623. }
  624. UA_Boolean
  625. compatibleValue(UA_Server *server, const UA_NodeId *targetDataTypeId,
  626. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  627. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  628. const UA_NumericRange *range) {
  629. /* Empty variant is only allowed for BaseDataType */
  630. if(!value->type) {
  631. if(UA_NodeId_equal(targetDataTypeId, &UA_TYPES[UA_TYPES_VARIANT].typeId) ||
  632. UA_NodeId_equal(targetDataTypeId, &UA_NODEID_NULL))
  633. return true;
  634. UA_LOG_INFO(server->config.logger, UA_LOGCATEGORY_SERVER,
  635. "Only Variables with data type BaseDataType may contain "
  636. "a null (empty) value");
  637. return false;
  638. }
  639. /* Has the value a subtype of the required type? BaseDataType (Variant) can
  640. * be anything... */
  641. if(!compatibleDataType(server, &value->type->typeId, targetDataTypeId, true))
  642. return false;
  643. /* Array dimensions are checked later when writing the range */
  644. if(range)
  645. return true;
  646. /* See if the array dimensions match. */
  647. if(!compatibleValueArrayDimensions(value, targetArrayDimensionsSize, targetArrayDimensions))
  648. return false;
  649. /* Check if the valuerank allows for the value dimension */
  650. return compatibleValueRankValue(targetValueRank, value);
  651. }
  652. /*****************/
  653. /* Write Service */
  654. /*****************/
  655. static void
  656. adjustValue(UA_Server *server, UA_Variant *value,
  657. const UA_NodeId *targetDataTypeId) {
  658. const UA_DataType *targetDataType = UA_findDataType(targetDataTypeId);
  659. if(!targetDataType)
  660. return;
  661. /* A string is written to a byte array. the valuerank and array dimensions
  662. * are checked later */
  663. if(targetDataType == &UA_TYPES[UA_TYPES_BYTE] &&
  664. value->type == &UA_TYPES[UA_TYPES_BYTESTRING] &&
  665. UA_Variant_isScalar(value)) {
  666. UA_ByteString *str = (UA_ByteString*)value->data;
  667. value->type = &UA_TYPES[UA_TYPES_BYTE];
  668. value->arrayLength = str->length;
  669. value->data = str->data;
  670. return;
  671. }
  672. /* An enum was sent as an int32, or an opaque type as a bytestring. This
  673. * is detected with the typeIndex indicating the "true" datatype. */
  674. enum type_equivalence te1 = typeEquivalence(targetDataType);
  675. enum type_equivalence te2 = typeEquivalence(value->type);
  676. if(te1 != TYPE_EQUIVALENCE_NONE && te1 == te2) {
  677. value->type = targetDataType;
  678. return;
  679. }
  680. /* No more possible equivalencies */
  681. }
  682. /* Stack layout: ... | node | type */
  683. static UA_StatusCode
  684. writeArrayDimensionsAttribute(UA_Server *server, UA_Session *session,
  685. UA_VariableNode *node, const UA_VariableTypeNode *type,
  686. size_t arrayDimensionsSize, UA_UInt32 *arrayDimensions) {
  687. UA_assert(node != NULL);
  688. UA_assert(type != NULL);
  689. /* If this is a variabletype, there must be no instances or subtypes of it
  690. * when we do the change */
  691. if(node->nodeClass == UA_NODECLASS_VARIABLETYPE &&
  692. UA_Node_hasSubTypeOrInstances((UA_Node*)node)) {
  693. UA_LOG_INFO(server->config.logger, UA_LOGCATEGORY_SERVER,
  694. "Cannot change a variable type with existing instances");
  695. return UA_STATUSCODE_BADINTERNALERROR;
  696. }
  697. /* Check that the array dimensions match with the valuerank */
  698. if(!compatibleValueRankArrayDimensions(node->valueRank, arrayDimensionsSize)) {
  699. UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER,
  700. "The current value rank does not match the new array dimensions");
  701. return UA_STATUSCODE_BADTYPEMISMATCH;
  702. }
  703. /* Check if the array dimensions match with the wildcards in the
  704. * variabletype (dimension length 0) */
  705. if(type->arrayDimensions &&
  706. !compatibleArrayDimensions(type->arrayDimensionsSize, type->arrayDimensions,
  707. arrayDimensionsSize, arrayDimensions)) {
  708. UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER,
  709. "Array dimensions in the variable type do not match");
  710. return UA_STATUSCODE_BADTYPEMISMATCH;
  711. }
  712. /* Check if the current value is compatible with the array dimensions */
  713. UA_DataValue value;
  714. UA_DataValue_init(&value);
  715. UA_StatusCode retval = readValueAttribute(server, session, node, &value);
  716. if(retval != UA_STATUSCODE_GOOD)
  717. return retval;
  718. if(value.hasValue) {
  719. if(!compatibleValueArrayDimensions(&value.value, arrayDimensionsSize, arrayDimensions))
  720. retval = UA_STATUSCODE_BADTYPEMISMATCH;
  721. UA_DataValue_deleteMembers(&value);
  722. if(retval != UA_STATUSCODE_GOOD) {
  723. UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER,
  724. "Array dimensions in the current value do not match");
  725. return retval;
  726. }
  727. }
  728. /* Ok, apply */
  729. UA_UInt32 *oldArrayDimensions = node->arrayDimensions;
  730. size_t oldArrayDimensionsSize = node->arrayDimensionsSize;
  731. retval = UA_Array_copy(arrayDimensions, arrayDimensionsSize,
  732. (void**)&node->arrayDimensions,
  733. &UA_TYPES[UA_TYPES_UINT32]);
  734. if(retval != UA_STATUSCODE_GOOD)
  735. return retval;
  736. UA_Array_delete(oldArrayDimensions, oldArrayDimensionsSize, &UA_TYPES[UA_TYPES_UINT32]);
  737. node->arrayDimensionsSize = arrayDimensionsSize;
  738. return UA_STATUSCODE_GOOD;
  739. }
  740. /* Stack layout: ... | node | type */
  741. static UA_StatusCode
  742. writeValueRankAttribute(UA_Server *server, UA_Session *session,
  743. UA_VariableNode *node, const UA_VariableTypeNode *type,
  744. UA_Int32 valueRank) {
  745. UA_assert(node != NULL);
  746. UA_assert(type != NULL);
  747. UA_Int32 constraintValueRank = type->valueRank;
  748. /* If this is a variabletype, there must be no instances or subtypes of it
  749. * when we do the change */
  750. if(node->nodeClass == UA_NODECLASS_VARIABLETYPE &&
  751. UA_Node_hasSubTypeOrInstances((const UA_Node*)node))
  752. return UA_STATUSCODE_BADINTERNALERROR;
  753. /* Check if the valuerank of the variabletype allows the change. */
  754. if(!compatibleValueRanks(valueRank, constraintValueRank))
  755. return UA_STATUSCODE_BADTYPEMISMATCH;
  756. /* Check if the new valuerank is compatible with the array dimensions. Use
  757. * the read service to handle data sources. */
  758. size_t arrayDims = node->arrayDimensionsSize;
  759. if(arrayDims == 0) {
  760. /* the value could be an array with no arrayDimensions defined.
  761. dimensions zero indicate a scalar for compatibleValueRankArrayDimensions. */
  762. UA_DataValue value;
  763. UA_DataValue_init(&value);
  764. UA_StatusCode retval = readValueAttribute(server, session, node, &value);
  765. if(retval != UA_STATUSCODE_GOOD)
  766. return retval;
  767. if(!value.hasValue || !value.value.type) {
  768. /* no value -> apply */
  769. node->valueRank = valueRank;
  770. return UA_STATUSCODE_GOOD;
  771. }
  772. if(!UA_Variant_isScalar(&value.value))
  773. arrayDims = 1;
  774. UA_DataValue_deleteMembers(&value);
  775. }
  776. if(!compatibleValueRankArrayDimensions(valueRank, arrayDims))
  777. return UA_STATUSCODE_BADTYPEMISMATCH;
  778. /* All good, apply the change */
  779. node->valueRank = valueRank;
  780. return UA_STATUSCODE_GOOD;
  781. }
  782. static UA_StatusCode
  783. writeDataTypeAttribute(UA_Server *server, UA_Session *session,
  784. UA_VariableNode *node, const UA_VariableTypeNode *type,
  785. const UA_NodeId *dataType) {
  786. UA_assert(node != NULL);
  787. UA_assert(type != NULL);
  788. /* If this is a variabletype, there must be no instances or subtypes of it
  789. when we do the change */
  790. if(node->nodeClass == UA_NODECLASS_VARIABLETYPE &&
  791. UA_Node_hasSubTypeOrInstances((const UA_Node*)node))
  792. return UA_STATUSCODE_BADINTERNALERROR;
  793. /* Does the new type match the constraints of the variabletype? */
  794. if(!compatibleDataType(server, dataType, &type->dataType, false))
  795. return UA_STATUSCODE_BADTYPEMISMATCH;
  796. /* Check if the current value would match the new type */
  797. UA_DataValue value;
  798. UA_DataValue_init(&value);
  799. UA_StatusCode retval = readValueAttribute(server, session, node, &value);
  800. if(retval != UA_STATUSCODE_GOOD)
  801. return retval;
  802. if(value.hasValue) {
  803. if(!compatibleValue(server, dataType, node->valueRank,
  804. node->arrayDimensionsSize, node->arrayDimensions,
  805. &value.value, NULL))
  806. retval = UA_STATUSCODE_BADTYPEMISMATCH;
  807. UA_DataValue_deleteMembers(&value);
  808. if(retval != UA_STATUSCODE_GOOD) {
  809. UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER,
  810. "The current value does not match the new data type");
  811. return retval;
  812. }
  813. }
  814. /* Replace the datatype nodeid */
  815. UA_NodeId dtCopy = node->dataType;
  816. retval = UA_NodeId_copy(dataType, &node->dataType);
  817. if(retval != UA_STATUSCODE_GOOD) {
  818. node->dataType = dtCopy;
  819. return retval;
  820. }
  821. UA_NodeId_deleteMembers(&dtCopy);
  822. return UA_STATUSCODE_GOOD;
  823. }
  824. static UA_StatusCode
  825. writeValueAttributeWithoutRange(UA_VariableNode *node, const UA_DataValue *value) {
  826. UA_DataValue new_value;
  827. UA_StatusCode retval = UA_DataValue_copy(value, &new_value);
  828. if(retval != UA_STATUSCODE_GOOD)
  829. return retval;
  830. UA_DataValue_deleteMembers(&node->value.data.value);
  831. node->value.data.value = new_value;
  832. return UA_STATUSCODE_GOOD;
  833. }
  834. static UA_StatusCode
  835. writeValueAttributeWithRange(UA_VariableNode *node, const UA_DataValue *value,
  836. const UA_NumericRange *rangeptr) {
  837. /* Value on both sides? */
  838. if(value->status != node->value.data.value.status ||
  839. !value->hasValue || !node->value.data.value.hasValue)
  840. return UA_STATUSCODE_BADINDEXRANGEINVALID;
  841. /* Make scalar a one-entry array for range matching */
  842. UA_Variant editableValue;
  843. const UA_Variant *v = &value->value;
  844. if(UA_Variant_isScalar(&value->value)) {
  845. editableValue = value->value;
  846. editableValue.arrayLength = 1;
  847. v = &editableValue;
  848. }
  849. /* Check that the type is an exact match and not only "compatible" */
  850. if(!node->value.data.value.value.type || !v->type ||
  851. !UA_NodeId_equal(&node->value.data.value.value.type->typeId,
  852. &v->type->typeId))
  853. return UA_STATUSCODE_BADTYPEMISMATCH;
  854. /* Write the value */
  855. UA_StatusCode retval = UA_Variant_setRangeCopy(&node->value.data.value.value,
  856. v->data, v->arrayLength, *rangeptr);
  857. if(retval != UA_STATUSCODE_GOOD)
  858. return retval;
  859. /* Write the status and timestamps */
  860. node->value.data.value.hasStatus = value->hasStatus;
  861. node->value.data.value.status = value->status;
  862. node->value.data.value.hasSourceTimestamp = value->hasSourceTimestamp;
  863. node->value.data.value.sourceTimestamp = value->sourceTimestamp;
  864. node->value.data.value.hasSourcePicoseconds = value->hasSourcePicoseconds;
  865. node->value.data.value.sourcePicoseconds = value->sourcePicoseconds;
  866. return UA_STATUSCODE_GOOD;
  867. }
  868. /* Stack layout: ... | node */
  869. static UA_StatusCode
  870. writeValueAttribute(UA_Server *server, UA_Session *session,
  871. UA_VariableNode *node, const UA_DataValue *value,
  872. const UA_String *indexRange) {
  873. UA_assert(node != NULL);
  874. /* Parse the range */
  875. UA_NumericRange range;
  876. UA_NumericRange *rangeptr = NULL;
  877. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  878. if(indexRange && indexRange->length > 0) {
  879. retval = UA_NumericRange_parseFromString(&range, indexRange);
  880. if(retval != UA_STATUSCODE_GOOD)
  881. return retval;
  882. rangeptr = &range;
  883. }
  884. /* Created an editable version. The data is not touched. Only the variant
  885. * "container". */
  886. UA_DataValue adjustedValue = *value;
  887. /* Type checking. May change the type of editableValue */
  888. if(value->hasValue && value->value.type) {
  889. adjustValue(server, &adjustedValue.value, &node->dataType);
  890. /* The value may be an extension object, especially the nodeset compiler
  891. * uses extension objects to write variable values. If value is an
  892. * extension object we check if the current node value is also an
  893. * extension object. */
  894. UA_Boolean compatible;
  895. if(value->value.type->typeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
  896. value->value.type->typeId.identifier.numeric == UA_NS0ID_STRUCTURE) {
  897. const UA_NodeId nodeDataType = UA_NODEID_NUMERIC(0, UA_NS0ID_STRUCTURE);
  898. compatible = compatibleValue(server, &nodeDataType, node->valueRank,
  899. node->arrayDimensionsSize, node->arrayDimensions,
  900. &adjustedValue.value, rangeptr);
  901. } else {
  902. compatible = compatibleValue(server, &node->dataType, node->valueRank,
  903. node->arrayDimensionsSize, node->arrayDimensions,
  904. &adjustedValue.value, rangeptr);
  905. }
  906. if(!compatible) {
  907. if(rangeptr)
  908. UA_free(range.dimensions);
  909. return UA_STATUSCODE_BADTYPEMISMATCH;
  910. }
  911. }
  912. /* Set the source timestamp if there is none */
  913. if(!adjustedValue.hasSourceTimestamp) {
  914. adjustedValue.sourceTimestamp = UA_DateTime_now();
  915. adjustedValue.hasSourceTimestamp = true;
  916. }
  917. /* Ok, do it */
  918. if(node->valueSource == UA_VALUESOURCE_DATA) {
  919. if(!rangeptr)
  920. retval = writeValueAttributeWithoutRange(node, &adjustedValue);
  921. else
  922. retval = writeValueAttributeWithRange(node, &adjustedValue, rangeptr);
  923. /* Callback after writing */
  924. if(retval == UA_STATUSCODE_GOOD && node->value.data.callback.onWrite)
  925. node->value.data.callback.onWrite(server, &session->sessionId,
  926. session->sessionHandle, &node->nodeId,
  927. node->context, rangeptr,
  928. &adjustedValue);
  929. } else {
  930. if(node->value.dataSource.write) {
  931. retval = node->value.dataSource.write(server, &session->sessionId,
  932. session->sessionHandle, &node->nodeId,
  933. node->context, rangeptr, &adjustedValue);
  934. } else {
  935. retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  936. }
  937. }
  938. /* Clean up */
  939. if(rangeptr)
  940. UA_free(range.dimensions);
  941. return retval;
  942. }
  943. static UA_StatusCode
  944. writeIsAbstractAttribute(UA_Node *node, UA_Boolean value) {
  945. switch(node->nodeClass) {
  946. case UA_NODECLASS_OBJECTTYPE:
  947. ((UA_ObjectTypeNode*)node)->isAbstract = value;
  948. break;
  949. case UA_NODECLASS_REFERENCETYPE:
  950. ((UA_ReferenceTypeNode*)node)->isAbstract = value;
  951. break;
  952. case UA_NODECLASS_VARIABLETYPE:
  953. ((UA_VariableTypeNode*)node)->isAbstract = value;
  954. break;
  955. case UA_NODECLASS_DATATYPE:
  956. ((UA_DataTypeNode*)node)->isAbstract = value;
  957. break;
  958. default:
  959. return UA_STATUSCODE_BADNODECLASSINVALID;
  960. }
  961. return UA_STATUSCODE_GOOD;
  962. }
  963. /*****************/
  964. /* Write Service */
  965. /*****************/
  966. #define CHECK_DATATYPE_SCALAR(EXP_DT) \
  967. if(!wvalue->value.hasValue || \
  968. &UA_TYPES[UA_TYPES_##EXP_DT] != wvalue->value.value.type || \
  969. !UA_Variant_isScalar(&wvalue->value.value)) { \
  970. retval = UA_STATUSCODE_BADTYPEMISMATCH; \
  971. break; \
  972. }
  973. #define CHECK_DATATYPE_ARRAY(EXP_DT) \
  974. if(!wvalue->value.hasValue || \
  975. &UA_TYPES[UA_TYPES_##EXP_DT] != wvalue->value.value.type || \
  976. UA_Variant_isScalar(&wvalue->value.value)) { \
  977. retval = UA_STATUSCODE_BADTYPEMISMATCH; \
  978. break; \
  979. }
  980. #define CHECK_NODECLASS_WRITE(CLASS) \
  981. if((node->nodeClass & (CLASS)) == 0) { \
  982. retval = UA_STATUSCODE_BADNODECLASSINVALID; \
  983. break; \
  984. }
  985. #define CHECK_USERWRITEMASK(mask) \
  986. if(!(userWriteMask & (mask))) { \
  987. retval = UA_STATUSCODE_BADUSERACCESSDENIED; \
  988. break; \
  989. }
  990. #define GET_NODETYPE \
  991. type = (const UA_VariableTypeNode*) \
  992. getNodeType(server, node); \
  993. if(!type) { \
  994. retval = UA_STATUSCODE_BADTYPEMISMATCH; \
  995. break; \
  996. }
  997. /* This function implements the main part of the write service and operates on a
  998. copy of the node (not in single-threaded mode). */
  999. static UA_StatusCode
  1000. copyAttributeIntoNode(UA_Server *server, UA_Session *session,
  1001. UA_Node *node, const UA_WriteValue *wvalue) {
  1002. const void *value = wvalue->value.value.data;
  1003. UA_UInt32 userWriteMask = getUserWriteMask(server, session, node);
  1004. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1005. const UA_VariableTypeNode *type;
  1006. switch(wvalue->attributeId) {
  1007. case UA_ATTRIBUTEID_NODEID:
  1008. case UA_ATTRIBUTEID_NODECLASS:
  1009. case UA_ATTRIBUTEID_USERWRITEMASK:
  1010. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  1011. case UA_ATTRIBUTEID_USEREXECUTABLE:
  1012. retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  1013. break;
  1014. case UA_ATTRIBUTEID_BROWSENAME:
  1015. CHECK_USERWRITEMASK(UA_WRITEMASK_BROWSENAME);
  1016. CHECK_DATATYPE_SCALAR(QUALIFIEDNAME);
  1017. UA_QualifiedName_deleteMembers(&node->browseName);
  1018. UA_QualifiedName_copy((const UA_QualifiedName *)value, &node->browseName);
  1019. break;
  1020. case UA_ATTRIBUTEID_DISPLAYNAME:
  1021. CHECK_USERWRITEMASK(UA_WRITEMASK_DISPLAYNAME);
  1022. CHECK_DATATYPE_SCALAR(LOCALIZEDTEXT);
  1023. UA_LocalizedText_deleteMembers(&node->displayName);
  1024. UA_LocalizedText_copy((const UA_LocalizedText *)value, &node->displayName);
  1025. break;
  1026. case UA_ATTRIBUTEID_DESCRIPTION:
  1027. CHECK_USERWRITEMASK(UA_WRITEMASK_DESCRIPTION);
  1028. CHECK_DATATYPE_SCALAR(LOCALIZEDTEXT);
  1029. UA_LocalizedText_deleteMembers(&node->description);
  1030. UA_LocalizedText_copy((const UA_LocalizedText *)value, &node->description);
  1031. break;
  1032. case UA_ATTRIBUTEID_WRITEMASK:
  1033. CHECK_USERWRITEMASK(UA_WRITEMASK_WRITEMASK);
  1034. CHECK_DATATYPE_SCALAR(UINT32);
  1035. node->writeMask = *(const UA_UInt32*)value;
  1036. break;
  1037. case UA_ATTRIBUTEID_ISABSTRACT:
  1038. CHECK_USERWRITEMASK(UA_WRITEMASK_ISABSTRACT);
  1039. CHECK_DATATYPE_SCALAR(BOOLEAN);
  1040. retval = writeIsAbstractAttribute(node, *(const UA_Boolean*)value);
  1041. break;
  1042. case UA_ATTRIBUTEID_SYMMETRIC:
  1043. CHECK_NODECLASS_WRITE(UA_NODECLASS_REFERENCETYPE);
  1044. CHECK_USERWRITEMASK(UA_WRITEMASK_SYMMETRIC);
  1045. CHECK_DATATYPE_SCALAR(BOOLEAN);
  1046. ((UA_ReferenceTypeNode*)node)->symmetric = *(const UA_Boolean*)value;
  1047. break;
  1048. case UA_ATTRIBUTEID_INVERSENAME:
  1049. CHECK_NODECLASS_WRITE(UA_NODECLASS_REFERENCETYPE);
  1050. CHECK_USERWRITEMASK(UA_WRITEMASK_INVERSENAME);
  1051. CHECK_DATATYPE_SCALAR(LOCALIZEDTEXT);
  1052. UA_LocalizedText_deleteMembers(&((UA_ReferenceTypeNode*)node)->inverseName);
  1053. UA_LocalizedText_copy((const UA_LocalizedText *)value,
  1054. &((UA_ReferenceTypeNode*)node)->inverseName);
  1055. break;
  1056. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  1057. CHECK_NODECLASS_WRITE(UA_NODECLASS_VIEW);
  1058. CHECK_USERWRITEMASK(UA_WRITEMASK_CONTAINSNOLOOPS);
  1059. CHECK_DATATYPE_SCALAR(BOOLEAN);
  1060. ((UA_ViewNode*)node)->containsNoLoops = *(const UA_Boolean*)value;
  1061. break;
  1062. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  1063. CHECK_NODECLASS_WRITE(UA_NODECLASS_VIEW | UA_NODECLASS_OBJECT);
  1064. CHECK_USERWRITEMASK(UA_WRITEMASK_EVENTNOTIFIER);
  1065. CHECK_DATATYPE_SCALAR(BYTE);
  1066. ((UA_ViewNode*)node)->eventNotifier = *(const UA_Byte*)value;
  1067. break;
  1068. case UA_ATTRIBUTEID_VALUE:
  1069. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  1070. if(node->nodeClass == UA_NODECLASS_VARIABLE) {
  1071. /* The access to a value variable is granted via the AccessLevel
  1072. * and UserAccessLevel attributes */
  1073. UA_Byte accessLevel = getAccessLevel(server, session, (const UA_VariableNode*)node);
  1074. if(!(accessLevel & (UA_ACCESSLEVELMASK_WRITE))) {
  1075. retval = UA_STATUSCODE_BADNOTWRITABLE;
  1076. break;
  1077. }
  1078. accessLevel = getUserAccessLevel(server, session,
  1079. (const UA_VariableNode*)node);
  1080. if(!(accessLevel & (UA_ACCESSLEVELMASK_WRITE))) {
  1081. retval = UA_STATUSCODE_BADUSERACCESSDENIED;
  1082. break;
  1083. }
  1084. } else { /* UA_NODECLASS_VARIABLETYPE */
  1085. CHECK_USERWRITEMASK(UA_WRITEMASK_VALUEFORVARIABLETYPE);
  1086. }
  1087. retval = writeValueAttribute(server, session, (UA_VariableNode*)node,
  1088. &wvalue->value, &wvalue->indexRange);
  1089. break;
  1090. case UA_ATTRIBUTEID_DATATYPE:
  1091. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  1092. CHECK_USERWRITEMASK(UA_WRITEMASK_DATATYPE);
  1093. CHECK_DATATYPE_SCALAR(NODEID);
  1094. GET_NODETYPE
  1095. retval = writeDataTypeAttribute(server, session, (UA_VariableNode*)node,
  1096. type, (const UA_NodeId*)value);
  1097. UA_Nodestore_release(server, (const UA_Node*)type);
  1098. break;
  1099. case UA_ATTRIBUTEID_VALUERANK:
  1100. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  1101. CHECK_USERWRITEMASK(UA_WRITEMASK_VALUERANK);
  1102. CHECK_DATATYPE_SCALAR(INT32);
  1103. GET_NODETYPE
  1104. retval = writeValueRankAttribute(server, session, (UA_VariableNode*)node,
  1105. type, *(const UA_Int32*)value);
  1106. UA_Nodestore_release(server, (const UA_Node*)type);
  1107. break;
  1108. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  1109. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  1110. CHECK_USERWRITEMASK(UA_WRITEMASK_ARRRAYDIMENSIONS);
  1111. CHECK_DATATYPE_ARRAY(UINT32);
  1112. GET_NODETYPE
  1113. retval = writeArrayDimensionsAttribute(server, session, (UA_VariableNode*)node,
  1114. type, wvalue->value.value.arrayLength,
  1115. (UA_UInt32 *)wvalue->value.value.data);
  1116. UA_Nodestore_release(server, (const UA_Node*)type);
  1117. break;
  1118. case UA_ATTRIBUTEID_ACCESSLEVEL:
  1119. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE);
  1120. CHECK_USERWRITEMASK(UA_WRITEMASK_ACCESSLEVEL);
  1121. CHECK_DATATYPE_SCALAR(BYTE);
  1122. ((UA_VariableNode*)node)->accessLevel = *(const UA_Byte*)value;
  1123. break;
  1124. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  1125. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE);
  1126. CHECK_USERWRITEMASK(UA_WRITEMASK_MINIMUMSAMPLINGINTERVAL);
  1127. CHECK_DATATYPE_SCALAR(DOUBLE);
  1128. ((UA_VariableNode*)node)->minimumSamplingInterval = *(const UA_Double*)value;
  1129. break;
  1130. case UA_ATTRIBUTEID_HISTORIZING:
  1131. CHECK_NODECLASS_WRITE(UA_NODECLASS_VARIABLE);
  1132. CHECK_USERWRITEMASK(UA_WRITEMASK_HISTORIZING);
  1133. CHECK_DATATYPE_SCALAR(BOOLEAN);
  1134. ((UA_VariableNode*)node)->historizing = *(const UA_Boolean*)value;
  1135. break;
  1136. case UA_ATTRIBUTEID_EXECUTABLE:
  1137. CHECK_NODECLASS_WRITE(UA_NODECLASS_METHOD);
  1138. CHECK_USERWRITEMASK(UA_WRITEMASK_EXECUTABLE);
  1139. CHECK_DATATYPE_SCALAR(BOOLEAN);
  1140. ((UA_MethodNode*)node)->executable = *(const UA_Boolean*)value;
  1141. break;
  1142. default:
  1143. retval = UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  1144. break;
  1145. }
  1146. if(retval != UA_STATUSCODE_GOOD)
  1147. UA_LOG_INFO_SESSION(server->config.logger, session,
  1148. "WriteRequest returned status code %s",
  1149. UA_StatusCode_name(retval));
  1150. return retval;
  1151. }
  1152. static void
  1153. Operation_Write(UA_Server *server, UA_Session *session, void *context,
  1154. UA_WriteValue *wv, UA_StatusCode *result) {
  1155. *result = UA_Server_editNode(server, session, &wv->nodeId,
  1156. (UA_EditNodeCallback)copyAttributeIntoNode, wv);
  1157. }
  1158. void
  1159. Service_Write(UA_Server *server, UA_Session *session,
  1160. const UA_WriteRequest *request,
  1161. UA_WriteResponse *response) {
  1162. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  1163. "Processing WriteRequest");
  1164. if(server->config.maxNodesPerWrite != 0 &&
  1165. request->nodesToWriteSize > server->config.maxNodesPerWrite) {
  1166. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  1167. return;
  1168. }
  1169. response->responseHeader.serviceResult =
  1170. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_Write, NULL,
  1171. &request->nodesToWriteSize, &UA_TYPES[UA_TYPES_WRITEVALUE],
  1172. &response->resultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  1173. }
  1174. UA_StatusCode
  1175. UA_Server_write(UA_Server *server, const UA_WriteValue *value) {
  1176. UA_StatusCode retval =
  1177. UA_Server_editNode(server, &adminSession, &value->nodeId,
  1178. (UA_EditNodeCallback)copyAttributeIntoNode,
  1179. /* casting away const qualifier because callback uses const anyway */
  1180. (UA_WriteValue *)(uintptr_t)value);
  1181. return retval;
  1182. }
  1183. /* Convenience function to be wrapped into inline functions */
  1184. UA_StatusCode
  1185. __UA_Server_write(UA_Server *server, const UA_NodeId *nodeId,
  1186. const UA_AttributeId attributeId,
  1187. const UA_DataType *attr_type,
  1188. const void *attr) {
  1189. UA_WriteValue wvalue;
  1190. UA_WriteValue_init(&wvalue);
  1191. wvalue.nodeId = *nodeId;
  1192. wvalue.attributeId = attributeId;
  1193. wvalue.value.hasValue = true;
  1194. if(attr_type != &UA_TYPES[UA_TYPES_VARIANT]) {
  1195. /* hacked cast. the target WriteValue is used as const anyway */
  1196. UA_Variant_setScalar(&wvalue.value.value,
  1197. (void*)(uintptr_t)attr, attr_type);
  1198. } else {
  1199. wvalue.value.value = *(const UA_Variant*)attr;
  1200. }
  1201. return UA_Server_write(server, &wvalue);
  1202. }