Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

46.4 KB
Raw Blame
//! 设备层:一台 iPhone 的全部底层能力,基于 `idevice`。
//!
//! - 发现:usbmuxd `get_devices`;`doctor` 另用 sysfs(Linux)或 IORegistry(macOS)看 USB 上有没有 iPhone/iPad
//! - 配对、设备信息、开发者模式:lockdown / amfi
//! - 隧道:CoreDeviceProxy + jktcp 用户态 TCP 栈(免 root、不建网卡),RSD 握手([`Tunnel`])
//! - 开发者镜像:`cryptexd::installed_ddi` / `install_ddi`(个性化 + TSS);DDI 下载见 [`ddi`]
//! - HID:universalhidservice(触摸)、indigo(按键、键盘);**必须同时持有显示媒体流**([`hid::HidSession`])
//! - 截图:CoreDevice screencaptureservice
//! - 剪贴板:CoreDevice pasteboardservice,HID 粘贴非 ASCII 文字用([`Tunnel::paste_text`])
//! - App:installation_proxy(列出时过滤 `SBAppTags` 含 `hidden` 的后台服务,见 [`apps`])、安装 IPA
//!
//! 不负责:WDA(见 `ictl-wda`)、状态机(见 `ictl-daemon`)。

pub mod apps;
pub mod ax;
mod coredevice;
pub mod ddi;
pub mod hid;
mod media;
mod models;
pub mod oslog;
mod pasteboard;

use std::future::Future;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use idevice::{
    IdeviceError, IdeviceService, RsdService,
    amfi::AmfiClient,
    core_device::{ImageFormat, ScreenCaptureServiceClient},
    core_device_proxy::CoreDeviceProxy,
    crashreportcopymobile::CrashReportCopyMobileClient,
    cryptexd::{Cryptex1Assets, install_ddi, installed_ddi},
    installation_proxy::InstallationProxyClient,
    lockdown::LockdownClient,
    misagent::MisagentClient,
    provider::{IdeviceProvider, UsbmuxdProvider},
    rsd::RsdHandshake,
    springboardservices::{InterfaceOrientation, SpringBoardServicesClient},
    tcp::handle::AdapterHandle,
    usbmuxd::{Connection, UsbmuxdAddr},
};

pub use apps::AppInfo;
pub use hid::{Button, HidSession};
pub use media::{FULL_BITRATE, LOW_BANDWIDTH_BITRATE, ScreenFeed, ScreenFrame, hevc_codec};

/// A running log stream from the phone; see [`Device::log_stream`]
pub struct LogStream(oslog::OsLogStream);

/// One line of the phone's unified log
#[derive(Debug, Clone)]
pub struct LogEntry {
    pub pid: u32,
    /// Path of the process' executable
    pub process: String,
    pub level: LogLevel,
    pub subsystem: Option<String>,
    pub message: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
    Debug,
    Info,
    Notice,
    Error,
    Fault,
}

impl LogStream {
    /// The next entry; an error means the stream is gone (phone unplugged, service stopped)
    pub async fn next(&mut self) -> Result<LogEntry> {
        let e = self.0.next().await?;
        Ok(LogEntry {
            pid: e.pid,
            process: e.filename,
            level: match e.level {
                1 => LogLevel::Info,
                2 => LogLevel::Debug,
                0x10 => LogLevel::Error,
                0x11 => LogLevel::Fault,
                _ => LogLevel::Notice,
            },
            subsystem: e.subsystem.map(|(s, c)| format!("{s}:{c}")),
            message: e.message,
        })
    }
}

/// 连接标签,出现在设备日志里
pub const LABEL: &str = "iphone-control";

