diff --git a/common/rdm/UIDTest.cpp b/common/rdm/UIDTest.cpp index 4be719bf83..3a114150d3 100644 --- a/common/rdm/UIDTest.cpp +++ b/common/rdm/UIDTest.cpp @@ -104,13 +104,13 @@ void UIDTest::testUID() { OLA_ASSERT_TRUE(uid.Pack(buffer, buffer_size)); uint8_t expected[] = {0, 1, 0, 0, 0, 2}; - OLA_ASSERT_EQ(0, memcmp(expected, buffer, buffer_size)); + OLA_ASSERT_DATA_EQUALS(expected, sizeof(expected), buffer, buffer_size); UID unpacked_uid1(buffer); OLA_ASSERT_EQ(uid, unpacked_uid1); OLA_ASSERT_TRUE(uid3.Pack(buffer, buffer_size)); uint8_t expected2[] = {0, 2, 0, 0, 0, 0x0a}; - OLA_ASSERT_EQ(0, memcmp(expected2, buffer, buffer_size)); + OLA_ASSERT_DATA_EQUALS(expected2, sizeof(expected2), buffer, buffer_size); UID unpacked_uid2(buffer); OLA_ASSERT_EQ(uid3, unpacked_uid2); @@ -233,6 +233,60 @@ void UIDTest::testUIDSet() { difference = set3.SetDifference(set1); OLA_ASSERT_EQ(0u, difference.Size()); + + // now test the packing & unpacking + UID uid3(3, 4); + UIDSet set4(set1); + set4.AddUID(uid3); + unsigned int buffer_size = UID::UID_SIZE * 3; + uint8_t *buffer = new uint8_t[buffer_size]; + OLA_ASSERT_TRUE(set4.Pack(buffer, buffer_size)); + + uint8_t expected[] = {0, 1, 0, 0, 0, 2, 0, 2, 0, 0, 0, 10, 0, 3, 0, 0, 0, 4}; + OLA_ASSERT_DATA_EQUALS(expected, sizeof(expected), buffer, buffer_size); + + // Creating UIDSets from binary data + uint8_t empty[] = {}; + unsigned int data_size = sizeof(empty); + UIDSet set5(empty, &data_size); + UIDSet empty_set = UIDSet(); + OLA_ASSERT_EQ(set5, empty_set); + + uint8_t raw_short[] = {0, 1}; + data_size = sizeof(raw_short); + UIDSet set6(raw_short, &data_size); + OLA_ASSERT_EQ(set6, empty_set); + + UIDSet set7; + set7.AddUID(UID(1, 2)); + uint8_t raw_one[] = {0, 1, 0, 0, 0, 2}; + data_size = sizeof(raw_one); + UIDSet set8(raw_one, &data_size); + OLA_ASSERT_EQ(6u, data_size); + OLA_ASSERT_EQ(set7, set8); + + uint8_t raw_one_extra[] = {0, 1, 0, 0, 0, 2, 3, 4}; + data_size = sizeof(raw_one_extra); + UIDSet set9(raw_one_extra, &data_size); + OLA_ASSERT_EQ(6u, data_size); + OLA_ASSERT_EQ(set7, set9); + + set7.AddUID(UID(2, 10)); + set7.AddUID(UID(3, 4)); + + uint8_t raw_three[] = + {0, 1, 0, 0, 0, 2, 0, 2, 0, 0, 0, 10, 0, 3, 0, 0, 0, 4}; + data_size = sizeof(raw_three); + UIDSet set10(raw_three, &data_size); + OLA_ASSERT_EQ(18u, data_size); + OLA_ASSERT_EQ(set7, set10); + + uint8_t raw_three_extra[] = + {0, 1, 0, 0, 0, 2, 0, 2, 0, 0, 0, 10, 0, 3, 0, 0, 0, 4, 5, 6}; + data_size = sizeof(raw_three_extra); + UIDSet set11(raw_three_extra, &data_size); + OLA_ASSERT_EQ(18u, data_size); + OLA_ASSERT_EQ(set7, set11); } diff --git a/include/ola/rdm/UIDSet.h b/include/ola/rdm/UIDSet.h index 88b43df66d..308cb2a68f 100644 --- a/include/ola/rdm/UIDSet.h +++ b/include/ola/rdm/UIDSet.h @@ -65,6 +65,21 @@ class UIDSet { m_uids(other.m_uids) { } + /** + * @brief Construct a new UIDSet from binary data. + * @param data a pointer to the memory containing the UIDSet data. The data + * should be most significant byte first. + * @param length is the length of the data you wish to retrieve + */ + explicit UIDSet(const uint8_t *data, unsigned int *length) { + unsigned int used_length = 0; + while ((*length - used_length) >= UID::LENGTH) { + m_uids.insert(UID(data + used_length)); + used_length += UID::LENGTH; + } + *length = used_length; + } + /** * @brief Assignment operator */ @@ -87,7 +102,7 @@ class UIDSet { * @return the number of UIDs in the set. */ unsigned int Size() const { - return m_uids.size(); + return static_cast(m_uids.size()); } /** @@ -207,6 +222,26 @@ class UIDSet { return out << uid_set.ToString(); } + /** + * @brief Write the binary representation of the UID set to memory. + * @param buffer a pointer to memory to write the UID set to + * @param length the size of the memory block, should be at least UID_SIZE + * * set size. + * @returns true if length was >= UID_SIZE * set size, false otherwise. + */ + bool Pack(uint8_t *buffer, unsigned int length) const { + if (static_cast(length) < (m_uids.size() * UID::UID_SIZE)) { + return false; + } + uint8_t *ptr = buffer; + std::set::const_iterator iter; + for (iter = m_uids.begin(); iter != m_uids.end(); ++iter) { + iter->Pack(ptr, UID::UID_SIZE); + ptr += UID::UID_SIZE; + } + return true; + } + private: std::set m_uids;