From 1f71bf22c66936f40aaa07f872fe248dcdae94da Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 29 Jun 2024 17:24:25 +0200 Subject: [PATCH 1/2] Add CRC check of the settings frame --- components/lolan_bms_ble/lolan_bms_ble.cpp | 6 ++++ components/lolan_bms_ble/lolan_bms_ble.h | 32 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/components/lolan_bms_ble/lolan_bms_ble.cpp b/components/lolan_bms_ble/lolan_bms_ble.cpp index 2672398..5a9033a 100644 --- a/components/lolan_bms_ble/lolan_bms_ble.cpp +++ b/components/lolan_bms_ble/lolan_bms_ble.cpp @@ -402,6 +402,12 @@ void LolanBmsBle::decode_settings_data_(const std::vector &data) { ESP_LOGI(TAG, " Discharge cell voltage: %.3f V", ieee_float_(lolan_get_32bit(100))); // 104 2 0x35 0xe2 CRC? + ESP_LOGI(TAG, " CRC: 0x%02X 0x%02X (0x%04X)", data[104], data[105], lolan_get_16bit(104)); + uint16_t remote_crc = lolan_get_16bit(104); + uint16_t computed_crc = crc16(data.data(), 104); + ESP_LOGI(TAG, " Remote CRC: 0x%04X", remote_crc); + ESP_LOGI(TAG, " Computed CRC: 0x%04X", computed_crc); + // 106 2 0x5a 0xa5 } diff --git a/components/lolan_bms_ble/lolan_bms_ble.h b/components/lolan_bms_ble/lolan_bms_ble.h index 22d5860..f2fd69b 100644 --- a/components/lolan_bms_ble/lolan_bms_ble.h +++ b/components/lolan_bms_ble/lolan_bms_ble.h @@ -15,6 +15,27 @@ namespace esphome { namespace lolan_bms_ble { +static const uint16_t crcTable[256] = { + 0x0000, 0xC5A9, 0xCE9B, 0x0B32, 0xD8DF, 0x1D56, 0x1664, 0xD3CD, 0xF3E7, 0x364E, 0x3D7C, 0xF8D5, 0x2CC8, 0xE961, + 0xE253, 0x27FA, 0xAD87, 0x682E, 0x631C, 0xA6B5, 0x7558, 0xB0F1, 0xBBC3, 0x7E6A, 0x5980, 0x9C29, 0x971B, 0x52B2, + 0x815F, 0x44F6, 0x4FC4, 0x8A6D, 0x5B2E, 0x9E87, 0x95B5, 0x501C, 0x83F1, 0x4658, 0x4D6A, 0x88C3, 0xA8C9, 0x6D60, + 0x6652, 0xA3FB, 0x7016, 0xB5BF, 0xBE8D, 0x7B24, 0xF6A9, 0x3300, 0x3832, 0xFD9B, 0x2E76, 0xEBDF, 0xE0ED, 0x2544, + 0x054E, 0xC0E7, 0xCBD5, 0x0E7C, 0xDD91, 0x1838, 0x130A, 0xD6A3, 0xB65C, 0x73F5, 0x78C7, 0xBD6E, 0x6E83, 0xAB2A, + 0xA018, 0x65B1, 0x45BB, 0x8012, 0x8B20, 0x4E89, 0x9D64, 0x58CD, 0x53FF, 0x9656, 0x1BDB, 0xDE72, 0xD540, 0x10E9, + 0xC304, 0x06AD, 0x0D9F, 0xC836, 0xE83C, 0x2D95, 0x26A7, 0xE30E, 0x30E3, 0xF54A, 0xFE78, 0x3BD1, 0xED72, 0x28DB, + 0x23E9, 0xE640, 0x35AD, 0xF004, 0xFB36, 0x3E9F, 0x1E95, 0xDB3C, 0xD00E, 0x15A7, 0xC64A, 0x03E3, 0x08D1, 0xCD78, + 0x40F5, 0x855C, 0x8E6E, 0x4BC7, 0x982A, 0x5D83, 0x56B1, 0x9318, 0xB312, 0x76BB, 0x7D89, 0xB820, 0x6BCD, 0xAE64, + 0xA556, 0x60FF, 0x0F80, 0xCA29, 0xC11B, 0x04B2, 0xD75F, 0x12F6, 0x19C4, 0xDC6D, 0xFC67, 0x39CE, 0x32FC, 0xF755, + 0x24B8, 0xE111, 0xEA23, 0x2F8A, 0xA207, 0x67AE, 0x6C9C, 0xA935, 0x7AD8, 0xBF71, 0xB443, 0x71EA, 0x51E0, 0x9449, + 0x9F7B, 0x5AD2, 0x893F, 0x4C96, 0x47A4, 0x820D, 0x54AE, 0x9107, 0x9A35, 0x5F9C, 0x8C71, 0x49D8, 0x42EA, 0x8743, + 0xA749, 0x62E0, 0x69D2, 0xAC7B, 0x7F96, 0xBA3F, 0xB10D, 0x74A4, 0xF929, 0x3C80, 0x37B2, 0xF21B, 0x21F6, 0xE45F, + 0xEF6D, 0x2AC4, 0x0ACE, 0xCF67, 0xC455, 0x01FC, 0xD211, 0x17B8, 0x1C8A, 0xD923, 0xB9DC, 0x7C75, 0x7747, 0xB2EE, + 0x6103, 0xA4AA, 0xAF98, 0x6A31, 0x4A3B, 0x8F92, 0x84A0, 0x4109, 0x92E4, 0x574D, 0x5C7F, 0x99D6, 0x145B, 0xD1F2, + 0xDAC0, 0x1F69, 0xCC84, 0x092D, 0x021F, 0xC7B6, 0xE7BC, 0x2215, 0x2927, 0xEC8E, 0x3F63, 0xFACA, 0xF1F8, 0x3451, + 0xE2F2, 0x275B, 0x2C69, 0xE9C0, 0x3A2D, 0xFF84, 0xF4B6, 0x311F, 0x1115, 0xD4BC, 0xDF8E, 0x1A27, 0xC9CA, 0x0C63, + 0x0751, 0xC2F8, 0x4F75, 0x8ADC, 0x81EE, 0x4447, 0x97AA, 0x5203, 0x5931, 0x9C98, 0xBC92, 0x793B, 0x7209, 0xB7A0, + 0x644D, 0xA1E4, 0xAAD6, 0x6F7F}; + namespace espbt = esphome::esp32_ble_tracker; class LolanBmsBle : public esphome::ble_client::BLEClientNode, public PollingComponent { @@ -169,6 +190,17 @@ class LolanBmsBle : public esphome::ble_client::BLEClientNode, public PollingCom std::memcpy(&ret, &f, sizeof(float)); return ret; } + + uint16_t crc16(const uint8_t *data, size_t len) { + uint16_t crc = 16; + size_t i; + + for (i = 0; i < len; ++i) { + crc = (crc << 8) ^ crcTable[((crc >> 7) ^ data[i]) & 0xFF]; + } + + return (crc & 0xFFFF) * 2; + } }; } // namespace lolan_bms_ble From 54de4128c1b23aa735894f4d7b446201d3cb9815 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Fri, 5 Jul 2024 22:12:01 +0200 Subject: [PATCH 2/2] Simplify --- components/lolan_bms_ble/lolan_bms_ble.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/lolan_bms_ble/lolan_bms_ble.h b/components/lolan_bms_ble/lolan_bms_ble.h index f2fd69b..c4e4dfa 100644 --- a/components/lolan_bms_ble/lolan_bms_ble.h +++ b/components/lolan_bms_ble/lolan_bms_ble.h @@ -193,9 +193,8 @@ class LolanBmsBle : public esphome::ble_client::BLEClientNode, public PollingCom uint16_t crc16(const uint8_t *data, size_t len) { uint16_t crc = 16; - size_t i; - for (i = 0; i < len; ++i) { + for (size_t i = 0; i < len; ++i) { crc = (crc << 8) ^ crcTable[((crc >> 7) ^ data[i]) & 0xFF]; }