2022-06-22 15:52:51 -07:00
|
|
|
//
|
|
|
|
|
// SerialConfig.swift
|
|
|
|
|
// Meshtastic Apple
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) Garth Vander Houwen 6/22/22.
|
|
|
|
|
//
|
2024-06-07 22:09:20 -05:00
|
|
|
import MeshtasticProtobufs
|
2024-06-03 02:17:55 -07:00
|
|
|
import OSLog
|
2024-06-07 22:09:20 -05:00
|
|
|
import SwiftUI
|
2022-06-22 15:52:51 -07:00
|
|
|
|
|
|
|
|
struct SerialConfig: View {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
2022-12-09 18:19:00 -08:00
|
|
|
@Environment(\.dismiss) private var goBack
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
var node: NodeInfoEntity?
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-28 20:20:02 -07:00
|
|
|
@State private var isPresentingSaveConfirm: Bool = false
|
|
|
|
|
@State var hasChanges = false
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
@State var enabled = false
|
|
|
|
|
@State var echo = false
|
|
|
|
|
@State var rxd = 0
|
|
|
|
|
@State var txd = 0
|
|
|
|
|
@State var baudRate = 0
|
|
|
|
|
@State var timeout = 0
|
2023-12-30 09:21:42 -08:00
|
|
|
@State var overrideConsoleSerialPort = false
|
2022-06-22 15:52:51 -07:00
|
|
|
@State var mode = 0
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
var body: some View {
|
|
|
|
|
VStack {
|
|
|
|
|
Form {
|
2024-02-17 22:39:22 -07:00
|
|
|
ConfigHeader(title: "Serial", config: \.serialConfig, node: node, onAppear: setSerialValues)
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2025-04-26 16:05:09 -07:00
|
|
|
Section(header: Text("Options")) {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
Toggle(isOn: $enabled) {
|
2025-04-26 16:05:09 -07:00
|
|
|
Label("Enabled", systemImage: "terminal")
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
Toggle(isOn: $echo) {
|
2022-12-13 17:47:23 -08:00
|
|
|
Label("echo", systemImage: "repeat")
|
2024-02-21 23:35:28 -08:00
|
|
|
Text("If set, any packets you send will be echoed back to your device.")
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-12-13 17:47:23 -08:00
|
|
|
Picker("Baud", selection: $baudRate ) {
|
2022-06-22 15:52:51 -07:00
|
|
|
ForEach(SerialBaudRates.allCases) { sbr in
|
|
|
|
|
Text(sbr.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2024-02-21 23:35:28 -08:00
|
|
|
.listRowSeparator(/*@START_MENU_TOKEN@*/.visible/*@END_MENU_TOKEN@*/)
|
2025-04-27 09:57:49 -07:00
|
|
|
Picker("Timeout", selection: $timeout ) {
|
2022-06-22 15:52:51 -07:00
|
|
|
ForEach(SerialTimeoutIntervals.allCases) { sti in
|
|
|
|
|
Text(sti.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2024-02-21 23:35:28 -08:00
|
|
|
.listRowSeparator(.hidden)
|
2022-06-28 20:20:02 -07:00
|
|
|
Text("The amount of time to wait before we consider your packet as done.")
|
2024-02-21 23:35:28 -08:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
|
.font(.callout)
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-12-13 17:47:23 -08:00
|
|
|
Picker("mode", selection: $mode ) {
|
2022-06-22 15:52:51 -07:00
|
|
|
ForEach(SerialModeTypes.allCases) { smt in
|
|
|
|
|
Text(smt.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
|
|
|
|
}
|
|
|
|
|
Section(header: Text("GPIO")) {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
Picker("Receive data (rxd) GPIO pin", selection: $rxd) {
|
2024-01-22 13:21:17 -08:00
|
|
|
ForEach(0..<49) {
|
2022-06-22 15:52:51 -07:00
|
|
|
if $0 == 0 {
|
2022-12-30 17:44:39 -08:00
|
|
|
Text("unset")
|
2022-06-22 15:52:51 -07:00
|
|
|
} else {
|
|
|
|
|
Text("Pin \($0)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2024-02-21 23:35:28 -08:00
|
|
|
.listRowSeparator(.visible)
|
2022-06-22 15:52:51 -07:00
|
|
|
|
|
|
|
|
Picker("Transmit data (txd) GPIO pin", selection: $txd) {
|
2024-01-22 13:21:17 -08:00
|
|
|
ForEach(0..<49) {
|
2022-06-22 15:52:51 -07:00
|
|
|
if $0 == 0 {
|
2022-12-30 17:44:39 -08:00
|
|
|
Text("unset")
|
2022-06-22 15:52:51 -07:00
|
|
|
} else {
|
|
|
|
|
Text("Pin \($0)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2024-02-21 23:35:28 -08:00
|
|
|
.listRowSeparator(.hidden)
|
2022-06-28 20:20:02 -07:00
|
|
|
Text("Set the GPIO pins for RXD and TXD.")
|
2024-02-21 23:35:28 -08:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
|
.font(.callout)
|
2022-06-28 20:20:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-01 09:19:45 -08:00
|
|
|
.disabled(self.bleManager.connectedPeripheral == nil || node?.serialConfig == nil)
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2024-02-18 00:03:34 -07:00
|
|
|
SaveConfigButton(node: node, hasChanges: $hasChanges) {
|
|
|
|
|
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
|
|
|
|
|
if connectedNode != nil {
|
|
|
|
|
var sc = ModuleConfig.SerialConfig()
|
|
|
|
|
sc.enabled = enabled
|
|
|
|
|
sc.echo = echo
|
|
|
|
|
sc.rxd = UInt32(rxd)
|
|
|
|
|
sc.txd = UInt32(txd)
|
|
|
|
|
sc.baud = SerialBaudRates(rawValue: baudRate)!.protoEnumValue()
|
|
|
|
|
sc.timeout = UInt32(timeout)
|
|
|
|
|
sc.overrideConsoleSerialPort = overrideConsoleSerialPort
|
|
|
|
|
sc.mode = SerialModeTypes(rawValue: mode)!.protoEnumValue()
|
|
|
|
|
|
|
|
|
|
let adminMessageId = bleManager.saveSerialModuleConfig(config: sc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
|
|
|
|
|
|
|
|
|
|
if adminMessageId > 0 {
|
|
|
|
|
// Should show a saved successfully alert once I know that to be true
|
|
|
|
|
// for now just disable the button after a successful save
|
|
|
|
|
hasChanges = false
|
|
|
|
|
goBack()
|
2022-06-28 20:20:02 -07:00
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-12 22:33:06 -08:00
|
|
|
.navigationTitle("serial.config")
|
2024-08-11 09:07:22 -07:00
|
|
|
.navigationBarItems(
|
|
|
|
|
trailing: ZStack {
|
|
|
|
|
ConnectedDevice(
|
|
|
|
|
bluetoothOn: bleManager.isSwitchedOn,
|
|
|
|
|
deviceConnected: bleManager.connectedPeripheral != nil,
|
|
|
|
|
name: bleManager.connectedPeripheral?.shortName ?? "?"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-09-05 19:31:29 -07:00
|
|
|
.onFirstAppear {
|
2023-02-01 09:19:45 -08:00
|
|
|
// Need to request a SerialModuleConfig from the remote node before allowing changes
|
2024-09-05 19:31:29 -07:00
|
|
|
if let connectedPeripheral = bleManager.connectedPeripheral, let node {
|
|
|
|
|
let connectedNode = getNodeInfo(id: connectedPeripheral.num, context: context)
|
|
|
|
|
if let connectedNode {
|
|
|
|
|
if node.num != connectedNode.num {
|
|
|
|
|
if UserDefaults.enableAdministration && node.num != connectedNode.num {
|
|
|
|
|
/// 2.5 Administration with session passkey
|
|
|
|
|
let expiration = node.sessionExpiration ?? Date()
|
|
|
|
|
if expiration < Date() || node.serialConfig == nil {
|
2024-11-29 13:15:46 -08:00
|
|
|
Logger.mesh.info("⚙️ Empty or expired serial module config requesting via PKI admin")
|
2024-09-05 19:31:29 -07:00
|
|
|
_ = bleManager.requestSerialModuleConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/// Legacy Administration
|
2024-11-29 13:15:46 -08:00
|
|
|
Logger.mesh.info("☠️ Using insecure legacy admin, empty serial module config")
|
2024-09-05 19:31:29 -07:00
|
|
|
_ = bleManager.requestSerialModuleConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-01 09:19:45 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: enabled) { oldEnabled, newEnabled in
|
|
|
|
|
if oldEnabled != newEnabled && newEnabled != node?.serialConfig?.enabled ?? false { hasChanges = true }
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: echo) { oldEcho, newEcho in
|
|
|
|
|
if oldEcho != newEcho && newEcho != node?.serialConfig?.echo ?? false { hasChanges = true }
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: rxd) { oldRxd, newRxd in
|
|
|
|
|
if oldRxd != newRxd && newRxd != node?.serialConfig?.rxd ?? -1 { hasChanges = true }
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: txd) { oldTxd, newTxd in
|
|
|
|
|
if oldTxd != newTxd && newTxd != node?.serialConfig?.txd ?? -1 { hasChanges = true }
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: baudRate) { oldBaud, newBaud in
|
|
|
|
|
if oldBaud != newBaud && newBaud != node?.serialConfig?.baudRate ?? -1 { hasChanges = true }
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: timeout) { oldTimeout, newTimeout in
|
|
|
|
|
if oldTimeout != newTimeout && newTimeout != node?.serialConfig?.timeout ?? -1 { hasChanges = true }
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: overrideConsoleSerialPort) { oldOverrideConsoleSerialPort, newOverrideConsoleSerialPort in
|
|
|
|
|
if oldOverrideConsoleSerialPort != newOverrideConsoleSerialPort && newOverrideConsoleSerialPort != node?.serialConfig?.overrideConsoleSerialPort ?? false { hasChanges = true }
|
2023-12-30 09:21:42 -08:00
|
|
|
}
|
2024-10-05 16:35:42 -07:00
|
|
|
.onChange(of: mode) { oldMode, newMode in
|
|
|
|
|
if oldMode != newMode && newMode != node?.serialConfig?.mode ?? -1 { hasChanges = true }
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-19 18:37:23 -07:00
|
|
|
func setSerialValues() {
|
|
|
|
|
self.enabled = node?.serialConfig?.enabled ?? false
|
|
|
|
|
self.echo = node?.serialConfig?.echo ?? false
|
|
|
|
|
self.rxd = Int(node?.serialConfig?.rxd ?? 0)
|
|
|
|
|
self.txd = Int(node?.serialConfig?.txd ?? 0)
|
|
|
|
|
self.baudRate = Int(node?.serialConfig?.baudRate ?? 0)
|
|
|
|
|
self.timeout = Int(node?.serialConfig?.timeout ?? 0)
|
|
|
|
|
self.mode = Int(node?.serialConfig?.mode ?? 0)
|
2023-12-30 09:21:42 -08:00
|
|
|
self.overrideConsoleSerialPort = false // node?.serialConfig?.overrideConsoleSerialPort ?? false
|
2023-03-19 18:37:23 -07:00
|
|
|
self.hasChanges = false
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|