Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read_cdr and take_cdr #481

Merged
merged 3 commits into from Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/ddscxx/include/dds/sub/detail/SamplesHolder.hpp
Expand Up @@ -77,22 +77,31 @@ class CDRSamplesHolder : public SamplesHolder

void append_sample(void *sample, const dds_sample_info_t *si)
{
ddsrt_iovec_t blob_content;
ddscxx_serdata<org::eclipse::cyclonedds::topic::CDRBlob> *sd;
org::eclipse::cyclonedds::topic::CDRBlob emptyBlob;
dds::sub::Sample<org::eclipse::cyclonedds::topic::CDRBlob, dds::sub::detail::Sample> *buffer;

sd = static_cast<ddscxx_serdata<org::eclipse::cyclonedds::topic::CDRBlob> *>(sample);
dds::sub::Sample<org::eclipse::cyclonedds::topic::CDRBlob, dds::sub::detail::Sample> blob_ssmple(
emptyBlob, sample_info_from_c(si));
samples_.delegate()->append_sample(blob_ssmple);
ddsi_serdata_to_ser_ref(sd, 0, ddsi_serdata_size(sd), &blob_content);
buffer = samples_.delegate()->get_buffer();
org::eclipse::cyclonedds::topic::CDRBlob &sample_data = buffer[samples_.length() - 1].delegate().data();
copy_buffer_to_cdr_blob(reinterpret_cast<uint8_t *>(blob_content.iov_base),
blob_content.iov_len, sample_data.kind(), sample_data);
ddsi_serdata_to_ser_unref(sd, &blob_content);
ddsi_serdata_unref(sd);
ddsrt_iovec_t blob_content;
if (ddsi_serdata_to_ser_ref(sd, 0, ddsi_serdata_size(sd), &blob_content)) {
org::eclipse::cyclonedds::topic::CDRBlob emptyBlob;
switch (sd->kind) {
case SDK_EMPTY:
break;
case SDK_KEY:
emptyBlob.kind(org::eclipse::cyclonedds::topic::BlobKind::KeyOnly);
break;
case SDK_DATA:
emptyBlob.kind(org::eclipse::cyclonedds::topic::BlobKind::Data);
break;
}
dds::sub::Sample<org::eclipse::cyclonedds::topic::CDRBlob, dds::sub::detail::Sample> blob_sample(
emptyBlob, sample_info_from_c(si));
samples_.delegate()->append_sample(blob_sample);
dds::sub::Sample<org::eclipse::cyclonedds::topic::CDRBlob, dds::sub::detail::Sample> *buffer;
buffer = samples_.delegate()->get_buffer();
org::eclipse::cyclonedds::topic::CDRBlob &sample_data = buffer[samples_.length() - 1].delegate().data();
copy_buffer_to_cdr_blob(reinterpret_cast<uint8_t *>(blob_content.iov_base),
blob_content.iov_len, sample_data.kind(), sample_data);
ddsi_serdata_to_ser_unref(sd, &blob_content);
}
}

private:
Expand Down
35 changes: 35 additions & 0 deletions src/ddscxx/tests/DataReader.cpp
Expand Up @@ -942,6 +942,41 @@ TEST_F(DataReader, readtake2)
}


TEST_F(DataReader, readcdr)
{
#if DDSRT_ENDIAN == DDSRT_LITTLE_ENDIAN
const std::array<char, 4> encoding{0x00, 0x01, 0x00, 0x00};
const std::vector<uint8_t> payloadcdr{0x00,0x00,0x00,0x00, 0x01,0x00,0x00,0x00, 0x02,0x00,0x00,0x00};
#else
const std::array<char, 4> encoding{0x00, 0x00, 0x00, 0x00};
const std::vector<uint8_t> payloadcdr{0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, 0x00,0x00,0x00,0x02};
#endif
dds::sub::LoanedSamples<org::eclipse::cyclonedds::topic::CDRBlob> read_samples;
dds::sub::LoanedSamples<org::eclipse::cyclonedds::topic::CDRBlob> take_samples;

/* Create and write data. */
std::vector<Space::Type1> testDataList = this->WriteData(1);

/* Check result by reading. */
read_samples = this->reader->read_cdr();
dds::sub::status::DataState notReadState(dds::sub::status::SampleState::not_read(),
dds::sub::status::ViewState::new_view(),
dds::sub::status::InstanceState::alive());
ASSERT_EQ(read_samples.begin()->data().kind(), org::eclipse::cyclonedds::topic::BlobKind::Data);
ASSERT_EQ(read_samples.begin()->data().encoding(), encoding);
ASSERT_EQ(read_samples.begin()->data().payload(), payloadcdr);

/* Check result. */
take_samples = this->reader->take_cdr();
dds::sub::status::DataState readState(dds::sub::status::SampleState::read(),
dds::sub::status::ViewState::not_new_view(),
dds::sub::status::InstanceState::alive());
ASSERT_EQ(take_samples.begin()->data().kind(), org::eclipse::cyclonedds::topic::BlobKind::Data);
ASSERT_EQ(take_samples.begin()->data().encoding(), encoding);
ASSERT_EQ(take_samples.begin()->data().payload(), payloadcdr);
}


TEST_F(DataReader, lookup_instance)
{
static const uint32_t MAX_INSTANCES = 7;
Expand Down