From 73c2c8def11cd70b33da2df34b7c17b1361c5089 Mon Sep 17 00:00:00 2001 From: geeksville Date: Thu, 11 Jun 2020 14:03:10 -0700 Subject: [PATCH] Fix #39: If unlucky device could be beginning sleep when we try to send --- .../geeksville/mesh/service/MeshService.kt | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 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 5a08bfd12..9383b2a4b 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -888,6 +888,13 @@ class MeshService : Service(), Logging { } } + private fun sendNow(p: DataPacket) { + val packet = toMeshPacket(p) + p.status = MessageStatus.ENROUTE + p.time = System.currentTimeMillis() // update time to the actual time we started sending + sendToRadio(packet) + } + /// Process any packets that showed up too early private fun processEarlyPackets() { earlyReceivedPackets.forEach { processReceivedMeshPacket(it) } @@ -895,11 +902,7 @@ class MeshService : Service(), Logging { offlineSentPackets.forEach { p -> // encapsulate our payload in the proper protobufs and fire it off - val packet = toMeshPacket(p) - p.status = MessageStatus.ENROUTE - p.time = - System.currentTimeMillis() // update time to the actual time we started sending - sendToRadio(packet) + sendNow(p) broadcastMessageStatus(p) } offlineSentPackets.clear() @@ -1468,6 +1471,12 @@ class MeshService : Service(), Logging { } } + + private fun enqueueForSending(p: DataPacket) { + p.status = MessageStatus.QUEUED + offlineSentPackets.add(p) + } + val binder = object : IMeshService.Stub() { override fun setDeviceAddress(deviceAddr: String?) = toRemoteExceptions { @@ -1527,19 +1536,18 @@ class MeshService : Service(), Logging { sentPackets[p.id] = p } - // If radio is sleeping, queue the packet + // If radio is sleeping or disconnected, queue the packet when (connectionState) { - ConnectionState.DEVICE_SLEEP -> { - p.status = MessageStatus.QUEUED - offlineSentPackets.add(p) - } - else -> { - p.status = MessageStatus.ENROUTE - - // encapsulate our payload in the proper protobufs and fire it off - val packet = toMeshPacket(p) - sendToRadio(packet) - } + ConnectionState.CONNECTED -> + try { + sendNow(p) + } catch (ex: Exception) { + // This can happen if a user is unlucky and the device goes to sleep after the GUI starts a send, but before we update connectionState + errormsg("Error sending message, so enqueueing", ex) + enqueueForSending(p) + } + else -> // sleeping or disconnected + enqueueForSending(p) } GeeksvilleApplication.analytics.track(