From 570558bb87783098942cb759ae34d502aac8e7ce Mon Sep 17 00:00:00 2001 From: v0-e Date: Fri, 26 Jan 2024 18:59:04 +0000 Subject: [PATCH] asn1: Add print() method to asn1c wrapper --- vanetza/asn1/asn1c_wrapper.cpp | 5 +++++ vanetza/asn1/asn1c_wrapper.hpp | 21 +++++++++++++++++++++ vanetza/asn1/tests/asn1c_wrapper.cpp | 6 ++++++ 3 files changed, 32 insertions(+) diff --git a/vanetza/asn1/asn1c_wrapper.cpp b/vanetza/asn1/asn1c_wrapper.cpp index 7082df6d3..7dfaae79a 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 print(FILE* stream, asn_TYPE_descriptor_t& td, const void* t) +{ + return asn_fprint(stream, &td, t); +} + std::size_t size_per(asn_TYPE_descriptor_t& td, const void* t) { asn_enc_rval_t ec; diff --git a/vanetza/asn1/asn1c_wrapper.hpp b/vanetza/asn1/asn1c_wrapper.hpp index c6ab37acb..6bf0ed387 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 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*); ByteBuffer encode_per(asn_TYPE_descriptor_t&, const void*); @@ -93,6 +94,26 @@ class asn1c_wrapper_common return vanetza::asn1::validate(m_type, m_struct, error); } + /** + * Print ASN.1 type to standard output + * \param stream Output stream + * \return 0 on success, -1 on error + */ + int print() const + { + return vanetza::asn1::print(stdout, m_type, m_struct); + } + + /** + * Print ASN.1 type to some file stream + * \param stream Output stream + * \return 0 on success, -1 on error + */ + int print(FILE* stream) const + { + return vanetza::asn1::print(stream, m_type, m_struct); + } + /** * Swap ASN.1 wrapper content * \param other wrapper diff --git a/vanetza/asn1/tests/asn1c_wrapper.cpp b/vanetza/asn1/tests/asn1c_wrapper.cpp index 8710bb71c..747ce2102 100644 --- a/vanetza/asn1/tests/asn1c_wrapper.cpp +++ b/vanetza/asn1/tests/asn1c_wrapper.cpp @@ -56,6 +56,12 @@ TEST(asn1c_wrapper, validate) { EXPECT_FALSE(msg.empty()); } +TEST(asn1c_wrapper, print) { + test_wrapper wrapper(asn_DEF_VanetzaTest); + OCTET_STRING_fromString(&wrapper->string, "1234"); + EXPECT_EQ(wrapper.print(), 0); +} + TEST(asn1c_wrapper, encode) { test_wrapper wrapper(asn_DEF_VanetzaTest); OCTET_STRING_fromString(&wrapper->string, "1234");