From db57a862d4187bdea3fdd29818709003dd035d56 Mon Sep 17 00:00:00 2001 From: Jake-B Date: Fri, 21 Mar 2025 16:06:56 -0400 Subject: [PATCH] Added UDP Broadcast to network settings --- Localizable.xcstrings | 6 + .../contents | 3 +- Meshtastic/Persistence/UpdateCoreData.swift | 2 + .../Views/Settings/Config/NetworkConfig.swift | 123 +++++++++++------- 4 files changed, 86 insertions(+), 48 deletions(-) diff --git a/Localizable.xcstrings b/Localizable.xcstrings index 809b2e8e..49e5ffe0 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -9300,6 +9300,9 @@ } } } + }, + "Enable broadcasting packets via UDP over the local network." : { + }, "Enable Notifications" : { "localizations" : { @@ -31501,6 +31504,9 @@ } } } + }, + "UDP Broadcast" : { + }, "Ukraine 433mhz" : { "extractionState" : "manual", diff --git a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV 50.xcdatamodel/contents b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV 50.xcdatamodel/contents index 52f1a59f..3eb01148 100644 --- a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV 50.xcdatamodel/contents +++ b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV 50.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -208,6 +208,7 @@ + diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 0c367968..5e90cd9e 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -658,12 +658,14 @@ func upsertNetworkConfigPacket(config: Config.NetworkConfig, nodeNum: Int64, ses newNetworkConfig.wifiSsid = config.wifiSsid newNetworkConfig.wifiPsk = config.wifiPsk newNetworkConfig.ethEnabled = config.ethEnabled + newNetworkConfig.enabledProtocols = Int32(config.enabledProtocols) fetchedNode[0].networkConfig = newNetworkConfig } else { fetchedNode[0].networkConfig?.ethEnabled = config.ethEnabled fetchedNode[0].networkConfig?.wifiEnabled = config.wifiEnabled fetchedNode[0].networkConfig?.wifiSsid = config.wifiSsid fetchedNode[0].networkConfig?.wifiPsk = config.wifiPsk + fetchedNode[0].networkConfig?.enabledProtocols = Int32(config.enabledProtocols) } if sessionPasskey != nil { fetchedNode[0].sessionPasskey = sessionPasskey diff --git a/Meshtastic/Views/Settings/Config/NetworkConfig.swift b/Meshtastic/Views/Settings/Config/NetworkConfig.swift index 84f48c41..c63e95be 100644 --- a/Meshtastic/Views/Settings/Config/NetworkConfig.swift +++ b/Meshtastic/Views/Settings/Config/NetworkConfig.swift @@ -24,66 +24,78 @@ struct NetworkConfig: View { @State var ntpServer = "" @State var ethEnabled = false @State var ethMode = 0 + @State var udpEnabled = false var body: some View { VStack { Form { ConfigHeader(title: "Network", config: \.networkConfig, node: node, onAppear: setNetworkValues) - if node != nil && node?.metadata?.hasWifi ?? false { - Section(header: Text("WiFi Options")) { + if let node { + if node.metadata?.hasWifi ?? false { + Section(header: Text("WiFi Options")) { - Toggle(isOn: $wifiEnabled) { - Label("enabled", systemImage: "wifi") - Text("Enabling WiFi will disable the bluetooth connection to the app.") - } - .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + Toggle(isOn: $wifiEnabled) { + Label("enabled", systemImage: "wifi") + Text("Enabling WiFi will disable the bluetooth connection to the app.") + } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) - HStack { - Label("ssid", systemImage: "network") - TextField("ssid", text: $wifiSsid) - .foregroundColor(.gray) - .autocapitalization(.none) - .disableAutocorrection(true) - .onChange(of: wifiSsid) { - var totalBytes = wifiSsid.utf8.count - // Only mess with the value if it is too big - while totalBytes > 32 { - wifiSsid = String(wifiSsid.dropLast()) - totalBytes = wifiSsid.utf8.count + HStack { + Label("ssid", systemImage: "network") + TextField("ssid", text: $wifiSsid) + .foregroundColor(.gray) + .autocapitalization(.none) + .disableAutocorrection(true) + .onChange(of: wifiSsid) { + var totalBytes = wifiSsid.utf8.count + // Only mess with the value if it is too big + while totalBytes > 32 { + wifiSsid = String(wifiSsid.dropLast()) + totalBytes = wifiSsid.utf8.count + } + hasChanges = true } - hasChanges = true - } - .foregroundColor(.gray) - } - .keyboardType(.default) - HStack { - Label("password", systemImage: "wallet.pass") - TextField("password", text: $wifiPsk) - .foregroundColor(.gray) - .autocapitalization(.none) - .disableAutocorrection(true) - .onChange(of: wifiPsk) { - var totalBytes = wifiPsk.utf8.count - // Only mess with the value if it is too big - while totalBytes > 63 { - wifiPsk = String(wifiPsk.dropLast()) - totalBytes = wifiPsk.utf8.count + .foregroundColor(.gray) + } + .keyboardType(.default) + HStack { + Label("password", systemImage: "wallet.pass") + TextField("password", text: $wifiPsk) + .foregroundColor(.gray) + .autocapitalization(.none) + .disableAutocorrection(true) + .onChange(of: wifiPsk) { + var totalBytes = wifiPsk.utf8.count + // Only mess with the value if it is too big + while totalBytes > 63 { + wifiPsk = String(wifiPsk.dropLast()) + totalBytes = wifiPsk.utf8.count + } + hasChanges = true } - hasChanges = true - } - .foregroundColor(.gray) + .foregroundColor(.gray) + } + .keyboardType(.default) } - .keyboardType(.default) } - } - if node != nil && node?.metadata?.hasEthernet ?? false { - Section(header: Text("Ethernet Options")) { - Toggle(isOn: $ethEnabled) { - Label("enabled", systemImage: "network") - Text("Enabling Ethernet will disable the bluetooth connection to the app.") + if node.metadata?.hasEthernet ?? false { + Section(header: Text("Ethernet Options")) { + Toggle(isOn: $ethEnabled) { + Label("enabled", systemImage: "network") + Text("Enabling Ethernet will disable the bluetooth connection to the app.") + } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + } + } + + if node.metadata?.hasEthernet ?? false || node.metadata?.hasWifi ?? false { + Section(header: Text("UDP Broadcast")) { + Toggle(isOn: $udpEnabled) { + Label("enabled", systemImage: "point.3.connected.trianglepath.dotted") + Text("Enable broadcasting packets via UDP over the local network.") + } } - .toggleStyle(SwitchToggleStyle(tint: .accentColor)) } } } @@ -98,6 +110,7 @@ struct NetworkConfig: View { network.wifiSsid = self.wifiSsid network.wifiPsk = self.wifiPsk network.ethEnabled = self.ethEnabled + network.enabledProtocols = self.udpEnabled ? UInt32(Config.NetworkConfig.ProtocolFlags.udpBroadcast.rawValue) : UInt32(Config.NetworkConfig.ProtocolFlags.noBroadcast.rawValue) // network.addressMode = Config.NetworkConfig.AddressMode.dhcp let adminMessageId = bleManager.saveNetworkConfig(config: network, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) @@ -166,14 +179,30 @@ struct NetworkConfig: View { } .onChange(of: ethEnabled) { _, newEthEnabled in if newEthEnabled != node?.networkConfig?.ethEnabled { hasChanges = true } + }.onChange(of: udpEnabled) {_, newUdpEnabled in + if let netConfig = node?.networkConfig { + let newValue: UInt32 + if newUdpEnabled { + newValue = UInt32(netConfig.enabledProtocols) | UInt32(Config.NetworkConfig.ProtocolFlags.udpBroadcast.rawValue) + } else { + newValue = UInt32(netConfig.enabledProtocols) & ~UInt32(Config.NetworkConfig.ProtocolFlags.udpBroadcast.rawValue) + } + if netConfig.enabledProtocols != Int32(newValue) { + netConfig.enabledProtocols = Int32(newValue) + hasChanges = true + } + } } } + 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 + let enabledProtocols = UInt32(node?.networkConfig?.enabledProtocols ?? Int32(Config.NetworkConfig.ProtocolFlags.noBroadcast.rawValue)) + self.udpEnabled = enabledProtocols & UInt32(Config.NetworkConfig.ProtocolFlags.udpBroadcast.rawValue) != 0 self.hasChanges = false } }