From 9a42b4fb64562d0497e26ebea461cca5032ffe98 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sat, 25 Mar 2023 14:30:18 -0700 Subject: [PATCH] ringtone config --- Meshtastic.xcodeproj/project.pbxproj | 4 + Meshtastic/Helpers/BLEManager.swift | 54 +++++++ .../contents | 5 + Meshtastic/Persistence/UpdateCoreData.swift | 39 +++++ Meshtastic/Protobufs/meshtastic/mesh.pb.swift | 10 ++ .../Settings/Config/Module/RtttlConfig.swift | 145 ++++++++++++++++++ Meshtastic/Views/Settings/Settings.swift | 10 +- 7 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 Meshtastic/Views/Settings/Config/Module/RtttlConfig.swift diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index b7ab23f0..4e131874 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -110,6 +110,7 @@ DDC4D568275499A500A4208E /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC4D567275499A500A4208E /* Persistence.swift */; }; DDC94FC129CE063B0082EA6E /* BatteryLevel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC94FC029CE063B0082EA6E /* BatteryLevel.swift */; }; DDC94FC229CE063B0082EA6E /* BatteryLevel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC94FC029CE063B0082EA6E /* BatteryLevel.swift */; }; + DDC94FCE29CF55310082EA6E /* RtttlConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC94FCD29CF55310082EA6E /* RtttlConfig.swift */; }; DDCDC6CB29481FCC004C1DDA /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = DDCDC6CD29481FCC004C1DDA /* Localizable.strings */; }; DDCE4E2C2869F92900BE9F8F /* UserConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDCE4E2B2869F92900BE9F8F /* UserConfig.swift */; }; DDD3BBD5292D763200D609B3 /* MeshtasticTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDD3BBD4292D763200D609B3 /* MeshtasticTests.swift */; }; @@ -286,6 +287,7 @@ DDC4D567275499A500A4208E /* Persistence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = ""; }; DDC94FC029CE063B0082EA6E /* BatteryLevel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryLevel.swift; sourceTree = ""; }; DDC94FC329CED7280082EA6E /* MeshtasticDataModelV10.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MeshtasticDataModelV10.xcdatamodel; sourceTree = ""; }; + DDC94FCD29CF55310082EA6E /* RtttlConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RtttlConfig.swift; sourceTree = ""; }; DDCDC69A29467643004C1DDA /* MeshtasticDataModelV3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MeshtasticDataModelV3.xcdatamodel; sourceTree = ""; }; DDCDC6CC29481FCC004C1DDA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = ""; }; DDCDC6CE294821AD004C1DDA /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = ""; }; @@ -459,6 +461,7 @@ DD6193742862F6E600E59241 /* ExternalNotificationConfig.swift */, DD2160AE28C5552500C17253 /* MQTTConfig.swift */, DD41582928585C32009B0E59 /* RangeTestConfig.swift */, + DDC94FCD29CF55310082EA6E /* RtttlConfig.swift */, DD6193782863875F00E59241 /* SerialConfig.swift */, DD415827285859C4009B0E59 /* TelemetryConfig.swift */, ); @@ -904,6 +907,7 @@ DDC4D568275499A500A4208E /* Persistence.swift in Sources */, DDD6EEAF29BC024700383354 /* Firmware.swift in Sources */, DD90860E26F69BAE00DC5189 /* NodeMap.swift in Sources */, + DDC94FCE29CF55310082EA6E /* RtttlConfig.swift in Sources */, DD964FBD296E6B01007C176F /* EmojiOnlyTextField.swift in Sources */, DD8169FF272476C700F4AB02 /* LogDocument.swift in Sources */, DDC94FC129CE063B0082EA6E /* BatteryLevel.swift in Sources */, diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index c5471674..5347d8b0 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -1343,6 +1343,33 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { } return 0 } + + public func saveRtttlConfig(config: RTTTLConfig, fromUser: UserEntity, toUser: UserEntity, adminIndex: Int32) -> Int64 { + + var adminPacket = AdminMessage() + adminPacket.setRingtoneMessage = config.ringtone + + var meshPacket: MeshPacket = MeshPacket() + meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { @@ -1736,6 +1763,33 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { } return false } + + public func requestRtttlConfig(fromUser: UserEntity, toUser: UserEntity, adminIndex: Int32) -> Bool { + + var adminPacket = AdminMessage() + adminPacket.getRingtoneRequest = true + + var meshPacket: MeshPacket = MeshPacket() + meshPacket.to = UInt32(toUser.num) + meshPacket.from = UInt32(fromUser.num) + meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Bool { diff --git a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV10.xcdatamodel/contents b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV10.xcdatamodel/contents index 196ac203..a6ce4ed4 100644 --- a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV10.xcdatamodel/contents +++ b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModelV10.xcdatamodel/contents @@ -199,6 +199,7 @@ + @@ -240,6 +241,10 @@ + + + + diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 639a2bb8..40b210f1 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -699,6 +699,45 @@ func upsertExternalNotificationModuleConfigPacket(config: Meshtastic.ModuleConfi } } +func upsertRtttlConfigPacket(config: RTTTLConfig, nodeNum: Int64, context: NSManagedObjectContext) { + + let logString = String.localizedStringWithFormat(NSLocalizedString("mesh.log.rangetest.config %@", comment: "Range Test module config received: %@"), String(nodeNum)) + MeshLogger.log("⛰️ \(logString)") + + let fetchNodeInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") + fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(nodeNum)) + + do { + + guard let fetchedNode = try context.fetch(fetchNodeInfoRequest) as? [NodeInfoEntity] else { + return + } + // Found a node, save RTTTL Config + if !fetchedNode.isEmpty { + if fetchedNode[0].rtttlConfig == nil { + let newRtttlConfig = RTTTLConfigEntity(context: context) + newRtttlConfig.ringtone = config.ringtone + fetchedNode[0].rtttlConfig = newRtttlConfig + } else { + fetchedNode[0].rtttlConfig?.ringtone = config.ringtone + } + do { + try context.save() + print("💾 Updated RTTTL Ringtone Config for node number: \(String(nodeNum))") + } catch { + context.rollback() + let nsError = error as NSError + print("💥 Error Updating Core Data RtttlConfigEntity: \(nsError)") + } + } else { + print("💥 No Nodes found in local database matching node number \(nodeNum) unable to save RTTTL Ringtone Config") + } + } catch { + let nsError = error as NSError + print("💥 Fetching node for core data RtttlConfigEntity failed: \(nsError)") + } +} + func upsertMqttModuleConfigPacket(config: Meshtastic.ModuleConfig.MQTTConfig, nodeNum: Int64, context: NSManagedObjectContext) { let logString = String.localizedStringWithFormat(NSLocalizedString("mesh.log.mqtt.config %@", comment: "MQTT module config received: %@"), String(nodeNum)) diff --git a/Meshtastic/Protobufs/meshtastic/mesh.pb.swift b/Meshtastic/Protobufs/meshtastic/mesh.pb.swift index 7fa22878..03b6c4d1 100644 --- a/Meshtastic/Protobufs/meshtastic/mesh.pb.swift +++ b/Meshtastic/Protobufs/meshtastic/mesh.pb.swift @@ -1587,6 +1587,10 @@ struct NodeInfo { /// Clears the value of `deviceMetrics`. Subsequent reads from it will return its default value. mutating func clearDeviceMetrics() {self._deviceMetrics = nil} + /// + /// local channel index we heard that node on. Only populated if its not the default channel. + var channel: UInt32 = 0 + var unknownFields = SwiftProtobuf.UnknownStorage() init() {} @@ -3181,6 +3185,7 @@ extension NodeInfo: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationB 4: .same(proto: "snr"), 5: .standard(proto: "last_heard"), 6: .standard(proto: "device_metrics"), + 7: .same(proto: "channel"), ] mutating func decodeMessage(decoder: inout D) throws { @@ -3195,6 +3200,7 @@ extension NodeInfo: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationB case 4: try { try decoder.decodeSingularFloatField(value: &self.snr) }() case 5: try { try decoder.decodeSingularFixed32Field(value: &self.lastHeard) }() case 6: try { try decoder.decodeSingularMessageField(value: &self._deviceMetrics) }() + case 7: try { try decoder.decodeSingularUInt32Field(value: &self.channel) }() default: break } } @@ -3223,6 +3229,9 @@ extension NodeInfo: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationB try { if let v = self._deviceMetrics { try visitor.visitSingularMessageField(value: v, fieldNumber: 6) } }() + if self.channel != 0 { + try visitor.visitSingularUInt32Field(value: self.channel, fieldNumber: 7) + } try unknownFields.traverse(visitor: &visitor) } @@ -3233,6 +3242,7 @@ extension NodeInfo: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationB if lhs.snr != rhs.snr {return false} if lhs.lastHeard != rhs.lastHeard {return false} if lhs._deviceMetrics != rhs._deviceMetrics {return false} + if lhs.channel != rhs.channel {return false} if lhs.unknownFields != rhs.unknownFields {return false} return true } diff --git a/Meshtastic/Views/Settings/Config/Module/RtttlConfig.swift b/Meshtastic/Views/Settings/Config/Module/RtttlConfig.swift new file mode 100644 index 00000000..1528e307 --- /dev/null +++ b/Meshtastic/Views/Settings/Config/Module/RtttlConfig.swift @@ -0,0 +1,145 @@ +// +// RingtoneConfig.swift +// Meshtastic +// +// Created by Garth Vander Houwen on 3/25/23. +// + +import SwiftUI + +struct RtttlConfig: View { + + @Environment(\.managedObjectContext) var context + @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack + + var node: NodeInfoEntity? + + @State private var isPresentingSaveConfirm: Bool = false + @State var hasChanges = false + @State var ringtone: String = "" + + var body: some View { + VStack { + Form { + if node != nil && node?.metadata == nil && node?.num ?? 0 != bleManager.connectedPeripheral?.num ?? 0 { + Text("There has been no response to a request for device metadata over the admin channel for this node.") + .font(.callout) + .foregroundColor(.orange) + + } else if node != nil && node?.num ?? 0 != bleManager.connectedPeripheral?.num ?? 0 { + // Let users know what is going on if they are using remote admin and don't have the config yet + if node?.rangeTestConfig == nil { + Text("RTTTL Ringtone config data was requested over the admin channel but no response has been returned from the remote node. You can check the status of admin message requests in the admin message log.") + .font(.callout) + .foregroundColor(.orange) + } else { + Text("Remote administration for: \(node?.user?.longName ?? "Unknown")") + .font(.title3) + .onAppear { + setRtttLConfigValue() + } + } + } else if node != nil && node?.num ?? 0 == bleManager.connectedPeripheral?.num ?? 0 { + Text("Configuration for: \(node?.user?.longName ?? "Unknown")") + .font(.title3) + } else { + Text("Please connect to a radio to configure settings.") + .font(.callout) + .foregroundColor(.orange) + } + Section(header: Text("options")) { + + HStack { + Label("RTTTL Ringtone", systemImage: "music.quarternote.3") + TextField("Ringtone Transfer Language", text: $ringtone, axis: .vertical) + .foregroundColor(.gray) + .autocapitalization(.none) + .disableAutocorrection(true) + .onChange(of: ringtone, perform: { _ in + + let totalBytes = ringtone.utf8.count + // Only mess with the value if it is too big + if totalBytes > 228 { + + let firstNBytes = Data(ringtone.utf8.prefix(228)) + if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) { + // Set the ringtone back to the last place where it was the right size + ringtone = maxBytesString + } + } + }) + .foregroundColor(.gray) + } + .keyboardType(.default) + Text("Ringtone Transfer Language(RTTTL) Ringtone String used by supported buzzers in external notifications.") + .font(.caption) + } + } + .disabled(self.bleManager.connectedPeripheral == nil || node?.rtttlConfig == nil) + Button { + isPresentingSaveConfirm = true + } label: { + Label("save", systemImage: "square.and.arrow.down") + } + .disabled(bleManager.connectedPeripheral == nil || !hasChanges || !(node?.myInfo?.hasWifi ?? false)) + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() + .confirmationDialog( + "are.you.sure", + isPresented: $isPresentingSaveConfirm, + titleVisibility: .visible + ) { + let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown") + let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName) + Button(buttonText) { + + let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) + if connectedNode != nil { + var rtttl = RTTTLConfig() + rtttl.ringtone = ringtone + let adminMessageId = bleManager.saveRtttlConfig(config: rtttl, 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() + } + } + } + } + message: { + Text("config.save.confirm") + } + .navigationTitle("rtttl.config") + .navigationBarItems(trailing: + ZStack { + ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????") + }) + .onAppear { + self.bleManager.context = context + setRtttLConfigValue() + + // Need to request a Rtttl Config from the remote node before allowing changes + if bleManager.connectedPeripheral != nil && node?.rangeTestConfig == nil { + let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context) + if node != nil && connectedNode != nil { + _ = bleManager.requestRangeTestModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0) + } + } + } + .onChange(of: ringtone) { newRingtone in + if node != nil && node!.rtttlConfig != nil { + if newRingtone != node!.rtttlConfig!.ringtone { hasChanges = true } + } + } + } + } + + func setRtttLConfigValue() { + self.ringtone = node?.rtttlConfig?.ringtone ?? "" + self.hasChanges = false + } +} diff --git a/Meshtastic/Views/Settings/Settings.swift b/Meshtastic/Views/Settings/Settings.swift index 6074fb0c..b5c8fbd6 100644 --- a/Meshtastic/Views/Settings/Settings.swift +++ b/Meshtastic/Views/Settings/Settings.swift @@ -35,6 +35,7 @@ struct Settings: View { case externalNotificationConfig case mqttConfig case rangeTestConfig + case ringtoneConfig case serialConfig case telemetryConfig case meshLog @@ -229,7 +230,14 @@ struct Settings: View { .symbolRenderingMode(.hierarchical) Text("range.test") } - .tag(SettingsSidebar.rangeTestConfig) + NavigationLink { + RtttlConfig(node: nodes.first(where: { $0.num == selectedNode })) + } label: { + Image(systemName: "music.note.list") + .symbolRenderingMode(.hierarchical) + Text("ringtone") + } + .tag(SettingsSidebar.ringtoneConfig) NavigationLink { SerialConfig(node: nodes.first(where: { $0.num == selectedNode }))