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;
#[derive(Debug, Clone)]
pub struct Discovered {
pub name: String,
pub address: String,
pub id: String,
pub rssi: Option<i16>,
pub model: Model,
peripheral: Peripheral,
}
type NotifStream = Pin<Box<dyn Stream<Item = btleplug::api::ValueNotification> + Send>>;
pub struct Device {
peripheral: Peripheral,
cmd_char: btleplug::api::Characteristic,
notifications: NotifStream,
model: Model,
name: String,
shared_secret: [u8; 32],
negotiated_at: Instant,
fragmenter: Fragmenter,
secure_key: Option<[u8; 16]>,
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")
}
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();
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)
}
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 {
pub async fn connect(self) -> Result<Device> {
Device::connect(self.peripheral, self.name, self.model).await
}
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
}
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
}
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)
}
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)
}
async fn write_raw(&mut self, bytes: &[u8]) -> Result<()> {
self.peripheral
.write(&self.cmd_char, bytes, WriteType::WithResponse)
.await?;
Ok(())
}
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(¬if.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(());
}
}
}
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] => {
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)
}
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(×tamp.to_le_bytes());
let ciphertext = encrypt(&self.shared_secret, &full);
let packet = build_packet(PATTERN_ENCRYPTED_TX, cmd, &ciphertext);
self.write_raw(&packet).await
}
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(¬if.value) {
Ok(p) => return Ok(p),
Err(e) => {
log::debug!("ignoring malformed packet: {e}");
continue;
}
}
}
}
pub async fn negotiate_secure(&mut self) -> Result<()> {
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);
}
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?;
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)?;
let key: [u8; 16] = self.shared_secret[..16].try_into().unwrap();
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);
if std::env::var("ANKER_FINALIZE").is_err() {
return Ok(());
}
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(())
}
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
}
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
}
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?;
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);
}
}
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(¬if.value)? {
return Ok(t);
}
}
}
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);
}
if packet.cmd == [0x03, 0x00] {
let params = parse_params(&packet.payload);
return Ok(Some(Telemetry::from_params(self.model, ¶ms)));
}
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(¶ms[&k]));
}
eprintln!("---");
}
return Ok(Some(Telemetry::from_params(self.model, ¶ms)));
}
} else {
log::debug!("unhandled session cmd {:02x?}", packet.cmd);
}
Ok(None)
}
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
}
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
}
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
}
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
}
pub async fn disconnect(&self) -> Result<()> {
self.peripheral.disconnect().await?;
Ok(())
}
}