diff --git a/app/src/main/java/org/dmodem/app/android/ui/ConnectionPathView.kt b/app/src/main/java/org/dmodem/app/android/ui/ConnectionPathView.kt new file mode 100644 index 0000000..a3adac0 --- /dev/null +++ b/app/src/main/java/org/dmodem/app/android/ui/ConnectionPathView.kt @@ -0,0 +1,259 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.android.ui + +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Call +import androidx.compose.material.icons.filled.Close +import androidx.compose.material.icons.filled.Phone +import androidx.compose.material.icons.filled.Send +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.drawBehind +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.PathEffect +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.semantics.clearAndSetSemantics +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import org.dmodem.app.android.NativeModem +import org.dmodem.app.android.bridge.Bridges +import org.dmodem.app.android.modem.ModemState +import org.dmodem.app.android.ppp.Internet +import org.dmodem.app.android.settings.BridgeSettings +import org.dmodem.app.android.settings.BridgeSettingsStore +import org.dmodem.app.android.settings.IspSettingsStore +import org.dmodem.app.android.settings.ModemSettings +import org.dmodem.app.path.ConnectionPath + +/** + * The connection path at the top of Settings, as PCOMM's "Connection + * Overview" draws its link: each step's role above its icon, what it is + * below, one line joining them. Idle, it shows how it's all set up (a step + * still to set in the error colour, its line dashed); starting, dialling and + * online, how it is now. A step opens its settings page. + */ +@Composable +fun ConnectionPathCard(s: ModemSettings, modifier: Modifier = Modifier) { + val context = LocalContext.current + val run by ModemState.run.collectAsStateWithLifecycle() + val status by ModemState.status.collectAsStateWithLifecycle() + val internet by Internet.state.collectAsStateWithLifecycle() + val bridges by remember { BridgeSettingsStore(context.applicationContext).settings } + .collectAsStateWithLifecycle(BridgeSettings()) + var isp by remember { mutableStateOf(false) } + var dialIn by remember { mutableStateOf(false) } + LaunchedEffect(Unit) { + IspSettingsStore(context.applicationContext).settings.collect { isp = it.complete; dialIn = it.dialIn } + } + // (a USB modem's line: whether one is plugged in) + val usbPlugged = if (s.usb) rememberUsbModemsPlugged() else null + + val path = ConnectionPath.of( + ConnectionPath.Config( + line = lineOf(s), + sipAnonymous = s.anonymous, + sipUser = s.user, + sipDomain = s.domain, + listen = s.listen, + peerName = s.peerName, + peerAddress = s.peerAddress, + btModemName = s.btModemName, + btModemAddress = s.btModemAddress, + serialBaud = s.serialModemBaud, + serialRtsCts = s.serialModemRtsCts, + usbDevice = usbPlugged?.let { if (it == "none") "" else it }, + cellSpeed = s.cellSpeed, + isp = isp, + dialIn = dialIn, + bridges = bridgeNames(bridges), + ), + ConnectionPath.Live( + run = ConnectionPath.Run.valueOf(run.name), + call = ConnectionPath.Call.valueOf(status.link.name), + rate = rateOf(status), + internet = internet?.connected == true, + ), + ) + ConnectionPathView(path, modifier) +} + +/** [path] drawn: a step's tap opens its settings page */ +@Composable +fun ConnectionPathView(path: ConnectionPath.Path, modifier: Modifier = Modifier) { + val colors = MaterialTheme.colorScheme + Surface( + shape = RoundedCornerShape(24.dp), + color = colors.surfaceContainer, + modifier = modifier.fillMaxWidth().semantics(mergeDescendants = false) {}, + ) { + Column(Modifier.padding(vertical = 12.dp)) { + Row(Modifier.fillMaxWidth().padding(horizontal = 4.dp)) { + path.steps.forEachIndexed { i, step -> + StepColumn( + step, + left = if (i > 0) path.link(i - 1) else null, + right = if (i < path.steps.size - 1) path.link(i) else null, + modifier = Modifier.weight(1f), + ) + } + } + val todo = path.steps.any { it.state == ConnectionPath.State.SETUP } + Text( + path.headline, + style = MaterialTheme.typography.bodyMedium, + color = if (todo) colors.error else colors.onSurfaceVariant, + textAlign = TextAlign.Center, + modifier = Modifier.fillMaxWidth().padding(start = 16.dp, end = 16.dp, top = 8.dp), + ) + } + } +} + +/** a step: its role, its icon on the line (its links to either side), its name and what it is */ +@Composable +private fun StepColumn(step: ConnectionPath.Step, left: ConnectionPath.State?, right: ConnectionPath.State?, modifier: Modifier) { + val colors = MaterialTheme.colorScheme + val tint = stateColor(step.state) + val leftColor = left?.let { stateColor(it) } + val rightColor = right?.let { stateColor(it) } + val dashLeft = left == ConnectionPath.State.SETUP || left == ConnectionPath.State.OFF + val dashRight = right == ConnectionPath.State.SETUP || right == ConnectionPath.State.OFF + Column( + modifier + .clickable { SettingsNav.open(pageOf(step.page)) } + .clearAndSetSemantics { contentDescription = "${step.role}: ${step.title}, ${step.detail}" } + .padding(vertical = 4.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(4.dp), + ) { + Text( + step.role, style = MaterialTheme.typography.labelSmall, color = colors.onSurfaceVariant, + maxLines = 1, overflow = TextOverflow.Ellipsis, + ) + // (the line through the icons' middles: half to the step before, half to the next) + Box( + Modifier.fillMaxWidth().height(48.dp).drawBehind { + val y = size.height / 2 + val w = 2.dp.toPx() + val dash = PathEffect.dashPathEffect(floatArrayOf(6.dp.toPx(), 4.dp.toPx())) + if (leftColor != null) drawLine(leftColor, Offset(0f, y), Offset(size.width / 2, y), w, pathEffect = if (dashLeft) dash else null) + if (rightColor != null) drawLine(rightColor, Offset(size.width / 2, y), Offset(size.width, y), w, pathEffect = if (dashRight) dash else null) + }, + contentAlignment = Alignment.Center, + ) { + val active = step.state == ConnectionPath.State.ACTIVE + Box( + Modifier.size(48.dp) + .background(if (active) colors.primaryContainer else colors.surface, CircleShape) + .border(2.dp, tint, CircleShape), + contentAlignment = Alignment.Center, + ) { + Icon(iconOf(step.kind), contentDescription = null, tint = tint, modifier = Modifier.size(24.dp)) + } + } + Text( + step.title, style = MaterialTheme.typography.labelLarge, color = colors.onSurface, + textAlign = TextAlign.Center, maxLines = 2, overflow = TextOverflow.Ellipsis, + modifier = Modifier.padding(horizontal = 2.dp), + ) + Text( + step.detail, style = MaterialTheme.typography.bodySmall, + color = if (step.state == ConnectionPath.State.SETUP) colors.error else colors.onSurfaceVariant, + textAlign = TextAlign.Center, maxLines = 3, overflow = TextOverflow.Ellipsis, + modifier = Modifier.padding(horizontal = 2.dp), + ) + } +} + +/** a state's colour: ready the primary, to set the error's, off faint, working and active the tertiary */ +@Composable +private fun stateColor(s: ConnectionPath.State): Color { + val c = MaterialTheme.colorScheme + return when (s) { + ConnectionPath.State.READY -> c.primary + ConnectionPath.State.SETUP -> c.error + ConnectionPath.State.OFF -> c.outlineVariant + ConnectionPath.State.WORKING -> c.tertiary + ConnectionPath.State.ACTIVE -> c.tertiary + } +} + +/** a step's icon */ +private fun iconOf(k: ConnectionPath.Kind): ImageVector = when (k) { + ConnectionPath.Kind.TERMINAL -> SettingsIcons.Terminal + ConnectionPath.Kind.PPP -> SettingsIcons.Internet + ConnectionPath.Kind.SOFTMODEM, ConnectionPath.Kind.MODEM -> SettingsIcons.Modem + ConnectionPath.Kind.SERIAL, ConnectionPath.Kind.USB -> SettingsIcons.Bridges + ConnectionPath.Kind.BLUETOOTH -> SettingsIcons.Bluetooth + ConnectionPath.Kind.WIFI -> SettingsIcons.Wifi + ConnectionPath.Kind.SIP, ConnectionPath.Kind.CELL -> Icons.Filled.Call + ConnectionPath.Kind.SIP_ADDRESS -> Icons.Filled.Send + ConnectionPath.Kind.PHONE_NETWORK, ConnectionPath.Kind.PHONE_LINE -> SettingsIcons.Line + ConnectionPath.Kind.PHONE -> Icons.Filled.Phone + ConnectionPath.Kind.NO_LINE, ConnectionPath.Kind.NOTHING -> Icons.Filled.Close +} + +/** the settings page a step's settings are on */ +private fun pageOf(p: ConnectionPath.Page): SettingsPage = when (p) { + ConnectionPath.Page.TERMINAL -> SettingsPage.TERMINAL + ConnectionPath.Page.INTERNET -> SettingsPage.INTERNET + ConnectionPath.Page.BRIDGES -> SettingsPage.BRIDGES + ConnectionPath.Page.MODEM -> SettingsPage.MODEM + ConnectionPath.Page.LINE -> SettingsPage.LINE +} + +/** the settings' line as the path's */ +private fun lineOf(s: ModemSettings): ConnectionPath.Line = when (s.line) { + ModemSettings.LINE_WIFI -> ConnectionPath.Line.WIFI + ModemSettings.LINE_BLUETOOTH -> ConnectionPath.Line.BLUETOOTH + ModemSettings.LINE_CELL -> ConnectionPath.Line.CELL + ModemSettings.LINE_NONE -> ConnectionPath.Line.NONE + ModemSettings.LINE_USB -> ConnectionPath.Line.USB + ModemSettings.LINE_BT_MODEM -> ConnectionPath.Line.BT_MODEM + ModemSettings.LINE_SERIAL_MODEM -> ConnectionPath.Line.SERIAL_MODEM + else -> ConnectionPath.Line.SIP +} + +/** the bridges on, a word each */ +private fun bridgeNames(b: BridgeSettings): List = buildList { + if (b.tcp) add("TCP") + if (b.bluetooth) add("Bluetooth") + if (b.usb || b.usbGadget) add("USB serial") +} + +/** the call's rate, online: "48000/26400 bit/s" (down/up), or one rate when both are the same */ +private fun rateOf(st: NativeModem.Status): String = when { + st.link != NativeModem.Link.ONLINE || (st.rxRate <= 0 && st.txRate <= 0) -> "" + st.rxRate == st.txRate -> "${st.rxRate} bit/s" + else -> "${st.rxRate}/${st.txRate} bit/s" +} diff --git a/app/src/main/java/org/dmodem/app/android/ui/SettingsScreen.kt b/app/src/main/java/org/dmodem/app/android/ui/SettingsScreen.kt index 742db52..c13e9c9 100644 --- a/app/src/main/java/org/dmodem/app/android/ui/SettingsScreen.kt +++ b/app/src/main/java/org/dmodem/app/android/ui/SettingsScreen.kt @@ -310,7 +310,7 @@ private fun settingsTransition(back: Boolean): ContentTransform { } } -/** Settings' menu: a row for each page, what it's set to beneath */ +/** Settings' menu: the connection path, then a row for each page, what it's set to beneath */ @Composable private fun SettingsMenu(modifier: Modifier, s: ModemSettings) { val rows = listOf( @@ -336,6 +336,8 @@ private fun SettingsMenu(modifier: Modifier, s: ModemSettings) { "Settings", style = MaterialTheme.typography.titleLarge, modifier = Modifier.heightIn(min = 48.dp).padding(start = 4.dp).wrapContentHeight(Alignment.CenterVertically), ) + // (how a call is made, step by step: as set up when idle, as it is in a call) + ConnectionPathCard(s) for ((page, summary) in rows) SettingsCard(page, summary) } } diff --git a/docs/features.md b/docs/features.md index 1a29010..3538c40 100644 --- a/docs/features.md +++ b/docs/features.md @@ -17,7 +17,16 @@ answer is off; Answer as a voice call, or Take a message (the answering machine). Gone when the modem answers (here, a computer on a bridge, S0) or the caller gives up. -- **Settings**: the SIP account (or paste `user:password@host`, as +- **Settings**: at the top, the connection path, as PCOMM's "Connection + Overview" draws its link: the terminal, the modem, the line and what it + reaches (for a modem of its own, the USB serial adapter or Bluetooth it's + reached by, the modem and its line), each step's role above its icon and + what it is below, one line joining them. Idle it shows the settings as + they are: a step still to set (no SIP account, no phone or modem chosen, + no USB modem plugged in) in red with its line dashed, and a line saying + what; dialling and online it lights as the call goes. On Android a step + opens its settings page. The shared Kotlin's `ConnectionPath` has the steps and their + words, for Android and iOS alike. Then the SIP account (or paste `user:password@host`, as dmodemd's `SIP_LOGIN`), taking incoming calls, T.38, the AT commands sent at start (`ATX3` by default: SIP has no dial tone), the log levels; the country, from the ITU's list of 192 (D-Modem's, after E.180): diff --git a/ios/DModem/Sources/ConnectionPathView.swift b/ios/DModem/Sources/ConnectionPathView.swift new file mode 100644 index 0000000..ae0f20d --- /dev/null +++ b/ios/DModem/Sources/ConnectionPathView.swift @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +import DModemKit +import DModemShared +import SwiftUI + +/// The connection path at the top of Settings, as PCOMM's "Connection +/// Overview" draws its link: each step's role above its icon, what it is +/// below, one line joining them (the shared Kotlin's ConnectionPath, as the +/// Android app has it). Idle, how it's set up (a step still to set in red, +/// its line dashed); dialling and online, how it is now. +struct ConnectionPathView: View { + @ObservedObject var model: AppModel + @ObservedObject var jobs: JobsModel + + var body: some View { + let path = Self.path(model: model, internet: jobs.internet?.connected == true) + VStack(spacing: 8) { + HStack(alignment: .top, spacing: 0) { + ForEach(Array(path.steps.enumerated()), id: \.offset) { i, step in + StepView( + step: step, + left: i > 0 ? path.link(i: Int32(i - 1)) : nil, + right: i < path.steps.count - 1 ? path.link(i: Int32(i)) : nil + ) + .frame(maxWidth: .infinity) + } + } + Text(path.headline) + .font(.footnote) + .multilineTextAlignment(.center) + .foregroundStyle(path.steps.contains { $0.state == ConnectionPath.State.setup } ? Color.red : Color.secondary) + } + .padding(.vertical, 4) + .accessibilityElement(children: .ignore) + .accessibilityLabel(path.spoken) + } + + /// the path as the settings are, and the modem now + static func path(model: AppModel, internet: Bool) -> ConnectionPath.Path { + let s = model.settings + let line: ConnectionPath.Line + switch s.line { + case .sip: line = .sip + case .wifi: line = .wifi + case .none: line = ConnectionPath.Line.none + } + let call: ConnectionPath.Call + switch model.status.link { + case .idle: call = .idle + case .connecting: call = .connecting + case .online: call = .online + case .hangingUp: call = .hangingUp + } + let st = model.status + let rate = st.link != .online || (st.rxRate <= 0 && st.txRate <= 0) ? "" + : st.rxRate == st.txRate ? "\(st.rxRate) bit/s" : "\(st.rxRate)/\(st.txRate) bit/s" + let config = ConnectionPath.Config( + line: line, sipAnonymous: s.anonymous, sipUser: s.user, sipDomain: s.domain, listen: s.listen, + peerName: "", peerAddress: s.peerAddress, btModemName: "", btModemAddress: "", + serialBaud: 115200, serialRtsCts: true, usbDevice: nil, cellSpeed: "", + isp: !s.ispNumber.isEmpty, dialIn: false, bridges: [] + ) + let live = ConnectionPath.Live( + run: model.run == .running ? .running : .stopped, call: call, rate: rate, internet: internet + ) + return ConnectionPath.shared.of(c: config, live: live) + } +} + +/// a step: its role, its icon on the line (its links to either side), its name and what it is +private struct StepView: View { + let step: ConnectionPath.Step + let left: ConnectionPath.State? + let right: ConnectionPath.State? + + var body: some View { + VStack(spacing: 4) { + Text(step.role).font(.caption2).foregroundStyle(.secondary).lineLimit(1) + ZStack { + // (the line through the icons' middles: half to the step before, half to the next) + GeometryReader { g in + let y = g.size.height / 2 + if let left { + Path { p in p.move(to: CGPoint(x: 0, y: y)); p.addLine(to: CGPoint(x: g.size.width / 2, y: y)) } + .stroke(Self.color(left), style: Self.stroke(left)) + } + if let right { + Path { p in p.move(to: CGPoint(x: g.size.width / 2, y: y)); p.addLine(to: CGPoint(x: g.size.width, y: y)) } + .stroke(Self.color(right), style: Self.stroke(right)) + } + } + Circle() + .fill(step.state == ConnectionPath.State.active ? Self.color(step.state).opacity(0.2) : Color(.systemBackground)) + .overlay(Circle().stroke(Self.color(step.state), lineWidth: 2)) + .frame(width: 44, height: 44) + Image(systemName: Self.symbol(step.kind)) + .font(.system(size: 18, weight: .medium)) + .foregroundStyle(Self.color(step.state)) + } + .frame(height: 44) + Text(step.title).font(.caption.weight(.semibold)).multilineTextAlignment(.center).lineLimit(2) + Text(step.detail) + .font(.caption2) + .multilineTextAlignment(.center) + .lineLimit(3) + .foregroundStyle(step.state == ConnectionPath.State.setup ? Color.red : Color.secondary) + } + .padding(.horizontal, 2) + } + + /// a state's colour: ready the accent, to set red, off faint, dialling and online green + static func color(_ s: ConnectionPath.State) -> Color { + if s == ConnectionPath.State.setup { return .red } + if s == ConnectionPath.State.off { return Color(.tertiaryLabel) } + if s == ConnectionPath.State.working || s == ConnectionPath.State.active { return .green } + return .accentColor + } + + /// a link's line: dashed where a step is still to set, or off + static func stroke(_ s: ConnectionPath.State) -> StrokeStyle { + let dashed = s == ConnectionPath.State.setup || s == ConnectionPath.State.off + return StrokeStyle(lineWidth: 2, dash: dashed ? [6, 4] : []) + } + + /// a step's SF Symbol + static func symbol(_ k: ConnectionPath.Kind) -> String { + switch k { + case .terminal: return "terminal" + case .ppp: return "globe" + case .softmodem, .modem: return "server.rack" + case .serial, .usb: return "cable.connector" + case .bluetooth: return "dot.radiowaves.left.and.right" + case .wifi: return "wifi" + case .sip, .cell: return "phone.arrow.up.right" + case .sipAddress: return "at" + case .phoneNetwork, .phoneLine: return "phone" + case .phone: return "iphone" + default: return "xmark" + } + } +} diff --git a/ios/DModem/Sources/SettingsView.swift b/ios/DModem/Sources/SettingsView.swift index 3eab26e..9bb7999 100644 --- a/ios/DModem/Sources/SettingsView.swift +++ b/ios/DModem/Sources/SettingsView.swift @@ -13,6 +13,10 @@ struct SettingsView: View { var body: some View { Form { + // (how a call is made, step by step: as set up when idle, as it is in a call) + Section { + ConnectionPathView(model: model, jobs: model.jobs) + } Section { Picker("Line", selection: $model.settings.line) { Text("SIP account").tag(ModemSettings.Line.sip) diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/path/ConnectionPath.kt b/shared/src/commonMain/kotlin/org/dmodem/app/path/ConnectionPath.kt new file mode 100644 index 0000000..2319a2c --- /dev/null +++ b/shared/src/commonMain/kotlin/org/dmodem/app/path/ConnectionPath.kt @@ -0,0 +1,252 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.path + +/** + * The connection path: the picture at the top of Settings of how a call is + * made, as PCOMM draws its session's link. Four steps, left to right: + * + * 1. what the user works in: the terminal (dial-up internet's PPP while + * it's up), and what else shares the modem (the bridges, dial-in); + * 2. the modem: D-Modem's own, or for a modem of its own, what the terminal + * reaches it by (a USB serial adapter, Bluetooth); + * 3. the line: SIP, another phone on Wi-Fi or Bluetooth, a USB modem's + * phone line, the phone's mobile calls, none; or the modem of its own; + * 4. what is reached: the phone network, SIP addresses, the other phone. + * + * Idle, each step is as it's set up (ready, or needing something set); + * starting, calling and online, as it is now. Each platform draws the + * steps; the words are here so that Android and iOS say the same. + */ +object ConnectionPath { + + /** the modem's line, as the settings have it (Android's ModemSettings.line, iOS's ModemSettings.Line) */ + enum class Line { SIP, WIFI, BLUETOOTH, CELL, NONE, USB, BT_MODEM, SERIAL_MODEM } + + /** the modem's run and its call, now */ + enum class Run { STOPPED, STARTING, RUNNING, STOPPING } + enum class Call { IDLE, CONNECTING, ONLINE, HANGING_UP } + + /** + * a step's state: [READY] set up (idle); [SETUP] something to set first; + * [OFF] not in use; [WORKING] starting or connecting; [ACTIVE] in the call + */ + enum class State { READY, SETUP, OFF, WORKING, ACTIVE } + + /** what a step is, for its icon */ + enum class Kind { + TERMINAL, PPP, SOFTMODEM, SERIAL, BLUETOOTH, MODEM, SIP, WIFI, CELL, USB, NO_LINE, + PHONE_NETWORK, SIP_ADDRESS, PHONE, PHONE_LINE, NOTHING, + } + + /** the settings page a step's settings are on */ + enum class Page { TERMINAL, INTERNET, BRIDGES, MODEM, LINE } + + /** + * a step: its icon, its name, a line of what it's set to, its state, where it's set; [role] what + * part it plays, the label above it (PCOMM's "Interface", "Attachment", "Type of Host") + */ + data class Step(val kind: Kind, val title: String, val detail: String, val state: State, val page: Page, val role: String = "") + + /** the path: its steps, and the whole in a line */ + data class Path(val steps: List, val headline: String) { + /** the link between step [i] and the next: the lesser of the two (OFF least, then SETUP) */ + fun link(i: Int): State = lesser(steps[i].state, steps[i + 1].state) + + /** the path for a screen reader: the headline, then each step */ + val spoken: String get() = headline + ". " + steps.joinToString(", then ") { "${it.title}: ${it.detail}" } + } + + /** the settings the path is drawn from */ + data class Config( + val line: Line, + /** SIP: no account; the account's user and server; taking calls (registering) */ + val sipAnonymous: Boolean = false, + val sipUser: String = "", + val sipDomain: String = "", + val listen: Boolean = false, + /** another phone's (Wi-Fi, Bluetooth): its name, its address */ + val peerName: String = "", + val peerAddress: String = "", + /** a Bluetooth modem's: its name, its address */ + val btModemName: String = "", + val btModemAddress: String = "", + /** a modem on a USB serial adapter's speed, and RTS/CTS */ + val serialBaud: Int = 115200, + val serialRtsCts: Boolean = true, + /** the USB device plugged in for the line (a USB modem, a serial adapter): its name; "" none; null not known */ + val usbDevice: String? = null, + /** mobile calls' modulation (+MS) */ + val cellSpeed: String = "", + /** dial-up internet's ISP is set up; dial-in for callers is on */ + val isp: Boolean = false, + val dialIn: Boolean = false, + /** the bridges on, in a word each ("TCP", "Bluetooth", "USB serial") */ + val bridges: List = emptyList(), + ) + + /** how it is now: the modem's run, its call, the call's rate, dial-up internet up */ + data class Live( + val run: Run = Run.STOPPED, + val call: Call = Call.IDLE, + val rate: String = "", + val internet: Boolean = false, + ) { + val idle: Boolean get() = call == Call.IDLE && (run == Run.STOPPED || run == Run.RUNNING) && !internet + } + + /** the path for [c], now as [live] is */ + fun of(c: Config, live: Live = Live()): Path { + val steps = mutableListOf(host(c, live)) + steps += when (c.line) { + Line.SERIAL_MODEM, Line.BT_MODEM -> external(c) + else -> softmodem(c, live) + } + // (a call: each step in use lights; idle: as set up) + val now = when { + live.call == Call.ONLINE || live.internet -> State.ACTIVE + live.call == Call.CONNECTING || live.call == Call.HANGING_UP -> State.WORKING + else -> null + } + val roles = if (c.line == Line.SERIAL_MODEM || c.line == Line.BT_MODEM) EXTERNAL_ROLES else ROLES + val shown = steps.mapIndexed { i, it -> + val st = if (now == null || it.state == State.OFF || it.state == State.SETUP) it.state else now + it.copy(state = st, role = roles[i]) + } + return Path(shown, headline(shown, c, live)) + } + + /** a dial-up internet session is shown as PPP; otherwise the terminal, and what shares the modem */ + private fun host(c: Config, live: Live): Step { + if (live.internet) return Step(Kind.PPP, "Internet", "PPP to the ISP", State.READY, Page.INTERNET) + val also = buildList { + if (c.isp) add("dial-up internet") + if (c.dialIn) add("dial-in") + addAll(c.bridges.map { "$it bridge" }) + } + val detail = if (also.isEmpty()) "AT commands and calls" else "Also " + also.joinToString(", ") + return Step(Kind.TERMINAL, "Terminal", detail, State.READY, Page.TERMINAL) + } + + /** D-Modem's own modem, its line, and what it reaches */ + private fun softmodem(c: Config, live: Live): List { + val modem = when (live.run) { + Run.STARTING -> Step(Kind.SOFTMODEM, "D-Modem", "Starting", State.WORKING, Page.MODEM) + Run.STOPPING -> Step(Kind.SOFTMODEM, "D-Modem", "Stopping", State.WORKING, Page.MODEM) + Run.RUNNING -> Step( + Kind.SOFTMODEM, "D-Modem", + if (live.call == Call.ONLINE && live.rate.isNotEmpty()) live.rate else "Running", State.READY, Page.MODEM, + ) + Run.STOPPED -> Step(Kind.SOFTMODEM, "D-Modem", "V.92 softmodem", State.READY, Page.MODEM) + } + return listOf(modem) + when (c.line) { + Line.SIP -> sip(c) + Line.WIFI, Line.BLUETOOTH -> peer(c) + Line.CELL -> listOf( + Step(Kind.CELL, "Mobile call", "This phone's calls" + c.cellSpeed.let { if (it.isEmpty()) "" else ", $it" }, State.READY, Page.LINE), + Step(Kind.PHONE_NETWORK, "Phone network", "Through the mobile network", State.READY, Page.LINE), + ) + Line.USB -> { + val dev = c.usbDevice + listOf( + Step( + Kind.USB, "USB modem", + when { dev == null -> "Its phone line"; dev.isEmpty() -> "None plugged in"; else -> dev }, + if (dev != null && dev.isEmpty()) State.SETUP else State.READY, Page.LINE, + ), + Step(Kind.PHONE_LINE, "Phone line", "The USB modem's", State.READY, Page.LINE), + ) + } + Line.NONE -> listOf( + Step(Kind.NO_LINE, "No line", "Dials get NO DIALTONE", State.OFF, Page.LINE), + Step(Kind.NOTHING, "Nothing", "The modem on its own", State.OFF, Page.LINE), + ) + Line.SERIAL_MODEM, Line.BT_MODEM -> emptyList() + } + } + + /** SIP: an account (registered when taking calls) or none (anonymous) */ + private fun sip(c: Config): List = when { + c.sipAnonymous -> listOf( + Step(Kind.SIP, "SIP", "Anonymous: nothing registered" + if (c.listen) ", takes direct calls" else "", State.READY, Page.LINE), + Step(Kind.SIP_ADDRESS, "SIP addresses", "Dialled directly (sip:user@host)", State.READY, Page.LINE), + ) + c.sipUser.isBlank() || c.sipDomain.isBlank() -> listOf( + Step(Kind.SIP, "SIP", "No account set up", State.SETUP, Page.LINE), + Step(Kind.PHONE_NETWORK, "Phone network", "Through a SIP provider", State.SETUP, Page.LINE), + ) + else -> listOf( + Step(Kind.SIP, "SIP", (if (c.listen) "Registers as " else "Account ") + "${c.sipUser}@${c.sipDomain}", State.READY, Page.LINE), + Step(Kind.PHONE_NETWORK, "Phone network", "Through ${c.sipDomain}", State.READY, Page.LINE), + ) + } + + /** another phone with the app: chosen (to call), or only taking its calls */ + private fun peer(c: Config): List { + val bt = c.line == Line.BLUETOOTH + val who = c.peerName.ifBlank { c.peerAddress } + val chosen = c.peerAddress.isNotBlank() + val link = Step( + if (bt) Kind.BLUETOOTH else Kind.WIFI, if (bt) "Bluetooth" else "Wi-Fi", + when { chosen && bt -> "Paired"; chosen -> "Same network"; c.listen -> "Waiting for its call"; bt -> "Pair and choose a phone"; else -> "Choose a phone" }, + if (chosen || c.listen) State.READY else State.SETUP, Page.LINE, + ) + val other = Step( + Kind.PHONE, "Other phone", + when { chosen -> who; c.listen -> "Any that calls"; else -> "Not chosen yet" }, + if (chosen || c.listen) State.READY else State.SETUP, Page.LINE, + ) + return listOf(link, other) + } + + /** a modem of its own: what the terminal reaches it by, the modem, its phone line */ + private fun external(c: Config): List { + if (c.line == Line.BT_MODEM) { + val chosen = c.btModemAddress.isNotBlank() + return listOf( + Step(Kind.BLUETOOTH, "Bluetooth", if (chosen) "Paired" else "Pair and choose a modem", if (chosen) State.READY else State.SETUP, Page.LINE), + Step(Kind.MODEM, "Bluetooth modem", c.btModemName.ifBlank { c.btModemAddress.ifBlank { "Not chosen yet" } }, if (chosen) State.READY else State.SETUP, Page.LINE), + Step(Kind.PHONE_LINE, "Phone line", "The modem's own", State.READY, Page.LINE), + ) + } + val dev = c.usbDevice + val plugged = dev == null || dev.isNotEmpty() + return listOf( + Step( + Kind.SERIAL, "USB serial", + (if (dev.isNullOrEmpty()) "" else "$dev, ") + "${c.serialBaud} bit/s" + if (c.serialRtsCts) ", RTS/CTS" else "", + if (plugged) State.READY else State.SETUP, Page.LINE, + ), + Step(Kind.MODEM, "Modem", if (plugged) "On the adapter" else "Plug in its adapter", if (plugged) State.READY else State.SETUP, Page.LINE), + Step(Kind.PHONE_LINE, "Phone line", "The modem's own", State.READY, Page.LINE), + ) + } + + /** the whole in a line: what needs setting, or what it's doing, or that it's ready */ + private fun headline(steps: List, c: Config, live: Live): String { + val todo = steps.firstOrNull { it.state == State.SETUP } + return when { + live.internet -> "Online: dial-up internet" + if (live.rate.isNotEmpty()) " at ${live.rate}" else "" + live.call == Call.ONLINE -> "Online" + if (live.rate.isNotEmpty()) " at ${live.rate}" else "" + live.call == Call.CONNECTING -> "Connecting" + live.call == Call.HANGING_UP -> "Hanging up" + todo != null -> "Not ready: ${todo.title} (${todo.detail.replaceFirstChar { it.lowercase() }})" + c.line == Line.NONE -> "No line: the modem answers AT commands only" + live.run == Run.STARTING -> "Starting the modem" + live.run == Run.RUNNING -> "Ready: the modem is running" + else -> "Ready to dial" + } + } + + /** the steps' roles: D-Modem's own modem's path, a modem of its own's */ + private val ROLES = listOf("Program", "Modem", "Line", "Reaches") + private val EXTERNAL_ROLES = listOf("Program", "Adapter", "Modem", "Line") + + /** the lesser of two states: OFF, then SETUP, then WORKING, READY, ACTIVE */ + fun lesser(a: State, b: State): State { + fun rank(s: State) = when (s) { + State.OFF -> 0; State.SETUP -> 1; State.WORKING -> 2; State.READY -> 3; State.ACTIVE -> 4 + } + return if (rank(a) <= rank(b)) a else b + } +} diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/path/ConnectionPathTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/path/ConnectionPathTest.kt new file mode 100644 index 0000000..6e935b4 --- /dev/null +++ b/shared/src/commonTest/kotlin/org/dmodem/app/path/ConnectionPathTest.kt @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.path + +import org.dmodem.app.path.ConnectionPath.Call +import org.dmodem.app.path.ConnectionPath.Config +import org.dmodem.app.path.ConnectionPath.Kind +import org.dmodem.app.path.ConnectionPath.Line +import org.dmodem.app.path.ConnectionPath.Live +import org.dmodem.app.path.ConnectionPath.Run +import org.dmodem.app.path.ConnectionPath.State +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** The connection path: each line's steps, idle as set up, and in a call as it is. */ +class ConnectionPathTest { + private fun kinds(p: ConnectionPath.Path) = p.steps.map { it.kind } + private fun states(p: ConnectionPath.Path) = p.steps.map { it.state } + + @Test + fun sipWithAnAccountRegisters() { + val p = ConnectionPath.of(Config(Line.SIP, sipUser = "alice", sipDomain = "sip.example.com", listen = true)) + assertEquals(listOf(Kind.TERMINAL, Kind.SOFTMODEM, Kind.SIP, Kind.PHONE_NETWORK), kinds(p)) + assertEquals("Registers as alice@sip.example.com", p.steps[2].detail) + assertTrue(states(p).all { it == State.READY }) + assertEquals("Ready to dial", p.headline) + assertEquals(listOf("Program", "Modem", "Line", "Reaches"), p.steps.map { it.role }) + } + + @Test + fun sipWithNoAccountNeedsSettingUp() { + val p = ConnectionPath.of(Config(Line.SIP)) + assertEquals(State.SETUP, p.steps[2].state) + assertEquals(State.SETUP, p.link(1)) + assertEquals("Not ready: SIP (no account set up)", p.headline) + } + + @Test + fun anonymousSipReachesSipAddresses() { + val p = ConnectionPath.of(Config(Line.SIP, sipAnonymous = true)) + assertEquals(Kind.SIP_ADDRESS, p.steps[3].kind) + assertEquals(State.READY, p.steps[2].state) + assertTrue(p.steps[2].detail.startsWith("Anonymous")) + } + + @Test + fun peersNeedPairingAndChoosing() { + val none = ConnectionPath.of(Config(Line.BLUETOOTH)) + assertEquals(listOf(Kind.TERMINAL, Kind.SOFTMODEM, Kind.BLUETOOTH, Kind.PHONE), kinds(none)) + assertEquals("Pair and choose a phone", none.steps[2].detail) + assertEquals(State.SETUP, none.steps[3].state) + val chosen = ConnectionPath.of(Config(Line.WIFI, peerName = "Pixel", peerAddress = "192.168.1.5")) + assertEquals("Pixel", chosen.steps[3].detail) + assertEquals(State.READY, chosen.steps[3].state) + // (taking the other phone's calls needs none chosen) + assertEquals(State.READY, ConnectionPath.of(Config(Line.WIFI, listen = true)).steps[3].state) + } + + @Test + fun aModemOfItsOwnIsReachedByItsAdapter() { + val p = ConnectionPath.of(Config(Line.SERIAL_MODEM, serialBaud = 57600, usbDevice = "FT232R")) + assertEquals(listOf(Kind.TERMINAL, Kind.SERIAL, Kind.MODEM, Kind.PHONE_LINE), kinds(p)) + assertEquals("FT232R, 57600 bit/s, RTS/CTS", p.steps[1].detail) + assertEquals(listOf("Program", "Adapter", "Modem", "Line"), p.steps.map { it.role }) + val unplugged = ConnectionPath.of(Config(Line.SERIAL_MODEM, usbDevice = "")) + assertEquals(State.SETUP, unplugged.steps[1].state) + val bt = ConnectionPath.of(Config(Line.BT_MODEM, btModemName = "Courier", btModemAddress = "00:11:22:33:44:55")) + assertEquals(listOf(Kind.TERMINAL, Kind.BLUETOOTH, Kind.MODEM, Kind.PHONE_LINE), kinds(bt)) + assertEquals("Courier", bt.steps[2].detail) + } + + @Test + fun noLineIsOff() { + val p = ConnectionPath.of(Config(Line.NONE)) + assertEquals(State.OFF, p.steps[2].state) + assertEquals(State.OFF, p.link(1)) + assertEquals(State.READY, p.link(0)) + } + + @Test + fun whatSharesTheModemIsOnTheTerminal() { + val p = ConnectionPath.of(Config(Line.SIP, sipAnonymous = true, isp = true, dialIn = true, bridges = listOf("TCP"))) + assertEquals("Also dial-up internet, dial-in, TCP bridge", p.steps[0].detail) + } + + @Test + fun aCallLightsThePath() { + val c = Config(Line.SIP, sipUser = "alice", sipDomain = "example.com") + val online = ConnectionPath.of(c, Live(Run.RUNNING, Call.ONLINE, rate = "48000/26400 bit/s")) + assertTrue(states(online).all { it == State.ACTIVE }) + assertEquals("Online at 48000/26400 bit/s", online.headline) + assertEquals("48000/26400 bit/s", online.steps[1].detail) + val dialling = ConnectionPath.of(c, Live(Run.RUNNING, Call.CONNECTING)) + assertTrue(states(dialling).all { it == State.WORKING }) + val internet = ConnectionPath.of(c, Live(Run.RUNNING, Call.ONLINE, internet = true)) + assertEquals(Kind.PPP, internet.steps[0].kind) + // (the modem starting: its step works, the rest as set up) + val starting = ConnectionPath.of(c, Live(Run.STARTING)) + assertEquals(listOf(State.READY, State.WORKING, State.READY, State.READY), states(starting)) + } +}