diff --git a/app/src/main/java/org/dmodem/app/android/modem/ModemService.kt b/app/src/main/java/org/dmodem/app/android/modem/ModemService.kt index bafc6bf..7e40ca1 100644 --- a/app/src/main/java/org/dmodem/app/android/modem/ModemService.kt +++ b/app/src/main/java/org/dmodem/app/android/modem/ModemService.kt @@ -254,10 +254,12 @@ class ModemService : Service() { netKey = if (net != null && lp != null) keyOf(net, lp) else null NativeModem.listener = nativeListener stopping = false + // (anonymous: no account, D-Modem's SIP then registers nothing and dials SIP addresses as they are) + val (sipUser, sipPassword, sipDomain) = settings.sipAccount val config = NativeModem.Config( - user = settings.user, - password = settings.password, - domain = settings.domain, + user = sipUser, + password = sipPassword, + domain = sipDomain, sipPort = settings.sipPort, listen = settings.listen, t38 = settings.t38, diff --git a/app/src/main/java/org/dmodem/app/android/settings/ModemSettings.kt b/app/src/main/java/org/dmodem/app/android/settings/ModemSettings.kt index cb7cfc3..a12cd78 100644 --- a/app/src/main/java/org/dmodem/app/android/settings/ModemSettings.kt +++ b/app/src/main/java/org/dmodem/app/android/settings/ModemSettings.kt @@ -21,6 +21,12 @@ data class ModemSettings( val user: String = "", val password: String = "", val domain: String = "", + /** + * SIP with no account (anonymous): nothing registered, calls dialled to a SIP address as it is + * (sip:user@host, or user@host) from "Anonymous" , and calls that + * come straight to the phone's address taken; the account's fields are kept but not used + */ + val anonymous: Boolean = false, /** the local SIP port */ val sipPort: Int = 5060, /** register, and answer calls (RING on the DTE, ATA or ATS0=n) */ @@ -102,8 +108,20 @@ data class ModemSettings( /** no line at all: the modem on its own (AT commands, the terminal, the bridges); a dial gets NO DIALTONE */ val noLine: Boolean get() = line == LINE_NONE - /** Enough to start: an account (or the line another phone's). */ - val complete: Boolean get() = !sip || (user.isNotBlank() && domain.isNotBlank()) + /** Enough to start: an account, or SIP with none (anonymous), or the line another phone's. */ + val complete: Boolean get() = !sip || anonymous || (user.isNotBlank() && domain.isNotBlank()) + + /** the account the modem's SIP runs with: user, password, domain; all empty with none (anonymous) */ + val sipAccount: Triple + get() = if (anonymous) Triple("", "", "") else Triple(user, password, domain) + + /** the SIP line, in a line: the account, or anonymous */ + val sipSummary: String + get() = when { + anonymous -> "anonymous (no account): dials SIP addresses" + complete -> "$user@$domain" + else -> "not set up" + } /** the commands sent as the modem starts: the init commands, on mobile calls the modulation first */ fun startCommands(): String { @@ -158,6 +176,7 @@ class SettingsStore(private val context: Context) { val password = stringPreferencesKey("password") val domain = stringPreferencesKey("domain") val sipPort = intPreferencesKey("sip_port") + val anonymous = booleanPreferencesKey("sip_anonymous") val listen = booleanPreferencesKey("listen") val t38 = booleanPreferencesKey("t38") val country = stringPreferencesKey("country") @@ -191,6 +210,7 @@ class SettingsStore(private val context: Context) { password = p[Keys.password] ?: d.password, domain = p[Keys.domain] ?: d.domain, sipPort = p[Keys.sipPort] ?: d.sipPort, + anonymous = p[Keys.anonymous] ?: d.anonymous, listen = p[Keys.listen] ?: d.listen, t38 = p[Keys.t38] ?: d.t38, country = p[Keys.country] ?: d.country, @@ -229,6 +249,7 @@ class SettingsStore(private val context: Context) { p[Keys.password] = s.password p[Keys.domain] = s.domain.trim() p[Keys.sipPort] = s.sipPort + p[Keys.anonymous] = s.anonymous p[Keys.listen] = s.listen p[Keys.t38] = s.t38 p[Keys.country] = s.country.trim() diff --git a/app/src/main/java/org/dmodem/app/android/terminal/Directory.kt b/app/src/main/java/org/dmodem/app/android/terminal/Directory.kt index 6a8a6db..253601c 100644 --- a/app/src/main/java/org/dmodem/app/android/terminal/Directory.kt +++ b/app/src/main/java/org/dmodem/app/android/terminal/Directory.kt @@ -24,7 +24,7 @@ data class DialEntry( val id: String = UUID.randomUUID().toString(), val name: String = "", val kind: EntryKind = EntryKind.MODEM, - /** what ATD dials: a number, or a SIP user (MODEM) */ + /** what ATD dials: a number, a SIP user, or a SIP address as it is (sip:user@host, user@host) (MODEM) */ val number: String = "", /** the host, its port and the user (SSH, TELNET, RLOGIN) */ val host: String = "", diff --git a/app/src/main/java/org/dmodem/app/android/ui/DashboardScreen.kt b/app/src/main/java/org/dmodem/app/android/ui/DashboardScreen.kt index 81da76c..67ef5a6 100644 --- a/app/src/main/java/org/dmodem/app/android/ui/DashboardScreen.kt +++ b/app/src/main/java/org/dmodem/app/android/ui/DashboardScreen.kt @@ -337,7 +337,7 @@ private fun LineCard(s: ModemSettings, run: ModemState.Run, network: String?, on } return@Column } - InfoRow("SIP", if (s.complete) "${s.user}@${s.domain}" else "not set up") + InfoRow("SIP", s.sipSummary) InfoRow("Calls in", if (s.listen) "answered (registered)" else "off") InfoRow("Network", network ?: "none") InfoRow("Fax", if (s.t38) "audio or T.38" else "audio") diff --git a/app/src/main/java/org/dmodem/app/android/ui/DirectoryScreen.kt b/app/src/main/java/org/dmodem/app/android/ui/DirectoryScreen.kt index 243c98b..9c695d9 100644 --- a/app/src/main/java/org/dmodem/app/android/ui/DirectoryScreen.kt +++ b/app/src/main/java/org/dmodem/app/android/ui/DirectoryScreen.kt @@ -168,7 +168,10 @@ internal fun EntryDialog(entry: DialEntry, onSave: (DialEntry) -> Unit, onDismis Choice("Connect by", e.kind.label, EntryKind.entries.map { it.label to it }) { e = e.copy(kind = it) } when (e.kind) { EntryKind.MODEM -> { - OutlinedTextField(e.number, { e = e.copy(number = it) }, label = { Text("Number or SIP user") }, singleLine = true) + OutlinedTextField(e.number, { e = e.copy(number = it) }, label = { Text("Number, SIP user, or SIP address") }, + supportingText = { Text("A SIP address (sip:bbs@example.com) is dialled as it is, with no account too") }, + singleLine = true, + ) OutlinedTextField( e.initCommands, { e = e.copy(initCommands = it) }, label = { Text("AT commands before dialling (; between)") }, singleLine = true, diff --git a/app/src/main/java/org/dmodem/app/android/ui/PeerLineUi.kt b/app/src/main/java/org/dmodem/app/android/ui/PeerLineUi.kt index add3571..3c69e04 100644 --- a/app/src/main/java/org/dmodem/app/android/ui/PeerLineUi.kt +++ b/app/src/main/java/org/dmodem/app/android/ui/PeerLineUi.kt @@ -61,7 +61,7 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) { if (BluetoothBridge.hasPermission(context)) onChange(s.copy(line = ModemSettings.LINE_BT_MODEM)) } val lines = listOf( - ModemSettings.LINE_SIP to "A SIP account (calls over the internet)", + ModemSettings.LINE_SIP to "SIP: an account, or anonymous (calls over the internet)", ModemSettings.LINE_WIFI to "Another phone with D-Modem, on Wi-Fi", ModemSettings.LINE_BLUETOOTH to "Another phone with D-Modem, on Bluetooth", ModemSettings.LINE_CELL to "This phone's mobile calls (slow; root)", diff --git a/app/src/main/java/org/dmodem/app/android/ui/PhoneUi.kt b/app/src/main/java/org/dmodem/app/android/ui/PhoneUi.kt index a59b500..c19083c 100644 --- a/app/src/main/java/org/dmodem/app/android/ui/PhoneUi.kt +++ b/app/src/main/java/org/dmodem/app/android/ui/PhoneUi.kt @@ -54,6 +54,7 @@ import kotlinx.coroutines.withContext import org.dmodem.app.android.modem.ModemState import org.dmodem.app.android.ppp.DialIn import org.dmodem.app.android.settings.FaxSettingsStore +import org.dmodem.app.android.settings.SettingsStore import org.dmodem.app.android.settings.VoiceSettingsStore import org.dmodem.app.android.voice.Voice import org.dmodem.app.io.monotonicMs @@ -96,15 +97,23 @@ fun PhoneSection(onOpenSettings: () -> Unit) { val messages by Voice.messages.collectAsStateWithLifecycle() val store = remember { VoiceSettingsStore(context.applicationContext) } val settings by store.settings.collectAsStateWithLifecycle(initialValue = null) + val modem by remember { SettingsStore(context.applicationContext) }.settings.collectAsStateWithLifecycle(initialValue = null) val mic = rememberMicrophone() var callDialog by remember { mutableStateOf(false) } + var sipDialog by remember { mutableStateOf(false) } val on = run == ModemState.Run.RUNNING val free = on && state?.running != true Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { state?.let { VoiceCallCard(it) } Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { - Button(onClick = { callDialog = true }, enabled = free) { Text("Call a number…") } + // (anonymous SIP has no number to call: a SIP address alone) + if (modem?.let { it.sip && it.anonymous } != true) { + Button(onClick = { callDialog = true }, enabled = free) { Text("Call a number…") } + } + if (modem?.sip == true) { + Button(onClick = { sipDialog = true }, enabled = free) { Text("Call a SIP address…") } + } if (ringing != null && free) { OutlinedButton(onClick = { mic { toast(context, Voice.answer(context)) } }) { Text("Answer") } OutlinedButton(onClick = { toast(context, Voice.takeMessage(context)) }) { Text("Take a message") } @@ -132,6 +141,12 @@ fun PhoneSection(onOpenSettings: () -> Unit) { mic { toast(context, Voice.dial(context, number)) } } } + if (sipDialog) { + CallDialog(sip = true, onDismiss = { sipDialog = false }) { address -> + sipDialog = false + mic { toast(context, Voice.dial(context, sipAddress(address))) } + } + } } /** the call now or just ended: who, its phase or outcome, its time; hang up, mute, the loudspeaker, the keypad, or listen and pick up */ @@ -249,17 +264,28 @@ private fun MessageRow(m: Voice.Message) { } } -/** the number to call */ +/** [address] as D-Modem dials a SIP address as it is: with sip: before it when it has no scheme */ +internal fun sipAddress(address: String): String { + val a = address.trim() + return if (a.startsWith("sip:", ignoreCase = true) || a.startsWith("sips:", ignoreCase = true)) a else "sip:$a" +} + +/** the number to call, or ([sip]) the SIP address (user@host), dialled as it is with no account too */ @Composable -private fun CallDialog(onDismiss: () -> Unit, onCall: (String) -> Unit) { +private fun CallDialog(sip: Boolean = false, onDismiss: () -> Unit, onCall: (String) -> Unit) { var number by remember { mutableStateOf("") } AlertDialog( onDismissRequest = onDismiss, - title = { Text("Call a number") }, + title = { Text(if (sip) "Call a SIP address" else "Call a number") }, text = { OutlinedTextField( - value = number, onValueChange = { number = it }, label = { Text("Number") }, singleLine = true, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone), modifier = Modifier.fillMaxWidth(), + value = number, onValueChange = { number = it }, + label = { Text(if (sip) "SIP address" else "Number") }, + placeholder = if (sip) ({ Text("sip:user@host") }) else null, + supportingText = if (sip) ({ Text("Dialled as it is: user@host, or sip:user@host") }) else null, + singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = if (sip) KeyboardType.Uri else KeyboardType.Phone), + modifier = Modifier.fillMaxWidth(), ) }, confirmButton = { Button(onClick = { onCall(number) }, enabled = number.isNotBlank()) { Text("Call") } }, 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 f90a7fd..742db52 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 @@ -184,31 +184,46 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = { SerialModemFields(s) { s = it } } else { Text("SIP account", style = MaterialTheme.typography.titleSmall) - OutlinedTextField( - value = login, - onValueChange = { v -> - login = v - ModemSettings.parseLogin(v.trim())?.let { (u, pw, d) -> - s = s.copy(user = u, password = pw, domain = d) - } + Toggle("No account: anonymous SIP", s.anonymous) { s = s.copy(anonymous = it) } + Text( + if (s.anonymous) { + "Calls go straight to a SIP address, from \"Anonymous\": dial sip:user@host or user@host " + + "(ATDsip:bbs@example.com, or as a directory entry's number); voice calls too. " + + "Nothing is registered, and numbers can't be dialled without an account. " + + "Calls that come straight to this phone's address and SIP port are taken." + } else { + "A SIP address (ATDsip:user@host, or user@host) is dialled as it is with an account too; " + + "the account's password only ever goes to its own server." }, - label = { Text("Paste user:password@host (optional)") }, - singleLine = true, - modifier = Modifier.fillMaxWidth(), + style = MaterialTheme.typography.bodySmall, ) - Field("User", s.user) { s = s.copy(user = it) } - OutlinedTextField( - value = s.password, - onValueChange = { s = s.copy(password = it) }, - label = { Text("Password") }, - singleLine = true, - visualTransformation = PasswordVisualTransformation(), - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password), - modifier = Modifier.fillMaxWidth(), - ) - Field("Server (host[:port])", s.domain) { s = s.copy(domain = it) } + if (!s.anonymous) { + OutlinedTextField( + value = login, + onValueChange = { v -> + login = v + ModemSettings.parseLogin(v.trim())?.let { (u, pw, d) -> + s = s.copy(user = u, password = pw, domain = d) + } + }, + label = { Text("Paste user:password@host (optional)") }, + singleLine = true, + modifier = Modifier.fillMaxWidth(), + ) + Field("User", s.user) { s = s.copy(user = it) } + OutlinedTextField( + value = s.password, + onValueChange = { s = s.copy(password = it) }, + label = { Text("Password") }, + singleLine = true, + visualTransformation = PasswordVisualTransformation(), + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password), + modifier = Modifier.fillMaxWidth(), + ) + Field("Server (host[:port])", s.domain) { s = s.copy(domain = it) } + } NumberField("Local SIP port", s.sipPort) { s = s.copy(sipPort = it) } - Toggle("Take incoming calls (register)", s.listen) { s = s.copy(listen = it) } + Toggle(if (s.anonymous) "Take incoming calls" else "Take incoming calls (register)", s.listen) { s = s.copy(listen = it) } Toggle("Fax over T.38", s.t38) { s = s.copy(t38 = it) } NumberField("SIP log level (0-4)", s.sipLogLevel) { s = s.copy(sipLogLevel = it.coerceIn(0, 4)) } } @@ -363,7 +378,11 @@ private fun lineSummary(s: ModemSettings): String = when (s.line) { ModemSettings.LINE_USB -> "A phone line, through a USB modem" ModemSettings.LINE_BT_MODEM -> "A Bluetooth modem" + s.btModemName.let { if (it.isBlank()) "" else ": $it" } ModemSettings.LINE_SERIAL_MODEM -> "A modem on a USB serial adapter, ${s.serialModemBaud} bit/s" - else -> if (s.complete) "SIP: ${s.user}@${s.domain}" + (if (s.listen) ", taking calls" else "") else "SIP: no account set up yet" + else -> when { + s.anonymous -> "SIP: anonymous, no account" + (if (s.listen) ", taking calls" else "") + s.complete -> "SIP: ${s.user}@${s.domain}" + (if (s.listen) ", taking calls" else "") + else -> "SIP: no account set up yet" + } } /** a one-line text field */ diff --git a/ios/DModem/Sources/HomeView.swift b/ios/DModem/Sources/HomeView.swift index d93cfd1..e32f0ff 100644 --- a/ios/DModem/Sources/HomeView.swift +++ b/ios/DModem/Sources/HomeView.swift @@ -53,7 +53,7 @@ struct HomeView: View { Text(e).foregroundStyle(.red).font(.callout) } } else { - LabeledContent("SIP account", value: model.settings.complete ? "\(model.settings.user)@\(model.settings.domain)" : "not set up") + LabeledContent("SIP account", value: model.settings.sipSummary) LabeledContent("Fax", value: model.settings.t38 ? "audio or T.38" : "audio") } if !model.settings.noLine { diff --git a/ios/DModem/Sources/PhoneView.swift b/ios/DModem/Sources/PhoneView.swift index 2c75cab..b608175 100644 --- a/ios/DModem/Sources/PhoneView.swift +++ b/ios/DModem/Sources/PhoneView.swift @@ -16,6 +16,7 @@ struct PhoneView: View { @ObservedObject var model: AppModel @ObservedObject var jobs: JobsModel @State private var number = "" + @State private var sipAddress = "" @State private var error: String? @State private var messages: [URL] = [] @State private var keypad = false @@ -35,6 +36,22 @@ struct PhoneView: View { } footer: { Text("A voice call through the modem's line, on this phone's speaker and microphone.") } + if model.settings.line == .sip { + Section { + TextField("sip:user@host", text: $sipAddress) + .keyboardType(.URL).textInputAutocapitalization(.never).autocorrectionDisabled() + Button("Call the SIP address") { + ask { error = jobs.voiceDial(ModemSettings.sipAddress(sipAddress)) } + } + .disabled(sipAddress.trimmingCharacters(in: .whitespaces).isEmpty || jobs.running || model.run != .running) + } header: { + Text("Call a SIP address") + } footer: { + Text(model.settings.anonymous + ? "Straight to that address, from \"Anonymous\": no account needed." + : "Straight to that address, dialled as it is (the account's password only goes to its own server).") + } + } Section("Ringing call") { Button("Answer to talk") { ask { error = model.answerVoice() } } .disabled(jobs.running || model.run != .running) diff --git a/ios/DModem/Sources/SettingsView.swift b/ios/DModem/Sources/SettingsView.swift index 5b9b349..3eab26e 100644 --- a/ios/DModem/Sources/SettingsView.swift +++ b/ios/DModem/Sources/SettingsView.swift @@ -49,25 +49,30 @@ struct SettingsView: View { @ViewBuilder private var sipSection: some View { Section { - TextField("User", text: $model.settings.user) - .textInputAutocapitalization(.never).autocorrectionDisabled() - SecureField("Password", text: $model.settings.password) - TextField("Server (host[:port])", text: $model.settings.domain) - .textInputAutocapitalization(.never).autocorrectionDisabled().keyboardType(.URL) - TextField("Or paste user:password@host", text: $login) - .textInputAutocapitalization(.never).autocorrectionDisabled() - .onSubmit { - if let l = ModemSettings.parseLogin(login) { - model.settings.user = l.user - model.settings.password = l.password - model.settings.domain = l.domain - login = "" + Toggle("No account: anonymous SIP", isOn: $model.settings.anonymous) + if !model.settings.anonymous { + TextField("User", text: $model.settings.user) + .textInputAutocapitalization(.never).autocorrectionDisabled() + SecureField("Password", text: $model.settings.password) + TextField("Server (host[:port])", text: $model.settings.domain) + .textInputAutocapitalization(.never).autocorrectionDisabled().keyboardType(.URL) + TextField("Or paste user:password@host", text: $login) + .textInputAutocapitalization(.never).autocorrectionDisabled() + .onSubmit { + if let l = ModemSettings.parseLogin(login) { + model.settings.user = l.user + model.settings.password = l.password + model.settings.domain = l.domain + login = "" + } } - } + } } header: { Text("SIP account") } footer: { - Text("The provider must carry G.711 (PCMU/PCMA) to the phone network.") + Text(model.settings.anonymous + ? "Calls go straight to a SIP address from \"Anonymous\": in the Terminal, ATDsip:user@host (or ATDuser@host); voice calls too. Nothing is registered, and numbers can't be dialled without an account." + : "The provider must carry G.711 (PCMU/PCMA) to the phone network. A SIP address (ATDsip:user@host) is dialled as it is with an account too; the password only goes to the account's own server.") } } diff --git a/ios/DModemKit/Sources/DModemKit/ModemSettings.swift b/ios/DModemKit/Sources/DModemKit/ModemSettings.swift index 7cf3a64..d01b52a 100644 --- a/ios/DModemKit/Sources/DModemKit/ModemSettings.swift +++ b/ios/DModemKit/Sources/DModemKit/ModemSettings.swift @@ -15,6 +15,10 @@ public struct ModemSettings: Codable, Equatable, Sendable { public var password = "" /// the registrar's or proxy's host[:port] public var domain = "" + /// SIP with no account (anonymous): nothing registered, calls dialled to a SIP address as it is + /// (sip:user@host, or user@host) from "Anonymous" , calls that come + /// straight to the phone's address taken; the account's fields kept, not used + public var anonymous = false /// the local SIP port public var sipPort = 5060 /// register and take calls (on Wi-Fi: take the other phone's) @@ -73,7 +77,20 @@ public struct ModemSettings: Codable, Equatable, Sendable { public var noLine: Bool { line == .none } /// enough to start: an account (or the line another phone's) - public var complete: Bool { peer || (!user.isEmpty && !domain.isEmpty) } + public var complete: Bool { peer || anonymous || (!user.isEmpty && !domain.isEmpty) } + + /// the SIP line, in a line: the account, or anonymous + public var sipSummary: String { + if anonymous { return "anonymous (no account)" } + return complete ? "\(user)@\(domain)" : "not set up" + } + + /// [address] as D-Modem dials a SIP address as it is: with sip: before it when it has no scheme + public static func sipAddress(_ address: String) -> String { + let a = address.trimmingCharacters(in: .whitespaces) + let l = a.lowercased() + return l.hasPrefix("sip:") || l.hasPrefix("sips:") ? a : "sip:" + a + } /// the commands sent as the modem starts (empty: none) public func startCommands() -> String { @@ -83,9 +100,10 @@ public struct ModemSettings: Codable, Equatable, Sendable { /// the engine's configuration for these, with the DNS servers the network has public func engineConfig(dns: [String] = [], dataFile: String = "") -> EngineConfig { var c = EngineConfig() - c.user = user - c.password = password - c.domain = domain + // (anonymous: no account, D-Modem's SIP then registers nothing and dials SIP addresses as they are) + c.user = anonymous ? "" : user + c.password = anonymous ? "" : password + c.domain = anonymous ? "" : domain c.sipPort = sipPort c.listen = listen c.t38 = t38 @@ -117,6 +135,7 @@ public struct ModemSettings: Codable, Equatable, Sendable { user = try c.decodeIfPresent(String.self, forKey: .user) ?? d.user password = try c.decodeIfPresent(String.self, forKey: .password) ?? d.password domain = try c.decodeIfPresent(String.self, forKey: .domain) ?? d.domain + anonymous = try c.decodeIfPresent(Bool.self, forKey: .anonymous) ?? d.anonymous sipPort = try c.decodeIfPresent(Int.self, forKey: .sipPort) ?? d.sipPort listen = try c.decodeIfPresent(Bool.self, forKey: .listen) ?? d.listen t38 = try c.decodeIfPresent(Bool.self, forKey: .t38) ?? d.t38