tutorial_pubsub_publish.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <signal.h>
  4. #include "open62541.h"
  5. /* Work in progress: This Tutorial/Example will be continuously extended during the next PubSub batches */
  6. UA_Boolean running = true;
  7. static void stopHandler(int sign) {
  8. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  9. running = false;
  10. }
  11. static UA_StatusCode
  12. addPubSubConnection(UA_Server *server){
  13. /* Details about the connection configuration and handling are located in the pubsub connection tutorial */
  14. UA_PubSubConnectionConfig connectionConfig;
  15. memset(&connectionConfig, 0, sizeof(connectionConfig));
  16. connectionConfig.name = UA_STRING("UDP-UADP Connection 1");
  17. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  18. connectionConfig.enabled = UA_TRUE;
  19. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  20. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  21. connectionConfig.publisherId.numeric = UA_UInt32_random();
  22. UA_NodeId connectionIdentifier;
  23. UA_StatusCode retval = UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdentifier);
  24. return retval;
  25. }
  26. /**
  27. * The PubSub publish example demonstrate the simplest way to publish
  28. * informations from the information model over UDP Multicast.
  29. */
  30. int main(void) {
  31. signal(SIGINT, stopHandler);
  32. signal(SIGTERM, stopHandler);
  33. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  34. UA_ServerConfig *config = UA_ServerConfig_new_default();
  35. /* Details about the connection configuration and handling are located in the pubsub connection tutorial */
  36. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  37. if(!config->pubsubTransportLayers) {
  38. UA_ServerConfig_delete(config);
  39. return -1;
  40. }
  41. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  42. config->pubsubTransportLayersSize++;
  43. UA_Server *server = UA_Server_new(config);
  44. if(addPubSubConnection(server) != UA_STATUSCODE_GOOD)
  45. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection creation failed!");
  46. /* The PublishedDataSetConfig contains all necessary public informations for the creation of a new PublishedDataSet */
  47. UA_PublishedDataSetConfig publishedDataSetConfig;
  48. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  49. publishedDataSetConfig.name = UA_STRING("Robot Axis");
  50. /* Create new PublishedDataSet based on the PublishedDataSetConfig. */
  51. UA_NodeId publishedDataSetIdent;
  52. if(UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent).addResult == UA_STATUSCODE_GOOD)
  53. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub PublishedDataSet creation successful!");
  54. retval |= UA_Server_run(server, &running);
  55. UA_Server_delete(server);
  56. UA_ServerConfig_delete(config);
  57. return (int)retval;
  58. }