From: Guilherme Maciel Ferreira Date: Sun, 19 Feb 2012 18:19:55 +0000 (-0200) Subject: Implemented encode and decode methods for just 1 bit. X-Git-Tag: v1.3~11^2~6 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=ad545958f408012956bcac4cf466c94bec014bcc;p=pingcheck Implemented encode and decode methods for just 1 bit. - In order to hide statements like "Payload[13] | bit ? 0x08 : 0x0" and "Payload[13] & 0x04". --- diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp index ab738bc..77aa56e 100644 --- a/src/host/messagepayload.cpp +++ b/src/host/messagepayload.cpp @@ -196,6 +196,64 @@ void MessagePayload::append( } /** + * @brief Retrieve 1 bit from the payload buffer. + * + * @param byte_index The index of the byte. + * @param bit_index The index of bit within the byte. The indexes are as follow: + * [0|1|2|3|4|5|6|7], with the index zero being the left most bit within the byte. + * + * @return The bit value stored at @a bit_index. + */ +bool MessagePayload::decode1( + const int byte_index, + const int bit_index +) const +{ + const int max_bit_index = 7; + BOOST_ASSERT( ( 0 <= byte_index ) && ( byte_index < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( ( 0 <= bit_index ) && ( bit_index <= max_bit_index ) ); + + int shift_amount = max_bit_index - bit_index; + uint8_t bit_mask = static_cast( 0x01U << shift_amount ); + bool value = static_cast( Payload[ byte_index ] & bit_mask ); + + return value; +} + +/** + * @brief Store 1 bit in the payload buffer. + * + * @param byte_index The index of the byte. + * @param bit_index The index of bit within the byte. + * @param value The bit value to store at @a bit_index. + * + * @return void + */ +void MessagePayload::encode1( + const int byte_index, + const int bit_index, + const bool value +) +{ + const int max_bit_index = 7; + BOOST_ASSERT( ( 0 <= byte_index ) && ( byte_index < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( ( 0 <= bit_index ) && ( bit_index <= max_bit_index ) ); + + int shift_amount = max_bit_index - bit_index; + uint8_t bit_mask = static_cast( 0x01U << shift_amount ); + + if ( value ) + { + Payload[ byte_index ] = Payload[ byte_index ] | bit_mask; + } + else + { + bit_mask = static_cast( ~bit_mask ); + Payload[ byte_index ] = Payload[ byte_index ] & bit_mask; + } +} + +/** * @brief Retrieve 16 bits from the payload buffer. * * @param left_byte_index The index of the left byte. diff --git a/src/host/messagepayload.h b/src/host/messagepayload.h index 7425bf7..0766874 100644 --- a/src/host/messagepayload.h +++ b/src/host/messagepayload.h @@ -43,6 +43,16 @@ public: const std::size_t extra_payload_size_in_bytes ); + bool decode1( + const int byte_index, + const int bit_index + ) const; + void encode1( + const int byte_index, + const int bit_index, + const bool value + ); + uint16_t decode16( const int left_byte_index, const int right_byte_index diff --git a/test/test_messagepayload.cpp b/test/test_messagepayload.cpp index fb05edf..37d9bbf 100644 --- a/test/test_messagepayload.cpp +++ b/test/test_messagepayload.cpp @@ -175,6 +175,57 @@ BOOST_AUTO_TEST_CASE( append ) BOOST_CHECK_EQUAL( mp_original[19], 0xB9 ); } +BOOST_AUTO_TEST_CASE( decode1 ) +{ + MessagePayload mp = create_message_payload( 2 ); + mp[0] = 0xAA; + mp[1] = 0xAA; + + BOOST_CHECK_EQUAL( mp.decode1( 0, 0 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 1 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 2 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 3 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 4 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 5 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 6 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 0, 7 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 0 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 1 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 2 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 3 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 4 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 5 ), false ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 6 ), true ); + BOOST_CHECK_EQUAL( mp.decode1( 1, 7 ), false ); +} + +BOOST_AUTO_TEST_CASE( encode1 ) +{ + MessagePayload mp = create_message_payload( 2 ); + mp[0] = 0x00; + mp[1] = 0xFF; + + mp.encode1( 0, 0, true ); + mp.encode1( 0, 1, false ); + mp.encode1( 0, 2, true ); + mp.encode1( 0, 3, false ); + mp.encode1( 0, 4, true ); + mp.encode1( 0, 5, false ); + mp.encode1( 0, 6, true ); + mp.encode1( 0, 7, false ); + mp.encode1( 1, 0, true ); + mp.encode1( 1, 1, false ); + mp.encode1( 1, 2, true ); + mp.encode1( 1, 3, false ); + mp.encode1( 1, 4, true ); + mp.encode1( 1, 5, false ); + mp.encode1( 1, 6, true ); + mp.encode1( 1, 7, false ); + + BOOST_CHECK_EQUAL( mp[0], 0xAA ); + BOOST_CHECK_EQUAL( mp[1], 0xAA ); +} + BOOST_AUTO_TEST_CASE( decode16 ) { MessagePayload mp = create_message_payload( 10 );