-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This required creation of new interfaces in order to break the static coupling. This also allowed for the removal of some plumbing of dependencies of these implementations since they are now directly injected.
- Loading branch information
Showing
25 changed files
with
370 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/geeksville/mesh/repository/radio/BluetoothInterfaceFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
import dagger.assisted.AssistedFactory | ||
|
||
/** | ||
* Factory for creating `BluetoothInterface` instances. | ||
*/ | ||
@AssistedFactory | ||
interface BluetoothInterfaceFactory : InterfaceFactorySpi<BluetoothInterface> |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/geeksville/mesh/repository/radio/BluetoothInterfaceSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
import android.annotation.SuppressLint | ||
import android.bluetooth.BluetoothAdapter | ||
import com.geeksville.mesh.android.Logging | ||
import com.geeksville.mesh.util.anonymize | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Bluetooth backend implementation. | ||
*/ | ||
class BluetoothInterfaceSpec @Inject constructor( | ||
private val factory: BluetoothInterfaceFactory, | ||
private val bluetoothAdapter: dagger.Lazy<BluetoothAdapter?> | ||
|
||
): InterfaceSpec<BluetoothInterface>, Logging { | ||
override fun createInterface(rest: String): BluetoothInterface { | ||
return factory.create(rest) | ||
} | ||
|
||
/** Return true if this address is still acceptable. For BLE that means, still bonded */ | ||
@SuppressLint("MissingPermission") | ||
override fun addressValid(rest: String): Boolean { | ||
val allPaired = bluetoothAdapter.get()?.bondedDevices.orEmpty() | ||
.map { it.address }.toSet() | ||
return if (!allPaired.contains(rest)) { | ||
warn("Ignoring stale bond to ${rest.anonymize}") | ||
false | ||
} else | ||
true | ||
} | ||
} |
41 changes: 29 additions & 12 deletions
41
app/src/main/java/com/geeksville/mesh/repository/radio/InterfaceFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
import android.content.Context | ||
import com.geeksville.mesh.repository.usb.UsbRepository | ||
import javax.inject.Inject | ||
import javax.inject.Provider | ||
|
||
/** | ||
* A base class for the singleton factories that make interfaces. One instance per interface type | ||
* Entry point for create radio backend instances given a specific address. | ||
* | ||
* This class is responsible for building and dissecting radio addresses based upon | ||
* their interface type and the "rest" of the address (which varies per implementation). | ||
*/ | ||
abstract class InterfaceFactory(val prefix: Char) { | ||
companion object { | ||
private val factories = mutableMapOf<Char, InterfaceFactory>() | ||
class InterfaceFactory @Inject constructor( | ||
private val nopInterfaceFactory: NopInterfaceFactory, | ||
private val specMap: Map<InterfaceId, @JvmSuppressWildcards Provider<InterfaceSpec<*>>> | ||
) { | ||
internal val nopInterface by lazy { | ||
nopInterfaceFactory.create("") | ||
} | ||
|
||
fun getFactory(l: Char) = factories.get(l) | ||
fun toInterfaceAddress(interfaceId: InterfaceId, rest: String): String { | ||
return "${interfaceId.id}$rest" | ||
} | ||
|
||
protected fun registerFactory() { | ||
factories[prefix] = this | ||
fun createInterface(address: String): IRadioInterface { | ||
val (spec, rest) = splitAddress(address) | ||
return spec?.createInterface(rest) ?: nopInterface | ||
} | ||
|
||
abstract fun createInterface(context: Context, service: RadioInterfaceService, usbRepository: UsbRepository, rest: String): IRadioInterface | ||
fun addressValid(address: String?): Boolean { | ||
return address?.let { | ||
val (spec, rest) = splitAddress(it) | ||
spec?.addressValid(rest) | ||
} ?: false | ||
} | ||
|
||
/** Return true if this address is still acceptable. For BLE that means, still bonded */ | ||
open fun addressValid(context: Context, usbRepository: UsbRepository, rest: String): Boolean = true | ||
private fun splitAddress(address: String): Pair<InterfaceSpec<*>?, String> { | ||
val c = address[0].let { InterfaceId.forIdChar(it) }?.let { specMap[it]?.get() } | ||
val rest = address.substring(1) | ||
return Pair(c, rest) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/geeksville/mesh/repository/radio/InterfaceFactorySpi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
/** | ||
* Radio interface factory service provider interface. Each radio backend implementation needs | ||
* to have a factory to create new instances. These instances are specific to a particular | ||
* address. This interface defines a common API across all radio interfaces for obtaining | ||
* implementation instances. | ||
* | ||
* This is primarily used in conjunction with Dagger assisted injection for each backend | ||
* interface type. | ||
*/ | ||
interface InterfaceFactorySpi<T: IRadioInterface> { | ||
fun create(rest: String): T | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/geeksville/mesh/repository/radio/InterfaceId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
/** | ||
* Address identifiers for all supported radio backend implementations. | ||
*/ | ||
enum class InterfaceId(val id: Char) { | ||
BLUETOOTH('x'), | ||
MOCK('m'), | ||
NOP('n'), | ||
SERIAL('s'), | ||
TCP('t'); | ||
|
||
companion object { | ||
fun forIdChar(id: Char): InterfaceId? { | ||
return values().firstOrNull { it.id == id } | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/geeksville/mesh/repository/radio/InterfaceMapKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
import dagger.MapKey | ||
|
||
/** | ||
* Dagger `MapKey` implementation allowing for `InterfaceId` to be used as a map key. | ||
*/ | ||
@MapKey | ||
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.PROPERTY_GETTER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class InterfaceMapKey(val value: InterfaceId) |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/geeksville/mesh/repository/radio/InterfaceSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
/** | ||
* This interface defines the contract that all radio backend implementations must adhere to. | ||
*/ | ||
interface InterfaceSpec<T : IRadioInterface> { | ||
fun createInterface(rest: String): T | ||
|
||
/** Return true if this address is still acceptable. For BLE that means, still bonded */ | ||
fun addressValid(rest: String): Boolean = true | ||
} |
33 changes: 7 additions & 26 deletions
33
app/src/main/java/com/geeksville/mesh/repository/radio/MockInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/geeksville/mesh/repository/radio/MockInterfaceFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.geeksville.mesh.repository.radio | ||
|
||
import dagger.assisted.AssistedFactory | ||
|
||
/** | ||
* Factory for creating `MockInterface` instances. | ||
*/ | ||
@AssistedFactory | ||
interface MockInterfaceFactory : InterfaceFactorySpi<MockInterface> |
Oops, something went wrong.