anker_solix 0.4.0

Library and CLI for monitoring/controlling Anker SOLIX power stations over Bluetooth LE
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
//! Async BLE session with a SOLIX power station: discovery, the ECDH
//! negotiation handshake, encrypted command transmission and telemetry receive.

use crate::crypto::{
    decrypt, derive_shared_secret, encrypt, gcm_decrypt_noverify, gcm_decrypt_noverify_nonce, gcm_encrypt, gcm_encrypt_nonce, SECURE_KEY,
};
use crate::error::{AnkerError, Result};
use crate::model::{Model, Telemetry};
use crate::protocol::*;
use btleplug::api::{Central, Manager as _, Peripheral as _, ScanFilter, WriteType};
use btleplug::platform::{Adapter, Manager, Peripheral};
use futures_util::{Stream, StreamExt};
use std::pin::Pin;
use std::time::{Duration, Instant};
use uuid::Uuid;

/// A discovered SOLIX device advertisement.
#[derive(Debug, Clone)]
pub struct Discovered {
    pub name: String,
    /// The BLE MAC address. **Note:** macOS does not expose the MAC and reports
    /// `00:00:00:00:00:00`; use [`Discovered::id`] for a stable identifier.
    pub address: String,
    /// A stable, per-host hardware identifier: the MAC on Linux, or the
    /// CoreBluetooth peripheral UUID on macOS (where the MAC is unavailable).
    /// Unique per device even when names collide.
    pub id: String,
    pub rssi: Option<i16>,
    pub model: Model,
    peripheral: Peripheral,
}

type NotifStream = Pin<Box<dyn Stream<Item = btleplug::api::ValueNotification> + Send>>;

/// A negotiated session with a power station.
pub struct Device {
    peripheral: Peripheral,
    cmd_char: btleplug::api::Characteristic,
    notifications: NotifStream,
    model: Model,
    name: String,
    shared_secret: [u8; 32],
    /// Reference instant for the replay-protection timestamp (set at stage 3).
    negotiated_at: Instant,
    fragmenter: Fragmenter,
    /// Session AES-128-GCM key once the *secure* (gen-2 settings) channel is
    /// negotiated. Present only after [`Device::negotiate_secure`].
    secure_key: Option<[u8; 16]>,
    /// Session GCM nonce (derived from the ECDH secret post-handshake).
    secure_nonce: [u8; 12],
}

async fn first_adapter() -> Result<Adapter> {
    let manager = Manager::new().await?;
    let adapters = manager.adapters().await?;
    adapters.into_iter().next().ok_or(AnkerError::NoAdapter)
}

fn uuid(s: &str) -> Uuid {
    Uuid::parse_str(s).expect("static UUID is valid")
}

/// Scan for nearby SOLIX / Prime devices for `secs` seconds.
pub async fn scan(secs: u64) -> Result<Vec<Discovered>> {
    let adapter = first_adapter().await?;
    adapter.start_scan(ScanFilter::default()).await?;
    tokio::time::sleep(Duration::from_secs(secs)).await;

    let identifier = uuid(UUID_IDENTIFIER);
    let mut found = Vec::new();
    for p in adapter.peripherals().await? {
        let props = match p.properties().await? {
            Some(props) => props,
            None => continue,
        };
        let advertises_anker = props.services.contains(&identifier);
        let name = props.local_name.clone().unwrap_or_default();
        // Some stacks don't surface the service UUID; fall back to name hints.
        let looks_anker = advertises_anker
            || name.to_ascii_uppercase().contains("ANKER")
            || name.to_ascii_uppercase().contains("SOLIX");
        if !looks_anker {
            continue;
        }
        let address = p.address().to_string();
        found.push(Discovered {
            model: Model::detect(&name),
            name,
            id: stable_id(&address, &p),
            address,
            rssi: props.rssi,
            peripheral: p,
        });
    }
    let _ = adapter.stop_scan().await;
    Ok(found)
}

