ua_services_attribute.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "ua_services.h"
  2. #include "ua_statuscodes.h"
  3. enum UA_AttributeId {
  4. UA_ATTRIBUTEID_NODEID = 1,
  5. UA_ATTRIBUTEID_NODECLASS = 2,
  6. UA_ATTRIBUTEID_BROWSENAME = 3,
  7. UA_ATTRIBUTEID_DISPLAYNAME = 4,
  8. UA_ATTRIBUTEID_DESCRIPTION = 5,
  9. UA_ATTRIBUTEID_WRITEMASK = 6,
  10. UA_ATTRIBUTEID_USERWRITEMASK = 7,
  11. UA_ATTRIBUTEID_ISABSTRACT = 8,
  12. UA_ATTRIBUTEID_SYMMETRIC = 9,
  13. UA_ATTRIBUTEID_INVERSENAME = 10,
  14. UA_ATTRIBUTEID_CONTAINSNOLOOPS = 11,
  15. UA_ATTRIBUTEID_EVENTNOTIFIER = 12,
  16. UA_ATTRIBUTEID_VALUE = 13,
  17. UA_ATTRIBUTEID_DATATYPE = 14,
  18. UA_ATTRIBUTEID_VALUERANK = 15,
  19. UA_ATTRIBUTEID_ARRAYDIMENSIONS = 16,
  20. UA_ATTRIBUTEID_ACCESSLEVEL = 17,
  21. UA_ATTRIBUTEID_USERACCESSLEVEL = 18,
  22. UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL = 19,
  23. UA_ATTRIBUTEID_HISTORIZING = 20,
  24. UA_ATTRIBUTEID_EXECUTABLE = 21,
  25. UA_ATTRIBUTEID_USEREXECUTABLE = 22
  26. };
  27. #define CHECK_NODECLASS(CLASS) do { \
  28. if((node->nodeClass & (CLASS)) != 0x00) { \
  29. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_STATUSCODE; \
  30. v.status = UA_STATUSCODE_BADNOTREADABLE; \
  31. } \
  32. break; \
  33. } while(0)
  34. static UA_DataValue service_read_node(Application *app, const UA_ReadValueId *id) {
  35. UA_DataValue v;
  36. UA_DataValue_init(&v);
  37. DBG(printf("service_read_node - entered with ns=%d,id=%d,attr=%i\n", id->nodeId.namespace,
  38. id->nodeId.identifier.numeric, id->attributeId));
  39. Namespace *ns = UA_indexedList_findValue(app->namespaces, id->nodeId.namespace);
  40. if(ns == UA_NULL) {
  41. DBG_VERBOSE(printf("service_read_node - unknown namespace %d\n", id->nodeId.namespace));
  42. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_STATUSCODE;
  43. v.status = UA_STATUSCODE_BADNODEIDUNKNOWN;
  44. return v;
  45. }
  46. UA_Node const *node = UA_NULL;
  47. Namespace_Entry_Lock *lock = UA_NULL;
  48. DBG_VERBOSE(UA_NodeId_printf("service_read_node - search for ", &(id->nodeId)));
  49. UA_Int32 result = Namespace_get(ns, &(id->nodeId), &node, &lock);
  50. if(result != UA_SUCCESS || node == UA_NULL) {
  51. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_STATUSCODE;
  52. v.status = UA_STATUSCODE_BADNODEIDUNKNOWN;
  53. return v;
  54. }
  55. DBG_VERBOSE(UA_NodeId_printf("service_read_node - found node=", &(node->nodeId)));
  56. UA_Int32 retval = UA_SUCCESS;
  57. switch(id->attributeId) {
  58. case UA_ATTRIBUTEID_NODEID:
  59. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  60. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_NODEID], &node->nodeId);
  61. break;
  62. case UA_ATTRIBUTEID_NODECLASS:
  63. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  64. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_INT32], &node->nodeClass);
  65. break;
  66. case UA_ATTRIBUTEID_BROWSENAME:
  67. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  68. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_QUALIFIEDNAME], &node->browseName);
  69. break;
  70. case UA_ATTRIBUTEID_DISPLAYNAME:
  71. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  72. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_LOCALIZEDTEXT],
  73. &node->displayName);
  74. break;
  75. case UA_ATTRIBUTEID_DESCRIPTION:
  76. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  77. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_LOCALIZEDTEXT],
  78. &node->description);
  79. break;
  80. case UA_ATTRIBUTEID_WRITEMASK:
  81. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  82. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_UINT32], &node->writeMask);
  83. break;
  84. case UA_ATTRIBUTEID_USERWRITEMASK:
  85. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  86. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_UINT32], &node->userWriteMask);
  87. break;
  88. case UA_ATTRIBUTEID_ISABSTRACT:
  89. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  90. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  91. retval |=
  92. UA_Variant_copySetValue(&v.value, &UA_.types[UA_BOOLEAN],
  93. &((UA_ReferenceTypeNode *)node)->isAbstract);
  94. break;
  95. case UA_ATTRIBUTEID_SYMMETRIC:
  96. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  97. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  98. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BOOLEAN],
  99. &((UA_ReferenceTypeNode *)node)->symmetric);
  100. break;
  101. case UA_ATTRIBUTEID_INVERSENAME:
  102. CHECK_NODECLASS(UA_NODECLASS_REFERENCETYPE);
  103. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  104. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_LOCALIZEDTEXT],
  105. &((UA_ReferenceTypeNode *)node)->inverseName);
  106. break;
  107. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  108. CHECK_NODECLASS(UA_NODECLASS_VIEW);
  109. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  110. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BOOLEAN],
  111. &((UA_ViewNode *)node)->containsNoLoops);
  112. break;
  113. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  114. CHECK_NODECLASS(UA_NODECLASS_VIEW);
  115. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  116. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BYTE],
  117. &((UA_ViewNode *)node)->eventNotifier);
  118. break;
  119. case UA_ATTRIBUTEID_VALUE:
  120. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  121. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  122. // TODO: Ensure that the borrowed value is not freed prematurely (multithreading)
  123. /* retval |= UA_Variant_borrowSetValue(&v.value, &UA_.types[UA_VARIANT], */
  124. /* &((UA_VariableNode *)node)->value); */
  125. retval |= UA_Variant_copy(&((UA_VariableNode *)node)->value, &v.value);
  126. break;
  127. case UA_ATTRIBUTEID_DATATYPE:
  128. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  129. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  130. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_NODEID],
  131. &((UA_VariableTypeNode *)node)->dataType);
  132. break;
  133. case UA_ATTRIBUTEID_VALUERANK:
  134. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  135. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  136. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_INT32],
  137. &((UA_VariableTypeNode *)node)->valueRank);
  138. break;
  139. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  140. CHECK_NODECLASS(UA_NODECLASS_VARIABLE | UA_NODECLASS_VARIABLETYPE);
  141. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  142. UA_Variant_copySetArray(&v.value, &UA_.types[UA_UINT32],
  143. ((UA_VariableTypeNode *)node)->arrayDimensionsSize,
  144. &((UA_VariableTypeNode *)node)->arrayDimensions);
  145. break;
  146. case UA_ATTRIBUTEID_ACCESSLEVEL:
  147. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  148. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  149. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BYTE],
  150. &((UA_VariableNode *)node)->accessLevel);
  151. break;
  152. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  153. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  154. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  155. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BYTE],
  156. &((UA_VariableNode *)node)->userAccessLevel);
  157. break;
  158. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  159. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  160. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  161. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_DOUBLE],
  162. &((UA_VariableNode *)node)->minimumSamplingInterval);
  163. break;
  164. case UA_ATTRIBUTEID_HISTORIZING:
  165. CHECK_NODECLASS(UA_NODECLASS_VARIABLE);
  166. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  167. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BOOLEAN],
  168. &((UA_VariableNode *)node)->historizing);
  169. break;
  170. case UA_ATTRIBUTEID_EXECUTABLE:
  171. CHECK_NODECLASS(UA_NODECLASS_METHOD);
  172. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  173. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BOOLEAN],
  174. &((UA_MethodNode *)node)->executable);
  175. break;
  176. case UA_ATTRIBUTEID_USEREXECUTABLE:
  177. CHECK_NODECLASS(UA_NODECLASS_METHOD);
  178. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_VARIANT;
  179. retval |= UA_Variant_copySetValue(&v.value, &UA_.types[UA_BOOLEAN],
  180. &((UA_MethodNode *)node)->userExecutable);
  181. break;
  182. default:
  183. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_STATUSCODE;
  184. v.status = UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  185. break;
  186. }
  187. Namespace_Entry_Lock_release(lock);
  188. if(retval != UA_SUCCESS) {
  189. v.encodingMask = UA_DATAVALUE_ENCODINGMASK_STATUSCODE;
  190. v.status = UA_STATUSCODE_BADNOTREADABLE;
  191. }
  192. return v;
  193. }
  194. UA_Int32 Service_Read(SL_Channel *channel, const UA_ReadRequest *request,
  195. UA_ReadResponse *response) {
  196. if(channel->session == UA_NULL || channel->session->application == UA_NULL)
  197. return UA_ERROR; // TODO: Return error message
  198. UA_Int32 readsize = request->nodesToReadSize;
  199. /* NothingTodo */
  200. if(readsize <= 0) {
  201. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  202. response->resultsSize = 0;
  203. return UA_SUCCESS;
  204. }
  205. response->resultsSize = readsize;
  206. UA_alloc((void **)&response->results, sizeof(UA_DataValue) * readsize);
  207. for(UA_Int32 i = 0;i < readsize;i++) {
  208. DBG_VERBOSE(printf("service_read - attributeId=%d\n", request->nodesToRead[i].attributeId));
  209. DBG_VERBOSE(UA_NodeId_printf("service_read - nodeId=", &(request->nodesToRead[i].nodeId)));
  210. response->results[i] = service_read_node(channel->session->application,
  211. &request->nodesToRead[i]);
  212. }
  213. response->responseHeader.serviceResult = UA_STATUSCODE_GOOD;
  214. response->diagnosticInfosSize = 0;
  215. return UA_SUCCESS;
  216. }
  217. UA_Int32 Service_Write_writeNode(Application *app, UA_WriteValue *writeValue, UA_StatusCode *result)
  218. {
  219. UA_Int32 retval = UA_SUCCESS;
  220. Namespace *ns = UA_indexedList_findValue(app->namespaces, writeValue->nodeId.namespace);
  221. if(ns==UA_NULL)
  222. {
  223. *result = UA_STATUSCODE_BADNODEIDINVALID;
  224. return UA_ERROR;
  225. }
  226. Namespace_Entry_Lock *lock;
  227. const UA_Node *node;
  228. if(Namespace_get(ns, &writeValue->nodeId,&node, &lock) != UA_SUCCESS){
  229. return UA_ERROR;
  230. }
  231. switch(writeValue->attributeId) {
  232. case UA_ATTRIBUTEID_NODEID:
  233. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){
  234. }
  235. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  236. return UA_ERROR;
  237. break;
  238. case UA_ATTRIBUTEID_NODECLASS:
  239. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){
  240. }
  241. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  242. return UA_ERROR;
  243. break;
  244. case UA_ATTRIBUTEID_BROWSENAME:
  245. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  246. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  247. return UA_ERROR;
  248. break;
  249. case UA_ATTRIBUTEID_DISPLAYNAME:
  250. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){
  251. }
  252. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  253. return UA_ERROR;
  254. break;
  255. case UA_ATTRIBUTEID_DESCRIPTION:
  256. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  257. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  258. return UA_ERROR;
  259. break;
  260. case UA_ATTRIBUTEID_WRITEMASK:
  261. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  262. break;
  263. case UA_ATTRIBUTEID_USERWRITEMASK:
  264. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  265. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  266. return UA_ERROR;
  267. break;
  268. case UA_ATTRIBUTEID_ISABSTRACT:
  269. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  270. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  271. break;
  272. case UA_ATTRIBUTEID_SYMMETRIC:
  273. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  274. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  275. return UA_ERROR;
  276. break;
  277. case UA_ATTRIBUTEID_INVERSENAME:
  278. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  279. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  280. break;
  281. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  282. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  283. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  284. return UA_ERROR;
  285. break;
  286. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  287. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  288. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  289. break;
  290. case UA_ATTRIBUTEID_VALUE:
  291. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){
  292. // TODO: Ensure that the borrowed value is not freed prematurely (multithreading)
  293. /* retval |= UA_Variant_borrowSetValue(&v.value, &UA_.types[UA_VARIANT], */
  294. /* &((UA_VariableNode *)node)->value); */
  295. #ifdef RASPI
  296. //Sten: this is highly hacked to cope with pthreads
  297. //tested only with raspberrypi and boolean
  298. *((UA_Boolean*)((UA_VariableNode *)node)->value.data) = *((UA_Boolean*)(writeValue->value.value.data));
  299. //it seems that UA_Variant_copy copies the value out of the shared memory
  300. #else
  301. retval |= UA_Variant_copy(&writeValue->value.value, &((UA_VariableNode *)node)->value);
  302. #endif
  303. *result = UA_STATUSCODE_GOOD;
  304. }
  305. break;
  306. case UA_ATTRIBUTEID_DATATYPE:
  307. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  308. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  309. break;
  310. case UA_ATTRIBUTEID_VALUERANK:
  311. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  312. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  313. break;
  314. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  315. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  316. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  317. break;
  318. case UA_ATTRIBUTEID_ACCESSLEVEL:
  319. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  320. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  321. break;
  322. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  323. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  324. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  325. return UA_ERROR;
  326. break;
  327. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  328. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  329. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  330. break;
  331. case UA_ATTRIBUTEID_HISTORIZING:
  332. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  333. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  334. break;
  335. case UA_ATTRIBUTEID_EXECUTABLE:
  336. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  337. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  338. break;
  339. case UA_ATTRIBUTEID_USEREXECUTABLE:
  340. if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){}
  341. *result = UA_STATUSCODE_BADWRITENOTSUPPORTED;
  342. break;
  343. default:
  344. *result = UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  345. break;
  346. }
  347. Namespace_Entry_Lock_release(lock);
  348. return retval;
  349. }
  350. UA_Int32 Service_Write(SL_Channel *channel, const UA_WriteRequest *request,
  351. UA_WriteResponse *response) {
  352. UA_Int32 retval = UA_SUCCESS;
  353. UA_Int32 i;
  354. if(channel->session == UA_NULL || channel->session->application == UA_NULL)
  355. return UA_ERROR; // TODO: Return error message
  356. response->resultsSize = request->nodesToWriteSize;
  357. //TODO evalutate diagnostic info within the request
  358. UA_Array_new((void**)&response->results,response->resultsSize,&UA_.types[UA_STATUSCODE]);
  359. for(i=0; i < request->nodesToWriteSize; i++){
  360. retval |= Service_Write_writeNode(channel->session->application, &request->nodesToWrite[i], &response->results[i]);
  361. }
  362. return retval;
  363. }