Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRC check of the settings frame #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions components/lolan_bms_ble/lolan_bms_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ void LolanBmsBle::decode_settings_data_(const std::vector<uint8_t> &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, " Computed CRC: 0x%04X", computed_crc);

// 106 2 0x5a 0xa5
}

Expand Down Expand Up @@ -544,5 +549,21 @@ std::string LolanBmsBle::bitmask_to_string_(const char *const messages[], const
return values;
}

uint16_t crc16(const uint8_t *data, uint8_t len) {
uint16_t crc = 0xFFFF;
while (len--) {
crc ^= *data++;
for (uint8_t i = 0; i < 8; i++) {
if ((crc & 0x01) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}

} // namespace lolan_bms_ble
} // namespace esphome
2 changes: 2 additions & 0 deletions components/lolan_bms_ble/lolan_bms_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class LolanBmsBle : public esphome::ble_client::BLEClientNode, public PollingCom
}
};

uint16_t crc16(const uint8_t *data, uint8_t len);

} // namespace lolan_bms_ble
} // namespace esphome

Expand Down
Loading