check_pubsub_informationmodel_methods.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_NodeId connection1, connection2, writerGroup1, writerGroup2, writerGroup3,
  22. publishedDataSet1, publishedDataSet2, dataSetWriter1, dataSetWriter2, dataSetWriter3;
  23. UA_Server *server = NULL;
  24. UA_ServerConfig *config = NULL;
  25. UA_Boolean running;
  26. THREAD_HANDLE server_thread;
  27. THREAD_CALLBACK(serverloop) {
  28. while (running)
  29. UA_Server_run_iterate(server, true);
  30. return 0;
  31. }
  32. static void setup(void) {
  33. running = true;
  34. config = UA_ServerConfig_new_default();
  35. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  36. if(!config->pubsubTransportLayers) {
  37. UA_ServerConfig_delete(config);
  38. }
  39. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  40. config->pubsubTransportLayersSize++;
  41. server = UA_Server_new(config);
  42. UA_Server_run_startup(server);
  43. THREAD_CREATE(server_thread, serverloop);
  44. }
  45. static void teardown(void) {
  46. running = false;
  47. THREAD_JOIN(server_thread);
  48. UA_Server_run_shutdown(server);
  49. UA_Server_delete(server);
  50. UA_ServerConfig_delete(config);
  51. }
  52. static UA_NodeId
  53. findSingleChildNode(UA_QualifiedName targetName,
  54. UA_NodeId referenceTypeId, UA_NodeId startingNode){
  55. UA_NodeId resultNodeId;
  56. UA_RelativePathElement rpe;
  57. UA_RelativePathElement_init(&rpe);
  58. rpe.referenceTypeId = referenceTypeId;
  59. rpe.isInverse = false;
  60. rpe.includeSubtypes = false;
  61. rpe.targetName = targetName;
  62. UA_BrowsePath bp;
  63. UA_BrowsePath_init(&bp);
  64. bp.startingNode = startingNode;
  65. bp.relativePath.elementsSize = 1;
  66. bp.relativePath.elements = &rpe;
  67. UA_BrowsePathResult bpr =
  68. UA_Server_translateBrowsePathToNodeIds(server, &bp);
  69. if(bpr.statusCode != UA_STATUSCODE_GOOD ||
  70. bpr.targetsSize < 1)
  71. return UA_NODEID_NULL;
  72. if(UA_NodeId_copy(&bpr.targets[0].targetId.nodeId, &resultNodeId) != UA_STATUSCODE_GOOD){
  73. UA_BrowsePathResult_deleteMembers(&bpr);
  74. return UA_NODEID_NULL;
  75. }
  76. UA_BrowsePathResult_deleteMembers(&bpr);
  77. return resultNodeId;
  78. }
  79. /*
  80. static UA_StatusCode startServer(void){
  81. config = UA_ServerConfig_new_default();
  82. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  83. if(!config->pubsubTransportLayers) {
  84. UA_ServerConfig_delete(config);
  85. }
  86. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  87. config->pubsubTransportLayersSize++;
  88. server = UA_Server_new(config);
  89. return UA_Server_run_startup(server);
  90. }*/
  91. START_TEST(AddNewPubSubConnectionUsingTheInformationModelMethod){
  92. UA_StatusCode retVal;
  93. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  94. retVal = UA_Client_connect(client, "opc.tcp://localhost:4840");
  95. if(retVal != UA_STATUSCODE_GOOD) {
  96. UA_Client_delete(client);
  97. }
  98. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  99. UA_Variant publisherId;
  100. UA_Variant_init(&publisherId);
  101. UA_UInt32 publisherIdValue = 13245;
  102. UA_Variant_setScalar(&publisherId, &publisherIdValue , &UA_TYPES[UA_TYPES_UINT32]);
  103. UA_PubSubConnectionDataType pubSubConnection;
  104. UA_PubSubConnectionDataType_init(&pubSubConnection);
  105. pubSubConnection.name = UA_STRING("Model Connection 1");
  106. pubSubConnection.enabled = UA_TRUE;
  107. pubSubConnection.publisherId = publisherId;
  108. pubSubConnection.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  109. UA_ExtensionObject eo;
  110. eo.encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
  111. UA_NetworkAddressUrlDataType networkAddressDataType = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  112. UA_ByteString_allocBuffer(&eo.content.encoded.body, UA_NetworkAddressUrlDataType_calcSizeBinary(&networkAddressDataType));
  113. UA_Byte *bufPos = eo.content.encoded.body.data;
  114. UA_NetworkAddressUrlDataType_encodeBinary(&networkAddressDataType, &bufPos, &(eo.content.encoded.body.data[eo.content.encoded.body.length]));
  115. eo.content.encoded.typeId = UA_NODEID_NUMERIC(0, UA_TYPES_NETWORKADDRESSURLDATATYPE);
  116. pubSubConnection.address = eo;
  117. UA_Variant inputArguments;
  118. UA_Variant_init(&inputArguments);
  119. UA_Variant_setScalar(&inputArguments, &pubSubConnection, &UA_TYPES[UA_TYPES_PUBSUBCONNECTIONDATATYPE]);
  120. UA_CallMethodRequest callMethodRequest;
  121. UA_CallMethodRequest_init(&callMethodRequest);
  122. callMethodRequest.inputArgumentsSize = 1;
  123. callMethodRequest.inputArguments = &inputArguments;
  124. callMethodRequest.objectId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE);
  125. callMethodRequest.methodId = UA_NODEID_NUMERIC(0, UA_NS0ID_PUBLISHSUBSCRIBE_ADDCONNECTION);
  126. UA_CallMethodResult result;
  127. UA_CallMethodResult_init(&result);
  128. result = UA_Server_call(server, &callMethodRequest);
  129. ck_assert_int_eq(1, result.outputArgumentsSize);
  130. ck_assert_int_eq(result.statusCode, UA_STATUSCODE_GOOD);
  131. UA_NodeId createdConnection;
  132. if(result.outputArguments->type == &UA_TYPES[UA_TYPES_NODEID])
  133. createdConnection = *((UA_NodeId *) result.outputArguments->data);
  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. ck_assert_str_eq((const char *) connectionDisplayName.text.data, "Model Connection 1");
  139. //todo browse and check childs
  140. UA_Variant serverPubSubConnectionValues;
  141. UA_Variant_init(&serverPubSubConnectionValues);
  142. /*UA_NodeId connectionAddress = findSingleChildNode(UA_QUALIFIEDNAME(0, "Address"),
  143. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  144. createdConnection);*/
  145. UA_NodeId connectionPublisherId = findSingleChildNode(UA_QUALIFIEDNAME(0, "PublisherId"),
  146. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  147. createdConnection);
  148. ck_assert_int_eq(UA_Server_readValue(server, connectionPublisherId, &serverPubSubConnectionValues),
  149. UA_STATUSCODE_GOOD);
  150. ck_assert_uint_eq(*((UA_UInt32 *) serverPubSubConnectionValues.data), publisherIdValue);
  151. //TODO create nd add a connection with string pulisherId
  152. } END_TEST
  153. int main(void) {
  154. //startServer(); //TODO CHECK IF SERVER IS RUNNING
  155. TCase *tc_add_pubsub_informationmodel_methods_connection = tcase_create("PubSub connection delete and creation using the information model methods");
  156. tcase_add_checked_fixture(tc_add_pubsub_informationmodel_methods_connection, setup, teardown);
  157. tcase_add_test(tc_add_pubsub_informationmodel_methods_connection, AddNewPubSubConnectionUsingTheInformationModelMethod);
  158. Suite *s = suite_create("PubSub CRUD configuration by the information model functions");
  159. suite_add_tcase(s, tc_add_pubsub_informationmodel_methods_connection);
  160. SRunner *sr = srunner_create(s);
  161. srunner_set_fork_status(sr, CK_NOFORK);
  162. srunner_run_all(sr,CK_NORMAL);
  163. int number_failed = srunner_ntests_failed(sr);
  164. srunner_free(sr);
  165. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  166. }