tutorial_pubsub_subscribe.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. }
  103. /* Define MetaData for TargetVariables */
  104. static void fillTestDataSetMetaData(UA_DataSetMetaDataType *pMetaData) {
  105. if(pMetaData == NULL) {
  106. return;
  107. }
  108. UA_DataSetMetaDataType_init (pMetaData);
  109. UA_String strTmp = UA_STRING ("DataSet 1");
  110. UA_String_copy (&strTmp, &pMetaData->name);
  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. strTmp = UA_STRING ("DateTime");
  123. UA_String_copy (&strTmp, &pMetaData->fields[0].name);
  124. pMetaData->fields[0].valueRank = -1; /* scalar */
  125. /* Int32 DataType */
  126. UA_FieldMetaData_init (&pMetaData->fields[1]);
  127. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT32].typeId,
  128. &pMetaData->fields[1].dataType);
  129. pMetaData->fields[1].builtInType = UA_NS0ID_INT32;
  130. strTmp = UA_STRING ("Int32");
  131. UA_String_copy (&strTmp, &pMetaData->fields[1].name);
  132. pMetaData->fields[1].valueRank = -1; /* scalar */
  133. /* Int64 DataType */
  134. UA_FieldMetaData_init (&pMetaData->fields[2]);
  135. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT64].typeId,
  136. &pMetaData->fields[2].dataType);
  137. pMetaData->fields[2].builtInType = UA_NS0ID_INT64;
  138. strTmp = UA_STRING ("Int64");
  139. UA_String_copy (&strTmp, &pMetaData->fields[2].name);
  140. pMetaData->fields[2].valueRank = -1; /* scalar */
  141. /* Boolean DataType */
  142. UA_FieldMetaData_init (&pMetaData->fields[3]);
  143. UA_NodeId_copy (&UA_TYPES[UA_TYPES_BOOLEAN].typeId,
  144. &pMetaData->fields[3].dataType);
  145. pMetaData->fields[3].builtInType = UA_NS0ID_BOOLEAN;
  146. strTmp = UA_STRING ("BoolToggle");
  147. UA_String_copy (&strTmp, &pMetaData->fields[3].name);
  148. pMetaData->fields[3].valueRank = -1; /* scalar */
  149. }
  150. UA_Boolean running = true;
  151. static void stopHandler(int sign) {
  152. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  153. running = false;
  154. }
  155. static int
  156. run(UA_String *transportProfile, UA_NetworkAddressUrlDataType *networkAddressUrl) {
  157. signal(SIGINT, stopHandler);
  158. signal(SIGTERM, stopHandler);
  159. /* Return value initialized to Status Good */
  160. UA_StatusCode retval;
  161. UA_Server *server = UA_Server_new();
  162. UA_ServerConfig *config = UA_Server_getConfig(server);
  163. UA_ServerConfig_setMinimal(config, 4801, NULL);
  164. /* Add the PubSub network layer implementation to the server config.
  165. * The TransportLayer is acting as factory to create new connections
  166. * on runtime. Details about the PubSubTransportLayer can be found inside the
  167. * tutorial_pubsub_connection */
  168. config->pubsubTransportLayers = (UA_PubSubTransportLayer *)
  169. UA_calloc(2, sizeof(UA_PubSubTransportLayer));
  170. if(!config->pubsubTransportLayers) {
  171. UA_Server_delete(server);
  172. return EXIT_FAILURE;
  173. }
  174. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  175. config->pubsubTransportLayersSize++;
  176. #ifdef UA_ENABLE_PUBSUB_ETH_UADP
  177. config->pubsubTransportLayers[1] = UA_PubSubTransportLayerEthernet();
  178. config->pubsubTransportLayersSize++;
  179. #endif
  180. /* API calls */
  181. addPubSubConnection(server, transportProfile, networkAddressUrl);
  182. addReaderGroup(server);
  183. addDataSetReader(server);
  184. addSubscribedVariables(server, readerIdentifier);
  185. retval = UA_Server_run(server, &running);
  186. UA_Server_delete(server);
  187. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  188. }
  189. static void
  190. usage(char *progname) {
  191. printf("usage: %s <uri> [device]\n", progname);
  192. }
  193. int main(int argc, char **argv) {
  194. UA_String transportProfile = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  195. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  196. if(argc > 1) {
  197. if(strcmp(argv[1], "-h") == 0) {
  198. usage(argv[0]);
  199. return EXIT_SUCCESS;
  200. } else if(strncmp(argv[1], "opc.udp://", 10) == 0) {
  201. networkAddressUrl.url = UA_STRING(argv[1]);
  202. } else if(strncmp(argv[1], "opc.eth://", 10) == 0) {
  203. transportProfile =
  204. UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-eth-uadp");
  205. if(argc < 3) {
  206. printf("Error: UADP/ETH needs an interface name\n");
  207. return EXIT_FAILURE;
  208. }
  209. networkAddressUrl.networkInterface = UA_STRING(argv[2]);
  210. networkAddressUrl.url = UA_STRING(argv[1]);
  211. } else {
  212. printf ("Error: unknown URI\n");
  213. return EXIT_FAILURE;
  214. }
  215. }
  216. return run(&transportProfile, &networkAddressUrl);
  217. }