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 72c73ce3a..0ae71fe69 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -1118,55 +1118,55 @@ class MeshService : Service(), Logging { // Important to never throw exceptions out of onReceive override fun onReceive(context: Context, intent: Intent) = exceptionReporter { - serviceScope.handledLaunch { - debug("Received broadcast ${intent.action}") - when (intent.action) { - RadioInterfaceService.RADIO_CONNECTED_ACTION -> { - try { - val connected = intent.getBooleanExtra(EXTRA_CONNECTED, false) - val permanent = intent.getBooleanExtra(EXTRA_PERMANENT, false) - onConnectionChanged( - when { - connected -> ConnectionState.CONNECTED - permanent -> ConnectionState.DISCONNECTED - else -> ConnectionState.DEVICE_SLEEP - } - ) - } catch (ex: RemoteException) { - // This can happen sometimes (especially if the device is slowly dying due to killing power, don't report to crashlytics - warn("Abandoning reconnect attempt, due to errors during init: ${ex.message}") - } + // NOTE: Do not call handledLaunch here, because it can cause out of order message processing - because each routine is scheduled independently + // serviceScope.handledLaunch { + debug("Received broadcast ${intent.action}") + when (intent.action) { + RadioInterfaceService.RADIO_CONNECTED_ACTION -> { + try { + val connected = intent.getBooleanExtra(EXTRA_CONNECTED, false) + val permanent = intent.getBooleanExtra(EXTRA_PERMANENT, false) + onConnectionChanged( + when { + connected -> ConnectionState.CONNECTED + permanent -> ConnectionState.DISCONNECTED + else -> ConnectionState.DEVICE_SLEEP + } + ) + } catch (ex: RemoteException) { + // This can happen sometimes (especially if the device is slowly dying due to killing power, don't report to crashlytics + warn("Abandoning reconnect attempt, due to errors during init: ${ex.message}") } - - RadioInterfaceService.RECEIVE_FROMRADIO_ACTION -> { - val proto = - MeshProtos.FromRadio.parseFrom( - intent.getByteArrayExtra( - EXTRA_PAYLOAD - )!! - ) - info("Received from radio service: ${proto.toOneLineString()}") - when (proto.variantCase.number) { - MeshProtos.FromRadio.PACKET_FIELD_NUMBER -> handleReceivedMeshPacket( - proto.packet - ) - - MeshProtos.FromRadio.CONFIG_COMPLETE_ID_FIELD_NUMBER -> handleConfigComplete( - proto.configCompleteId - ) - - MeshProtos.FromRadio.MY_INFO_FIELD_NUMBER -> handleMyInfo(proto.myInfo) - - MeshProtos.FromRadio.NODE_INFO_FIELD_NUMBER -> handleNodeInfo(proto.nodeInfo) - - MeshProtos.FromRadio.RADIO_FIELD_NUMBER -> handleRadioConfig(proto.radio) - - else -> errormsg("Unexpected FromRadio variant") - } - } - - else -> errormsg("Unexpected radio interface broadcast") } + + RadioInterfaceService.RECEIVE_FROMRADIO_ACTION -> { + val proto = + MeshProtos.FromRadio.parseFrom( + intent.getByteArrayExtra( + EXTRA_PAYLOAD + )!! + ) + info("Received from radio service: ${proto.toOneLineString()}") + when (proto.variantCase.number) { + MeshProtos.FromRadio.PACKET_FIELD_NUMBER -> handleReceivedMeshPacket( + proto.packet + ) + + MeshProtos.FromRadio.CONFIG_COMPLETE_ID_FIELD_NUMBER -> handleConfigComplete( + proto.configCompleteId + ) + + MeshProtos.FromRadio.MY_INFO_FIELD_NUMBER -> handleMyInfo(proto.myInfo) + + MeshProtos.FromRadio.NODE_INFO_FIELD_NUMBER -> handleNodeInfo(proto.nodeInfo) + + MeshProtos.FromRadio.RADIO_FIELD_NUMBER -> handleRadioConfig(proto.radio) + + else -> errormsg("Unexpected FromRadio variant") + } + } + + else -> errormsg("Unexpected radio interface broadcast") } } } @@ -1295,6 +1295,7 @@ class MeshService : Service(), Logging { configNonce += 1 newNodes.clear() newMyNodeInfo = null + debug("Starting config nonce=$configNonce") sendToRadio(ToRadio.newBuilder().apply { this.wantConfigId = configNonce diff --git a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt index 2ca2afabf..ebf978539 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -154,6 +154,8 @@ class RadioInterfaceService : Service(), Logging { receivedPacketsLog.flush() } + // ignoreException { debug("FromRadio: ${MeshProtos.FromRadio.parseFrom(p)}") } + broadcastReceivedFromRadio( this, p diff --git a/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt b/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt index 9237c8864..23c87e1aa 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SerialInterface.kt @@ -88,14 +88,15 @@ class SerialInterface(private val service: RadioInterfaceService, val address: S override fun handleSendToRadio(p: ByteArray) { // This method is called from a continuation and it might show up late, so check for uart being null - uart?.apply { - val header = ByteArray(4) - header[0] = START1 - header[1] = START2 - header[2] = (p.size shr 8).toByte() - header[3] = (p.size and 0xff).toByte() - write(header, 0) // FIXME - combine these into one write (for fewer USB transactions) - write(p, 0) + + val header = ByteArray(4) + header[0] = START1 + header[1] = START2 + header[2] = (p.size shr 8).toByte() + header[3] = (p.size and 0xff).toByte() + ioManager?.apply { + writeAsync(header) + writeAsync(p) } }