From d6f759cabc1d0b5675771e7d76d32b7eb294ac14 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 8 Feb 2021 12:32:23 +0800 Subject: [PATCH] stop using device currentpacketid, first send message could be discarded as dup --- .../geeksville/mesh/service/MeshService.kt | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index ae42489a..45b311f8 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -126,7 +126,7 @@ class MeshService : Service(), Logging { getNodeNum = { myNodeNum } ) - private fun getSenderName(packet : DataPacket?): String { + private fun getSenderName(packet: DataPacket?): String { val name = nodeDBbyID[packet?.from]?.user?.longName return name ?: "Unknown username" } @@ -255,7 +255,8 @@ class MeshService : Service(), Logging { private fun updateMessageNotification(message: DataPacket) = serviceNotifications.updateMessageNotification( - getSenderName(message), message.bytes!!.toString(utf8)) + getSenderName(message), message.bytes!!.toString(utf8) + ) /** * tell android not to kill us @@ -269,7 +270,8 @@ class MeshService : Service(), Logging { // We always start foreground because that's how our service is always started (if we didn't then android would kill us) // but if we don't really need foreground we immediately stop it. val notification = serviceNotifications.createServiceStateNotification( - notificationSummary) + notificationSummary + ) startForeground(serviceNotifications.notifyId, notification) if (!wantForeground) { @@ -633,7 +635,11 @@ class MeshService : Service(), Logging { // Handle position updates from the device if (data.portnumValue == Portnums.PortNum.POSITION_APP_VALUE) { val rxTime = if (packet.rxTime != 0) packet.rxTime else currentSecond() - handleReceivedPosition(packet.from, MeshProtos.Position.parseFrom(data.payload), rxTime) + handleReceivedPosition( + packet.from, + MeshProtos.Position.parseFrom(data.payload), + rxTime + ) } else debug("Ignoring packet sent from our node, portnum=${data.portnumValue} ${bytes.size} bytes") } else { @@ -1390,13 +1396,10 @@ class MeshService : Service(), Logging { if (currentPacketId == 0L) { logAssert(it.packetIdBits == 8 || it.packetIdBits == 32) // Only values I'm expecting (though we don't require this) - val devicePacketId = if (it.currentPacketId == 0L) { - // Old devices don't send their current packet ID, in that case just pick something random and it will probably be fine ;-) - val random = Random(System.currentTimeMillis()) - random.nextLong().absoluteValue - } else - it.currentPacketId - + // We now always pick a random initial packet id (odds of collision with the device is insanely low with 32 bit ids) + val random = Random(System.currentTimeMillis()) + val devicePacketId = random.nextLong().absoluteValue + // Not inited - pick a number on the opposite side of what the device is using currentPacketId = devicePacketId + numPacketIds / 2 } else {