}
/**
+ * @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<int>(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<uint8_t>( 0x01U << shift_amount );
+ bool value = static_cast<bool>( 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<int>(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<uint8_t>( 0x01U << shift_amount );
+
+ if ( value )
+ {
+ Payload[ byte_index ] = Payload[ byte_index ] | bit_mask;
+ }
+ else
+ {
+ bit_mask = static_cast<uint8_t>( ~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.
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 );