Procházet zdrojové kódy

fix UA_Client_Subscriptions_addMonitored(Items/Events) definition and add test

StalderT před 6 roky
rodič
revize
de11543f21

+ 2 - 2
include/ua_client_highlevel.h

@@ -595,7 +595,7 @@ typedef void (*UA_MonitoredItemHandlingFunction)(UA_UInt32 monId, UA_DataValue *
 UA_StatusCode UA_EXPORT
 UA_Client_Subscriptions_addMonitoredItems(UA_Client *client, const UA_UInt32 subscriptionId,
                                           UA_MonitoredItemCreateRequest *items, size_t itemsSize,
-                                          UA_MonitoredItemHandlingFunction **hfs,
+                                          UA_MonitoredItemHandlingFunction *hfs,
                                           void **hfContexts, UA_StatusCode *itemResults,
                                           UA_UInt32 *newMonitoredItemIds);
 
@@ -617,7 +617,7 @@ typedef void (*UA_MonitoredEventHandlingFunction)(const UA_UInt32 monId,
 UA_StatusCode UA_EXPORT
 UA_Client_Subscriptions_addMonitoredEvents(UA_Client *client, const UA_UInt32 subscriptionId,
                                            UA_MonitoredItemCreateRequest *items, size_t itemsSize,
-                                           UA_MonitoredEventHandlingFunction **hfs,
+                                           UA_MonitoredEventHandlingFunction *hfs,
                                            void **hfContexts, UA_StatusCode *itemResults,
                                            UA_UInt32 *newMonitoredItemIds);
 

+ 2 - 2
src/client/ua_client_highlevel_subscriptions.c

@@ -208,7 +208,7 @@ addMonitoredItems(UA_Client *client, const UA_UInt32 subscriptionId,
 UA_StatusCode
 UA_Client_Subscriptions_addMonitoredItems(UA_Client *client, const UA_UInt32 subscriptionId,
                                           UA_MonitoredItemCreateRequest *items, size_t itemsSize, 
-                                          UA_MonitoredItemHandlingFunction **hfs,
+                                          UA_MonitoredItemHandlingFunction *hfs,
                                           void **hfContexts, UA_StatusCode *itemResults,
                                           UA_UInt32 *newMonitoredItemIds) {
     return addMonitoredItems(client, subscriptionId, items, itemsSize, (void**)hfs,
@@ -240,7 +240,7 @@ UA_Client_Subscriptions_addMonitoredItem(UA_Client *client, UA_UInt32 subscripti
 UA_StatusCode
 UA_Client_Subscriptions_addMonitoredEvents(UA_Client *client, const UA_UInt32 subscriptionId,
                                            UA_MonitoredItemCreateRequest *items, size_t itemsSize, 
-                                           UA_MonitoredEventHandlingFunction **hfs,
+                                           UA_MonitoredEventHandlingFunction *hfs,
                                            void **hfContexts, UA_StatusCode *itemResults,
                                            UA_UInt32 *newMonitoredItemIds) {
     return addMonitoredItems(client, subscriptionId, items, itemsSize, (void**)hfs,

+ 89 - 0
tests/client/check_client_subscriptions.c

@@ -51,9 +51,11 @@ static void teardown(void) {
 #ifdef UA_ENABLE_SUBSCRIPTIONS
 
 UA_Boolean notificationReceived;
+UA_UInt32 countNotificationReceived = 0;
 
 static void monitoredItemHandler(UA_UInt32 monId, UA_DataValue *value, void *context) {
     notificationReceived = true;
+    countNotificationReceived++;
 }
 
 START_TEST(Client_subscription) {
@@ -82,6 +84,92 @@ START_TEST(Client_subscription) {
     retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, monId);
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
+    retval = UA_Client_Subscriptions_remove(client, subId);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+
+    UA_Client_disconnect(client);
+    UA_Client_delete(client);
+}
+END_TEST
+
+START_TEST(Client_subscription_addMonitoredItems) {
+    UA_Client *client = UA_Client_new(UA_ClientConfig_default);
+    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+
+    UA_UInt32 subId;
+    retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+
+    UA_MonitoredItemCreateRequest items[3];
+    UA_MonitoredItemHandlingFunction hfs[3];
+    void *hfContexts[3];
+    UA_StatusCode itemResults[3];
+    UA_UInt32 newMonitoredItemIds[3];
+
+    /* monitor the server state */
+    UA_MonitoredItemCreateRequest_init(&items[0]);
+    items[0].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2259);
+    items[0].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
+    items[0].monitoringMode = UA_MONITORINGMODE_REPORTING;
+    items[0].requestedParameters.samplingInterval = 250;
+    items[0].requestedParameters.discardOldest = true;
+    items[0].requestedParameters.queueSize = 1;
+    hfs[0] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
+    hfContexts[0] = NULL;
+
+    /* monitor current time */
+    UA_MonitoredItemCreateRequest_init(&items[1]);
+    items[1].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
+    items[1].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
+    items[1].monitoringMode = UA_MONITORINGMODE_REPORTING;
+    items[1].requestedParameters.samplingInterval = 250;
+    items[1].requestedParameters.discardOldest = true;
+    items[1].requestedParameters.queueSize = 1;
+    hfs[1] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
+    hfContexts[1] = NULL;
+
+    /* monitor invalid node */
+    UA_MonitoredItemCreateRequest_init(&items[2]);
+    items[2].itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 99999999);
+    items[2].itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
+    items[2].monitoringMode = UA_MONITORINGMODE_REPORTING;
+    items[2].requestedParameters.samplingInterval = 250;
+    items[2].requestedParameters.discardOldest = true;
+    items[2].requestedParameters.queueSize = 1;
+    hfs[2] = (UA_MonitoredItemHandlingFunction)(uintptr_t)monitoredItemHandler;
+    hfContexts[2] = NULL;
+
+    retval = UA_Client_Subscriptions_addMonitoredItems(client, subId, items, 3,
+                                                      hfs, hfContexts, itemResults,
+                                                      newMonitoredItemIds);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+    ck_assert_uint_eq(itemResults[0], UA_STATUSCODE_GOOD);
+    ck_assert_uint_eq(itemResults[1], UA_STATUSCODE_GOOD);
+    ck_assert_uint_eq(itemResults[2], UA_STATUSCODE_BADNODEIDUNKNOWN);
+
+    UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
+
+    notificationReceived = false;
+    countNotificationReceived = 0;
+    retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+    ck_assert_uint_eq(notificationReceived, true);
+    ck_assert_uint_eq(countNotificationReceived, 2);
+
+    UA_fakeSleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
+
+    notificationReceived = false;
+    retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+    ck_assert_uint_eq(notificationReceived, true);
+    ck_assert_uint_eq(countNotificationReceived, 3);
+
+    retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, newMonitoredItemIds[0]);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
+
+    retval = UA_Client_Subscriptions_removeMonitoredItem(client, subId, newMonitoredItemIds[1]);
+    ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
 
     retval = UA_Client_Subscriptions_remove(client, subId);
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
@@ -184,6 +272,7 @@ static Suite* testSuite_Client(void) {
 #ifdef UA_ENABLE_SUBSCRIPTIONS
     tcase_add_test(tc_client, Client_subscription);
     tcase_add_test(tc_client, Client_subscription_connectionClose);
+    tcase_add_test(tc_client, Client_subscription_addMonitoredItems);
 #endif /* UA_ENABLE_SUBSCRIPTIONS */
 
     TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");