ua_services_attribute.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #include "ua_server_internal.h"
  2. #include "ua_types_generated.h"
  3. #include "ua_services.h"
  4. #include "ua_statuscodes.h"
  5. #include "ua_nodestore.h"
  6. #include "ua_util.h"
  7. #ifndef BUILD_UNIT_TESTS
  8. static
  9. #endif
  10. UA_StatusCode parse_numericrange(const UA_String str, UA_NumericRange *range) {
  11. if(str.length < 0 || str.length >= 1023)
  12. return UA_STATUSCODE_BADINTERNALERROR;
  13. #ifdef NO_ALLOCA
  14. char cstring[str.length+1];
  15. #else
  16. char *cstring = UA_alloca(str.length+1);
  17. #endif
  18. UA_memcpy(cstring, str.data, str.length);
  19. cstring[str.length] = 0;
  20. UA_Int32 index = 0;
  21. size_t dimensionsIndex = 0;
  22. size_t dimensionsMax = 3; // more should be uncommon, realloc if necessary
  23. struct UA_NumericRangeDimension *dimensions = UA_malloc(sizeof(struct UA_NumericRangeDimension) * 3);
  24. if(!dimensions)
  25. return UA_STATUSCODE_BADOUTOFMEMORY;
  26. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  27. do {
  28. UA_Int32 min, max;
  29. UA_Int32 progress;
  30. UA_Int32 res = sscanf(&cstring[index], "%" SCNu32 "%n", &min, &progress);
  31. if(res <= 0 || min < 0) {
  32. retval = UA_STATUSCODE_BADINDEXRANGEINVALID;
  33. break;
  34. }
  35. index += progress;
  36. if(index >= str.length || cstring[index] == ',')
  37. max = min;
  38. else {
  39. res = sscanf(&cstring[index], ":%" SCNu32 "%n", &max, &progress);
  40. if(res <= 0 || max < 0 || min >= max) {
  41. retval = UA_STATUSCODE_BADINDEXRANGEINVALID;
  42. break;
  43. }
  44. index += progress;
  45. }
  46. if(dimensionsIndex >= dimensionsMax) {
  47. struct UA_NumericRangeDimension *newDimensions =
  48. UA_realloc(dimensions, sizeof(struct UA_NumericRangeDimension) * 2 * dimensionsMax);
  49. if(!newDimensions) {
  50. UA_free(dimensions);
  51. return UA_STATUSCODE_BADOUTOFMEMORY;
  52. }
  53. dimensions = newDimensions;
  54. dimensionsMax *= 2;
  55. }
  56. dimensions[dimensionsIndex].min = min;
  57. dimensions[dimensionsIndex].max = max;
  58. dimensionsIndex++;
  59. } while(retval == UA_STATUSCODE_GOOD && index + 1 < str.length && cstring[index] == ',' && ++index);
  60. if(retval != UA_STATUSCODE_GOOD) {
  61. UA_free(dimensions);
  62. return retval;
  63. }
  64. range->dimensions = dimensions;
  65. range->dimensionsSize = dimensionsIndex;
  66. return retval;
  67. }
  68. #define CHECK_NODECLASS(CLASS) \
  69. if(!(node->nodeClass & (CLASS))) { \
  70. v->hasStatus = UA_TRUE; \
  71. v->status = UA_STATUSCODE_BADATTRIBUTEIDINVALID; \
  72. break; \
  73. }
  74. static void handleServerTimestamps(UA_TimestampsToReturn timestamps, UA_DataValue* v) {
  75. if (v && (timestamps == UA_TIMESTAMPSTORETURN_SERVER
  76. || timestamps == UA_TIMESTAMPSTORETURN_BOTH)) {
  77. v->hasServerTimestamp = UA_TRUE;
  78. v->serverTimestamp = UA_DateTime_now();
  79. }
  80. }
  81. static void handleSourceTimestamps(UA_TimestampsToReturn timestamps, UA_DataValue* v) {
  82. if(timestamps == UA_TIMESTAMPSTORETURN_SOURCE || timestamps == UA_TIMESTAMPSTORETURN_BOTH) {
  83. v->hasSourceTimestamp = UA_TRUE;
  84. v->sourceTimestamp = UA_DateTime_now();
  85. }
  86. }
  87. /** Reads a single attribute from a node in the nodestore. */
  88. #ifndef BUILD_UNIT_TESTS
  89. static
  90. #endif
  91. void readValue(UA_Server *server, UA_TimestampsToReturn timestamps, const UA_ReadValueId *id, UA_DataValue *v) {
  92. UA_String binEncoding = UA_STRING("DefaultBinary");
  93. UA_String xmlEncoding = UA_STRING("DefaultXml");
  94. if(id->dataEncoding.name.length >= 0){
  95. if(!UA_String_equal(&binEncoding, &id->dataEncoding.name) &&
  96. !UA_String_equal(&xmlEncoding, &id->dataEncoding.name)) {
  97. v->hasStatus = UA_TRUE;
  98. v->status = UA_STATUSCODE_BADDATAENCODINGINVALID;
  99. return;
  100. }
  101. }
  102. //index range for a non-value
  103. if(id->indexRange.length >= 0 && id->attributeId != UA_ATTRIBUTEID_VALUE){
  104. v->hasStatus = UA_TRUE;
  105. v->status = UA_STATUSCODE_BADINDEXRANGENODATA;
  106. return;
  107. }
  108. UA_Node const *node = UA_NodeStore_get(server->nodestore, &(id->nodeId));
  109. if(!node) {
  110. v->hasStatus = UA_TRUE;
  111. v->status = UA_STATUSCODE_BADNODEIDUNKNOWN;
  112. return;
  113. }
  114. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  115. switch(id->attributeId) {
  116. case UA_ATTRIBUTEID_NODEID:
  117. v->hasValue = UA_TRUE;
  118. retval |= UA_Variant_setScalarCopy(&v->value, &node->nodeId, &UA_TYPES[UA_TYPES_NODEID]);
  119. break;
  120. case UA_ATTRIBUTEID_NODECLASS:
  121. v->hasValue = UA_TRUE;
  122. retval |= UA_Variant_setScalarCopy(&v->value, &node->nodeClass, &UA_TYPES[UA_TYPES_INT32]);
  123. break;
  124. case UA_ATTRIBUTEID_BROWSENAME:
  125. v->hasValue = UA_TRUE;
  126. retval |= UA_Variant_setScalarCopy(&v->value, &node->browseName, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  127. break;
  128. case UA_ATTRIBUTEID_DISPLAYNAME:
  129. retval |= UA_Variant_setScalarCopy(&v->value, &node->displayName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  130. if(retval == UA_STATUSCODE_GOOD)
  131. v->hasValue = UA_TRUE;
  132. break;
  133. case UA_ATTRIBUTEID_DESCRIPTION:
  134. v->hasValue = UA_TRUE;
  135. retval |= UA_Variant_setScalarCopy(&v->value, &node->description, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  136. break;
  137. case UA_ATTRIBUTEID_WRITEMASK:
  138. v->hasValue = UA_TRUE;
  139. retval |= UA_Variant_setScalarCopy(&v->value, &node->writeMask, &UA_TYPES[UA_TYPES_UINT32]);
  140. break;
  141. case UA_ATTRIBUTEID_USERWRITEMASK:
  142. v->hasValue = UA_TRUE;
  143. retval |= UA_Variant_setScalarCopy(&v->value, &node->userWriteMask, &UA_TYPES[UA_TYPES_UINT32]);
  144. break;
  145. case UA_ATTRIBUTEID_ISABSTRACT:
  146. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE | UA_NODECLASS_OBJECTTYPE | UA_NODECLASS_VARIABLETYPE |
  147. UA_NODECLASS_DATATYPE);
  148. v->hasValue = UA_TRUE;
  149. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_ReferenceTypeNode *)node)->isAbstract,
  150. &UA_TYPES[UA_TYPES_BOOLEAN]);
  151. break;
  152. case UA_ATTRIBUTEID_SYMMETRIC:
  153. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  154. v->hasValue = UA_TRUE;
  155. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_ReferenceTypeNode *)node)->symmetric,
  156. &UA_TYPES[UA_TYPES_BOOLEAN]);
  157. break;
  158. case UA_ATTRIBUTEID_INVERSENAME:
  159. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  160. v->hasValue = UA_TRUE;
  161. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_ReferenceTypeNode *)node)->inverseName,
  162. &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  163. break;
  164. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  165. CHECK_NODECLASS(UA_NODECLASS_VIEW);
  166. v->hasValue = UA_TRUE;
  167. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_ViewNode *)node)->containsNoLoops,
  168. &UA_TYPES[UA_TYPES_BOOLEAN]);
  169. break;
  170. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  171. CHECK_NODECLASS(UA_NODECLASS_VIEW | UA_NODECLASS_OBJECT);
  172. v->hasValue = UA_TRUE;
  173. if(node->nodeClass == UA_NODECLASS_VIEW){
  174. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_ViewNode *)node)->eventNotifier,
  175. &UA_TYPES[UA_TYPES_BYTE]);
  176. } else {
  177. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_ObjectNode *)node)->eventNotifier,
  178. &UA_TYPES[UA_TYPES_BYTE]);
  179. }
  180. break;
  181. case UA_ATTRIBUTEID_VALUE:
  182. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  183. {
  184. if(node->nodeClass != UA_NODECLASS_VARIABLE) {
  185. v->hasValue = UA_FALSE;
  186. handleSourceTimestamps(timestamps, v);
  187. }
  188. UA_NumericRange range;
  189. UA_NumericRange *rangeptr = UA_NULL;
  190. if(id->indexRange.length > 0) {
  191. retval = parse_numericrange(id->indexRange, &range);
  192. if(retval != UA_STATUSCODE_GOOD)
  193. break;
  194. rangeptr = &range;
  195. }
  196. const UA_VariableNode *vn = (const UA_VariableNode*)node;
  197. if(vn->valueSource == UA_VALUESOURCE_VARIANT) {
  198. if(rangeptr)
  199. retval |= UA_Variant_copyRange(&vn->value.variant, &v->value, range);
  200. else
  201. retval |= UA_Variant_copy(&vn->value.variant, &v->value);
  202. if(retval == UA_STATUSCODE_GOOD) {
  203. v->hasValue = UA_TRUE;
  204. handleSourceTimestamps(timestamps, v);
  205. }
  206. } else {
  207. UA_Boolean sourceTimeStamp = (timestamps == UA_TIMESTAMPSTORETURN_SOURCE || timestamps == UA_TIMESTAMPSTORETURN_BOTH);
  208. retval |= vn->value.dataSource.read(vn->value.dataSource.handle, sourceTimeStamp, rangeptr, v);
  209. }
  210. if(rangeptr)
  211. UA_free(range.dimensions);
  212. }
  213. break;
  214. case UA_ATTRIBUTEID_DATATYPE: {
  215. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  216. const UA_VariableNode *vn = (const UA_VariableNode*)node;
  217. if(vn->valueSource == UA_VALUESOURCE_VARIANT)
  218. retval = UA_Variant_setScalarCopy(&v->value, &vn->value.variant.type->typeId, &UA_TYPES[UA_TYPES_NODEID]);
  219. else {
  220. UA_DataValue val;
  221. UA_DataValue_init(&val);
  222. retval = vn->value.dataSource.read(vn->value.dataSource.handle, UA_FALSE, UA_NULL, &val);
  223. if(retval != UA_STATUSCODE_GOOD)
  224. break;
  225. retval = UA_Variant_setScalarCopy(&v->value, &val.value.type->typeId, &UA_TYPES[UA_TYPES_NODEID]);
  226. UA_DataValue_deleteMembers(&val);
  227. }
  228. if(retval == UA_STATUSCODE_GOOD)
  229. v->hasValue = UA_TRUE;
  230. }
  231. break;
  232. case UA_ATTRIBUTEID_VALUERANK:
  233. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  234. v->hasValue = UA_TRUE;
  235. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_VariableTypeNode *)node)->valueRank, &UA_TYPES[UA_TYPES_INT32]);
  236. break;
  237. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  238. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  239. {
  240. const UA_VariableNode *vn = (const UA_VariableNode *)node;
  241. if(vn->valueSource == UA_VALUESOURCE_VARIANT) {
  242. retval = UA_Variant_setArrayCopy(&v->value, vn->value.variant.arrayDimensions, vn->value.variant.arrayDimensionsSize, &UA_TYPES[UA_TYPES_INT32]);
  243. if(retval == UA_STATUSCODE_GOOD)
  244. v->hasValue = UA_TRUE;
  245. } else {
  246. UA_DataValue val;
  247. UA_DataValue_init(&val);
  248. retval |= vn->value.dataSource.read(vn->value.dataSource.handle, UA_FALSE, UA_NULL, &val);
  249. if(retval != UA_STATUSCODE_GOOD)
  250. break;
  251. retval = UA_Variant_setArrayCopy(&v->value, val.value.arrayDimensions, val.value.arrayDimensionsSize, &UA_TYPES[UA_TYPES_INT32]);
  252. UA_DataValue_deleteMembers(&val);
  253. }
  254. }
  255. break;
  256. case UA_ATTRIBUTEID_ACCESSLEVEL:
  257. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  258. v->hasValue = UA_TRUE;
  259. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_VariableNode *)node)->accessLevel, &UA_TYPES[UA_TYPES_BYTE]);
  260. break;
  261. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  262. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  263. v->hasValue = UA_TRUE;
  264. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_VariableNode *)node)->userAccessLevel, &UA_TYPES[UA_TYPES_BYTE]);
  265. break;
  266. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  267. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  268. v->hasValue = UA_TRUE;
  269. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_VariableNode *)node)->minimumSamplingInterval, &UA_TYPES[UA_TYPES_DOUBLE]);
  270. break;
  271. case UA_ATTRIBUTEID_HISTORIZING:
  272. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  273. v->hasValue = UA_TRUE;
  274. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_VariableNode *)node)->historizing, &UA_TYPES[UA_TYPES_BOOLEAN]);
  275. break;
  276. case UA_ATTRIBUTEID_EXECUTABLE:
  277. CHECK_NODECLASS(UA_NODECLASS_METHOD);
  278. v->hasValue = UA_TRUE;
  279. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_MethodNode *)node)->executable, &UA_TYPES[UA_TYPES_BOOLEAN]);
  280. break;
  281. case UA_ATTRIBUTEID_USEREXECUTABLE:
  282. CHECK_NODECLASS(UA_NODECLASS_METHOD);
  283. v->hasValue = UA_TRUE;
  284. retval |= UA_Variant_setScalarCopy(&v->value, &((const UA_MethodNode *)node)->userExecutable, &UA_TYPES[UA_TYPES_BOOLEAN]);
  285. break;
  286. default:
  287. v->hasStatus = UA_TRUE;
  288. v->status = UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  289. break;
  290. }
  291. UA_NodeStore_release(node);
  292. if(retval != UA_STATUSCODE_GOOD) {
  293. v->hasStatus = UA_TRUE;
  294. v->status = retval;
  295. }
  296. handleServerTimestamps(timestamps, v);
  297. }
  298. void Service_Read(UA_Server *server, UA_Session *session, const UA_ReadRequest *request,
  299. UA_ReadResponse *response) {
  300. if(request->nodesToReadSize <= 0) {
  301. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  302. return;
  303. }
  304. if(request->timestampsToReturn > 3){
  305. response->responseHeader.serviceResult = UA_STATUSCODE_BADTIMESTAMPSTORETURNINVALID;
  306. return;
  307. }
  308. size_t size = request->nodesToReadSize;
  309. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_DATAVALUE], size);
  310. if(!response->results) {
  311. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  312. return;
  313. }
  314. response->resultsSize = size;
  315. if(request->maxAge < 0) {
  316. response->responseHeader.serviceResult = UA_STATUSCODE_BADMAXAGEINVALID;
  317. return;
  318. }
  319. #ifdef UA_EXTERNAL_NAMESPACES
  320. #ifdef NO_ALLOCA
  321. UA_Boolean isExternal[size];
  322. UA_UInt32 indices[size];
  323. #else
  324. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * size);
  325. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * size);
  326. #endif /*NO_ALLOCA */
  327. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size);
  328. for(size_t j = 0;j<server->externalNamespacesSize;j++) {
  329. size_t indexSize = 0;
  330. for(size_t i = 0;i < size;i++) {
  331. if(request->nodesToRead[i].nodeId.namespaceIndex != server->externalNamespaces[j].index)
  332. continue;
  333. isExternal[i] = UA_TRUE;
  334. indices[indexSize] = i;
  335. indexSize++;
  336. }
  337. if(indexSize == 0)
  338. continue;
  339. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  340. ens->readNodes(ens->ensHandle, &request->requestHeader, request->nodesToRead,
  341. indices, indexSize, response->results, UA_FALSE, response->diagnosticInfos);
  342. }
  343. #endif
  344. for(size_t i = 0;i < size;i++) {
  345. #ifdef UA_EXTERNAL_NAMESPACES
  346. if(!isExternal[i])
  347. #endif
  348. readValue(server, request->timestampsToReturn, &request->nodesToRead[i], &response->results[i]);
  349. }
  350. #ifdef EXTENSION_STATELESS
  351. if(session==&anonymousSession){
  352. /* expiry header */
  353. UA_ExtensionObject additionalHeader;
  354. UA_ExtensionObject_init(&additionalHeader);
  355. additionalHeader.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  356. UA_Variant variant;
  357. UA_Variant_init(&variant);
  358. variant.type = &UA_TYPES[UA_TYPES_DATETIME];
  359. variant.arrayLength = request->nodesToReadSize;
  360. UA_DateTime* expireArray = UA_NULL;
  361. expireArray = UA_Array_new(&UA_TYPES[UA_TYPES_DATETIME], request->nodesToReadSize);
  362. variant.data = expireArray;
  363. /*expires in 20 seconds*/
  364. for(UA_Int32 i = 0;i < response->resultsSize;i++) {
  365. expireArray[i] = UA_DateTime_now() + 20 * 100 * 1000 * 1000;
  366. }
  367. size_t offset = 0;
  368. UA_Connection *c = UA_NULL;
  369. UA_SecureChannel *sc = session->channel;
  370. if(!sc) {
  371. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  372. return;
  373. }
  374. UA_ByteString str;
  375. UA_ByteString_newMembers(&str, c->remoteConf.maxMessageSize);
  376. UA_Variant_encodeBinary(&variant, &str, &offset);
  377. additionalHeader.body = str;
  378. response->responseHeader.additionalHeader = additionalHeader;
  379. }
  380. #endif
  381. }
  382. #define SETATTRIBUTE_IF_DATATYPE_IS(EXP_DT) \
  383. /* The Userspace setAttribute expects the value being passed being the correct type and has no own means for checking \
  384. We need to check for setAttribute if the dataType is correct \
  385. */ \
  386. expectType = UA_NODEID_NUMERIC(0, UA_NS0ID_##EXP_DT); \
  387. if (! UA_NodeId_equal(&expectType, &wvalue->value.value.type->typeId)) \
  388. retval |= UA_STATUSCODE_BADTYPEMISMATCH; \
  389. else \
  390. retval = UA_Server_setAttributeValue(server, wvalue->nodeId, wvalue->attributeId, (void *) wvalue->value.value.data); \
  391. #ifndef BUILD_UNIT_TESTS
  392. static
  393. #endif
  394. UA_StatusCode writeValue(UA_Server *server, UA_WriteValue *wvalue) {
  395. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  396. /* is there a value at all */
  397. if(!wvalue->value.hasValue)
  398. return UA_STATUSCODE_BADTYPEMISMATCH;
  399. // we might repeat writing, e.g. when the node got replaced mid-work
  400. UA_Boolean done = UA_FALSE;
  401. UA_NodeId expectType;
  402. while(!done) {
  403. const UA_Node *node = UA_NodeStore_get(server->nodestore, &wvalue->nodeId);
  404. if(!node)
  405. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  406. switch(wvalue->attributeId) {
  407. case UA_ATTRIBUTEID_BROWSENAME:
  408. SETATTRIBUTE_IF_DATATYPE_IS(QUALIFIEDNAME)
  409. done = UA_TRUE;
  410. break;
  411. case UA_ATTRIBUTEID_DISPLAYNAME:
  412. SETATTRIBUTE_IF_DATATYPE_IS(LOCALIZEDTEXT)
  413. done = UA_TRUE;
  414. break;
  415. case UA_ATTRIBUTEID_DESCRIPTION:
  416. SETATTRIBUTE_IF_DATATYPE_IS(LOCALIZEDTEXT)
  417. done = UA_TRUE;
  418. break;
  419. case UA_ATTRIBUTEID_WRITEMASK:
  420. SETATTRIBUTE_IF_DATATYPE_IS(UINT32)
  421. done = UA_TRUE;
  422. break;
  423. case UA_ATTRIBUTEID_USERWRITEMASK:
  424. SETATTRIBUTE_IF_DATATYPE_IS(UINT32)
  425. done = UA_TRUE;
  426. break;
  427. case UA_ATTRIBUTEID_ISABSTRACT:
  428. SETATTRIBUTE_IF_DATATYPE_IS(BOOLEAN)
  429. done = UA_TRUE;
  430. break;
  431. case UA_ATTRIBUTEID_SYMMETRIC:
  432. SETATTRIBUTE_IF_DATATYPE_IS(BOOLEAN)
  433. done = UA_TRUE;
  434. break;
  435. case UA_ATTRIBUTEID_INVERSENAME:
  436. SETATTRIBUTE_IF_DATATYPE_IS(LOCALIZEDTEXT)
  437. done = UA_TRUE;
  438. break;
  439. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  440. SETATTRIBUTE_IF_DATATYPE_IS(BOOLEAN)
  441. done = UA_TRUE;
  442. break;
  443. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  444. SETATTRIBUTE_IF_DATATYPE_IS(BYTE)
  445. done = UA_TRUE;
  446. break;
  447. case UA_ATTRIBUTEID_VALUERANK:
  448. SETATTRIBUTE_IF_DATATYPE_IS(INT32)
  449. done = UA_TRUE;
  450. break;
  451. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  452. SETATTRIBUTE_IF_DATATYPE_IS(INT32)
  453. done = UA_TRUE;
  454. break;
  455. case UA_ATTRIBUTEID_ACCESSLEVEL:
  456. SETATTRIBUTE_IF_DATATYPE_IS(UINT32)
  457. done = UA_TRUE;
  458. break;
  459. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  460. SETATTRIBUTE_IF_DATATYPE_IS(UINT32)
  461. done = UA_TRUE;
  462. break;
  463. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  464. SETATTRIBUTE_IF_DATATYPE_IS(DOUBLE)
  465. done = UA_TRUE;
  466. break;
  467. case UA_ATTRIBUTEID_HISTORIZING:
  468. SETATTRIBUTE_IF_DATATYPE_IS(BOOLEAN)
  469. done = UA_TRUE;
  470. break;
  471. case UA_ATTRIBUTEID_EXECUTABLE:
  472. SETATTRIBUTE_IF_DATATYPE_IS(BOOLEAN)
  473. done = UA_TRUE;
  474. break;
  475. case UA_ATTRIBUTEID_USEREXECUTABLE:
  476. SETATTRIBUTE_IF_DATATYPE_IS(BOOLEAN)
  477. done = UA_TRUE;
  478. break;
  479. // The value logic implemented by jpfr is far more advanced that that in the userspace, so we use that
  480. // here
  481. case UA_ATTRIBUTEID_VALUE: {
  482. if(node->nodeClass != UA_NODECLASS_VARIABLE &&
  483. node->nodeClass != UA_NODECLASS_VARIABLETYPE) {
  484. retval = UA_STATUSCODE_BADTYPEMISMATCH;
  485. break;
  486. }
  487. /* parse the range */
  488. UA_Boolean hasRange = UA_FALSE;
  489. UA_NumericRange range;
  490. if(wvalue->indexRange.length > 0) {
  491. retval = parse_numericrange(wvalue->indexRange, &range);
  492. if(retval != UA_STATUSCODE_GOOD)
  493. break;
  494. hasRange = UA_TRUE;
  495. }
  496. /* the relevant members are similar for variables and variabletypes */
  497. const UA_VariableNode *vn = (const UA_VariableNode*)node;
  498. if(vn->valueSource == UA_VALUESOURCE_DATASOURCE) {
  499. if(!vn->value.dataSource.write) {
  500. retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  501. goto clean_up_range;
  502. }
  503. // todo: writing ranges
  504. if(hasRange)
  505. retval = vn->value.dataSource.write(vn->value.dataSource.handle, &wvalue->value.value, &range);
  506. else
  507. retval = vn->value.dataSource.write(vn->value.dataSource.handle, &wvalue->value.value, UA_NULL);
  508. done = UA_TRUE;
  509. goto clean_up_range;
  510. }
  511. const UA_Variant *oldV = &vn->value.variant;
  512. /* the nodeid on the wire may be != the nodeid in the node: opaque types, enums and bytestrings */
  513. if(!UA_NodeId_equal(&oldV->type->typeId, &wvalue->value.value.type->typeId)) {
  514. if(oldV->type->namespaceZero && wvalue->value.value.type->namespaceZero &&
  515. oldV->type->typeIndex == wvalue->value.value.type->typeIndex)
  516. /* An enum was sent as an int32, or an opaque type as a bytestring. This is
  517. detected with the typeIndex indicated the "true" datatype. */
  518. wvalue->value.value.type = oldV->type;
  519. else if(oldV->type == &UA_TYPES[UA_TYPES_BYTE] && !UA_Variant_isScalar(oldV) &&
  520. wvalue->value.value.type == &UA_TYPES[UA_TYPES_BYTESTRING] &&
  521. UA_Variant_isScalar(&wvalue->value.value)) {
  522. /* a string is written to a byte array */
  523. UA_ByteString *str = (UA_ByteString*) wvalue->value.value.data;
  524. wvalue->value.value.arrayLength = str->length;
  525. wvalue->value.value.data = str->data;
  526. wvalue->value.value.type = &UA_TYPES[UA_TYPES_BYTE];
  527. UA_free(str);
  528. } else {
  529. retval = UA_STATUSCODE_BADTYPEMISMATCH;
  530. goto clean_up_range;
  531. }
  532. }
  533. /* copy the node */
  534. UA_VariableNode *newVn = (node->nodeClass == UA_NODECLASS_VARIABLE) ?
  535. UA_VariableNode_new() : (UA_VariableNode*)UA_VariableTypeNode_new();
  536. if(!newVn) {
  537. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  538. goto clean_up_range;
  539. }
  540. retval = (node->nodeClass == UA_NODECLASS_VARIABLE) ? UA_VariableNode_copy(vn, newVn) :
  541. UA_VariableTypeNode_copy((const UA_VariableTypeNode*)vn, (UA_VariableTypeNode*)newVn);
  542. if(retval != UA_STATUSCODE_GOOD)
  543. goto clean_up;
  544. /* insert the new value */
  545. if(hasRange)
  546. retval = UA_Variant_setRangeCopy(&newVn->value.variant, wvalue->value.value.data,
  547. wvalue->value.value.arrayLength, range);
  548. else {
  549. UA_Variant_deleteMembers(&newVn->value.variant);
  550. retval = UA_Variant_copy(&wvalue->value.value, &newVn->value.variant);
  551. }
  552. if(retval == UA_STATUSCODE_GOOD && UA_NodeStore_replace(server->nodestore, node,
  553. (UA_Node*)newVn, UA_NULL) == UA_STATUSCODE_GOOD) {
  554. done = UA_TRUE;
  555. goto clean_up_range;
  556. }
  557. clean_up:
  558. if(node->nodeClass == UA_NODECLASS_VARIABLE)
  559. UA_VariableNode_delete(newVn);
  560. else
  561. UA_VariableTypeNode_delete((UA_VariableTypeNode*)newVn);
  562. clean_up_range:
  563. if(hasRange)
  564. UA_free(range.dimensions);
  565. }
  566. break;
  567. case UA_ATTRIBUTEID_NODEID:
  568. case UA_ATTRIBUTEID_NODECLASS:
  569. case UA_ATTRIBUTEID_DATATYPE:
  570. default:
  571. retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  572. break;
  573. }
  574. UA_NodeStore_release(node);
  575. if(retval != UA_STATUSCODE_GOOD)
  576. break;
  577. }
  578. return retval;
  579. }
  580. void Service_Write(UA_Server *server, UA_Session *session, const UA_WriteRequest *request,
  581. UA_WriteResponse *response) {
  582. UA_assert(server != UA_NULL && session != UA_NULL && request != UA_NULL && response != UA_NULL);
  583. if(request->nodesToWriteSize <= 0){
  584. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  585. return;
  586. }
  587. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_STATUSCODE], request->nodesToWriteSize);
  588. if(!response->results) {
  589. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  590. return;
  591. }
  592. #ifdef UA_EXTERNAL_NAMESPACES
  593. #ifdef NO_ALLOCA
  594. UA_Boolean isExternal[request->nodesToWriteSize];
  595. UA_UInt32 indices[request->nodesToWriteSize];
  596. #else
  597. UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * request->nodesToWriteSize);
  598. UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * request->nodesToWriteSize);
  599. #endif /*NO_ALLOCA */
  600. UA_memset(isExternal, UA_FALSE, sizeof(UA_Boolean)*request->nodesToWriteSize);
  601. for(size_t j = 0; j < server->externalNamespacesSize; j++) {
  602. UA_UInt32 indexSize = 0;
  603. for(UA_Int32 i = 0; i < request->nodesToWriteSize; i++) {
  604. if(request->nodesToWrite[i].nodeId.namespaceIndex !=
  605. server->externalNamespaces[j].index)
  606. continue;
  607. isExternal[i] = UA_TRUE;
  608. indices[indexSize] = i;
  609. indexSize++;
  610. }
  611. if(indexSize == 0)
  612. continue;
  613. UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore;
  614. ens->writeNodes(ens->ensHandle, &request->requestHeader, request->nodesToWrite,
  615. indices, indexSize, response->results, response->diagnosticInfos);
  616. }
  617. #endif
  618. response->resultsSize = request->nodesToWriteSize;
  619. for(UA_Int32 i = 0;i < request->nodesToWriteSize;i++) {
  620. #ifdef UA_EXTERNAL_NAMESPACES
  621. if(!isExternal[i])
  622. #endif
  623. response->results[i] = writeValue(server, &request->nodesToWrite[i]);
  624. }
  625. }