#[derive(Debug, thiserror::Error)]
pub enum DeviceError {
    #[error("{}", ictl_l10n::tr!("设备未连接", "Device not connected"))]
    NotConnected,
    #[error("{}", ictl_l10n::tr!("设备未配对,需要在手机上点「信任」", "Device not paired: tap “Trust” on the phone"))]
    NotPaired,
    #[error("{}", ictl_l10n::tr!("手机上的「信任此电脑」弹窗还在等待确认", "The “Trust This Computer” prompt on the phone is still waiting for an answer"))]
    PairingPending,
    #[error("{}", ictl_l10n::tr!("用户在手机上拒绝了配对", "Pairing was declined on the phone"))]
    PairingDenied,
    #[error("{}", ictl_l10n::tr!("开发者模式未开启", "Developer Mode is off"))]
    DeveloperModeOff,
    /// 锁屏且自开机以来从未解锁(BFU),或锁屏时建隧道,都会报这个
    #[error("{}", ictl_l10n::tr!("手机锁屏(PasswordProtected),需要解锁", "The phone is locked (PasswordProtected) and has to be unlocked"))]
    PasswordProtected,
    #[error("{}", ictl_l10n::trf!("下载开发者镜像失败:{0}", "Failed to download the developer disk image: {0}", .0))]
    Download(String),
    #[error("{0}")]
    Io(#[from] std::io::Error),
    #[error("{}", ictl_l10n::trf!("idevice:{0}", "idevice: {0}", .0))]
    Idevice(String),
    /// 手机上的服务没在限定时间内应答。调用方应丢掉隧道重建
    #[error("{}", ictl_l10n::trf!("超时:{0}", "Timed out: {0}", .0))]
    Timeout(String),
    /// 无障碍服务(remoteAXService)没在限定时间内应答:连接、握手或请求超时。读出来和 [`Self::Timeout`] 一样。
    /// 手机上的 axauditd 卡住时就是这样,重连、重建隧道都没用,要结束它([`Tunnel::restart_ax_daemon`])
    #[error("{}", ictl_l10n::trf!("超时:{0}", "Timed out: {0}", .0))]
    AxUnanswered(String),
    /// 调用方给的参数不合法(例如 HID 不能输入的字符)。与连接无关,不需要重建会话
    #[error("{0}")]
    InvalidInput(String),
    /// Preparing the pasteboard failed before the paste shortcut was sent.
    #[error("{0}")]
    PasteNotSent(String),
    /// 手机拒绝启动 App([`Tunnel::launch_in_background`] 回了 NSError)。签名证书被吊销时就是这样:
    /// 手机日志里 amfid 报 `The identity used to sign the executable is no longer valid`
    #[error("{}", ictl_l10n::trf!("手机拒绝启动(NSError code {0})", "The phone refused to launch it (NSError code {0})", .0))]
    LaunchRefused(i64),
}

/// 同一时间只装一个包,见 [`Device::install`]
static INSTALLING: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// usbmuxd、lockdown 这类一问一答的查询的时限。idevice 读应答没有超时,对方接了连接不应答会一直等
const QUERY: Duration = Duration::from_secs(15);

/// 给对手机的一次调用加时限:卡住的服务不能把调用方(守护进程持锁时)一起卡死
pub(crate) async fn limit<T>(what: &str, d: Duration, f: impl Future<Output = Result<T>>) -> Result<T> {
    tokio::time::timeout(d, f)
        .await
        .map_err(|_| DeviceError::Timeout(ictl_l10n::trf!("{what}({} 秒)", "{what} ({} s)", d.as_secs())))?
}

impl From<IdeviceError> for DeviceError {
    fn from(e: IdeviceError) -> Self {
        match e {
            IdeviceError::PasswordProtected => Self::PasswordProtected,
            IdeviceError::DeviceNotFound => Self::NotConnected,
            IdeviceError::InvalidHostID => Self::NotPaired,
            IdeviceError::PairingDialogResponsePending => Self::PairingPending,
            IdeviceError::UserDeniedPairing => Self::PairingDenied,
            IdeviceError::DeveloperModeNotEnabled => Self::DeveloperModeOff,
            other => {
                let s = other.to_string();
                Self::Idevice(device_error_text(&s).unwrap_or(s))
            }
        }
    }
}

/// What a CoreDevice error says, out of the archive it comes in: idevice gives the error dictionary as text, with
/// `userInfoWithNSSecureCoding` as the bytes of an NSKeyedArchive holding `NSLocalizedDescription` («The process
/// identifier of the launched application could not be determined. It may have already terminated.»). None when
/// there is none
fn device_error_text(s: &str) -> Option<String> {
    let start = s.find("userInfoWithNSSecureCoding")?;
    let rest = &s[start..];
    let open = rest.find("Data([")? + "Data([".len();
    let close = rest[open..].find("])")? + open;
    let bytes: Vec<u8> =
        rest[open..close].split(',').map(|n| n.trim().parse::<u8>()).collect::<std::result::Result<_, _>>().ok()?;
    let v = plist::Value::from_reader(std::io::Cursor::new(bytes)).ok()?;
    let objects = v.as_dictionary()?.get("$objects")?.as_array()?;
    // the archive's strings: keys and class names have no spaces, a sentence does
    let text: Vec<&str> = objects.iter().filter_map(|o| o.as_string()).filter(|t| t.contains(' ')).collect();
    (!text.is_empty()).then(|| text.join(" "))
}

pub type Result<T> = std::result::Result<T, DeviceError>;

/// 屏幕逻辑尺寸(点)
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScreenSize {
    pub width: f64,
    pub height: f64,
}

impl ScreenSize {
    /// 设备竖放时的尺寸(短边是宽)
    pub fn portrait(self) -> Self {
        Self { width: self.width.min(self.height), height: self.width.max(self.height) }
    }
}

/// 界面相对设备竖放转了多少(WDA `GET /rotation` 的 z)。HID 触摸始终按设备竖放的坐标解释,
/// 横屏时要把界面上的点换算回去。真机:iPhone 15 Pro Max 摄像头在左 z=270、在右 z=90;
/// iPad mini 5 Home 键在左 z=90。两个横屏方向 WDA 的 `/orientation` 都报 `LANDSCAPE`,分不出来
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Rotation {
    #[default]
    Portrait,
    /// z=90:竖放的顶边转到了屏幕右边
    TopToRight,
    /// z=180
    UpsideDown,
    /// z=270:竖放的顶边转到了屏幕左边
    TopToLeft,
}

impl Rotation {
    pub fn from_degrees(z: f64) -> Option<Self> {
        if !z.is_finite() || z.fract() != 0.0 {
            return None;
        }
        Some(match z.rem_euclid(360.0) as u16 {
            0 => Self::Portrait,
            90 => Self::TopToRight,
            180 => Self::UpsideDown,
            270 => Self::TopToLeft,
            _ => return None,
        })
    }

    fn from_interface(orientation: InterfaceOrientation) -> Option<Self> {
        match orientation {
            InterfaceOrientation::Portrait => Some(Self::Portrait),
            InterfaceOrientation::PortraitUpsideDown => Some(Self::UpsideDown),
            InterfaceOrientation::LandscapeRight => Some(Self::TopToLeft),
            InterfaceOrientation::LandscapeLeft => Some(Self::TopToRight),
            InterfaceOrientation::Unknown => None,
        }
    }

    pub fn degrees(self) -> u16 {
        match self {
            Self::Portrait => 0,
            Self::TopToRight => 90,
            Self::UpsideDown => 180,
            Self::TopToLeft => 270,
        }
    }

    pub fn is_landscape(self) -> bool {
        matches!(self, Self::TopToRight | Self::TopToLeft)
    }

