jk_bms 0.3.2

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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
pub mod bms;
pub mod error;
pub mod pack;
pub mod protocol;
pub mod session;
pub mod module;
pub mod jk_info;
pub mod transport;

pub use error::{JkError, Result};
pub use pack::{MybmmPack, ProtocolVersion, JkSettings};
pub use protocol::{getdata, get_info_command, get_cell_info_command, get_settings_command, get_short, get_signed_short, crc, error_bitmask_to_strings, ERROR_DESCRIPTIONS, FrameAssembler, ParseFlags, get_16bit, get_32bit, ieee_float, SettingDef, SETTINGS, get_setting_register, get_setting_def, get_setting_by_register, build_write_frame, build_setting_write_frame, CAN_FRAME_SIZE, CAN_CMD_INFO, CAN_CMD_CELL_INFO, CAN_CMD_WRITE_REG, build_can_command, get_can_info_command, get_can_cell_info_command, build_can_write_frame, build_can_setting_write_frame};
pub use bms::{read_data, JkBms};
pub use session::JkSession;
pub use module::{MybmmModule, Transport, jk_init, jk_new, jk_open, jk_read, jk_close, jk_control, MYBMM_CHARGE_CONTROL, MYBMM_DISCHARGE_CONTROL, MYBMM_BALANCE_CONTROL};
pub use jk_info::{JkInfo, parse_info_strings};
pub use async_trait::async_trait;

#[cfg(feature = "bluetooth")]
pub use transport::{BluetoothTransport, BtDevice, scan as bt_scan};
#[cfg(feature = "serial")]
pub use transport::SerialTransport;
#[cfg(all(feature = "can", target_os = "linux"))]
pub use transport::CanTransport;

pub const JK_MODULE_NAME: &str = "jk";
pub const JK_MODULE_TYPE: i32 = 1;

