check_pubsub_informationmodel_methods.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. eo.encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
  90. UA_NetworkAddressUrlDataType networkAddressDataType = {UA_STRING("eth0"), UA_STRING("opc.udp://224.0.0.22:4840/")};
  91. UA_ByteString_allocBuffer(&eo.content.encoded.body, UA_NetworkAddressUrlDataType_calcSizeBinary(&networkAddressDataType));
  92. UA_Byte *bufPos = eo.content.encoded.body.data;
  93. UA_NetworkAddressUrlDataType_encodeBinary(&networkAddressDataType, &bufPos, &(eo.content.encoded.body.data[eo.content.encoded.body.length]));
  94. eo.content.encoded.typeId = UA_NODEID_NUMERIC(0, UA_TYPES_NETWORKADDRESSURLDATATYPE);
  95. pubSubConnection.address = eo;
  96. pubSubConnection.connectionPropertiesSize = 2;
  97. UA_KeyValuePair connectionOptions[2];
  98. memset(connectionOptions, 0, sizeof(UA_KeyValuePair)* 2);
  99. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  100. UA_UInt32 ttl = 10;
  101. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  102. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  103. UA_Boolean loopback = UA_FALSE;
  104. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_UINT32]);
  105. pubSubConnection.connectionProperties = connectionOptions;
  106. UA_Variant inputArguments;
  107. UA_Variant_init(&inputArguments);
  108. UA_Variant_setScalar(&inputArguments, &pubSubConnection, &UA_TYPES[UA_TYPES_PUBSUBCONNECTIONDATATYPE]);
  109. UA_CallMethodRequest callMethodRequest;
  110. UA_CallMethodRequest_init(&callMethodRequest);
  111. callMethodRequest.inputArgumentsSize = 1;
  112. callMethodRequest.inputArguments = &inputArguments;
  113. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE);
  114. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_ADDCONNECTION);
  115. UA_NodeId connectionId = UA_NODEID_NULL;
  116. UA_CallMethodResult result;
  117. UA_CallMethodResult_init(&result);
  118. result = UA_Server_call(server, &callMethodRequest);
  119. ck_assert_int_eq(1, result.outputArgumentsSize);
  120. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  121. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  122. connectionId = *((UA_NodeId *) result.outputArguments->data);
  123. UA_ExtensionObject_deleteMembers(&eo);
  124. callMethodRequest.inputArguments = NULL;
  125. callMethodRequest.inputArgumentsSize = 0;
  126. UA_CallMethodRequest_deleteMembers(&callMethodRequest);
  127. UA_CallMethodResult_deleteMembers(&result);
  128. return connectionId;
  129. }
  130. START_TEST(AddNewPubSubConnectionUsingTheInformationModelMethod){
  131. UA_StatusCode retVal;
  132. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  133. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  134. if(retVal != UA_STATUSCODE_GOOD) {
  135. UA_Client_delete(client);
  136. }
  137. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  138. UA_NodeId createdConnection = addPubSubConnection();
  139. UA_LocalizedText connectionDisplayName;
  140. UA_LocalizedText_init(&connectionDisplayName);
  141. retVal = UA_Server_readDisplayName(server, createdConnection, &connectionDisplayName);
  142. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  143. UA_String compareText = UA_STRING("Model Connection 1");
  144. ck_assert(UA_String_equal(&connectionDisplayName.text, &compareText) == UA_TRUE);
  145. //todo browse and check childs
  146. UA_Variant serverPubSubConnectionValues;
  147. UA_Variant_init(&serverPubSubConnectionValues);
  148. UA_NodeId connectionPublisherId = findSingleChildNode(UA_QUALIFIEDNAME(0, "PublisherId"),
  149. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  150. createdConnection);
  151. ck_assert_int_eq(UA_Server_readValue(server, connectionPublisherId, &serverPubSubConnectionValues),
  152. UA_STATUSCODE_GOOD);
  153. ck_assert_uint_eq(*((UA_UInt32 *) serverPubSubConnectionValues.data), 13245);
  154. UA_Variant_deleteMembers(&serverPubSubConnectionValues);
  155. UA_Client_disconnect(client);
  156. UA_Client_delete(client);
  157. UA_LocalizedText_deleteMembers(&connectionDisplayName);
  158. } END_TEST
  159. START_TEST(AddAndRemovePublishedDataSetFolders){
  160. UA_StatusCode retVal;
  161. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  162. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  163. if(retVal != UA_STATUSCODE_GOOD) {
  164. UA_Client_delete(client);
  165. }
  166. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  167. UA_String folderName = UA_STRING("TestFolder");
  168. UA_Variant inputArguments;
  169. UA_Variant_init(&inputArguments);
  170. UA_Variant_setScalar(&inputArguments, &folderName, &UA_TYPES[UA_TYPES_STRING]);
  171. UA_CallMethodRequest callMethodRequest;
  172. UA_CallMethodRequest_init(&callMethodRequest);
  173. callMethodRequest.inputArgumentsSize = 1;
  174. callMethodRequest.inputArguments = &inputArguments;
  175. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  176. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDDATASETFOLDER);
  177. UA_CallMethodResult result;
  178. UA_CallMethodResult_init(&result);
  179. result = UA_Server_call(server, &callMethodRequest);
  180. ck_assert_int_eq(1, result.outputArgumentsSize);
  181. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  182. UA_NodeId createdFolder = UA_NODEID_NULL;
  183. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  184. createdFolder = *((UA_NodeId *) result.outputArguments->data);
  185. UA_LocalizedText connectionDisplayName;
  186. UA_LocalizedText_init(&connectionDisplayName);
  187. retVal = UA_Server_readDisplayName(server, createdFolder, &connectionDisplayName);
  188. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  189. UA_String compareText = UA_STRING("TestFolder");
  190. ck_assert(UA_String_equal(&connectionDisplayName.text, &compareText) == UA_TRUE);
  191. retVal = UA_Server_readNodeId(server, createdFolder, &createdFolder);
  192. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  193. UA_CallMethodResult_deleteMembers(&result);
  194. UA_LocalizedText_deleteMembers(&connectionDisplayName);
  195. //create folder inside the new folder
  196. folderName = UA_STRING("TestFolder2");
  197. UA_Variant_init(&inputArguments);
  198. UA_Variant_setScalar(&inputArguments, &folderName, &UA_TYPES[UA_TYPES_STRING]);
  199. UA_CallMethodRequest_init(&callMethodRequest);
  200. callMethodRequest.inputArgumentsSize = 1;
  201. callMethodRequest.inputArguments = &inputArguments;
  202. callMethodRequest.objectId = createdFolder;
  203. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDDATASETFOLDER);
  204. UA_CallMethodResult_init(&result);
  205. result = UA_Server_call(server, &callMethodRequest);
  206. ck_assert_int_eq(1, result.outputArgumentsSize);
  207. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  208. UA_NodeId createdFolder2 = UA_NODEID_NULL;
  209. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  210. createdFolder2 = *((UA_NodeId *) result.outputArguments->data);
  211. UA_LocalizedText_init(&connectionDisplayName);
  212. retVal = UA_Server_readDisplayName(server, createdFolder2, &connectionDisplayName);
  213. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  214. compareText = UA_STRING("TestFolder2");
  215. ck_assert(UA_String_equal(&connectionDisplayName.text, &compareText) == UA_TRUE);
  216. retVal = UA_Server_readNodeId(server, createdFolder2, &createdFolder2);
  217. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  218. UA_CallMethodResult_deleteMembers(&result);
  219. //delete the folder
  220. UA_Variant_init(&inputArguments);
  221. UA_Variant_setScalar(&inputArguments, &createdFolder, &UA_TYPES[UA_TYPES_NODEID]);
  222. UA_CallMethodRequest_init(&callMethodRequest);
  223. callMethodRequest.inputArgumentsSize = 1;
  224. callMethodRequest.inputArguments = &inputArguments;
  225. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  226. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_REMOVEDATASETFOLDER);
  227. result = UA_Server_call(server, &callMethodRequest);
  228. ck_assert_int_eq(0, result.outputArgumentsSize);
  229. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  230. //check if the node is deleted
  231. retVal = UA_Server_readNodeId(server, createdFolder, NULL);
  232. ck_assert_int_eq(retVal, UA_STATUSCODE_BADNODEIDUNKNOWN);
  233. UA_CallMethodResult_deleteMembers(&result);
  234. UA_Client_disconnect(client);
  235. UA_Client_delete(client);
  236. UA_LocalizedText_deleteMembers(&connectionDisplayName);
  237. } END_TEST
  238. START_TEST(AddAndRemovePublishedDataSetItems){
  239. UA_StatusCode retVal;
  240. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  241. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  242. if(retVal != UA_STATUSCODE_GOOD) {
  243. UA_Client_delete(client);
  244. }
  245. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  246. UA_Variant *inputArguments = (UA_Variant *) UA_calloc(4, (sizeof(UA_Variant)));
  247. UA_String pdsName = UA_STRING("Test PDS");
  248. UA_Variant_setScalar(&inputArguments[0], &pdsName, &UA_TYPES[UA_TYPES_STRING]);
  249. UA_String *fieldNameAliases = (UA_String *) UA_calloc(2, sizeof(UA_String));
  250. fieldNameAliases[0] = UA_STRING("field1");
  251. fieldNameAliases[1] = UA_STRING("field2");
  252. UA_Variant_setArray(&inputArguments[1], fieldNameAliases, 2, &UA_TYPES[UA_TYPES_STRING]);
  253. UA_DataSetFieldFlags *dataSetFieldFlags = (UA_DataSetFieldFlags *) UA_calloc(2, sizeof(UA_DataSetFieldFlags));
  254. dataSetFieldFlags[0] = UA_DATASETFIELDFLAGS_PROMOTEDFIELD;
  255. dataSetFieldFlags[1] = UA_DATASETFIELDFLAGS_PROMOTEDFIELD;
  256. UA_Variant_setArray(&inputArguments[2], dataSetFieldFlags, 2, &UA_TYPES[UA_TYPES_DATASETFIELDFLAGS]);
  257. UA_PublishedVariableDataType *variablesToAdd = (UA_PublishedVariableDataType *) UA_calloc(2, sizeof(UA_PublishedVariableDataType));
  258. variablesToAdd[0].publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  259. variablesToAdd[0].attributeId = UA_ATTRIBUTEID_VALUE;
  260. variablesToAdd[1].publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERREDUNDANCY_CURRENTSERVERID);
  261. variablesToAdd[1].attributeId = UA_ATTRIBUTEID_VALUE;
  262. UA_Variant_setArray(&inputArguments[3], variablesToAdd, 2, &UA_TYPES[UA_TYPES_PUBLISHEDVARIABLEDATATYPE]);
  263. UA_CallMethodRequest callMethodRequest;
  264. UA_CallMethodRequest_init(&callMethodRequest);
  265. callMethodRequest.inputArgumentsSize = 4;
  266. callMethodRequest.inputArguments = inputArguments;
  267. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  268. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDPUBLISHEDDATAITEMS);
  269. UA_CallMethodResult result;
  270. UA_CallMethodResult_init(&result);
  271. result = UA_Server_call(server, &callMethodRequest);
  272. ck_assert_int_eq(3, result.outputArgumentsSize);
  273. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  274. //TODO checked correctness of created items
  275. UA_CallMethodResult_deleteMembers(&result);
  276. UA_free(inputArguments);
  277. UA_free(fieldNameAliases);
  278. UA_free(dataSetFieldFlags);
  279. UA_free(variablesToAdd);
  280. UA_Client_disconnect(client);
  281. UA_Client_delete(client);
  282. } END_TEST
  283. START_TEST(AddAndRemoveWriterGroups){
  284. UA_StatusCode retVal;
  285. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  286. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  287. if(retVal != UA_STATUSCODE_GOOD) {
  288. UA_Client_delete(client);
  289. }
  290. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  291. UA_NodeId createdConnection = addPubSubConnection();
  292. UA_Variant *inputArgument = (UA_Variant *) UA_calloc(1, (sizeof(UA_Variant)));
  293. UA_WriterGroupDataType writerGroupDataType;
  294. UA_WriterGroupDataType_init(&writerGroupDataType);
  295. writerGroupDataType.name = UA_STRING("TestWriterGroup");
  296. writerGroupDataType.enabled = UA_TRUE;
  297. writerGroupDataType.publishingInterval = 500;
  298. writerGroupDataType.writerGroupId = 1234;
  299. UA_Variant_setScalar(inputArgument, &writerGroupDataType, &UA_TYPES[UA_TYPES_WRITERGROUPDATATYPE]);
  300. UA_CallMethodRequest callMethodRequest;
  301. UA_CallMethodRequest_init(&callMethodRequest);
  302. callMethodRequest.inputArgumentsSize = 1;
  303. callMethodRequest.inputArguments = inputArgument;
  304. callMethodRequest.objectId = createdConnection;
  305. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBSUBCONNECTIONTYPE_ADDWRITERGROUP);
  306. UA_CallMethodResult result;
  307. UA_CallMethodResult_init(&result);
  308. result = UA_Server_call(server, &callMethodRequest);
  309. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  310. ck_assert_int_eq(1, result.outputArgumentsSize);
  311. UA_NodeId createdWriterGroup = UA_NODEID_NULL;
  312. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  313. createdWriterGroup = *((UA_NodeId *) result.outputArguments->data);
  314. UA_LocalizedText writerGroupDisplayName;
  315. UA_LocalizedText_init(&writerGroupDisplayName);
  316. retVal = UA_Server_readDisplayName(server, createdWriterGroup, &writerGroupDisplayName);
  317. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  318. UA_String compareText = UA_STRING("TestWriterGroup");
  319. ck_assert(UA_String_equal(&writerGroupDisplayName.text, &compareText) == UA_TRUE);
  320. UA_free(inputArgument);
  321. UA_CallMethodResult_deleteMembers(&result);
  322. UA_Client_disconnect(client);
  323. UA_Client_delete(client);
  324. UA_LocalizedText_deleteMembers(&writerGroupDisplayName);
  325. } END_TEST
  326. int main(void) {
  327. TCase *tc_add_pubsub_informationmodel_methods_connection = tcase_create("PubSub connection delete and creation using the information model methods");
  328. tcase_add_checked_fixture(tc_add_pubsub_informationmodel_methods_connection, setup, teardown);
  329. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddNewPubSubConnectionUsingTheInformationModelMethod);
  330. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemovePublishedDataSetFolders);
  331. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemovePublishedDataSetItems);
  332. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemoveWriterGroups);
  333. //TODO TestCase add publishedDataItems and removePublishedDataItems, writergroup remove
  334. Suite *s = suite_create("PubSub CRUD configuration by the information model functions");
  335. suite_add_tcase(s, tc_add_pubsub_informationmodel_methods_connection);
  336. SRunner *sr = srunner_create(s);
  337. srunner_set_fork_status(sr, CK_NOFORK);
  338. srunner_run_all(sr,CK_NORMAL);
  339. int number_failed = srunner_ntests_failed(sr);
  340. srunner_free(sr);
  341. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  342. }