    /// 界面上的点 → 设备竖放时的点。`portrait`:竖放时的屏幕尺寸
    pub fn to_portrait(self, (x, y): (f64, f64), portrait: ScreenSize) -> (f64, f64) {
        let (w, h) = (portrait.width, portrait.height);
        match self {
            Self::Portrait => (x, y),
            Self::UpsideDown => (w - x, h - y),
            Self::TopToLeft => (w - y, x),
            Self::TopToRight => (y, h - x),
        }
    }
}

/// usbmuxd 报告的一台 USB 设备
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UsbDevice {
    pub udid: String,
    pub device_id: u32,
}

/// 设备基本信息(lockdown 不开会话就能读)
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct DeviceInfo {
    pub udid: String,
    pub name: String,
    pub product_type: String,
    pub product_version: String,
}

impl DeviceInfo {
    /// iOS 主版本号,读不出返回 0
    pub fn major_version(&self) -> u32 {
        self.product_version.split('.').next().and_then(|s| s.parse().ok()).unwrap_or(0)
    }
}

/// usbmuxd 地址:环境变量 `USBMUXD_SOCKET_ADDRESS` 优先,否则默认 `/var/run/usbmuxd`
pub fn usbmuxd_addr() -> UsbmuxdAddr {
    UsbmuxdAddr::from_env_var().unwrap_or_default()
}

/// sysfs 里 iPhone/iPad/iPod 的台数:厂商号 05ac、产品名以 iPhone/iPad/iPod 开头(Apple 键盘之类的配件不算)。
/// `root` 一般是 `/sys/bus/usb/devices`。
/// 接着几台时和 usbmuxd 认到的台数比,才看得出「新插的那台没被认出来」
pub fn ios_usb_count(root: &Path) -> usize {
    let Ok(entries) = std::fs::read_dir(root) else { return 0 };
    let read = |p: &Path, f: &str| std::fs::read_to_string(p.join(f)).map(|v| v.trim().to_owned()).unwrap_or_default();
    entries
        .flatten()
        .filter(|e| {
            let p = e.path();
            read(&p, "idVendor") == "05ac"
                && ["iPhone", "iPad", "iPod"].iter().any(|n| read(&p, "product").starts_with(n))
        })
        .count()
}

/// Linux 上 USB 设备在 sysfs 里的目录
pub const SYSFS_USB: &str = "/sys/bus/usb/devices";

/// 宿主机 USB 上有没有 iPhone/iPad:Linux 读 sysfs([`ios_usb_count`]),macOS 查 IORegistry。
/// 判断不了(其他系统、`ioreg` 运行失败)返回 `None`,调用方应跳过这项检查而不是当成「没有」。
pub async fn host_ios_usb_present() -> Option<bool> {
    if cfg!(target_os = "linux") {
        return Some(ios_usb_count(Path::new(SYSFS_USB)) > 0);
    }
    if !cfg!(target_os = "macos") {
        return None;
    }
    // Mac 上没有 sysfs;Apple Silicon 自带的 Apple 设备(键盘、触控板)也不在 IOUSB 平面上,但外接的 Apple 配件会在,
    // 所以按节点名认手机而不是只看厂商号
    let out = tokio::process::Command::new("/usr/sbin/ioreg").args(["-p", "IOUSB", "-w0"]).output().await.ok()?;
    out.status.success().then(|| ioreg_has_ios_device(&String::from_utf8_lossy(&out.stdout)))
}

/// `ioreg -p IOUSB` 的输出里有没有 iPhone/iPad/iPod 节点(形如 `+-o iPhone@00100000  <class IOUSBHostDevice, …>`)
fn ioreg_has_ios_device(out: &str) -> bool {
    out.lines().filter_map(|l| l.split("+-o ").nth(1)).any(|node| {
        let name = node.split('@').next().unwrap_or("");
        ["iPhone", "iPad", "iPod"].contains(&name)
    })
}

/// usbmuxd 看到的 USB 设备(不含网络设备)。usbmuxd 没在运行时返回错误。
pub async fn list_devices() -> Result<Vec<UsbDevice>> {
    limit(ictl_l10n::tr!("列出 usbmuxd 设备", "list usbmuxd devices"), QUERY, async {
        // 不用 UsbmuxdConnection::default():它固定连 /var/run/usbmuxd,不看 USBMUXD_SOCKET_ADDRESS
        let mut mux = usbmuxd_addr().connect(0).await?;
        Ok(mux
            .get_devices()
            .await?
            .into_iter()
            .filter(|d| matches!(d.connection_type, Connection::Usb))
            .map(|d| UsbDevice { udid: d.udid, device_id: d.device_id })
            .collect())
    })
    .await
}

/// 一台设备的句柄。只持有 usbmuxd provider,每次调用按需新建连接;隧道见 [`Device::open_tunnel`]。
#[derive(Debug, Clone)]
pub struct Device {
    pub udid: String,
    provider: Arc<UsbmuxdProvider>,
}

impl Device {
    /// `udid` 为空时取 usbmuxd 看到的第一台 USB 设备
    pub async fn find(udid: Option<&str>) -> Result<Self> {
        let devices = list_devices().await?;
        let dev = match udid {
            Some(u) => devices.into_iter().find(|d| d.udid == u),
            None => devices.into_iter().next(),
        }
        .ok_or(DeviceError::NotConnected)?;
        Ok(Self::from_usb(&dev))
    }

    /// 用 [`list_devices`] 列出的一项建句柄(不连 usbmuxd)
    pub fn from_usb(dev: &UsbDevice) -> Self {
        let provider = UsbmuxdProvider {
            addr: usbmuxd_addr(),
            tag: dev.device_id,
            udid: dev.udid.clone(),
            device_id: dev.device_id,
            label: LABEL.into(),
        };
        Self { udid: dev.udid.clone(), provider: Arc::new(provider) }
    }

    /// usbmuxd 给这次连接分的编号。拔插、USB 重新枚举后会变(UDID 不变),旧编号的连接全部失效
    pub fn device_id(&self) -> u32 {
        self.provider.device_id
    }

    /// 是不是 `dev` 这一次连接:UDID 和 usbmuxd 编号都要相同
    pub fn is_connection(&self, dev: &UsbDevice) -> bool {
        self.udid == dev.udid && self.device_id() == dev.device_id
    }

    /// 给 XCTest、WDA 等需要自己建连接的组件用
    pub fn provider(&self) -> Arc<dyn IdeviceProvider> {
        self.provider.clone()
    }

    /// usbmuxd 里有没有这台手机的配对记录。只有「没有记录」算 false;连不上 usbmuxd 等错误照常返回,
    /// 免得 usbmuxd 抖一下就让状态变成「请在手机上点信任」
    pub async fn paired(&self) -> Result<bool> {
        limit(ictl_l10n::tr!("查配对记录", "check the pairing record"), QUERY, async {
            let mut mux = self.provider.addr.connect(0).await?;
            match mux.get_pair_record(&self.udid).await {
                Ok(_) => Ok(true),
                // 没有记录时 usbmuxd 的应答里没有 PairRecordData;记录损坏同样需要重新配对
                Err(IdeviceError::UnexpectedResponse(_)) => Ok(false),
                Err(e) => Err(e.into()),
            }
        })
        .await
    }

