ua_subscription_datachange.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. * Copyright 2018 (c) Ari Breitkreuz, fortiss GmbH
  8. * Copyright 2018 (c) Thomas Stalder, Blue Time Concept SA
  9. * Copyright 2018 (c) Fabian Arndt, Root-Core
  10. */
  11. #include "ua_server_internal.h"
  12. #include "ua_subscription.h"
  13. #include "ua_types_encoding_binary.h"
  14. #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
  15. #define UA_VALUENCODING_MAXSTACK 512
  16. #define ABS_SUBTRACT_TYPE_INDEPENDENT(a,b) ((a)>(b)?(a)-(b):(b)-(a))
  17. static UA_Boolean
  18. outOfDeadBand(const void *data1, const void *data2,
  19. const UA_DataType *type, const UA_Double deadbandValue) {
  20. if(type == &UA_TYPES[UA_TYPES_BOOLEAN]) {
  21. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Boolean*)data1, *(const UA_Boolean*)data2) <= deadbandValue)
  22. return false;
  23. } else if(type == &UA_TYPES[UA_TYPES_SBYTE]) {
  24. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_SByte*)data1, *(const UA_SByte*)data2) <= deadbandValue)
  25. return false;
  26. } else if(type == &UA_TYPES[UA_TYPES_BYTE]) {
  27. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Byte*)data1, *(const UA_Byte*)data2) <= deadbandValue)
  28. return false;
  29. } else if(type == &UA_TYPES[UA_TYPES_INT16]) {
  30. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Int16*)data1, *(const UA_Int16*)data2) <= deadbandValue)
  31. return false;
  32. } else if(type == &UA_TYPES[UA_TYPES_UINT16]) {
  33. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_UInt16*)data1, *(const UA_UInt16*)data2) <= deadbandValue)
  34. return false;
  35. } else if(type == &UA_TYPES[UA_TYPES_INT32]) {
  36. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Int32*)data1, *(const UA_Int32*)data2) <= deadbandValue)
  37. return false;
  38. } else if(type == &UA_TYPES[UA_TYPES_UINT32]) {
  39. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_UInt32*)data1, *(const UA_UInt32*)data2) <= deadbandValue)
  40. return false;
  41. } else if(type == &UA_TYPES[UA_TYPES_INT64]) {
  42. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Int64*)data1, *(const UA_Int64*)data2) <= deadbandValue)
  43. return false;
  44. } else if(type == &UA_TYPES[UA_TYPES_UINT64]) {
  45. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_UInt64*)data1, *(const UA_UInt64*)data2) <= deadbandValue)
  46. return false;
  47. } else if(type == &UA_TYPES[UA_TYPES_FLOAT]) {
  48. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Float*)data1, *(const UA_Float*)data2) <= deadbandValue)
  49. return false;
  50. } else if(type == &UA_TYPES[UA_TYPES_DOUBLE]) {
  51. if(ABS_SUBTRACT_TYPE_INDEPENDENT(*(const UA_Double*)data1, *(const UA_Double*)data2) <= deadbandValue)
  52. return false;
  53. }
  54. return true;
  55. }
  56. static UA_INLINE UA_Boolean
  57. updateNeededForFilteredValue(const UA_Variant *value, const UA_Variant *oldValue,
  58. const UA_Double deadbandValue) {
  59. if(value->arrayLength != oldValue->arrayLength)
  60. return true;
  61. if(value->type != oldValue->type)
  62. return true;
  63. size_t length = 1;
  64. if(!UA_Variant_isScalar(value))
  65. length = value->arrayLength;
  66. uintptr_t data = (uintptr_t)value->data;
  67. for(size_t i = 0; i < length; ++i) {
  68. if(outOfDeadBand((const void*)data, oldValue->data, value->type, deadbandValue))
  69. return true;
  70. data += value->type->memSize;
  71. }
  72. return false;
  73. }
  74. /* When a change is detected, encoding contains the heap-allocated binary
  75. * encoded value. The default for changed is false. */
  76. static UA_StatusCode
  77. detectValueChangeWithFilter(UA_Server *server, UA_Session *session, UA_MonitoredItem *mon,
  78. UA_DataValue *value, UA_ByteString *encoding, UA_Boolean *changed) {
  79. if(UA_DataType_isNumeric(value->value.type) &&
  80. (mon->filter.dataChangeFilter.trigger == UA_DATACHANGETRIGGER_STATUSVALUE ||
  81. mon->filter.dataChangeFilter.trigger == UA_DATACHANGETRIGGER_STATUSVALUETIMESTAMP)) {
  82. if(mon->filter.dataChangeFilter.deadbandType == UA_DEADBANDTYPE_ABSOLUTE) {
  83. if(!updateNeededForFilteredValue(&value->value, &mon->lastValue,
  84. mon->filter.dataChangeFilter.deadbandValue))
  85. return UA_STATUSCODE_GOOD;
  86. }
  87. }
  88. /* Stack-allocate some memory for the value encoding. We might heap-allocate
  89. * more memory if needed. This is just enough for scalars and small
  90. * structures. */
  91. UA_STACKARRAY(UA_Byte, stackValueEncoding, UA_VALUENCODING_MAXSTACK);
  92. UA_ByteString valueEncoding;
  93. valueEncoding.data = stackValueEncoding;
  94. valueEncoding.length = UA_VALUENCODING_MAXSTACK;
  95. /* Encode the value */
  96. UA_Byte *bufPos = valueEncoding.data;
  97. const UA_Byte *bufEnd = &valueEncoding.data[valueEncoding.length];
  98. UA_StatusCode retval = UA_encodeBinary(value, &UA_TYPES[UA_TYPES_DATAVALUE],
  99. &bufPos, &bufEnd, NULL, NULL);
  100. if(retval == UA_STATUSCODE_BADENCODINGERROR) {
  101. size_t binsize = UA_calcSizeBinary(value, &UA_TYPES[UA_TYPES_DATAVALUE]);
  102. if(binsize == 0)
  103. return UA_STATUSCODE_BADENCODINGERROR;
  104. if(binsize > UA_VALUENCODING_MAXSTACK) {
  105. retval = UA_ByteString_allocBuffer(&valueEncoding, binsize);
  106. if(retval == UA_STATUSCODE_GOOD) {
  107. bufPos = valueEncoding.data;
  108. bufEnd = &valueEncoding.data[valueEncoding.length];
  109. retval = UA_encodeBinary(value, &UA_TYPES[UA_TYPES_DATAVALUE],
  110. &bufPos, &bufEnd, NULL, NULL);
  111. }
  112. }
  113. }
  114. if(retval != UA_STATUSCODE_GOOD) {
  115. if(valueEncoding.data != stackValueEncoding)
  116. UA_ByteString_clear(&valueEncoding);
  117. return retval;
  118. }
  119. /* Has the value changed? */
  120. valueEncoding.length = (uintptr_t)bufPos - (uintptr_t)valueEncoding.data;
  121. *changed = (!mon->lastSampledValue.data ||
  122. !UA_String_equal(&valueEncoding, &mon->lastSampledValue));
  123. /* No change */
  124. if(!(*changed)) {
  125. if(valueEncoding.data != stackValueEncoding)
  126. UA_ByteString_clear(&valueEncoding);
  127. return UA_STATUSCODE_GOOD;
  128. }
  129. /* Change detected. Copy encoding on the heap if necessary. */
  130. if(valueEncoding.data == stackValueEncoding)
  131. return UA_ByteString_copy(&valueEncoding, encoding);
  132. *encoding = valueEncoding;
  133. return UA_STATUSCODE_GOOD;
  134. }
  135. /* Has this sample changed from the last one? The method may allocate additional
  136. * space for the encoding buffer. Detect the change in encoding->data. */
  137. static UA_StatusCode
  138. detectValueChange(UA_Server *server, UA_Session *session, UA_MonitoredItem *mon,
  139. UA_DataValue value, UA_ByteString *encoding, UA_Boolean *changed) {
  140. UA_LOCK_ASSERT(server->serviceMutex, 1);
  141. /* Apply Filter */
  142. if(mon->filter.dataChangeFilter.trigger == UA_DATACHANGETRIGGER_STATUS)
  143. value.hasValue = false;
  144. value.hasServerTimestamp = false;
  145. value.hasServerPicoseconds = false;
  146. if(mon->filter.dataChangeFilter.trigger < UA_DATACHANGETRIGGER_STATUSVALUETIMESTAMP) {
  147. value.hasSourceTimestamp = false;
  148. value.hasSourcePicoseconds = false;
  149. }
  150. /* Detect the value change */
  151. return detectValueChangeWithFilter(server, session, mon, &value, encoding, changed);
  152. }
  153. /* movedValue returns whether the sample was moved to the notification. The
  154. * default is false. */
  155. static UA_StatusCode
  156. sampleCallbackWithValue(UA_Server *server, UA_Session *session,
  157. UA_Subscription *sub, UA_MonitoredItem *mon,
  158. UA_DataValue *value, UA_Boolean *movedValue) {
  159. UA_assert(mon->attributeId != UA_ATTRIBUTEID_EVENTNOTIFIER);
  160. /* Contains heap-allocated binary encoding of the value if a change was detected */
  161. UA_ByteString binValueEncoding = UA_BYTESTRING_NULL;
  162. /* Has the value changed? Allocates memory in binValueEncoding if necessary.
  163. * value is edited internally so we make a shallow copy. */
  164. UA_Boolean changed = false;
  165. UA_StatusCode retval = detectValueChange(server, session, mon, *value, &binValueEncoding, &changed);
  166. if(retval != UA_STATUSCODE_GOOD) {
  167. UA_LOG_WARNING_SESSION(&server->config.logger, session, "Subscription %u | "
  168. "MonitoredItem %i | Value change detection failed with StatusCode %s",
  169. sub ? sub->subscriptionId : 0, mon->monitoredItemId,
  170. UA_StatusCode_name(retval));
  171. return retval;
  172. }
  173. if(!changed) {
  174. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Subscription %u | "
  175. "MonitoredItem %i | The value has not changed",
  176. sub ? sub->subscriptionId : 0, mon->monitoredItemId);
  177. return UA_STATUSCODE_GOOD;
  178. }
  179. /* The MonitoredItem is attached to a subscription (not server-local).
  180. * Prepare a notification and enqueue it. */
  181. if(sub) {
  182. /* Allocate a new notification */
  183. UA_Notification *newNotification = (UA_Notification *)UA_malloc(sizeof(UA_Notification));
  184. if(!newNotification) {
  185. UA_ByteString_clear(&binValueEncoding);
  186. return UA_STATUSCODE_BADOUTOFMEMORY;
  187. }
  188. if(value->value.storageType == UA_VARIANT_DATA) {
  189. newNotification->data.value = *value; /* Move the value to the notification */
  190. *movedValue = true;
  191. } else { /* => (value->value.storageType == UA_VARIANT_DATA_NODELETE) */
  192. retval = UA_DataValue_copy(value, &newNotification->data.value);
  193. if(retval != UA_STATUSCODE_GOOD) {
  194. UA_ByteString_clear(&binValueEncoding);
  195. UA_free(newNotification);
  196. return retval;
  197. }
  198. }
  199. /* <-- Point of no return --> */
  200. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Subscription %u | "
  201. "MonitoredItem %i | Enqueue a new notification",
  202. sub ? sub->subscriptionId : 0, mon->monitoredItemId);
  203. newNotification->mon = mon;
  204. UA_Notification_enqueue(server, sub, mon, newNotification);
  205. }
  206. /* Store the encoding for comparison */
  207. UA_ByteString_clear(&mon->lastSampledValue);
  208. mon->lastSampledValue = binValueEncoding;
  209. /* Store the value for filter comparison (we don't want to decode
  210. * lastSampledValue in every iteration). Don't test the return code here. If
  211. * this fails, lastValue is empty and a notification will be forced for the
  212. * next deadband comparison. */
  213. if((mon->filter.dataChangeFilter.deadbandType == UA_DEADBANDTYPE_NONE ||
  214. mon->filter.dataChangeFilter.deadbandType == UA_DEADBANDTYPE_ABSOLUTE ||
  215. mon->filter.dataChangeFilter.deadbandType == UA_DEADBANDTYPE_PERCENT) &&
  216. (mon->filter.dataChangeFilter.trigger == UA_DATACHANGETRIGGER_STATUS ||
  217. mon->filter.dataChangeFilter.trigger == UA_DATACHANGETRIGGER_STATUSVALUE ||
  218. mon->filter.dataChangeFilter.trigger == UA_DATACHANGETRIGGER_STATUSVALUETIMESTAMP)) {
  219. UA_Variant_clear(&mon->lastValue);
  220. UA_Variant_copy(&value->value, &mon->lastValue);
  221. #ifdef UA_ENABLE_DA
  222. UA_StatusCode_clear(&mon->lastStatus);
  223. UA_StatusCode_copy(&value->status, &mon->lastStatus);
  224. #endif
  225. }
  226. /* Call the local callback if the MonitoredItem is not attached to a
  227. * subscription. Do this at the very end. Because the callback might delete
  228. * the subscription. */
  229. if(!sub) {
  230. UA_LocalMonitoredItem *localMon = (UA_LocalMonitoredItem*) mon;
  231. void *nodeContext = NULL;
  232. getNodeContext(server, mon->monitoredNodeId, &nodeContext);
  233. UA_UNLOCK(server->serviceMutex);
  234. localMon->callback.dataChangeCallback(server, mon->monitoredItemId,
  235. localMon->context,
  236. &mon->monitoredNodeId,
  237. nodeContext, mon->attributeId,
  238. value);
  239. UA_LOCK(server->serviceMutex);
  240. }
  241. return UA_STATUSCODE_GOOD;
  242. }
  243. void
  244. UA_MonitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem)
  245. {
  246. UA_LOCK(server->serviceMutex);
  247. monitoredItem_sampleCallback(server, monitoredItem);
  248. UA_UNLOCK(server->serviceMutex)
  249. }
  250. void
  251. monitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem) {
  252. UA_LOCK_ASSERT(server->serviceMutex, 1);
  253. UA_Subscription *sub = monitoredItem->subscription;
  254. UA_Session *session = &server->adminSession;
  255. if(sub)
  256. session = sub->session;
  257. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Subscription %u | "
  258. "MonitoredItem %i | Sample callback called",
  259. sub ? sub->subscriptionId : 0, monitoredItem->monitoredItemId);
  260. UA_assert(monitoredItem->attributeId != UA_ATTRIBUTEID_EVENTNOTIFIER);
  261. /* Get the node */
  262. const UA_Node *node = UA_NODESTORE_GET(server, &monitoredItem->monitoredNodeId);
  263. /* Sample the value. The sample can still point into the node. */
  264. UA_DataValue value;
  265. UA_DataValue_init(&value);
  266. if(node) {
  267. UA_ReadValueId rvid;
  268. UA_ReadValueId_init(&rvid);
  269. rvid.nodeId = monitoredItem->monitoredNodeId;
  270. rvid.attributeId = monitoredItem->attributeId;
  271. rvid.indexRange = monitoredItem->indexRange;
  272. ReadWithNode(node, server, session, monitoredItem->timestampsToReturn, &rvid, &value);
  273. } else {
  274. value.hasStatus = true;
  275. value.status = UA_STATUSCODE_BADNODEIDUNKNOWN;
  276. }
  277. /* Operate on the sample */
  278. UA_Boolean movedValue = false;
  279. UA_StatusCode retval = sampleCallbackWithValue(server, session, sub, monitoredItem, &value, &movedValue);
  280. if(retval != UA_STATUSCODE_GOOD) {
  281. UA_LOG_WARNING_SESSION(&server->config.logger, session, "Subscription %u | "
  282. "MonitoredItem %i | Sampling returned the statuscode %s",
  283. sub ? sub->subscriptionId : 0, monitoredItem->monitoredItemId,
  284. UA_StatusCode_name(retval));
  285. }
  286. /* Delete the sample if it was not moved to the notification. */
  287. if(!movedValue)
  288. UA_DataValue_clear(&value); /* Does nothing for UA_VARIANT_DATA_NODELETE */
  289. if(node)
  290. UA_NODESTORE_RELEASE(server, node);
  291. }
  292. #endif /* UA_ENABLE_SUBSCRIPTIONS */