Skip to content

CarLife USB: reopen the accessory after a session ends without a detach - #129

Merged
riko2chen merged 1 commit into
mainfrom
feature/carlife-usb-reopen
Nov 29, 2026
Merged

riko2chen merged 1 commit into
mainfrom
feature/carlife-usb-reopen

Conversation

@riko2chen

Copy link
Copy Markdown
Contributor

What

  • A USB CarLife lease is live only while its accessory streams are open. When Stream closes them (the session ended: the head unit restarted CarLife, read EOF, protocol failure), the lease opened at that generation is released and usb_lease_lost is reported. Release is keyed by generation, so a late close never drops a newer lease.
  • While no lease is live, the accessory worker sweeps the inventory every 3 s. A lost lease waits for the next sweep rather than reopening at once, so a head unit that ends every session immediately is retried at that interval, not in a tight loop.
  • ACCESSORY_ATTACHED is registered on the receiver as well (usb_attached diagnostic).
  • SecurityException from openAccessory() (grant revoked by a detach while the old entry is still listed) is reported as usb_permission_revoked and retried on the next sweep, instead of stopping reconciliation with usb_error.
  • AccessoryStreams.close() no longer holds a monitor, because the reader thread can end the session during that teardown.

Why

A community tester (OnePlus 7 Pro, CarLife 2.x head unit) reported that after a session ended the picture only came back after restarting the app. #127 already ends the lease on a matching detach. This covers the cases without a detach: a head unit that restarts CarLife on an untouched cable, and attach intents that only reach a foreground activity.

Tests

CarlifeUsbEventsTest now attaches through a seam and ends sessions explicitly, with sweeps driven by a manual timer, so the tests stay deterministic. New cases: session end without detach reopens on the next sweep; closing a detached lease neither reports loss nor drops the next lease; sweeps run only while no lease is live; a revoked grant is retried; the attached broadcast rescans. AccessoryAttachmentTest covers generation-keyed release.

Tests and lint were not run locally; CI runs them. Not yet verified on a head unit.

A head unit can end the CarLife session and restart it on a cable that
was never touched, and Android delivers the accessory attach intent only
to a foreground activity. The lease stayed recorded after its streams had
closed, so every later scan saw the same accessory as connected and
never opened it again until the app restarted.

- A lease is live only while its streams are open. When Stream closes
  them, the lease opened at that generation is released and
  usb_lease_lost is reported; a newer lease is never dropped.
- While no lease is live, the inventory is swept every 3 s on the
  accessory worker. A lost lease waits for the next sweep, so a head unit
  that ends every session at once is retried at that interval.
- ACCESSORY_ATTACHED is also registered on the receiver (usb_attached).
- SecurityException from openAccessory, a grant revoked by a detach while
  the old entry is listed, is retried on the next sweep instead of
  stopping with usb_error.

Tests end sessions explicitly through an attach seam and drive sweeps
with a manual timer, so they stay deterministic.
@riko2chen
riko2chen enabled auto-merge November 29, 2026 16:16
@riko2chen
riko2chen merged commit 9139713 into main Nov 29, 2026
7 checks passed
@riko2chen
riko2chen deleted the feature/carlife-usb-reopen branch November 29, 2026 16:20
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant