jk_bms 0.4.0

Async library and CLI (jktool) for JIKONG (JK) Battery Management Systems over Serial, Bluetooth LE and CAN
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! High-level device API: [`JkBms`].
//!
//! Mirrors the `scan()` → `connect()` → `read()` shape of sibling device
//! crates: one struct owns the session and pack, and every transport
//! (BLE / serial / CAN) drives the same assembler-based read path that the
//! `jktool` CLI uses.
//!
//! ```no_run
//! # async fn demo() -> jk_bms::Result<()> {
//! use jk_bms::JkBms;
//!
//! for dev in JkBms::scan(5).await? {
//!     println!("{} [{}]", dev.name.as_deref().unwrap_or("?"), dev.id);
//! }
//!
//! let mut bms = JkBms::connect_ble("aa:bb:cc:dd:ee:ff").await?;
//! let pack = bms.read().await?;
//! println!("SOC {:.0}%  {:.2} V", pack.soc, pack.voltage);
//! bms.disconnect().await?;
//! # Ok(()) }
//! ```

use crate::error::{JkError, Result};
use crate::module::{
    MybmmModule, Transport, MYBMM_BALANCE_CONTROL, MYBMM_CHARGE_CONTROL, MYBMM_DISCHARGE_CONTROL,
};
use crate::pack::{JkSettings, MybmmPack};
use crate::protocol::{
    build_can_setting_write_frame, build_setting_write_frame, get_can_cell_info_command,
    get_can_info_command, get_cell_info_command, get_info_command, FrameAssembler,
};
use crate::session::JkSession;

/// Read live data from the BMS into `pack`.
///
/// This is the one true read path (used by [`JkBms`], [`crate::jk_read`] and
/// the `jktool` CLI): commands are transport-aware (CAN vs serial/BLE) and
/// every read is fed through a [`FrameAssembler`], so frames fragmented
/// across BLE notifications or interleaved with heartbeats still decode.
///
/// Phase 1 requests device info (model, protocol version; non-fatal if
/// missed), phase 2 requests cell info and errors with
/// [`JkError::NoVoltageData`] if no voltage frame arrives. When
/// `need_settings` is true, phase 2 also waits for a settings frame.
pub async fn read_data(
    session: &mut JkSession,
    pack: &mut MybmmPack,
    need_settings: bool,
) -> Result<()> {
    const RETRIES: u32 = 5;
    const READ_WINDOW: std::time::Duration = std::time::Duration::from_secs(3);
    const POLL: std::time::Duration = std::time::Duration::from_millis(50);

    let is_can = pack.transport == "can";
    let mut data = vec![0u8; 2048];
    let mut asm = FrameAssembler::new();

    // Phase 1: getInfo (model, protocol version). Non-fatal if missed; the
    // pack keeps whatever was learned on a previous read.
    let info_cmd: Vec<u8> = if is_can {
        get_can_info_command().to_vec()
    } else {
        get_info_command().to_vec()
    };
    let mut got_info = false;
    'info: for _ in 0..RETRIES {
        let handle = session
            .tp_handle
            .as_mut()
            .ok_or(JkError::TransportNotInitialized)?;
        handle.write(&info_cmd).await?;
        let start = std::time::Instant::now();
        while start.elapsed() < READ_WINDOW {
            let bytes = handle.read(&mut data).await?;
            log::debug!("read {} bytes for getInfo", bytes);
            if bytes > 0 {
                if let Some(flags) = asm.feed_and_decode(pack, &data[..bytes]) {
                    if flags.got_info {
                        got_info = true;
                        break 'info;
                    }
                }
            }
            tokio::time::sleep(POLL).await;
        }
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    }
    if !got_info {
        log::warn!("no device info frame received; using last known protocol version");
    }

    // Phase 2: getCellInfo (voltages, current, SOC, and optionally settings).
    let cell_cmd: Vec<u8> = if is_can {
        get_can_cell_info_command().to_vec()
    } else {
        get_cell_info_command().to_vec()
    };
    let mut got_volts = false;
    let mut got_settings = pack.settings.is_some();
    // Once live data is flowing, don't wait forever for a settings frame —
    // some firmware (e.g. JK-PB fw15.x) never sends one.
    let mut settings_patience: u32 = 2;
    asm.clear();
    'cell: for _ in 0..RETRIES {
        let handle = session
            .tp_handle
            .as_mut()
            .ok_or(JkError::TransportNotInitialized)?;
        handle.write(&cell_cmd).await?;
        let start = std::time::Instant::now();
        while start.elapsed() < READ_WINDOW {
            let bytes = handle.read(&mut data).await?;
            log::debug!("read {} bytes for getCellInfo", bytes);
            if bytes > 0 {
                if let Some(flags) = asm.feed_and_decode(pack, &data[..bytes]) {
                    got_volts |= flags.got_volts;
                    got_settings |= flags.got_res;
                    if got_volts && (!need_settings || got_settings) {
                        break 'cell;
                    }
                }
            }
            tokio::time::sleep(POLL).await;
        }
        if got_volts && need_settings && !got_settings {
            settings_patience -= 1;
            if settings_patience == 0 {
                log::warn!("no settings frame received; continuing with live data only");
                break 'cell;
            }
        }
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    }

    if !got_volts {
        return Err(JkError::NoVoltageData);
    }
    Ok(())
}

