check_pubsub_publish_json.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "ua_server_pubsub.h"
  8. #include "src_generated/ua_types_generated_encoding_binary.h"
  9. #include "ua_types.h"
  10. #include "ua_pubsub.h"
  11. #include "ua_config_default.h"
  12. #include "ua_network_pubsub_udp.h"
  13. #include "ua_server_internal.h"
  14. #include "check.h"
  15. #include "stdio.h"
  16. UA_Server *server = NULL;
  17. UA_ServerConfig *config = NULL;
  18. UA_NodeId connection1, writerGroup1, publishedDataSet1, dataSetWriter1;
  19. static void setup(void) {
  20. config = UA_ServerConfig_new_default();
  21. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  22. if(!config->pubsubTransportLayers) {
  23. UA_ServerConfig_delete(config);
  24. }
  25. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  26. config->pubsubTransportLayersSize++;
  27. server = UA_Server_new(config);
  28. UA_Server_run_startup(server);
  29. UA_PubSubConnectionConfig connectionConfig;
  30. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  31. connectionConfig.name = UA_STRING("UADP Connection");
  32. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  33. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  34. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  35. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  36. UA_Server_addPubSubConnection(server, &connectionConfig, &connection1);
  37. }
  38. static void teardown(void) {
  39. UA_Server_run_shutdown(server);
  40. UA_Server_delete(server);
  41. UA_ServerConfig_delete(config);
  42. }
  43. START_TEST(SinglePublishDataSetField){
  44. UA_WriterGroupConfig writerGroupConfig;
  45. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  46. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  47. writerGroupConfig.messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_JSONDATASETWRITERMESSAGEDATATYPE];
  48. UA_JsonDataSetWriterMessageDataType d;
  49. d.dataSetMessageContentMask = UA_JSONDATASETMESSAGECONTENTMASK_SEQUENCENUMBER;
  50. writerGroupConfig.messageSettings.content.decoded.data = &d;
  51. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  52. writerGroupConfig.publishingInterval = 10;
  53. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_JSON;
  54. UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &writerGroup1);
  55. UA_PublishedDataSetConfig pdsConfig;
  56. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  57. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  58. pdsConfig.name = UA_STRING("PublishedDataSet 1");
  59. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSet1);
  60. UA_DataSetWriterConfig dataSetWriterConfig;
  61. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  62. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1");
  63. UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter1);
  64. UA_DataSetFieldConfig dataSetFieldConfig;
  65. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  66. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  67. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  68. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  69. dataSetFieldConfig.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  70. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  71. UA_Server_addDataSetField(server, publishedDataSet1, &dataSetFieldConfig, NULL);
  72. UA_WriterGroup *wg = UA_WriterGroup_findWGbyId(server, writerGroup1);
  73. UA_WriterGroup_publishCallback(server, wg);
  74. } END_TEST
  75. int main(void) {
  76. TCase *tc_pubsub_publish = tcase_create("PubSub publish");
  77. tcase_add_checked_fixture(tc_pubsub_publish, setup, teardown);
  78. tcase_add_test(tc_pubsub_publish, SinglePublishDataSetField);
  79. Suite *s = suite_create("PubSub publishing json via udp");
  80. suite_add_tcase(s, tc_pubsub_publish);
  81. SRunner *sr = srunner_create(s);
  82. srunner_set_fork_status(sr, CK_NOFORK);
  83. srunner_run_all(sr,CK_NORMAL);
  84. int number_failed = srunner_ntests_failed(sr);
  85. srunner_free(sr);
  86. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  87. }