ua_services_subscription.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "ua_services.h"
  2. #include "ua_server_internal.h"
  3. #include "ua_subscription_manager.h"
  4. #include "ua_subscription.h"
  5. #include "ua_statuscodes.h"
  6. #include "ua_util.h"
  7. #include "ua_nodestore.h"
  8. #define UA_BOUNDEDVALUE_SETWBOUNDS(BOUNDS, SRC, DST) { \
  9. if(SRC > BOUNDS.maxValue) DST = BOUNDS.maxValue; \
  10. else if(SRC < BOUNDS.minValue) DST = BOUNDS.minValue; \
  11. else DST = SRC; \
  12. }
  13. void Service_CreateSubscription(UA_Server *server, UA_Session *session,
  14. const UA_CreateSubscriptionRequest *request,
  15. UA_CreateSubscriptionResponse *response) {
  16. response->subscriptionId = SubscriptionManager_getUniqueUIntID(&session->subscriptionManager);
  17. UA_Subscription *newSubscription = UA_Subscription_new(response->subscriptionId);
  18. /* set the publishing interval */
  19. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalPublishingInterval,
  20. request->requestedPublishingInterval, response->revisedPublishingInterval);
  21. newSubscription->publishingInterval = response->revisedPublishingInterval;
  22. /* set the subscription lifetime (deleted when no publish requests arrive within this time) */
  23. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalLifeTimeCount,
  24. request->requestedLifetimeCount, response->revisedLifetimeCount);
  25. newSubscription->lifeTime = (UA_UInt32_BoundedValue) {
  26. .minValue=session->subscriptionManager.globalLifeTimeCount.minValue,
  27. .maxValue=session->subscriptionManager.globalLifeTimeCount.maxValue,
  28. .currentValue=response->revisedLifetimeCount};
  29. /* set the keepalive count. the server sends an empty notification when
  30. nothin has happened for n publishing intervals */
  31. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalKeepAliveCount,
  32. request->requestedMaxKeepAliveCount, response->revisedMaxKeepAliveCount);
  33. newSubscription->keepAliveCount = (UA_Int32_BoundedValue) {
  34. .minValue=session->subscriptionManager.globalKeepAliveCount.minValue,
  35. .maxValue=session->subscriptionManager.globalKeepAliveCount.maxValue,
  36. .currentValue=response->revisedMaxKeepAliveCount};
  37. newSubscription->notificationsPerPublish = request->maxNotificationsPerPublish;
  38. newSubscription->publishingMode = request->publishingEnabled;
  39. newSubscription->priority = request->priority;
  40. /* add the update job */
  41. UA_Guid jobId = SubscriptionManager_getUniqueGUID(&session->subscriptionManager);
  42. Subscription_createdUpdateJob(server, jobId, newSubscription);
  43. Subscription_registerUpdateJob(server, newSubscription);
  44. SubscriptionManager_addSubscription(&session->subscriptionManager, newSubscription);
  45. }
  46. static void createMonitoredItems(UA_Server *server, UA_Session *session, UA_Subscription *sub,
  47. const UA_MonitoredItemCreateRequest *request,
  48. UA_MonitoredItemCreateResult *result) {
  49. const UA_Node *target = UA_NodeStore_get(server->nodestore, &request->itemToMonitor.nodeId);
  50. if(!target) {
  51. result->statusCode = UA_STATUSCODE_BADNODEIDINVALID;
  52. return;
  53. }
  54. UA_MonitoredItem *newMon = UA_MonitoredItem_new();
  55. if(!newMon) {
  56. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  57. UA_NodeStore_release(target);
  58. return;
  59. }
  60. UA_StatusCode retval = UA_NodeId_copy(&target->nodeId, &newMon->monitoredNodeId);
  61. if(retval != UA_STATUSCODE_GOOD) {
  62. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  63. MonitoredItem_delete(newMon);
  64. UA_NodeStore_release(target);
  65. return;
  66. }
  67. newMon->itemId = ++(session->subscriptionManager.lastSessionID);
  68. result->monitoredItemId = newMon->itemId;
  69. newMon->clientHandle = request->requestedParameters.clientHandle;
  70. /* set the sampling interval */
  71. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalSamplingInterval,
  72. request->requestedParameters.samplingInterval,
  73. result->revisedSamplingInterval);
  74. newMon->samplingInterval = result->revisedSamplingInterval;
  75. /* set the queue size */
  76. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalQueueSize,
  77. request->requestedParameters.queueSize,
  78. result->revisedQueueSize);
  79. newMon->queueSize = (UA_UInt32_BoundedValue) {
  80. .maxValue=(result->revisedQueueSize) + 1,
  81. .minValue=0, .currentValue=0 };
  82. newMon->attributeID = request->itemToMonitor.attributeId;
  83. newMon->monitoredItemType = MONITOREDITEM_TYPE_CHANGENOTIFY;
  84. newMon->discardOldest = request->requestedParameters.discardOldest;
  85. LIST_INSERT_HEAD(&sub->MonitoredItems, newMon, listEntry);
  86. // todo: add a job that samples the value (for fixed intervals)
  87. // todo: add a pointer to the monitoreditem to the variable, so that events get propagated
  88. UA_NodeStore_release(target);
  89. }
  90. void Service_CreateMonitoredItems(UA_Server *server, UA_Session *session,
  91. const UA_CreateMonitoredItemsRequest *request,
  92. UA_CreateMonitoredItemsResponse *response) {
  93. UA_Subscription *sub = SubscriptionManager_getSubscriptionByID(&session->subscriptionManager,
  94. request->subscriptionId);
  95. if(!sub) {
  96. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  97. return;
  98. }
  99. if(request->itemsToCreateSize <= 0) {
  100. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
  101. return;
  102. }
  103. response->results = UA_Array_new(&UA_TYPES[UA_TYPES_MONITOREDITEMCREATERESULT], request->itemsToCreateSize);
  104. if(!response->results) {
  105. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  106. return;
  107. }
  108. response->resultsSize = request->itemsToCreateSize;
  109. for(UA_Int32 i = 0; i<request->itemsToCreateSize; i++)
  110. createMonitoredItems(server, session, sub, &request->itemsToCreate[i], &response->results[i]);
  111. }
  112. void Service_Publish(UA_Server *server, UA_Session *session, const UA_PublishRequest *request,
  113. UA_PublishResponse *response) {
  114. UA_SubscriptionManager *manager= &session->subscriptionManager;
  115. if(!manager)
  116. return;
  117. // Delete Acknowledged Subscription Messages
  118. response->resultsSize = request->subscriptionAcknowledgementsSize;
  119. response->results = UA_malloc(sizeof(UA_StatusCode)*(response->resultsSize));
  120. for(UA_Int32 i = 0; i < request->subscriptionAcknowledgementsSize; i++) {
  121. response->results[i] = UA_STATUSCODE_GOOD;
  122. UA_Subscription *sub =
  123. SubscriptionManager_getSubscriptionByID(&session->subscriptionManager,
  124. request->subscriptionAcknowledgements[i].subscriptionId);
  125. if(!sub) {
  126. response->results[i] = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  127. continue;
  128. }
  129. if(Subscription_deleteUnpublishedNotification(request->subscriptionAcknowledgements[i].sequenceNumber, sub) == 0)
  130. response->results[i] = UA_STATUSCODE_BADSEQUENCENUMBERINVALID;
  131. }
  132. // See if any new data is available
  133. UA_Subscription *sub;
  134. LIST_FOREACH(sub, &manager->serverSubscriptions, listEntry) {
  135. if(sub->timedUpdateIsRegistered == UA_FALSE) {
  136. // FIXME: We are forcing a value update for monitored items. This should be done by the event system.
  137. // NOTE: There is a clone of this functionality in the Subscription_timedUpdateNotificationsJob
  138. UA_MonitoredItem *mon;
  139. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry)
  140. MonitoredItem_QueuePushDataValue(server, mon);
  141. // FIXME: We are forcing notification updates for the subscription. This
  142. // should be done by a timed work item.
  143. Subscription_updateNotifications(sub);
  144. }
  145. if(Subscription_queuedNotifications(sub) <= 0)
  146. continue;
  147. response->subscriptionId = sub->subscriptionID;
  148. Subscription_copyTopNotificationMessage(&response->notificationMessage, sub);
  149. if(sub->unpublishedNotifications.lh_first->notification->sequenceNumber > sub->sequenceNumber) {
  150. // If this is a keepalive message, its seqNo is the next seqNo to be used for an actual msg.
  151. response->availableSequenceNumbersSize = 0;
  152. // .. and must be deleted
  153. Subscription_deleteUnpublishedNotification(sub->sequenceNumber + 1, sub);
  154. } else {
  155. response->availableSequenceNumbersSize = Subscription_queuedNotifications(sub);
  156. response->availableSequenceNumbers = Subscription_getAvailableSequenceNumbers(sub);
  157. }
  158. // FIXME: This should be in processMSG();
  159. session->validTill = UA_DateTime_now() + session->timeout * 10000;
  160. return;
  161. }
  162. // FIXME: At this point, we would return nothing and "queue" the publish
  163. // request, but currently we need to return something to the client. If no
  164. // subscriptions have notifications, force one to generate a keepalive so we
  165. // don't return an empty message
  166. sub = LIST_FIRST(&manager->serverSubscriptions);
  167. if(!sub) {
  168. response->subscriptionId = sub->subscriptionID;
  169. sub->keepAliveCount.currentValue=sub->keepAliveCount.minValue;
  170. Subscription_generateKeepAlive(sub);
  171. Subscription_copyTopNotificationMessage(&response->notificationMessage, sub);
  172. Subscription_deleteUnpublishedNotification(sub->sequenceNumber + 1, sub);
  173. }
  174. // FIXME: This should be in processMSG();
  175. session->validTill = UA_DateTime_now() + session->timeout * 10000;
  176. }
  177. void Service_ModifySubscription(UA_Server *server, UA_Session *session,
  178. const UA_ModifySubscriptionRequest *request,
  179. UA_ModifySubscriptionResponse *response) {
  180. UA_Subscription *sub = SubscriptionManager_getSubscriptionByID(&session->subscriptionManager,
  181. request->subscriptionId);
  182. if(!sub) {
  183. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  184. return;
  185. }
  186. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalPublishingInterval,
  187. request->requestedPublishingInterval, response->revisedPublishingInterval);
  188. sub->publishingInterval = response->revisedPublishingInterval;
  189. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalLifeTimeCount,
  190. request->requestedLifetimeCount, response->revisedLifetimeCount);
  191. sub->lifeTime = (UA_UInt32_BoundedValue) {
  192. .minValue=session->subscriptionManager.globalLifeTimeCount.minValue,
  193. .maxValue=session->subscriptionManager.globalLifeTimeCount.maxValue,
  194. .currentValue=response->revisedLifetimeCount};
  195. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.globalKeepAliveCount,
  196. request->requestedMaxKeepAliveCount, response->revisedMaxKeepAliveCount);
  197. sub->keepAliveCount = (UA_Int32_BoundedValue) {
  198. .minValue=session->subscriptionManager.globalKeepAliveCount.minValue,
  199. .maxValue=session->subscriptionManager.globalKeepAliveCount.maxValue,
  200. .currentValue=response->revisedMaxKeepAliveCount};
  201. sub->notificationsPerPublish = request->maxNotificationsPerPublish;
  202. sub->priority = request->priority;
  203. Subscription_registerUpdateJob(server, sub);
  204. return;
  205. }
  206. void Service_DeleteSubscriptions(UA_Server *server, UA_Session *session,
  207. const UA_DeleteSubscriptionsRequest *request,
  208. UA_DeleteSubscriptionsResponse *response) {
  209. response->results = UA_malloc(sizeof(UA_StatusCode) * request->subscriptionIdsSize);
  210. if(!response->results) {
  211. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  212. return;
  213. }
  214. response->resultsSize = request->subscriptionIdsSize;
  215. for(UA_Int32 i = 0; i < request->subscriptionIdsSize; i++)
  216. response->results[i] = SubscriptionManager_deleteSubscription(server, &session->subscriptionManager,
  217. request->subscriptionIds[i]);
  218. }
  219. void Service_DeleteMonitoredItems(UA_Server *server, UA_Session *session,
  220. const UA_DeleteMonitoredItemsRequest *request,
  221. UA_DeleteMonitoredItemsResponse *response) {
  222. UA_SubscriptionManager *manager = &session->subscriptionManager;
  223. UA_Subscription *sub = SubscriptionManager_getSubscriptionByID(manager, request->subscriptionId);
  224. if(!sub) {
  225. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  226. return;
  227. }
  228. response->results = UA_malloc(sizeof(UA_StatusCode) * request->monitoredItemIdsSize);
  229. if(!response->results) {
  230. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  231. return;
  232. }
  233. response->resultsSize = request->monitoredItemIdsSize;
  234. for(UA_Int32 i = 0; i < request->monitoredItemIdsSize; i++)
  235. response->results[i] = SubscriptionManager_deleteMonitoredItem(manager, sub->subscriptionID,
  236. request->monitoredItemIds[i]);
  237. }