1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <open62541/client.h>
#include <open62541/client_highlevel_async.h>
#include <open62541/client_config_default.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>
#include "client/ua_client_internal.h"
#include "server/ua_server_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include "check.h"
#include "testing_clock.h"
#include "thread_wrapper.h"
UA_Server *server;
UA_Boolean running;
THREAD_HANDLE server_thread;
static UA_Boolean noNewSubscription; /* Don't create a subscription when the
session activates */
#define VARLENGTH 16366
static void
addVariable(size_t size) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32* array = (UA_Int32*)UA_malloc(size * sizeof(UA_Int32));
for(size_t i = 0; i < size; i++)
array[i] = (UA_Int32)i;
UA_Variant_setArray(&attr.value, array, size, &UA_TYPES[UA_TYPES_INT32]);
char name[] = "my.variable";
attr.description = UA_LOCALIZEDTEXT("en-US", name);
attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
/* Add the variable node to the information model */
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, name);
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, name);
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
parentReferenceNodeId, myIntegerName,
UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
attr, NULL, NULL);
/* add displayname to variable */
UA_Server_writeDisplayName(server, myIntegerNodeId,
UA_LOCALIZEDTEXT("de", "meine.Variable"));
UA_free(array);
}
THREAD_CALLBACK(serverloop) {
while(running)
UA_Server_run_iterate(server, true);
return 0;
}
static void setup(void) {
noNewSubscription = false;
running = true;
server = UA_Server_new();
ck_assert(server != NULL);
UA_ServerConfig *config = UA_Server_getConfig(server);
UA_ServerConfig_setDefault(config);
config->maxPublishReqPerSession = 5;
UA_Server_run_startup(server);
addVariable(VARLENGTH);
THREAD_CREATE(server_thread, serverloop);
}
static void teardown(void) {
if(!server)
return;
running = false;
THREAD_JOIN(server_thread);
UA_Server_run_shutdown(server);
UA_Server_delete(server);
server = NULL;
}
UA_Boolean notificationReceived = false;
UA_UInt32 countNotificationReceived = 0;
UA_Double publishingInterval = 500.0;
static void
dataChangeHandler(UA_Client *client, UA_UInt32 subId, void *subContext,
UA_UInt32 monId, void *monContext, UA_DataValue *value) {
notificationReceived = true;
countNotificationReceived++;
}
static void clearLocale(UA_ClientConfig *config) {
if(config->sessionLocaleIdsSize > 0 && config->sessionLocaleIds) {
UA_Array_delete(config->sessionLocaleIds,
config->sessionLocaleIdsSize, &UA_TYPES[UA_TYPES_LOCALEID]);
}
config->sessionLocaleIds = NULL;
config->sessionLocaleIdsSize = 0;
}
static void changeLocale(UA_Client *client) {
UA_LocalizedText loc;
UA_ClientConfig *config = UA_Client_getConfig(client);
clearLocale(config);
config->sessionLocaleIdsSize = 2;
config->sessionLocaleIds = (UA_LocaleId *)UA_Array_new(2, &UA_TYPES[UA_TYPES_LOCALEID]);
config->sessionLocaleIds[0] = UA_STRING_ALLOC("en-US");
config->sessionLocaleIds[1] = UA_STRING_ALLOC("de");
UA_StatusCode retval = UA_Client_activateCurrentSession(client);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
const UA_NodeId nodeIdString = UA_NODEID_STRING(1, "my.variable");
retval = UA_Client_readDisplayNameAttribute(client, nodeIdString, &loc);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
UA_LocalizedText newLocaleEng = UA_LOCALIZEDTEXT("en-US", "my.variable");
ck_assert(UA_String_equal(&newLocaleEng.locale, &loc.locale));
ck_assert(UA_String_equal(&newLocaleEng.text, &loc.text));
UA_LocalizedText_clear(&loc);
clearLocale(config);
config->sessionLocaleIdsSize = 2;
config->sessionLocaleIds = (UA_LocaleId *)UA_Array_new(2, &UA_TYPES[UA_TYPES_LOCALEID]);
config->sessionLocaleIds[0] = UA_STRING_ALLOC("de");
config->sessionLocaleIds[1] = UA_STRING_ALLOC("en-US");
retval = UA_Client_activateCurrentSession(client);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
retval = UA_Client_readDisplayNameAttribute(client, nodeIdString, &loc);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
UA_LocalizedText newLocaleGerm = UA_LOCALIZEDTEXT("de", "meine.Variable");
ck_assert(UA_String_equal(&newLocaleGerm.locale, &loc.locale));
ck_assert(UA_String_equal(&newLocaleGerm.text, &loc.text));
UA_LocalizedText_clear(&loc);
}
START_TEST(Client_subscription_createDataChanges) {
UA_Client *client = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
NULL, NULL, NULL);
ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
changeLocale(client);
UA_UInt32 subId = response.subscriptionId;
UA_MonitoredItemCreateRequest items[3];
UA_UInt32 newMonitoredItemIds[3];
UA_Client_DataChangeNotificationCallback callbacks[3];
UA_Client_DeleteMonitoredItemCallback deleteCallbacks[3];
void *contexts = NULL;
changeLocale(client);
/* monitor the server state */
items[0] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE));
callbacks[0] = dataChangeHandler;
deleteCallbacks[0] = NULL;
/* monitor invalid node */
items[1] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, 999999));
callbacks[1] = dataChangeHandler;
deleteCallbacks[1] = NULL;
/* monitor current time */
items[2] = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
callbacks[2] = dataChangeHandler;
deleteCallbacks[2] = NULL;
UA_CreateMonitoredItemsRequest createRequest;
UA_CreateMonitoredItemsRequest_init(&createRequest);
createRequest.subscriptionId = subId;
createRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_BOTH;
createRequest.itemsToCreate = items;
createRequest.itemsToCreateSize = 3;
UA_CreateMonitoredItemsResponse createResponse =
UA_Client_MonitoredItems_createDataChanges(client, createRequest, NULL,
callbacks, deleteCallbacks);
ck_assert_uint_eq(createResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
ck_assert_uint_eq(createResponse.resultsSize, 3);
ck_assert_uint_eq(createResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
newMonitoredItemIds[0] = createResponse.results[0].monitoredItemId;
ck_assert_uint_eq(createResponse.results[1].statusCode, UA_STATUSCODE_BADNODEIDUNKNOWN);
newMonitoredItemIds[1] = createResponse.results[1].monitoredItemId;
ck_assert_uint_eq(newMonitoredItemIds[1], 0);
ck_assert_uint_eq(createResponse.results[2].statusCode, UA_STATUSCODE_GOOD);
newMonitoredItemIds[2] = createResponse.results[2].monitoredItemId;
ck_assert_uint_eq(createResponse.results[2].statusCode, UA_STATUSCODE_GOOD);
UA_CreateMonitoredItemsResponse_clear(&createResponse);
changeLocale(client);
/* manually control the server thread */
running = false;
THREAD_JOIN(server_thread);
retval = UA_Client_run_iterate(client, 1);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
UA_fakeSleep((UA_UInt32)publishingInterval + 1);
UA_Server_run_iterate(server, true);
notificationReceived = false;
countNotificationReceived = 0;
UA_fakeSleep((UA_UInt32)publishingInterval + 1);
retval = UA_Client_run_iterate(client, 1);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
ck_assert_uint_eq(notificationReceived, true);
ck_assert_uint_eq(countNotificationReceived, 2);
UA_fakeSleep((UA_UInt32)publishingInterval + 1);
UA_Server_run_iterate(server, true);
notificationReceived = false;
retval = UA_Client_run_iterate(client, 1);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
ck_assert_uint_eq(notificationReceived, true);
ck_assert_uint_eq(countNotificationReceived, 3);
/* run the server in an independent thread again */
running = true;
THREAD_CREATE(server_thread, serverloop);
UA_DeleteMonitoredItemsRequest deleteRequest;
UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
deleteRequest.subscriptionId = subId;
deleteRequest.monitoredItemIds = newMonitoredItemIds;
deleteRequest.monitoredItemIdsSize = 3;
UA_DeleteMonitoredItemsResponse deleteResponse =
UA_Client_MonitoredItems_delete(client, deleteRequest);
ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
ck_assert_uint_eq(deleteResponse.resultsSize, 3);
ck_assert_uint_eq(deleteResponse.results[0], UA_STATUSCODE_GOOD);
ck_assert_uint_eq(deleteResponse.results[1], UA_STATUSCODE_BADMONITOREDITEMIDINVALID);
ck_assert_uint_eq(deleteResponse.results[2], UA_STATUSCODE_GOOD);
UA_DeleteMonitoredItemsResponse_clear(&deleteResponse);
retval = UA_Client_Subscriptions_deleteSingle(client, subId);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
UA_Client_delete(client);
}
END_TEST
static Suite* testSuite_Client(void) {
Suite *s = suite_create("Client Subscription");
TCase *tc_client = tcase_create("Client Subscription Basic");
tcase_add_checked_fixture(tc_client, setup, teardown);
tcase_add_test(tc_client, Client_subscription_createDataChanges);
suite_add_tcase(s, tc_client);
return s;
}
int main(void) {
Suite *s = testSuite_Client();
SRunner *sr = srunner_create(s);
srunner_set_fork_status(sr, CK_NOFORK);
srunner_run_all(sr,CK_NORMAL);
int number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|