pub fn create_jk_module() -> MybmmModule {
    MybmmModule::new(
        JK_MODULE_NAME,
        MYBMM_CHARGE_CONTROL | MYBMM_DISCHARGE_CONTROL | MYBMM_BALANCE_CONTROL,
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper: compute CRC and place it at byte 299 of a 300-byte frame
    fn finalize_frame(data: &mut [u8]) {
        assert!(data.len() >= 300);
        data[299] = crate::protocol::crc(data, 299);
    }

    // --- jk_read over a fragmenting transport (regression: BLE heartbeats +
    // frames split across reads used to yield NoVoltageData) ---

    /// Mock transport that ignores writes and serves a scripted sequence of
    /// read chunks, mimicking BLE notification delivery.
    struct ScriptedTransport {
        reads: std::collections::VecDeque<Vec<u8>>,
    }

    #[async_trait::async_trait]
    impl Transport for ScriptedTransport {
        async fn open(&mut self) -> Result<()> {
            Ok(())
        }
        async fn close(&mut self) -> Result<()> {
            Ok(())
        }
        async fn write(&mut self, data: &[u8]) -> Result<usize> {
            Ok(data.len())
        }
        async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
            let Some(chunk) = self.reads.pop_front() else {
                return Ok(0);
            };
            buf[..chunk.len()].copy_from_slice(&chunk);
            Ok(chunk.len())
        }
    }

    fn info_frame() -> Vec<u8> {
        let mut f = vec![0u8; 300];
        f[0] = 0x55; f[1] = 0xAA; f[2] = 0xEB; f[3] = 0x90;
        f[4] = 0x03; // info frame
        let model = b"JK-B2A16S";
        f[6..6 + model.len()].copy_from_slice(model);
        finalize_frame(&mut f);
        f
    }

    fn cell_frame() -> Vec<u8> {
        let mut f = vec![0u8; 300];
        f[0] = 0x55; f[1] = 0xAA; f[2] = 0xEB; f[3] = 0x90;
        f[4] = 0x02; // cell info frame
        f[6] = 0xAC; f[7] = 0x0D; // 3500 mV
        f[118] = 0x00; f[119] = 0xC8; f[120] = 0x00; f[121] = 0x00;
        finalize_frame(&mut f);
        f
    }

    #[tokio::test]
    async fn test_jk_read_fragmented_with_heartbeats() {
        let info = info_frame();
        let cell = cell_frame();

        // Interleave 4-byte heartbeats and split both frames across reads,
        // starting phase 2 mid-stream (stale partial frame before a fresh one).
        let reads: std::collections::VecDeque<Vec<u8>> = [
            // phase 1: getInfo
            vec![0xAA, 0x55, 0x90, 0xEB],   // heartbeat
            info[..150].to_vec(),           // info frame, fragmented
            info[150..].to_vec(),
            // phase 2: getCellInfo
            cell[250..].to_vec(),           // tail of a stale frame
            vec![0xAA, 0x55, 0x90, 0xEB],   // heartbeat
            cell[..100].to_vec(),           // fresh frame, fragmented
            cell[100..250].to_vec(),
            cell[250..].to_vec(),
        ]
        .into_iter()
        .collect();

        let mut pack = MybmmPack::new("test");
        let mut session = JkSession {
            pp: pack.clone(),
            tp: MybmmModule::new("test", 0),
            tp_handle: Some(Box::new(ScriptedTransport { reads })),
        };

        jk_read(&mut session, &mut pack).await.expect("jk_read should succeed");
        assert_eq!(pack.model, "JK-B2A16S");
        assert!((pack.voltage - 51.2).abs() < 0.01);
    }

    #[tokio::test]
    async fn test_next_update_streams_pushed_frames() {
        let cell = cell_frame();

        // Two pushed frames, each fragmented, with heartbeats interleaved —
        // no re-request between them.
        let reads: std::collections::VecDeque<Vec<u8>> = [
            vec![0xAA, 0x55, 0x90, 0xEB],
            cell[..120].to_vec(),
            cell[120..].to_vec(),
            cell[..40].to_vec(),
            cell[40..280].to_vec(),
            cell[280..].to_vec(),
        ]
        .into_iter()
        .collect();

        let mut bms = JkBms::open("test", "mock", Box::new(ScriptedTransport { reads }))
            .await
            .unwrap();

        let timeout = std::time::Duration::from_millis(200);
        let pack = bms.next_update(timeout).await.expect("first update");
        assert!((pack.voltage - 51.2).abs() < 0.01);
        let _ = bms.next_update(timeout).await.expect("second update");

        // Stream exhausted -> timeout.
        let err = bms.next_update(std::time::Duration::from_millis(20)).await.unwrap_err();
        assert!(matches!(err, JkError::Timeout));
    }

    #[tokio::test]
    async fn test_jk_read_no_data_errors() {
        let mut pack = MybmmPack::new("test");
        let mut session = JkSession {
            pp: pack.clone(),
            tp: MybmmModule::new("test", 0),
            tp_handle: Some(Box::new(ScriptedTransport {
                reads: std::collections::VecDeque::new(),
            })),
        };
        let err = jk_read(&mut session, &mut pack).await.unwrap_err();
        assert!(matches!(err, JkError::NoVoltageData));
    }

    #[test]
    fn test_get_short() {
        let data: [u8; 4] = [0x00, 0x04, 0x00, 0x00];
        assert_eq!(get_short(&data, 0), 0x0400);
    }

    #[test]
    fn test_get_signed_short() {
        let data: [u8; 4] = [0xFF, 0xFF, 0x00, 0x00];
        assert_eq!(get_signed_short(&data, 0), -1);
    }

    #[test]
    fn test_commands() {
        let info_cmd = get_info_command();
        assert_eq!(info_cmd[0], 0xaa);
        assert_eq!(info_cmd[4], 0x97);

        let cell_cmd = get_cell_info_command();
        assert_eq!(cell_cmd[0], 0xaa);
        assert_eq!(cell_cmd[4], 0x96);
    }

    // --- JK02_24S (old) protocol tests ---
    // Voltage/current are 4-byte LE per the ESPHome reference

    #[test]
    fn test_getdata_jk02_24s_voltage() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        // 2 cells: 3500mV and 3600mV
        data[6] = 0xAC; data[7] = 0x0D;  // 0x0DAC = 3500
        data[8] = 0x10; data[9] = 0x0E;  // 0x0E10 = 3600

        // Voltage at offset 118: 51.2V = 51200 mV = 0x0000C800 (4 bytes LE)
        data[118] = 0x00; data[119] = 0xC8; data[120] = 0x00; data[121] = 0x00;

        // Current at offset 126: 5.0A = 5000 mA = 0x00001388 (4 bytes LE)
        data[126] = 0x88; data[127] = 0x13; data[128] = 0x00; data[129] = 0x00;

        // Temp 1 at offset 130: 25.0°C = 250 = 0x00FA (signed)
        data[130] = 0xFA; data[131] = 0x00;

        // Temp 2 at offset 132: 26.5°C = 265 = 0x0109 (signed)
        data[132] = 0x09; data[133] = 0x01;

        // MOS temp at offset 134: 35.0°C = 350 = 0x015E
        data[134] = 0x5E; data[135] = 0x01;

        // Error bitmask at offset 136: 0x0000 (no errors)
        data[136] = 0x00; data[137] = 0x00;

        // SOC at offset 141: 84%
        data[141] = 84;

        // SOH at offset 158: 100%
        data[158] = 100;

        // Charging mosfet at offset 166: on
        data[166] = 0x01;

        // Discharging mosfet at offset 167: on
        data[167] = 0x01;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert!(!flags.got_info);
        assert!(!flags.got_res);
        assert_eq!(pack.cells, 2);
        assert!((pack.cellvolt[0] - 3.5).abs() < 0.001);
        assert!((pack.cellvolt[1] - 3.6).abs() < 0.001);
        assert!((pack.voltage - 51.2).abs() < 0.01, "voltage: got {}", pack.voltage);
        assert!((pack.current - 5.0).abs() < 0.001, "current: got {}", pack.current);
        assert!((pack.temps[0] - 25.0).abs() < 0.1);
        assert!((pack.temps[1] - 26.5).abs() < 0.1);
        assert!((pack.power_tube_temp - 35.0).abs() < 0.1);
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_24S);
        assert!((pack.soc - 84.0).abs() < 0.1);
        assert!((pack.soh - 100.0).abs() < 0.1);
        assert!(pack.charging);
        assert!(pack.discharging);
    }

    #[test]
    fn test_getdata_jk02_24s_negative_current_and_temp() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        // 1 cell at 3300mV
        data[6] = 0xE4; data[7] = 0x0C; // 0x0CE4 = 3300

        // Voltage at 118: 53.060V = 53060 mV = 0x0000CF44
        data[118] = 0x44; data[119] = 0xCF; data[120] = 0x00; data[121] = 0x00;

        // Current at 126: -5.0A = -5000 mA = 0xFFFFEC78 (i32 LE)
        data[126] = 0x78; data[127] = 0xEC; data[128] = 0xFF; data[129] = 0xFF;

        // Negative temp at 130: -5.0°C = -50 = 0xFFCE (i16 LE)
        data[130] = 0xCE; data[131] = 0xFF;
        // Normal temp at 132: 24.0°C = 240 = 0x00F0
        data[132] = 0xF0; data[133] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert!((pack.current - (-5.0)).abs() < 0.01, "current: got {}", pack.current);
        assert!((pack.voltage - 53.060).abs() < 0.01);
        assert!((pack.temps[0] - (-5.0)).abs() < 0.1, "temp0: got {}", pack.temps[0]);
        assert!((pack.temps[1] - 24.0).abs() < 0.1);
        assert!(pack.power < 0.0); // discharging
    }

    #[test]
    fn test_getdata_jk02_24s_error_bitmask() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        data[6] = 0xAC; data[7] = 0x0D; // 3500mV

        // Voltage at 118
        data[118] = 0x00; data[119] = 0xC8; data[120] = 0x00; data[121] = 0x00;
        // Current at 126
        data[126] = 0x00; data[127] = 0x00; data[128] = 0x00; data[129] = 0x00;

        // Error bitmask at offset 136: bit 3 (Cell Undervoltage) + bit 12 (Cell Overvoltage)
        // = 0x1008
        data[136] = 0x08; data[137] = 0x10;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let _ = getdata(&mut pack, &data);

        assert_eq!(pack.error_bitmask, 0x1008);
        let errors = error_bitmask_to_strings(pack.error_bitmask);
        assert!(errors.contains(&"Cell Undervoltage"));
        assert!(errors.contains(&"Cell Overvoltage"));
        assert_eq!(errors.len(), 2);
    }

    // --- JK02_32S (PB2) protocol tests ---

    #[test]
    fn test_getdata_jk02_32s_cell_info() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;
        data[5] = 0x55; // frame counter (happens to be 0x55, tests scanner robustness)

        // 2 cells: 3318mV and 3317mV (at offset 6, 2 bytes each LE)
        data[6] = 0xF6; data[7] = 0x0C;  // 0x0CF6 = 3318
        data[8] = 0xF5; data[9] = 0x0C;  // 0x0CF5 = 3317

        // Cell resistances at offset 64+16=80: 72 mΩ, 75 mΩ (0.001 Ohm each)
        data[80] = 0x48; data[81] = 0x00; // 0x0048 = 72 -> 0.072 Ohm
        data[82] = 0x4B; data[83] = 0x00; // 0x004B = 75 -> 0.075 Ohm

        // For 32S, offset = 16, offset2 = 32
        let _offset2 = 32;

        // Voltage at offset 118+32=150: 53047 mV = 0x0000CF37
        data[150] = 0x37; data[151] = 0xCF; data[152] = 0x00; data[153] = 0x00;

        // Current at offset 126+32=158: -6808 mA = 0xFFFFE568
        data[158] = 0x68; data[159] = 0xE5; data[160] = 0xFF; data[161] = 0xFF;

        // Temp 1 at offset 130+32=162: 234 = 23.4°C
        data[162] = 0xEA; data[163] = 0x00;

        // Temp 2 at offset 132+32=164: 240 = 24.0°C
        data[164] = 0xF0; data[165] = 0x00;

        // Error bitmask at offset 134+32=166 (32S uses this instead of MOS temp)
        data[166] = 0x00; data[167] = 0x00;

        // SOC at offset 141+32=173
        data[173] = 76;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        pack.protocol_version = ProtocolVersion::Jk02_32S;
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 2);
        assert!((pack.cellvolt[0] - 3.318).abs() < 0.001);
        assert!((pack.cellvolt[1] - 3.317).abs() < 0.001);
        assert!((pack.voltage - 53.047).abs() < 0.01, "voltage: got {}", pack.voltage);
        assert!((pack.current - (-6.808)).abs() < 0.01, "current: got {}", pack.current);
        assert!((pack.temps[0] - 23.4).abs() < 0.1);
        assert!((pack.temps[1] - 24.0).abs() < 0.1);
        // Cell resistances are in Ohms (raw * 0.001)
        assert!((pack.cellres[0] - 0.072).abs() < 0.001, "cellres[0]: got {}", pack.cellres[0]);
        assert!((pack.cellres[1] - 0.075).abs() < 0.001, "cellres[1]: got {}", pack.cellres[1]);
        assert!((pack.soc - 76.0).abs() < 0.1);
    }

    #[test]
    fn test_getdata_jk02_32s_16cells() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;
        data[5] = 0x63;

        // 16 cells all at 3315mV (0x0CF3)
        for i in 0..16 {
            data[6 + i * 2] = 0xF3;
            data[6 + i * 2 + 1] = 0x0C;
        }

        let _offset2 = 32;

        // Voltage at 118+32=150: 53056 mV
        data[150] = 0x40; data[151] = 0xCF; data[152] = 0x00; data[153] = 0x00;

        // Current at 158: -9058 mA
        data[158] = 0x9E; data[159] = 0xDC; data[160] = 0xFF; data[161] = 0xFF;

        // Temps
        data[162] = 0xD5; data[163] = 0x00; // 213 = 21.3°C
        data[164] = 0xDB; data[165] = 0x00; // 219 = 21.9°C

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        pack.protocol_version = ProtocolVersion::Jk02_32S;
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 16);
        for i in 0..16 {
            assert!((pack.cellvolt[i] - 3.315).abs() < 0.001, "cell {} = {}", i, pack.cellvolt[i]);
        }
        assert!((pack.voltage - 53.056).abs() < 0.01);
        assert!((pack.current - (-9.058)).abs() < 0.01);
        assert!((pack.temps[0] - 21.3).abs() < 0.1);
        assert!((pack.temps[1] - 21.9).abs() < 0.1);
    }

    // --- Info frame tests ---

    #[test]
    fn test_getdata_info_frame_detects_pb2() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x03;

        let model = b"JK_PB2A16S20P";
        data[6..6 + model.len()].copy_from_slice(model);
        data[6 + model.len()] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_info);
        assert!(!flags.got_volts);
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_32S, "JK_PB2 should be JK02_32S");
        assert!(pack.is_pb2());
    }

    #[test]
    fn test_getdata_info_frame_non_pb2() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x03;

        let model = b"JK-B2A16S";
        data[6..6 + model.len()].copy_from_slice(model);
        data[6 + model.len()] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_info);
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_24S, "JK-B2 should be JK02_24S");
        assert!(!pack.is_pb2());
    }

    #[test]
    fn test_getdata_info_detects_jk_bd() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x03;

        let model = b"JK-BD6A20S";
        data[6..6 + model.len()].copy_from_slice(model);
        data[6 + model.len()] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_info);
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_32S, "JK-BD should be JK02_32S");
    }

    #[test]
    fn test_getdata_info_detects_jk_hy() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x03;

        let model = b"JK_HY102A16S";
        data[6..6 + model.len()].copy_from_slice(model);
        data[6 + model.len()] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_info);
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_32S, "JK_HY should be JK02_32S");
    }

    // --- Scanner tests ---

    #[test]
    fn test_getdata_type01_res_frame() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x01;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_res);
        assert!(!flags.got_volts);
        assert!(!flags.got_info);
    }

    #[test]
    fn test_getdata_frame_at_nonzero_offset() {
        let mut data = vec![0u8; 400];
        data[0] = b'A'; data[1] = b'T'; data[2] = 0x0D; data[3] = 0x0A;

        // Frame starts at offset 4
        data[4] = 0x55; data[5] = 0xAA; data[6] = 0xEB; data[7] = 0x90;
        data[8] = 0x02;

        // 1 cell at 3315mV
        data[10] = 0xF3; data[11] = 0x0C;

        // Old protocol voltage at offset 4+118 = 122 (4 bytes LE)
        data[122] = 0x44; data[123] = 0xCF; data[124] = 0x00; data[125] = 0x00;

        // Current at offset 4+126 = 130 (4 bytes LE)
        data[130] = 0x78; data[131] = 0xEC; data[132] = 0xFF; data[133] = 0xFF;

        // Temps at 4+130=134, 4+132=136
        data[134] = 0xEA; data[135] = 0x00;
        data[136] = 0xF0; data[137] = 0x00;

        // CRC at offset 4+299=303
        finalize_frame(&mut data[4..].to_vec().as_mut_slice());
        // Recompute: need CRC relative to frame start
        // Actually, finalize_frame works on the whole slice starting at 0
        // Let's compute CRC manually for the frame starting at offset 4
        let frame_start = 4;
        let frame_data = &data[frame_start..];
        let mut crc_val: u8 = 0;
        for i in 0..299 {
            crc_val = crc_val.wrapping_add(frame_data[i]);
        }
        data[frame_start + 299] = crc_val;

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 1);
        assert!((pack.cellvolt[0] - 3.315).abs() < 0.001);
        assert!((pack.voltage - 53.060).abs() < 0.01);
    }

    #[test]
    fn test_getdata_multi_frame_info_then_voltage() {
        let mut data = vec![0u8; 800];

        // Info frame at offset 0
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x03;
        let model = b"JK_PB2A16S20P";
        data[6..6 + model.len()].copy_from_slice(model);
        data[6 + model.len()] = 0x00;

        // CRC for info frame
        {
            let mut crc_val: u8 = 0;
            for i in 0..299 { crc_val = crc_val.wrapping_add(data[i]); }
            data[299] = crc_val;
        }

        // Voltage frame at offset 300
        let voff = 300;
        data[voff] = 0x55; data[voff + 1] = 0xAA;
        data[voff + 2] = 0xEB; data[voff + 3] = 0x90;
        data[voff + 4] = 0x02;

        // 2 cells
        data[voff + 6] = 0xF6; data[voff + 7] = 0x0C;
        data[voff + 8] = 0xF5; data[voff + 9] = 0x0C;

        // Since PB2 detected from info frame, offset=16, offset2=32
        // Voltage at voff+150
        data[voff + 150] = 0x37; data[voff + 151] = 0xCF;
        data[voff + 152] = 0x00; data[voff + 153] = 0x00;

        // Current at voff+158
        data[voff + 158] = 0x68; data[voff + 159] = 0xE5;
        data[voff + 160] = 0xFF; data[voff + 161] = 0xFF;

        // Temps at voff+162, voff+164
        data[voff + 162] = 0xEA; data[voff + 163] = 0x00;
        data[voff + 164] = 0xF0; data[voff + 165] = 0x00;

        // CRC for voltage frame
        {
            let mut crc_val: u8 = 0;
            for i in 0..299 { crc_val = crc_val.wrapping_add(data[voff + i]); }
            data[voff + 299] = crc_val;
        }

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_info, "should find info frame");
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_32S, "should detect JK02_32S from info frame model");
        assert!(flags.got_volts, "should find voltage frame after info frame");
        assert!((pack.voltage - 53.047).abs() < 0.01);
        assert!((pack.current - (-6.808)).abs() < 0.01);
    }

    #[test]
    fn test_getdata_0x55_in_data() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;
        data[5] = 0x55;

        // 1 cell
        data[6] = 0xF6; data[7] = 0x0C;

        // Voltage at 118
        data[118] = 0x00; data[119] = 0x80; data[120] = 0x00; data[121] = 0x00;

        // Current at 126
        data[126] = 0x00; data[127] = 0x00; data[128] = 0x00; data[129] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 1);
        assert!((pack.voltage - 32.768).abs() < 0.01);
    }

    #[test]
    fn test_getdata_no_signature() {
        let data = vec![0u8; 200];
        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(!flags.got_volts);
        assert!(!flags.got_info);
        assert!(!flags.got_res);
    }

    #[test]
    fn test_getdata_short_buffer() {
        let data = [0x55, 0xAA, 0xEB, 0x90, 0x02, 0x00];
        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(!flags.got_volts);
    }

    #[test]
    fn test_crc_verification() {
        let mut data = vec![0u8; 300];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;
        data[6] = 0xAC; data[7] = 0x0D; // 3500mV

        finalize_frame(&mut data);

        // CRC should pass
        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);
        assert!(flags.got_volts);

        // Corrupt a byte and CRC should fail
        data[50] = data[50].wrapping_add(1);
        let mut pack2 = MybmmPack::new("test");
        let flags2 = getdata(&mut pack2, &data);
        assert!(!flags2.got_volts, "CRC failure should reject frame");
    }

    #[test]
    fn test_getdata_stops_after_got_volts() {
        let mut data = vec![0u8; 800];

        // Voltage frame at offset 0
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;
        data[6] = 0xAC; data[7] = 0x0D; // 3500mV
        data[118] = 0x00; data[119] = 0xC8; data[120] = 0x00; data[121] = 0x00;

        finalize_frame(&mut data);

        // We can't easily make a second frame since CRC needs to be valid
        // Just verify the first frame is parsed correctly
        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert!((pack.cellvolt[0] - 3.5).abs() < 0.001);
        assert!((pack.voltage - 51.2).abs() < 0.01);
    }

    #[test]
    fn test_protocol_version_from_model() {
        assert_eq!(ProtocolVersion::from_model("JK_PB2A16S20P"), ProtocolVersion::Jk02_32S);
        assert_eq!(ProtocolVersion::from_model("JK-BD6A20S"), ProtocolVersion::Jk02_32S);
        assert_eq!(ProtocolVersion::from_model("JK_HY102A16S"), ProtocolVersion::Jk02_32S);
        assert_eq!(ProtocolVersion::from_model("JK-B2A16S"), ProtocolVersion::Jk02_24S);
        assert_eq!(ProtocolVersion::from_model("JK-B2A24S"), ProtocolVersion::Jk02_24S);
        assert_eq!(ProtocolVersion::from_model("SomeOther"), ProtocolVersion::Jk02_24S);
    }

    #[test]
    fn test_error_bitmask_to_strings() {
        // All bits
        let all = error_bitmask_to_strings(0xFFFF);
        assert_eq!(all.len(), 16);
        assert_eq!(all[0], "Charge Overtemperature");
        assert_eq!(all[15], "Charge short circuit");

        // Single bit
        let single = error_bitmask_to_strings(0x0008);
        assert_eq!(single.len(), 1);
        assert_eq!(single[0], "Cell Undervoltage");

        // No errors
        let none = error_bitmask_to_strings(0x0000);
        assert!(none.is_empty());
    }

    #[test]
    fn test_get_short_out_of_bounds() {
        let data = [0x01, 0x02];
        assert_eq!(get_short(&data, 0), 0x0201);
        assert_eq!(get_short(&data, 1), 0);
        assert_eq!(get_short(&data, 2), 0);
    }

    #[test]
    fn test_crc_function() {
        // Known CRC: all zeros should give 0
        let zeros = [0u8; 300];
        assert_eq!(crate::protocol::crc(&zeros, 299), 0);

        // Single byte
        assert_eq!(crate::protocol::crc(&[0x42], 1), 0x42);
    }

    // --- FrameAssembler tests (BLE fragmentation) ---

    #[test]
    fn test_frame_assembler_single_complete_frame() {
        let mut frame = vec![0u8; 300];
        frame[0] = 0x55; frame[1] = 0xAA; frame[2] = 0xEB; frame[3] = 0x90;
        frame[4] = 0x02;
        frame[6] = 0xAC; frame[7] = 0x0D; // 3500mV
        frame[118] = 0x00; frame[119] = 0xC8; frame[120] = 0x00; frame[121] = 0x00;
        finalize_frame(&mut frame);

        let mut assembler = FrameAssembler::new();
        let mut pack = MybmmPack::new("test");

        // Feed entire frame at once
        let result = assembler.feed_and_decode(&mut pack, &frame);
        assert!(result.is_some());
        let flags = result.unwrap();
        assert!(flags.got_volts);
        assert!((pack.voltage - 51.2).abs() < 0.01);
    }

    #[test]
    fn test_frame_assembler_fragmented_ble() {
        let mut frame = vec![0u8; 300];
        frame[0] = 0x55; frame[1] = 0xAA; frame[2] = 0xEB; frame[3] = 0x90;
        frame[4] = 0x02;
        frame[6] = 0xAC; frame[7] = 0x0D; // 3500mV
        frame[118] = 0x00; frame[119] = 0xC8; frame[120] = 0x00; frame[121] = 0x00;
        finalize_frame(&mut frame);

        let mut assembler = FrameAssembler::new();

        // Simulate BLE notifications arriving in 20-byte chunks
        // Feed all but the last chunk using feed() (no decode attempt)
        let chunks: Vec<&[u8]> = frame.chunks(20).collect();
        for i in 0..chunks.len() - 1 {
            assembler.feed(chunks[i]);
            assert!(assembler.buffer_len() < 300, "should not have 300 bytes yet");
        }

        // Buffer should have 280 bytes (14 chunks * 20)
        assert!(assembler.buffer_len() < 300);

        // Feed the last chunk and decode
        let mut pack = MybmmPack::new("test");
        let result = assembler.feed_and_decode(&mut pack, chunks.last().unwrap());
        assert!(result.is_some(), "should decode once buffer has 300+ bytes");
        assert!(result.unwrap().got_volts);
        assert!((pack.voltage - 51.2).abs() < 0.01);
    }

    #[test]
    fn test_frame_assembler_preamble_flushes() {
        let mut frame1 = vec![0u8; 300];
        frame1[0] = 0x55; frame1[1] = 0xAA; frame1[2] = 0xEB; frame1[3] = 0x90;
        frame1[4] = 0x03; // info frame
        let model = b"JK-B2A16S";
        frame1[6..6 + model.len()].copy_from_slice(model);
        frame1[6 + model.len()] = 0x00;
        finalize_frame(&mut frame1);

        let mut frame2 = vec![0u8; 300];
        frame2[0] = 0x55; frame2[1] = 0xAA; frame2[2] = 0xEB; frame2[3] = 0x90;
        frame2[4] = 0x02;
        frame2[6] = 0xAC; frame2[7] = 0x0D;
        frame2[118] = 0x00; frame2[119] = 0xC8; frame2[120] = 0x00; frame2[121] = 0x00;
        finalize_frame(&mut frame2);

        let mut assembler = FrameAssembler::new();
        let mut pack = MybmmPack::new("test");

        // Feed first 100 bytes of frame1 (partial)
        assembler.feed(&frame1[..100]);
        assert_eq!(assembler.buffer_len(), 100);

        // Now feed frame2 preamble — should flush the partial frame1 data
        assembler.feed(&frame2[..50]);
        // Buffer should have been flushed when preamble was seen, then filled with frame2 data
        assert_eq!(assembler.buffer_len(), 50);

        // Feed the rest of frame2
        let result = assembler.feed_and_decode(&mut pack, &frame2[50..]);
        assert!(result.is_some());
        assert!(result.unwrap().got_volts);
    }

    #[test]
    fn test_frame_assembler_crc_failure_clears() {
        let mut frame = vec![0u8; 300];
        frame[0] = 0x55; frame[1] = 0xAA; frame[2] = 0xEB; frame[3] = 0x90;
        frame[4] = 0x02;
        finalize_frame(&mut frame);

        let mut assembler = FrameAssembler::new();

        // Corrupt a byte
        frame[50] = frame[50].wrapping_add(1);

        // Feed the corrupted frame
        let mut pack = MybmmPack::new("test");
        let result = assembler.feed_and_decode(&mut pack, &frame);
        assert!(result.is_none(), "CRC failure should return None");
        assert_eq!(assembler.buffer_len(), 0, "buffer should be cleared after CRC failure");
    }

    // --- Byte ordering tests for get_16bit / get_32bit (ESPHome-compatible) ---

    #[test]
    fn test_get_16bit_byte_order() {
        // ESPHome: (data[i+1] << 8) | data[i]  — little-endian pairs
        let data = [0x34, 0x12, 0x78, 0x56];
        // get_16bit at 0 should give (0x12 << 8) | 0x34 = 0x1234
        assert_eq!(crate::protocol::get_16bit(&data, 0), 0x1234);
        // get_16bit at 2 should give (0x56 << 8) | 0x78 = 0x5678
        assert_eq!(crate::protocol::get_16bit(&data, 2), 0x5678);
    }

    #[test]
    fn test_get_32bit_byte_order() {
        // ESPHome: (get_16bit(i+2) << 16) | get_16bit(i)
        // For data [0x78 0x56 0x34 0x12]:
        //   get_16bit(0) = (0x56 << 8) | 0x78 = 0x5678
        //   get_16bit(2) = (0x12 << 8) | 0x34 = 0x1234
        //   get_32bit(0) = (0x1234 << 16) | 0x5678 = 0x12345678
        let data = [0x78, 0x56, 0x34, 0x12];
        assert_eq!(crate::protocol::get_32bit(&data, 0), 0x12345678);
    }

    #[test]
    fn test_ieee_float() {
        // 3.315 as IEEE 754: f32::to_bits() = 0x405428F6
        let bits = 3.315f32.to_bits();
        assert_eq!(crate::protocol::ieee_float(bits), 3.315f32);

        // -5.0
        let bits_neg = (-5.0f32).to_bits();
        assert_eq!(crate::protocol::ieee_float(bits_neg), -5.0f32);

        // 0.0
        assert_eq!(crate::protocol::ieee_float(0u32), 0.0f32);
    }

    // --- Comprehensive JK02_24S test covering ALL fields ---

    #[test]
    fn test_getdata_jk02_24s_all_fields() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        // 2 cells: 3500mV and 3600mV
        data[6] = 0xAC; data[7] = 0x0D;   // 0x0DAC = 3500
        data[8] = 0x10; data[9] = 0x0E;   // 0x0E10 = 3600

        // Cell resistances at offset 64: 63 mΩ and 85 mΩ → 0.063 Ω, 0.085 Ω
        data[64] = 0x3F; data[65] = 0x00;  // 0x003F = 63 → 0.063 Ω
        data[66] = 0x55; data[67] = 0x00;  // 0x0055 = 85 → 0.085 Ω

        // Enabled cells bitmask at offset 54: 0x00000003 (2 cells)
        data[54] = 0x03; data[55] = 0x00; data[56] = 0x00; data[57] = 0x00;

        // Voltage at offset 118: 51.2V = 51200 mV = 0x0000C800
        data[118] = 0x00; data[119] = 0xC8; data[120] = 0x00; data[121] = 0x00;

        // Current at offset 126: -5.0A = -5000 mA = 0xFFFFEC78
        data[126] = 0x78; data[127] = 0xEC; data[128] = 0xFF; data[129] = 0xFF;

        // Temp 1 at 130: 25.0°C = 250
        data[130] = 0xFA; data[131] = 0x00;
        // Temp 2 at 132: 26.5°C = 265
        data[132] = 0x09; data[133] = 0x01;

        // MOSFET temp at 134: 35.0°C = 350
        data[134] = 0x5E; data[135] = 0x01;

        // Error bitmask at 136: 0x0008 (Cell Undervoltage)
        data[136] = 0x08; data[137] = 0x00;

        // Balancing current at 138: 0.050 A = 50 mA
        data[138] = 0x32; data[139] = 0x00;  // 0x0032 = 50 → 0.050 A

        // Balancing at 140: 0x01 (charging balancer)
        data[140] = 0x01;

        // SOC at 141: 84%
        data[141] = 84;

        // Capacity remaining at 142: 100.0 Ah = 100000 mAh
        // Using get_32bit byte order: [lo16_lo, lo16_hi, hi16_lo, hi16_hi]
        // 100000 = 0x000186A0 → lo16=0x86A0, hi16=0x0001
        // get_16bit reads (hi<<8)|lo → 0xA0 then 0x86 → 0x86A0, then 0x01 0x00 → 0x0001
        data[142] = 0xA0; data[143] = 0x86; data[144] = 0x01; data[145] = 0x00;

        // Total battery capacity at 146: 300.0 Ah = 300000 mAh = 0x000493E0
        // lo16=0x93E0, hi16=0x0004
        data[146] = 0xE0; data[147] = 0x93; data[148] = 0x04; data[149] = 0x00;

        // Charging cycles at 150: 42 = 0x0000002A
        data[150] = 0x2A; data[151] = 0x00; data[152] = 0x00; data[153] = 0x00;

        // Total charging cycle capacity at 154: 1000.0 Ah = 1000000 mAh = 0x000F4240
        // lo16=0x4240, hi16=0x000F
        data[154] = 0x40; data[155] = 0x42; data[156] = 0x0F; data[157] = 0x00;

        // SOH at 158: 99%
        data[158] = 99;

        // Total runtime at 162: 86400 seconds = 1 day = 0x00015180
        data[162] = 0x80; data[163] = 0x51; data[164] = 0x01; data[165] = 0x00;

        // Charging MOS at 166: on
        data[166] = 0x01;

        // Discharging MOS at 167: on
        data[167] = 0x01;

        // Precharging at 168: on
        data[168] = 0x01;

        // Heating at 183: off
        data[183] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 2);

        // Cell voltages
        assert!((pack.cellvolt[0] - 3.5).abs() < 0.001);
        assert!((pack.cellvolt[1] - 3.6).abs() < 0.001);

        // Cell resistances (in Ohms, raw * 0.001)
        assert!((pack.cellres[0] - 0.063).abs() < 0.001, "cellres[0]: got {}", pack.cellres[0]);
        assert!((pack.cellres[1] - 0.085).abs() < 0.001, "cellres[1]: got {}", pack.cellres[1]);

        // Computed cell stats
        assert!((pack.cell_min - 3.5).abs() < 0.001, "cell_min: got {}", pack.cell_min);
        assert!((pack.cell_max - 3.6).abs() < 0.001, "cell_max: got {}", pack.cell_max);
        assert!((pack.cell_diff - 0.1).abs() < 0.001, "cell_diff: got {}", pack.cell_diff);
        assert!((pack.cell_avg - 3.55).abs() < 0.001, "cell_avg: got {}", pack.cell_avg);

        // Enabled cells bitmask
        assert_eq!(pack.enabled_cells_bitmask, 0x00000003);

        // Voltage, current, power
        assert!((pack.voltage - 51.2).abs() < 0.01);
        assert!((pack.current - (-5.0)).abs() < 0.01);
        assert!((pack.power - (-256.0)).abs() < 0.1, "power: got {}", pack.power);
        assert!((pack.charging_power - 0.0).abs() < 0.1, "charging_power: got {}", pack.charging_power);
        assert!((pack.discharging_power - 256.0).abs() < 0.1, "discharging_power: got {}", pack.discharging_power);

        // Temps
        assert_eq!(pack.ntemps, 2);
        assert!((pack.temps[0] - 25.0).abs() < 0.1);
        assert!((pack.temps[1] - 26.5).abs() < 0.1);

        // MOSFET temp
        assert!((pack.power_tube_temp - 35.0).abs() < 0.1);

        // Error bitmask
        assert_eq!(pack.error_bitmask, 0x0008);

        // Balancing current
        assert!((pack.balancing_current - 0.050).abs() < 0.001, "balancing_current: got {}", pack.balancing_current);

        // Balancing
        assert!(pack.balancing);

        // SOC
        assert!((pack.soc - 84.0).abs() < 0.1);

        // Capacity remaining
        assert!((pack.capacity_remaining - 100.0).abs() < 0.01, "capacity_remaining: got {}", pack.capacity_remaining);

        // Total battery capacity
        assert!((pack.total_battery_capacity - 300.0).abs() < 0.01);

        // Charging cycles
        assert_eq!(pack.charging_cycles, 42);

        // Total charging cycle capacity
        assert!((pack.total_charging_cycle_capacity - 1000.0).abs() < 0.1, "total_charging_cycle_capacity: got {}", pack.total_charging_cycle_capacity);

        // SOH
        assert!((pack.soh - 99.0).abs() < 0.1);

        // Total runtime
        assert_eq!(pack.total_runtime, 86400);

        // MOS states
        assert!(pack.charging);
        assert!(pack.discharging);
        assert!(pack.precharging);
        assert!(!pack.heating);
    }

    // --- Comprehensive JK02_32S test covering ALL fields including 32S-specific ones ---

    #[test]
    fn test_getdata_jk02_32s_all_fields() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        // 2 cells: 3500mV and 3600mV
        data[6] = 0xAC; data[7] = 0x0D;
        data[8] = 0x10; data[9] = 0x0E;

        // Cell resistances at 64+16=80: 63mΩ, 85mΩ → 0.063Ω, 0.085Ω
        data[80] = 0x3F; data[81] = 0x00;
        data[82] = 0x55; data[83] = 0x00;

        // offset=16, offset2=32
        let _o2 = 32;

        // Power tube temp at 112+o2=144: 35.0°C = 350
        data[144] = 0x5E; data[145] = 0x01;

        // Voltage at 118+o2=150: 51.2V
        data[150] = 0x00; data[151] = 0xC8; data[152] = 0x00; data[153] = 0x00;

        // Current at 126+o2=158: -5.0A
        data[158] = 0x78; data[159] = 0xEC; data[160] = 0xFF; data[161] = 0xFF;

        // Temp 1 at 130+o2=162: 25.0°C
        data[162] = 0xFA; data[163] = 0x00;
        // Temp 2 at 132+o2=164: 26.5°C
        data[164] = 0x09; data[165] = 0x01;

        // Error bitmask at 134+o2=166 (32S reads from here, NOT 136)
        data[166] = 0x20; data[167] = 0x00; // bit 5: Discharge overcurrent

        // Balancing current at 138+o2=170: 0.050A
        data[170] = 0x32; data[171] = 0x00;

        // Balancing at 140+o2=172: active
        data[172] = 0x02; // discharging balancer

        // SOC at 141+o2=173
        data[173] = 76;

        // Capacity remaining at 142+o2=174: 100.0 Ah
        data[174] = 0xA0; data[175] = 0x86; data[176] = 0x01; data[177] = 0x00;

        // Total battery capacity at 146+o2=178: 300.0 Ah
        data[178] = 0xE0; data[179] = 0x93; data[180] = 0x04; data[181] = 0x00;

        // Charging cycles at 150+o2=182: 42
        data[182] = 0x2A; data[183] = 0x00; data[184] = 0x00; data[185] = 0x00;

        // Total charging cycle capacity at 154+o2=186: 1000.0 Ah
        data[186] = 0x40; data[187] = 0x42; data[188] = 0x0F; data[189] = 0x00;

        // SOH at 158+o2=190
        data[190] = 99;

        // Total runtime at 162+o2=194
        data[194] = 0x80; data[195] = 0x51; data[196] = 0x01; data[197] = 0x00;

        // Charging MOS at 166+o2=198
        data[198] = 0x01;
        // Discharging MOS at 167+o2=199
        data[199] = 0x01;
        // Precharging at 168+o2=200
        data[200] = 0x01;
        // Heating at 183+o2=215
        data[215] = 0x01;

        // Extra temp sensors (32S only):
        // Temp 5 at 226+o2=258: 28.0°C = 280
        data[258] = 0x18; data[259] = 0x01;
        // Temp 4 at 224+o2=256: 27.5°C = 275
        data[256] = 0x13; data[257] = 0x01;
        // Temp 3 at 222+o2=254: 27.0°C = 270
        data[254] = 0x0E; data[255] = 0x01;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        pack.protocol_version = ProtocolVersion::Jk02_32S;
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 2);

        // Cell voltages & resistances
        assert!((pack.cellvolt[0] - 3.5).abs() < 0.001);
        assert!((pack.cellvolt[1] - 3.6).abs() < 0.001);
        assert!((pack.cellres[0] - 0.063).abs() < 0.001);
        assert!((pack.cellres[1] - 0.085).abs() < 0.001);

        // Computed stats
        assert!((pack.cell_min - 3.5).abs() < 0.001);
        assert!((pack.cell_max - 3.6).abs() < 0.001);
        assert!((pack.cell_diff - 0.1).abs() < 0.001);
        assert!((pack.cell_avg - 3.55).abs() < 0.001);

        // Power tube temp (32S reads from 112+offset2)
        assert!((pack.power_tube_temp - 35.0).abs() < 0.1, "power_tube_temp: got {}", pack.power_tube_temp);

        // Voltage, current
        assert!((pack.voltage - 51.2).abs() < 0.01);
        assert!((pack.current - (-5.0)).abs() < 0.01);
        assert!((pack.power - (-256.0)).abs() < 0.1);
        assert!((pack.discharging_power - 256.0).abs() < 0.1);

        // Temps 1 & 2
        assert!((pack.temps[0] - 25.0).abs() < 0.1);
        assert!((pack.temps[1] - 26.5).abs() < 0.1);

        // 32S-specific: error bitmask at 134+offset2 (NOT 136+offset2)
        assert_eq!(pack.error_bitmask, 0x0020, "32S error_bitmask should come from offset 134+o2");
        let errors = error_bitmask_to_strings(pack.error_bitmask);
        assert!(errors.contains(&"Discharge overcurrent"));

        // Balancing current
        assert!((pack.balancing_current - 0.050).abs() < 0.001);

        // Balancing
        assert!(pack.balancing);

        // SOC
        assert!((pack.soc - 76.0).abs() < 0.1);

        // Capacity
        assert!((pack.capacity_remaining - 100.0).abs() < 0.01);
        assert!((pack.total_battery_capacity - 300.0).abs() < 0.01);

        // Charging cycles
        assert_eq!(pack.charging_cycles, 42);

        // Total charging cycle capacity
        assert!((pack.total_charging_cycle_capacity - 1000.0).abs() < 0.1);

        // SOH
        assert!((pack.soh - 99.0).abs() < 0.1);

        // Total runtime
        assert_eq!(pack.total_runtime, 86400);

        // MOS states
        assert!(pack.charging);
        assert!(pack.discharging);
        assert!(pack.precharging);
        assert!(pack.heating);

        // 32S extra temp sensors
        assert_eq!(pack.ntemps, 5, "32S should have 5 temp probes");
        // temps[2] from 226+o2=258: 0x0118 = 280 → 28.0°C
        assert!((pack.temps[2] - 28.0).abs() < 0.1, "temp[2]: got {}", pack.temps[2]);
        // temps[3] from 224+o2=256: 0x0113 = 275 → 27.5°C
        assert!((pack.temps[3] - 27.5).abs() < 0.1, "temp[3]: got {}", pack.temps[3]);
        // temps[4] from 222+o2=254: 0x010E = 270 → 27.0°C
        assert!((pack.temps[4] - 27.0).abs() < 0.1, "temp[4]: got {}", pack.temps[4]);
    }

    // --- JK04 protocol test (IEEE 754 float cell voltages/resistances) ---

    #[test]
    fn test_getdata_jk04_cell_info() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        // JK04: cell voltages are IEEE 754 floats at offset 6+i*4
        // Cell 1: 3.315 V
        let bits1 = 3.315f32.to_bits();
        data[6] = bits1 as u8; data[7] = (bits1 >> 8) as u8; data[8] = (bits1 >> 16) as u8; data[9] = (bits1 >> 24) as u8;
        // Cell 2: 3.325 V
        let bits2 = 3.325f32.to_bits();
        data[10] = bits2 as u8; data[11] = (bits2 >> 8) as u8; data[12] = (bits2 >> 16) as u8; data[13] = (bits2 >> 24) as u8;

        // Cells 3-24: 0.0 (unused)
        // Already zeroed

        // Cell resistances at 102+i*4: IEEE 754 floats
        // Cell 1 resistance: 0.063 Ohm
        let r1 = 0.063f32.to_bits();
        data[102] = r1 as u8; data[103] = (r1 >> 8) as u8; data[104] = (r1 >> 16) as u8; data[105] = (r1 >> 24) as u8;
        // Cell 2 resistance: 0.085 Ohm
        let r2 = 0.085f32.to_bits();
        data[106] = r2 as u8; data[107] = (r2 >> 8) as u8; data[108] = (r2 >> 16) as u8; data[109] = (r2 >> 24) as u8;

        // Balancing action at 220: active
        data[220] = 0x01;

        // Balancing current at 222: 0.050 A as IEEE 754
        let bc = 0.050f32.to_bits();
        data[222] = bc as u8; data[223] = (bc >> 8) as u8; data[224] = (bc >> 16) as u8; data[225] = (bc >> 24) as u8;

        // Total runtime at 286: 86400
        data[286] = 0x80; data[287] = 0x51; data[288] = 0x01; data[289] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        pack.protocol_version = ProtocolVersion::Jk04;
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_volts);
        assert_eq!(pack.cells, 2);

        // Cell voltages (IEEE 754 floats)
        assert!((pack.cellvolt[0] - 3.315).abs() < 0.001, "cellvolt[0]: got {}", pack.cellvolt[0]);
        assert!((pack.cellvolt[1] - 3.325).abs() < 0.001, "cellvolt[1]: got {}", pack.cellvolt[1]);

        // Cell resistances (IEEE 754 floats, in Ohms)
        assert!((pack.cellres[0] - 0.063).abs() < 0.001, "cellres[0]: got {}", pack.cellres[0]);
        assert!((pack.cellres[1] - 0.085).abs() < 0.001, "cellres[1]: got {}", pack.cellres[1]);

        // Total voltage = sum of cell voltages
        assert!((pack.voltage - 6.640).abs() < 0.01, "voltage: got {}", pack.voltage);

        // Computed stats
        assert!((pack.cell_min - 3.315).abs() < 0.001);
        assert!((pack.cell_max - 3.325).abs() < 0.001);
        assert!((pack.cell_diff - 0.010).abs() < 0.001);
        assert!((pack.cell_avg - 3.320).abs() < 0.001);

        // Balancing
        assert!(pack.balancing);

        // Balancing current
        assert!((pack.balancing_current - 0.050).abs() < 0.001, "balancing_current: got {}", pack.balancing_current);

        // Total runtime
        assert_eq!(pack.total_runtime, 86400);
    }

    // --- Info frame string extraction test ---

    #[test]
    fn test_getdata_info_frame_extracts_model_hw_sw() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x03;

        // Model at offset 6: "JK_PB2A16S20P\0"
        let model = b"JK_PB2A16S20P";
        data[6..6 + model.len()].copy_from_slice(model);
        data[6 + model.len()] = 0x00;

        // HW version after model + null padding: "19A\0"
        let hw = b"19A";
        let hw_start = 6 + model.len() + 1; // after model null
        data[hw_start..hw_start + hw.len()].copy_from_slice(hw);
        data[hw_start + hw.len()] = 0x00;

        // SW version after hw + null padding: "19.07\0"
        let sw = b"19.07";
        let sw_start = hw_start + hw.len() + 1;
        data[sw_start..sw_start + sw.len()].copy_from_slice(sw);
        data[sw_start + sw.len()] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_info);
        assert_eq!(pack.model, "JK_PB2A16S20P", "model should be extracted from info frame");
        assert_eq!(pack.hwvers, "19A", "hwvers should be extracted from info frame");
        assert_eq!(pack.swvers, "19.07", "swvers should be extracted from info frame");
        assert_eq!(pack.protocol_version, ProtocolVersion::Jk02_32S);
    }

    // --- JK02_24S with 0V cells (unused cell slots) ---

    #[test]
    fn test_getdata_jk02_24s_zero_voltage_cells_ignored() {
        let mut data = vec![0u8; 400];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x02;

        // Cell 1: 3315mV, Cell 2: 0mV (unused), Cell 3: 3320mV
        data[6] = 0xF3; data[7] = 0x0C;   // 3315
        data[8] = 0x00; data[9] = 0x00;    // 0 — unused cell
        data[10] = 0xF8; data[11] = 0x0C;  // 3320

        // Voltage at 118
        data[118] = 0x44; data[119] = 0xCF; data[120] = 0x00; data[121] = 0x00;

        // Current at 126
        data[126] = 0x00; data[127] = 0x00; data[128] = 0x00; data[129] = 0x00;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        let _ = getdata(&mut pack, &data);

        // Only 2 cells enabled (0V cell is skipped in count)
        assert_eq!(pack.cells, 2);
        assert!((pack.cellvolt[0] - 3.315).abs() < 0.001);
        // Cell 2 is stored as 0.0 in the array but not counted as enabled
        assert!((pack.cellvolt[2] - 3.320).abs() < 0.001);
        // cell_min/cell_max should only consider non-zero cells
        assert!((pack.cell_min - 3.315).abs() < 0.001);
        assert!((pack.cell_max - 3.320).abs() < 0.001);
    }

    // =========================================================================
    // Settings tests
    // =========================================================================

    /// Helper: build a JK02_24S settings frame with known values
    fn make_jk02_24s_settings_frame() -> Vec<u8> {
        let mut data = vec![0u8; 300];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x01; // settings frame type

        // Using get_32bit byte ordering: [lo16_lo, lo16_hi, hi16_lo, hi16_hi]
        // where get_16bit = (data[i+1]<<8) | data[i]

        // 6: Smart sleep voltage = 0.003 V = 3 → 0x00000003
        data[6] = 0x03; data[7] = 0x00; data[8] = 0x00; data[9] = 0x00;

        // 10: Cell UVP = 2.800 V = 2800 → 0x00000AF0
        data[10] = 0xF0; data[11] = 0x0A; data[12] = 0x00; data[13] = 0x00;

        // 14: Cell UVPR = 3.000 V = 3000 → 0x00000BB8
        data[14] = 0xB8; data[15] = 0x0B; data[16] = 0x00; data[17] = 0x00;

        // 18: Cell OVP = 3.650 V = 3650 → 0x00000E42
        data[18] = 0x42; data[19] = 0x0E; data[20] = 0x00; data[21] = 0x00;

        // 22: Cell OVPR = 3.600 V = 3600 → 0x00000E10
        data[22] = 0x10; data[23] = 0x0E; data[24] = 0x00; data[25] = 0x00;

        // 26: Balance trigger voltage = 0.010 V = 10 → 0x0000000A
        data[26] = 0x0A; data[27] = 0x00; data[28] = 0x00; data[29] = 0x00;

        // 46: Power off voltage = 2.800 V = 2800 → 0x00000AF0
        data[46] = 0xF0; data[47] = 0x0A; data[48] = 0x00; data[49] = 0x00;

        // 50: Max charge current = 50.0 A = 50000 → 0x0000C350
        data[50] = 0x50; data[51] = 0xC3; data[52] = 0x00; data[53] = 0x00;

        // 54: Charge OCP delay = 30 s → 0x0000001E
        data[54] = 0x1E; data[55] = 0x00; data[56] = 0x00; data[57] = 0x00;

        // 58: Charge OCP recovery = 60 s → 0x0000003C
        data[58] = 0x3C; data[59] = 0x00; data[60] = 0x00; data[61] = 0x00;

        // 62: Max discharge current = 100.0 A = 100000 → 0x000186A0
        data[62] = 0xA0; data[63] = 0x86; data[64] = 0x01; data[65] = 0x00;

        // 66: Discharge OCP delay = 300 s → 0x0000012C
        data[66] = 0x2C; data[67] = 0x01; data[68] = 0x00; data[69] = 0x00;

        // 70: Discharge OCP recovery = 60 s → 0x0000003C
        data[70] = 0x3C; data[71] = 0x00; data[72] = 0x00; data[73] = 0x00;

        // 74: SCP recovery = 60 s → 0x0000003C
        data[74] = 0x3C; data[75] = 0x00; data[76] = 0x00; data[77] = 0x00;

        // 78: Max balance current = 0.5 A = 500 → 0x000001F4
        data[78] = 0xF4; data[79] = 0x01; data[80] = 0x00; data[81] = 0x00;

        // 82: Charge OTP = 65.0°C = 650 → 0x0000028A
        data[82] = 0x8A; data[83] = 0x02; data[84] = 0x00; data[85] = 0x00;

        // 86: Charge OTP recovery = 55.0°C = 550 → 0x00000226
        data[86] = 0x26; data[87] = 0x02; data[88] = 0x00; data[89] = 0x00;

        // 90: Discharge OTP = 65.0°C = 650 → 0x0000028A
        data[90] = 0x8A; data[91] = 0x02; data[92] = 0x00; data[93] = 0x00;

        // 94: Discharge OTP recovery = 55.0°C = 550 → 0x00000226
        data[94] = 0x26; data[95] = 0x02; data[96] = 0x00; data[97] = 0x00;

        // 98: Charge UTP = -20.0°C = -200 → 0xFFFFFF38 (signed)
        data[98] = 0x38; data[99] = 0xFF; data[100] = 0xFF; data[101] = 0xFF;

        // 102: Charge UTP recovery = -10.0°C = -100 → 0xFFFFFF9C
        data[102] = 0x9C; data[103] = 0xFF; data[104] = 0xFF; data[105] = 0xFF;

        // 106: MOSFET OTP = 85.0°C = 850 → 0x00000352
        data[106] = 0x52; data[107] = 0x03; data[108] = 0x00; data[109] = 0x00;

        // 110: MOSFET OTP recovery = 70.0°C = 700 → 0x000002BC
        data[110] = 0xBC; data[111] = 0x02; data[112] = 0x00; data[113] = 0x00;

        // 114: Cell count = 16
        data[114] = 16;

        // 118: Charge switch = on
        data[118] = 0x01;

        // 122: Discharge switch = on
        data[122] = 0x01;

        // 126: Balancer switch = on
        data[126] = 0x01;

        // 130: Battery capacity = 310.0 Ah = 310000 → 0x0004BAF0
        data[130] = 0xF0; data[131] = 0xBA; data[132] = 0x04; data[133] = 0x00;

        // 134: SCP delay = 10000 μs → 0x00002710
        data[134] = 0x10; data[135] = 0x27; data[136] = 0x00; data[137] = 0x00;

        // 138: Start balance voltage = 3.400 V = 3400 → 0x00000D48
        data[138] = 0x48; data[139] = 0x0D; data[140] = 0x00; data[141] = 0x00;

        finalize_frame(&mut data);
        data
    }

    #[test]
    fn test_parse_jk02_24s_settings() {
        let data = make_jk02_24s_settings_frame();

        let mut pack = MybmmPack::new("test");
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_res, "should detect settings frame (type 0x01)");
        assert!(!flags.got_volts);
        assert!(!flags.got_info);

        let s = pack.settings.as_ref().expect("settings should be populated");

        assert!((s.smart_sleep_voltage - 0.003).abs() < 0.001);
        assert!((s.cell_uvp - 2.800).abs() < 0.001);
        assert!((s.cell_uvpr - 3.000).abs() < 0.001);
        assert!((s.cell_ovp - 3.650).abs() < 0.001);
        assert!((s.cell_ovpr - 3.600).abs() < 0.001);
        assert!((s.balance_trigger_voltage - 0.010).abs() < 0.001);
        assert!((s.power_off_voltage - 2.800).abs() < 0.001);
        assert!((s.max_charge_current - 50.0).abs() < 0.01);
        assert!((s.charge_ocp_delay - 30.0).abs() < 0.1);
        assert!((s.charge_ocp_recovery - 60.0).abs() < 0.1);
        assert!((s.max_discharge_current - 100.0).abs() < 0.01);
        assert!((s.discharge_ocp_delay - 300.0).abs() < 0.1);
        assert!((s.discharge_ocp_recovery - 60.0).abs() < 0.1);
        assert!((s.scp_recovery - 60.0).abs() < 0.1);
        assert!((s.max_balance_current - 0.5).abs() < 0.01);
        assert!((s.charge_otp - 65.0).abs() < 0.1);
        assert!((s.charge_otp_recovery - 55.0).abs() < 0.1);
        assert!((s.discharge_otp - 65.0).abs() < 0.1);
        assert!((s.discharge_otp_recovery - 55.0).abs() < 0.1);
        assert!((s.charge_utp - (-20.0)).abs() < 0.1, "charge_utp: got {}", s.charge_utp);
        assert!((s.charge_utp_recovery - (-10.0)).abs() < 0.1);
        assert!((s.power_tube_otp - 85.0).abs() < 0.1);
        assert!((s.power_tube_otp_recovery - 70.0).abs() < 0.1);
        assert_eq!(s.cell_count, 16);
        assert!(s.charging_switch);
        assert!(s.discharging_switch);
        assert!(s.balancer_switch);
        assert!((s.total_battery_capacity - 310.0).abs() < 0.01);
        assert!((s.scp_delay - 10000.0).abs() < 0.1);
        assert!((s.balance_starting_voltage - 3.400).abs() < 0.001);
    }

    #[test]
    fn test_parse_jk02_32s_settings() {
        let mut data = vec![0u8; 300];
        data[0] = 0x55; data[1] = 0xAA; data[2] = 0xEB; data[3] = 0x90;
        data[4] = 0x01; // settings frame

        // Same offsets as JK02_24S for the common fields
        // Cell OVP = 4.200 V = 4200 → 0x00001068
        data[18] = 0x68; data[19] = 0x10; data[20] = 0x00; data[21] = 0x00;

        // Cell count = 16
        data[114] = 16;

        // Battery capacity = 200.0 Ah = 200000 → 0x00030D40
        data[130] = 0x40; data[131] = 0x0D; data[132] = 0x03; data[133] = 0x00;

        // 142-269: Wire resistances 1-32 (all zero, already)

        // 274: Precharge time = 30 s
        data[274] = 30;

        // 282: Controls bitmask: heating(1) + display_always_on(16) + smart_sleep(64) = 81 = 0x51
        data[282] = 0x51;
        // 283: timed_stored_data(1) = 0x01
        data[283] = 0x01;

        // 284: Heating start temperature = -5°C (i8)
        data[284] = (-5i8) as u8;
        // 285: Heating stop temperature = 5°C
        data[285] = 5;

        // 296: Discharge UTP = -20°C (i8)
        data[296] = (-20i8) as u8;
        // 297: Discharge UTP recovery = -10°C
        data[297] = (-10i8) as u8;

        finalize_frame(&mut data);

        let mut pack = MybmmPack::new("test");
        pack.protocol_version = ProtocolVersion::Jk02_32S;
        let flags = getdata(&mut pack, &data);

        assert!(flags.got_res);
        let s = pack.settings.as_ref().expect("settings should be populated");

        assert!((s.cell_ovp - 4.200).abs() < 0.001);
        assert_eq!(s.cell_count, 16);
        assert!((s.total_battery_capacity - 200.0).abs() < 0.01);
        assert_eq!(s.discharge_precharge_time, 30);
        assert!(s.heating_switch, "heating switch should be on");
        assert!(!s.disable_temp_sensors, "disable_temp_sensors should be off");
        assert!(s.display_always_on, "display_always_on should be on");
        assert!(s.smart_sleep_switch, "smart_sleep should be on");
        assert!(!s.disable_pcl_module, "disable_pcl_module should be off");
        assert!(s.timed_stored_data, "timed_stored_data should be on");
        assert!(!s.charging_float_mode, "charging_float_mode should be off");
        assert!((s.heating_start_temperature - (-5.0)).abs() < 0.1);
        assert!((s.heating_stop_temperature - 5.0).abs() < 0.1);
        assert!((s.discharge_utp - (-20.0)).abs() < 0.1);
        assert!((s.discharge_utp_recovery - (-10.0)).abs() < 0.1);
    }

    #[test]
    fn test_build_write_frame() {
        // Write max_charge_current = 50.0 A to register 0x0C (JK02_24S)
        // factor = 1000, so raw value = 50000 = 0x0000C350
        let frame = build_write_frame(0x0C, 50000, 4);

        assert_eq!(frame[0], 0xAA, "write header byte 0");
        assert_eq!(frame[1], 0x55, "write header byte 1");
        assert_eq!(frame[2], 0x90, "write header byte 2");
        assert_eq!(frame[3], 0xEB, "write header byte 3");
        assert_eq!(frame[4], 0x0C, "register");
        assert_eq!(frame[5], 4, "length");
        assert_eq!(frame[6], 0x50, "value byte 0");
        assert_eq!(frame[7], 0xC3, "value byte 1");
        assert_eq!(frame[8], 0x00, "value byte 2");
        assert_eq!(frame[9], 0x00, "value byte 3");
        // bytes 10-18 should be zero
        for i in 10..19 {
            assert_eq!(frame[i], 0, "padding byte {} should be zero", i);
        }
        // CRC at byte 19
        let expected_crc = crate::protocol::crc(&frame, 19);
        assert_eq!(frame[19], expected_crc, "CRC should match");
    }

    #[test]
    fn test_build_setting_write_frame_numeric() {
        // Write max_charge_current = 50.0 A for JK02_24S
        let frame = build_setting_write_frame("max_charge_current", "50.0", ProtocolVersion::Jk02_24S)
            .expect("should build frame");
        assert_eq!(frame[4], 0x0C, "register should be 0x0C for JK02_24S");

        // Raw value = 50.0 * 1000 = 50000
        assert_eq!(frame[6], 0x50);
        assert_eq!(frame[7], 0xC3);
    }

    #[test]
    fn test_build_setting_write_frame_switch() {
        // Write charging = on for JK02_32S
        let frame = build_setting_write_frame("charging", "on", ProtocolVersion::Jk02_32S)
            .expect("should build frame");
        assert_eq!(frame[4], 0x1D, "register should be 0x1D for JK02_32S charging");
        assert_eq!(frame[6], 0x01, "value should be 1 for on");

        // Write charging = off
        let frame = build_setting_write_frame("charging", "off", ProtocolVersion::Jk02_32S)
            .expect("should build frame");
        assert_eq!(frame[6], 0x00, "value should be 0 for off");
    }

    #[test]
    fn test_build_setting_write_frame_unsupported() {
        // discharge_utp is not supported on JK02_24S (register = 0)
        let result = build_setting_write_frame("discharge_utp", "-20", ProtocolVersion::Jk02_24S);
        assert!(result.is_none(), "should return None for unsupported setting");
    }

    #[test]
    fn test_get_setting_register() {
        assert_eq!(get_setting_register("max_charge_current", ProtocolVersion::Jk02_24S), Some(0x0C));
        assert_eq!(get_setting_register("max_charge_current", ProtocolVersion::Jk02_32S), Some(0x0C));
        assert_eq!(get_setting_register("heating", ProtocolVersion::Jk02_24S), None); // not supported
        assert_eq!(get_setting_register("heating", ProtocolVersion::Jk02_32S), Some(0x27));
        assert_eq!(get_setting_register("balancer", ProtocolVersion::Jk04), Some(0x6C));
        assert_eq!(get_setting_register("nonexistent", ProtocolVersion::Jk02_32S), None);
    }

    #[test]
    fn test_get_setting_def() {
        let def = get_setting_def("max_charge_current").expect("should find setting");
        assert_eq!(def.name, "max_charge_current");
        assert_eq!(def.unit, "A");
        assert_eq!(def.factor, 1000.0);
        assert_eq!(def.length, 4);
        assert!(!def.is_switch);

        let switch_def = get_setting_def("charging").expect("should find switch setting");
        assert!(switch_def.is_switch);
    }

    #[test]
    fn test_get_settings_command() {
        let cmd = get_settings_command();
        assert_eq!(cmd[0], 0xaa);
        assert_eq!(cmd[1], 0x55);
        assert_eq!(cmd[2], 0x90);
        assert_eq!(cmd[3], 0xeb);
        assert_eq!(cmd[4], 0x96);
    }

    #[test]
    fn test_settings_frame_raw_data_saved() {
        let data = make_jk02_24s_settings_frame();
        let mut pack = MybmmPack::new("test");
        let _ = getdata(&mut pack, &data);
        let s = pack.settings.as_ref().expect("settings should be populated");
        assert_eq!(s.raw_frame.len(), 300);
        // First 4 bytes should be the signature
        assert_eq!(s.raw_frame[0], 0x55);
        assert_eq!(s.raw_frame[1], 0xAA);
    }
}