ua_subscription.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include "ua_subscription.h"
  2. #include "ua_server_internal.h"
  3. #include "ua_nodestore.h"
  4. /****************/
  5. /* Subscription */
  6. /****************/
  7. UA_Subscription *UA_Subscription_new(UA_Int32 subscriptionID) {
  8. UA_Subscription *new = UA_malloc(sizeof(UA_Subscription));
  9. if(!new)
  10. return NULL;
  11. new->subscriptionID = subscriptionID;
  12. new->lastPublished = 0;
  13. new->sequenceNumber = 1;
  14. memset(&new->timedUpdateJobGuid, 0, sizeof(UA_Guid));
  15. new->timedUpdateJob = NULL;
  16. new->timedUpdateIsRegistered = UA_FALSE;
  17. LIST_INIT(&new->MonitoredItems);
  18. LIST_INIT(&new->unpublishedNotifications);
  19. new->unpublishedNotificationsSize = 0;
  20. return new;
  21. }
  22. void UA_Subscription_deleteMembers(UA_Subscription *subscription, UA_Server *server) {
  23. // Just in case any parallel process attempts to access this subscription
  24. // while we are deleting it... make it vanish.
  25. subscription->subscriptionID = 0;
  26. // Delete monitored Items
  27. UA_MonitoredItem *mon, *tmp_mon;
  28. LIST_FOREACH_SAFE(mon, &subscription->MonitoredItems, listEntry, tmp_mon) {
  29. LIST_REMOVE(mon, listEntry);
  30. MonitoredItem_delete(mon);
  31. }
  32. // Delete unpublished Notifications
  33. Subscription_deleteUnpublishedNotification(0, true, subscription);
  34. // Unhook/Unregister any timed work assiociated with this subscription
  35. if(subscription->timedUpdateJob) {
  36. Subscription_unregisterUpdateJob(server, subscription);
  37. UA_free(subscription->timedUpdateJob);
  38. }
  39. }
  40. void Subscription_generateKeepAlive(UA_Subscription *subscription) {
  41. if(subscription->keepAliveCount.currentValue > subscription->keepAliveCount.minValue &&
  42. subscription->keepAliveCount.currentValue <= subscription->keepAliveCount.maxValue)
  43. return;
  44. UA_unpublishedNotification *msg = UA_calloc(1,sizeof(UA_unpublishedNotification));
  45. if(!msg)
  46. return;
  47. msg->notification.notificationData = NULL;
  48. // KeepAlive uses next message, but does not increment counter
  49. msg->notification.sequenceNumber = subscription->sequenceNumber + 1;
  50. msg->notification.publishTime = UA_DateTime_now();
  51. msg->notification.notificationDataSize = 0;
  52. LIST_INSERT_HEAD(&subscription->unpublishedNotifications, msg, listEntry);
  53. subscription->unpublishedNotificationsSize += 1;
  54. subscription->keepAliveCount.currentValue = subscription->keepAliveCount.maxValue;
  55. }
  56. void Subscription_updateNotifications(UA_Subscription *subscription) {
  57. UA_MonitoredItem *mon;
  58. //MonitoredItem_queuedValue *queuedValue;
  59. UA_unpublishedNotification *msg;
  60. UA_UInt32 monItemsChangeT = 0, monItemsStatusT = 0, monItemsEventT = 0;
  61. if(!subscription || subscription->lastPublished + subscription->publishingInterval > UA_DateTime_now())
  62. return;
  63. // Make sure there is data to be published and establish which message types
  64. // will need to be generated
  65. LIST_FOREACH(mon, &subscription->MonitoredItems, listEntry) {
  66. // Check if this MonitoredItems Queue holds data and how much data is held in total
  67. if(!TAILQ_FIRST(&mon->queue))
  68. continue;
  69. if((mon->monitoredItemType & MONITOREDITEM_TYPE_CHANGENOTIFY) != 0)
  70. monItemsChangeT+=mon->queueSize.currentValue;
  71. else if((mon->monitoredItemType & MONITOREDITEM_TYPE_STATUSNOTIFY) != 0)
  72. monItemsStatusT+=mon->queueSize.currentValue;
  73. else if((mon->monitoredItemType & MONITOREDITEM_TYPE_EVENTNOTIFY) != 0)
  74. monItemsEventT+=mon->queueSize.currentValue;
  75. }
  76. // FIXME: This is hardcoded to 100 because it is not covered by the spec but we need to protect the server!
  77. if(subscription->unpublishedNotificationsSize >= 10) {
  78. // Remove last entry
  79. Subscription_deleteUnpublishedNotification(0, true, subscription);
  80. }
  81. if(monItemsChangeT == 0 && monItemsEventT == 0 && monItemsStatusT == 0) {
  82. // Decrement KeepAlive
  83. subscription->keepAliveCount.currentValue--;
  84. // +- Generate KeepAlive msg if counter overruns
  85. if (subscription->keepAliveCount.currentValue < subscription->keepAliveCount.minValue)
  86. Subscription_generateKeepAlive(subscription);
  87. return;
  88. }
  89. msg = UA_calloc(1, sizeof(UA_unpublishedNotification));
  90. msg->notification.sequenceNumber = subscription->sequenceNumber++;
  91. msg->notification.publishTime = UA_DateTime_now();
  92. // NotificationData is an array of Change, Status and Event messages, each containing the appropriate
  93. // list of Queued values from all monitoredItems of that type
  94. msg->notification.notificationDataSize = !!monItemsChangeT; // 1 if the pointer is not null, else 0
  95. // + ISNOTZERO(monItemsEventT) + ISNOTZERO(monItemsStatusT);
  96. msg->notification.notificationData =
  97. UA_Array_new(msg->notification.notificationDataSize, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  98. for(size_t notmsgn = 0; notmsgn < msg->notification.notificationDataSize; notmsgn++) {
  99. // Set the notification message type and encoding for each of
  100. // the three possible NotificationData Types
  101. /* msg->notification->notificationData[notmsgn].encoding = 1; // Encoding is always binary */
  102. /* msg->notification->notificationData[notmsgn].typeId = UA_NODEID_NUMERIC(0, 811); */
  103. if(notmsgn == 0) {
  104. UA_DataChangeNotification *changeNotification = UA_DataChangeNotification_new();
  105. changeNotification->monitoredItems = UA_Array_new(monItemsChangeT, &UA_TYPES[UA_TYPES_MONITOREDITEMNOTIFICATION]);
  106. // Scan all monitoredItems in this subscription and have their queue transformed into an Array of
  107. // the propper NotificationMessageType (Status, Change, Event)
  108. monItemsChangeT = 0;
  109. LIST_FOREACH(mon, &subscription->MonitoredItems, listEntry) {
  110. if(mon->monitoredItemType != MONITOREDITEM_TYPE_CHANGENOTIFY || !TAILQ_FIRST(&mon->queue))
  111. continue;
  112. // Note: Monitored Items might not return a queuedValue if there is a problem encoding it.
  113. monItemsChangeT += MonitoredItem_QueueToDataChangeNotifications(&changeNotification->monitoredItems[monItemsChangeT], mon);
  114. MonitoredItem_ClearQueue(mon);
  115. }
  116. changeNotification->monitoredItemsSize = monItemsChangeT;
  117. msg->notification.notificationData[notmsgn].encoding = UA_EXTENSIONOBJECT_DECODED;
  118. msg->notification.notificationData[notmsgn].content.decoded.type = &UA_TYPES[UA_TYPES_DATACHANGENOTIFICATION];
  119. msg->notification.notificationData[notmsgn].content.decoded.data = changeNotification;
  120. } else if(notmsgn == 1) {
  121. // FIXME: Constructing a StatusChangeNotification is not implemented
  122. } else if(notmsgn == 2) {
  123. // FIXME: Constructing a EventListNotification is not implemented
  124. }
  125. }
  126. LIST_INSERT_HEAD(&subscription->unpublishedNotifications, msg, listEntry);
  127. subscription->unpublishedNotificationsSize += 1;
  128. }
  129. UA_UInt32 *Subscription_getAvailableSequenceNumbers(UA_Subscription *sub) {
  130. UA_UInt32 *seqArray = UA_malloc(sizeof(UA_UInt32) * sub->unpublishedNotificationsSize);
  131. if(!seqArray)
  132. return NULL;
  133. int i = 0;
  134. UA_unpublishedNotification *not;
  135. LIST_FOREACH(not, &sub->unpublishedNotifications, listEntry) {
  136. seqArray[i] = not->notification.sequenceNumber;
  137. i++;
  138. }
  139. return seqArray;
  140. }
  141. void Subscription_copyNotificationMessage(UA_NotificationMessage *dst, UA_unpublishedNotification *src) {
  142. if(!dst)
  143. return;
  144. UA_NotificationMessage *latest = &src->notification;
  145. dst->notificationDataSize = latest->notificationDataSize;
  146. dst->publishTime = latest->publishTime;
  147. dst->sequenceNumber = latest->sequenceNumber;
  148. if(latest->notificationDataSize == 0)
  149. return;
  150. dst->notificationData = UA_ExtensionObject_new();
  151. UA_ExtensionObject_copy(latest->notificationData, dst->notificationData);
  152. }
  153. UA_UInt32 Subscription_deleteUnpublishedNotification(UA_UInt32 seqNo, UA_Boolean bDeleteAll, UA_Subscription *sub) {
  154. UA_UInt32 deletedItems = 0;
  155. UA_unpublishedNotification *not, *tmp;
  156. LIST_FOREACH_SAFE(not, &sub->unpublishedNotifications, listEntry, tmp) {
  157. if(!bDeleteAll && not->notification.sequenceNumber != seqNo)
  158. continue;
  159. LIST_REMOVE(not, listEntry);
  160. sub->unpublishedNotificationsSize -= 1;
  161. UA_NotificationMessage_deleteMembers(&not->notification);
  162. UA_free(not);
  163. deletedItems++;
  164. }
  165. return deletedItems;
  166. }
  167. static void Subscription_timedUpdateNotificationsJob(UA_Server *server, void *data) {
  168. // Timed-Worker/Job Version of updateNotifications
  169. UA_Subscription *sub = (UA_Subscription *) data;
  170. UA_MonitoredItem *mon;
  171. if(!data || !server)
  172. return;
  173. // This is set by the Subscription_delete function to detere us from fiddling with
  174. // this subscription if it is being deleted (not technically thread save, but better
  175. // then nothing at all)
  176. if(sub->subscriptionID == 0)
  177. return;
  178. // FIXME: This should be done by the event system
  179. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry)
  180. MonitoredItem_QueuePushDataValue(server, mon);
  181. Subscription_updateNotifications(sub);
  182. }
  183. UA_StatusCode Subscription_createdUpdateJob(UA_Server *server, UA_Guid jobId, UA_Subscription *sub) {
  184. if(server == NULL || sub == NULL)
  185. return UA_STATUSCODE_BADSERVERINDEXINVALID;
  186. UA_Job *theWork;
  187. theWork = (UA_Job *) UA_malloc(sizeof(UA_Job));
  188. if(!theWork)
  189. return UA_STATUSCODE_BADOUTOFMEMORY;
  190. *theWork = (UA_Job) {.type = UA_JOBTYPE_METHODCALL,
  191. .job.methodCall = {.method = Subscription_timedUpdateNotificationsJob, .data = sub} };
  192. sub->timedUpdateJobGuid = jobId;
  193. sub->timedUpdateJob = theWork;
  194. return UA_STATUSCODE_GOOD;
  195. }
  196. UA_StatusCode Subscription_registerUpdateJob(UA_Server *server, UA_Subscription *sub) {
  197. if(sub->publishingInterval <= 5 )
  198. return UA_STATUSCODE_BADNOTSUPPORTED;
  199. /* Practically enough, the client sends a uint32 in ms, which we store as
  200. datetime, which here is required in as uint32 in ms as the interval */
  201. UA_StatusCode retval = UA_Server_addRepeatedJob(server, *sub->timedUpdateJob, sub->publishingInterval,
  202. &sub->timedUpdateJobGuid);
  203. if(retval == UA_STATUSCODE_GOOD)
  204. sub->timedUpdateIsRegistered = UA_TRUE;
  205. return retval;
  206. }
  207. UA_StatusCode Subscription_unregisterUpdateJob(UA_Server *server, UA_Subscription *sub) {
  208. sub->timedUpdateIsRegistered = UA_FALSE;
  209. return UA_Server_removeRepeatedJob(server, sub->timedUpdateJobGuid);
  210. }
  211. /*****************/
  212. /* MonitoredItem */
  213. /*****************/
  214. UA_MonitoredItem * UA_MonitoredItem_new() {
  215. UA_MonitoredItem *new = (UA_MonitoredItem *) UA_malloc(sizeof(UA_MonitoredItem));
  216. new->queueSize = (UA_UInt32_BoundedValue) { .minValue = 0, .maxValue = 0, .currentValue = 0};
  217. new->lastSampled = 0;
  218. // FIXME: This is currently hardcoded;
  219. new->monitoredItemType = MONITOREDITEM_TYPE_CHANGENOTIFY;
  220. TAILQ_INIT(&new->queue);
  221. UA_NodeId_init(&new->monitoredNodeId);
  222. new->lastSampledValue.data = 0;
  223. return new;
  224. }
  225. void MonitoredItem_delete(UA_MonitoredItem *monitoredItem) {
  226. // Delete Queued Data
  227. MonitoredItem_ClearQueue(monitoredItem);
  228. // Remove from subscription list
  229. LIST_REMOVE(monitoredItem, listEntry);
  230. // Release comparison sample
  231. if(monitoredItem->lastSampledValue.data != NULL) {
  232. UA_free(monitoredItem->lastSampledValue.data);
  233. }
  234. UA_NodeId_deleteMembers(&(monitoredItem->monitoredNodeId));
  235. UA_free(monitoredItem);
  236. }
  237. int MonitoredItem_QueueToDataChangeNotifications(UA_MonitoredItemNotification *dst,
  238. UA_MonitoredItem *monitoredItem) {
  239. int queueSize = 0;
  240. MonitoredItem_queuedValue *queueItem;
  241. // Count instead of relying on the items currentValue
  242. TAILQ_FOREACH(queueItem, &monitoredItem->queue, listEntry) {
  243. dst[queueSize].clientHandle = monitoredItem->clientHandle;
  244. UA_DataValue_copy(&queueItem->value, &dst[queueSize].value);
  245. dst[queueSize].value.hasServerPicoseconds = UA_FALSE;
  246. dst[queueSize].value.hasServerTimestamp = UA_TRUE;
  247. dst[queueSize].value.serverTimestamp = UA_DateTime_now();
  248. // Do not create variants with no type -> will make calcSizeBinary() segfault.
  249. if(dst[queueSize].value.value.type)
  250. queueSize++;
  251. }
  252. return queueSize;
  253. }
  254. void MonitoredItem_ClearQueue(UA_MonitoredItem *monitoredItem) {
  255. MonitoredItem_queuedValue *val, *val_tmp;
  256. TAILQ_FOREACH_SAFE(val, &monitoredItem->queue, listEntry, val_tmp) {
  257. TAILQ_REMOVE(&monitoredItem->queue, val, listEntry);
  258. UA_DataValue_deleteMembers(&val->value);
  259. UA_free(val);
  260. }
  261. monitoredItem->queueSize.currentValue = 0;
  262. }
  263. UA_Boolean MonitoredItem_CopyMonitoredValueToVariant(UA_UInt32 attributeID, const UA_Node *src,
  264. UA_DataValue *dst) {
  265. UA_Boolean samplingError = UA_TRUE;
  266. UA_DataValue sourceDataValue;
  267. UA_DataValue_init(&sourceDataValue);
  268. // FIXME: Not all attributeIDs can be monitored yet
  269. switch(attributeID) {
  270. case UA_ATTRIBUTEID_NODEID:
  271. UA_Variant_setScalarCopy(&dst->value, (const UA_NodeId*)&src->nodeId, &UA_TYPES[UA_TYPES_NODEID]);
  272. dst->hasValue = UA_TRUE;
  273. samplingError = UA_FALSE;
  274. break;
  275. case UA_ATTRIBUTEID_NODECLASS:
  276. UA_Variant_setScalarCopy(&dst->value, (const UA_Int32*)&src->nodeClass, &UA_TYPES[UA_TYPES_INT32]);
  277. dst->hasValue = UA_TRUE;
  278. samplingError = UA_FALSE;
  279. break;
  280. case UA_ATTRIBUTEID_BROWSENAME:
  281. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->browseName, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  282. dst->hasValue = UA_TRUE;
  283. samplingError = UA_FALSE;
  284. break;
  285. case UA_ATTRIBUTEID_DISPLAYNAME:
  286. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->displayName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  287. dst->hasValue = UA_TRUE;
  288. samplingError = UA_FALSE;
  289. break;
  290. case UA_ATTRIBUTEID_DESCRIPTION:
  291. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->displayName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  292. dst->hasValue = UA_TRUE;
  293. samplingError = UA_FALSE;
  294. break;
  295. case UA_ATTRIBUTEID_WRITEMASK:
  296. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->writeMask, &UA_TYPES[UA_TYPES_UINT32]);
  297. dst->hasValue = UA_TRUE;
  298. samplingError = UA_FALSE;
  299. break;
  300. case UA_ATTRIBUTEID_USERWRITEMASK:
  301. UA_Variant_setScalarCopy(&dst->value, (const UA_String*)&src->writeMask, &UA_TYPES[UA_TYPES_UINT32]);
  302. dst->hasValue = UA_TRUE;
  303. samplingError = UA_FALSE;
  304. break;
  305. case UA_ATTRIBUTEID_ISABSTRACT:
  306. break;
  307. case UA_ATTRIBUTEID_SYMMETRIC:
  308. break;
  309. case UA_ATTRIBUTEID_INVERSENAME:
  310. break;
  311. case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
  312. break;
  313. case UA_ATTRIBUTEID_EVENTNOTIFIER:
  314. break;
  315. case UA_ATTRIBUTEID_VALUE:
  316. if(src->nodeClass == UA_NODECLASS_VARIABLE) {
  317. const UA_VariableNode *vsrc = (const UA_VariableNode*)src;
  318. if(vsrc->valueSource == UA_VALUESOURCE_VARIANT) {
  319. if(vsrc->value.variant.callback.onRead)
  320. vsrc->value.variant.callback.onRead(vsrc->value.variant.callback.handle, vsrc->nodeId,
  321. &dst->value, NULL);
  322. UA_Variant_copy(&vsrc->value.variant.value, &dst->value);
  323. dst->hasValue = UA_TRUE;
  324. samplingError = UA_FALSE;
  325. } else {
  326. if(vsrc->valueSource != UA_VALUESOURCE_DATASOURCE || vsrc->value.dataSource.read == NULL)
  327. break;
  328. if(vsrc->value.dataSource.read(vsrc->value.dataSource.handle, vsrc->nodeId, UA_TRUE,
  329. NULL, &sourceDataValue) != UA_STATUSCODE_GOOD)
  330. break;
  331. UA_DataValue_copy(&sourceDataValue, dst);
  332. if(sourceDataValue.value.data) {
  333. UA_deleteMembers(sourceDataValue.value.data, sourceDataValue.value.type);
  334. UA_free(sourceDataValue.value.data);
  335. sourceDataValue.value.data = NULL;
  336. }
  337. UA_DataValue_deleteMembers(&sourceDataValue);
  338. samplingError = UA_FALSE;
  339. }
  340. }
  341. break;
  342. case UA_ATTRIBUTEID_DATATYPE:
  343. break;
  344. case UA_ATTRIBUTEID_VALUERANK:
  345. break;
  346. case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
  347. break;
  348. case UA_ATTRIBUTEID_ACCESSLEVEL:
  349. break;
  350. case UA_ATTRIBUTEID_USERACCESSLEVEL:
  351. break;
  352. case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
  353. break;
  354. case UA_ATTRIBUTEID_HISTORIZING:
  355. break;
  356. case UA_ATTRIBUTEID_EXECUTABLE:
  357. break;
  358. case UA_ATTRIBUTEID_USEREXECUTABLE:
  359. break;
  360. default:
  361. break;
  362. }
  363. return samplingError;
  364. }
  365. void MonitoredItem_QueuePushDataValue(UA_Server *server, UA_MonitoredItem *monitoredItem) {
  366. UA_ByteString newValueAsByteString = { .length=0, .data=NULL };
  367. size_t encodingOffset = 0;
  368. if(!monitoredItem || monitoredItem->lastSampled + monitoredItem->samplingInterval > UA_DateTime_now())
  369. return;
  370. // FIXME: Actively suppress non change value based monitoring. There should be
  371. // another function to handle status and events.
  372. if(monitoredItem->monitoredItemType != MONITOREDITEM_TYPE_CHANGENOTIFY)
  373. return;
  374. MonitoredItem_queuedValue *newvalue = UA_malloc(sizeof(MonitoredItem_queuedValue));
  375. if(!newvalue)
  376. return;
  377. newvalue->listEntry.tqe_next = NULL;
  378. newvalue->listEntry.tqe_prev = NULL;
  379. UA_DataValue_init(&newvalue->value);
  380. // Verify that the *Node being monitored is still valid
  381. // Looking up the in the nodestore is only necessary if we suspect that it is changed during writes
  382. // e.g. in multithreaded applications
  383. const UA_Node *target = UA_NodeStore_get(server->nodestore, &monitoredItem->monitoredNodeId);
  384. if(!target) {
  385. UA_free(newvalue);
  386. return;
  387. }
  388. UA_Boolean samplingError = MonitoredItem_CopyMonitoredValueToVariant(monitoredItem->attributeID, target,
  389. &newvalue->value);
  390. if(samplingError != UA_FALSE || !newvalue->value.value.type) {
  391. UA_DataValue_deleteMembers(&newvalue->value);
  392. UA_free(newvalue);
  393. return;
  394. }
  395. if(monitoredItem->queueSize.currentValue >= monitoredItem->queueSize.maxValue) {
  396. if(monitoredItem->discardOldest != UA_TRUE) {
  397. // We cannot remove the oldest value and theres no queue space left. We're done here.
  398. UA_free(newvalue);
  399. return;
  400. }
  401. MonitoredItem_queuedValue *queueItem = TAILQ_LAST(&monitoredItem->queue, QueueOfQueueDataValues);
  402. TAILQ_REMOVE(&monitoredItem->queue, queueItem, listEntry);
  403. UA_free(queueItem);
  404. monitoredItem->queueSize.currentValue--;
  405. }
  406. // encode the data to find if its different to the previous
  407. newValueAsByteString.length = 512; // Todo: Hack! We should make a copy of the value, not encode it. UA_calcSizeBinary(&newvalue->value, &UA_TYPES[UA_TYPES_DATAVALUE]);
  408. newValueAsByteString.data = UA_malloc(newValueAsByteString.length);
  409. UA_StatusCode retval = UA_encodeBinary(&newvalue->value, &UA_TYPES[UA_TYPES_DATAVALUE], &newValueAsByteString, &encodingOffset);
  410. if(retval != UA_STATUSCODE_GOOD)
  411. UA_ByteString_deleteMembers(&newValueAsByteString);
  412. if(!monitoredItem->lastSampledValue.data) {
  413. UA_ByteString_copy(&newValueAsByteString, &monitoredItem->lastSampledValue);
  414. TAILQ_INSERT_HEAD(&monitoredItem->queue, newvalue, listEntry);
  415. monitoredItem->queueSize.currentValue++;
  416. monitoredItem->lastSampled = UA_DateTime_now();
  417. UA_free(newValueAsByteString.data);
  418. } else {
  419. if(UA_String_equal(&newValueAsByteString, &monitoredItem->lastSampledValue) == UA_TRUE) {
  420. UA_DataValue_deleteMembers(&newvalue->value);
  421. UA_free(newvalue);
  422. UA_String_deleteMembers(&newValueAsByteString);
  423. return;
  424. }
  425. UA_ByteString_deleteMembers(&monitoredItem->lastSampledValue);
  426. monitoredItem->lastSampledValue = newValueAsByteString;
  427. TAILQ_INSERT_HEAD(&monitoredItem->queue, newvalue, listEntry);
  428. monitoredItem->queueSize.currentValue++;
  429. monitoredItem->lastSampled = UA_DateTime_now();
  430. }
  431. }