check_pubsub_informationmodel_methods.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 (c) 2017 - 2018 Fraunhofer IOSB (Author: Andreas Ebner)
  6. */
  7. #include <string.h>
  8. #include <math.h>
  9. #include "src_generated/ua_types_generated.h"
  10. #include "ua_types.h"
  11. #include "src_generated/ua_types_generated_encoding_binary.h"
  12. #include "ua_types.h"
  13. #include "ua_server_pubsub.h"
  14. #include "src_generated/ua_types_generated.h"
  15. #include "ua_network_pubsub_udp.h"
  16. #include "ua_server_internal.h"
  17. #include "check.h"
  18. #include "ua_plugin_pubsub.h"
  19. #include "ua_config_default.h"
  20. #include "thread_wrapper.h"
  21. UA_Server *server = NULL;
  22. UA_ServerConfig *config = NULL;
  23. UA_Boolean running;
  24. THREAD_HANDLE server_thread;
  25. THREAD_CALLBACK(serverloop) {
  26. while (running)
  27. UA_Server_run_iterate(server, true);
  28. return 0;
  29. }
  30. static void setup(void) {
  31. running = true;
  32. config = UA_ServerConfig_new_default();
  33. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  34. if(!config->pubsubTransportLayers) {
  35. UA_ServerConfig_delete(config);
  36. }
  37. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  38. config->pubsubTransportLayersSize++;
  39. server = UA_Server_new(config);
  40. UA_Server_run_startup(server);
  41. THREAD_CREATE(server_thread, serverloop);
  42. }
  43. static void teardown(void) {
  44. running = false;
  45. THREAD_JOIN(server_thread);
  46. UA_Server_run_shutdown(server);
  47. UA_Server_delete(server);
  48. UA_ServerConfig_delete(config);
  49. }
  50. static UA_NodeId
  51. findSingleChildNode(UA_QualifiedName targetName,
  52. UA_NodeId referenceTypeId, UA_NodeId startingNode){
  53. UA_NodeId resultNodeId;
  54. UA_RelativePathElement rpe;
  55. UA_RelativePathElement_init(&rpe);
  56. rpe.referenceTypeId = referenceTypeId;
  57. rpe.isInverse = false;
  58. rpe.includeSubtypes = false;
  59. rpe.targetName = targetName;
  60. UA_BrowsePath bp;
  61. UA_BrowsePath_init(&bp);
  62. bp.startingNode = startingNode;
  63. bp.relativePath.elementsSize = 1;
  64. bp.relativePath.elements = &rpe;
  65. UA_BrowsePathResult bpr =
  66. UA_Server_translateBrowsePathToNodeIds(server, &bp);
  67. if(bpr.statusCode != UA_STATUSCODE_GOOD ||
  68. bpr.targetsSize < 1)
  69. return UA_NODEID_NULL;
  70. if(UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, &resultNodeId) != UA_STATUSCODE_GOOD){
  71. UA_BrowsePathResult_deleteMembers(&bpr);
  72. return UA_NODEID_NULL;
  73. }
  74. UA_BrowsePathResult_deleteMembers(&bpr);
  75. return resultNodeId;
  76. }
  77. static UA_NodeId addPubSubConnection(void){
  78. UA_Variant publisherId;
  79. UA_Variant_init(&publisherId);
  80. UA_UInt32 publisherIdValue = 13245;
  81. UA_Variant_setScalar(&publisherId, &publisherIdValue , &UA_TYPES[UA_TYPES_UINT32]);
  82. UA_PubSubConnectionDataType pubSubConnection;
  83. UA_PubSubConnectionDataType_init(&pubSubConnection);
  84. pubSubConnection.name = UA_STRING("Model Connection 1");
  85. pubSubConnection.enabled = UA_TRUE;
  86. pubSubConnection.publisherId = publisherId;
  87. pubSubConnection.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  88. UA_ExtensionObject eo;
  89. UA_NetworkAddressUrlDataType networkAddressDataType = {UA_STRING("eth0"), UA_STRING("opc.udp://224.0.0.22:4840/")};
  90. UA_NetworkAddressUrlDataType* identityToken = UA_NetworkAddressUrlDataType_new();
  91. UA_NetworkAddressUrlDataType_init(identityToken);
  92. UA_NetworkAddressUrlDataType_copy(&networkAddressDataType, identityToken);
  93. eo.encoding = UA_EXTENSIONOBJECT_DECODED;
  94. eo.content.decoded.type = &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE];
  95. eo.content.decoded.data = identityToken;
  96. pubSubConnection.address = eo;
  97. pubSubConnection.connectionPropertiesSize = 2;
  98. UA_KeyValuePair connectionOptions[2];
  99. memset(connectionOptions, 0, sizeof(UA_KeyValuePair)* 2);
  100. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  101. UA_UInt32 ttl = 10;
  102. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  103. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  104. UA_Boolean loopback = UA_FALSE;
  105. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_UINT32]);
  106. pubSubConnection.connectionProperties = connectionOptions;
  107. UA_Variant inputArguments;
  108. UA_Variant_init(&inputArguments);
  109. UA_Variant_setScalar(&inputArguments, &pubSubConnection, &UA_TYPES[UA_TYPES_PUBSUBCONNECTIONDATATYPE]);
  110. UA_CallMethodRequest callMethodRequest;
  111. UA_CallMethodRequest_init(&callMethodRequest);
  112. callMethodRequest.inputArgumentsSize = 1;
  113. callMethodRequest.inputArguments = &inputArguments;
  114. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE);
  115. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_ADDCONNECTION);
  116. UA_NodeId connectionId = UA_NODEID_NULL;
  117. UA_CallMethodResult result;
  118. UA_CallMethodResult_init(&result);
  119. result = UA_Server_call(server, &callMethodRequest);
  120. ck_assert_int_eq(1, result.outputArgumentsSize);
  121. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  122. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  123. connectionId = *((UA_NodeId *) result.outputArguments->data);
  124. UA_ExtensionObject_deleteMembers(&eo);
  125. callMethodRequest.inputArguments = NULL;
  126. callMethodRequest.inputArgumentsSize = 0;
  127. UA_CallMethodRequest_deleteMembers(&callMethodRequest);
  128. UA_CallMethodResult_deleteMembers(&result);
  129. return connectionId;
  130. }
  131. START_TEST(AddNewPubSubConnectionUsingTheInformationModelMethod){
  132. UA_StatusCode retVal;
  133. UA_Client *client = UA_Client_new();
  134. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  135. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  136. if(retVal != UA_STATUSCODE_GOOD) {
  137. UA_Client_delete(client);
  138. }
  139. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  140. UA_NodeId createdConnection = addPubSubConnection();
  141. UA_LocalizedText connectionDisplayName;
  142. UA_LocalizedText_init(&connectionDisplayName);
  143. retVal = UA_Server_readDisplayName(server, createdConnection, &connectionDisplayName);
  144. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  145. UA_String compareText = UA_STRING("Model Connection 1");
  146. ck_assert(UA_String_equal(&connectionDisplayName.text, &compareText) == UA_TRUE);
  147. //todo browse and check childs
  148. UA_Variant serverPubSubConnectionValues;
  149. UA_Variant_init(&serverPubSubConnectionValues);
  150. UA_NodeId connectionPublisherId = findSingleChildNode(UA_QUALIFIEDNAME(0, "PublisherId"),
  151. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  152. createdConnection);
  153. ck_assert_int_eq(UA_Server_readValue(server, connectionPublisherId, &serverPubSubConnectionValues),
  154. UA_STATUSCODE_GOOD);
  155. ck_assert_uint_eq(*((UA_UInt32 *) serverPubSubConnectionValues.data), 13245);
  156. UA_Variant_deleteMembers(&serverPubSubConnectionValues);
  157. UA_Client_disconnect(client);
  158. UA_Client_delete(client);
  159. UA_LocalizedText_deleteMembers(&connectionDisplayName);
  160. } END_TEST
  161. START_TEST(AddAndRemovePublishedDataSetFolders){
  162. UA_StatusCode retVal;
  163. UA_Client *client = UA_Client_new();
  164. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  165. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  166. if(retVal != UA_STATUSCODE_GOOD) {
  167. UA_Client_delete(client);
  168. }
  169. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  170. UA_String folderName = UA_STRING("TestFolder");
  171. UA_Variant inputArguments;
  172. UA_Variant_init(&inputArguments);
  173. UA_Variant_setScalar(&inputArguments, &folderName, &UA_TYPES[UA_TYPES_STRING]);
  174. UA_CallMethodRequest callMethodRequest;
  175. UA_CallMethodRequest_init(&callMethodRequest);
  176. callMethodRequest.inputArgumentsSize = 1;
  177. callMethodRequest.inputArguments = &inputArguments;
  178. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  179. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDDATASETFOLDER);
  180. UA_CallMethodResult result;
  181. UA_CallMethodResult_init(&result);
  182. result = UA_Server_call(server, &callMethodRequest);
  183. ck_assert_int_eq(1, result.outputArgumentsSize);
  184. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  185. UA_NodeId createdFolder = UA_NODEID_NULL;
  186. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  187. createdFolder = *((UA_NodeId *) result.outputArguments->data);
  188. UA_LocalizedText connectionDisplayName;
  189. UA_LocalizedText_init(&connectionDisplayName);
  190. retVal = UA_Server_readDisplayName(server, createdFolder, &connectionDisplayName);
  191. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  192. UA_String compareText = UA_STRING("TestFolder");
  193. ck_assert(UA_String_equal(&connectionDisplayName.text, &compareText) == UA_TRUE);
  194. retVal = UA_Server_readNodeId(server, createdFolder, &createdFolder);
  195. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  196. UA_CallMethodResult_deleteMembers(&result);
  197. UA_LocalizedText_deleteMembers(&connectionDisplayName);
  198. //create folder inside the new folder
  199. folderName = UA_STRING("TestFolder2");
  200. UA_Variant_init(&inputArguments);
  201. UA_Variant_setScalar(&inputArguments, &folderName, &UA_TYPES[UA_TYPES_STRING]);
  202. UA_CallMethodRequest_init(&callMethodRequest);
  203. callMethodRequest.inputArgumentsSize = 1;
  204. callMethodRequest.inputArguments = &inputArguments;
  205. callMethodRequest.objectId = createdFolder;
  206. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDDATASETFOLDER);
  207. UA_CallMethodResult_init(&result);
  208. result = UA_Server_call(server, &callMethodRequest);
  209. ck_assert_int_eq(1, result.outputArgumentsSize);
  210. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  211. UA_NodeId createdFolder2 = UA_NODEID_NULL;
  212. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  213. createdFolder2 = *((UA_NodeId *) result.outputArguments->data);
  214. UA_LocalizedText_init(&connectionDisplayName);
  215. retVal = UA_Server_readDisplayName(server, createdFolder2, &connectionDisplayName);
  216. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  217. compareText = UA_STRING("TestFolder2");
  218. ck_assert(UA_String_equal(&connectionDisplayName.text, &compareText) == UA_TRUE);
  219. retVal = UA_Server_readNodeId(server, createdFolder2, &createdFolder2);
  220. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  221. UA_CallMethodResult_deleteMembers(&result);
  222. //delete the folder
  223. UA_Variant_init(&inputArguments);
  224. UA_Variant_setScalar(&inputArguments, &createdFolder, &UA_TYPES[UA_TYPES_NODEID]);
  225. UA_CallMethodRequest_init(&callMethodRequest);
  226. callMethodRequest.inputArgumentsSize = 1;
  227. callMethodRequest.inputArguments = &inputArguments;
  228. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  229. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_REMOVEDATASETFOLDER);
  230. result = UA_Server_call(server, &callMethodRequest);
  231. ck_assert_int_eq(0, result.outputArgumentsSize);
  232. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  233. //check if the node is deleted
  234. retVal = UA_Server_readNodeId(server, createdFolder, NULL);
  235. ck_assert_int_eq(retVal, UA_STATUSCODE_BADNODEIDUNKNOWN);
  236. UA_CallMethodResult_deleteMembers(&result);
  237. UA_Client_disconnect(client);
  238. UA_Client_delete(client);
  239. UA_LocalizedText_deleteMembers(&connectionDisplayName);
  240. } END_TEST
  241. START_TEST(AddAndRemovePublishedDataSetItems){
  242. UA_StatusCode retVal;
  243. UA_Client *client = UA_Client_new();
  244. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  245. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  246. if(retVal != UA_STATUSCODE_GOOD) {
  247. UA_Client_delete(client);
  248. }
  249. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  250. UA_Variant *inputArguments = (UA_Variant *) UA_calloc(4, (sizeof(UA_Variant)));
  251. UA_String pdsName = UA_STRING("Test PDS");
  252. UA_Variant_setScalar(&inputArguments[0], &pdsName, &UA_TYPES[UA_TYPES_STRING]);
  253. UA_String *fieldNameAliases = (UA_String *) UA_calloc(2, sizeof(UA_String));
  254. fieldNameAliases[0] = UA_STRING("field1");
  255. fieldNameAliases[1] = UA_STRING("field2");
  256. UA_Variant_setArray(&inputArguments[1], fieldNameAliases, 2, &UA_TYPES[UA_TYPES_STRING]);
  257. UA_DataSetFieldFlags *dataSetFieldFlags = (UA_DataSetFieldFlags *) UA_calloc(2, sizeof(UA_DataSetFieldFlags));
  258. dataSetFieldFlags[0] = UA_DATASETFIELDFLAGS_PROMOTEDFIELD;
  259. dataSetFieldFlags[1] = UA_DATASETFIELDFLAGS_PROMOTEDFIELD;
  260. UA_Variant_setArray(&inputArguments[2], dataSetFieldFlags, 2, &UA_TYPES[UA_TYPES_DATASETFIELDFLAGS]);
  261. UA_PublishedVariableDataType *variablesToAdd = (UA_PublishedVariableDataType *) UA_calloc(2, sizeof(UA_PublishedVariableDataType));
  262. variablesToAdd[0].publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  263. variablesToAdd[0].attributeId = UA_ATTRIBUTEID_VALUE;
  264. variablesToAdd[1].publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERREDUNDANCY_CURRENTSERVERID);
  265. variablesToAdd[1].attributeId = UA_ATTRIBUTEID_VALUE;
  266. UA_Variant_setArray(&inputArguments[3], variablesToAdd, 2, &UA_TYPES[UA_TYPES_PUBLISHEDVARIABLEDATATYPE]);
  267. UA_CallMethodRequest callMethodRequest;
  268. UA_CallMethodRequest_init(&callMethodRequest);
  269. callMethodRequest.inputArgumentsSize = 4;
  270. callMethodRequest.inputArguments = inputArguments;
  271. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  272. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDPUBLISHEDDATAITEMS);
  273. UA_CallMethodResult result;
  274. UA_CallMethodResult_init(&result);
  275. result = UA_Server_call(server, &callMethodRequest);
  276. ck_assert_int_eq(3, result.outputArgumentsSize);
  277. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  278. //TODO checked correctness of created items
  279. UA_CallMethodResult_deleteMembers(&result);
  280. UA_free(inputArguments);
  281. UA_free(fieldNameAliases);
  282. UA_free(dataSetFieldFlags);
  283. UA_free(variablesToAdd);
  284. UA_Client_disconnect(client);
  285. UA_Client_delete(client);
  286. } END_TEST
  287. START_TEST(AddAndRemoveWriterGroups){
  288. UA_StatusCode retVal;
  289. UA_Client *client = UA_Client_new();
  290. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  291. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  292. if(retVal != UA_STATUSCODE_GOOD) {
  293. UA_Client_delete(client);
  294. }
  295. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  296. UA_NodeId createdConnection = addPubSubConnection();
  297. UA_Variant *inputArgument = (UA_Variant *) UA_calloc(1, (sizeof(UA_Variant)));
  298. UA_WriterGroupDataType writerGroupDataType;
  299. UA_WriterGroupDataType_init(&writerGroupDataType);
  300. writerGroupDataType.name = UA_STRING("TestWriterGroup");
  301. writerGroupDataType.enabled = UA_TRUE;
  302. writerGroupDataType.publishingInterval = 500;
  303. writerGroupDataType.writerGroupId = 1234;
  304. UA_Variant_setScalar(inputArgument, &writerGroupDataType, &UA_TYPES[UA_TYPES_WRITERGROUPDATATYPE]);
  305. UA_CallMethodRequest callMethodRequest;
  306. UA_CallMethodRequest_init(&callMethodRequest);
  307. callMethodRequest.inputArgumentsSize = 1;
  308. callMethodRequest.inputArguments = inputArgument;
  309. callMethodRequest.objectId = createdConnection;
  310. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBSUBCONNECTIONTYPE_ADDWRITERGROUP);
  311. UA_CallMethodResult result;
  312. UA_CallMethodResult_init(&result);
  313. result = UA_Server_call(server, &callMethodRequest);
  314. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  315. ck_assert_int_eq(1, result.outputArgumentsSize);
  316. UA_NodeId createdWriterGroup = UA_NODEID_NULL;
  317. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  318. createdWriterGroup = *((UA_NodeId *) result.outputArguments->data);
  319. UA_LocalizedText writerGroupDisplayName;
  320. UA_LocalizedText_init(&writerGroupDisplayName);
  321. retVal = UA_Server_readDisplayName(server, createdWriterGroup, &writerGroupDisplayName);
  322. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  323. UA_String compareText = UA_STRING("TestWriterGroup");
  324. ck_assert(UA_String_equal(&writerGroupDisplayName.text, &compareText) == UA_TRUE);
  325. UA_free(inputArgument);
  326. UA_CallMethodResult_deleteMembers(&result);
  327. UA_Client_disconnect(client);
  328. UA_Client_delete(client);
  329. UA_LocalizedText_deleteMembers(&writerGroupDisplayName);
  330. } END_TEST
  331. int main(void) {
  332. TCase *tc_add_pubsub_informationmodel_methods_connection = tcase_create("PubSub connection delete and creation using the information model methods");
  333. tcase_add_checked_fixture(tc_add_pubsub_informationmodel_methods_connection, setup, teardown);
  334. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddNewPubSubConnectionUsingTheInformationModelMethod);
  335. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemovePublishedDataSetFolders);
  336. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemovePublishedDataSetItems);
  337. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemoveWriterGroups);
  338. //TODO TestCase add publishedDataItems and removePublishedDataItems, writergroup remove
  339. Suite *s = suite_create("PubSub CRUD configuration by the information model functions");
  340. suite_add_tcase(s, tc_add_pubsub_informationmodel_methods_connection);
  341. SRunner *sr = srunner_create(s);
  342. srunner_set_fork_status(sr, CK_NOFORK);
  343. srunner_run_all(sr,CK_NORMAL);
  344. int number_failed = srunner_ntests_failed(sr);
  345. srunner_free(sr);
  346. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  347. }