    /// 发起配对:手机上弹「信任此电脑」。用户还没点时返回 [`DeviceError::PairingPending`],调用方应轮询重试。
    /// 成功后配对记录交给 usbmuxd 保存(Linux 上是 `/var/lib/lockdown`,macOS 上是 `/var/db/lockdown`)。
    pub async fn pair(&self, host_name: &str) -> Result<()> {
        limit(ictl_l10n::tr!("配对", "pair"), Duration::from_secs(30), async {
            let mut mux = self.provider.addr.connect(0).await?;
            let buid = mux.get_buid().await?;
            let mut lockdown = LockdownClient::connect(&*self.provider).await?;
            let host_id = uuid::Uuid::new_v4().to_string().to_uppercase();
            let record = lockdown.pair_once(host_id, buid, Some(host_name)).await?;
            let mut mux = self.provider.addr.connect(0).await?;
            mux.save_pair_record(&self.udid, record.serialize()?).await?;
            Ok(())
        })
        .await
    }

    /// 名称、型号、iOS 版本(不需要配对)
    pub async fn info(&self) -> Result<DeviceInfo> {
        limit(ictl_l10n::tr!("读设备信息", "read device info"), QUERY, self.info_inner()).await
    }

    async fn info_inner(&self) -> Result<DeviceInfo> {
        let mut lockdown = LockdownClient::connect(&*self.provider).await?;
        let mut get = async |key: &str| -> String {
            match lockdown.get_value(Some(key), None).await {
                Ok(v) => v.as_string().map(str::to_owned).unwrap_or_default(),
                Err(e) => {
                    tracing::warn!(
                        "{}",
                        ictl_l10n::log_trf!("读设备信息 {key} 失败:{e}", "Reading device info {key} failed: {e}")
                    );
                    String::new()
                }
            }
        };
        Ok(DeviceInfo {
            udid: self.udid.clone(),
            name: get("DeviceName").await,
            product_type: get("ProductType").await,
            product_version: get("ProductVersion").await,
        })
    }

    /// 手机上装着的全部描述文件(.mobileprovision 原始内容,经 misagent 读)。装 App 时随包带的描述文件会留在这里,
    /// 用它判断「这台手机上的 WDA 还能用多久」:同一个 Apple ID 给几台手机先后签过,每台装的是各自那一次签的
    pub async fn installed_profiles(&self) -> Result<Vec<Vec<u8>>> {
        limit(ictl_l10n::tr!("读描述文件", "read provisioning profiles"), QUERY, async {
            Ok(MisagentClient::connect(&*self.provider).await?.copy_all().await?)
        })
        .await
    }

    /// SpringBoard reports the four interface orientations separately, even when WDA lacks /rotation.
    pub async fn interface_rotation(&self) -> Result<Option<Rotation>> {
        limit(ictl_l10n::tr!("读界面方向", "read interface orientation"), Duration::from_secs(2), async {
            let mut springboard = SpringBoardServicesClient::connect(&*self.provider).await?;
            Ok(Rotation::from_interface(springboard.get_interface_orientation().await?))
        })
        .await
    }

    pub async fn developer_mode_enabled(&self) -> Result<bool> {
        limit(ictl_l10n::tr!("查开发者模式", "check Developer Mode"), QUERY, async {
            Ok(AmfiClient::connect(&*self.provider).await?.get_developer_mode_status().await?)
        })
        .await
    }

    /// 让「设置 → 隐私与安全性」里出现开发者模式开关(打开仍需用户在手机上操作)
    pub async fn reveal_developer_mode(&self) -> Result<()> {
        limit(ictl_l10n::tr!("显示开发者模式开关", "reveal the Developer Mode switch"), QUERY, async {
            Ok(AmfiClient::connect(&*self.provider).await?.reveal_developer_mode_option_in_ui().await?)
        })
        .await
    }

    /// 建立 CoreDeviceProxy 用户态隧道并完成 RSD 握手。锁屏时报 [`DeviceError::PasswordProtected`]。
    pub async fn open_tunnel(&self) -> Result<Tunnel> {
        limit(ictl_l10n::tr!("建隧道", "open the tunnel"), Duration::from_secs(30), async {
            let proxy = CoreDeviceProxy::connect(&*self.provider).await?;
            let rsd_port = proxy.tunnel_info().server_rsd_port;
            tracing::debug!("CoreDeviceProxy tunnel: MTU {}", proxy.tunnel_info().mtu);
            let mut adapter = proxy.create_software_tunnel()?.to_async_handle();
            let stream = adapter.connect(rsd_port).await?;
            let handshake = RsdHandshake::new(stream).await?;
            Ok(Tunnel {
                adapter,
                handshake,
                hid: None,
                ax: None,
                springboard: None,
                springboard_app: None,
                screenshots: None,
            })
        })
        .await
    }

    /// 已安装的 App。`include_system` 为 false 只列用户 App,为 true 只列系统 App(与 toolkit 一致)。
    pub async fn list_apps(&self, include_system: bool) -> Result<Vec<AppInfo>> {
        limit(ictl_l10n::tr!("列出 App", "list apps"), Duration::from_secs(30), async {
            let mut proxy = InstallationProxyClient::connect(&*self.provider).await?;
            let kind = if include_system { "System" } else { "User" };
            Ok(apps::parse_apps(proxy.get_apps(Some(kind), None).await?))
        })
        .await
    }

    /// 经 usbmuxd 直连手机上的 TCP 端口(`iphone-control wda-bridge` 用)
    pub async fn connect_port(&self, port: u16) -> Result<Box<dyn idevice::ReadWrite>> {
        let dev = limit(ictl_l10n::tr!("连接手机端口", "connect to a port on the phone"), QUERY, async {
            Ok(self.provider.connect(port).await?)
        })
        .await?;
        dev.get_socket().ok_or_else(|| {
            DeviceError::Idevice(ictl_l10n::tr!("连接没有套接字", "the connection has no socket").into())
        })
    }

