tutorial_pubsub_subscribe.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright (c) 2019 Kalycito Infotech Private Limited
  5. */
  6. /**
  7. * IMPORTANT ANNOUNCEMENT
  8. * The PubSub Subscriber API is currently not finished. This example can be used
  9. * to receive and display values that are published by tutorial_pubsub_publish
  10. * example in the TargetVariables of Subscriber Information Model .
  11. */
  12. #include <open62541/plugin/log_stdout.h>
  13. #include <open62541/plugin/pubsub_udp.h>
  14. #include <open62541/server.h>
  15. #include <open62541/server_config_default.h>
  16. #include <open62541/types_generated.h>
  17. #include "ua_pubsub.h"
  18. #ifdef UA_ENABLE_PUBSUB_ETH_UADP
  19. #include <open62541/plugin/pubsub_ethernet.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <stdlib.h>
  24. UA_NodeId connectionIdentifier;
  25. UA_NodeId readerGroupIdentifier;
  26. UA_NodeId readerIdentifier;
  27. UA_DataSetReaderConfig readerConfig;
  28. static void fillTestDataSetMetaData(UA_DataSetMetaDataType *pMetaData);
  29. /* Add new connection to the server */
  30. static void
  31. addPubSubConnection(UA_Server *server, UA_String *transportProfile,
  32. UA_NetworkAddressUrlDataType *networkAddressUrl) {
  33. if((server == NULL) && (transportProfile == NULL) &&
  34. (networkAddressUrl == NULL)) {
  35. return;
  36. }
  37. /* Configuration creation for the connection */
  38. UA_PubSubConnectionConfig connectionConfig;
  39. memset (&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  40. connectionConfig.name = UA_STRING("UDPMC Connection 1");
  41. connectionConfig.transportProfileUri = *transportProfile;
  42. connectionConfig.enabled = UA_TRUE;
  43. UA_Variant_setScalar(&connectionConfig.address, networkAddressUrl,
  44. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  45. connectionConfig.publisherId.numeric = UA_UInt32_random ();
  46. UA_Server_addPubSubConnection (server, &connectionConfig, &connectionIdentifier);
  47. }
  48. /* Add ReaderGroup to the created connection */
  49. static void
  50. addReaderGroup(UA_Server *server) {
  51. if(server == NULL) {
  52. return;
  53. }
  54. UA_ReaderGroupConfig readerGroupConfig;
  55. memset (&readerGroupConfig, 0, sizeof(UA_ReaderGroupConfig));
  56. readerGroupConfig.name = UA_STRING("ReaderGroup1");
  57. UA_Server_addReaderGroup(server, connectionIdentifier, &readerGroupConfig,
  58. &readerGroupIdentifier);
  59. }
  60. /* Add DataSetReader to the ReaderGroup */
  61. static void
  62. addDataSetReader(UA_Server *server) {
  63. if(server == NULL) {
  64. return;
  65. }
  66. memset (&readerConfig, 0, sizeof(UA_DataSetReaderConfig));
  67. readerConfig.name = UA_STRING("DataSet Reader 1");
  68. readerConfig.dataSetWriterId = 1;
  69. /* Setting up Meta data configuration in DataSetReader */
  70. fillTestDataSetMetaData(&readerConfig.dataSetMetaData);
  71. UA_Server_addDataSetReader(server, readerGroupIdentifier, &readerConfig,
  72. &readerIdentifier);
  73. }
  74. /* Set SubscribedDataSet type to TargetVariables data type
  75. * Add subscribedvariables to the DataSetReader */
  76. static void addSubscribedVariables (UA_Server *server, UA_NodeId dataSetReaderId) {
  77. if(server == NULL) {
  78. return;
  79. }
  80. UA_NodeId folderId;
  81. UA_String folderName = readerConfig.dataSetMetaData.name;
  82. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  83. UA_QualifiedName folderBrowseName;
  84. if(folderName.length > 0) {
  85. oAttr.displayName.locale = UA_STRING ("en-US");
  86. oAttr.displayName.text = folderName;
  87. folderBrowseName.namespaceIndex = 1;
  88. folderBrowseName.name = folderName;
  89. }
  90. else {
  91. oAttr.displayName = UA_LOCALIZEDTEXT ("en-US", "Subscribed Variables");
  92. folderBrowseName = UA_QUALIFIEDNAME (1, "Subscribed Variables");
  93. }
  94. UA_Server_addObjectNode (server, UA_NODEID_NULL,
  95. UA_NODEID_NUMERIC (0, UA_NS0ID_OBJECTSFOLDER),
  96. UA_NODEID_NUMERIC (0, UA_NS0ID_ORGANIZES),
  97. folderBrowseName, UA_NODEID_NUMERIC (0,
  98. UA_NS0ID_BASEOBJECTTYPE), oAttr, NULL, &folderId);
  99. UA_Server_DataSetReader_addTargetVariables (server, &folderId,
  100. dataSetReaderId,
  101. UA_PUBSUB_SDS_TARGET);
  102. UA_free(readerConfig.dataSetMetaData.fields);
  103. }
  104. /* Define MetaData for TargetVariables */
  105. static void fillTestDataSetMetaData(UA_DataSetMetaDataType *pMetaData) {
  106. if(pMetaData == NULL) {
  107. return;
  108. }
  109. UA_DataSetMetaDataType_init (pMetaData);
  110. pMetaData->name = UA_STRING ("DataSet 1");
  111. /* Static definition of number of fields size to 4 to create four different
  112. * targetVariables of distinct datatype
  113. * Currently the publisher sends only DateTime data type */
  114. pMetaData->fieldsSize = 4;
  115. pMetaData->fields = (UA_FieldMetaData*)UA_Array_new (pMetaData->fieldsSize,
  116. &UA_TYPES[UA_TYPES_FIELDMETADATA]);
  117. /* DateTime DataType */
  118. UA_FieldMetaData_init (&pMetaData->fields[0]);
  119. UA_NodeId_copy (&UA_TYPES[UA_TYPES_DATETIME].typeId,
  120. &pMetaData->fields[0].dataType);
  121. pMetaData->fields[0].builtInType = UA_NS0ID_DATETIME;
  122. pMetaData->fields[0].name = UA_STRING ("DateTime");
  123. pMetaData->fields[0].valueRank = -1; /* scalar */
  124. /* Int32 DataType */
  125. UA_FieldMetaData_init (&pMetaData->fields[1]);
  126. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT32].typeId,
  127. &pMetaData->fields[1].dataType);
  128. pMetaData->fields[1].builtInType = UA_NS0ID_INT32;
  129. pMetaData->fields[1].name = UA_STRING ("Int32");
  130. pMetaData->fields[1].valueRank = -1; /* scalar */
  131. /* Int64 DataType */
  132. UA_FieldMetaData_init (&pMetaData->fields[2]);
  133. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT64].typeId,
  134. &pMetaData->fields[2].dataType);
  135. pMetaData->fields[2].builtInType = UA_NS0ID_INT64;
  136. pMetaData->fields[2].name = UA_STRING ("Int64");
  137. pMetaData->fields[2].valueRank = -1; /* scalar */
  138. /* Boolean DataType */
  139. UA_FieldMetaData_init (&pMetaData->fields[3]);
  140. UA_NodeId_copy (&UA_TYPES[UA_TYPES_BOOLEAN].typeId,
  141. &pMetaData->fields[3].dataType);
  142. pMetaData->fields[3].builtInType = UA_NS0ID_BOOLEAN;
  143. pMetaData->fields[3].name = UA_STRING ("BoolToggle");
  144. pMetaData->fields[3].valueRank = -1; /* scalar */
  145. }
  146. UA_Boolean running = true;
  147. static void stopHandler(int sign) {
  148. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  149. running = false;
  150. }
  151. static int
  152. run(UA_String *transportProfile, UA_NetworkAddressUrlDataType *networkAddressUrl) {
  153. signal(SIGINT, stopHandler);
  154. signal(SIGTERM, stopHandler);
  155. /* Return value initialized to Status Good */
  156. UA_StatusCode retval;
  157. UA_Server *server = UA_Server_new();
  158. UA_ServerConfig *config = UA_Server_getConfig(server);
  159. UA_ServerConfig_setMinimal(config, 4801, NULL);
  160. /* Add the PubSub network layer implementation to the server config.
  161. * The TransportLayer is acting as factory to create new connections
  162. * on runtime. Details about the PubSubTransportLayer can be found inside the
  163. * tutorial_pubsub_connection */
  164. config->pubsubTransportLayers = (UA_PubSubTransportLayer *)
  165. UA_calloc(2, sizeof(UA_PubSubTransportLayer));
  166. if(!config->pubsubTransportLayers) {
  167. UA_Server_delete(server);
  168. return EXIT_FAILURE;
  169. }
  170. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  171. config->pubsubTransportLayersSize++;
  172. #ifdef UA_ENABLE_PUBSUB_ETH_UADP
  173. config->pubsubTransportLayers[1] = UA_PubSubTransportLayerEthernet();
  174. config->pubsubTransportLayersSize++;
  175. #endif
  176. /* API calls */
  177. addPubSubConnection(server, transportProfile, networkAddressUrl);
  178. addReaderGroup(server);
  179. addDataSetReader(server);
  180. addSubscribedVariables(server, readerIdentifier);
  181. retval = UA_Server_run(server, &running);
  182. UA_Server_delete(server);
  183. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  184. }
  185. static void
  186. usage(char *progname) {
  187. printf("usage: %s <uri> [device]\n", progname);
  188. }
  189. int main(int argc, char **argv) {
  190. UA_String transportProfile = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  191. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  192. if(argc > 1) {
  193. if(strcmp(argv[1], "-h") == 0) {
  194. usage(argv[0]);
  195. return EXIT_SUCCESS;
  196. } else if(strncmp(argv[1], "opc.udp://", 10) == 0) {
  197. networkAddressUrl.url = UA_STRING(argv[1]);
  198. } else if(strncmp(argv[1], "opc.eth://", 10) == 0) {
  199. transportProfile =
  200. UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-eth-uadp");
  201. if(argc < 3) {
  202. printf("Error: UADP/ETH needs an interface name\n");
  203. return EXIT_FAILURE;
  204. }
  205. networkAddressUrl.networkInterface = UA_STRING(argv[2]);
  206. networkAddressUrl.url = UA_STRING(argv[1]);
  207. } else {
  208. printf ("Error: unknown URI\n");
  209. return EXIT_FAILURE;
  210. }
  211. }
  212. return run(&transportProfile, &networkAddressUrl);
  213. }