Skip to content

Commit

Permalink
Fix incoming keys
Browse files Browse the repository at this point in the history
  • Loading branch information
garthvh committed Sep 10, 2024
1 parent f52a091 commit 2daf3e3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
28 changes: 21 additions & 7 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1429,8 +1429,15 @@
"Bad" : {

},
"Bad Packets: %d" : {

"Bad Packets: %d %@%%" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Bad Packets: %1$d %2$@%%"
}
}
}
},
"Bandwidth" : {

Expand Down Expand Up @@ -16079,11 +16086,15 @@
"Override automatic OLED screen detection." : {

},
"Packets Received: %d" : {

},
"Packets Sent: %d" : {

"Packets: Sent: %d Received: %d" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Packets: Sent: %1$d Received: %2$d"
}
}
}
},
"password" : {
"localizations" : {
Expand Down Expand Up @@ -22314,6 +22325,9 @@
}
}
}
},
"Uptime: %@" : {

},
"Use a PWM output (like the RAK Buzzer) for tunes instead of an on/off output. This will ignore the output, output duration and active settings and use the device config buzzer GPIO option instead." : {

Expand Down
14 changes: 9 additions & 5 deletions Meshtastic/Helpers/MeshPackets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,10 @@ func textMessageAppPacket(
newMessage.isEmoji = packet.decoded.emoji == 1
newMessage.channel = Int32(packet.channel)
newMessage.portNum = Int32(packet.decoded.portnum.rawValue)
newMessage.publicKey = packet.publicKey
newMessage.pkiEncrypted = packet.pkiEncrypted
if newMessage.toUser?.pkiEncrypted ?? false {
newMessage.pkiEncrypted = true
newMessage.publicKey = packet.publicKey
}
if packet.decoded.portnum == PortNum.detectionSensorApp {
if !UserDefaults.enableDetectionNotifications {
newMessage.read = true
Expand All @@ -889,9 +891,11 @@ func textMessageAppPacket(
newMessage.fromUser?.newPublicKey = newMessage.publicKey
}
} else {
/// We have no key, set it
newMessage.fromUser?.publicKey = packet.publicKey
newMessage.fromUser?.pkiEncrypted = packet.pkiEncrypted
/// We have no key, set it if it is not empty
if !packet.publicKey.isEmpty {
newMessage.fromUser?.pkiEncrypted = true
newMessage.fromUser?.publicKey = packet.publicKey
}
}
if packet.rxTime > 0 {
newMessage.fromUser?.userNode?.lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(packet.rxTime)))
Expand Down
21 changes: 13 additions & 8 deletions Meshtastic/Persistence/UpdateCoreData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext)
newUser.role = Int32(newUserMessage.role.rawValue)
newUser.hwModel = String(describing: newUserMessage.hwModel).uppercased()
newUser.hwModelId = Int32(newUserMessage.hwModel.rawValue)
newUser.pkiEncrypted = packet.pkiEncrypted
newUser.publicKey = packet.publicKey
if !newUserMessage.publicKey.isEmpty {
newUser.pkiEncrypted = true
newUser.publicKey = newUserMessage.publicKey
}

Task {
Api().loadDeviceHardwareData { (hw) in
let dh = hw.first(where: { $0.hwModel == newUser.hwModelId })
Expand All @@ -209,8 +212,10 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext)
} else {
if packet.from > Constants.minimumNodeNum {
let newUser = createUser(num: Int64(packet.from), context: context)
newNode.user?.pkiEncrypted = packet.pkiEncrypted
newNode.user?.publicKey = packet.publicKey
if !packet.publicKey.isEmpty {
newNode.user?.pkiEncrypted = true
newNode.user?.publicKey = packet.publicKey
}
newNode.user = newUser
}
}
Expand All @@ -224,6 +229,7 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext)
myInfoEntity.rebootCount = 0
do {
try context.save()
Logger.data.info("💾 [NodeInfo] Saved a NodeInfo for node number: \(packet.from.toHex(), privacy: .public)")
Logger.data.info("💾 [MyInfoEntity] Saved a new myInfo for node number: \(packet.from.toHex(), privacy: .public)")
} catch {
context.rollback()
Expand Down Expand Up @@ -271,10 +277,9 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext)
fetchedNode[0].user!.role = Int32(nodeInfoMessage.user.role.rawValue)
fetchedNode[0].user!.hwModel = String(describing: nodeInfoMessage.user.hwModel).uppercased()
fetchedNode[0].user!.hwModelId = Int32(nodeInfoMessage.user.hwModel.rawValue)

if !packet.publicKey.isEmpty {
fetchedNode[0].user!.pkiEncrypted = packet.pkiEncrypted
fetchedNode[0].user!.publicKey = packet.publicKey
if !nodeInfoMessage.user.publicKey.isEmpty {
fetchedNode[0].user!.pkiEncrypted = true
fetchedNode[0].user!.publicKey = nodeInfoMessage.user.publicKey
}
Task {
Api().loadDeviceHardwareData { (hw) in
Expand Down
15 changes: 10 additions & 5 deletions Meshtastic/Views/Bluetooth/Connect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct Connect: View {
}
VStack {
let localStats = node?.telemetries?.filtered(using: NSPredicate(format: "metricsType == 6")).lastObject as? TelemetryEntity

if localStats != nil {
Divider()
if localStats?.numTotalNodes ?? 0 >= 100 {
Expand All @@ -111,25 +112,29 @@ struct Connect: View {
.font(.caption)
.fontWeight(.medium)
.foregroundStyle(.secondary)
Text("Packets Sent: \(localStats?.numPacketsTx ?? 0)")
Text("Packets: Sent: \(localStats?.numPacketsTx ?? 0) Received: \(localStats?.numPacketsRx ?? 0)")
.font(.caption)
.fontWeight(.medium)
.foregroundStyle(.secondary)
Text("Packets Received: \(localStats?.numPacketsRx ?? 0)")
let errorRate = (Double(localStats?.numPacketsRxBad ?? -1) / Double(localStats?.numPacketsRx ?? -1)) * 100
Text("Bad Packets: \(localStats?.numPacketsRxBad ?? 0) \(String(format: "Error Rate: %.2f", errorRate))%")
.font(.caption)
.fontWeight(.medium)
.foregroundStyle(.secondary)
Text("Bad Packets: \(localStats?.numPacketsRxBad ?? 0)")
.fixedSize()
let now = Date.now
let later = now + TimeInterval(Double(localStats?.numPacketsRxBad ?? 0))
let uptime = (now..<later).formatted(.components(style: .narrow))
Text("Uptime: \(uptime)")
.font(.caption)
.fontWeight(.medium)
.foregroundStyle(.secondary)
.fixedSize()
}
}
}
.font(.caption)
.foregroundColor(Color.gray)
.padding([.top, .bottom])
.padding([.top])
.swipeActions {
Button(role: .destructive) {
if let connectedPeripheral = bleManager.connectedPeripheral,
Expand Down
2 changes: 1 addition & 1 deletion Widgets/WidgetsLiveActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ struct NodeInfoView: View {
var timerRange: ClosedRange<Date>

var body: some View {
let errorRate = (Double(badReceivedPackets) / Double(receivedPackets)) * 100
VStack(alignment: .leading, spacing: 0) {
Text(nodeName)
.font(nodeName.count > 14 ? .callout : .title3)
Expand All @@ -208,7 +209,6 @@ struct NodeInfoView: View {
.foregroundStyle(.secondary)
.opacity(isLuminanceReduced ? 0.8 : 1.0)
.fixedSize()
let errorRate = (Double(badReceivedPackets) / Double(receivedPackets)) * 100
Text("Bad: \(badReceivedPackets) \(String(format: "Error Rate: %.2f", errorRate))%")
.font(.caption)
.fontWeight(.medium)
Expand Down

0 comments on commit 2daf3e3

Please sign in to comment.