    /// 上传并安装 .ipa 或 .app 目录
    pub async fn install(&self, package: &Path) -> Result<()> {
        // idevice 把每个 .ipa 都上传到手机上同一个文件(PublicStaging/idevice.ipa):同时装两个会互相覆盖
        // (重签和 iphone_install_app 撞上、两个会话同时安装)。排队等的时间不算进下面的时限
        let _one_at_a_time = INSTALLING.lock().await;
        // 含上传几十 MB 的包,USB 2 上也就一两分钟
        limit(ictl_l10n::tr!("安装", "install"), Duration::from_secs(600), async {
            idevice::utils::installation::install_package(&*self.provider, package, None).await?;
            Ok(())
        })
        .await
    }

    /// Removes an app by bundle ID (installation_proxy)
    pub async fn uninstall(&self, bundle_id: &str) -> Result<()> {
        limit(ictl_l10n::tr!("卸载", "uninstall"), Duration::from_secs(120), async {
            let mut proxy = InstallationProxyClient::connect(&*self.provider).await?;
            proxy.uninstall(bundle_id, None).await?;
            Ok(())
        })
        .await
    }

    /// Names of the crash reports in the top directory, after asking the phone to move new ones there
    pub async fn crash_reports(&self) -> Result<Vec<String>> {
        limit(ictl_l10n::tr!("列出崩溃报告", "list crash reports"), Duration::from_secs(30), async {
            // without the flush, reports from the last minutes may not be visible yet; it is only a hint
            let _ = idevice::crashreportcopymobile::flush_reports(&*self.provider).await;
            let mut c = CrashReportCopyMobileClient::connect(&*self.provider).await?;
            Ok(c.ls(None).await?)
        })
        .await
    }

    /// One crash report's content
    pub async fn crash_report(&self, name: &str) -> Result<Vec<u8>> {
        limit(ictl_l10n::tr!("读取崩溃报告", "read crash report"), Duration::from_secs(30), async {
            let mut c = CrashReportCopyMobileClient::connect(&*self.provider).await?;
            Ok(c.pull(name).await?)
        })
        .await
    }

    /// Opens the Web Inspector service (inspectable pages and WebViews)
    pub async fn web_inspector(&self) -> Result<ictl_webinspector::WebInspector> {
        limit(ictl_l10n::tr!("打开 Web Inspector", "open the Web Inspector"), QUERY, async {
            ictl_webinspector::WebInspector::open(&*self.provider).await.map_err(|e| match e {
                ictl_webinspector::Error::Device(e) => e.into(),
                // kept as I/O so the caller can tell the phone closed the connection (Web Inspector off)
                ictl_webinspector::Error::Io(e) => DeviceError::Io(e),
                e => DeviceError::Idevice(e.to_string()),
            })
        })
        .await
    }

    /// Starts the phone's unified log stream (os_trace_relay), every process, without debug messages.
    /// Asking for debug messages (what idevice's `start_trace` does) makes every process on the phone produce them:
    /// about 650 lines a second instead of about 80 on an idle iPhone 15 Pro Max (iOS 27.0.1), and gestures got
    /// slower and some failed while the stream ran. Most info messages go with them, but some processes still send
    /// Info entries (MobileTimer on iOS 27.2.1); callers that promise default, Error and Fault only drop those
    pub async fn log_stream(&self) -> Result<LogStream> {
        let flags = oslog::flags::IDEVICE_DEFAULT & !oslog::flags::DEBUG;
        let inner =
            limit(ictl_l10n::tr!("打开日志流", "open the log stream"), QUERY, oslog::start(&*self.provider, flags))
                .await?;
        Ok(LogStream(inner))
    }
}

/// [`Tunnel::restore_pasteboard`] 的结果
#[derive(Debug, Clone, PartialEq)]
pub enum ClipboardRestore {
    /// 原来的内容已经写回
    Restored,
    /// 粘贴后剪贴板被别人改过:不覆盖,留着现在的内容
    ChangedMeanwhile,
    Failed(String),
}

/// [`Tunnel::paste_text`] 存下的原剪贴板,等粘贴弹窗答完再写回([`Tunnel::restore_pasteboard`])
#[derive(Debug, Clone)]
pub struct PendingRestore {
    items: Vec<pasteboard::Item>,
    /// 粘贴用的文字和写进去后的改动计数:剪贴板还是它,才说明期间没人改过
    text: String,
    change_count: Option<i64>,
}

async fn write_back(
    adapter: &mut AdapterHandle,
    handshake: &mut RsdHandshake,
    items: &[pasteboard::Item],
) -> ClipboardRestore {
    match limit(ictl_l10n::tr!("还原剪贴板", "restore the pasteboard"), Duration::from_secs(10), async {
        pasteboard::write(adapter, handshake, items).await
    })
    .await
    {
        Ok(()) => ClipboardRestore::Restored,
        Err(e) => ClipboardRestore::Failed(e.to_string()),
    }
}

/// 一条活着的隧道。HID、截图、cryptexd 都经它访问 RSD 服务;丢掉它隧道就断了。
///
/// HID 会话归隧道所有:它持有屏幕媒体流,必须在隧道断开之前停掉,否则手机端一直挂着这个会话
/// (灵动岛「配对的电脑」图标常驻、不会自动锁屏,进程退出和拔插数据线都清不掉)。
/// 所以不要直接丢掉 `Tunnel`,用 [`Tunnel::close`]。
pub struct Tunnel {
    adapter: AdapterHandle,
    handshake: RsdHandshake,
    hid: Option<HidSession>,
    /// remoteAXService([`ax`]),用时才开
    ax: Option<ax::AxSession>,
    /// SpringBoard 的进程号([`ax`] 读盖在 App 上的弹窗用),每条隧道查一次
    springboard: Option<i64>,
    /// SpringBoard 的应用元素,每个无障碍会话问一次(每次读界面省一个请求)
    springboard_app: Option<ictl_ax::ObjectRef>,
    /// 截屏服务的连接,留着复用([`Tunnel::screenshot_png`])
    screenshots: Option<ScreenCaptureServiceClient<Box<dyn idevice::ReadWrite>>>,
}

impl Tunnel {
    /// RSD 里是否提供某个服务(例如 iOS 27 的 `com.apple.coredevice.hid.universalhidservice`)
    pub fn has_service(&self, name: &str) -> bool {
        self.handshake.services.contains_key(name)
    }

