prevent race condition if meshservice starts running before radiointerface

This commit is contained in:
geeksville
2020-06-11 11:20:51 -07:00
parent b4687b0a2f
commit e4223d6476
3 changed files with 19 additions and 3 deletions

View File

@@ -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.

View File

@@ -148,7 +148,12 @@ class MeshService : Service(), Logging {
private val clientPackages = mutableMapOf<String, String>()
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()

View File

@@ -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) }