ua_services_monitoreditem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2016-2017 (c) Florian Palm
  7. * Copyright 2015 (c) Chris Iatrou
  8. * Copyright 2015-2016 (c) Sten Grüner
  9. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  10. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2018 (c) Ari Breitkreuz, fortiss GmbH
  12. * Copyright 2017 (c) Mattias Bornhager
  13. * Copyright 2017 (c) Henrik Norrman
  14. * Copyright 2017-2018 (c) Thomas Stalder, Blue Time Concept SA
  15. * Copyright 2018 (c) Fabian Arndt, Root-Core
  16. */
  17. #include "ua_server_internal.h"
  18. #include "ua_services.h"
  19. #include "ua_subscription.h"
  20. #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
  21. static UA_StatusCode
  22. setMonitoredItemSettings(UA_Server *server, UA_MonitoredItem *mon,
  23. UA_MonitoringMode monitoringMode,
  24. const UA_MonitoringParameters *params,
  25. const UA_DataType* dataType) {
  26. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  27. if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
  28. /* Event MonitoredItem */
  29. #ifndef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  30. return UA_STATUSCODE_BADNOTSUPPORTED;
  31. #else
  32. if(params->filter.encoding != UA_EXTENSIONOBJECT_DECODED &&
  33. params->filter.encoding != UA_EXTENSIONOBJECT_DECODED_NODELETE)
  34. return UA_STATUSCODE_BADEVENTFILTERINVALID;
  35. if(params->filter.content.decoded.type != &UA_TYPES[UA_TYPES_EVENTFILTER])
  36. return UA_STATUSCODE_BADEVENTFILTERINVALID;
  37. UA_EventFilter_clear(&mon->filter.eventFilter);
  38. retval = UA_EventFilter_copy((UA_EventFilter *)params->filter.content.decoded.data,
  39. &mon->filter.eventFilter);
  40. #endif
  41. } else {
  42. /* DataChange MonitoredItem */
  43. if(params->filter.encoding != UA_EXTENSIONOBJECT_DECODED &&
  44. params->filter.encoding != UA_EXTENSIONOBJECT_DECODED_NODELETE) {
  45. /* Default: Look for status and value */
  46. UA_DataChangeFilter_clear(&mon->filter.dataChangeFilter);
  47. mon->filter.dataChangeFilter.trigger = UA_DATACHANGETRIGGER_STATUSVALUE;
  48. } else if(params->filter.content.decoded.type == &UA_TYPES[UA_TYPES_DATACHANGEFILTER]) {
  49. UA_DataChangeFilter *filter = (UA_DataChangeFilter *)params->filter.content.decoded.data;
  50. // TODO implement EURange to support UA_DEADBANDTYPE_PERCENT
  51. switch(filter->deadbandType) {
  52. case UA_DEADBANDTYPE_NONE:
  53. break;
  54. case UA_DEADBANDTYPE_ABSOLUTE:
  55. if(!dataType || !UA_DataType_isNumeric(dataType))
  56. return UA_STATUSCODE_BADFILTERNOTALLOWED;
  57. break;
  58. case UA_DEADBANDTYPE_PERCENT:
  59. return UA_STATUSCODE_BADMONITOREDITEMFILTERUNSUPPORTED;
  60. default:
  61. return UA_STATUSCODE_BADMONITOREDITEMFILTERUNSUPPORTED;
  62. }
  63. retval = UA_DataChangeFilter_copy(filter, &mon->filter.dataChangeFilter);
  64. } else {
  65. return UA_STATUSCODE_BADMONITOREDITEMFILTERUNSUPPORTED;
  66. }
  67. }
  68. if(retval != UA_STATUSCODE_GOOD)
  69. return retval;
  70. /* <-- The point of no return --> */
  71. /* Unregister the callback */
  72. UA_MonitoredItem_unregisterSampleCallback(server, mon);
  73. /* Remove the old samples */
  74. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  75. UA_Variant_deleteMembers(&mon->lastValue);
  76. /* ClientHandle */
  77. mon->clientHandle = params->clientHandle;
  78. /* SamplingInterval */
  79. UA_Double samplingInterval = params->samplingInterval;
  80. if(mon->attributeId == UA_ATTRIBUTEID_VALUE) {
  81. const UA_VariableNode *vn = (const UA_VariableNode *)
  82. UA_Nodestore_getNode(server->nsCtx, &mon->monitoredNodeId);
  83. if(vn) {
  84. if(vn->nodeClass == UA_NODECLASS_VARIABLE &&
  85. samplingInterval < vn->minimumSamplingInterval)
  86. samplingInterval = vn->minimumSamplingInterval;
  87. UA_Nodestore_releaseNode(server->nsCtx, (const UA_Node *)vn);
  88. }
  89. }
  90. UA_BOUNDEDVALUE_SETWBOUNDS(server->config.samplingIntervalLimits,
  91. samplingInterval, mon->samplingInterval);
  92. if(samplingInterval != samplingInterval) /* Check for nan */
  93. mon->samplingInterval = server->config.samplingIntervalLimits.min;
  94. /* QueueSize */
  95. UA_BOUNDEDVALUE_SETWBOUNDS(server->config.queueSizeLimits,
  96. params->queueSize, mon->maxQueueSize);
  97. /* DiscardOldest */
  98. mon->discardOldest = params->discardOldest;
  99. /* Register sample callback if reporting is enabled */
  100. mon->monitoringMode = monitoringMode;
  101. if(monitoringMode == UA_MONITORINGMODE_SAMPLING ||
  102. monitoringMode == UA_MONITORINGMODE_REPORTING)
  103. return UA_MonitoredItem_registerSampleCallback(server, mon);
  104. return UA_STATUSCODE_GOOD;
  105. }
  106. static const UA_String binaryEncoding = {sizeof("Default Binary") - 1, (UA_Byte *)"Default Binary"};
  107. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  108. static UA_StatusCode
  109. UA_Server_addMonitoredItemToNodeEditNodeCallback(UA_Server *server, UA_Session *session,
  110. UA_Node *node, void *data) {
  111. /* data is the MonitoredItem */
  112. /* SLIST_INSERT_HEAD */
  113. ((UA_MonitoredItem *)data)->next = ((UA_ObjectNode *)node)->monitoredItemQueue;
  114. ((UA_ObjectNode *)node)->monitoredItemQueue = (UA_MonitoredItem *)data;
  115. return UA_STATUSCODE_GOOD;
  116. }
  117. #endif
  118. /* Thread-local variables to pass additional arguments into the operation */
  119. struct createMonContext {
  120. UA_Subscription *sub;
  121. UA_TimestampsToReturn timestampsToReturn;
  122. /* If sub is NULL, use local callbacks */
  123. UA_Server_DataChangeNotificationCallback dataChangeCallback;
  124. void *context;
  125. };
  126. static void
  127. Operation_CreateMonitoredItem(UA_Server *server, UA_Session *session, struct createMonContext *cmc,
  128. const UA_MonitoredItemCreateRequest *request,
  129. UA_MonitoredItemCreateResult *result) {
  130. /* Check available capacity */
  131. if(cmc->sub &&
  132. (((server->config.maxMonitoredItems != 0) &&
  133. (server->numMonitoredItems >= server->config.maxMonitoredItems)) ||
  134. ((server->config.maxMonitoredItemsPerSubscription != 0) &&
  135. (cmc->sub->monitoredItemsSize >= server->config.maxMonitoredItemsPerSubscription)))) {
  136. result->statusCode = UA_STATUSCODE_BADTOOMANYMONITOREDITEMS;
  137. return;
  138. }
  139. /* Make an example read to get errors in the itemToMonitor. Allow return
  140. * codes "good" and "uncertain", as well as a list of statuscodes that might
  141. * be repaired inside the data source. */
  142. UA_DataValue v = UA_Server_readWithSession(server, session, &request->itemToMonitor,
  143. cmc->timestampsToReturn);
  144. if(v.hasStatus && (v.status >> 30) > 1 &&
  145. v.status != UA_STATUSCODE_BADRESOURCEUNAVAILABLE &&
  146. v.status != UA_STATUSCODE_BADCOMMUNICATIONERROR &&
  147. v.status != UA_STATUSCODE_BADWAITINGFORINITIALDATA &&
  148. v.status != UA_STATUSCODE_BADUSERACCESSDENIED &&
  149. v.status != UA_STATUSCODE_BADNOTREADABLE &&
  150. v.status != UA_STATUSCODE_BADINDEXRANGENODATA) {
  151. result->statusCode = v.status;
  152. UA_DataValue_deleteMembers(&v);
  153. return;
  154. }
  155. /* Check if the encoding is supported */
  156. if(request->itemToMonitor.dataEncoding.name.length > 0 &&
  157. (!UA_String_equal(&binaryEncoding, &request->itemToMonitor.dataEncoding.name) ||
  158. request->itemToMonitor.dataEncoding.namespaceIndex != 0)) {
  159. result->statusCode = UA_STATUSCODE_BADDATAENCODINGUNSUPPORTED;
  160. UA_DataValue_deleteMembers(&v);
  161. return;
  162. }
  163. /* Check if the encoding is set for a value */
  164. if(request->itemToMonitor.attributeId != UA_ATTRIBUTEID_VALUE &&
  165. request->itemToMonitor.dataEncoding.name.length > 0) {
  166. result->statusCode = UA_STATUSCODE_BADDATAENCODINGINVALID;
  167. UA_DataValue_deleteMembers(&v);
  168. return;
  169. }
  170. /* Allocate the MonitoredItem */
  171. size_t nmsize = sizeof(UA_MonitoredItem);
  172. if(!cmc->sub)
  173. nmsize = sizeof(UA_LocalMonitoredItem);
  174. UA_MonitoredItem *newMon = (UA_MonitoredItem*)UA_malloc(nmsize);
  175. if(!newMon) {
  176. result->statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
  177. UA_DataValue_deleteMembers(&v);
  178. return;
  179. }
  180. /* Initialize the MonitoredItem */
  181. UA_MonitoredItem_init(newMon, cmc->sub);
  182. newMon->attributeId = request->itemToMonitor.attributeId;
  183. newMon->timestampsToReturn = cmc->timestampsToReturn;
  184. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  185. retval |= UA_NodeId_copy(&request->itemToMonitor.nodeId, &newMon->monitoredNodeId);
  186. retval |= UA_String_copy(&request->itemToMonitor.indexRange, &newMon->indexRange);
  187. retval |= setMonitoredItemSettings(server, newMon, request->monitoringMode,
  188. &request->requestedParameters, v.value.type);
  189. UA_DataValue_deleteMembers(&v);
  190. if(retval != UA_STATUSCODE_GOOD) {
  191. UA_LOG_INFO_SESSION(&server->config.logger, session,
  192. "Subscription %u | Could not create a MonitoredItem "
  193. "with StatusCode %s", cmc->sub ? cmc->sub->subscriptionId : 0,
  194. UA_StatusCode_name(retval));
  195. result->statusCode = retval;
  196. UA_MonitoredItem_delete(server, newMon);
  197. return;
  198. }
  199. /* Add to the subscriptions or the local MonitoredItems */
  200. if(cmc->sub) {
  201. newMon->monitoredItemId = ++cmc->sub->lastMonitoredItemId;
  202. UA_Subscription_addMonitoredItem(server, cmc->sub, newMon);
  203. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  204. if(newMon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
  205. /* Insert the monitored item into the node's queue */
  206. UA_Server_editNode(server, NULL, &newMon->monitoredNodeId,
  207. UA_Server_addMonitoredItemToNodeEditNodeCallback, newMon);
  208. }
  209. #endif
  210. } else {
  211. //TODO support events for local monitored items
  212. UA_LocalMonitoredItem *localMon = (UA_LocalMonitoredItem*)newMon;
  213. localMon->context = cmc->context;
  214. localMon->callback.dataChangeCallback = cmc->dataChangeCallback;
  215. newMon->monitoredItemId = ++server->lastLocalMonitoredItemId;
  216. LIST_INSERT_HEAD(&server->localMonitoredItems, newMon, listEntry);
  217. }
  218. /* Register MonitoredItem in userland */
  219. if(server->config.monitoredItemRegisterCallback) {
  220. void *targetContext = NULL;
  221. UA_Server_getNodeContext(server, request->itemToMonitor.nodeId, &targetContext);
  222. server->config.monitoredItemRegisterCallback(server, &session->sessionId,
  223. session->sessionHandle,
  224. &request->itemToMonitor.nodeId,
  225. targetContext, newMon->attributeId, false);
  226. newMon->registered = true;
  227. }
  228. UA_LOG_INFO_SESSION(&server->config.logger, session,
  229. "Subscription %u | MonitoredItem %i | "
  230. "Created the MonitoredItem",
  231. cmc->sub ? cmc->sub->subscriptionId : 0,
  232. newMon->monitoredItemId);
  233. /* Create the first sample */
  234. if(request->monitoringMode == UA_MONITORINGMODE_REPORTING &&
  235. newMon->attributeId != UA_ATTRIBUTEID_EVENTNOTIFIER)
  236. UA_MonitoredItem_sampleCallback(server, newMon);
  237. /* Prepare the response */
  238. result->revisedSamplingInterval = newMon->samplingInterval;
  239. result->revisedQueueSize = newMon->maxQueueSize;
  240. result->monitoredItemId = newMon->monitoredItemId;
  241. }
  242. void
  243. Service_CreateMonitoredItems(UA_Server *server, UA_Session *session,
  244. const UA_CreateMonitoredItemsRequest *request,
  245. UA_CreateMonitoredItemsResponse *response) {
  246. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Processing CreateMonitoredItemsRequest");
  247. if(server->config.maxMonitoredItemsPerCall != 0 &&
  248. request->itemsToCreateSize > server->config.maxMonitoredItemsPerCall) {
  249. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  250. return;
  251. }
  252. /* Check if the timestampstoreturn is valid */
  253. struct createMonContext cmc;
  254. cmc.timestampsToReturn = request->timestampsToReturn;
  255. if(cmc.timestampsToReturn > UA_TIMESTAMPSTORETURN_NEITHER) {
  256. response->responseHeader.serviceResult = UA_STATUSCODE_BADTIMESTAMPSTORETURNINVALID;
  257. return;
  258. }
  259. /* Find the subscription */
  260. cmc.sub = UA_Session_getSubscriptionById(session, request->subscriptionId);
  261. if(!cmc.sub) {
  262. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  263. return;
  264. }
  265. /* Reset the subscription lifetime */
  266. cmc.sub->currentLifetimeCount = 0;
  267. response->responseHeader.serviceResult =
  268. UA_Server_processServiceOperations(server, session, (UA_ServiceOperation)Operation_CreateMonitoredItem, &cmc,
  269. &request->itemsToCreateSize, &UA_TYPES[UA_TYPES_MONITOREDITEMCREATEREQUEST],
  270. &response->resultsSize, &UA_TYPES[UA_TYPES_MONITOREDITEMCREATERESULT]);
  271. }
  272. UA_MonitoredItemCreateResult
  273. UA_Server_createDataChangeMonitoredItem(UA_Server *server,
  274. UA_TimestampsToReturn timestampsToReturn,
  275. const UA_MonitoredItemCreateRequest item,
  276. void *monitoredItemContext,
  277. UA_Server_DataChangeNotificationCallback callback) {
  278. struct createMonContext cmc;
  279. cmc.sub = NULL;
  280. cmc.context = monitoredItemContext;
  281. cmc.dataChangeCallback = callback;
  282. cmc.timestampsToReturn = timestampsToReturn;
  283. UA_MonitoredItemCreateResult result;
  284. UA_MonitoredItemCreateResult_init(&result);
  285. Operation_CreateMonitoredItem(server, &server->adminSession, &cmc, &item, &result);
  286. return result;
  287. }
  288. static void
  289. Operation_ModifyMonitoredItem(UA_Server *server, UA_Session *session, UA_Subscription *sub,
  290. const UA_MonitoredItemModifyRequest *request,
  291. UA_MonitoredItemModifyResult *result) {
  292. /* Get the MonitoredItem */
  293. UA_MonitoredItem *mon = UA_Subscription_getMonitoredItem(sub, request->monitoredItemId);
  294. if(!mon) {
  295. result->statusCode = UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  296. return;
  297. }
  298. /* Read the current value to test if filters are possible.
  299. * Can return an empty value (v.value.type == NULL). */
  300. UA_ReadValueId rvid;
  301. UA_ReadValueId_init(&rvid);
  302. rvid.nodeId = mon->monitoredNodeId;
  303. rvid.attributeId = mon->attributeId;
  304. rvid.indexRange = mon->indexRange;
  305. UA_DataValue v = UA_Server_readWithSession(server, session, &rvid, mon->timestampsToReturn);
  306. UA_StatusCode retval = setMonitoredItemSettings(server, mon, mon->monitoringMode,
  307. &request->requestedParameters,
  308. v.value.type);
  309. UA_DataValue_deleteMembers(&v);
  310. if(retval != UA_STATUSCODE_GOOD) {
  311. result->statusCode = retval;
  312. return;
  313. }
  314. result->revisedSamplingInterval = mon->samplingInterval;
  315. result->revisedQueueSize = mon->maxQueueSize;
  316. /* Remove some notifications if the queue is now too small */
  317. UA_MonitoredItem_ensureQueueSpace(server, mon);
  318. }
  319. void
  320. Service_ModifyMonitoredItems(UA_Server *server, UA_Session *session,
  321. const UA_ModifyMonitoredItemsRequest *request,
  322. UA_ModifyMonitoredItemsResponse *response) {
  323. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Processing ModifyMonitoredItemsRequest");
  324. if(server->config.maxMonitoredItemsPerCall != 0 &&
  325. request->itemsToModifySize > server->config.maxMonitoredItemsPerCall) {
  326. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  327. return;
  328. }
  329. /* Check if the timestampstoreturn is valid */
  330. if(request->timestampsToReturn > UA_TIMESTAMPSTORETURN_NEITHER) {
  331. response->responseHeader.serviceResult = UA_STATUSCODE_BADTIMESTAMPSTORETURNINVALID;
  332. return;
  333. }
  334. /* Get the subscription */
  335. UA_Subscription *sub = UA_Session_getSubscriptionById(session, request->subscriptionId);
  336. if(!sub) {
  337. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  338. return;
  339. }
  340. sub->currentLifetimeCount = 0; /* Reset the subscription lifetime */
  341. response->responseHeader.serviceResult =
  342. UA_Server_processServiceOperations(server, session,
  343. (UA_ServiceOperation)Operation_ModifyMonitoredItem, sub,
  344. &request->itemsToModifySize, &UA_TYPES[UA_TYPES_MONITOREDITEMMODIFYREQUEST],
  345. &response->resultsSize, &UA_TYPES[UA_TYPES_MONITOREDITEMMODIFYRESULT]);
  346. }
  347. struct setMonitoringContext {
  348. UA_Subscription *sub;
  349. UA_MonitoringMode monitoringMode;
  350. };
  351. static void
  352. Operation_SetMonitoringMode(UA_Server *server, UA_Session *session,
  353. struct setMonitoringContext *smc,
  354. const UA_UInt32 *monitoredItemId, UA_StatusCode *result) {
  355. UA_MonitoredItem *mon = UA_Subscription_getMonitoredItem(smc->sub, *monitoredItemId);
  356. if(!mon) {
  357. *result = UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  358. return;
  359. }
  360. UA_Subscription *sub = mon->subscription;
  361. /* Check if the MonitoringMode is valid or not */
  362. if(smc->monitoringMode > UA_MONITORINGMODE_REPORTING) {
  363. *result = UA_STATUSCODE_BADMONITORINGMODEINVALID;
  364. return;
  365. }
  366. /* Nothing has changed */
  367. if(mon->monitoringMode == smc->monitoringMode)
  368. return;
  369. mon->monitoringMode = smc->monitoringMode;
  370. /* When reporting is enabled, put all notifications that were already
  371. * sampled into the global queue of the subscription. When sampling is
  372. * enabled, remove all notifications from the global queue. !!! This needs
  373. * to be the same operation as in UA_Notification_enqueue !!! */
  374. if(mon->monitoringMode == UA_MONITORINGMODE_REPORTING) {
  375. UA_Notification *notification;
  376. TAILQ_FOREACH(notification, &mon->queue, listEntry) {
  377. TAILQ_INSERT_TAIL(&sub->notificationQueue, notification, globalEntry);
  378. ++sub->notificationQueueSize;
  379. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  380. if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
  381. ++sub->eventNotifications;
  382. } else
  383. #endif
  384. {
  385. ++sub->dataChangeNotifications;
  386. }
  387. }
  388. /* Register the sampling callback with an interval */
  389. *result = UA_MonitoredItem_registerSampleCallback(server, mon);
  390. } else if(mon->monitoringMode == UA_MONITORINGMODE_SAMPLING) {
  391. UA_Notification *notification;
  392. TAILQ_FOREACH(notification, &mon->queue, listEntry) {
  393. TAILQ_REMOVE(&sub->notificationQueue, notification, globalEntry);
  394. TAILQ_NEXT(notification, globalEntry) = UA_SUBSCRIPTION_QUEUE_SENTINEL;
  395. --sub->notificationQueueSize;
  396. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  397. if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
  398. --sub->eventNotifications;
  399. } else
  400. #endif
  401. {
  402. --sub->dataChangeNotifications;
  403. }
  404. }
  405. /* Register the sampling callback with an interval */
  406. *result = UA_MonitoredItem_registerSampleCallback(server, mon);
  407. } else {
  408. /* UA_MONITORINGMODE_DISABLED */
  409. UA_MonitoredItem_unregisterSampleCallback(server, mon);
  410. /* Setting the mode to DISABLED or SAMPLING causes all queued Notifications to be deleted */
  411. UA_Notification *notification, *notification_tmp;
  412. TAILQ_FOREACH_SAFE(notification, &mon->queue, listEntry, notification_tmp) {
  413. UA_Notification_dequeue(server, notification);
  414. UA_Notification_delete(notification);
  415. }
  416. /* Initialize lastSampledValue */
  417. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  418. UA_Variant_deleteMembers(&mon->lastValue);
  419. }
  420. }
  421. void
  422. Service_SetMonitoringMode(UA_Server *server, UA_Session *session,
  423. const UA_SetMonitoringModeRequest *request,
  424. UA_SetMonitoringModeResponse *response) {
  425. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Processing SetMonitoringMode");
  426. if(server->config.maxMonitoredItemsPerCall != 0 &&
  427. request->monitoredItemIdsSize > server->config.maxMonitoredItemsPerCall) {
  428. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  429. return;
  430. }
  431. /* Get the subscription */
  432. struct setMonitoringContext smc;
  433. smc.sub = UA_Session_getSubscriptionById(session, request->subscriptionId);
  434. if(!smc.sub) {
  435. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  436. return;
  437. }
  438. smc.sub->currentLifetimeCount = 0; /* Reset the subscription lifetime */
  439. smc.monitoringMode = request->monitoringMode;
  440. response->responseHeader.serviceResult =
  441. UA_Server_processServiceOperations(server, session,
  442. (UA_ServiceOperation)Operation_SetMonitoringMode, &smc,
  443. &request->monitoredItemIdsSize, &UA_TYPES[UA_TYPES_UINT32],
  444. &response->resultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  445. }
  446. static void
  447. Operation_DeleteMonitoredItem(UA_Server *server, UA_Session *session, UA_Subscription *sub,
  448. const UA_UInt32 *monitoredItemId, UA_StatusCode *result) {
  449. *result = UA_Subscription_deleteMonitoredItem(server, sub, *monitoredItemId);
  450. }
  451. void
  452. Service_DeleteMonitoredItems(UA_Server *server, UA_Session *session,
  453. const UA_DeleteMonitoredItemsRequest *request,
  454. UA_DeleteMonitoredItemsResponse *response) {
  455. UA_LOG_DEBUG_SESSION(&server->config.logger, session,
  456. "Processing DeleteMonitoredItemsRequest");
  457. if(server->config.maxMonitoredItemsPerCall != 0 &&
  458. request->monitoredItemIdsSize > server->config.maxMonitoredItemsPerCall) {
  459. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYOPERATIONS;
  460. return;
  461. }
  462. /* Get the subscription */
  463. UA_Subscription *sub = UA_Session_getSubscriptionById(session, request->subscriptionId);
  464. if(!sub) {
  465. response->responseHeader.serviceResult = UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID;
  466. return;
  467. }
  468. /* Reset the subscription lifetime */
  469. sub->currentLifetimeCount = 0;
  470. response->responseHeader.serviceResult =
  471. UA_Server_processServiceOperations(server, session,
  472. (UA_ServiceOperation)Operation_DeleteMonitoredItem, sub,
  473. &request->monitoredItemIdsSize, &UA_TYPES[UA_TYPES_UINT32],
  474. &response->resultsSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  475. }
  476. UA_StatusCode
  477. UA_Server_deleteMonitoredItem(UA_Server *server, UA_UInt32 monitoredItemId) {
  478. UA_MonitoredItem *mon;
  479. LIST_FOREACH(mon, &server->localMonitoredItems, listEntry) {
  480. if(mon->monitoredItemId != monitoredItemId)
  481. continue;
  482. LIST_REMOVE(mon, listEntry);
  483. UA_MonitoredItem_delete(server, mon);
  484. return UA_STATUSCODE_GOOD;
  485. }
  486. return UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  487. }
  488. #endif /* UA_ENABLE_SUBSCRIPTIONS */