    /// 已挂载的开发者镜像版本,未挂载返回 None
    pub async fn ddi_mounted(&mut self) -> Result<Option<String>> {
        limit(ictl_l10n::tr!("查询开发者镜像", "query the developer disk image"), Duration::from_secs(20), async {
            Ok(installed_ddi(&mut self.adapter, &mut self.handshake).await?.map(|c| c.version))
        })
        .await
    }

    /// 个性化并安装开发者镜像。`restore_dir` 见 [`ddi::ensure_cache`]
    pub async fn mount_ddi(&mut self, restore_dir: &Path) -> Result<String> {
        let assets = Cryptex1Assets::load(restore_dir).await?;
        // 含 TSS 联网签票
        limit(ictl_l10n::tr!("挂载开发者镜像", "mount the developer disk image"), Duration::from_secs(180), async {
            Ok(install_ddi(&mut self.adapter, &mut self.handshake, &assets).await?.version)
        })
        .await
    }

    /// The screen in the format the phone calls `format` (`requestedFormat`; idevice's "jpeg" is refused: «Cannot
    /// initialize ScreenshotImageFormat from invalid String value jpeg»)
    pub async fn screenshot_as(&mut self, format: &str) -> Result<Vec<u8>> {
        limit(ictl_l10n::tr!("截图", "screenshot"), Duration::from_secs(30), async {
            let port = self
                .handshake
                .services
                .get("com.apple.coredevice.screencaptureservice")
                .map(|s| s.port)
                .ok_or_else(|| DeviceError::Idevice("the phone offers no screencaptureservice".into()))?;
            let stream: Box<dyn idevice::ReadWrite> = Box::new(self.adapter.connect(port).await?);
            let mut c = idevice::core_device::CoreDeviceServiceClient::new(stream).await?;
            let mut req = plist::Dictionary::new();
            req.insert("requestedFormat".into(), format.into());
            let res = c
                .invoke_with_plist_action(
                    "com.apple.coredevice.feature.capturescreenshot",
                    req,
                    "com.apple.coredevice.action.capturescreenshot",
                )
                .await?;
            match res.as_dictionary().and_then(|d| d.get("image")) {
                Some(plist::Value::Data(image)) => Ok(image.clone()),
                _ => Err(DeviceError::Idevice("the screenshot came without an image".into())),
            }
        })
        .await
    }

    /// The screen as PNG, on the screen capture service's connection kept from the last time: the first request on
    /// a new one does the service's handshake too (0.82 s against 0.30 s for the next ones on a simple screen). A
    /// connection that fails is dropped and one new one tried
    pub async fn screenshot_png(&mut self) -> Result<Vec<u8>> {
        limit(ictl_l10n::tr!("截图", "screenshot"), Duration::from_secs(30), async {
            if let Some(c) = self.screenshots.as_mut() {
                match c.take_screenshot(None, ImageFormat::Png).await {
                    Ok(png) => return Ok(png),
                    Err(e) => {
                        tracing::debug!("screenshot on the kept connection failed, connecting again: {e}");
                        self.screenshots = None;
                    }
                }
            }
            let mut c = ScreenCaptureServiceClient::connect_rsd(&mut self.adapter, &mut self.handshake).await?;
            let png = c.take_screenshot(None, ImageFormat::Png).await?;
            self.screenshots = Some(c);
            Ok(png)
        })
        .await
    }

    /// HID 会话;没有就开一个:先启动显示媒体流(HID 的授权闸门,码率上限 `max_bitrate`,见 media.rs),再连
    /// universalhidservice 和 indigo。已经开着就照旧用,不管它的码率上限
    pub async fn hid(&mut self, screen: ScreenSize, max_bitrate: u32) -> Result<&mut HidSession> {
        if self.hid.is_none() {
            // 各步自带时限(见 HidSession::open):不能在这里整体取消,否则起来一半的媒体流没人停
            let h = HidSession::open(&mut self.adapter, &mut self.handshake, screen, max_bitrate).await?;
            self.hid = Some(h);
        }
        let h = self.hid.as_mut().expect("刚建好");
        h.screen = screen.portrait();
        Ok(h)
    }

    pub fn hid_open(&self) -> bool {
        self.hid.is_some()
    }

    /// The HID session if it is already open (no request to the phone)
    pub fn open_hid(&mut self) -> Option<&mut HidSession> {
        self.hid.as_mut()
    }

    /// 经剪贴板输入文字(HID 后备路线输入中文等非 ASCII 用),HID 会话要已经开着([`Self::hid`]):
    ///
    /// 1. 存下原来的剪贴板(读不出来、或有不能原样写回的内容时照样粘贴,只是不还原:返回 None)
    /// 2. 写入 `text`,读回核对;对不上就不粘贴,报错
    /// 3. 按 Cmd-V。整段一次粘贴,中文输入法不会把它当成拼音
    ///
    /// iOS 把经 `dtpasteboardd` 写入的内容当成别的 App 的,Cmd-V 也要用户确认:App 弹出「“备忘录”想从“dtpasteboardd”
    /// 粘贴」,点「允许粘贴」才粘进去(iPhone16,2 / iOS 27.2.1 的备忘录,每次都弹)。剪贴板一改,这个弹窗就消失,什么也
    /// 不粘,所以这里不还原:调用方等弹窗答完再用返回的 [`PendingRestore`] 调 [`Self::restore_pasteboard`]。
    /// 手机不确认按键,没有输入框获得焦点时什么也粘不上,结果要看屏幕
    pub async fn paste_text(&mut self, text: &str) -> Result<Option<PendingRestore>> {
        let Tunnel { adapter, handshake, hid, .. } = self;
        let Some(hid) = hid.as_mut() else {
            return Err(DeviceError::InvalidInput(ictl_l10n::tr!("HID 会话没开", "The HID session isn't open").into()));
        };
        if !pasteboard::supported(handshake) {
            return Err(DeviceError::InvalidInput(
                ictl_l10n::tr!(
                    "手机没有提供剪贴板服务,HID 不能输入非 ASCII 文字",
                    "The phone offers no pasteboard service, so HID can't type non-ASCII text"
                )
                .into(),
            ));
        }
        // 读不出来不影响粘贴;连接卡住时别等太久
        let saved = limit(ictl_l10n::tr!("读剪贴板", "read the pasteboard"), Duration::from_secs(5), async {
            pasteboard::read(adapter, handshake).await
        })
        .await
        .ok()
        .and_then(|c| c.items);
        let ours = pasteboard::text_items(text);
        let written = limit(ictl_l10n::tr!("写剪贴板", "write the pasteboard"), Duration::from_secs(10), async {
            pasteboard::write(adapter, handshake, &ours).await?;
            pasteboard::read(adapter, handshake).await
        })
        .await
        .map_err(|e| {
            DeviceError::PasteNotSent(ictl_l10n::trf!(
                "文字写不进剪贴板,没有粘贴:{e}",
                "The text couldn't be put on the pasteboard; nothing was pasted: {e}"
            ))
        })?;
        if written.text.as_deref() != Some(text) {
            // 还没粘贴,马上写回
            if let Some(items) = &saved {
                write_back(adapter, handshake, items).await;
            }
            return Err(DeviceError::PasteNotSent(
                ictl_l10n::tr!(
                    "文字写进剪贴板后读回来对不上,没有粘贴",
                    "The text read back from the pasteboard didn't match what was written; nothing was pasted"
                )
                .into(),
            ));
        }
        hid.paste_shortcut().await.map_err(|e| {
            DeviceError::Idevice(ictl_l10n::trf!(
                "按 Cmd-V 失败(可能已经按下,先截图确认):{e}",
                "Pressing Cmd-V failed (it may have gone through; take a screenshot first): {e}"
            ))
        })?;
        Ok(saved.map(|items| PendingRestore { items, text: text.to_owned(), change_count: written.change_count }))
    }

