check_pubsub_informationmodel_methods.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "src_generated/ua_types_generated.h"
  11. #include "ua_types.h"
  12. #include "src_generated/ua_types_generated_encoding_binary.h"
  13. #include "ua_types.h"
  14. #include "ua_server_pubsub.h"
  15. #include "src_generated/ua_types_generated.h"
  16. #include "ua_network_pubsub_udp.h"
  17. #include "ua_server_internal.h"
  18. #include "check.h"
  19. #include "ua_plugin_pubsub.h"
  20. #include "ua_config_default.h"
  21. #include "thread_wrapper.h"
  22. UA_NodeId connection1, connection2, writerGroup1, writerGroup2, writerGroup3,
  23. publishedDataSet1, publishedDataSet2, dataSetWriter1, dataSetWriter2, dataSetWriter3;
  24. UA_Server *server = NULL;
  25. UA_ServerConfig *config = NULL;
  26. UA_Boolean running;
  27. THREAD_HANDLE server_thread;
  28. THREAD_CALLBACK(serverloop) {
  29. while (running)
  30. UA_Server_run_iterate(server, true);
  31. return 0;
  32. }
  33. static void setup(void) {
  34. running = true;
  35. config = UA_ServerConfig_new_default();
  36. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  37. if(!config->pubsubTransportLayers) {
  38. UA_ServerConfig_delete(config);
  39. }
  40. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  41. config->pubsubTransportLayersSize++;
  42. server = UA_Server_new(config);
  43. UA_Server_run_startup(server);
  44. THREAD_CREATE(server_thread, serverloop);
  45. }
  46. static void teardown(void) {
  47. running = false;
  48. THREAD_JOIN(server_thread);
  49. UA_Server_run_shutdown(server);
  50. UA_Server_delete(server);
  51. UA_ServerConfig_delete(config);
  52. }
  53. static UA_NodeId
  54. findSingleChildNode(UA_QualifiedName targetName,
  55. UA_NodeId referenceTypeId, UA_NodeId startingNode){
  56. UA_NodeId resultNodeId;
  57. UA_RelativePathElement rpe;
  58. UA_RelativePathElement_init(&rpe);
  59. rpe.referenceTypeId = referenceTypeId;
  60. rpe.isInverse = false;
  61. rpe.includeSubtypes = false;
  62. rpe.targetName = targetName;
  63. UA_BrowsePath bp;
  64. UA_BrowsePath_init(&bp);
  65. bp.startingNode = startingNode;
  66. bp.relativePath.elementsSize = 1;
  67. bp.relativePath.elements = &rpe;
  68. UA_BrowsePathResult bpr =
  69. UA_Server_translateBrowsePathToNodeIds(server, &bp);
  70. if(bpr.statusCode != UA_STATUSCODE_GOOD ||
  71. bpr.targetsSize < 1)
  72. return UA_NODEID_NULL;
  73. if(UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, &resultNodeId) != UA_STATUSCODE_GOOD){
  74. UA_BrowsePathResult_deleteMembers(&bpr);
  75. return UA_NODEID_NULL;
  76. }
  77. UA_BrowsePathResult_deleteMembers(&bpr);
  78. return resultNodeId;
  79. }
  80. START_TEST(AddNewPubSubConnectionUsingTheInformationModelMethod){
  81. UA_StatusCode retVal;
  82. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  83. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  84. if(retVal != UA_STATUSCODE_GOOD) {
  85. UA_Client_delete(client);
  86. }
  87. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  88. UA_Variant publisherId;
  89. UA_Variant_init(&publisherId);
  90. UA_UInt32 publisherIdValue = 13245;
  91. UA_Variant_setScalar(&publisherId, &publisherIdValue , &UA_TYPES[UA_TYPES_UINT32]);
  92. UA_PubSubConnectionDataType pubSubConnection;
  93. UA_PubSubConnectionDataType_init(&pubSubConnection);
  94. pubSubConnection.name = UA_STRING("Model Connection 1");
  95. pubSubConnection.enabled = UA_TRUE;
  96. pubSubConnection.publisherId = publisherId;
  97. pubSubConnection.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  98. UA_ExtensionObject eo;
  99. eo.encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
  100. UA_NetworkAddressUrlDataType networkAddressDataType = {UA_STRING("eth0"), UA_STRING("opc.udp://224.0.0.22:4840/")};
  101. UA_ByteString_allocBuffer(&eo.content.encoded.body, UA_NetworkAddressUrlDataType_calcSizeBinary(&networkAddressDataType));
  102. UA_Byte *bufPos = eo.content.encoded.body.data;
  103. UA_NetworkAddressUrlDataType_encodeBinary(&networkAddressDataType, &bufPos, &(eo.content.encoded.body.data[eo.content.encoded.body.length]));
  104. eo.content.encoded.typeId = UA_NODEID_NUMERIC(0, UA_TYPES_NETWORKADDRESSURLDATATYPE);
  105. pubSubConnection.address = eo;
  106. pubSubConnection.connectionPropertiesSize = 2;
  107. UA_KeyValuePair connectionOptions[2];
  108. memset(connectionOptions, 0, sizeof(UA_KeyValuePair)* 2);
  109. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  110. UA_UInt32 ttl = 10;
  111. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  112. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  113. UA_Boolean loopback = UA_FALSE;
  114. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_UINT32]);
  115. pubSubConnection.connectionProperties = connectionOptions;
  116. UA_Variant inputArguments;
  117. UA_Variant_init(&inputArguments);
  118. UA_Variant_setScalar(&inputArguments, &pubSubConnection, &UA_TYPES[UA_TYPES_PUBSUBCONNECTIONDATATYPE]);
  119. UA_CallMethodRequest callMethodRequest;
  120. UA_CallMethodRequest_init(&callMethodRequest);
  121. callMethodRequest.inputArgumentsSize = 1;
  122. callMethodRequest.inputArguments = &inputArguments;
  123. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE);
  124. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_ADDCONNECTION);
  125. UA_CallMethodResult result;
  126. UA_CallMethodResult_init(&result);
  127. result = UA_Server_call(server, &callMethodRequest);
  128. ck_assert_int_eq(1, result.outputArgumentsSize);
  129. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  130. UA_NodeId createdConnection;
  131. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  132. createdConnection = *((UA_NodeId *) result.outputArguments->data);
  133. UA_LocalizedText connectionDisplayName;
  134. UA_LocalizedText_init(&connectionDisplayName);
  135. retVal = UA_Server_readDisplayName(server, createdConnection, &connectionDisplayName);
  136. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  137. ck_assert_str_eq((const char *) connectionDisplayName.text.data, "Model Connection 1");
  138. //todo browse and check childs
  139. UA_Variant serverPubSubConnectionValues;
  140. UA_Variant_init(&serverPubSubConnectionValues);
  141. /*UA_NodeId connectionAddress = findSingleChildNode(UA_QUALIFIEDNAME(0, "Address"),
  142. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  143. createdConnection);*/
  144. UA_NodeId connectionPublisherId = findSingleChildNode(UA_QUALIFIEDNAME(0, "PublisherId"),
  145. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  146. createdConnection);
  147. ck_assert_int_eq(UA_Server_readValue(server, connectionPublisherId, &serverPubSubConnectionValues),
  148. UA_STATUSCODE_GOOD);
  149. ck_assert_uint_eq(*((UA_UInt32 *) serverPubSubConnectionValues.data), publisherIdValue);
  150. } END_TEST
  151. START_TEST(AddAndRemovePublishedDataSetFolders){
  152. UA_StatusCode retVal;
  153. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  154. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  155. if(retVal != UA_STATUSCODE_GOOD) {
  156. UA_Client_delete(client);
  157. }
  158. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  159. UA_String folderName = UA_STRING("TestFolder");
  160. UA_Variant inputArguments;
  161. UA_Variant_init(&inputArguments);
  162. UA_Variant_setScalar(&inputArguments, &folderName, &UA_TYPES[UA_TYPES_STRING]);
  163. UA_CallMethodRequest callMethodRequest;
  164. UA_CallMethodRequest_init(&callMethodRequest);
  165. callMethodRequest.inputArgumentsSize = 1;
  166. callMethodRequest.inputArguments = &inputArguments;
  167. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  168. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDDATASETFOLDER);
  169. UA_CallMethodResult result;
  170. UA_CallMethodResult_init(&result);
  171. result = UA_Server_call(server, &callMethodRequest);
  172. ck_assert_int_eq(1, result.outputArgumentsSize);
  173. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  174. UA_NodeId createdFolder;
  175. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  176. createdFolder = *((UA_NodeId *) result.outputArguments->data);
  177. UA_LocalizedText connectionDisplayName;
  178. UA_LocalizedText_init(&connectionDisplayName);
  179. retVal = UA_Server_readDisplayName(server, createdFolder, &connectionDisplayName);
  180. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  181. ck_assert_str_eq((const char *) connectionDisplayName.text.data, "TestFolder");
  182. retVal = UA_Server_readNodeId(server, createdFolder, &createdFolder);
  183. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  184. //TODO add folder inside the created folder
  185. //create folder inside the new folder
  186. folderName = UA_STRING("TestFolder2");
  187. UA_Variant_init(&inputArguments);
  188. UA_Variant_setScalar(&inputArguments, &folderName, &UA_TYPES[UA_TYPES_STRING]);
  189. UA_CallMethodRequest_init(&callMethodRequest);
  190. callMethodRequest.inputArgumentsSize = 1;
  191. callMethodRequest.inputArguments = &inputArguments;
  192. callMethodRequest.objectId = createdFolder;
  193. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_ADDDATASETFOLDER);
  194. UA_CallMethodResult_init(&result);
  195. result = UA_Server_call(server, &callMethodRequest);
  196. ck_assert_int_eq(1, result.outputArgumentsSize);
  197. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  198. UA_NodeId createdFolder2;
  199. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  200. createdFolder2 = *((UA_NodeId *) result.outputArguments->data);
  201. UA_LocalizedText_init(&connectionDisplayName);
  202. retVal = UA_Server_readDisplayName(server, createdFolder2, &connectionDisplayName);
  203. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  204. ck_assert_str_eq((const char *) connectionDisplayName.text.data, "TestFolder2");
  205. retVal = UA_Server_readNodeId(server, createdFolder2, &createdFolder2);
  206. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  207. //delete the folder
  208. UA_Variant_init(&inputArguments);
  209. UA_Variant_setScalar(&inputArguments, &createdFolder, &UA_TYPES[UA_TYPES_NODEID]);
  210. UA_CallMethodRequest_init(&callMethodRequest);
  211. callMethodRequest.inputArgumentsSize = 1;
  212. callMethodRequest.inputArguments = &inputArguments;
  213. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_PUBLISHEDDATASETS);
  214. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_DATASETFOLDERTYPE_REMOVEDATASETFOLDER);
  215. result = UA_Server_call(server, &callMethodRequest);
  216. ck_assert_int_eq(0, result.outputArgumentsSize);
  217. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  218. //check if the node is deleted
  219. retVal = UA_Server_readNodeId(server, createdFolder, NULL);
  220. ck_assert_int_eq(retVal, UA_STATUSCODE_BADNODEIDUNKNOWN);
  221. } END_TEST
  222. int main(void) {
  223. TCase *tc_add_pubsub_informationmodel_methods_connection = tcase_create("PubSub connection delete and creation using the information model methods");
  224. tcase_add_checked_fixture(tc_add_pubsub_informationmodel_methods_connection, setup, teardown);
  225. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddNewPubSubConnectionUsingTheInformationModelMethod);
  226. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddAndRemovePublishedDataSetFolders);
  227. Suite *s = suite_create("PubSub CRUD configuration by the information model functions");
  228. suite_add_tcase(s, tc_add_pubsub_informationmodel_methods_connection);
  229. SRunner *sr = srunner_create(s);
  230. srunner_set_fork_status(sr, CK_NOFORK);
  231. srunner_run_all(sr,CK_NORMAL);
  232. int number_failed = srunner_ntests_failed(sr);
  233. srunner_free(sr);
  234. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  235. }