diff --git a/vanetza/asn1/asn1c_wrapper.cpp b/vanetza/asn1/asn1c_wrapper.cpp index 7dfaae79a..1720ac3a2 100644 --- a/vanetza/asn1/asn1c_wrapper.cpp +++ b/vanetza/asn1/asn1c_wrapper.cpp @@ -83,6 +83,11 @@ bool validate(asn_TYPE_descriptor_t& td, const void* t, std::string& error) return ok; } +int compare(asn_TYPE_descriptor_t& td, const void* a, const void* b) +{ + return td.op->compare_struct(&td, a, b); +} + int print(FILE* stream, asn_TYPE_descriptor_t& td, const void* t) { return asn_fprint(stream, &td, t); diff --git a/vanetza/asn1/asn1c_wrapper.hpp b/vanetza/asn1/asn1c_wrapper.hpp index 6bf0ed387..7e6110789 100644 --- a/vanetza/asn1/asn1c_wrapper.hpp +++ b/vanetza/asn1/asn1c_wrapper.hpp @@ -18,6 +18,7 @@ void free(asn_TYPE_descriptor_t&, void*); void* copy(asn_TYPE_descriptor_t&, const void*); bool validate(asn_TYPE_descriptor_t&, const void*); bool validate(asn_TYPE_descriptor_t&, const void*, std::string&); +int compare(asn_TYPE_descriptor_t&, const void*, const void*); int print(FILE* stream, asn_TYPE_descriptor_t&, const void*); std::size_t size_per(asn_TYPE_descriptor_t&, const void*); std::size_t size_oer(asn_TYPE_descriptor_t&, const void*); @@ -74,6 +75,16 @@ class asn1c_wrapper_common const asn1c_type* content() const { return m_struct; } asn1c_type* content() { return m_struct; } + // compare semantics + bool operator==(const asn1c_wrapper_common& rhs) const + { + return vanetza::asn1::compare(m_type, m_struct, rhs.m_struct) == 0; + } + bool operator!=(const asn1c_wrapper_common& rhs) const + { + return vanetza::asn1::compare(m_type, m_struct, rhs.m_struct) != 0; + } + /** * Check ASN.1 constraints * \param error (optional) copy of error message @@ -94,9 +105,18 @@ class asn1c_wrapper_common return vanetza::asn1::validate(m_type, m_struct, error); } + /** + * Compare ASN.1 types + * \param other Other ASN.1 type to compare with + * \return 0 if equal, <0 if other is "greater", >0 if other is "smaller" + */ + int compare(const asn1c_wrapper_common& other) const + { + return vanetza::asn1::compare(m_type, m_struct, other.m_struct); + } + /** * Print ASN.1 type to standard output - * \param stream Output stream * \return 0 on success, -1 on error */ int print() const diff --git a/vanetza/asn1/tests/asn1c_wrapper.cpp b/vanetza/asn1/tests/asn1c_wrapper.cpp index 747ce2102..24a7c3309 100644 --- a/vanetza/asn1/tests/asn1c_wrapper.cpp +++ b/vanetza/asn1/tests/asn1c_wrapper.cpp @@ -56,6 +56,31 @@ TEST(asn1c_wrapper, validate) { EXPECT_FALSE(msg.empty()); } +TEST(asn1c_wrapper, compare) { + test_wrapper wrapper1(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper1->string, "1234"); + test_wrapper wrapper2(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper2->string, "1234"); + test_wrapper wrapper3(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper3->string, "0123"); + test_wrapper wrapper4(asn_DEF_VanetzaTest); + wrapper4->field = 5; + OCTET_STRING_fromString(&wrapper4->string, "1234"); + + // .compare() + EXPECT_EQ(wrapper1.compare(wrapper1), 0); + EXPECT_EQ(wrapper1.compare(wrapper2), 0); + EXPECT_EQ(wrapper1.compare(wrapper3), 1); + EXPECT_EQ(wrapper1.compare(wrapper4), -1); + + // operators == and != + EXPECT_TRUE(wrapper1 == wrapper1); + EXPECT_TRUE(wrapper1 == wrapper2); + EXPECT_FALSE(wrapper1 == wrapper3); + EXPECT_TRUE(wrapper1 != wrapper3); + EXPECT_TRUE(wrapper1 != wrapper4); +} + TEST(asn1c_wrapper, print) { test_wrapper wrapper(asn_DEF_VanetzaTest); OCTET_STRING_fromString(&wrapper->string, "1234");