/// A bank of RS485-linked JK-PB packs discovered over BLE, presented as one
/// logical battery. Only the [`master`](Self::master) answers BLE; slaves are
/// read through it.
#[derive(Debug, Clone)]
pub struct JkBank {
    /// The master pack (address 00) — connect to this.
    pub master: crate::BtDevice,
    /// Slave packs (address 01+) on the same bank, ordered by address.
    pub slaves: Vec<crate::BtDevice>,
}

impl JkBank {
    /// Number of packs in the bank (master + slaves).
    pub fn pack_count(&self) -> usize {
        1 + self.slaves.len()
    }
}

/// Group discovered BLE devices into banks.
///
/// Slaves (`-01`+) are attached to a master (`-00`) **only when exactly one
/// master is present** — with multiple banks in range the `-NN` suffix alone
/// can't say which slave belongs to which master, so each device is returned
/// as its own single-pack bank. Masters (and address-less standalone units)
/// always become banks; orphan slaves (no master in range) are surfaced as
/// their own bank so they remain visible.
pub fn group_banks(devices: Vec<crate::BtDevice>) -> Vec<JkBank> {
    let (masters, slaves): (Vec<_>, Vec<_>) = devices.into_iter().partition(|d| d.is_master());

    if masters.len() == 1 {
        let mut slaves = slaves;
        slaves.sort_by_key(|d| d.address);
        return vec![JkBank { master: masters.into_iter().next().unwrap(), slaves }];
    }

    // Ambiguous (or no) master: everything is its own single-pack bank.
    masters
        .into_iter()
        .chain(slaves)
        .map(|master| JkBank { master, slaves: Vec::new() })
        .collect()
}

/// A connected JK BMS.
pub struct JkBms {
    session: JkSession,
    pack: MybmmPack,
    /// Assembler for the push stream ([`Self::next_update`]); persistent so
    /// partial frames survive across calls.
    stream_asm: FrameAssembler,
    /// Whether the cell-info request that starts the push stream was sent.
    streaming: bool,
}

