ua_services_subscription.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #ifdef ENABLE_SUBSCRIPTIONS
  2. #include "ua_services.h"
  3. #include "ua_server_internal.h"
  4. #include "ua_subscription_manager.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. UA_Subscription *newSubscription;
  17. // Create Subscription and Response
  18. response->subscriptionId = ++(session->subscriptionManager.LastSessionID);
  19. newSubscription = UA_Subscription_new(response->subscriptionId);
  20. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.GlobalPublishingInterval, request->requestedPublishingInterval, response->revisedPublishingInterval);
  21. newSubscription->PublishingInterval = response->revisedPublishingInterval;
  22. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.GlobalLifeTimeCount, request->requestedLifetimeCount, response->revisedLifetimeCount);
  23. newSubscription->LifeTime = (UA_UInt32_BoundedValue) { .minValue=session->subscriptionManager.GlobalLifeTimeCount.minValue, .maxValue=session->subscriptionManager.GlobalLifeTimeCount.maxValue, .currentValue=response->revisedLifetimeCount};
  24. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.GlobalKeepAliveCount, request->requestedMaxKeepAliveCount, response->revisedMaxKeepAliveCount);
  25. newSubscription->KeepAliveCount = (UA_Int32_BoundedValue) { .minValue=session->subscriptionManager.GlobalKeepAliveCount.minValue, .maxValue=session->subscriptionManager.GlobalKeepAliveCount.maxValue, .currentValue=response->revisedMaxKeepAliveCount};
  26. newSubscription->NotificationsPerPublish = request->maxNotificationsPerPublish;
  27. newSubscription->PublishingMode = request->publishingEnabled;
  28. newSubscription->Priority = request->priority;
  29. SubscriptionManager_addSubscription(&(session->subscriptionManager), newSubscription);
  30. }
  31. void Service_CreateMonitoredItems(UA_Server *server, UA_Session *session,
  32. const UA_CreateMonitoredItemsRequest *request,
  33. UA_CreateMonitoredItemsResponse *response) {
  34. UA_MonitoredItem *newMon;
  35. UA_Subscription *sub;
  36. UA_MonitoredItemCreateResult *createResults, *thisItemsResult;
  37. UA_MonitoredItemCreateRequest *thisItemsRequest;
  38. // Verify Session and Subscription
  39. sub = SubscriptionManager_getSubscriptionByID(&(session->subscriptionManager), request->subscriptionId);
  40. if(!sub) {
  41. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  42. return;
  43. }
  44. // Allocate Result Array
  45. if (request->itemsToCreateSize > 0) {
  46. createResults = (UA_MonitoredItemCreateResult *) UA_malloc(sizeof(UA_MonitoredItemCreateResult) * (request->itemsToCreateSize));
  47. if (createResults == NULL) {
  48. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  49. return;
  50. }
  51. }
  52. else
  53. return;
  54. response->resultsSize = request->itemsToCreateSize;
  55. response->results = createResults;
  56. for(int i=0; i<request->itemsToCreateSize; i++) {
  57. thisItemsRequest = &(request->itemsToCreate[i]);
  58. thisItemsResult = &(createResults[i]);
  59. // FIXME: Completely ignoring filters for now!
  60. thisItemsResult->filterResult.typeId = UA_NODEID_NULL;
  61. thisItemsResult->filterResult.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED;
  62. thisItemsResult->filterResult.body = UA_BYTESTRING_NULL;
  63. if (UA_NodeStore_get(server->nodestore, (const UA_NodeId *) &(thisItemsRequest->itemToMonitor.nodeId)) == UA_NULL) {
  64. thisItemsResult->statusCode = UA_STATUSCODE_BADNODEIDINVALID;
  65. thisItemsResult->monitoredItemId = 0;
  66. thisItemsResult->revisedSamplingInterval = 0;
  67. thisItemsResult->revisedQueueSize = 0;
  68. }
  69. else {
  70. thisItemsResult->statusCode = UA_STATUSCODE_GOOD;
  71. newMon = UA_MonitoredItem_new();
  72. newMon->monitoredNode = UA_NodeStore_get(server->nodestore, (const UA_NodeId *) &(thisItemsRequest->itemToMonitor.nodeId));
  73. newMon->ItemId = ++(session->subscriptionManager.LastSessionID);
  74. thisItemsResult->monitoredItemId = newMon->ItemId;
  75. newMon->ClientHandle = thisItemsRequest->requestedParameters.clientHandle;
  76. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.GlobalSamplingInterval , thisItemsRequest->requestedParameters.samplingInterval, thisItemsResult->revisedSamplingInterval);
  77. newMon->SamplingInterval = thisItemsResult->revisedSamplingInterval;
  78. UA_BOUNDEDVALUE_SETWBOUNDS(session->subscriptionManager.GlobalQueueSize, thisItemsRequest->requestedParameters.queueSize, thisItemsResult->revisedQueueSize);
  79. newMon->QueueSize = (UA_UInt32_BoundedValue) { .maxValue=(thisItemsResult->revisedQueueSize) + 1, .minValue=0, .currentValue=0 };
  80. newMon->AttributeID = thisItemsRequest->itemToMonitor.attributeId;
  81. newMon->MonitoredItemType = MONITOREDITEM_TYPE_CHANGENOTIFY;
  82. newMon->DiscardOldest = thisItemsRequest->requestedParameters.discardOldest;
  83. LIST_INSERT_HEAD(&sub->MonitoredItems, newMon, listEntry);
  84. }
  85. }
  86. }
  87. void Service_Publish(UA_Server *server, UA_Session *session,
  88. const UA_PublishRequest *request, UA_PublishResponse *response) {
  89. UA_Subscription *sub;
  90. UA_MonitoredItem *mon;
  91. UA_SubscriptionManager *manager;
  92. // Verify Session and Subscription
  93. response->responseHeader.serviceResult = UA_STATUSCODE_GOOD;
  94. response->diagnosticInfosSize = 0;
  95. response->diagnosticInfos = 0;
  96. response->availableSequenceNumbersSize = 0;
  97. response->resultsSize = 0;
  98. response->subscriptionId = 0;
  99. response->moreNotifications = UA_FALSE;
  100. response->notificationMessage.notificationDataSize = 0;
  101. //printf("Validity: %lu, %lu\n", session->validTill - UA_DateTime_now(), session->timeout);
  102. manager = &(session->subscriptionManager);
  103. if (manager == NULL) return;
  104. // Delete Acknowledged Subscription Messages
  105. response->resultsSize = request->subscriptionAcknowledgementsSize;
  106. response->results = (UA_StatusCode *) UA_malloc(sizeof(UA_StatusCode)*(response->resultsSize));
  107. for(int i=0; i<request->subscriptionAcknowledgementsSize;i++ ) {
  108. response->results[i] = UA_STATUSCODE_GOOD;
  109. sub = SubscriptionManager_getSubscriptionByID(&(session->subscriptionManager), request->subscriptionAcknowledgements[i].subscriptionId);
  110. if (sub == NULL) {
  111. response->results[i] = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  112. continue;
  113. }
  114. if(Subscription_deleteUnpublishedNotification(request->subscriptionAcknowledgements[i].sequenceNumber, sub) == 0) {
  115. response->results[i] = UA_STATUSCODE_BADSEQUENCENUMBERINVALID;
  116. }
  117. }
  118. // See if any new data is available
  119. for (sub=(manager->ServerSubscriptions)->lh_first; sub != NULL; sub = sub->listEntry.le_next) {
  120. // FIXME: We are forcing a value update for monitored items. This should be done by the event system.
  121. if (sub->MonitoredItems.lh_first != NULL) {
  122. for(mon=sub->MonitoredItems.lh_first; mon != NULL; mon=mon->listEntry.le_next) {
  123. MonitoredItem_QueuePushDataValue(mon);
  124. }
  125. }
  126. // FIXME: We are forcing notification updates for the subscription. This should be done by a timed work item.
  127. Subscription_updateNotifications(sub);
  128. if (Subscription_queuedNotifications(sub) > 0) {
  129. response->subscriptionId = sub->SubscriptionID;
  130. Subscription_copyTopNotificationMessage(&(response->notificationMessage), sub);
  131. if (sub->unpublishedNotifications.lh_first->notification->sequenceNumber > sub->SequenceNumber) {
  132. // If this is a keepalive message, its seqNo is the next seqNo to be used for an actual msg.
  133. response->availableSequenceNumbersSize = 0;
  134. // .. and must be deleted
  135. Subscription_deleteUnpublishedNotification(sub->SequenceNumber + 1, sub);
  136. }
  137. else {
  138. response->availableSequenceNumbersSize = Subscription_queuedNotifications(sub);
  139. response->availableSequenceNumbers = Subscription_getAvailableSequenceNumbers(sub);
  140. }
  141. // FIXME: This should be in processMSG();
  142. session->validTill = UA_DateTime_now() + session->timeout * 10000;
  143. return;
  144. }
  145. }
  146. // FIXME: At this point, we would return nothing and "queue" the publish request, but currently we need to
  147. // return something to the client.
  148. // If no subscriptions have notifications, force one to generate a keepalive so we don't return an
  149. // empty message
  150. sub = manager->ServerSubscriptions->lh_first;
  151. if ( sub != NULL) {
  152. response->subscriptionId = sub->SubscriptionID;
  153. sub->KeepAliveCount.currentValue=sub->KeepAliveCount.minValue;
  154. Subscription_generateKeepAlive(sub);
  155. Subscription_copyTopNotificationMessage(&(response->notificationMessage), sub);
  156. Subscription_deleteUnpublishedNotification(sub->SequenceNumber + 1, sub);
  157. response->availableSequenceNumbersSize = 0;
  158. }
  159. // FIXME: This should be in processMSG();
  160. session->validTill = UA_DateTime_now() + session->timeout * 10000;
  161. response->diagnosticInfosSize = 0;
  162. response->diagnosticInfos = 0;
  163. }
  164. void Service_DeleteSubscriptions(UA_Server *server, UA_Session *session,
  165. const UA_DeleteSubscriptionsRequest *request,
  166. UA_DeleteSubscriptionsResponse *response) {
  167. response->results = UA_malloc(sizeof(UA_UInt32) * request->subscriptionIdsSize);
  168. if (!response->results) {
  169. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  170. return;
  171. }
  172. response->resultsSize = request->subscriptionIdsSize;
  173. for(int i=0; i<request->subscriptionIdsSize;i++)
  174. response->results[i] = SubscriptionManager_deleteSubscription(&(session->subscriptionManager),
  175. request->subscriptionIds[i]);
  176. }
  177. void Service_DeleteMonitoredItems(UA_Server *server, UA_Session *session,
  178. const UA_DeleteMonitoredItemsRequest *request,
  179. UA_DeleteMonitoredItemsResponse *response) {
  180. UA_SubscriptionManager *manager;
  181. UA_Subscription *sub;
  182. UA_Int32 *resultCodes;
  183. response->diagnosticInfosSize=0;
  184. response->resultsSize=0;
  185. manager = &(session->subscriptionManager);
  186. sub = SubscriptionManager_getSubscriptionByID(manager, request->subscriptionId);
  187. if (sub == NULL) {
  188. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  189. return;
  190. }
  191. resultCodes = (UA_Int32 *) UA_malloc(sizeof(UA_UInt32) * request->monitoredItemIdsSize);
  192. if (resultCodes == NULL) {
  193. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  194. return;
  195. }
  196. response->results = (UA_StatusCode *) resultCodes;
  197. response->resultsSize = request->monitoredItemIdsSize;
  198. for(int i=0; i < request->monitoredItemIdsSize; i++) {
  199. resultCodes[i] = SubscriptionManager_deleteMonitoredItem(manager, sub->SubscriptionID, (request->monitoredItemIds)[i]);
  200. }
  201. }
  202. #endif //#ifdef ENABLE_SUBSCRIPTIONS