C Program to Write/Read Dynamic Data Example

How to write a C Program to  Write/Read Dynamic Data Example in C Programming Language ?


This C Program to  Write/Read Dynamic Data Example.

Solution:

  1. #include <stdio.h>
  2. #include <ndds/ndds_c.h>
  3.  
  4. #define MAX_SEQUENCE_LEN 8
  5.  
  6. DDS_TypeCode * Example_createTypecode(
  7.         struct DDS_TypeCodeFactory *typeCodeFactory)
  8. {
  9.     struct DDS_TypeCode *typeCode = NULL;
  10.     struct DDS_TypeCode *msgStructTypeCode = NULL;
  11.     struct DDS_TypeCode *sequenceStructTypeCode = NULL;
  12.     struct DDS_TypeCode *numberListTypeCode = NULL;
  13.     struct DDS_StructMemberSeq msgStructMembers = DDS_SEQUENCE_INITIALIZER;
  14.     struct DDS_StructMemberSeq basicDataMembers = DDS_SEQUENCE_INITIALIZER;
  15.     int numberListLen = 10;
  16.     int sequenceStructLen = 8;
  17.  
  18.     DDS_TypeCode * seqElementTC = NULL;
  19.     DDS_ExceptionCode_t ex;
  20.  
  21.     /* ------------------------------------------------
  22.      *  Create msgStructTypeCode
  23.      * ------------------------------------------------*/
  24.     msgStructTypeCode = DDS_TypeCodeFactory_create_struct_tc(
  25.             typeCodeFactory,
  26.             "SNAExamples::MsgStruct",
  27.             &msgStructMembers,
  28.             &ex);
  29.     if (ex != DDS_NO_EXCEPTION_CODE) {
  30.         fprintf(stderr, "! Unable to create struct MsgStruct\n");
  31.         goto done;
  32.     }
  33.  
  34.     /* Add MsgStruct.doubleA */
  35.     DDS_TypeCode_add_member(
  36.             msgStructTypeCode,
  37.             "doubleA",
  38.             DDS_TYPECODE_MEMBER_ID_INVALID,
  39.             DDS_TypeCodeFactory_get_primitive_tc(
  40.                     typeCodeFactory,
  41.                     DDS_TK_DOUBLE),
  42.             DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
  43.             &ex);
  44.     if (ex != DDS_NO_EXCEPTION_CODE) {
  45.         fprintf(stderr, "! Unable to add member 'doubleA' to 'MsgStruct'\n");
  46.         goto done;
  47.     }
  48.  
  49.     /* Add MsgStruct.intB */
  50.     DDS_TypeCode_add_member(
  51.             msgStructTypeCode,
  52.             "intB",
  53.             DDS_TYPECODE_MEMBER_ID_INVALID,
  54.             DDS_TypeCodeFactory_get_primitive_tc(
  55.                     typeCodeFactory,
  56.                     DDS_TK_ULONG),
  57.             DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
  58.             &ex);
  59.     if (ex != DDS_NO_EXCEPTION_CODE) {
  60.         fprintf(stderr, "! Unable to add member 'intB' to 'MsgStruct'\n");
  61.         goto done;
  62.     }
  63.  
  64.     /* ------------------------------------------------
  65.      *  Create numberListTypeCode
  66.      * ------------------------------------------------*/
  67.     numberListTypeCode = DDS_TypeCodeFactory_create_sequence_tc(
  68.             typeCodeFactory,
  69.             numberListLen,
  70.             DDS_TypeCodeFactory_get_primitive_tc(
  71.                     typeCodeFactory,
  72.                     DDS_TK_ULONG),
  73.             &ex);
  74.     if (ex != DDS_NO_EXCEPTION_CODE) {
  75.         fprintf(stderr, "! Unable to create numberListTypeCode\n");
  76.         goto done;
  77.     }
  78.  
  79.     /* ------------------------------------------------
  80.      *  Create sequenceStructTypeCode
  81.      * ------------------------------------------------*/
  82.     sequenceStructTypeCode = DDS_TypeCodeFactory_create_sequence_tc(
  83.             typeCodeFactory,
  84.             sequenceStructLen,
  85.             msgStructTypeCode,
  86.             &ex);
  87.     if (ex != DDS_NO_EXCEPTION_CODE) {
  88.         fprintf(stderr, "! Unable to create sequenceStructTypeCode\n");
  89.         goto done;
  90.     }
  91.  
  92.  
  93.     /* ------------------------------------------------
  94.      *  Create DDSBasicData_msg typeCode
  95.      * ------------------------------------------------*/
  96.     typeCode = DDS_TypeCodeFactory_create_struct_tc(
  97.             typeCodeFactory,
  98.             "SNAExamples::DDSBasicData_msg",
  99.             &basicDataMembers,
  100.             &ex);
  101.     if (ex != DDS_NO_EXCEPTION_CODE) {
  102.         fprintf(stderr, "! Unable to create struct DDSBasicData_msg\n");
  103.         goto done;
  104.     }
  105.  
  106.     DDS_TypeCode_add_member(
  107.             typeCode,
  108.             "a",
  109.             DDS_TYPECODE_MEMBER_ID_INVALID,
  110.             DDS_TypeCodeFactory_get_primitive_tc(
  111.                     typeCodeFactory,
  112.                     DDS_TK_DOUBLE),
  113.             DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
  114.             &ex);
  115.     if (ex != DDS_NO_EXCEPTION_CODE) {
  116.         fprintf(stderr, "! Unable to add member 'a' to 'DDSBasicData_msg'\n");
  117.         goto done;
  118.     }
  119.  
  120.     DDS_TypeCode_add_member(
  121.             typeCode,
  122.             "b",
  123.             DDS_TYPECODE_MEMBER_ID_INVALID,
  124.             numberListTypeCode,
  125.             DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
  126.             &ex);
  127.     if (ex != DDS_NO_EXCEPTION_CODE) {
  128.         fprintf(stderr, "! Unable to add member 'b' to 'DDSBasicData_msg'\n");
  129.         goto done;
  130.     }
  131.  
  132.     DDS_TypeCode_add_member(
  133.             typeCode,
  134.             "seq",
  135.             DDS_TYPECODE_MEMBER_ID_INVALID,
  136.             sequenceStructTypeCode,
  137.             DDS_TYPECODE_NONKEY_REQUIRED_MEMBER,
  138.             &ex);
  139.     if (ex != DDS_NO_EXCEPTION_CODE) {
  140.         fprintf(stderr, "! Unable to add member 'b' to 'DDSBasicData_msg'\n");
  141.         goto done;
  142.     }
  143.  
  144. done:
  145.     if (msgStructTypeCode != NULL) {
  146.         DDS_TypeCodeFactory_delete_tc(
  147.                 typeCodeFactory,
  148.                 msgStructTypeCode,
  149.                 &ex);
  150.         msgStructTypeCode = NULL;
  151.     }
  152.  
  153.     if (numberListTypeCode != NULL) {
  154.         DDS_TypeCodeFactory_delete_tc(
  155.                 typeCodeFactory,
  156.                 numberListTypeCode,
  157.                 &ex);
  158.         numberListTypeCode = NULL;
  159.     }
  160.  
  161.     if (sequenceStructTypeCode != NULL) {
  162.         DDS_TypeCodeFactory_delete_tc(
  163.                 typeCodeFactory,
  164.                 sequenceStructTypeCode,
  165.                 &ex);
  166.         sequenceStructTypeCode = NULL;
  167.  
  168.     }
  169.  
  170.     DDS_StructMemberSeq_finalize(&msgStructMembers);
  171.     DDS_StructMemberSeq_finalize(&basicDataMembers);
  172.  
  173.     return typeCode;
  174. }
  175.  
  176. void Example_deleteTypeCode(
  177.         DDS_TypeCodeFactory *typeCodeFactory,
  178.         DDS_TypeCode *typeCode)
  179. {
  180.     DDS_ExceptionCode_t ex;
  181.  
  182.     if (typeCode != NULL) {
  183.         DDS_TypeCodeFactory_delete_tc(
  184.                 typeCodeFactory,
  185.                 typeCode,
  186.                 &ex);
  187.         typeCode = NULL;
  188.     }
  189. }
  190.  
  191. DDS_ReturnCode_t Example_takeSamples(DDS_DynamicDataReader *dynamicDataReader)
  192. {
  193.     int i = 0;
  194.     DDS_ReturnCode_t retCode = DDS_RETCODE_ERROR;
  195.     struct DDS_DynamicDataSeq dataSeq = DDS_SEQUENCE_INITIALIZER;
  196.     struct DDS_SampleInfoSeq infoSeq = DDS_SEQUENCE_INITIALIZER;
  197.  
  198.     retCode = DDS_DynamicDataReader_take(
  199.             dynamicDataReader,
  200.             &dataSeq,
  201.             &infoSeq,
  202.             DDS_ANY_SAMPLE_STATE,
  203.             DDS_ANY_SAMPLE_STATE,
  204.             DDS_LENGTH_UNLIMITED,
  205.             DDS_ANY_INSTANCE_STATE);
  206.     if (retCode == DDS_RETCODE_OK) {
  207.         for (= 0;  i < DDS_DynamicDataSeq_get_length(&dataSeq); ++i) {
  208.             struct DDS_DynamicData sample = DDS_DynamicDataSeq_get(&dataSeq, i);
  209.             printf("Sample %d\n", i);
  210.             DDS_DynamicData_print(&sample, stdout, 2);
  211.         }
  212.     } else {
  213.         goto done;
  214.     }
  215.  
  216. done:
  217.     DDS_DynamicDataReader_return_loan(dynamicDataReader, &dataSeq, &infoSeq);
  218.     DDS_DynamicDataSeq_finalize(&dataSeq);
  219.     DDS_SampleInfoSeq_finalize(&infoSeq);
  220.  
  221.     return retCode;
  222. }
  223.  
  224. DDS_ReturnCode_t Example_writeSample(
  225.         DDS_DynamicDataWriter *dynamicDataWriter,
  226.         DDS_TypeCode * typeCode)
  227. {
  228.     DDS_ReturnCode_t retCode = DDS_RETCODE_ERROR;
  229.     DDS_DynamicData *sample = NULL;
  230.  
  231.     sample = DDS_DynamicData_new(
  232.             typeCode,
  233.             &DDS_DYNAMIC_DATA_PROPERTY_DEFAULT);
  234.     if (sample == DDS_RETCODE_OK) {
  235.         fprintf(stderr, "! Unable write\n");
  236.         goto done;
  237.     }
  238.  
  239.     /* Modify sample here */
  240.  
  241.     retCode = DDS_DynamicDataWriter_write(
  242.             dynamicDataWriter,
  243.             sample,
  244.             &DDS_HANDLE_NIL);
  245.  
  246. done:
  247.     if (sample != NULL) {
  248.         DDS_DynamicData_delete(sample);
  249.         sample = NULL;
  250.     }
  251.  
  252.     return retCode;
  253. }
  254.  
  255. int main()
  256. {
  257.     DDS_DomainParticipant *participant = NULL;
  258.     DDS_DataWriter *writer = NULL;
  259.     DDS_DataReader *reader = NULL;
  260.     DDS_DynamicDataWriter *dynamicDataWriter = NULL;
  261.     DDS_DynamicDataReader *dynamicDataReader = NULL;
  262.     DDS_Topic *topic = NULL;
  263.     DDS_DynamicData * sample = NULL;
  264.     struct DDS_TypeCode *typeCode = NULL;
  265.     DDS_Publisher *publisher = NULL;
  266.     DDS_Subscriber *subscriber = NULL;
  267.     int domainId = 0;
  268.     struct DDS_DynamicDataTypeSupport *typeSupport = NULL;
  269.     struct DDS_DynamicDataTypeProperty_t props =
  270.             DDS_DynamicDataTypeProperty_t_INITIALIZER;
  271.     struct DDS_TypeCodeFactory *typeCodeFactory = NULL;
  272.     const char * typeName = "SNAExamples::DDSBasicData_msg";
  273.     const char * topicName = "Example SNAExamples_DDSBasicData_msg";
  274.     DDS_ReturnCode_t retCode = DDS_RETCODE_ERROR;
  275.     int i = 0;
  276.  
  277.     participant = DDS_DomainParticipantFactory_create_participant(
  278.             DDS_TheParticipantFactory,
  279.             domainId,
  280.             &DDS_PARTICIPANT_QOS_DEFAULT,
  281.             NULL /* listener */,
  282.             DDS_STATUS_MASK_NONE);
  283.     if (participant == NULL) {
  284.         fprintf(stderr, "! Error creating Participant\n");
  285.         goto done;
  286.     }
  287.  
  288.     publisher = DDS_DomainParticipant_get_implicit_publisher(participant);
  289.     if (publisher == NULL) {
  290.         fprintf(stderr, "! Unable to get implicit publisher\n");
  291.         goto done;
  292.     }
  293.  
  294.     subscriber = DDS_DomainParticipant_get_implicit_subscriber(participant);
  295.     if (subscriber == NULL) {
  296.         fprintf(stderr, "! Unable to get implicit subscriber\n");
  297.         goto done;
  298.     }
  299.  
  300.  
  301.     typeCodeFactory = DDS_TypeCodeFactory_get_instance();
  302.     if (typeCodeFactory == NULL) {
  303.         fprintf(stderr, "! Error getting typeCodeFactory\n");
  304.         goto done;
  305.     }
  306.  
  307.     typeCode = Example_createTypecode(typeCodeFactory);
  308.     if (typeCode == NULL) {
  309.         fprintf(stderr, "! Error getting typeCode\n");
  310.         goto done;
  311.     }
  312.  
  313.     /* Create the Dynamic Data TypeSupport object */
  314.     typeSupport = DDS_DynamicDataTypeSupport_new(typeCode, &props);
  315.     if (typeSupport == NULL) {
  316.         fprintf(stderr, "! Unable to create dynamic data type support\n");
  317.         goto done;
  318.     }
  319.  
  320.     retCode = DDS_DynamicDataTypeSupport_register_type(
  321.             typeSupport,
  322.             participant,
  323.             typeName);
  324.     if (retCode != DDS_RETCODE_OK) {
  325.         fprintf(stderr, "! Unable to register type %s\n", typeName);
  326.         goto done;
  327.     }
  328.  
  329.     topic = DDS_DomainParticipant_create_topic(
  330.             participant,
  331.             topicName,
  332.             typeName,
  333.             &DDS_TOPIC_QOS_DEFAULT,
  334.             NULL,
  335.             DDS_STATUS_MASK_NONE);
  336.     if (topic == NULL) {
  337.         fprintf(stderr, "! Unable to create topic\n");
  338.         goto done;
  339.     }
  340.  
  341.     writer = DDS_Publisher_create_datawriter(
  342.             publisher,
  343.             topic,
  344.             &DDS_DATAWRITER_QOS_DEFAULT,
  345.             NULL,
  346.             DDS_STATUS_MASK_NONE);
  347.     if (writer == NULL) {
  348.         fprintf(stderr, "! Unable to create writer\n");
  349.         goto done;
  350.     }
  351.  
  352.     dynamicDataWriter = DDS_DynamicDataWriter_narrow(writer);
  353.     if (dynamicDataWriter == NULL) {
  354.         fprintf(stderr, "! Unable to create dynamicDataWriter\n");
  355.         goto done;
  356.     }
  357.  
  358.     reader = DDS_Subscriber_create_datareader(
  359.             subscriber,
  360.             DDS_Topic_as_topicdescription(topic),
  361.             &DDS_DATAREADER_QOS_DEFAULT,
  362.             NULL,
  363.             DDS_STATUS_MASK_NONE);
  364.     if (reader == NULL) {
  365.         fprintf(stderr, "! Unable to create reader\n");
  366.         return -1;
  367.     }
  368.  
  369.     dynamicDataReader = DDS_DynamicDataReader_narrow(reader);
  370.  
  371.     /* If you want to write
  372.      * Example_writeSample(dynamicDataWriter);
  373.      */
  374.  
  375.     while (DDS_BOOLEAN_TRUE) {
  376.         Example_takeSamples(dynamicDataReader);
  377.         sleep(2);
  378.     }
  379.  
  380.     retCode = DDS_RETCODE_OK;
  381. done:
  382.     if (typeCode != NULL) {
  383.         Example_deleteTypeCode(typeCodeFactory, typeCode);
  384.         typeCode = NULL;
  385.     }
  386.  
  387.     if (participant != NULL) {
  388.         DDS_DomainParticipant_delete_contained_entities(participant);
  389.  
  390.         DDS_DomainParticipantFactory_delete_participant(
  391.                 DDS_TheParticipantFactory, participant);
  392.         participant = NULL;
  393.     }
  394.  
  395.     DDS_DomainParticipantFactory_finalize_instance();
  396.  
  397.     return retCode;
  398. }


Learn More :