From e4223d647645efd6b17eae9f81893dc140ea4b89 Mon Sep 17 00:00:00 2001 From: geeksville Date: Thu, 11 Jun 2020 11:20:51 -0700 Subject: [PATCH] prevent race condition if meshservice starts running before radiointerface --- .../com/geeksville/mesh/IRadioInterfaceService.aidl | 4 ++++ .../java/com/geeksville/mesh/service/MeshService.kt | 7 ++++++- .../geeksville/mesh/service/RadioInterfaceService.kt | 11 +++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl b/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl index a10975157..8485dfbcc 100644 --- a/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl +++ b/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl @@ -5,6 +5,10 @@ package com.geeksville.mesh; interface IRadioInterfaceService { + /** If the service is not currently connected to the radio, try to connect now. At boot the radio interface service will + * not connect to a radio until this call is received. */ + void connect(); + void sendToRadio(in byte [] a); /// If a macaddress we will try to talk to our device, if null we will be idle. 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 097695c22..5a08bfd12 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -148,7 +148,12 @@ class MeshService : Service(), Logging { private val clientPackages = mutableMapOf() val radio = ServiceClient { - IRadioInterfaceService.Stub.asInterface(it) + val stub = IRadioInterfaceService.Stub.asInterface(it) + + // Now that we are connected to the radio service, tell it to connect to the radio + stub.connect() + + stub } private val serviceJob = Job() 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 ebf978539..9322a4716 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -171,7 +171,6 @@ class RadioInterfaceService : Service(), Logging { runningService = this super.onCreate() registerReceiver(bluetoothStateReceiver, bluetoothStateReceiver.intent) - startInterface() } override fun onDestroy() { @@ -237,7 +236,7 @@ class RadioInterfaceService : Service(), Logging { @SuppressLint("NewApi") - fun setBondedDeviceAddress(addressIn: String?) { + private fun setBondedDeviceAddress(addressIn: String?) { // Record that this use has configured a radio GeeksvilleApplication.analytics.track( "mesh_bond" @@ -272,6 +271,14 @@ class RadioInterfaceService : Service(), Logging { setBondedDeviceAddress(deviceAddr) } + /** If the service is not currently connected to the radio, try to connect now. At boot the radio interface service will + * not connect to a radio until this call is received. */ + override fun connect() = toRemoteExceptions { + // We don't start actually talking to our device until MeshService binds to us - this prevents + // broadcasting connection events before MeshService is ready to receive them + startInterface() + } + override fun sendToRadio(a: ByteArray) { // Do this in the IO thread because it might take a while (and we don't care about the result code) serviceScope.handledLaunch { handleSendToRadio(a) }