ua_subscription.c 23 KB

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