From 4f6e02316f9f14161a7e851f5b2018ed753ca54f Mon Sep 17 00:00:00 2001 From: Remi Bettan Date: Wed, 19 Jun 2024 13:25:34 +0300 Subject: [PATCH] md parser for always enabled param added --- src/metadata-parser.h | 100 +++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 31 deletions(-) diff --git a/src/metadata-parser.h b/src/metadata-parser.h index 42a24d881e..f21c372b6a 100644 --- a/src/metadata-parser.h +++ b/src/metadata-parser.h @@ -100,70 +100,108 @@ namespace librealsense * to the inner struct, we pre-calculate and store the attribute offset internally * http://stackoverflow.com/questions/1929887/is-pointer-to-inner-struct-member-forbidden */ - template - class md_attribute_parser : public md_attribute_parser_base + + /**\brief always enabled param parser class*/ + template + class md_always_enabled_param_parser : public md_attribute_parser_base { public: - md_attribute_parser( Attribute S::*attribute_name, Flag flag, unsigned long long offset, attrib_modifyer mod ) - : _md_attribute( attribute_name ) - , _md_flag( flag ) - , _offset( offset ) - , _modifyer( mod ) - { - } + md_always_enabled_param_parser(Attribute S::* attribute_name, unsigned long long offset, attrib_modifyer mod) + : _md_attribute(attribute_name) + , _offset(offset) + , _modifyer(mod) {}; - bool find( const frame & frm, rs2_metadata_type * p_value ) const override + bool find(const frame& frm, rs2_metadata_type* p_value) const override { - auto s = reinterpret_cast< const S * >( ( (const uint8_t *)frm.additional_data.metadata_blob.data() ) - + _offset ); + auto s = reinterpret_cast(((const uint8_t*)frm.additional_data.metadata_blob.data()) + + _offset); - if( ! is_attribute_valid( s ) ) + if (!is_attribute_valid(s)) return false; - if( p_value ) + if (p_value) { auto attrib = static_cast((*s).*_md_attribute); - if( _modifyer ) - attrib = _modifyer( attrib ); + if (_modifyer) + attrib = _modifyer(attrib); *p_value = attrib; } return true; } protected: - bool is_attribute_valid( const S * s ) const + virtual bool is_attribute_valid(const S* s) const { // verify that the struct is of the correct type // Check that the header id and the struct size corresponds. // Note that this heurisic is not deterministic and may validate false frames! TODO - requires review md_type expected_type = md_type_trait< S >::type; - if( ( s->header.md_type_id != expected_type ) || ( s->header.md_size < sizeof( *s ) ) ) + if (s->header.md_type_id != expected_type) { std::string type - = ( md_type_desc.count( s->header.md_type_id ) > 0 ) - ? md_type_desc.at( s->header.md_type_id ) - : ( rsutils::string::from() - << "0x" << std::hex << static_cast< uint32_t >( s->header.md_type_id ) << std::dec ); - LOG_DEBUG( "Metadata mismatch - actual: " << type << ", expected: 0x" << std::hex - << (uint32_t)expected_type << std::dec << " (" - << md_type_desc.at( expected_type ) << ")" ); + = (md_type_desc.count(s->header.md_type_id) > 0) + ? md_type_desc.at(s->header.md_type_id) + : (rsutils::string::from() + << "0x" << std::hex << static_cast(s->header.md_type_id) << std::dec); + LOG_DEBUG("Metadata type mismatch - actual: " << type << ", expected: 0x" << std::hex + << (uint32_t)expected_type << std::dec << " (" + << md_type_desc.at(expected_type) << ")"); + return false; + } + + if (s->header.md_size < sizeof(*s)) + { + LOG_DEBUG("Metadata size mismatch - actual: " << (uint32_t)s->header.md_size << ", expected: " << sizeof(*s) << std::dec << " (" + << md_type_desc.at(expected_type) << ")"); return false; } + return true; + } + + private: + md_always_enabled_param_parser() = delete; + md_always_enabled_param_parser(const md_always_enabled_param_parser&) = delete; + + Attribute S::* _md_attribute; // Pointer to the attribute within uvc header that provides the relevant data + unsigned long long _offset; // Inner struct offset with regard to the most outer one + attrib_modifyer _modifyer; // Post-processing on received attribute + }; + + /**\brief A utility function to create UVC metadata header parser*/ + template + std::shared_ptr make_always_enabled_param_parser(Attribute S::* attribute, unsigned long long offset, attrib_modifyer mod = nullptr) + { + std::shared_ptr> parser(new md_always_enabled_param_parser(attribute, offset, mod)); + return parser; + } + + template + class md_attribute_parser : public md_always_enabled_param_parser + { + public: + md_attribute_parser( Attribute S::*attribute_name, Flag flag, unsigned long long offset, attrib_modifyer mod ) + : md_always_enabled_param_parser(attribute_name, offset, mod) + , _md_flag( flag ) + { + } + + protected: + virtual bool is_attribute_valid( const S * s ) const + { + if (!md_always_enabled_param_parser::is_attribute_valid(s)) + return false; // Check if the attribute's flag is set - auto attribute_enabled = ( 0 != ( s->flags & static_cast< uint32_t >( _md_flag ) ) ); + auto attribute_enabled = (0 != (s->flags & static_cast(_md_flag))); return attribute_enabled; } + Flag _md_flag; // Bit that indicates whether the particular attribute is active + private: md_attribute_parser() = delete; md_attribute_parser(const md_attribute_parser&) = delete; - - Attribute S::* _md_attribute; // Pointer to the attribute within struct that holds the relevant data - Flag _md_flag; // Bit that indicates whether the particular attribute is active - unsigned long long _offset; // Inner struct offset with regard to the most outer one - attrib_modifyer _modifyer; // Post-processing on received attribute }; /**\brief A helper function to create a specialized attribute parser.