    /// 把 [`Self::paste_text`] 存下的原剪贴板写回去。剪贴板已经不是粘贴的那段文字(期间有人复制了别的)就不动它
    pub async fn restore_pasteboard(&mut self, p: PendingRestore) -> ClipboardRestore {
        let now = limit(ictl_l10n::tr!("读剪贴板", "read the pasteboard"), Duration::from_secs(5), async {
            pasteboard::read(&mut self.adapter, &mut self.handshake).await
        })
        .await;
        match now {
            Ok(now) if now.text.as_deref() != Some(p.text.as_str()) || now.change_count != p.change_count => {
                ClipboardRestore::ChangedMeanwhile
            }
            Ok(_) => write_back(&mut self.adapter, &mut self.handshake, &p.items).await,
            Err(e) => ClipboardRestore::Failed(e.to_string()),
        }
    }

    /// 停掉 HID 会话(含屏幕媒体流),隧道保留
    pub async fn close_hid(&mut self) {
        if let Some(h) = self.hid.take() {
            // boxed: the future holds the whole session, and callers that may close it would carry that
            let _ = tokio::time::timeout(Duration::from_secs(3), Box::pin(h.close())).await;
        }
    }

    /// 在后台启动一个 App(DVT processcontrol,选项 `ActivateSuspended`),先结束已在运行的实例,返回进程号。
    ///
    /// 不带启动参数。前台启动(CoreDevice appservice,或 processcontrol 不带 `ActivateSuspended`)
    /// 锁屏时会被 SpringBoard 拒绝(`RequestDenied … Locked`),后台启动不会(iPhone16,2 / iOS 27.0.1 真机)。
    /// 用来启动 WDA 时,runner 自己合成测试配置,不需要主机持有 XCTest 会话(见 `docs/dev/wda-dependency-research.md`)。
    /// `env` 是这个进程的环境变量(WDA 的 `USE_IP` 就靠它)
    pub async fn launch_in_background(&mut self, bundle_id: &str, env: &[(&str, &str)]) -> Result<u64> {
        use idevice::dvt::{message::AuxValue, remote_server::RemoteServerClient};
        limit(ictl_l10n::tr!("后台启动 App", "launch an app in the background"), Duration::from_secs(60), async {
            let mut dvt: RemoteServerClient<Box<dyn idevice::ReadWrite>> =
                RemoteServerClient::connect_rsd(&mut self.adapter, &mut self.handshake).await?;
            let mut ch = dvt.make_channel("com.apple.instruments.server.services.processcontrol").await?;
            let mut opts = plist::Dictionary::new();
            opts.insert("StartSuspendedKey".into(), plist::Value::Boolean(false));
            opts.insert("ActivateSuspended".into(), plist::Value::Boolean(true));
            opts.insert("KillExisting".into(), plist::Value::Boolean(true));
            ch.call_method(
                Some(plist::Value::String(
                    "launchSuspendedProcessWithDevicePath:bundleIdentifier:environment:arguments:options:".into(),
                )),
                Some(vec![
                    AuxValue::archived_value(""),
                    AuxValue::archived_value(bundle_id.to_owned()),
                    AuxValue::archived_value(environment(env)),
                    AuxValue::archived_value(plist::Value::Array(vec![])),
                    AuxValue::archived_value(opts),
                ]),
                true,
            )
            .await?;
            let reply = ch.read_message().await?;
            match reply.data.as_ref().and_then(|d| d.as_unsigned_integer()) {
                Some(pid) => Ok(pid),
                // 失败时回的是 NSError 归档,idevice 只解出 Uid 引用(读不出 domain、说明),只有错误码
                None => match reply.data.as_ref().and_then(|d| d.as_dictionary()?.get("NSCode")?.as_signed_integer()) {
                    Some(code) => Err(DeviceError::LaunchRefused(code)),
                    None => Err(DeviceError::Idevice(format!("processcontrol: {:?}", reply.data))),
                },
            }
        })
        .await
    }

    /// 手机上正在运行的进程:(进程号,可执行文件路径)
    pub async fn processes(&mut self) -> Result<Vec<(u64, String)>> {
        use idevice::core_device::AppServiceClient;
        limit(ictl_l10n::tr!("列出进程", "list processes"), Duration::from_secs(20), async {
            let mut c = AppServiceClient::connect_rsd(&mut self.adapter, &mut self.handshake).await?;
            Ok(c.list_processes()
                .await?
                .into_iter()
                .map(|p| (u64::from(p.pid), p.executable_url.map(|u| u.relative).unwrap_or_default()))
                .collect())
        })
        .await
    }

