use crate::pack::{JkSettings, MybmmPack, ProtocolVersion};
const SIG_BYTES: [u8; 4] = [0x55, 0xAA, 0xEB, 0x90];
const JK02_FRAME_SIZE: usize = 300;
const MAX_RESPONSE_SIZE: usize = 400;
const PB2_FRAME_SIZE: usize = 168;
pub fn get_short(data: &[u8], offset: usize) -> u16 {
if offset + 2 <= data.len() {
u16::from_le_bytes([data[offset], data[offset + 1]])
} else {
0
}
}
pub fn get_signed_short(data: &[u8], offset: usize) -> i16 {
if offset + 2 <= data.len() {
i16::from_le_bytes([data[offset], data[offset + 1]])
} else {
0
}
}
pub fn get_16bit(data: &[u8], offset: usize) -> u16 {
if offset + 2 <= data.len() {
(u16::from(data[offset + 1]) << 8) | u16::from(data[offset])
} else {
0
}
}
pub fn get_32bit(data: &[u8], offset: usize) -> u32 {
if offset + 4 <= data.len() {
(u32::from(get_16bit(data, offset + 2)) << 16) | u32::from(get_16bit(data, offset))
} else {
0
}
}
pub fn ieee_float(raw: u32) -> f32 {
f32::from_bits(raw)
}
pub fn crc(data: &[u8], len: usize) -> u8 {
let mut crc: u8 = 0;
for &b in &data[..len.min(data.len())] {
crc = crc.wrapping_add(b);
}
crc
}
pub const ERROR_DESCRIPTIONS: [&str; 16] = [
"Charge Overtemperature", "Charge Undertemperature", "Coprocessor communication error", "Cell Undervoltage", "Battery pack undervoltage", "Discharge overcurrent", "Discharge short circuit", "Discharge overtemperature", "Wire resistance", "Mosfet overtemperature", "Cell count is not equal to settings", "Current sensor anomaly", "Cell Overvoltage", "Battery pack overvoltage", "Charge overcurrent protection", "Charge short circuit", ];
pub fn error_bitmask_to_strings(bitmask: u16) -> Vec<&'static str> {
let mut errors = vec![];
for (i, desc) in ERROR_DESCRIPTIONS.iter().enumerate() {
if bitmask & (1 << i) != 0 {
errors.push(*desc);
}
}
errors
}
fn parse_jk02_cell_voltages(pp: &mut MybmmPack, data: &[u8], offset: usize) {
let cells = pp.protocol_version.max_frame_cells();
let mut cells_enabled = 0usize;
let mut min_cell_voltage = 100.0f32;
let mut max_cell_voltage = -100.0f32;
let mut average_cell_voltage = 0.0f32;
let mut _min_voltage_cell = 0u8;
let mut _max_voltage_cell = 0u8;
for i in 0..cells {
let cell_voltage = get_16bit(data, i * 2 + 6) as f32 * 0.001;
let cell_resistance = get_16bit(data, i * 2 + 64 + offset) as f32 * 0.001;
if cell_voltage > 0.0 {
average_cell_voltage += cell_voltage;
cells_enabled += 1;
}
if cell_voltage > 0.0 && cell_voltage < min_cell_voltage {
min_cell_voltage = cell_voltage;
_min_voltage_cell = (i + 1) as u8;
}
if cell_voltage > max_cell_voltage {
max_cell_voltage = cell_voltage;
_max_voltage_cell = (i + 1) as u8;
}
pp.cellvolt[i] = cell_voltage;
pp.cellres[i] = cell_resistance;
}
pp.cells = cells_enabled as i32;
if cells_enabled > 0 {
pp.cell_avg = average_cell_voltage / cells_enabled as f32;
}
pp.cell_min = min_cell_voltage;
pp.cell_max = max_cell_voltage;
pp.cell_diff = max_cell_voltage - min_cell_voltage;
}
fn parse_jk02_cell_info(pp: &mut MybmmPack, data: &[u8]) {
let offset = pp.protocol_version.cell_offset();
parse_jk02_cell_voltages(pp, data, offset);
pp.enabled_cells_bitmask = get_32bit(data, 54 + offset);
let offset2 = offset * 2;
if pp.protocol_version == ProtocolVersion::Jk02_32S {
pp.power_tube_temp = get_16bit(data, 112 + offset2) as i16 as f32 * 0.1;
}
let total_voltage = get_32bit(data, 118 + offset2) as f32 * 0.001;
pp.voltage = total_voltage;
let current = get_32bit(data, 126 + offset2) as i32 as f32 * 0.001;
pp.current = current;
pp.power = total_voltage * current;
pp.charging_power = pp.power.max(0.0);
pp.discharging_power = pp.power.min(0.0).abs();
pp.temps[0] = get_16bit(data, 130 + offset2) as i16 as f32 * 0.1;
pp.temps[1] = get_16bit(data, 132 + offset2) as i16 as f32 * 0.1;
pp.ntemps = 2;
if pp.protocol_version == ProtocolVersion::Jk02_32S {
pp.error_bitmask = get_16bit(data, 134 + offset2);
} else {
pp.power_tube_temp = get_16bit(data, 134 + offset2) as i16 as f32 * 0.1;
}
if pp.protocol_version != ProtocolVersion::Jk02_32S {
pp.error_bitmask = get_16bit(data, 136 + offset2);
}
pp.balancing_current = get_16bit(data, 138 + offset2) as i16 as f32 * 0.001;
pp.balancing = data.get(140 + offset2).is_some_and(|&b| b != 0x00);
pp.soc = *data.get(141 + offset2).unwrap_or(&0) as f32;
pp.capacity_remaining = get_32bit(data, 142 + offset2) as f32 * 0.001;
pp.total_battery_capacity = get_32bit(data, 146 + offset2) as f32 * 0.001;
pp.charging_cycles = get_32bit(data, 150 + offset2);
pp.total_charging_cycle_capacity = get_32bit(data, 154 + offset2) as f32 * 0.001;
pp.soh = *data.get(158 + offset2).unwrap_or(&0) as f32;
pp.total_runtime = get_32bit(data, 162 + offset2);
pp.charging = data.get(166 + offset2).is_some_and(|&b| b != 0x00);
pp.discharging = data.get(167 + offset2).is_some_and(|&b| b != 0x00);
pp.precharging = data.get(168 + offset2).is_some_and(|&b| b != 0x00);
pp.heating = data.get(183 + offset2).is_some_and(|&b| b != 0x00);
if pp.protocol_version == ProtocolVersion::Jk02_32S {
if data.len() > 223 + offset2 {
pp.temps[2] = get_16bit(data, 226 + offset2) as i16 as f32 * 0.1;
pp.temps[3] = get_16bit(data, 224 + offset2) as i16 as f32 * 0.1;
pp.temps[4] = get_16bit(data, 222 + offset2) as i16 as f32 * 0.1;
pp.ntemps = 5;
}
}
}
fn parse_jk04_cell_info(pp: &mut MybmmPack, data: &[u8]) {
let cells = 24;
let mut cells_enabled = 0usize;
let mut min_cell_voltage = 100.0f32;
let mut max_cell_voltage = -100.0f32;
let mut average_cell_voltage = 0.0f32;
let mut total_voltage = 0.0f32;
let mut _min_voltage_cell = 0u8;
let mut _max_voltage_cell = 0u8;
for i in 0..cells {
let cell_voltage = ieee_float(get_32bit(data, i * 4 + 6));
let cell_resistance = ieee_float(get_32bit(data, i * 4 + 102));
total_voltage += cell_voltage;
if cell_voltage > 0.0 {
average_cell_voltage += cell_voltage;
cells_enabled += 1;
}
if cell_voltage > 0.0 && cell_voltage < min_cell_voltage {
min_cell_voltage = cell_voltage;
_min_voltage_cell = (i + 1) as u8;
}
if cell_voltage > max_cell_voltage {
max_cell_voltage = cell_voltage;
_max_voltage_cell = (i + 1) as u8;
}
pp.cellvolt[i] = cell_voltage;
pp.cellres[i] = cell_resistance;
}
pp.cells = cells_enabled as i32;
if cells_enabled > 0 {
pp.cell_avg = average_cell_voltage / cells_enabled as f32;
}
pp.cell_min = min_cell_voltage;
pp.cell_max = max_cell_voltage;
pp.cell_diff = max_cell_voltage - min_cell_voltage;
pp.voltage = total_voltage;
pp.balancing_current = ieee_float(get_32bit(data, 222));
pp.balancing = data.get(220).is_some_and(|&b| b != 0x00);
pp.total_runtime = get_32bit(data, 286);
}
fn parse_cell_info(pp: &mut MybmmPack, data: &[u8]) {
match pp.protocol_version {
ProtocolVersion::Jk04 => parse_jk04_cell_info(pp, data),
ProtocolVersion::Jk02_24S | ProtocolVersion::Jk02_32S => parse_jk02_cell_info(pp, data),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseFlags {
pub got_res: bool,
pub got_volts: bool,
pub got_info: bool,
}
impl Default for ParseFlags {
fn default() -> Self {
Self::new()
}
}
impl ParseFlags {
pub fn new() -> Self {
Self {
got_res: false,
got_volts: false,
got_info: false,
}
}
}
fn verify_crc(data: &[u8]) -> bool {
if data.len() < 300 {
return false;
}
let computed = crc(data, 299);
let expected = data[299];
computed == expected
}
pub fn getdata(pp: &mut MybmmPack, data: &[u8]) -> ParseFlags {
let mut flags = ParseFlags::new();
let mut j = 0;
let mut start = 0;
let mut i = 0;
while i < data.len() {
let byte = data[i];
if j < SIG_BYTES.len() && byte == SIG_BYTES[j] {
if j == 0 {
start = i;
}
j += 1;
if j >= SIG_BYTES.len() {
if i + 1 < data.len() {
let subtype = data[i + 1];
let required_size = match subtype {
2 => JK02_FRAME_SIZE,
3 => JK02_FRAME_SIZE,
1 => JK02_FRAME_SIZE,
_ => 6,
};
if start + required_size <= data.len() {
let frame_data = &data[start..];
if !verify_crc(frame_data) {
log::warn!("CRC check failed on frame at offset {}", start);
i = start + PB2_FRAME_SIZE.min(data.len() - start);
j = 0;
continue;
}
match subtype {
3 => {
if frame_data.len() > 10 {
let model_end = frame_data[6..].iter().position(|&b| b == 0).unwrap_or(0);
if model_end > 0 {
let model = String::from_utf8_lossy(&frame_data[6..6 + model_end]);
pp.protocol_version = ProtocolVersion::from_model(&model);
pp.model = model.to_string();
}
let mut pos = 6 + model_end + 1;
while pos < frame_data.len() && frame_data[pos] == 0 {
pos += 1;
}
if pos < frame_data.len() {
let end = frame_data[pos..].iter().position(|&b| b == 0).unwrap_or(0);
if end > 0 {
pp.hwvers = String::from_utf8_lossy(&frame_data[pos..pos + end]).to_string();
pos += end + 1;
}
}
while pos < frame_data.len() && frame_data[pos] == 0 {
pos += 1;
}
if pos < frame_data.len() {
let end = frame_data[pos..].iter().position(|&b| b == 0).unwrap_or(0);
if end > 0 {
pp.swvers = String::from_utf8_lossy(&frame_data[pos..pos + end]).to_string();
}
}
}
flags.got_info = true;
}
1 => {
let settings = if pp.protocol_version.is_jk04() {
parse_jk04_settings(frame_data)
} else {
parse_jk02_settings(pp.protocol_version, frame_data)
};
pp.settings = Some(settings);
flags.got_res = true;
}
2 => {
parse_cell_info(pp, frame_data);
flags.got_volts = true;
}
_ => {}
}
}
}
i = start + PB2_FRAME_SIZE.min(data.len() - start);
j = 0;
continue;
}
} else {
if byte == SIG_BYTES[0] {
j = 1;
start = i;
} else {
j = 0;
}
}
if flags.got_volts {
break;
}
i += 1;
}
flags
}
pub fn get_info_command() -> [u8; 20] {
[0xaa, 0x55, 0x90, 0xeb, 0x97, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11]
}
pub fn get_cell_info_command() -> [u8; 20] {
[0xaa, 0x55, 0x90, 0xeb, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10]
}
#[derive(Debug, Clone)]
pub struct FrameAssembler {
buffer: Vec<u8>,
}
impl FrameAssembler {
pub fn new() -> Self {
Self {
buffer: Vec::with_capacity(MAX_RESPONSE_SIZE),
}
}
pub fn feed(&mut self, data: &[u8]) {
if self.buffer.len() > MAX_RESPONSE_SIZE {
log::warn!("Frame assembler buffer overflow, clearing");
self.buffer.clear();
}
if data.len() >= 4 && data[0] == 0x55 && data[1] == 0xAA && data[2] == 0xEB && data[3] == 0x90 {
self.buffer.clear();
}
self.buffer.extend_from_slice(data);
}
pub fn try_decode(&mut self) -> Option<Vec<u8>> {
if self.buffer.len() < JK02_FRAME_SIZE {
return None;
}
if !verify_crc(&self.buffer) {
log::warn!("Frame assembler CRC check failed, clearing buffer");
self.buffer.clear();
return None;
}
let frame: Vec<u8> = self.buffer.drain(..JK02_FRAME_SIZE).collect();
Some(frame)
}
pub fn feed_and_decode(&mut self, pp: &mut MybmmPack, data: &[u8]) -> Option<ParseFlags> {
self.feed(data);
self.try_decode().map(|frame| getdata(pp, &frame))
}
pub fn clear(&mut self) {
self.buffer.clear();
}
pub fn buffer_len(&self) -> usize {
self.buffer.len()
}
}
impl Default for FrameAssembler {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct SettingDef {
pub name: &'static str,
pub unit: &'static str,
pub registers: [u8; 3],
pub factor: f32,
pub length: u8,
pub is_switch: bool,
}
pub const SETTINGS: &[SettingDef] = &[
SettingDef { name: "smart_sleep_voltage", unit: "V", registers: [0x00, 0x01, 0x01], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_uvp", unit: "V", registers: [0x00, 0x02, 0x02], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_uvpr", unit: "V", registers: [0x00, 0x03, 0x03], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_ovp", unit: "V", registers: [0x00, 0x04, 0x04], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_ovpr", unit: "V", registers: [0x00, 0x05, 0x05], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "balance_trigger_voltage", unit: "V", registers: [0x00, 0x06, 0x06], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_soc100_voltage", unit: "V", registers: [0x00, 0x07, 0x07], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_soc0_voltage", unit: "V", registers: [0x00, 0x08, 0x08], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_request_charge_voltage", unit: "V", registers: [0x00, 0x09, 0x09], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "cell_request_float_voltage", unit: "V", registers: [0x00, 0x0A, 0x0A], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "power_off_voltage", unit: "V", registers: [0x00, 0x0B, 0x0B], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "balance_starting_voltage", unit: "V", registers: [0x00, 0x26, 0x22], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "max_charge_current", unit: "A", registers: [0x00, 0x0C, 0x0C], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "max_discharge_current", unit: "A", registers: [0x00, 0x0F, 0x0F], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "max_balance_current", unit: "A", registers: [0x00, 0x13, 0x13], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "charge_ocp_delay", unit: "s", registers: [0x00, 0x0D, 0x0D], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "charge_ocp_recovery", unit: "s", registers: [0x00, 0x0E, 0x0E], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "discharge_ocp_delay", unit: "s", registers: [0x00, 0x10, 0x10], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "discharge_ocp_recovery", unit: "s", registers: [0x00, 0x11, 0x11], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "scp_recovery", unit: "s", registers: [0x00, 0x12, 0x12], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "scp_delay", unit: "", registers: [0x00, 0x25, 0x21], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "charge_otp", unit: "°C", registers: [0x00, 0x14, 0x14], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "charge_otp_recovery", unit: "°C", registers: [0x00, 0x15, 0x15], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "discharge_otp", unit: "°C", registers: [0x00, 0x16, 0x16], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "discharge_otp_recovery", unit: "°C", registers: [0x00, 0x17, 0x17], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "charge_utp", unit: "°C", registers: [0x00, 0x18, 0x18], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "charge_utp_recovery", unit: "°C", registers: [0x00, 0x19, 0x19], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "power_tube_otp", unit: "°C", registers: [0x00, 0x1A, 0x1A], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "power_tube_otp_recovery", unit: "°C", registers: [0x00, 0x1B, 0x1B], factor: 10.0, length: 4, is_switch: false },
SettingDef { name: "discharge_utp", unit: "°C", registers: [0x00, 0x00, 0x3A], factor: 1.0, length: 1, is_switch: false },
SettingDef { name: "discharge_utp_recovery", unit: "°C", registers: [0x00, 0x00, 0x3B], factor: 1.0, length: 1, is_switch: false },
SettingDef { name: "heating_start_temperature", unit: "°C", registers: [0x00, 0x00, 0x37], factor: 1.0, length: 1, is_switch: false },
SettingDef { name: "heating_stop_temperature", unit: "°C", registers: [0x00, 0x00, 0x38], factor: 1.0, length: 1, is_switch: false },
SettingDef { name: "cell_count", unit: "", registers: [0x00, 0x1C, 0x1C], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "total_battery_capacity", unit: "Ah", registers: [0x00, 0x20, 0x20], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "voltage_calibration", unit: "V", registers: [0x00, 0x21, 0x64], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "current_calibration", unit: "A", registers: [0x00, 0x24, 0x67], factor: 1000.0, length: 4, is_switch: false },
SettingDef { name: "discharge_precharge_time", unit: "s", registers: [0x00, 0x00, 0x25], factor: 1.0, length: 4, is_switch: false },
SettingDef { name: "cell_request_charge_voltage_time", unit: "h", registers: [0x00, 0x00, 0xB3], factor: 10.0, length: 1, is_switch: false },
SettingDef { name: "cell_request_float_voltage_time", unit: "h", registers: [0x00, 0x00, 0xB4], factor: 10.0, length: 1, is_switch: false },
SettingDef { name: "re_bulk_soc", unit: "%", registers: [0x00, 0x00, 0xB7], factor: 1.0, length: 1, is_switch: false },
SettingDef { name: "charging", unit: "", registers: [0x00, 0x1D, 0x1D], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "discharging", unit: "", registers: [0x00, 0x1E, 0x1E], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "balancer", unit: "", registers: [0x6C, 0x1F, 0x1F], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "emergency", unit: "", registers: [0x00, 0x00, 0x6B], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "heating", unit: "", registers: [0x00, 0x00, 0x27], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "disable_temperature_sensors", unit: "", registers: [0x00, 0x00, 0x28], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "display_always_on", unit: "", registers: [0x00, 0x00, 0x2B], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "smart_sleep", unit: "", registers: [0x00, 0x00, 0x2D], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "disable_pcl_module", unit: "", registers: [0x00, 0x00, 0x2E], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "timed_stored_data", unit: "", registers: [0x00, 0x00, 0x2F], factor: 1.0, length: 4, is_switch: true },
SettingDef { name: "charging_float_mode", unit: "", registers: [0x00, 0x00, 0x30], factor: 1.0, length: 4, is_switch: true },
];
pub fn get_setting_register(name: &str, version: ProtocolVersion) -> Option<u8> {
let idx = match version {
ProtocolVersion::Jk04 => 0,
ProtocolVersion::Jk02_24S => 1,
ProtocolVersion::Jk02_32S => 2,
};
SETTINGS.iter().find(|s| s.name == name).map(|s| s.registers[idx]).filter(|&r| r != 0)
}
pub fn get_setting_def(name: &str) -> Option<&'static SettingDef> {
SETTINGS.iter().find(|s| s.name == name)
}
pub fn get_setting_by_register(register: u8, version: ProtocolVersion) -> Option<&'static SettingDef> {
let idx = match version {
ProtocolVersion::Jk04 => 0,
ProtocolVersion::Jk02_24S => 1,
ProtocolVersion::Jk02_32S => 2,
};
SETTINGS.iter().find(|s| s.registers[idx] == register)
}
fn parse_jk02_settings(version: ProtocolVersion, data: &[u8]) -> JkSettings {
let mut s = JkSettings::new();
s.raw_frame = data[..data.len().min(300)].to_vec();
s.smart_sleep_voltage = get_32bit(data, 6) as f32 * 0.001;
s.cell_uvp = get_32bit(data, 10) as f32 * 0.001;
s.cell_uvpr = get_32bit(data, 14) as f32 * 0.001;
s.cell_ovp = get_32bit(data, 18) as f32 * 0.001;
s.cell_ovpr = get_32bit(data, 22) as f32 * 0.001;
s.balance_trigger_voltage = get_32bit(data, 26) as f32 * 0.001;
s.cell_soc100_voltage = get_32bit(data, 30) as f32 * 0.001;
s.cell_soc0_voltage = get_32bit(data, 34) as f32 * 0.001;
s.cell_request_charge_voltage = get_32bit(data, 38) as f32 * 0.001;
s.cell_request_float_voltage = get_32bit(data, 42) as f32 * 0.001;
s.power_off_voltage = get_32bit(data, 46) as f32 * 0.001;
s.max_charge_current = get_32bit(data, 50) as f32 * 0.001;
s.charge_ocp_delay = get_32bit(data, 54) as f32;
s.charge_ocp_recovery = get_32bit(data, 58) as f32;
s.max_discharge_current = get_32bit(data, 62) as f32 * 0.001;
s.discharge_ocp_delay = get_32bit(data, 66) as f32;
s.discharge_ocp_recovery = get_32bit(data, 70) as f32;
s.scp_recovery = get_32bit(data, 74) as f32;
s.max_balance_current = get_32bit(data, 78) as f32 * 0.001;
s.charge_otp = get_32bit(data, 82) as f32 * 0.1;
s.charge_otp_recovery = get_32bit(data, 86) as f32 * 0.1;
s.discharge_otp = get_32bit(data, 90) as f32 * 0.1;
s.discharge_otp_recovery = get_32bit(data, 94) as f32 * 0.1;
s.charge_utp = get_32bit(data, 98) as i32 as f32 * 0.1;
s.charge_utp_recovery = get_32bit(data, 102) as i32 as f32 * 0.1;
s.power_tube_otp = get_32bit(data, 106) as i32 as f32 * 0.1;
s.power_tube_otp_recovery = get_32bit(data, 110) as i32 as f32 * 0.1;
s.cell_count = data.get(114).copied().unwrap_or(0);
s.charging_switch = data.get(118).is_some_and(|&b| b != 0);
s.discharging_switch = data.get(122).is_some_and(|&b| b != 0);
s.balancer_switch = data.get(126).is_some_and(|&b| b != 0);
s.total_battery_capacity = get_32bit(data, 130) as f32 * 0.001;
s.scp_delay = get_32bit(data, 134) as f32;
s.balance_starting_voltage = get_32bit(data, 138) as f32 * 0.001;
if version == ProtocolVersion::Jk02_24S {
for i in 0..24 {
s.wire_resistance[i] = get_32bit(data, 158 + i * 4) as f32 * 0.001;
}
} else {
for i in 0..32 {
s.wire_resistance[i] = get_32bit(data, 142 + i * 4) as f32 * 0.001;
}
s.discharge_precharge_time = data.get(274).copied().unwrap_or(0);
let ctrl_lo = data.get(282).copied().unwrap_or(0);
let ctrl_hi = data.get(283).copied().unwrap_or(0);
s.heating_switch = (ctrl_lo & 0x01) != 0;
s.disable_temp_sensors = (ctrl_lo & 0x02) != 0;
s.display_always_on = (ctrl_lo & 0x10) != 0;
s.smart_sleep_switch = (ctrl_lo & 0x40) != 0;
s.disable_pcl_module = (ctrl_lo & 0x80) != 0;
s.timed_stored_data = (ctrl_hi & 0x01) != 0;
s.charging_float_mode = (ctrl_hi & 0x02) != 0;
s.heating_start_temperature = data.get(284).map_or(0.0, |&b| b as i8 as f32);
s.heating_stop_temperature = data.get(285).map_or(0.0, |&b| b as i8 as f32);
s.discharge_utp = data.get(296).map_or(0.0, |&b| b as i8 as f32);
s.discharge_utp_recovery = data.get(297).map_or(0.0, |&b| b as i8 as f32);
}
s
}
fn parse_jk04_settings(data: &[u8]) -> JkSettings {
let mut s = JkSettings::new();
s.raw_frame = data[..data.len().min(300)].to_vec();
s.cell_count = data.get(34).copied().unwrap_or(0);
s.power_off_voltage = ieee_float(get_32bit(data, 38));
s.balance_starting_voltage = ieee_float(get_32bit(data, 98));
s.balance_trigger_voltage = ieee_float(get_32bit(data, 106));
s.max_balance_current = ieee_float(get_32bit(data, 110));
s.balancer_switch = data.get(114).is_some_and(|&b| b != 0);
s
}
pub fn build_write_frame(register: u8, value: u32, length: u8) -> [u8; 20] {
let mut frame = [0u8; 20];
frame[0] = 0xAA;
frame[1] = 0x55;
frame[2] = 0x90;
frame[3] = 0xEB;
frame[4] = register;
frame[5] = length;
frame[6] = value as u8;
frame[7] = (value >> 8) as u8;
frame[8] = (value >> 16) as u8;
frame[9] = (value >> 24) as u8;
frame[19] = crc(&frame, 19);
frame
}
pub fn build_setting_write_frame(name: &str, value: &str, version: ProtocolVersion) -> Option<[u8; 20]> {
let def = get_setting_def(name)?;
let idx = match version {
ProtocolVersion::Jk04 => 0,
ProtocolVersion::Jk02_24S => 1,
ProtocolVersion::Jk02_32S => 2,
};
let register = def.registers[idx];
if register == 0 {
return None; }
let raw_value: u32 = if def.is_switch {
let b = value.parse::<bool>().unwrap_or_else(|_| {
match value.to_lowercase().as_str() {
"1" | "on" | "true" | "yes" => true,
"0" | "off" | "false" | "no" => false,
_ => false,
}
});
if b { 1 } else { 0 }
} else {
let v: f32 = value.parse().ok()?;
(v * def.factor) as i32 as u32
};
Some(build_write_frame(register, raw_value, def.length))
}
pub fn get_settings_command() -> [u8; 20] {
[0xaa, 0x55, 0x90, 0xeb, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10]
}
pub const CAN_FRAME_SIZE: usize = 8;
pub const CAN_CMD_INFO: u8 = 0x97;
pub const CAN_CMD_CELL_INFO: u8 = 0x96;
pub const CAN_CMD_WRITE_REG: u8 = 0x05;
pub fn build_can_command(cmd: u8, data: &[u8]) -> [u8; 8] {
let mut frame = [0u8; 8];
frame[0] = cmd;
let len = data.len().min(7);
frame[1..1 + len].copy_from_slice(&data[..len]);
frame
}
pub fn get_can_info_command() -> [u8; 8] {
build_can_command(CAN_CMD_INFO, &[])
}
pub fn get_can_cell_info_command() -> [u8; 8] {
build_can_command(CAN_CMD_CELL_INFO, &[])
}
pub fn build_can_write_frame(register: u8, value: u32, length: u8) -> [u8; 8] {
let mut frame = [0u8; 8];
frame[0] = CAN_CMD_WRITE_REG;
frame[1] = register;
frame[2] = length;
let value_bytes = value.to_le_bytes();
let copy_len = (length.min(5) as usize).min(5); frame[3..3 + copy_len].copy_from_slice(&value_bytes[..copy_len]);
frame
}
pub fn build_can_setting_write_frame(name: &str, value: &str, version: ProtocolVersion) -> Option<[u8; 8]> {
let def = get_setting_def(name)?;
let idx = match version {
ProtocolVersion::Jk04 => 0,
ProtocolVersion::Jk02_24S => 1,
ProtocolVersion::Jk02_32S => 2,
};
let register = def.registers[idx];
if register == 0 {
return None;
}
let raw_value: u32 = if def.is_switch {
let b = value.parse::<bool>().unwrap_or_else(|_| {
match value.to_lowercase().as_str() {
"1" | "on" | "true" | "yes" => true,
"0" | "off" | "false" | "no" => false,
_ => false,
}
});
if b { 1 } else { 0 }
} else {
let v: f32 = value.parse().ok()?;
(v * def.factor) as i32 as u32
};
Some(build_can_write_frame(register, raw_value, def.length))
}