From c3d2cfc33caceeda68505ea6e4c7b6d20751b919 Mon Sep 17 00:00:00 2001 From: kifir23917 <159958110+kifir23917@users.noreply.github.com> Date: Fri, 8 Nov 2024 21:59:17 +0200 Subject: [PATCH] Implement setBitRangeMoto method (#7056) * only:enhance tests a little bit * implement `setBitRangeMsb` method * implement `setBitRangeMoto` method --- firmware/util/efilib.cpp | 23 +++- firmware/util/efilib.h | 2 + unit_tests/tests/lua/test_bit_range_msb.cpp | 130 ++++++++++++++++++++ unit_tests/tests/lua/test_lua_dbc.cpp | 27 ++-- unit_tests/tests/tests.mk | 3 +- 5 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 unit_tests/tests/lua/test_bit_range_msb.cpp diff --git a/firmware/util/efilib.cpp b/firmware/util/efilib.cpp index 46ba29e62c..a4c4e4d2ef 100644 --- a/firmware/util/efilib.cpp +++ b/firmware/util/efilib.cpp @@ -232,6 +232,22 @@ int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth) { return getBitRangeCommon(data, bitIndex, bitWidth, -1); } +void setBitRangeMsb(uint8_t data[], const int totalBitIndex, const int bitWidth, const int value) { + int leftBitWidh = bitWidth; + const int byteIndex = totalBitIndex >> 3; + const int bitInByteIndex = totalBitIndex - byteIndex * 8; + if (bitInByteIndex + leftBitWidh > 8) { + const int bitsToHandleNow = 8 - bitInByteIndex; + setBitRangeMsb(data, totalBitIndex - bitsToHandleNow, leftBitWidh - bitsToHandleNow, value >> bitsToHandleNow); + leftBitWidh = bitsToHandleNow; + } + const int mask = (1 << leftBitWidh) - 1; + data[byteIndex] = data[byteIndex] & (~(mask << bitInByteIndex)); + const int maskedValue = value & mask; + const int shiftedValue = maskedValue << bitInByteIndex; + data[byteIndex] = data[byteIndex] | shiftedValue; +} + int motorolaMagicFromDbc(int b, int length) { // https://github.com/ebroecker/canmatrix/wiki/signal-Byteorder // convert from lsb0 bit numbering to msb0 bit numbering (or msb0 to lsb0) @@ -244,6 +260,11 @@ int motorolaMagicFromDbc(int b, int length) { } int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth) { - int b = motorolaMagicFromDbc(bitIndex, bitWidth); + const int b = motorolaMagicFromDbc(bitIndex, bitWidth); return getBitRangeMsb(data, b, bitWidth); } + +void setBitRangeMoto(uint8_t data[], const int totalBitIndex, const int bitWidth, const int value) { + const int b = motorolaMagicFromDbc(totalBitIndex, bitWidth); + return setBitRangeMsb(data, b, bitWidth, value); +} \ No newline at end of file diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index 6f0d94cb06..c2fd656e15 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -134,6 +134,8 @@ int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth); for instance DBC 8|16@0 */ int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth); +void setBitRangeMsb(uint8_t data[], int totalBitIndex, int bitWidth, int value); int motorolaMagicFromDbc(int b, int length); int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth); +void setBitRangeMoto(uint8_t data[], int totalBitIndex, int bitWidth, int value); diff --git a/unit_tests/tests/lua/test_bit_range_msb.cpp b/unit_tests/tests/lua/test_bit_range_msb.cpp new file mode 100644 index 0000000000..37632938c0 --- /dev/null +++ b/unit_tests/tests/lua/test_bit_range_msb.cpp @@ -0,0 +1,130 @@ +// +// Created by kifir on 11/8/24. +// + +#include "pch.h" + +#include "lua_lib.h" + +// inspired by TEST(LuaE65, offtopicTestGetBitRangeMsb) from test_lua_e65.cpp +TEST(BitRangeMsbTest, offtopicTestGetBitRangeMsb) { + // 1001 1111 0100 0001 0011 0010 0010 0000 0010 0011 0011 0000 1111 1111 0100 0011 + // ^------------^ + uint8_t data[] = { 0x9F, 0x41, 0x32, 0x20, 0x23, 0x30, 0xFF, 0x43 }; + + EXPECT_EQ(getBitRangeMsb(data, 12, 12), 0x9F4); // 1001 1111 0100 +} + +// inspired by TEST(LuaE65, offtopicTestGetBitRangeMsb2) from test_lua_e65.cpp +TEST(BitRangeMsbTest, offtopicTestGetBitRangeMsb2) { + // 0111 0000 0000 0100 0001 1111 + // ^-----------------^ + uint8_t data[] = { 0x70, 0x04, 0x1F}; + EXPECT_EQ(getBitRangeMsb(data, 16, 16), 0x41F); // 0111 0000 0000 0100 +} + +// inspired by TEST(LuaE65, offtopicTestSetBitRangeMsb2) from test_lua_e65.cpp +TEST(BitRangeMsbTest, offtopicTestSetBitRangeMsb2) { + // 1000 0000 0111 + // v------------v + // 0110 1000 0000 0111 + uint8_t data[] = { 0x68, 0x07 }; + setBitRangeMsb(data, 8, 13, 0x807); // 1000 0000 0111 + EXPECT_THAT(data, testing::ElementsAre(0x68, 0x07)); + + EXPECT_EQ(getBitRangeMsb(data, 8, 13), 0x807); +} + +// inspired by TEST(LuaE65, offtopicTestSetBitRangeMsb3) from test_lua_e65.cpp +TEST(BitRangeMsbTest, offtopicTestSetBitRangeMsb3) { + // 0011 0000 0100 + // v------------v + // 0110 1000 0000 0111 + uint8_t data[] = { 0x68, 0x07 }; + setBitRangeMsb(data, 8, 13, 0x304); // 0011 0000 0100 + EXPECT_THAT(data, testing::ElementsAre(0x63, 0x04)); + + EXPECT_EQ(getBitRangeMsb(data, 8, 13), 0x304); +} + +// inspired by TEST(LuaE65, offtopicTestGetBitRangeMsb2) from test_lua_e65.cpp +TEST(BitRangeMsbTest, getBitRangeMsbTest) { + // 1001 1111 0000 0001 0011 0010 0010 0000 0010 0011 0110 0111 0100 0000 0000 0000 + // ^------------^ + uint8_t data[] = { 0x9F, 0x01, 0x32, 0x20, 0x23, 0x67, 0x40, 0x00 }; + + EXPECT_EQ(getBitRangeMsb(data, 6 * 8, 13), 0x740); // 0111 0100 0000 +} + +// inspired by TEST(LuaE65, setBitRangeMsbTest) from test_lua_e65.cpp +TEST(BitRangeMsbTest, setBitRangeMsbTest) { + // v--------------v + // 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 + uint8_t data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + setBitRangeMsb(data, 6 * 8, 13, 0x740); // 0111 0100 0000 + EXPECT_THAT( + data, + // 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 0100 0000 0000 0000 + testing::ElementsAre(0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x40, 0x00) + ); + + EXPECT_EQ(getBitRangeMsb(data, 6 * 8, 13), 0x740); +} + +TEST(BitRangeMsbTest, getBitRangeMsbTest1) { + // 0000 0000 1111 1111 0000 0000 + uint8_t data[] = {0x00, 0xFF, 0x00}; + + // 0000 0000 1111 1111 0000 0000 + // ^-------^ + EXPECT_EQ(getBitRangeMsb(data, 8, 8), 0xFF); // 1111 1111 + + // 0000 0000 1111 1111 0000 0000 + // ^--------^ + EXPECT_EQ(getBitRangeMsb(data, 9, 8), 0x7F); // 0111 1111 + + // 0000 0000 1111 1111 0000 0000 + // ^--------^ + EXPECT_EQ(getBitRangeMsb(data, 23, 8), 0xFE); // 1111 1110 + + // 0000 0000 1111 1111 0000 0000 + // ^---------^ + EXPECT_EQ(getBitRangeMsb(data, 23, 9), 0x1FE); // 0001 1111 1110 + + // 0000 0000 1111 1111 0000 0000 + // ^---------^ + EXPECT_EQ(getBitRangeMsb(data, 8, 9), 0x0FF); // 0 1111 1111 + + // 0000 0000 1111 1111 0000 0000 + // ^-----------^ + EXPECT_EQ(getBitRangeMsb(data, 23, 10), 0x1FE); // 0001 1111 1110 +} + +TEST(BitRangeMsbTest, getBitRangeMsbTest2) { + // 1111 1111 0111 1110 1111 1111 + uint8_t data[] = {0xFF, 0x7E, 0xFF}; + + // 1111 1111 0111 1110 1111 1111 + // ^-------^ + EXPECT_EQ(getBitRangeMsb(data, 8, 8), 0x7E); // 0111 1110 + + // 1111 1111 0111 1110 1111 1111 + // ^--------^ + EXPECT_EQ(getBitRangeMsb(data, 9, 8), 0xBF); // 1011 1111 + + // 1111 1111 0111 1110 1111 1111 + // ^--------^ + EXPECT_EQ(getBitRangeMsb(data, 23, 8), 0xFD); // 1111 1101 + + // 1111 1111 0111 1110 1111 1111 + // ^---------^ + EXPECT_EQ(getBitRangeMsb(data, 23, 9), 0x0FD); // 0000 1111 1101 + + // 1111 1111 0111 1110 1111 1111 + // ^---------^ + EXPECT_EQ(getBitRangeMsb(data, 8, 9), 0x17E); // 0001 0111 1110 + + // 1111 1111 0111 1110 1111 1111 + // ^-----------^ + EXPECT_NE(getBitRangeMsb(data, 23, 10), 0x2FD); // 0010 1111 1101 +} diff --git a/unit_tests/tests/lua/test_lua_dbc.cpp b/unit_tests/tests/lua/test_lua_dbc.cpp index 44dde5d2af..c3fd4f89d7 100644 --- a/unit_tests/tests/lua/test_lua_dbc.cpp +++ b/unit_tests/tests/lua/test_lua_dbc.cpp @@ -2,13 +2,19 @@ #include "rusefi_lua.h" #include "lua_lib.h" +// `motorolaMagicFromDbc` function calculates data payload LST (Least Significant Bit) index from the corresponding +// `Bit start | length` values in DBC file. The following test checks that for some sample DBC file +// `motorolaMagicFromDbc` function returns values that match pictures we see if we open this DBC file with `Kvazer +// Database Editor` program. TEST(dbcMotorola, testFromDbc) { ASSERT_EQ(24, motorolaMagicFromDbc(27, 4)); ASSERT_EQ(24, motorolaMagicFromDbc(30, 7)); ASSERT_EQ(24, motorolaMagicFromDbc(31, 8)); ASSERT_EQ(24, motorolaMagicFromDbc(17, 10)); - - ASSERT_EQ(24, motorolaMagicFromDbc(17, 10)); + ASSERT_EQ(24, motorolaMagicFromDbc(16, 9)); + ASSERT_EQ(22, motorolaMagicFromDbc(13, 8)); + ASSERT_EQ(22, motorolaMagicFromDbc(14, 9)); + ASSERT_EQ(23, motorolaMagicFromDbc(14, 8)); } TEST(dbcMotorola, testFromIntelIntoMotorolaDbc) { @@ -16,13 +22,18 @@ TEST(dbcMotorola, testFromIntelIntoMotorolaDbc) { } TEST(dbcMotorola, accessRange) { - uint8_t data[] = { 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00}; + uint8_t data[] = { 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00}; - EXPECT_NEAR_M3(getBitRangeMoto(data, 17, 10), 0x03FF); + EXPECT_EQ(getBitRangeMoto(data, 17, 10), 0x03FF); - EXPECT_NEAR_M3(getBitRangeLsb(data, 16, 2), 3); - EXPECT_NEAR_M3(getBitRangeLsb(data, 24, 8), 0xFF); + EXPECT_EQ(getBitRangeLsb(data, 16, 2), 3); + EXPECT_EQ(getBitRangeLsb(data, 24, 8), 0xFF); - // todo: setBitRangeMoto(data, 17, 10, 0x03FF); - // todo: assert data is empty now? + setBitRangeMoto(data, 17, 10, 0x0234); + EXPECT_THAT(data, testing::ElementsAre(0x00, 0x00, 0x02, 0x34, 0x00, 0x00, 0x00, 0x00)); + + EXPECT_EQ(getBitRangeMoto(data, 17, 10), 0x0234); + + EXPECT_EQ(getBitRangeLsb(data, 16, 2), 2); + EXPECT_EQ(getBitRangeLsb(data, 24, 8), 0x34); } diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index 9107bf87b2..de26c15ae7 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -71,7 +71,8 @@ TESTS_SRC_CPP = \ tests/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp \ tests/test_fft.cpp \ tests/lua/test_lua_basic.cpp \ - tests/lua/test_lua_dbc.cpp \ + tests/lua/test_bit_range_msb.cpp \ + tests/lua/test_lua_dbc.cpp \ tests/lua/test_lookup.cpp \ tests/lua/test_lua_e38.cpp \ tests/lua/test_lua_canam.cpp \