Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
run: |
# what the installer runs or installs, and what the comma runs as
# root; the bench scripts are not held to it yet
shellcheck install.sh scripts/jetlink jetlink/comma/jetlink-root.sh scripts/comma/jetlink-root.sh tests/installer/*.sh
shellcheck install.sh scripts/jetlink jetlink/comma/jetlink-root.sh scripts/comma/jetlink-root.sh scripts/comma/jetlink-usbmux.sh scripts/upstream/fetch_usbmux_deps.sh tests/installer/*.sh

- name: Run the installer scenarios
run: tests/installer/run.sh
Expand Down
140 changes: 140 additions & 0 deletions .github/workflows/upstream-usbmux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Tracks upstream libimobiledevice/usbmuxd, commaai/agnos-builder (and
# agnos-kernel-sdm845 defconfig), and zoompilot/jetlink for changes that could
# break Jetlink's USB Host-mode iOS usbmuxd transport.
#
# Pull requests check against the pins in scripts/upstream/USBMUX_PINS.json.
# Weekly scheduled runs (and manual 'latest' dispatches) check against upstream
# HEAD, summarize any new commits/AGNOS versions, and open a tracking issue on
# breaking contract changes.
name: Upstream usbmuxd & AGNOS

on:
push:
branches: [main, usbmuxd-transport]
paths:
- jetlink/comma/usbmux_transport.py
- scripts/comma/jetlink-usbmux.sh
- scripts/upstream/USBMUX_PINS.json
- scripts/upstream/fetch_usbmux_deps.sh
- scripts/upstream/check_usbmux_deps.py
- .github/workflows/upstream-usbmux.yml
pull_request:
paths:
- jetlink/comma/usbmux_transport.py
- scripts/comma/jetlink-usbmux.sh
- scripts/upstream/USBMUX_PINS.json
- scripts/upstream/fetch_usbmux_deps.sh
- scripts/upstream/check_usbmux_deps.py
- .github/workflows/upstream-usbmux.yml
schedule:
- cron: "23 7 * * 1"
workflow_dispatch:
inputs:
mode:
description: "Check pinned commits or latest upstream HEAD"
type: choice
default: latest
options:
- latest
- pinned

concurrency:
group: upstream-usbmux-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

permissions:
contents: read

defaults:
run:
shell: bash

jobs:
check-upstream:
name: usbmuxd & AGNOS compatibility
runs-on: ubuntu-24.04
if: github.event_name != 'schedule' || github.repository == 'moltenglue/jetlink' || github.repository == 'zoompilot/jetlink'
outputs:
report: ${{ steps.audit.outputs.report }}
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Fetch sparse upstream dependencies
run: |
DEST=$RUNNER_TEMP/usbmux-deps
echo "DEST=$DEST" >> "$GITHUB_ENV"
ARGS=()
if [[ "${{ github.event_name }}" == "schedule" || ( "${{ github.event_name }}" == "workflow_dispatch" && "${{ inputs.mode }}" == "latest" ) ]]; then
ARGS+=(--latest)
fi
scripts/upstream/fetch_usbmux_deps.sh "$DEST" "${ARGS[@]}"

- name: Check contracts & pin drift
id: audit
run: |
REPORT=$RUNNER_TEMP/usbmux-upstream-report.txt
set +e
PYTHONPATH=. python3 -I scripts/upstream/check_usbmux_deps.py check "$DEST" | tee "$REPORT"
STATUS=${PIPESTATUS[0]}
set -e
{
echo 'report<<END_OF_REPORT'
cat "$REPORT"
echo 'END_OF_REPORT'
} >> "$GITHUB_OUTPUT"
{
echo '```'
cat "$REPORT"
echo '```'
if [[ $STATUS -eq 0 ]]; then
echo "To update pins locally: \`scripts/upstream/fetch_usbmux_deps.sh /tmp/ux --latest && python3 scripts/upstream/check_usbmux_deps.py update-pins /tmp/ux\`"
fi
} >> "$GITHUB_STEP_SUMMARY"
exit "$STATUS"

issue:
name: Weekly upstream alert
needs: check-upstream
if: ${{ !cancelled() && github.event_name == 'schedule' && (github.repository == 'moltenglue/jetlink' || github.repository == 'zoompilot/jetlink') }}
runs-on: ubuntu-24.04
permissions:
issues: write
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
TITLE: "upstream-usbmux: usbmuxd or AGNOS upstream contract changed"
RESULT: ${{ needs.check-upstream.result }}
REPORT: ${{ needs.check-upstream.outputs.report }}
RUN: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
steps:
- name: Open, refresh or close issue
run: |
number=$(gh issue list --state open --limit 200 --json number,title --jq "map(select(.title == \"$TITLE\"))[0].number // empty")
if [[ "$RESULT" == "success" ]]; then
if [[ -n "$number" ]]; then
gh issue close "$number" --comment "Upstream usbmuxd/AGNOS checks are green again ($RUN)."
fi
exit 0
fi
{
echo "The weekly usbmuxd / AGNOS upstream compatibility check failed ($RUN)."
echo
echo '```'
echo "${REPORT:-no report: fetch or check failed}"
echo '```'
echo
echo "Update jetlink/comma/usbmux_transport.py or scripts/comma/jetlink-usbmux.sh, then run:"
echo '```bash'
echo "scripts/upstream/fetch_usbmux_deps.sh /tmp/ux --latest"
echo "python3 scripts/upstream/check_usbmux_deps.py check /tmp/ux"
echo "python3 scripts/upstream/check_usbmux_deps.py update-pins /tmp/ux"
echo '```'
} > "$RUNNER_TEMP/issue.md"
if [[ -n "$number" ]]; then
gh issue edit "$number" --body-file "$RUNNER_TEMP/issue.md"
else
gh issue create --title "$TITLE" --body-file "$RUNNER_TEMP/issue.md"
fi
7 changes: 7 additions & 0 deletions JetlinkKit/Sources/JetlinkKit/ControlProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public struct LinkEvent: Codable, Sendable, Equatable {
/// jetlink/openpilot/link.py's Link says in its hello.
public var isDrive: Bool { state == .connected && client == "modeld" }

/// True when the connection arrived over loopback (127.0.0.1 / ::1),
/// which on iOS is a comma USB Host-mode usbmuxd tunnel.
public var isLoopback: Bool {
guard let peer else { return false }
return peer.hasPrefix("127.0.0.1:") || peer.hasPrefix("[::1]:") || peer.hasPrefix("::1:")
}

/// What a connected link is carried over, for display: the server's word,
/// or for a server or comma older than the field, a guess from the peer.
/// The Python server's USB host says "usb", and a peer on the comma's cable
Expand Down
24 changes: 21 additions & 3 deletions JetlinkKit/Sources/JetlinkServer/Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ public final class TCPTransport: @unchecked Sendable {
private let polls = UnsafeMutablePointer<pollfd>.allocate(capacity: 2)

/// A port and a new token for the comma to send frames to as datagrams,
/// or nil off the cable: only a phone's USB link is offered them. The
/// session's loop thread calls this, as it calls `recv()`.
/// or nil off the cable: only CDC-NCM (`medium == .usb`) is offered them.
/// CDC-NCM reaches ~4.3 ms/frame at best on 393 KB frames with UDP JFRM
/// (~7.35 ms over TCP) because of Linux 4.9 `u_ether`; USB Host-mode
/// usbmuxd tunnels report `.usb3`/`.usb2` over loopback and bypass `u_ether`,
/// targeting multi-MB full-frame payloads over raw TCP directly.
/// The session's loop thread calls this, as it calls `recv()`.
public func offerDatagrams() -> (port: UInt16, token: UInt32)? {
guard medium == .usb || TCPTransport.datagramsAnyPeer else { return nil }
if datagrams == nil {
Expand Down Expand Up @@ -299,11 +303,22 @@ public final class TCPTransport: @unchecked Sendable {
/// A listening TCP socket. `accept` polls so `close` can stop it from another
/// thread, and frees the port at once.
public final class TCPListener: @unchecked Sendable {
/// Loopback bind address used for USB Host-mode usbmuxd tunnels on iOS so
/// experimental mode never exposes a LAN socket or requires Local Network permission.
public static let loopbackHost = "127.0.0.1"

public let port: UInt16
private let fd: Int32
private let lock = NSLock()
private var closed = false

/// Listens strictly on 127.0.0.1 (IPv4 loopback) with TCP_NODELAY and
/// pre-allocated receive buffers on accepted connections, keeping TestFlight
/// builds 100% public-API (no USBDriverKit or private APIs).
public static func loopback(port: UInt16 = Wire.defaultPort) throws -> TCPListener {
try TCPListener(host: TCPListener.loopbackHost, port: port, dualStack: false)
}

/// Listens on `host`, or with `dualStack` on every address: IPv6 with IPv4
/// mapped in, since a phone may resolve a name to either, else IPv4 on `host`.
public init(host: String = "0.0.0.0", port: UInt16 = Wire.defaultPort, dualStack: Bool = false) throws {
Expand Down Expand Up @@ -416,7 +431,10 @@ public final class TCPListener: @unchecked Sendable {
getnameinfo($0, length, &host, .init(host.count), &service, .init(service.count), NI_NUMERICHOST | NI_NUMERICSERV)
}
}
return (String(cString: host), UInt16(String(cString: service)) ?? 0)
return (
String(decoding: host.prefix { $0 != 0 }.map(UInt8.init(bitPattern:)), as: UTF8.self),
UInt16(String(decoding: service.prefix { $0 != 0 }.map(UInt8.init(bitPattern:)), as: UTF8.self)) ?? 0
)
}

public func close() {
Expand Down
Loading
Loading