impl JkBms {
    /// Scan BLE for JK BMS devices for `secs` seconds.
    ///
    /// Unlike [`crate::bt_scan`], this returns only peripherals that look
    /// like JK BMS units: they advertise the JK serial service (`0xFFE0`) or
    /// carry the default `JK` name prefix. Names are user-customisable via
    /// the app, so the service UUID is the reliable signal.
    #[cfg(feature = "bluetooth")]
    pub async fn scan(secs: u64) -> Result<Vec<crate::transport::BtDevice>> {
        use btleplug::api::{Central, Manager as _, Peripheral as _, ScanFilter};
        use btleplug::platform::Manager;

        /// 0000ffe0-0000-1000-8000-00805f9b34fb — the UART-style service JK
        /// BMS units advertise.
        const JK_SERVICE: uuid::Uuid =
            uuid::Uuid::from_u128(0x0000ffe0_0000_1000_8000_00805f9b34fb);

        let err = |e: btleplug::Error| JkError::TransportError(e.to_string());
        let manager = Manager::new().await.map_err(err)?;
        let adapter = manager
            .adapters()
            .await
            .map_err(err)?
            .into_iter()
            .next()
            .ok_or_else(|| JkError::TransportError("no bluetooth adapter found".to_string()))?;

        adapter.start_scan(ScanFilter::default()).await.map_err(err)?;
        tokio::time::sleep(std::time::Duration::from_secs(secs)).await;
        let _ = adapter.stop_scan().await;

        let mut out = Vec::new();
        for p in adapter.peripherals().await.map_err(err)? {
            let Ok(Some(props)) = p.properties().await else {
                continue;
            };
            let name = props.local_name.unwrap_or_default();
            let is_jk = props.services.contains(&JK_SERVICE)
                || name.to_ascii_uppercase().starts_with("JK");
            if !is_jk {
                continue;
            }
            out.push(crate::transport::BtDevice {
                address: crate::transport::parse_pack_address(&name),
                name: (!name.is_empty()).then_some(name),
                id: p.id().to_string(),
                rssi: props.rssi,
            });
        }
        Ok(out)
    }

    /// Connect over BLE. `target` is the peripheral id from [`Self::scan`]
    /// (or a MAC address on platforms that expose it), optionally followed by
    /// `,<characteristic>` to override the default `ffe1`.
    #[cfg(feature = "bluetooth")]
    pub async fn connect_ble(target: &str) -> Result<Self> {
        let t = crate::transport::BluetoothTransport::from_target(target);
        Self::open("bt", target, Box::new(t)).await
    }

    /// Connect over serial. `target` is `<path>[,<baud>]`, e.g.
    /// `/dev/ttyUSB0,115200`.
    #[cfg(feature = "serial")]
    pub async fn connect_serial(target: &str) -> Result<Self> {
        let t = crate::transport::SerialTransport::from_target(target);
        Self::open("serial", target, Box::new(t)).await
    }

    /// Connect over CAN. `target` is `<iface>[,<rx-id>,<tx-id>]`, e.g.
    /// `can0,0x18ff0000,0x18fe0000`.
    #[cfg(all(feature = "can", target_os = "linux"))]
    pub async fn connect_can(target: &str) -> Result<Self> {
        let t = crate::transport::CanTransport::from_target(target)?;
        Self::open("can", target, Box::new(t)).await
    }

    /// Connect over a caller-supplied [`Transport`] (e.g. a mock in tests).
    /// `transport_name` selects the command dialect: `"can"` for CAN,
    /// anything else for the standard serial/BLE frames.
    pub async fn open(
        transport_name: &str,
        target: &str,
        transport: Box<dyn Transport>,
    ) -> Result<Self> {
        let mut pack = MybmmPack::new("jk");
        pack.transport = transport_name.to_string();
        pack.target = target.to_string();
        let module = MybmmModule::new(
            "jk",
            MYBMM_CHARGE_CONTROL | MYBMM_DISCHARGE_CONTROL | MYBMM_BALANCE_CONTROL,
        );
        let mut session = JkSession {
            pp: pack.clone(),
            tp: module,
            tp_handle: Some(transport),
        };
        session.open().await?;
        Ok(Self {
            session,
            pack,
            stream_asm: FrameAssembler::new(),
            streaming: false,
        })
    }

    /// Wait for the next pushed update and return the refreshed pack state.
    ///
    /// A JK BMS keeps broadcasting cell-info frames (roughly every second
    /// over BLE) after a single `getCellInfo` request — no polling needed.
    /// The first call sends that request; subsequent calls just decode the
    /// push stream. Errors with [`JkError::Timeout`] if no complete frame
    /// arrives within `timeout`.
    ///
    /// ```no_run
    /// # async fn demo(bms: &mut jk_bms::JkBms) -> jk_bms::Result<()> {
    /// loop {
    ///     let pack = bms.next_update(std::time::Duration::from_secs(10)).await?;
    ///     println!("SOC {:.0}%  {:.3} V", pack.soc, pack.voltage);
    /// }
    /// # }
    /// ```
    pub async fn next_update(&mut self, timeout: std::time::Duration) -> Result<&MybmmPack> {
        let is_can = self.pack.transport == "can";
        let handle = self
            .session
            .tp_handle
            .as_mut()
            .ok_or(JkError::TransportNotInitialized)?;

        if !self.streaming {
            let cell_cmd: Vec<u8> = if is_can {
                get_can_cell_info_command().to_vec()
            } else {
                get_cell_info_command().to_vec()
            };
            handle.write(&cell_cmd).await?;
            self.streaming = true;
        }

        let deadline = std::time::Instant::now() + timeout;
        let mut buf = vec![0u8; 2048];
        loop {
            let remaining = deadline.saturating_duration_since(std::time::Instant::now());
            if remaining.is_zero() {
                return Err(JkError::Timeout);
            }
            let bytes = match tokio::time::timeout(remaining, handle.read(&mut buf)).await {
                Ok(res) => res?,
                Err(_) => return Err(JkError::Timeout),
            };
            if bytes == 0 {
                continue;
            }
            if let Some(flags) = self.stream_asm.feed_and_decode(&mut self.pack, &buf[..bytes]) {
                if flags.got_volts {
                    return Ok(&self.pack);
                }
            }
        }
    }

    /// Read live data (cell voltages, current, SOC, temperatures, ...) and
    /// return the full decoded pack state.
    pub async fn read(&mut self) -> Result<&MybmmPack> {
        read_data(&mut self.session, &mut self.pack, false).await?;
        Ok(&self.pack)
    }

    /// Read live data *and* wait for the settings frame.
    /// Errors if the BMS never sends one.
    pub async fn read_settings(&mut self) -> Result<JkSettings> {
        read_data(&mut self.session, &mut self.pack, true).await?;
        self.pack
            .settings
            .clone()
            .ok_or_else(|| JkError::TransportError("no settings frame received".to_string()))
    }

    /// Write a named setting (see [`crate::SETTINGS`] /
    /// `jktool list-settings`), e.g. `set("charge", "off")`.
    ///
    /// Reads first if the device has never been read, since write frames are
    /// protocol-specific and the protocol version comes from the info frame.
    pub async fn set(&mut self, name: &str, value: &str) -> Result<()> {
        if self.pack.model.is_empty() {
            let _ = self.read().await;
        }
        let frame: Vec<u8> = if self.pack.transport == "can" {
            build_can_setting_write_frame(name, value, self.pack.protocol_version)
                .map(|f| f.to_vec())
        } else {
            build_setting_write_frame(name, value, self.pack.protocol_version)
                .map(|f| f.to_vec())
        }
        .ok_or_else(|| {
            JkError::TransportError(format!(
                "unknown or unsupported setting '{}' for protocol {:?}",
                name, self.pack.protocol_version
            ))
        })?;

        let handle = self
            .session
            .tp_handle
            .as_mut()
            .ok_or(JkError::TransportNotInitialized)?;
        handle.write(&frame).await?;
        Ok(())
    }

    /// The raw decoded pack state from the last read.
    pub fn pack(&self) -> &MybmmPack {
        &self.pack
    }

    /// Model string reported by the BMS (empty until the first read).
    pub fn model(&self) -> &str {
        &self.pack.model
    }

    pub async fn disconnect(&mut self) -> Result<()> {
        self.session.close().await
    }
}