/// Compute a stable hardware id: prefer a real MAC, else the platform peripheral
/// id (a UUID on macOS), stripped of btleplug's `PeripheralId(...)` wrapper.
fn stable_id(address: &str, peripheral: &Peripheral) -> String {
    if !address.is_empty() && address != "00:00:00:00:00:00" {
        return address.to_string();
    }
    let dbg = format!("{:?}", btleplug::api::Peripheral::id(peripheral));
    dbg.trim_start_matches("PeripheralId")
        .trim_start_matches('(')
        .trim_end_matches(')')
        .trim_matches('"')
        .to_string()
}

impl Discovered {
    /// Connect and run the full encryption handshake, returning a ready session.
    ///
    /// Connects to the exact discovered peripheral, so it works even on macOS
    /// where the MAC address is unavailable.
    pub async fn connect(self) -> Result<Device> {
        Device::connect(self.peripheral, self.name, self.model).await
    }

    /// Connect using the secure (AES-128-GCM) channel for settings writes.
    pub async fn connect_secure(self) -> Result<Device> {
        Device::connect_secure(self.peripheral, self.name, self.model).await
    }
}

impl Device {
    pub fn model(&self) -> Model {
        self.model
    }
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Find a device by name substring (case-insensitive) or MAC address and connect.
    pub async fn find_and_connect(target: &str, scan_secs: u64) -> Result<Device> {
        let want = target.to_ascii_lowercase();
        let devices = scan(scan_secs).await?;
        let chosen = devices
            .into_iter()
            .find(|d| {
                d.address.to_ascii_lowercase() == want
                    || d.name.to_ascii_lowercase().contains(&want)
            })
            .ok_or_else(|| AnkerError::DeviceNotFound(target.to_string()))?;
        chosen.connect().await
    }

    /// Like [`Device::find_and_connect`] but negotiates the secure GCM channel.
    pub async fn find_and_connect_secure(target: &str, scan_secs: u64) -> Result<Device> {
        let want = target.to_ascii_lowercase();
        let chosen = scan(scan_secs)
            .await?
            .into_iter()
            .find(|d| {
                d.address.to_ascii_lowercase() == want
                    || d.name.to_ascii_lowercase().contains(&want)
            })
            .ok_or_else(|| AnkerError::DeviceNotFound(target.to_string()))?;
        chosen.connect_secure().await
    }

    async fn connect(peripheral: Peripheral, name: String, model: Model) -> Result<Device> {
        let mut dev = Self::setup(peripheral, name, model).await?;
        dev.negotiate().await?;
        if let Some((cmd, payload)) = dev.model.subscribe_command() {
            dev.send_command(cmd, payload).await?;
        }
        Ok(dev)
    }

    /// Connect and negotiate the **secure** (AES-128-GCM) channel instead of the
    /// basic one — required for gen-2 settings writes.
    pub async fn connect_secure(
        peripheral: Peripheral,
        name: String,
        model: Model,
    ) -> Result<Device> {
        let mut dev = Self::setup(peripheral, name, model).await?;
        dev.negotiate_secure().await?;
        if let Some((cmd, payload)) = dev.model.subscribe_command() {
            dev.send_secure(cmd, payload).await?;
        }
        Ok(dev)
    }

    async fn setup(peripheral: Peripheral, name: String, model: Model) -> Result<Device> {
        if !peripheral.is_connected().await? {
            peripheral.connect().await?;
        }
        peripheral.discover_services().await?;

        let chars = peripheral.characteristics();
        let cmd_char = chars
            .iter()
            .find(|c| c.uuid == uuid(UUID_COMMAND))
            .cloned()
            .ok_or(AnkerError::CharacteristicNotFound("command"))?;
        let telemetry_char = chars
            .iter()
            .find(|c| c.uuid == uuid(UUID_TELEMETRY))
            .cloned()
            .ok_or(AnkerError::CharacteristicNotFound("telemetry"))?;

        peripheral.subscribe(&telemetry_char).await?;
        let notifications = peripheral.notifications().await?;

        let mut dev = Device {
            peripheral,
            cmd_char,
            notifications,
            model,
            name,
            shared_secret: [0u8; 32],
            negotiated_at: Instant::now(),
            fragmenter: Fragmenter::new(),
            secure_key: None,
            secure_nonce: crate::crypto::SECURE_NONCE,
        };

        Ok(dev)
    }

