ua_subscription_monitoreditem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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-2018 (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. #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
  14. /****************/
  15. /* Notification */
  16. /****************/
  17. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  18. static const UA_NodeId overflowEventType =
  19. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_EVENTQUEUEOVERFLOWEVENTTYPE}};
  20. static const UA_NodeId simpleOverflowEventType =
  21. {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE}};
  22. static UA_Boolean
  23. UA_Notification_isOverflowEvent(UA_Server *server, UA_Notification *n) {
  24. UA_MonitoredItem *mon = n->mon;
  25. if(mon->monitoredItemType != UA_MONITOREDITEMTYPE_EVENTNOTIFY)
  26. return false;
  27. UA_EventFieldList *efl = &n->data.event.fields;
  28. if(efl->eventFieldsSize == 1 &&
  29. efl->eventFields[0].type == &UA_TYPES[UA_TYPES_NODEID] &&
  30. isNodeInTree(server->nsCtx, (const UA_NodeId *)efl->eventFields[0].data,
  31. &overflowEventType, &subtypeId, 1)) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. static UA_Notification *
  37. createEventOverflowNotification(UA_MonitoredItem *mon) {
  38. UA_Notification *overflowNotification = (UA_Notification *) UA_malloc(sizeof(UA_Notification));
  39. if(!overflowNotification)
  40. return NULL;
  41. overflowNotification->mon = mon;
  42. UA_EventFieldList_init(&overflowNotification->data.event.fields);
  43. overflowNotification->data.event.fields.eventFields = UA_Variant_new();
  44. if(!overflowNotification->data.event.fields.eventFields) {
  45. UA_EventFieldList_deleteMembers(&overflowNotification->data.event.fields);
  46. UA_free(overflowNotification);
  47. return NULL;
  48. }
  49. overflowNotification->data.event.fields.eventFieldsSize = 1;
  50. UA_StatusCode retval =
  51. UA_Variant_setScalarCopy(overflowNotification->data.event.fields.eventFields,
  52. &simpleOverflowEventType, &UA_TYPES[UA_TYPES_NODEID]);
  53. if(retval != UA_STATUSCODE_GOOD) {
  54. UA_EventFieldList_deleteMembers(&overflowNotification->data.event.fields);
  55. UA_free(overflowNotification);
  56. return NULL;
  57. }
  58. return overflowNotification;
  59. }
  60. #endif
  61. void
  62. UA_Notification_enqueue(UA_Server *server, UA_Subscription *sub,
  63. UA_MonitoredItem *mon, UA_Notification *n) {
  64. /* Add to the MonitoredItem */
  65. TAILQ_INSERT_TAIL(&mon->queue, n, listEntry);
  66. ++mon->queueSize;
  67. /* Add to the subscription */
  68. TAILQ_INSERT_TAIL(&sub->notificationQueue, n, globalEntry);
  69. ++sub->notificationQueueSize;
  70. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  71. ++sub->dataChangeNotifications;
  72. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  73. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  74. ++sub->eventNotifications;
  75. if(UA_Notification_isOverflowEvent(server, n))
  76. ++mon->eventOverflows;
  77. #endif
  78. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_STATUSNOTIFY) {
  79. ++sub->statusChangeNotifications;
  80. }
  81. /* Ensure enough space is available in the MonitoredItem. Do this only after
  82. * adding the new Notification. */
  83. UA_MonitoredItem_ensureQueueSpace(server, mon);
  84. }
  85. void
  86. UA_Notification_dequeue(UA_Server *server, UA_Notification *n) {
  87. UA_MonitoredItem *mon = n->mon;
  88. UA_Subscription *sub = mon->subscription;
  89. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  90. --sub->dataChangeNotifications;
  91. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  92. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  93. --sub->eventNotifications;
  94. if(UA_Notification_isOverflowEvent(server, n))
  95. --mon->eventOverflows;
  96. #endif
  97. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_STATUSNOTIFY) {
  98. --sub->statusChangeNotifications;
  99. }
  100. TAILQ_REMOVE(&mon->queue, n, listEntry);
  101. --mon->queueSize;
  102. TAILQ_REMOVE(&sub->notificationQueue, n, globalEntry);
  103. --sub->notificationQueueSize;
  104. }
  105. void
  106. UA_Notification_delete(UA_Notification *n) {
  107. UA_MonitoredItem *mon = n->mon;
  108. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  109. UA_DataValue_deleteMembers(&n->data.value);
  110. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  111. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  112. UA_EventFieldList_deleteMembers(&n->data.event.fields);
  113. /* EventFilterResult currently isn't being used
  114. * UA_EventFilterResult_delete(notification->data.event->result); */
  115. #endif
  116. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_STATUSNOTIFY) {
  117. /* Nothing to do */
  118. }
  119. UA_free(n);
  120. }
  121. /*****************/
  122. /* MonitoredItem */
  123. /*****************/
  124. void
  125. UA_MonitoredItem_init(UA_MonitoredItem *mon, UA_Subscription *sub) {
  126. memset(mon, 0, sizeof(UA_MonitoredItem));
  127. mon->subscription = sub;
  128. TAILQ_INIT(&mon->queue);
  129. }
  130. void
  131. UA_MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem) {
  132. if(monitoredItem->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  133. /* Remove the sampling callback */
  134. UA_MonitoredItem_unregisterSampleCallback(server, monitoredItem);
  135. } else if (monitoredItem->monitoredItemType != UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  136. /* TODO: Access val data.event */
  137. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  138. "MonitoredItemTypes other than ChangeNotify or EventNotify "
  139. "are not supported yet");
  140. }
  141. /* Remove the queued notifications if attached to a subscription (not a
  142. * local MonitoredItem) */
  143. if(monitoredItem->subscription) {
  144. UA_Notification *notification, *notification_tmp;
  145. TAILQ_FOREACH_SAFE(notification, &monitoredItem->queue,
  146. listEntry, notification_tmp) {
  147. /* Remove the item from the queues and free the memory */
  148. UA_Notification_dequeue(server, notification);
  149. UA_Notification_delete(notification);
  150. }
  151. }
  152. /* if(monitoredItem->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY)
  153. * -> UA_DataChangeFilter does not hold dynamic content we need to free */
  154. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  155. if(monitoredItem->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  156. /* Remove the monitored item from the node queue */
  157. UA_Server_editNode(server, NULL, &monitoredItem->monitoredNodeId,
  158. UA_MonitoredItem_removeNodeEventCallback, monitoredItem);
  159. /* Delete the event filter */
  160. UA_EventFilter_deleteMembers(&monitoredItem->filter.eventFilter);
  161. }
  162. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  163. /* Deregister MonitoredItem in userland */
  164. if(server->config.monitoredItemRegisterCallback && monitoredItem->registered) {
  165. /* Get the session context. Local MonitoredItems don't have a subscription. */
  166. UA_Session *session = NULL;
  167. if(monitoredItem->subscription)
  168. session = monitoredItem->subscription->session;
  169. if(!session)
  170. session = &server->adminSession;
  171. /* Get the node context */
  172. void *targetContext = NULL;
  173. UA_Server_getNodeContext(server, monitoredItem->monitoredNodeId, &targetContext);
  174. /* Deregister */
  175. server->config.monitoredItemRegisterCallback(server, &session->sessionId,
  176. session->sessionHandle, &monitoredItem->monitoredNodeId,
  177. targetContext, monitoredItem->attributeId, true);
  178. }
  179. /* Remove the monitored item */
  180. if(monitoredItem->listEntry.le_prev != NULL)
  181. LIST_REMOVE(monitoredItem, listEntry);
  182. UA_String_deleteMembers(&monitoredItem->indexRange);
  183. UA_ByteString_deleteMembers(&monitoredItem->lastSampledValue);
  184. UA_Variant_deleteMembers(&monitoredItem->lastValue);
  185. UA_NodeId_deleteMembers(&monitoredItem->monitoredNodeId);
  186. /* No actual callback, just remove the structure */
  187. monitoredItem->delayedFreePointers.callback = NULL;
  188. UA_WorkQueue_enqueueDelayed(&server->workQueue, &monitoredItem->delayedFreePointers);
  189. }
  190. #ifdef __clang_analyzer__
  191. # define UA_CA_assert(clause) UA_assert(clause)
  192. #else
  193. # define UA_CA_assert(clause)
  194. #endif
  195. UA_StatusCode
  196. UA_MonitoredItem_ensureQueueSpace(UA_Server *server, UA_MonitoredItem *mon) {
  197. if(mon->queueSize - mon->eventOverflows <= mon->maxQueueSize)
  198. return UA_STATUSCODE_GOOD;
  199. /* Remove notifications until the queue size is reached */
  200. UA_Subscription *sub = mon->subscription;
  201. #ifdef __clang_analyzer__
  202. UA_Notification *last = NULL;
  203. #endif
  204. while(mon->queueSize - mon->eventOverflows > mon->maxQueueSize) {
  205. /* At least two notifications that are not eventOverflows in the queue */
  206. UA_assert(mon->queueSize - mon->eventOverflows >= 2);
  207. /* Select the next notification to delete. Skip over overflow events. */
  208. UA_Notification *del;
  209. if(mon->discardOldest) {
  210. /* Remove the oldest */
  211. del = TAILQ_FIRST(&mon->queue);
  212. UA_CA_assert(del != last);
  213. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  214. while(UA_Notification_isOverflowEvent(server, del)) {
  215. del = TAILQ_NEXT(del, listEntry); /* skip overflow events */
  216. UA_CA_assert(del != last);
  217. }
  218. #endif
  219. } else {
  220. /* Remove the second newest (to keep the up-to-date notification) */
  221. del = TAILQ_LAST(&mon->queue, NotificationQueue);
  222. del = TAILQ_PREV(del, NotificationQueue, listEntry);
  223. UA_CA_assert(del != last);
  224. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  225. while(UA_Notification_isOverflowEvent(server, del)) {
  226. del = TAILQ_PREV(del, NotificationQueue, listEntry); /* skip overflow events */
  227. UA_CA_assert(del != last);
  228. }
  229. #endif
  230. }
  231. // NOLINTNEXTLINE
  232. UA_assert(del && del->mon == mon);
  233. /* Move after_del right after del in the global queue. (It is already
  234. * right after del in the per-MonitoredItem queue.) This is required so
  235. * we don't starve MonitoredItems with a high sampling interval by
  236. * always removing their first appearance in the gloal queue for the
  237. * Subscription. */
  238. UA_Notification *after_del = TAILQ_NEXT(del, listEntry);
  239. UA_CA_assert(after_del != last);
  240. if(after_del) {
  241. TAILQ_REMOVE(&sub->notificationQueue, after_del, globalEntry);
  242. TAILQ_INSERT_AFTER(&sub->notificationQueue, del, after_del, globalEntry);
  243. }
  244. #ifdef __clang_analyzer__
  245. last = del;
  246. #endif
  247. /* Delete the notification */
  248. UA_Notification_dequeue(server, del);
  249. UA_Notification_delete(del);
  250. }
  251. /* Get the element where the overflow shall be announced (infobits or
  252. * overflowevent) */
  253. UA_Notification *indicator;
  254. if(mon->discardOldest)
  255. indicator = TAILQ_FIRST(&mon->queue);
  256. else
  257. indicator = TAILQ_LAST(&mon->queue, NotificationQueue);
  258. UA_assert(indicator);
  259. UA_CA_assert(indicator != last);
  260. /* Create an overflow notification */
  261. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  262. /* The specification states in Part 4 5.12.1.5 that an EventQueueOverflowEvent
  263. * "is generated when the first Event has to be discarded [...] without discarding
  264. * any other event". So only generate one for all deleted events. */
  265. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  266. /* Avoid two redundant overflow events in a row */
  267. if(UA_Notification_isOverflowEvent(server, indicator)) {
  268. if(mon->discardOldest)
  269. return UA_STATUSCODE_GOOD;
  270. UA_Notification *prev = TAILQ_PREV(indicator, NotificationQueue, listEntry);
  271. UA_CA_assert(prev != last);
  272. if(prev && UA_Notification_isOverflowEvent(server, prev))
  273. return UA_STATUSCODE_GOOD;
  274. }
  275. /* A notification is inserted into the queue which includes only the
  276. * NodeId of the overflowEventType. It is up to the client to check for
  277. * possible overflows. */
  278. UA_Notification *overflowNotification = createEventOverflowNotification(mon);
  279. if(!overflowNotification)
  280. return UA_STATUSCODE_BADOUTOFMEMORY;
  281. /* Insert before the "indicator notification". This is either first in
  282. * the queue (if the oldest notification was removed) or before the new
  283. * event that remains the last element of the queue. */
  284. TAILQ_INSERT_BEFORE(indicator, overflowNotification, listEntry);
  285. TAILQ_INSERT_BEFORE(indicator, overflowNotification, globalEntry);
  286. ++mon->eventOverflows;
  287. ++mon->queueSize;
  288. ++sub->notificationQueueSize;
  289. ++sub->eventNotifications;
  290. }
  291. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  292. /* Set the infobits of a datachange notification */
  293. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  294. /* Set the infobits */
  295. if(mon->maxQueueSize > 1) {
  296. /* Add the infobits either to the newest or the new last entry */
  297. indicator->data.value.hasStatus = true; // NOLINT
  298. indicator->data.value.status |= (UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW);
  299. } else {
  300. /* If the queue size is reduced to one, remove the infobits */
  301. indicator->data.value.status &= // NOLINT
  302. ~(UA_StatusCode)(UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW); // NOLINT
  303. }
  304. }
  305. return UA_STATUSCODE_GOOD;
  306. }
  307. UA_StatusCode
  308. UA_MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon) {
  309. if(mon->sampleCallbackIsRegistered)
  310. return UA_STATUSCODE_GOOD;
  311. /* Only DataChange MonitoredItems have a callback with a sampling interval */
  312. if(mon->monitoredItemType != UA_MONITOREDITEMTYPE_CHANGENOTIFY)
  313. return UA_STATUSCODE_GOOD;
  314. UA_StatusCode retval =
  315. UA_Server_addRepeatedCallback(server, (UA_ServerCallback)UA_MonitoredItem_sampleCallback,
  316. mon, mon->samplingInterval, &mon->sampleCallbackId);
  317. if(retval == UA_STATUSCODE_GOOD)
  318. mon->sampleCallbackIsRegistered = true;
  319. return retval;
  320. }
  321. void
  322. UA_MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon) {
  323. if(!mon->sampleCallbackIsRegistered)
  324. return;
  325. UA_Server_removeRepeatedCallback(server, mon->sampleCallbackId);
  326. mon->sampleCallbackIsRegistered = false;
  327. }
  328. #endif /* UA_ENABLE_SUBSCRIPTIONS */