ua_subscription.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 2015-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2015 (c) Chris Iatrou
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2017-2018 (c) Thomas Stalder, Blue Time Concept SA
  9. * Copyright 2015 (c) Joakim L. Gilje
  10. * Copyright 2016-2017 (c) Florian Palm
  11. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  12. * Copyright 2017 (c) frax2222
  13. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  14. * Copyright 2017 (c) Ari Breitkreuz, fortiss GmbH
  15. * Copyright 2017 (c) Mattias Bornhager
  16. */
  17. #include "ua_server_internal.h"
  18. #include "ua_subscription_events.h"
  19. #include "ua_subscription.h"
  20. #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
  21. void UA_Notification_delete(UA_Notification *n) {
  22. if(n->mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  23. UA_DataValue_deleteMembers(&n->data.value);
  24. } else if (n->mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  25. UA_Array_delete(n->data.event.fields.eventFields, n->data.event.fields.eventFieldsSize,
  26. &UA_TYPES[UA_TYPES_VARIANT]);
  27. }
  28. UA_free(n);
  29. }
  30. UA_Subscription *
  31. UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId) {
  32. /* Allocate the memory */
  33. UA_Subscription *newSub =
  34. (UA_Subscription*)UA_calloc(1, sizeof(UA_Subscription));
  35. if(!newSub)
  36. return NULL;
  37. /* Remaining members are covered by calloc zeroing out the memory */
  38. newSub->session = session;
  39. newSub->subscriptionId = subscriptionId;
  40. newSub->state = UA_SUBSCRIPTIONSTATE_NORMAL; /* The first publish response is sent immediately */
  41. TAILQ_INIT(&newSub->retransmissionQueue);
  42. TAILQ_INIT(&newSub->notificationQueue);
  43. return newSub;
  44. }
  45. void
  46. UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub) {
  47. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  48. "Delete the subscription", sub->subscriptionId);
  49. Subscription_unregisterPublishCallback(server, sub);
  50. /* Delete monitored Items */
  51. UA_MonitoredItem *mon, *tmp_mon;
  52. LIST_FOREACH_SAFE(mon, &sub->monitoredItems, listEntry, tmp_mon) {
  53. LIST_REMOVE(mon, listEntry);
  54. UA_MonitoredItem_delete(server, mon);
  55. }
  56. sub->monitoredItemsSize = 0;
  57. /* Delete Retransmission Queue */
  58. UA_NotificationMessageEntry *nme, *nme_tmp;
  59. TAILQ_FOREACH_SAFE(nme, &sub->retransmissionQueue, listEntry, nme_tmp) {
  60. TAILQ_REMOVE(&sub->retransmissionQueue, nme, listEntry);
  61. UA_NotificationMessage_deleteMembers(&nme->message);
  62. UA_free(nme);
  63. }
  64. sub->retransmissionQueueSize = 0;
  65. }
  66. UA_MonitoredItem *
  67. UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId) {
  68. UA_MonitoredItem *mon;
  69. LIST_FOREACH(mon, &sub->monitoredItems, listEntry) {
  70. if(mon->monitoredItemId == monitoredItemId)
  71. break;
  72. }
  73. return mon;
  74. }
  75. UA_StatusCode
  76. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  77. UA_UInt32 monitoredItemId) {
  78. /* Find the MonitoredItem */
  79. UA_MonitoredItem *mon;
  80. LIST_FOREACH(mon, &sub->monitoredItems, listEntry) {
  81. if(mon->monitoredItemId == monitoredItemId)
  82. break;
  83. }
  84. if(!mon)
  85. return UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  86. /* Remove the MonitoredItem */
  87. LIST_REMOVE(mon, listEntry);
  88. sub->monitoredItemsSize--;
  89. /* Remove content and delayed free */
  90. UA_MonitoredItem_delete(server, mon);
  91. return UA_STATUSCODE_GOOD;
  92. }
  93. void
  94. UA_Subscription_addMonitoredItem(UA_Subscription *sub, UA_MonitoredItem *newMon) {
  95. sub->monitoredItemsSize++;
  96. LIST_INSERT_HEAD(&sub->monitoredItems, newMon, listEntry);
  97. }
  98. static void
  99. UA_Subscription_addRetransmissionMessage(UA_Server *server, UA_Subscription *sub,
  100. UA_NotificationMessageEntry *entry) {
  101. /* Release the oldest entry if there is not enough space */
  102. if(server->config.maxRetransmissionQueueSize > 0 &&
  103. sub->retransmissionQueueSize >= server->config.maxRetransmissionQueueSize) {
  104. UA_NotificationMessageEntry *lastentry =
  105. TAILQ_LAST(&sub->retransmissionQueue, ListOfNotificationMessages);
  106. TAILQ_REMOVE(&sub->retransmissionQueue, lastentry, listEntry);
  107. --sub->retransmissionQueueSize;
  108. UA_NotificationMessage_deleteMembers(&lastentry->message);
  109. UA_free(lastentry);
  110. }
  111. /* Add entry */
  112. TAILQ_INSERT_HEAD(&sub->retransmissionQueue, entry, listEntry);
  113. ++sub->retransmissionQueueSize;
  114. }
  115. UA_StatusCode
  116. UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub, UA_UInt32 sequenceNumber) {
  117. /* Find the retransmission message */
  118. UA_NotificationMessageEntry *entry;
  119. TAILQ_FOREACH(entry, &sub->retransmissionQueue, listEntry) {
  120. if(entry->message.sequenceNumber == sequenceNumber)
  121. break;
  122. }
  123. if(!entry)
  124. return UA_STATUSCODE_BADSEQUENCENUMBERUNKNOWN;
  125. /* Remove the retransmission message */
  126. TAILQ_REMOVE(&sub->retransmissionQueue, entry, listEntry);
  127. --sub->retransmissionQueueSize;
  128. UA_NotificationMessage_deleteMembers(&entry->message);
  129. UA_free(entry);
  130. return UA_STATUSCODE_GOOD;
  131. }
  132. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  133. /* EventChange: Iterate over the monitoredItems of the subscription, starting at mon, and
  134. * move notifications into the response. */
  135. static void
  136. Events_moveNotificationsFromMonitoredItems(UA_Server *server, UA_Subscription *sub, UA_EventFieldList *efls,
  137. size_t eflsSize) {
  138. UA_StatusCode retval;
  139. size_t pos = 0;
  140. UA_Notification *notification, *notification_tmp;
  141. TAILQ_FOREACH_SAFE(notification, &sub->notificationQueue, globalEntry, notification_tmp) {
  142. if (pos >= eflsSize) {
  143. return;
  144. }
  145. UA_MonitoredItem *mon = notification->mon;
  146. /* Remove the notification from the queues */
  147. TAILQ_REMOVE(&sub->notificationQueue, notification, globalEntry);
  148. TAILQ_REMOVE(&mon->queue, notification, listEntry);
  149. /* removing an overflowEvent should not reduce the queueSize */
  150. UA_NodeId overflowId = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  151. if (!(notification->data.event.fields.eventFieldsSize == 1
  152. && notification->data.event.fields.eventFields->type == &UA_TYPES[UA_TYPES_NODEID]
  153. && UA_NodeId_equal((UA_NodeId *)notification->data.event.fields.eventFields->data, &overflowId))) {
  154. --mon->queueSize;
  155. --sub->notificationQueueSize;
  156. }
  157. /* Move the content to the response */
  158. UA_EventFieldList *efl = &efls[pos];
  159. efl->clientHandle = mon->clientHandle;
  160. efl->eventFieldsSize = notification->data.event.fields.eventFieldsSize;
  161. retval = UA_Array_copy(notification->data.event.fields.eventFields,
  162. notification->data.event.fields.eventFieldsSize,
  163. (void **) &efl->eventFields, &UA_TYPES[UA_TYPES_VARIANT]);
  164. if (retval != UA_STATUSCODE_GOOD) {
  165. return;
  166. }
  167. /* EventFilterResult currently isn't being used
  168. UA_EventFilterResult_deleteMembers(&notification->data.event.result); */
  169. UA_EventFieldList_deleteMembers(&notification->data.event.fields);
  170. UA_free(notification);
  171. }
  172. }
  173. #endif
  174. /* DataChange: Iterate over the monitoreditems of the subscription, starting at mon, and
  175. * move notifications into the response. */
  176. static void
  177. DataChange_moveNotificationsFromMonitoredItems(UA_Subscription *sub, UA_MonitoredItemNotification *mins,
  178. size_t minsSize) {
  179. size_t pos = 0;
  180. UA_Notification *notification, *notification_tmp;
  181. TAILQ_FOREACH_SAFE(notification, &sub->notificationQueue, globalEntry, notification_tmp) {
  182. if(pos >= minsSize)
  183. return;
  184. UA_MonitoredItem *mon = notification->mon;
  185. /* Remove the notification from the queues */
  186. TAILQ_REMOVE(&sub->notificationQueue, notification, globalEntry);
  187. TAILQ_REMOVE(&mon->queue, notification, listEntry);
  188. --mon->queueSize;
  189. --sub->notificationQueueSize;
  190. /* Move the content to the response */
  191. UA_MonitoredItemNotification *min = &mins[pos];
  192. min->clientHandle = mon->clientHandle;
  193. min->value = notification->data.value;
  194. UA_free(notification);
  195. ++pos;
  196. }
  197. }
  198. static UA_StatusCode
  199. prepareNotificationMessage(UA_Server *server, UA_Subscription *sub, UA_NotificationMessage *message,
  200. size_t notifications) {
  201. /* Array of ExtensionObject to hold different kinds of notifications
  202. * (currently only DataChangeNotifications) */
  203. message->notificationData = UA_ExtensionObject_new();
  204. if(!message->notificationData)
  205. return UA_STATUSCODE_BADOUTOFMEMORY;
  206. message->notificationDataSize = 1;
  207. UA_ExtensionObject *data = message->notificationData;
  208. data->encoding = UA_EXTENSIONOBJECT_DECODED;
  209. /* TODO: basing type of notificationtype off of first monitoredItem in subscription which isnt very good */
  210. /* Allocate Notification */
  211. if (LIST_FIRST(&sub->monitoredItems)->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  212. UA_DataChangeNotification *dcn = UA_DataChangeNotification_new();
  213. if (!dcn) {
  214. UA_NotificationMessage_deleteMembers(message);
  215. return UA_STATUSCODE_BADOUTOFMEMORY;
  216. }
  217. data->content.decoded.data = dcn;
  218. data->content.decoded.type = &UA_TYPES[UA_TYPES_DATACHANGENOTIFICATION];
  219. /* Allocate array of notifications */
  220. dcn->monitoredItems = (UA_MonitoredItemNotification *)
  221. UA_Array_new(notifications,
  222. &UA_TYPES[UA_TYPES_MONITOREDITEMNOTIFICATION]);
  223. if(!dcn->monitoredItems) {
  224. UA_NotificationMessage_deleteMembers(message);
  225. return UA_STATUSCODE_BADOUTOFMEMORY;
  226. }
  227. dcn->monitoredItemsSize = notifications;
  228. /* Move notifications into the response .. the point of no return */
  229. DataChange_moveNotificationsFromMonitoredItems(sub, dcn->monitoredItems, notifications);
  230. }
  231. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  232. else if (LIST_FIRST(&sub->monitoredItems)->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  233. UA_EventNotificationList *enl = UA_EventNotificationList_new();
  234. if (!enl) {
  235. UA_NotificationMessage_deleteMembers(message);
  236. return UA_STATUSCODE_BADOUTOFMEMORY;
  237. }
  238. UA_EventNotificationList_init(enl);
  239. data->content.decoded.data = enl;
  240. data->content.decoded.type = &UA_TYPES[UA_TYPES_EVENTNOTIFICATIONLIST];
  241. /* Allocate array of notifications */
  242. enl->events = (UA_EventFieldList *) UA_Array_new(notifications, &UA_TYPES[UA_TYPES_EVENTFIELDLIST]);
  243. if (!enl->events) {
  244. UA_NotificationMessage_deleteMembers(message);
  245. return UA_STATUSCODE_BADOUTOFMEMORY;
  246. }
  247. enl->eventsSize = notifications;
  248. /* Move the list into the response .. the point of no return */
  249. Events_moveNotificationsFromMonitoredItems(server, sub, enl->events, notifications);
  250. }
  251. #endif
  252. else {
  253. return UA_STATUSCODE_BADNOTIMPLEMENTED;
  254. }
  255. return UA_STATUSCODE_GOOD;
  256. }
  257. /* According to OPC Unified Architecture, Part 4 5.13.1.1 i) The value 0 is
  258. * never used for the sequence number */
  259. static UA_UInt32
  260. UA_Subscription_nextSequenceNumber(UA_UInt32 sequenceNumber) {
  261. UA_UInt32 nextSequenceNumber = sequenceNumber + 1;
  262. if(nextSequenceNumber == 0)
  263. nextSequenceNumber = 1;
  264. return nextSequenceNumber;
  265. }
  266. static void
  267. publishCallback(UA_Server *server, UA_Subscription *sub) {
  268. sub->readyNotifications = sub->notificationQueueSize;
  269. UA_Subscription_publish(server, sub);
  270. }
  271. void
  272. UA_Subscription_publish(UA_Server *server, UA_Subscription *sub) {
  273. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  274. "Publish Callback", sub->subscriptionId);
  275. /* Dequeue a response */
  276. UA_PublishResponseEntry *pre = UA_Session_dequeuePublishReq(sub->session);
  277. if(pre) {
  278. sub->currentLifetimeCount = 0; /* Reset the LifetimeCounter */
  279. } else {
  280. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  281. "Subscription %u | The publish queue is empty",
  282. sub->subscriptionId);
  283. ++sub->currentLifetimeCount;
  284. if(sub->currentLifetimeCount > sub->lifeTimeCount) {
  285. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  286. "Subscription %u | End of lifetime "
  287. "for subscription", sub->subscriptionId);
  288. UA_Session_deleteSubscription(server, sub->session, sub->subscriptionId);
  289. /* TODO: send a StatusChangeNotification with Bad_Timeout */
  290. return;
  291. }
  292. }
  293. if (sub->readyNotifications > sub->notificationQueueSize)
  294. sub->readyNotifications = sub->notificationQueueSize;
  295. /* Count the available notifications */
  296. UA_UInt32 notifications = sub->readyNotifications;
  297. if(!sub->publishingEnabled)
  298. notifications = 0;
  299. UA_Boolean moreNotifications = false;
  300. if(notifications > sub->notificationsPerPublish) {
  301. notifications = sub->notificationsPerPublish;
  302. moreNotifications = true;
  303. }
  304. /* Return if no notifications and no keepalive */
  305. if(notifications == 0) {
  306. ++sub->currentKeepAliveCount;
  307. if(sub->currentKeepAliveCount < sub->maxKeepAliveCount) {
  308. if(pre)
  309. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  310. return;
  311. }
  312. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  313. "Subscription %u | Sending a KeepAlive",
  314. sub->subscriptionId);
  315. }
  316. /* We want to send a response. Is it possible? */
  317. UA_SecureChannel *channel = sub->session->header.channel;
  318. if(!channel || !pre) {
  319. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  320. "Subscription %u | Want to send a publish response but can't. "
  321. "The subscription is late.", sub->subscriptionId);
  322. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  323. if(pre)
  324. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  325. return;
  326. }
  327. /* Prepare the response */
  328. UA_PublishResponse *response = &pre->response;
  329. UA_NotificationMessage *message = &response->notificationMessage;
  330. UA_NotificationMessageEntry *retransmission = NULL;
  331. if(notifications > 0) {
  332. /* Allocate the retransmission entry */
  333. retransmission = (UA_NotificationMessageEntry*)UA_malloc(sizeof(UA_NotificationMessageEntry));
  334. if(!retransmission) {
  335. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  336. "Subscription %u | Could not allocate memory for retransmission. "
  337. "The subscription is late.", sub->subscriptionId);
  338. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  339. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  340. return;
  341. }
  342. /* Prepare the response */
  343. UA_StatusCode retval = prepareNotificationMessage(server, sub, message, notifications);
  344. if(retval != UA_STATUSCODE_GOOD) {
  345. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  346. "Subscription %u | Could not prepare the notification message. "
  347. "The subscription is late.", sub->subscriptionId);
  348. UA_free(retransmission);
  349. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  350. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  351. return;
  352. }
  353. }
  354. /* <-- The point of no return --> */
  355. /* Adjust the number of ready notifications */
  356. UA_assert(sub->readyNotifications >= notifications);
  357. sub->readyNotifications -= notifications;
  358. /* Set up the response */
  359. response->responseHeader.timestamp = UA_DateTime_now();
  360. response->subscriptionId = sub->subscriptionId;
  361. response->moreNotifications = moreNotifications;
  362. message->publishTime = response->responseHeader.timestamp;
  363. /* Set the sequence number. The sequence number will be reused if there are
  364. * no notifications (and this is a keepalive message). */
  365. message->sequenceNumber = UA_Subscription_nextSequenceNumber(sub->sequenceNumber);
  366. if(notifications != 0) {
  367. /* There are notifications. So we can't reuse the sequence number. */
  368. sub->sequenceNumber = message->sequenceNumber;
  369. /* Put the notification message into the retransmission queue. This
  370. * needs to be done here, so that the message itself is included in the
  371. * available sequence numbers for acknowledgement. */
  372. retransmission->message = response->notificationMessage;
  373. UA_Subscription_addRetransmissionMessage(server, sub, retransmission);
  374. }
  375. /* Get the available sequence numbers from the retransmission queue */
  376. size_t available = sub->retransmissionQueueSize;
  377. UA_STACKARRAY(UA_UInt32, seqNumbers, available);
  378. if(available > 0) {
  379. response->availableSequenceNumbers = seqNumbers;
  380. response->availableSequenceNumbersSize = available;
  381. size_t i = 0;
  382. UA_NotificationMessageEntry *nme;
  383. TAILQ_FOREACH(nme, &sub->retransmissionQueue, listEntry) {
  384. response->availableSequenceNumbers[i] = nme->message.sequenceNumber;
  385. ++i;
  386. }
  387. }
  388. /* Send the response */
  389. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  390. "Subscription %u | Sending out a publish response "
  391. "with %u notifications", sub->subscriptionId,
  392. (UA_UInt32)notifications);
  393. UA_SecureChannel_sendSymmetricMessage(sub->session->header.channel, pre->requestId,
  394. UA_MESSAGETYPE_MSG, response,
  395. &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  396. /* Reset subscription state to normal */
  397. sub->state = UA_SUBSCRIPTIONSTATE_NORMAL;
  398. sub->currentKeepAliveCount = 0;
  399. /* Free the response */
  400. UA_Array_delete(response->results, response->resultsSize, &UA_TYPES[UA_TYPES_UINT32]);
  401. UA_free(pre); /* No need for UA_PublishResponse_deleteMembers */
  402. /* Repeat sending responses if there are more notifications to send */
  403. if(moreNotifications)
  404. UA_Subscription_publish(server, sub);
  405. }
  406. UA_Boolean
  407. UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session) {
  408. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Reached number of publish request limit");
  409. /* Dequeue a response */
  410. UA_PublishResponseEntry *pre = UA_Session_dequeuePublishReq(session);
  411. /* Cannot publish without a response */
  412. if(!pre) {
  413. UA_LOG_FATAL_SESSION(server->config.logger, session, "No publish requests available");
  414. return false;
  415. }
  416. /* <-- The point of no return --> */
  417. UA_PublishResponse *response = &pre->response;
  418. UA_NotificationMessage *message = &response->notificationMessage;
  419. /* Set up the response. Note that this response has no related subscription id */
  420. response->responseHeader.timestamp = UA_DateTime_now();
  421. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYPUBLISHREQUESTS;
  422. response->subscriptionId = 0;
  423. response->moreNotifications = false;
  424. message->publishTime = response->responseHeader.timestamp;
  425. message->sequenceNumber = 0;
  426. response->availableSequenceNumbersSize = 0;
  427. /* Send the response */
  428. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  429. "Sending out a publish response triggered by too many publish requests");
  430. UA_SecureChannel_sendSymmetricMessage(session->header.channel, pre->requestId,
  431. UA_MESSAGETYPE_MSG, response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  432. /* Free the response */
  433. UA_Array_delete(response->results, response->resultsSize, &UA_TYPES[UA_TYPES_UINT32]);
  434. UA_free(pre); /* no need for UA_PublishResponse_deleteMembers */
  435. return true;
  436. }
  437. UA_StatusCode
  438. Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub) {
  439. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  440. "Subscription %u | Register subscription "
  441. "publishing callback", sub->subscriptionId);
  442. if(sub->publishCallbackIsRegistered)
  443. return UA_STATUSCODE_GOOD;
  444. UA_StatusCode retval =
  445. UA_Server_addRepeatedCallback(server, (UA_ServerCallback)publishCallback,
  446. sub, (UA_UInt32)sub->publishingInterval, &sub->publishCallbackId);
  447. if(retval != UA_STATUSCODE_GOOD)
  448. return retval;
  449. sub->publishCallbackIsRegistered = true;
  450. return UA_STATUSCODE_GOOD;
  451. }
  452. UA_StatusCode
  453. Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub) {
  454. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  455. "Unregister subscription publishing callback", sub->subscriptionId);
  456. if(!sub->publishCallbackIsRegistered)
  457. return UA_STATUSCODE_GOOD;
  458. UA_StatusCode retval = UA_Server_removeRepeatedCallback(server, sub->publishCallbackId);
  459. if(retval != UA_STATUSCODE_GOOD)
  460. return retval;
  461. sub->publishCallbackIsRegistered = false;
  462. return UA_STATUSCODE_GOOD;
  463. }
  464. /* When the session has publish requests stored but the last subscription is
  465. * deleted... Send out empty responses */
  466. void
  467. UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session) {
  468. /* No session or there are remaining subscriptions */
  469. if(!session || LIST_FIRST(&session->serverSubscriptions))
  470. return;
  471. /* Send a response for every queued request */
  472. UA_PublishResponseEntry *pre;
  473. while((pre = UA_Session_dequeuePublishReq(session))) {
  474. UA_PublishResponse *response = &pre->response;
  475. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOSUBSCRIPTION;
  476. response->responseHeader.timestamp = UA_DateTime_now();
  477. UA_SecureChannel_sendSymmetricMessage(session->header.channel, pre->requestId, UA_MESSAGETYPE_MSG,
  478. response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  479. UA_PublishResponse_deleteMembers(response);
  480. UA_free(pre);
  481. }
  482. }
  483. #endif /* UA_ENABLE_SUBSCRIPTIONS */