Files
app/ios/DModem/Sources/AppModel.swift
T
Claude befcd0d5ef iOS: the line to another phone over Wi-Fi, found with Bonjour
shared (Kotlin/Native, iOS and Linux): FdDuplex (a socket: the engine's
line, a TCP connection; closing it wakes a blocked read), PosixTcpTransport
(calls taken on a port, polled so close() always ends it; host[:port] and
[IPv6]:port called within 5 s; no SIGPIPE: Linux's send flag, Apple's
socket option), and PeerHost: PeerLine on it from plain values for Swift
(no default arguments or flows there), its state as text. PosixPeerTest
(native): two phones over real TCP on the loopback, the modems socket
pairs: a call rung, answered and heard both ways, busy, the hang-up, a
wrong PIN turned away, nobody there.

DModemKit: ModemSession.onLine, the peer line's modem off hook to the
app (tested: a dial reaches it, its FAILED a NO CARRIER).

The app: the settings' line (a SIP account or another phone on Wi-Fi:
whom ATD calls, the port, the PIN, taking calls), phones found with
Bonjour (_dmodem._tcp, as the Android app's NSD offers them), this one
offered while it takes calls; Home's line its state and last failure.
And a start fixed: the modem's log while it started ended the
background audio (and now the line) before it was running.

The Swift and appleMain's Kotlin aren't built here (no Xcode); the
Kotlin is built and tested for Linux native.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
2026-10-18 02:27:41 +00:00

126 lines
4.6 KiB
Swift

// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
import DModemKit
import Foundation
import SwiftUI
/// The app's state for its views: the modem session (DModemKit) brought to
/// the main thread, the settings kept, the speaker and the background
/// audio that keeps a call going when the app isn't in front.
@MainActor
final class AppModel: ObservableObject {
@Published private(set) var run = ModemSession.Run.stopped
@Published private(set) var status = ModemStatus()
@Published private(set) var log: [String] = []
@Published private(set) var console = ""
@Published private(set) var error: String?
/// when the call came online (for its time on Home)
@Published private(set) var onlineSince: Date?
/// the line to another phone: what it's doing, what went wrong last (nil on a SIP line)
@Published private(set) var peerState: String?
@Published private(set) var peerError: String?
@Published var settings: ModemSettings {
didSet { store.settings = settings }
}
let session = ModemSession()
let terminal = TerminalModel()
private let store = SettingsStore()
private let audio = ModemAudio()
private let peer = PeerLineController()
private var peerTimer: Timer?
init() {
settings = store.settings
session.onChange = { [weak self] in
// (many changes at once, one update)
Task { @MainActor [weak self] in self?.refresh() }
}
session.onData = { [weak self] data in
Task { @MainActor [weak self] in self?.terminal.feed(data) }
}
session.onSpeaker = { [audio] level in audio.speaker(level) }
session.onSound = { [audio] pcm in audio.play(pcm) }
terminal.send = { [weak self] data in self?.session.send(data) }
}
var connectLabel: String { status.link == .online ? ConnectInfo.label(console) : "" }
func start() {
error = nil
audio.enabled = settings.speaker
let dataFile = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
.map { url -> String in
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
return url.appendingPathComponent("modem-data").path
} ?? ""
let s = settings
Task.detached { [session, audio, peer] in
// (the background audio first: the call keeps the app running)
audio.begin()
// (the line to another phone first: the modem's start commands may dial)
if s.peer { peer.start(s, session: session) } else { session.onLine = nil }
if !session.start(s, dns: SystemDNS.servers(), dataFile: dataFile) {
peer.stop()
audio.end()
}
}
watchPeer(s.peer)
}
func stop() {
Task.detached { [session, audio, peer] in
session.stop()
peer.stop()
audio.end()
}
watchPeer(false)
}
/// the peer line's state, twice a second while there's one
private func watchPeer(_ on: Bool) {
peerTimer?.invalidate()
peerTimer = nil
peerState = nil
peerError = nil
guard on else { return }
peerTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
Task { @MainActor [weak self] in
guard let self else { return }
let st = self.peer.stateText
let err = self.peer.lastError
if st != self.peerState { self.peerState = st }
if err != self.peerError { self.peerError = err }
}
}
}
func hangUp() {
Task.detached { [session] in session.hangUp() }
}
/// an AT command (Home's line)
func command(_ cmd: String) { session.command(cmd) }
private func refresh() {
let wasOnline = status.link == .online
let wasRunning = run == .running
run = session.run
status = session.status
log = session.log
console = session.console
error = session.error
if status.link == .online && !wasOnline { onlineSince = Date() }
if status.link != .online { onlineSince = nil }
// (the modem stopped by itself: the call over, the line to the other phone too. Only a stop
// after it ran: while it starts, its log comes before it's running)
if wasRunning && run == .stopped {
audio.end()
if peerTimer != nil {
peer.stop()
watchPeer(false)
}
}
}
}