Meshtastic-Apple/Meshtastic/Views/Settings/Config/NetworkConfig.swift

179 lines
6.1 KiB
Swift
Raw Normal View History

2022-08-01 21:57:03 -07:00
//
2023-03-30 11:23:19 -07:00
// NetworkConfig.swift
2022-08-01 21:57:03 -07:00
// Meshtastic
//
// Copyright (c) Garth Vander Houwen 8/1/2022
//
import MeshtasticProtobufs
2024-06-03 02:17:55 -07:00
import OSLog
import SwiftUI
2022-08-01 21:57:03 -07:00
2022-09-10 17:14:03 -07:00
struct NetworkConfig: View {
2023-03-06 10:33:18 -08:00
2022-08-01 21:57:03 -07:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
2023-03-06 10:33:18 -08:00
2022-08-01 21:57:03 -07:00
var node: NodeInfoEntity?
2023-03-06 10:33:18 -08:00
2022-08-01 21:57:03 -07:00
@State var hasChanges: Bool = false
2022-09-10 17:14:03 -07:00
@State var wifiEnabled = false
@State var wifiSsid = ""
@State var wifiPsk = ""
@State var wifiMode = 0
2022-09-27 22:18:50 -07:00
@State var ntpServer = ""
@State var ethEnabled = false
@State var ethMode = 0
2023-03-06 10:33:18 -08:00
2022-08-01 21:57:03 -07:00
var body: some View {
VStack {
Form {
ConfigHeader(title: "Network", config: \.networkConfig, node: node, onAppear: setNetworkValues)
if node != nil && node?.metadata?.hasWifi ?? false {
2023-03-30 11:23:19 -07:00
Section(header: Text("WiFi Options")) {
Toggle(isOn: $wifiEnabled) {
Label("enabled", systemImage: "wifi")
Text("Enabling WiFi will disable the bluetooth connection to the app.")
2023-03-30 11:23:19 -07:00
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2023-03-30 11:23:19 -07:00
HStack {
Label("ssid", systemImage: "network")
TextField("ssid", text: $wifiSsid)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
2024-10-05 15:50:57 -07:00
.onChange(of: wifiSsid) {
2024-09-22 08:03:18 -07:00
var totalBytes = wifiSsid.utf8.count
2023-03-30 11:23:19 -07:00
// Only mess with the value if it is too big
2024-09-22 08:03:18 -07:00
while totalBytes > 32 {
wifiSsid = String(wifiSsid.dropLast())
2024-09-22 08:03:18 -07:00
totalBytes = wifiSsid.utf8.count
2022-08-02 09:44:59 -07:00
}
2023-03-30 11:23:19 -07:00
hasChanges = true
2024-10-05 15:50:57 -07:00
}
2023-03-30 11:23:19 -07:00
.foregroundColor(.gray)
}
.keyboardType(.default)
HStack {
Label("password", systemImage: "wallet.pass")
TextField("password", text: $wifiPsk)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
2024-10-05 15:50:57 -07:00
.onChange(of: wifiPsk) {
2024-09-22 08:03:18 -07:00
var totalBytes = wifiPsk.utf8.count
2023-03-30 11:23:19 -07:00
// Only mess with the value if it is too big
2024-09-22 08:03:18 -07:00
while totalBytes > 63 {
wifiPsk = String(wifiPsk.dropLast())
2024-09-22 08:03:18 -07:00
totalBytes = wifiPsk.utf8.count
2022-08-02 09:44:59 -07:00
}
2023-03-30 11:23:19 -07:00
hasChanges = true
2024-10-05 15:50:57 -07:00
}
2023-03-30 11:23:19 -07:00
.foregroundColor(.gray)
}
.keyboardType(.default)
2022-08-02 09:44:59 -07:00
}
}
if node != nil && node?.metadata?.hasEthernet ?? false {
2023-03-30 11:23:19 -07:00
Section(header: Text("Ethernet Options")) {
Toggle(isOn: $ethEnabled) {
Label("enabled", systemImage: "network")
Text("Enabling Ethernet will disable the bluetooth connection to the app.")
2023-03-30 11:23:19 -07:00
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
2022-08-01 21:57:03 -07:00
}
}
2022-09-15 12:34:10 -07:00
.scrollDismissesKeyboard(.interactively)
.disabled(self.bleManager.connectedPeripheral == nil || node?.networkConfig == nil)
2023-03-06 10:33:18 -08:00
SaveConfigButton(node: node, hasChanges: $hasChanges) {
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
if connectedNode != nil {
var network = Config.NetworkConfig()
network.wifiEnabled = self.wifiEnabled
network.wifiSsid = self.wifiSsid
network.wifiPsk = self.wifiPsk
network.ethEnabled = self.ethEnabled
// network.addressMode = Config.NetworkConfig.AddressMode.dhcp
let adminMessageId = bleManager.saveNetworkConfig(config: network, 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-08-01 21:57:03 -07:00
}
}
}
}
2022-12-13 07:49:46 -08:00
.navigationTitle("network.config")
.navigationBarItems(
trailing: ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: bleManager.connectedPeripheral?.shortName ?? "?"
)
}
)
2022-08-01 21:57:03 -07:00
.onAppear {
2023-01-31 22:08:03 -08:00
// Need to request a NetworkConfig from the remote node before allowing changes
if bleManager.connectedPeripheral != nil && node?.networkConfig == nil {
2024-06-03 02:17:55 -07:00
Logger.mesh.info("empty network config")
2023-01-31 22:08:03 -08:00
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
2023-03-05 14:40:07 -08:00
if node != nil && connectedNode != nil {
_ = bleManager.requestNetworkConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
2023-01-31 22:08:03 -08:00
}
}
2022-08-01 21:57:03 -07:00
}
2024-09-04 10:27:06 -07:00
.onFirstAppear {
// Need to request a NetworkConfig from the remote node before allowing changes
if let connectedPeripheral = bleManager.connectedPeripheral, let node {
Logger.mesh.info("empty network config")
let connectedNode = getNodeInfo(id: connectedPeripheral.num, context: context)
if let connectedNode {
if node.num != connectedNode.num {
if UserDefaults.enableAdministration {
/// 2.5 Administration with session passkey
let expiration = node.sessionExpiration ?? Date()
if expiration < Date() || node.networkConfig == nil {
_ = bleManager.requestNetworkConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
}
} else {
/// Legacy Administration
2024-09-04 10:27:06 -07:00
_ = bleManager.requestNetworkConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
}
}
}
}
}
2024-08-11 17:31:27 -07:00
.onChange(of: wifiEnabled) {
if $0 != node?.networkConfig?.wifiEnabled { hasChanges = true }
2022-09-10 17:14:03 -07:00
}
2024-10-05 15:50:57 -07:00
.onChange(of: wifiSsid) { _, newSSID in
if newSSID != node?.networkConfig?.wifiSsid { hasChanges = true }
2022-09-10 17:14:03 -07:00
}
2024-10-05 15:50:57 -07:00
.onChange(of: wifiPsk) { _, newPsk in
if newPsk != node?.networkConfig?.wifiPsk { hasChanges = true }
}
2024-08-11 17:31:27 -07:00
.onChange(of: wifiMode) {
if $0 != node?.networkConfig?.wifiMode ?? -1 { hasChanges = true }
2022-08-01 21:57:03 -07:00
}
2024-08-11 17:31:27 -07:00
.onChange(of: ethEnabled) {
if $0 != node?.networkConfig?.ethEnabled { hasChanges = true }
}
2022-08-01 21:57:03 -07:00
}
func setNetworkValues() {
self.wifiEnabled = node?.networkConfig?.wifiEnabled ?? false
self.wifiSsid = node?.networkConfig?.wifiSsid ?? ""
self.wifiPsk = node?.networkConfig?.wifiPsk ?? ""
self.wifiMode = Int(node?.networkConfig?.wifiMode ?? 0)
self.ethEnabled = node?.networkConfig?.ethEnabled ?? false
self.hasChanges = false
}
2022-08-01 21:57:03 -07:00
}