Skip to main content

iphone_cli/device/
session.rs

1//! Everything the daemon holds open for one phone: the tunnel, the
2//! accessibility subscription and the touch-input session.
3
4use std::{
5    sync::Arc,
6    time::{Duration, Instant},
7};
8
9use idevice::{
10    ReadWrite, RsdService,
11    core_device::{
12        AppListEntry, AppServiceClient, ImageFormat, PasteboardServiceClient,
13        ScreenCaptureServiceClient,
14    },
15};
16
17use crate::{
18    ax::{
19        dump::Dump,
20        tree::{BuildOptions, Screen},
21    },
22    config::Config,
23    device::{
24        ax::{ANSWER_AT_LEAST, AxSub},
25        conn::{self, Tunnel},
26        input::InputSession,
27    },
28    i18n::t,
29    reply::{Fail, Res},
30};
31
32const APP_SERVICE: &str = "com.apple.coredevice.appservice";
33
34/// One touch or button gesture, in normalized HID coordinates.
35#[derive(Debug, Clone, Copy)]
36pub enum Gesture {
37    Tap { x: u16, y: u16, hold: Duration },
38    DoubleTap { x: u16, y: u16 },
39    Drag { from: (u16, u16), to: (u16, u16), duration: Duration, motion: crate::device::input::Motion },
40    Button { page: u64, usage: u64, hold: Duration },
41}
42
43/// How fresh a reading must be.
44#[derive(Debug, Clone, Copy, PartialEq)]
45pub enum Fresh {
46    /// Ask the phone for a new dump and wait for it.
47    Force,
48    /// Accept the latest dump if it is at most this old.
49    MaxAge(Duration),
50}
51
52/// A cached reading younger than this is reused when a command follows
53/// another one closely (scripts, batches).
54pub const REUSE: Duration = Duration::from_millis(1500);
55
56/// `REUSE`, or `IPH_REUSE_MS` when set (for experiments).
57pub fn reuse() -> Duration {
58    std::env::var("IPH_REUSE_MS")
59        .ok()
60        .and_then(|v| v.parse().ok())
61        .map(Duration::from_millis)
62        .unwrap_or(REUSE)
63}
64/// How long to wait for one dump before asking again. Dumps requested during
65/// an animation sometimes never come.
66/// How long to wait for the answer to a request before asking again. Every
67/// request restarts the phone's wait, so this must be generous: answers took
68/// up to 5.8 s while a list was still settling.
69const ASK_AGAIN: Duration = Duration::from_millis(6500);
70const READ_TIMEOUT: Duration = Duration::from_secs(14);
71/// The phone's accessibility daemon is not ended more often than this.
72const AX_RESTART_GAP: Duration = Duration::from_secs(120);
73/// Shortest time between the end of one input session and the start of the
74/// next.
75const REOPEN_GAP: Duration = Duration::from_millis(2000);
76const SECOND_PASS_WAIT: Duration = Duration::from_millis(2500);
77
78/// How the phone's accessibility daemon failed a reading, when that calls
79/// for ending it so the phone starts a new one.
80#[derive(Debug, PartialEq, Clone, Copy)]
81pub(crate) enum AxStall {
82    /// Not a word on a connection that is up.
83    Silent,
84    /// Only answers that carry nothing (no application, no element). That is
85    /// how it stayed for 25 minutes on 2026-11-11 (every reading
86    /// `ax_timeout`, three empty answers each) while the phone was unlocked
87    /// and showing Settings; a new daemon answered at once.
88    EmptyOnly,
89}
90
91/// `empty` and `unparsed`: answers of each kind during the failed reading.
92/// Answers that were not understood point at the parser, not the daemon.
93pub(crate) fn ax_stall(empty: u64, unparsed: u64) -> Option<AxStall> {
94    match (empty, unparsed) {
95        (0, 0) => Some(AxStall::Silent),
96        (_, 0) => Some(AxStall::EmptyOnly),
97        _ => None,
98    }
99}
100
101pub struct Session {
102    pub udid: String,
103    pub config: Config,
104    pub tunnel: Option<Tunnel>,
105    pub ax: Option<AxSub>,
106    pub input: Option<InputSession>,
107    pub(crate) input_used: Instant,
108    /// When the last input session ended, to keep the next one apart from it.
109    input_closed_at: Option<Instant>,
110    /// A system panel came up over the app after the last tap (a Face ID
111    /// prompt, for one). Scrolls sent on the same input session right after
112    /// that moved nothing, with no error (seen once, 2026-11-11), so the next
113    /// gesture starts a new session.
114    pub(crate) input_suspect: bool,
115    /// Input sessions given up after an error, each with the tunnel it ran on
116    /// when that was given up too. Their streams are stopped at the next
117    /// quiet moment (`sweep`).
118    abandoned: Vec<(InputSession, Option<Tunnel>)>,
119    apps: Option<Vec<AppListEntry>>,
120    /// Logical screen size from the last complete reading.
121    pub(crate) screen_size: Option<(f64, f64)>,
122    /// Readings that arrived before this moment no longer describe the
123    /// screen (something changed it after they were taken).
124    stale_before: Option<Instant>,
125    /// Counts tunnel (re)connections, for `iph doctor` and logs.
126    pub connects: u64,
127    /// How often, and when last, the phone's accessibility daemon was ended
128    /// because it had stopped answering.
129    pub ax_restarts: u64,
130    ax_restarted_at: Option<Instant>,
131    /// The daemon was ended and the one the phone started in its place did
132    /// not answer either, or it could not be ended. It is not ended again
133    /// until a reading comes back: on 2026-11-12, after about six ends in
134    /// ten minutes, the phone stopped starting it at all until the phone was
135    /// locked and unlocked.
136    ax_restart_unhelpful: bool,
137    /// The last complete reading showed the home screen, Control Center or
138    /// another of the system's panels. The daemon is not ended for a stall
139    /// there: Control Center during a screen recording gets no answer from
140    /// a new daemon either, and the old one answers again once it is closed
141    /// (2026-11-12).
142    system_in_front: bool,
143    /// Do Not Disturb: whether it is on for this session and what to put back.
144    pub dnd: crate::dnd::State,
145}
146
147/// A file URL or path as a plain path without a trailing slash.
148fn plain_path(s: &str) -> String {
149    let s = s.strip_prefix("file://").unwrap_or(s);
150    let raw = s.as_bytes();
151    let mut bytes = Vec::with_capacity(raw.len());
152    let mut i = 0;
153    while i < raw.len() {
154        if raw[i] == b'%' && i + 2 < raw.len() {
155            let hex = std::str::from_utf8(&raw[i + 1..i + 3]).unwrap_or("");
156            if let Ok(v) = u8::from_str_radix(hex, 16) {
157                bytes.push(v);
158                i += 3;
159                continue;
160            }
161        }
162        bytes.push(raw[i]);
163        i += 1;
164    }
165    let mut path = String::from_utf8_lossy(&bytes).into_owned();
166    while path.len() > 1 && path.ends_with('/') {
167        path.pop();
168    }
169    // The same app is reported under /private/var and /var.
170    path.strip_prefix("/private").map(String::from).unwrap_or(path)
171}
172
173fn is_transport_error(msg: &str) -> bool {
174    let m = msg.to_lowercase();
175    [
176        "adapter closed",
177        "broken pipe",
178        "channel recv timeout",
179        "connection reset",
180        "not connected",
181        "timed out",
182        "closed",
183        "unreachable",
184        "eof",
185    ]
186    .iter()
187    .any(|needle| m.contains(needle))
188}
189
190/// Bound a device call that has no time limit of its own.
191async fn bounded<T, E: std::fmt::Debug>(
192    what: &str,
193    secs: u64,
194    fut: impl std::future::Future<Output = Result<T, E>>,
195) -> Result<T, String> {
196    match tokio::time::timeout(Duration::from_secs(secs), fut).await {
197        Ok(Ok(v)) => Ok(v),
198        Ok(Err(e)) => Err(format!("{what}: {e:?}")),
199        Err(_) => Err(format!("{what}: no answer within {secs} s")),
200    }
201}
202
203impl Session {
204    pub fn new(udid: String, config: Config) -> Self {
205        let dnd = crate::dnd::State::load(&udid);
206        Self {
207            udid,
208            config,
209            tunnel: None,
210            ax: None,
211            input: None,
212            input_used: Instant::now(),
213            input_closed_at: None,
214            input_suspect: false,
215            abandoned: Vec::new(),
216            apps: None,
217            screen_size: None,
218            stale_before: None,
219            connects: 0,
220            ax_restarts: 0,
221            ax_restarted_at: None,
222            ax_restart_unhelpful: false,
223            system_in_front: false,
224            dnd,
225        }
226    }
227
228    /// Mark every reading taken so far as out of date.
229    /// Age of the latest reading, unless it predates `invalidate_readings`.
230    pub(crate) fn reading_age(&self) -> Option<Duration> {
231        let arrived = self.ax.as_ref()?.latest_arrival()?;
232        if self.stale_before.is_some_and(|limit| arrived < limit) {
233            return None;
234        }
235        Some(arrived.elapsed())
236    }
237
238    pub fn invalidate_readings(&mut self) {
239        self.stale_before = Some(Instant::now());
240    }
241
242    /// Port of an RSD service, for connections made outside `&mut self`.
243    pub async fn service_port(&mut self, name: &str) -> Res<(crate::tcp::handle::AdapterHandle, u16)> {
244        let tunnel = self.ensure_tunnel().await?;
245        let port = tunnel
246            .rsd
247            .services
248            .get(name)
249            .map(|s| s.port)
250            .ok_or_else(|| Fail::not_sent("service_unavailable", format!("{name} is not on offer")))?;
251        Ok((tunnel.adapter.clone(), port))
252    }
253
254    /// Abandon the input session without the orderly close (after an error).
255    pub fn drop_input(&mut self) {
256        if let Some(input) = self.input.take() {
257            self.input_closed_at = Some(Instant::now());
258            self.abandoned.push((input, None));
259        }
260    }
261
262    /// Drop every connection. The next request builds them again.
263    pub fn reset(&mut self) {
264        let tunnel = self.tunnel.take();
265        if let Some(input) = self.input.take() {
266            self.input_closed_at = Some(Instant::now());
267            // The tunnel stays until the stream has been told to stop.
268            self.abandoned.push((input, tunnel));
269        }
270        self.ax = None;
271    }
272
273    /// Tell the phone to stop the streams of abandoned input sessions. Left
274    /// alone they keep the phone encoding, and after a row of timeouts
275    /// (2026-11-11, with the Mac overloaded) the phone answered ever slower.
276    pub async fn sweep(&mut self) {
277        for (input, tunnel) in std::mem::take(&mut self.abandoned) {
278            input.abandon().await;
279            drop(tunnel);
280        }
281    }
282
283    pub fn connection(&self) -> Option<&str> {
284        self.tunnel.as_ref().map(|t| t.connection.as_str())
285    }
286
287    pub async fn ensure_tunnel(&mut self) -> Res<&mut Tunnel> {
288        if self.tunnel.is_none() {
289            // One tunnel at a time: let go of an abandoned one first.
290            self.sweep().await;
291            let tunnel = conn::open_tunnel(&self.udid).await.map_err(|e| {
292                Fail::not_sent("tunnel_failed", format!("{e:#}")).hint(t(
293                    "폰이 같은 Wi-Fi에 있거나 USB로 연결되어 있고 잠금이 풀려 있는지 확인하세요. `iph doctor`로 점검할 수 있습니다.",
294                    "Check that the phone is on the same Wi-Fi or plugged in, and unlocked. Run `iph doctor`.",
295                ))
296            })?;
297            self.connects += 1;
298            self.tunnel = Some(tunnel);
299        }
300        Ok(self.tunnel.as_mut().unwrap())
301    }
302
303    async fn ensure_ax(&mut self) -> Res<()> {
304        if self.ax.as_ref().is_some_and(|a| a.is_closed()) {
305            // The accessibility connection died; the tunnel may be gone too.
306            self.reset();
307        }
308        if self.ax.is_none() {
309            let mut last = None;
310            for attempt in 0..2 {
311                let tunnel = self.ensure_tunnel().await?;
312                let mut adapter = tunnel.adapter.clone();
313                match bounded("accessibility service", 12, AxSub::open(&mut adapter, &tunnel.rsd)).await {
314                    Ok(ax) => {
315                        self.ax = Some(ax);
316                        return Ok(());
317                    }
318                    Err(e) => {
319                        last = Some(e);
320                        if attempt == 0 {
321                            self.reset();
322                        }
323                    }
324                }
325            }
326            return Err(Fail::not_sent(
327                "ax_unavailable",
328                last.unwrap_or_else(|| "accessibility service unavailable".into()),
329            )
330            .hint(t(
331                "개발자 모드가 켜져 있고 개발자 디스크 이미지가 연결되어 있어야 합니다. `iph doctor`로 확인하세요.",
332                "Developer Mode must be on and the developer disk image mounted. Run `iph doctor`.",
333            )));
334        }
335        Ok(())
336    }
337
338    /// End the phone's accessibility daemon (`axauditd`) so the phone starts
339    /// a new one. Returns the pid that was ended.
340    ///
341    /// The daemon sometimes goes silent: it runs and takes new connections
342    /// but answers no request, and neither a new tunnel nor another route
343    /// changes that (2026-11-11, twice, both after minutes of work over
344    /// Wi-Fi; the cause is not known). The new one answers at once.
345    async fn restart_ax_daemon(&mut self) -> Result<u32, String> {
346        let mut svc = self.app_service().await.map_err(|f| f.message)?;
347        let processes = bounded("list processes", 10, svc.list_processes()).await?;
348        let pid = processes
349            .iter()
350            .find(|p| {
351                p.executable_url
352                    .as_ref()
353                    .is_some_and(|u| plain_path(&u.relative).ends_with("/axauditd"))
354            })
355            .map(|p| p.pid)
356            .ok_or("axauditd is not among the phone's processes")?;
357        // SIGABRT first: the phone then writes a crash report with every
358        // thread's backtrace, which is the only record of where the daemon
359        // was stuck (Settings > Privacy & Security > Analytics & Improvements
360        // > Analytics Data, "axauditd-…"). SIGKILL after it in case the
361        // daemon does not go.
362        const SIGABRT: u32 = 6;
363        const SIGKILL: u32 = 9;
364        bounded("signal", 10, svc.send_signal(pid, SIGABRT)).await?;
365        tokio::time::sleep(Duration::from_millis(1500)).await;
366        let _ = bounded("signal", 5, svc.send_signal(pid, SIGKILL)).await;
367        Ok(pid)
368    }
369
370    /// One reading of the screen as the raw dump plus its sequence number.
371    /// `full` waits for the second pass (identifiers).
372    pub async fn dump(&mut self, fresh: Fresh, full: bool) -> Res<(u64, Arc<Dump>)> {
373        let mut started = Instant::now();
374        let mut restarted = false;
375        let mut reconnects = 0;
376        let mut second_pass_retries = 0;
377        let mut fresh = fresh;
378        // The connection's counts of empty and not understood answers when
379        // this reading began on it: only what came during the reading tells
380        // how the phone is answering now.
381        let mut base: Option<(Instant, u64, u64)> = None;
382        loop {
383            self.ensure_ax().await?;
384            let ax = self.ax.as_mut().unwrap();
385            if base.is_none_or(|(opened, _, _)| opened != ax.opened) {
386                base = Some((ax.opened, ax.empty_count(), ax.unparsed_count()));
387            }
388            let usable = match (self.stale_before, ax.latest_arrival()) {
389                (Some(limit), Some(arrived)) => arrived >= limit,
390                _ => true,
391            };
392            let cached = match fresh {
393                Fresh::MaxAge(max) if usable => ax
394                    .latest()
395                    .filter(|(_, _, age)| *age <= max)
396                    .map(|(seq, dump, _)| (seq, dump)),
397                _ => None,
398            };
399            let got = match cached {
400                Some(c) => Some(c),
401                None => {
402                    // A connection that has not delivered yet already has its
403                    // answer on the way; asking again would only delay it.
404                    let mut asked_at = if ax.latest().is_none() && ax.opened.elapsed() < ASK_AGAIN {
405                        Some(ax.opened)
406                    } else {
407                        ax.request().ok()
408                    };
409                    let mut got = None;
410                    while let Some(at) = asked_at {
411                        // Anything that arrives sooner than an answer can
412                        // belongs to an earlier request and may predate what
413                        // the caller wants to see.
414                        if let Some(x) = ax.wait_arrived(at + ANSWER_AT_LEAST, ASK_AGAIN).await {
415                            crate::device::ax::trace(|| {
416                                let ms = ax
417                                    .latest_arrival()
418                                    .map(|t| t.saturating_duration_since(at).as_millis())
419                                    .unwrap_or(0);
420                                format!("answer accepted {ms} ms after the request")
421                            });
422                            got = Some(x);
423                            break;
424                        }
425                        if ax.is_closed() || started.elapsed() >= READ_TIMEOUT {
426                            break;
427                        }
428                        asked_at = ax.request().ok();
429                    }
430                    got
431                }
432            };
433            let Some((seq, dump)) = got else {
434                if self.ax.as_ref().is_some_and(|a| a.is_closed()) && reconnects < 2 {
435                    reconnects += 1;
436                    self.reset();
437                    continue;
438                }
439                let (empty, unparsed) = match (self.ax.as_ref(), base) {
440                    (Some(a), Some((_, e0, u0))) => {
441                        (a.empty_count().saturating_sub(e0), a.unparsed_count().saturating_sub(u0))
442                    }
443                    _ => (0, 0),
444                };
445                let up = self.ax.as_ref().is_some_and(|a| !a.is_closed());
446                let stall = if up { ax_stall(empty, unparsed) } else { None };
447                // Empty answers are also what a locked phone might send; a
448                // daemon that is fine is not ended for that.
449                let locked = match stall {
450                    Some(AxStall::EmptyOnly) => match conn::route(&self.udid).await {
451                        Ok(r) => conn::passcode_locked(&*r.provider).await,
452                        Err(_) => None,
453                    },
454                    _ => None,
455                };
456                let lately = self.ax_restarted_at.is_some_and(|at| at.elapsed() < AX_RESTART_GAP);
457                let mut why_not = "";
458                if restarted {
459                    // The daemon the phone started in its place did not
460                    // answer either.
461                    self.ax_restart_unhelpful = true;
462                }
463                if stall.is_some() && locked != Some(true) {
464                    if restarted {
465                        why_not = "it was already ended once during this reading";
466                    } else if self.system_in_front {
467                        why_not = "the home screen or a system panel was in front";
468                    } else if self.ax_restart_unhelpful {
469                        why_not = "ending it last time did not help";
470                    } else if lately {
471                        why_not = "it was ended less than 2 minutes ago";
472                    } else if !self.config.daemon.restart_accessibility {
473                        why_not = "[daemon] restart_accessibility = false";
474                    }
475                }
476                if stall.is_some() && locked != Some(true) && why_not.is_empty() {
477                    restarted = true;
478                    let waited = started.elapsed().as_secs();
479                    let how = match stall {
480                        Some(AxStall::EmptyOnly) => format!("sent only empty answers ({empty})"),
481                        _ => "gave no answer".to_string(),
482                    };
483                    match self.restart_ax_daemon().await {
484                        Ok(pid) => {
485                            eprintln!(
486                                "[{}] the phone's accessibility daemon {how} for {waited} s; ended it (pid {pid}) so the phone starts a new one",
487                                crate::clock::now_utc()
488                            );
489                            self.ax_restarted_at = Some(Instant::now());
490                            self.ax_restarts += 1;
491                            self.ax = None;
492                            tokio::time::sleep(Duration::from_millis(500)).await;
493                            started = Instant::now();
494                            continue;
495                        }
496                        Err(e) => {
497                            eprintln!(
498                                "[{}] the phone's accessibility daemon {how} for {waited} s and could not be restarted: {e}",
499                                crate::clock::now_utc()
500                            );
501                            why_not = "ending it failed";
502                            self.ax_restart_unhelpful = true;
503                        }
504                    }
505                }
506                let fail = Fail::not_sent(
507                    "ax_timeout",
508                    format!(
509                        "no accessibility tree within {} ms ({empty} empty, {unparsed} not understood)",
510                        started.elapsed().as_millis(),
511                    ),
512                )
513                .with("phone_locked", locked.map(serde_json::Value::Bool).unwrap_or(serde_json::Value::Null));
514                return Err(if locked == Some(true) {
515                    fail.hint(t("폰이 잠겨 있습니다. 잠금을 풀어 주세요.", "The phone is locked. Unlock it."))
516                } else if stall.is_some() && self.system_in_front {
517                    fail.with("ax_restart_skipped", why_not).hint(t(
518                        "폰의 화면 읽기 서비스가 응답하지 않습니다. 홈 화면이나 제어 센터 같은 시스템 화면이 앞에 있습니다. 화면 기록 중에는 제어 센터가 읽히지 않습니다. `iph button home`으로 닫거나 `iph launch`로 앱을 띄운 뒤 다시 읽으세요. 이 두 명령은 화면을 못 읽어도 나갑니다.",
519                        "The phone's screen-reading service is not answering, and the home screen or a system panel such as Control Center is in front. Control Center cannot be read while the screen is being recorded. Close it with `iph button home` or bring an app up with `iph launch`, then read again; both go out without a reading.",
520                    ))
521                } else if stall.is_some() && self.ax_restart_unhelpful {
522                    fail.with("ax_restart_skipped", why_not).hint(t(
523                        "폰의 화면 읽기 서비스를 다시 띄워도 응답이 없습니다. 폰을 잠갔다가 다시 풀어 주세요.",
524                        "The phone's screen-reading service did not answer after a restart either. Lock the phone and unlock it again.",
525                    ))
526                } else if stall.is_some() {
527                    fail.with("ax_restart_skipped", why_not).hint(t(
528                        "폰의 화면 읽기 서비스(axauditd)가 응답하지 않습니다. 폰이 잠긴 것은 아닙니다. 잠시 뒤 다시 시도하세요. iph가 2분에 한 번까지 그 서비스를 다시 띄웁니다.",
529                        "The phone's screen-reading service (axauditd) is not answering. The phone is not locked. Try again shortly; iph restarts that service at most every 2 minutes.",
530                    ))
531                } else {
532                    fail.hint(t(
533                        "화면 읽기 응답을 이해하지 못했습니다. `iph read --raw`로 확인하세요.",
534                        "The screen-reading answers were not understood. Look with `iph read --raw`.",
535                    ))
536                });
537            };
538            if !full {
539                return Ok((seq, dump));
540            }
541            let ax = self.ax.as_mut().unwrap();
542            if let Some(merged) = ax.full(seq) {
543                return Ok((seq, merged));
544            }
545            match ax.wait_full(seq, SECOND_PASS_WAIT).await {
546                Some(merged) => return Ok((seq, merged)),
547                None => {
548                    // A newer reading replaced this one: take that instead.
549                    if ax.seq() > seq && started.elapsed() < READ_TIMEOUT {
550                        fresh = Fresh::MaxAge(Duration::from_secs(5));
551                        continue;
552                    }
553                    // The second pass sometimes stays away (seen right after
554                    // an input). Ask once more before going on without
555                    // identifiers.
556                    if second_pass_retries < 1 && started.elapsed() < READ_TIMEOUT {
557                        second_pass_retries += 1;
558                        fresh = Fresh::Force;
559                        continue;
560                    }
561                    return Ok((seq, dump));
562                }
563            }
564        }
565    }
566
567    pub async fn screen(&mut self, fresh: Fresh, full: bool, opts: BuildOptions) -> Res<(u64, Screen)> {
568        let mut fresh = fresh;
569        let mut last = None;
570        // A reading taken while an app is coming to the front can lack the
571        // application's frame; look again rather than hand out a screen
572        // without a size.
573        for _ in 0..3 {
574            let (seq, dump) = Box::pin(self.dump(fresh, full)).await?;
575            let screen = Screen::build(&dump, opts);
576            self.ax_restart_unhelpful = false;
577            self.system_in_front = crate::dnd::system_ui(&screen);
578            if screen.width > 0.0 && screen.height > 0.0 {
579                self.screen_size = Some((screen.width, screen.height));
580                return Ok((seq, screen));
581            }
582            last = Some((seq, screen));
583            fresh = Fresh::Force;
584        }
585        let (seq, mut screen) = last.unwrap();
586        if let Some((w, h)) = self.screen_size {
587            screen.width = w;
588            screen.height = h;
589        }
590        Ok((seq, screen))
591    }
592
593    /// Open the input session if needed. Fails with `not_sent`: nothing has
594    /// been written to the phone when this returns an error.
595    pub async fn ensure_input(&mut self) -> Res<&mut InputSession> {
596        // A media stream that stopped delivering packets no longer holds the
597        // phone's HID gate open.
598        let stale = self
599            .input
600            .as_ref()
601            .is_some_and(|i| i.media_idle_ms().is_none_or(|ms| ms > 2500));
602        if (stale || self.input_suspect)
603            && let Some(old) = self.input.take() {
604                old.close().await;
605                self.input_closed_at = Some(Instant::now());
606            }
607        self.input_suspect = false;
608        if self.input.is_none() {
609            self.sweep().await;
610            // Keep a new session a little apart from the one before it.
611            // (Lost first gestures turned out to be touches sent before the
612            // phone had opened the session's devices, which
613            // `InputSession::open` now waits for; the gap stays as a margin.
614            // `IPH_REOPEN_GAP_MS` overrides it.)
615            if let Some(closed) = self.input_closed_at {
616                let gap = std::env::var("IPH_REOPEN_GAP_MS")
617                    .ok()
618                    .and_then(|v| v.parse().ok())
619                    .map(Duration::from_millis)
620                    .unwrap_or(REOPEN_GAP);
621                let since = closed.elapsed();
622                if since < gap {
623                    tokio::time::sleep(gap - since).await;
624                }
625            }
626            let mut last = String::new();
627            for attempt in 0..2 {
628                let video = self.config.daemon.screen_video;
629                let tunnel = self.ensure_tunnel().await?;
630                let mut adapter = tunnel.adapter.clone();
631                match bounded("input session", 20, InputSession::open(&mut adapter, &mut tunnel.rsd, video)).await {
632                    Ok(s) => {
633                        self.input = Some(s);
634                        break;
635                    }
636                    Err(e) => {
637                        last = e;
638                        if attempt == 0 && is_transport_error(&last) {
639                            self.reset();
640                        } else {
641                            break;
642                        }
643                    }
644                }
645            }
646            if self.input.is_none() {
647                return Err(Fail::not_sent("input_unavailable", last).hint(t(
648                    "터치 입력에는 개발자 디스크 이미지의 display service가 필요합니다. `iph doctor`로 확인하세요.",
649                    "Touch input needs the display service from the developer disk image. Run `iph doctor`.",
650                )));
651            }
652        }
653        self.input_used = Instant::now();
654        Ok(self.input.as_mut().unwrap())
655    }
656
657    /// Send one gesture. The phone sometimes closes the input connections on
658    /// its own (it answers the next report with a reset, seen 2026-11-11);
659    /// iph only finds out when it writes. When that write fails before a
660    /// single report went out, nothing reached the phone, so a new session is
661    /// opened and the gesture is sent once more. A gesture that failed
662    /// halfway is never repeated.
663    pub async fn send_gesture(&mut self, gesture: Gesture) -> Res<()> {
664        for attempt in 0..2 {
665            let input = self.ensure_input().await?;
666            let before = input.reports_sent();
667            let sent = match gesture {
668                Gesture::Tap { x, y, hold } => input.tap(x, y, hold).await,
669                Gesture::DoubleTap { x, y } => input.double_tap(x, y).await,
670                Gesture::Drag { from, to, duration, motion } => input.drag(from, to, duration, motion).await,
671                Gesture::Button { page, usage, hold } => input.button(page, usage, hold).await,
672            };
673            let Err(e) = sent else {
674                return Ok(());
675            };
676            let untouched = input.reports_sent() == before;
677            self.drop_input();
678            if attempt == 0 && untouched {
679                crate::device::ax::trace(|| format!("gesture not sent ({e:#}); trying once on a new session"));
680                eprintln!("[{}] input connection was closed ({e:#}); sending again on a new session", crate::clock::now_utc());
681                continue;
682            }
683            return Err(Fail::unknown("input_failed", format!("{e:#}")).hint(t(
684                "입력이 나갔을 수 있습니다. 다시 보내지 말고 `iph read`로 화면을 확인하세요.",
685                "The input may have gone out. Do not resend; check the screen with `iph read`.",
686            )));
687        }
688        unreachable!("the loop returns on its second pass")
689    }
690
691    /// Close the input session when it has not been used for a while; the
692    /// media stream keeps the phone encoding video.
693    pub async fn close_idle_input(&mut self) {
694        let idle = Duration::from_secs(self.config.daemon.input_idle_secs);
695        if self.input.is_some() && self.input_used.elapsed() > idle
696            && let Some(s) = self.input.take() {
697                s.close().await;
698                self.input_closed_at = Some(Instant::now());
699            }
700    }
701
702    /// End the input session now.
703    pub async fn close_input(&mut self) {
704        if let Some(s) = self.input.take() {
705            s.close().await;
706            self.input_closed_at = Some(Instant::now());
707        }
708    }
709
710    /// With `input_idle_secs = 0` the input session ends with each command,
711    /// so the phone shows "screen sharing" only while input is being sent.
712    /// For apps that hide their content or complain while the screen is
713    /// shared. Every input then pays for a new session (3 to 4 s).
714    pub async fn close_input_if_unheld(&mut self) {
715        if self.config.daemon.input_idle_secs == 0
716            && let Some(s) = self.input.take() {
717                s.close().await;
718                self.input_closed_at = Some(Instant::now());
719            }
720    }
721
722    pub async fn shutdown(&mut self) {
723        if let Some(s) = self.input.take() {
724            s.close().await;
725        }
726        self.sweep().await;
727        self.reset();
728    }
729
730    async fn rsd_client<T>(&mut self) -> Res<T>
731    where
732        T: RsdService,
733    {
734        let mut last = String::new();
735        for attempt in 0..2 {
736            let tunnel = self.ensure_tunnel().await?;
737            let mut adapter = tunnel.adapter.clone();
738            match bounded("connect", 12, T::connect_rsd(&mut adapter, &mut tunnel.rsd)).await {
739                Ok(c) => return Ok(c),
740                Err(e) => {
741                    last = e;
742                    if attempt == 0 && is_transport_error(&last) {
743                        self.reset();
744                    } else {
745                        break;
746                    }
747                }
748            }
749        }
750        Err(Fail::not_sent(
751            "service_unavailable",
752            format!("{}: {last}", T::rsd_service_name()),
753        ))
754    }
755
756    pub async fn app_service(&mut self) -> Res<AppServiceClient<Box<dyn ReadWrite>>> {
757        self.rsd_client().await
758    }
759
760    pub async fn pasteboard(&mut self) -> Res<PasteboardServiceClient<Box<dyn ReadWrite>>> {
761        self.rsd_client().await
762    }
763
764    pub async fn list_apps(&mut self, refresh: bool) -> Res<Vec<AppListEntry>> {
765        if refresh || self.apps.is_none() {
766            // The plain `listapps` call gets no answer on iOS 27; the phone
767            // advertises a streaming list instead. Use whichever is on offer.
768            let streams = self.ensure_tunnel().await?.rsd.services.get(APP_SERVICE).is_some_and(|svc| {
769                svc.features
770                    .as_ref()
771                    .is_some_and(|f| f.iter().any(|x| x == "com.apple.coredevice.feature.streamapplist"))
772            });
773            let mut svc = self.app_service().await?;
774            let apps: Vec<AppListEntry> = if streams {
775                use futures_util::TryStreamExt;
776                bounded(
777                    "stream app list",
778                    30,
779                    svc.stream_apps(true, true, false, false, true).try_collect(),
780                )
781                .await
782            } else {
783                bounded("list apps", 30, svc.list_apps(true, true, false, false, true)).await
784            }
785            .map_err(|e| Fail::not_sent("apps_failed", e))?;
786            self.apps = Some(apps);
787        }
788        Ok(self.apps.clone().unwrap_or_default())
789    }
790
791    pub async fn screenshot_png(&mut self) -> Res<Vec<u8>> {
792        let mut svc: ScreenCaptureServiceClient<Box<dyn ReadWrite>> = self.rsd_client().await?;
793        bounded("screenshot", 20, svc.take_screenshot(None, ImageFormat::Png))
794            .await
795            .map_err(|e| Fail::not_sent("screenshot_failed", e))
796    }
797}
798
799#[cfg(test)]
800mod tests {
801    use super::{AxStall, ax_stall, plain_path};
802
803    #[test]
804    fn paths_compare_across_spellings() {
805        assert_eq!(
806            plain_path("file:///private/var/containers/Bundle/Application/AB/My%20App.app/"),
807            "/var/containers/Bundle/Application/AB/My App.app"
808        );
809        assert_eq!(plain_path("/Applications/Preferences.app"), "/Applications/Preferences.app");
810        assert_eq!(plain_path("file:///Applications/%EB%A9%94%EB%AA%A8.app/x"), "/Applications/메모.app/x");
811    }
812
813    #[test]
814    fn a_daemon_that_sends_only_empty_answers_is_restarted_too() {
815        // Before: only a silent daemon was ended (`empty == 0 && unparsed == 0`),
816        // and one that sent three empty answers a reading stayed stuck.
817        assert_eq!(ax_stall(0, 0), Some(AxStall::Silent));
818        assert_eq!(ax_stall(3, 0), Some(AxStall::EmptyOnly));
819        // Answers the parser could not read are not the daemon's fault.
820        assert_eq!(ax_stall(0, 1), None);
821        assert_eq!(ax_stall(2, 1), None);
822    }
823}