check_pubsub_informationmodel_methods.c 18 KB

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