-
-
Notifications
You must be signed in to change notification settings - Fork 194
/
RadioInterfaceService.kt
289 lines (244 loc) · 9.95 KB
/
RadioInterfaceService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.geeksville.mesh.repository.radio
import android.app.Application
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import com.geeksville.mesh.CoroutineDispatchers
import com.geeksville.mesh.android.BinaryLogFile
import com.geeksville.mesh.android.GeeksvilleApplication
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.concurrent.handledLaunch
import com.geeksville.mesh.repository.bluetooth.BluetoothRepository
import com.geeksville.mesh.repository.network.NetworkRepository
import com.geeksville.mesh.util.anonymize
import com.geeksville.mesh.util.ignoreException
import com.geeksville.mesh.util.toRemoteExceptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Singleton
/**
* Handles the bluetooth link with a mesh radio device. Does not cache any device state,
* just does bluetooth comms etc...
*
* This service is not exposed outside of this process.
*
* Note - this class intentionally dumb. It doesn't understand protobuf framing etc...
* It is designed to be simple so it can be stubbed out with a simulated version as needed.
*/
@Singleton
class RadioInterfaceService @Inject constructor(
private val context: Application,
private val dispatchers: CoroutineDispatchers,
private val bluetoothRepository: BluetoothRepository,
private val networkRepository: NetworkRepository,
private val processLifecycle: Lifecycle,
@RadioRepositoryQualifier private val prefs: SharedPreferences,
private val interfaceFactory: InterfaceFactory,
) : Logging {
private val _connectionState = MutableStateFlow(RadioServiceConnectionState())
val connectionState = _connectionState.asStateFlow()
private val _receivedData = MutableSharedFlow<ByteArray>()
val receivedData: SharedFlow<ByteArray> = _receivedData
private val logSends = false
private val logReceives = false
private lateinit var sentPacketsLog: BinaryLogFile // inited in onCreate
private lateinit var receivedPacketsLog: BinaryLogFile
val mockInterfaceAddress: String by lazy {
toInterfaceAddress(InterfaceId.MOCK, "")
}
/**
* We recreate this scope each time we stop an interface
*/
var serviceScope = CoroutineScope(Dispatchers.IO + Job())
private var radioIf: IRadioInterface = NopInterface("")
/** true if we have started our interface
*
* Note: an interface may be started without necessarily yet having a connection
*/
private var isStarted = false
/// true if our interface is currently connected to a device
private var isConnected = false
private fun initStateListeners() {
bluetoothRepository.state.onEach { state ->
if (state.enabled) startInterface()
else if (radioIf is BluetoothInterface) stopInterface()
}.launchIn(processLifecycle.coroutineScope)
networkRepository.networkAvailable.onEach { state ->
if (state) startInterface()
else if (radioIf is TCPInterface) stopInterface()
}.launchIn(processLifecycle.coroutineScope)
}
companion object : Logging {
const val DEVADDR_KEY = "devAddr2" // the new name for devaddr
}
/**
* Constructs a full radio address for the specific interface type.
*/
fun toInterfaceAddress(interfaceId: InterfaceId, rest: String): String {
return interfaceFactory.toInterfaceAddress(interfaceId, rest)
}
fun isAddressValid(address: String?): Boolean {
return interfaceFactory.addressValid(address)
}
/** Return the device we are configured to use, or null for none
* device address strings are of the form:
*
* at
*
* where a is either x for bluetooth or s for serial
* and t is an interface specific address (macaddr or a device path)
*/
fun getDeviceAddress(): String? {
// If the user has unpaired our device, treat things as if we don't have one
var address = prefs.getString(DEVADDR_KEY, null)
// If we are running on the emulator we default to the mock interface, so we can have some data to show to the user
if (address == null && isAddressValid(mockInterfaceAddress)) {
address = mockInterfaceAddress
}
return address
}
/** Like getDeviceAddress, but filtered to return only devices we are currently bonded with
*
* at
*
* where a is either x for bluetooth or s for serial
* and t is an interface specific address (macaddr or a device path)
*/
fun getBondedDeviceAddress(): String? {
// If the user has unpaired our device, treat things as if we don't have one
val address = getDeviceAddress()
return if (interfaceFactory.addressValid(address)) {
address
} else {
null
}
}
private fun broadcastConnectionChanged(isConnected: Boolean, isPermanent: Boolean) {
debug("Broadcasting connection=$isConnected")
processLifecycle.coroutineScope.launch(dispatchers.default) {
_connectionState.emit(
RadioServiceConnectionState(isConnected, isPermanent)
)
}
}
/// Send a packet/command out the radio link, this routine can block if it needs to
private fun handleSendToRadio(p: ByteArray) {
radioIf.handleSendToRadio(p)
}
// Handle an incoming packet from the radio, broadcasts it as an android intent
fun handleFromRadio(p: ByteArray) {
if (logReceives) {
receivedPacketsLog.write(p)
receivedPacketsLog.flush()
}
// ignoreException { debug("FromRadio: ${MeshProtos.FromRadio.parseFrom(p)}") }
processLifecycle.coroutineScope.launch(dispatchers.io) {
_receivedData.emit(p)
}
}
fun onConnect() {
if (!isConnected) {
isConnected = true
broadcastConnectionChanged(isConnected = true, isPermanent = false)
}
}
fun onDisconnect(isPermanent: Boolean) {
if (isConnected) {
isConnected = false
broadcastConnectionChanged(isConnected = false, isPermanent = isPermanent)
}
}
/** Start our configured interface (if it isn't already running) */
private fun startInterface() {
if (radioIf !is NopInterface)
warn("Can't start interface - $radioIf is already running")
else {
val address = getBondedDeviceAddress()
if (address == null)
warn("No bonded mesh radio, can't start interface")
else {
info("Starting radio ${address.anonymize}")
isStarted = true
if (logSends)
sentPacketsLog = BinaryLogFile(context, "sent_log.pb")
if (logReceives)
receivedPacketsLog = BinaryLogFile(context, "receive_log.pb")
radioIf = interfaceFactory.createInterface(address)
}
}
}
private fun stopInterface() {
val r = radioIf
info("stopping interface $r")
isStarted = false
radioIf = interfaceFactory.nopInterface
r.close()
// cancel any old jobs and get ready for the new ones
serviceScope.cancel("stopping interface")
serviceScope = CoroutineScope(Dispatchers.IO + Job())
if (logSends)
sentPacketsLog.close()
if (logReceives)
receivedPacketsLog.close()
// Don't broadcast disconnects if we were just using the nop device
if (r !is NopInterface)
onDisconnect(isPermanent = true) // Tell any clients we are now offline
}
/**
* Change to a new device
*
* @return true if the device changed, false if no change
*/
private fun setBondedDeviceAddress(address: String?): Boolean {
return if (getBondedDeviceAddress() == address && isStarted) {
warn("Ignoring setBondedDevice ${address.anonymize}, because we are already using that device")
false
} else {
// Record that this use has configured a new radio
GeeksvilleApplication.analytics.track(
"mesh_bond"
)
// Ignore any errors that happen while closing old device
ignoreException {
stopInterface()
}
// The device address "n" can be used to mean none
debug("Setting bonded device to ${address.anonymize}")
prefs.edit {
if (address == null)
this.remove(DEVADDR_KEY)
else
putString(DEVADDR_KEY, address)
}
// Force the service to reconnect
startInterface()
true
}
}
fun setDeviceAddress(deviceAddr: String?): Boolean = toRemoteExceptions {
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. */
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()
initStateListeners()
}
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) }
}
}