    // Takes `&mut self` (not `&self`) so the returned future captures `&mut
    // Device` rather than `&Device`; the latter would require `Device: Sync`,
    // which fails because the boxed notification stream is `Send` but not `Sync`.
    async fn write_raw(&mut self, bytes: &[u8]) -> Result<()> {
        self.peripheral
            .write(&self.cmd_char, bytes, WriteType::WithResponse)
            .await?;
        Ok(())
    }

    /// Run the fixed ECDH handshake until an encrypted session is established.
    async fn negotiate(&mut self) -> Result<()> {
        self.write_raw(&hex::decode(NEGOTIATION_COMMAND_0).unwrap())
            .await?;

        let overall = Duration::from_secs(90);
        let start = Instant::now();

        loop {
            if start.elapsed() > overall {
                return Err(AnkerError::NegotiationTimeout);
            }
            let notif = match tokio::time::timeout(
                Duration::from_secs(15),
                self.notifications.next(),
            )
            .await
            {
                Ok(Some(n)) => n,
                Ok(None) => return Err(AnkerError::NegotiationTimeout),
                Err(_) => return Err(AnkerError::NegotiationTimeout),
            };

            let packet = match split_packet(&notif.value) {
                Ok(p) => p,
                Err(e) => {
                    log::debug!("ignoring malformed packet during negotiation: {e}");
                    continue;
                }
            };

            if packet.pattern != PATTERN_NEGOTIATION {
                log::debug!("non-negotiation packet {:02x?} ignored", packet.pattern);
                continue;
            }

            if self.handle_negotiation_stage(&packet).await? {
                log::info!("encrypted session negotiated with '{}'", self.name);
                return Ok(());
            }
        }
    }

    /// Handle one negotiation packet. Returns `true` once the session is ready.
    async fn handle_negotiation_stage(&mut self, packet: &Packet) -> Result<bool> {
        match packet.cmd {
            [0x08, 0x01] => {
                self.write_raw(&hex::decode(NEGOTIATION_COMMAND_1).unwrap())
                    .await?;
            }
            [0x08, 0x03] => {
                self.write_raw(&hex::decode(NEGOTIATION_COMMAND_2).unwrap())
                    .await?;
            }
            [0x08, 0x29] => {
                // Timestamp reference for replay protection is set here.
                self.negotiated_at = Instant::now();
                self.write_raw(&hex::decode(NEGOTIATION_COMMAND_3).unwrap())
                    .await?;
            }
            [0x08, 0x05] => {
                self.write_raw(&hex::decode(NEGOTIATION_COMMAND_4).unwrap())
                    .await?;
            }
            [0x08, 0x21] => {
                let params = parse_params(&packet.payload);
                let pubkey = params
                    .get("a1")
                    .ok_or_else(|| AnkerError::Crypto("stage 5 missing device pubkey".into()))?;
                self.shared_secret = derive_shared_secret(pubkey)?;
                self.write_raw(&hex::decode(NEGOTIATION_COMMAND_5).unwrap())
                    .await?;
                return Ok(true);
            }
            other => {
                log::debug!("unexpected negotiation cmd {:02x?}", other);
            }
        }
        Ok(false)
    }