    /// 关掉隧道:先停 HID 的屏幕媒体流
    pub async fn close(mut self) {
        self.close_ax().await;
        self.close_hid().await;
    }
}

/// A launch's environment as processcontrol takes it
fn environment(env: &[(&str, &str)]) -> plist::Dictionary {
    env.iter().map(|(k, v)| ((*k).to_owned(), plist::Value::String((*v).to_owned()))).collect()
}

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

    #[test]
    fn the_launch_environment_is_a_dictionary_of_strings() {
        let env = environment(&[("USE_IP", "127.0.0.1")]);
        assert_eq!(env.len(), 1);
        assert_eq!(env.get("USE_IP").and_then(plist::Value::as_string), Some("127.0.0.1"));
        assert!(environment(&[]).is_empty());
    }

    #[test]
    fn springboard_interface_orientation_preserves_all_four_directions() {
        use InterfaceOrientation::*;
        assert_eq!(Rotation::from_interface(Portrait), Some(Rotation::Portrait));
        assert_eq!(Rotation::from_interface(PortraitUpsideDown), Some(Rotation::UpsideDown));
        assert_eq!(Rotation::from_interface(LandscapeRight), Some(Rotation::TopToLeft));
        assert_eq!(Rotation::from_interface(LandscapeLeft), Some(Rotation::TopToRight));
        assert_eq!(Rotation::from_interface(Unknown), None);
    }

    /// 真机:摄像头在左(z=270)时,HID 按竖放发 (265,314) 落在了界面的 (314,165),反过来要换算回 (265,314)
    #[test]
    fn rotation_maps_screen_points_back_to_portrait() {
        let p = ScreenSize { width: 932.0, height: 430.0 }.portrait();
        assert_eq!(p, ScreenSize { width: 430.0, height: 932.0 });
        assert_eq!(Rotation::from_degrees(270.0), Some(Rotation::TopToLeft));
        assert_eq!(Rotation::from_degrees(90.0), Some(Rotation::TopToRight));
        assert_eq!(Rotation::from_degrees(-90.0), Some(Rotation::TopToLeft));
        assert_eq!(Rotation::from_degrees(0.0), Some(Rotation::Portrait));
        assert_eq!(Rotation::from_degrees(45.0), None);
        for z in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 0.4, 90.1] {
            assert_eq!(Rotation::from_degrees(z), None);
        }
        assert_eq!(Rotation::TopToLeft.to_portrait((314.0, 165.0), p), (265.0, 314.0));
        // 顶边在右:界面左上角是竖放时的左下角
        assert_eq!(Rotation::TopToRight.to_portrait((0.0, 0.0), p), (0.0, 932.0));
        assert_eq!(Rotation::TopToRight.to_portrait((226.0, 97.0), p), (97.0, 706.0));
        assert_eq!(Rotation::UpsideDown.to_portrait((0.0, 0.0), p), (430.0, 932.0));
        assert_eq!(Rotation::Portrait.to_portrait((12.0, 34.0), p), (12.0, 34.0));
    }

    #[test]
    fn ios_usb_count_counts_iphones_and_ipads_only() {
        let root = std::env::temp_dir().join(format!("ictl-sysfs-{}", std::process::id()));
        let dev = |name: &str, vendor: &str, product: &str| {
            let d = root.join(name);
            std::fs::create_dir_all(&d).unwrap();
            std::fs::write(d.join("idVendor"), format!("{vendor}\n")).unwrap();
            std::fs::write(d.join("product"), format!("{product}\n")).unwrap();
        };
        dev("5-1", "05ac", "iPhone");
        dev("7-1", "05ac", "iPad");
        dev("3-1", "05ac", "Magic Keyboard");
        dev("1-1", "2109", "USB2.0 Hub");
        // 接口目录没有 idVendor
        std::fs::create_dir_all(root.join("5-1:1.0")).unwrap();
        assert_eq!(ios_usb_count(&root), 2);
        std::fs::remove_dir_all(&root).unwrap();
        assert_eq!(ios_usb_count(&root), 0);
    }

    #[test]
    fn same_udid_new_mux_id_is_new_connection() {
        let first = UsbDevice { udid: "00008130-0019295E012B803A".into(), device_id: 2 };
        let d = Device::from_usb(&first);
        assert_eq!(d.device_id(), 2);
        assert!(d.is_connection(&first));
        // 拔插后 usbmuxd 重新编号:UDID 相同也要当成新连接
        assert!(!d.is_connection(&UsbDevice { device_id: 3, ..first.clone() }));
        assert!(!d.is_connection(&UsbDevice { udid: "other".into(), ..first }));
    }

    #[test]
    fn ioreg_finds_phone_node() {
        let with = "+-o Root  <class IORegistryEntry, id 0x100000100, retain 35>
  +-o AppleT8122USBXHCI@00000000  <class AppleT8122USBXHCI, id 0x1000003f8, registered>
  | +-o iPhone@00100000  <class IOUSBHostDevice, id 0x10009fe32, registered, matched>
  +-o AppleT8122USBXHCI@02000000  <class AppleT8122USBXHCI, id 0x100000424, registered>";
        assert!(ioreg_has_ios_device(with));
        let without = "+-o Root  <class IORegistryEntry>
  +-o AppleT8122USBXHCI@00000000  <class AppleT8122USBXHCI>
  | +-o Magic Keyboard@00100000  <class IOUSBHostDevice>";
        assert!(!ioreg_has_ios_device(without));
    }

    #[test]
    fn a_coredevice_error_says_what_it_says() {
        let mut d = plist::Dictionary::new();
        d.insert("$version".into(), 100000u64.into());
        d.insert("$archiver".into(), "NSKeyedArchiver".into());
        d.insert(
            "$objects".into(),
            plist::Value::Array(vec![
                "$null".into(),
                "NSLocalizedDescription".into(),
                "The process identifier of the launched application could not be determined.".into(),
            ]),
        );
        let mut bytes = Vec::new();
        plist::Value::Dictionary(d).to_writer_binary(&mut bytes).unwrap();
        let s = format!(
            "device returned an error: Dictionary({{\"userInfoWithNSSecureCoding\": Data({bytes:?}), \"code\": Integer(1)}})"
        );
        assert_eq!(
            device_error_text(&s).as_deref(),
            Some("The process identifier of the launched application could not be determined.")
        );
        assert_eq!(device_error_text("socket closed"), None);
    }
}