    /// Send an encrypted command. The replay-protection timestamp and trailer
    /// are appended before encryption.
    ///
    /// Takes `&mut self` so that the returned future is `Send` even though the
    /// notification stream is not `Sync`; this lets `Device` be driven from
    /// `Send` async contexts (e.g. trait objects).
    pub async fn send_command(&mut self, cmd: [u8; 2], payload: &[u8]) -> Result<()> {
        let elapsed = self.negotiated_at.elapsed().as_secs() as u32;
        let timestamp = BASE_TIMESTAMP_LE.wrapping_add(elapsed);

        let mut full = Vec::with_capacity(payload.len() + 7);
        full.extend_from_slice(payload);
        full.extend_from_slice(&[0xfe, 0x05, 0x03]);
        full.extend_from_slice(&timestamp.to_le_bytes());

        let ciphertext = encrypt(&self.shared_secret, &full);
        let packet = build_packet(PATTERN_ENCRYPTED_TX, cmd, &ciphertext);
        self.write_raw(&packet).await
    }

    /// Read the next framed packet from the notification stream (any pattern).
    pub async fn recv_packet(&mut self, timeout: Duration) -> Result<Packet> {
        let deadline = Instant::now() + timeout;
        loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                return Err(AnkerError::ResponseTimeout);
            }
            let notif = match tokio::time::timeout(remaining, self.notifications.next()).await {
                Ok(Some(n)) => n,
                _ => return Err(AnkerError::ResponseTimeout),
            };
            match split_packet(&notif.value) {
                Ok(p) => return Ok(p),
                Err(e) => {
                    log::debug!("ignoring malformed packet: {e}");
                    continue;
                }
            }
        }
    }

    /// Negotiate the gen-2 **secure** (AES-128-GCM) channel required for
    /// settings writes (charge/discharge limits, etc.).
    ///
    /// Mirrors the app's `SecureEcdhConfer`: the ECDH handshake frames (cmd
    /// `40xx`) are GCM-encrypted with the hardcoded bootstrap key; after the
    /// pubkey exchange the session key = ECDH shared secret, used for all
    /// subsequent `send_secure` commands.
    pub async fn negotiate_secure(&mut self) -> Result<()> {
        // Handshake step plaintexts recovered from the app (guest / no-login).
        let steps: &[(u16, &str)] = &[
            (0x4001, "a1047de5606a"),
            (0x4003, "a1047de5606aa30120a40200f0"),
            (0x4029, "a1047de5606a"),
            (0x4005, "a1047ee5606aa30120a402fd00a50144a60102"),
        ];
        for (cmd, pt_hex) in steps {
            self.send_secure_neg(*cmd, &hex::decode(pt_hex).unwrap()).await?;
            let _resp = self.recv_packet(Duration::from_secs(10)).await?;
            log::debug!("secure handshake {cmd:#06x} -> resp {:02x?}", _resp.cmd);
        }

        // Send our ECDH public key (cmd 4021), read the device's pubkey (4821).
        let pubkey = crate::crypto::client_public_key();
        let mut body = vec![0xa1, 0x40];
        body.extend_from_slice(&pubkey);
        self.send_secure_neg(0x4021, &body).await?;

        let resp = self.recv_packet(Duration::from_secs(10)).await?;
        // Device response is CTR-recoverable (its GCM tag uses a different key).
        let pt = gcm_decrypt_noverify(&SECURE_KEY, &resp.payload);
        let params = parse_params(pt.strip_prefix(&[0x00]).unwrap_or(&pt));
        let dev_pub = params
            .get("a1")
            .ok_or_else(|| AnkerError::Crypto("secure stage missing device pubkey".into()))?;
        self.shared_secret = derive_shared_secret(dev_pub)?;
        // Session GCM key = low 16 bytes of the ECDH shared secret (same split
        // as the basic CBC channel); nonce + AAD stay the secure constants.
        let key: [u8; 16] = self.shared_secret[..16].try_into().unwrap();
        // Session nonce derived from the ECDH secret (mirrors the CBC iv split,
        // truncated to 12 bytes for GCM).
        self.secure_nonce = self.shared_secret[16..28].try_into().unwrap();
        self.secure_key = Some(key);
        self.negotiated_at = Instant::now();
        log::info!("secure GCM session established with '{}'", self.name);

        // Finalization / authentication (cmd 4022 then 4027, session-key GCM on
        // the negotiation channel). The device gates these on a physical
        // power-button press (proximity auth), so the response can take a while.
        if std::env::var("ANKER_FINALIZE").is_err() {
            return Ok(());
        }
        // Poll the auth request (4022) repeatedly so we can watch the status
        // change when the power button is pressed.
        log::warn!(">>> PRESS THE POWER BUTTON on the device now (watching auth status) <<<");
        for i in 0..15 {
            let ct = gcm_encrypt_nonce(&key, &self.secure_nonce, &hex::decode("a10121").unwrap());
            let packet = build_packet(PATTERN_NEGOTIATION, [0x40, 0x22], &ct);
            if self.write_raw(&packet).await.is_err() {
                log::warn!("auth poll {i}: write failed (disconnected)");
                break;
            }
            match self.recv_packet(Duration::from_secs(3)).await {
                Ok(r) => {
                    let pt = gcm_decrypt_noverify_nonce(&key, &self.secure_nonce, &r.payload);
                    log::info!("auth poll {i}: cmd={:02x?} status={}", r.cmd, hex::encode(&pt));
                }
                Err(_) => log::info!("auth poll {i}: (no response)"),
            }
            tokio::time::sleep(Duration::from_secs(2)).await;
        }
        Ok(())
    }

    /// Build + send a GCM negotiation frame (pattern `030001`) with the
    /// hardcoded bootstrap key.
    async fn send_secure_neg(&mut self, cmd: u16, plaintext: &[u8]) -> Result<()> {
        let ct = gcm_encrypt(&SECURE_KEY, plaintext);
        let packet = build_packet(PATTERN_NEGOTIATION, cmd.to_be_bytes(), &ct);
        self.write_raw(&packet).await
    }

    /// Send an encrypted command over the **secure** GCM session (call
    /// [`Device::negotiate_secure`] first). Payload is GCM-encrypted with the
    /// session key.
    pub async fn send_secure(&mut self, cmd: [u8; 2], payload: &[u8]) -> Result<()> {
        let key = self
            .secure_key
            .ok_or_else(|| AnkerError::Crypto("secure session not negotiated".into()))?;
        let ct = gcm_encrypt_nonce(&key, &self.secure_nonce, payload);
        let packet = build_packet(PATTERN_ENCRYPTED_TX, cmd, &ct);
        self.write_raw(&packet).await
    }

    /// Send a secure command and return the device's decrypted response
    /// `(cmd, plaintext)` (the ack echoes cmd with the `0x08` response bit).
    pub async fn send_secure_recv(
        &mut self,
        cmd: [u8; 2],
        payload: &[u8],
        timeout: Duration,
    ) -> Result<([u8; 2], Vec<u8>)> {
        let key = self
            .secure_key
            .ok_or_else(|| AnkerError::Crypto("secure session not negotiated".into()))?;
        self.send_secure(cmd, payload).await?;
        // The ack echoes our cmd with the 0x08 response bit set on the high byte.
        let want = [cmd[0] | 0x08, cmd[1]];
        let deadline = Instant::now() + timeout;
        loop {
            let resp = self
                .recv_packet(deadline.saturating_duration_since(Instant::now()))
                .await?;
            if resp.cmd == want {
                return Ok((resp.cmd, gcm_decrypt_noverify_nonce(&key, &self.secure_nonce, &resp.payload)));
            }
            log::debug!("skip non-ack {:02x?} waiting for {:02x?}", resp.cmd, want);
        }
    }

    /// Wait for the next decoded telemetry snapshot (up to `timeout`).
    pub async fn next_telemetry(&mut self, timeout: Duration) -> Result<Telemetry> {
        let deadline = Instant::now() + timeout;
        loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                return Err(AnkerError::ResponseTimeout);
            }
            let notif =
                match tokio::time::timeout(remaining, self.notifications.next()).await {
                    Ok(Some(n)) => n,
                    _ => return Err(AnkerError::ResponseTimeout),
                };
            if let Some(t) = self.process_session_packet(&notif.value)? {
                return Ok(t);
            }
        }
    }

    /// Decode a session (telemetry) notification, returning telemetry when a
    /// complete, decrypted snapshot is available.
    fn process_session_packet(&mut self, raw: &[u8]) -> Result<Option<Telemetry>> {
        let packet = match split_packet(raw) {
            Ok(p) => p,
            Err(e) => {
                log::debug!("ignoring malformed telemetry packet: {e}");
                return Ok(None);
            }
        };

        log::debug!(
            "rx pattern={:02x?} cmd={:02x?} len={}",
            packet.pattern,
            packet.cmd,
            packet.payload.len()
        );
        let is_session =
            packet.pattern == PATTERN_SESSION_A || packet.pattern == PATTERN_SESSION_B;
        if !is_session {
            return Ok(None);
        }

        // Plaintext telemetry (older firmware).
        if packet.cmd == [0x03, 0x00] {
            let params = parse_params(&packet.payload);
            return Ok(Some(Telemetry::from_params(self.model, &params)));
        }

        // Encrypted telemetry. On the secure channel the device uses its own
        // (GCM) command codes and cipher; accept any session cmd and GCM-decrypt.
        let secure = self.secure_key.is_some();
        if secure || self.model.telemetry_commands().contains(&packet.cmd) {
            if let Some(body) = self.fragmenter.push(packet.cmd, &packet.payload) {
                let plaintext = match self.secure_key {
                    Some(key) => gcm_decrypt_noverify_nonce(&key, &self.secure_nonce, &body),
                    None => decrypt(&self.shared_secret, &body)?,
                };
                let params = parse_params(&plaintext);
                if std::env::var("ANKER_DUMP").is_ok() {
                    let mut keys: Vec<_> = params.keys().cloned().collect();
                    keys.sort();
                    for k in keys {
                        eprintln!("RAW {k} = {}", hex::encode(&params[&k]));
                    }
                    eprintln!("---");
                }
                return Ok(Some(Telemetry::from_params(self.model, &params)));
            }
        } else {
            log::debug!("unhandled session cmd {:02x?}", packet.cmd);
        }
        Ok(None)
    }

    /// Turn the AC output on or off.
    pub async fn set_ac(&mut self, on: bool) -> Result<()> {
        self.send_command(self.model.cmd_ac_output(), crate::model::on_off_payload(on))
            .await
    }

    /// Turn the DC (12 V) output on or off.
    pub async fn set_dc(&mut self, on: bool) -> Result<()> {
        self.send_command(self.model.cmd_dc_output(), crate::model::on_off_payload(on))
            .await
    }

    /// Set the LED light-bar brightness/mode.
    ///
    /// Supported on gen-1 models; the gen-2 command code is not yet known, so
    /// this returns [`AnkerError::UnsupportedModel`] on gen-2.
    pub async fn set_light(&mut self, brightness: crate::model::Brightness) -> Result<()> {
        let cmd = self.model.cmd_light_mode().ok_or_else(|| {
            AnkerError::UnsupportedModel(format!(
                "light control not known for {:?}",
                self.model
            ))
        })?;
        self.send_command(cmd, &crate::model::light_mode_payload(brightness))
            .await
    }

    /// Turn the LCD display on or off.
    ///
    /// Supported on gen-1 models; the gen-2 command code is not yet known, so
    /// this returns [`AnkerError::UnsupportedModel`] on gen-2.
    pub async fn set_display(&mut self, on: bool) -> Result<()> {
        let cmd = self.model.cmd_display_on_off().ok_or_else(|| {
            AnkerError::UnsupportedModel(format!(
                "display control not known for {:?}",
                self.model
            ))
        })?;
        self.send_command(cmd, crate::model::on_off_payload(on)).await
    }

    /// Disconnect from the device.
    pub async fn disconnect(&self) -> Result<()> {
        self.peripheral.disconnect().await?;
        Ok(())
    }
}