Skip to content

Commit aa91b1b

Browse files
authored
More logging added to Channel Sounding (#199)
1 parent fb0db81 commit aa91b1b

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

profile_manager/src/main/java/no/nordicsemi/android/toolbox/profile/manager/ChannelSoundingManager.kt

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class ChannelSoundingManager(
7575
}
7676

7777
private var activeSession: RangingSession? = null
78-
private var capabilitiesCallback: RangingManager.RangingCapabilitiesCallback? = null
7978
private val previousRangingData = mutableListOf<Float>()
8079

8180
/** Rate the currently active (or last started) session was configured with. */
@@ -127,13 +126,15 @@ class ChannelSoundingManager(
127126
fun changeUpdateRate(rate: UpdateRate) {
128127
repository.updateRate(rate)
129128
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.BAKLAVA || rate == currentRate) return
129+
Timber.tag(tag).log(Log.Level.APPLICATION, "Update rate changed to: $rate")
130130
closeSession { startRangingMeasurement(rate) }
131131
}
132132

133133
/** Closes and restarts the ranging session using the currently selected update rate. */
134134
fun restartRangingSession() {
135135
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.BAKLAVA) return
136136
val rate = repository.data.value.updateRate
137+
Timber.tag(tag).log(Log.Level.APPLICATION, "Session restarted")
137138
closeSession { startRangingMeasurement(rate) }
138139
}
139140

@@ -158,9 +159,15 @@ class ChannelSoundingManager(
158159
repository.updateSessionAction(RangingSessionAction.OnRestarting)
159160
}
160161
try {
161-
if (isSessionOpen) session.stop() else session.close()
162+
if (isSessionOpen) {
163+
Timber.tag(tag).v("Stopping session...")
164+
session.stop()
165+
} else {
166+
Timber.tag(tag).v("Closing session...")
167+
session.close()
168+
}
162169
} catch (e: Exception) {
163-
Timber.tag(tag).e(e, "Error closing ranging session")
170+
Timber.tag(tag).e("Operation failed: ${e.message}")
164171
closing = false
165172
pendingRestart = null
166173
repository.updateSessionAction(RangingSessionAction.OnError(SessionClosedReason.UNKNOWN))
@@ -223,27 +230,33 @@ class ChannelSoundingManager(
223230
.setSessionConfig(sessionConfig)
224231
.build()
225232

226-
val newCapabilitiesCallback = RangingManager.RangingCapabilitiesCallback { capabilities ->
233+
var callback: RangingManager.RangingCapabilitiesCallback? = null
234+
callback = RangingManager.RangingCapabilitiesCallback { capabilities ->
235+
callback?.let { rangingManager.unregisterCapabilitiesCallback(it) }
227236
if (activeSession != null) return@RangingCapabilitiesCallback
237+
Timber.tag(tag).log(Log.Level.APPLICATION, "Ranging capabilities: $capabilities")
228238
val csCapabilities = capabilities.csCapabilities
229239
when {
230240
csCapabilities == null -> {
231241
repository.updateSessionAction(RangingSessionAction.OnError(SessionClosedReason.NOT_SUPPORTED))
232242
}
233243

234-
!csCapabilities.supportedSecurityLevels.contains(BleCsRangingCapabilities.CS_SECURITY_LEVEL_ONE) -> {
244+
BleCsRangingCapabilities.CS_SECURITY_LEVEL_ONE !in csCapabilities.supportedSecurityLevels -> {
245+
Timber.tag(tag).w("Security level 1 not supported")
235246
repository.updateSessionAction(RangingSessionAction.OnError(SessionClosedReason.CS_SECURITY_NOT_AVAILABLE))
236247
}
237248

238249
!hasRangingPermission() -> {
250+
Timber.tag(tag).w("Missing RANGING permission")
239251
repository.updateSessionAction(RangingSessionAction.OnError(SessionClosedReason.MISSING_PERMISSION))
240252
}
241253

242254
else -> openRangingSession(rangingManager, rangingPreference)
243255
}
256+
}.also { callback ->
257+
Timber.tag(tag).v("Requesting host capabilities...")
258+
rangingManager.registerCapabilitiesCallback(context.mainExecutor, callback)
244259
}
245-
capabilitiesCallback = newCapabilitiesCallback
246-
rangingManager.registerCapabilitiesCallback(context.mainExecutor, newCapabilitiesCallback)
247260
}
248261

249262
/** Creates and starts the [RangingSession]. Permission was just verified by the caller. */
@@ -253,29 +266,34 @@ class ChannelSoundingManager(
253266
rangingManager: RangingManager,
254267
rangingPreference: RangingPreference,
255268
) {
269+
Timber.tag(tag).v("Creating ranging session...")
256270
val session = rangingManager.createRangingSession(context.mainExecutor, createRangingSessionCallback())
257271
if (session == null) {
272+
Timber.tag(tag).w("Creating ranging session failed")
258273
repository.updateSessionAction(RangingSessionAction.OnError(SessionClosedReason.UNKNOWN))
259274
return
260275
}
261276
activeSession = session
277+
Timber.tag(tag).log(Log.Level.APPLICATION, "Starting session with preference: $rangingPreference")
262278
session.start(rangingPreference)
263279
}
264280

265281
@RequiresApi(Build.VERSION_CODES.BAKLAVA)
266282
private fun createRangingSessionCallback() = object : RangingSession.Callback {
267283
override fun onOpened() {
268284
isSessionOpen = true
285+
Timber.tag(tag).log(Log.Level.APPLICATION, "Ranging session opened")
269286
repository.updateSessionAction(RangingSessionAction.OnStart)
270287
}
271288

272289
override fun onOpenFailed(reason: Int) {
273290
activeSession = null
274-
unregisterCapabilitiesCallback()
291+
Timber.tag(tag).e("Opening ranging session failed: $reason")
275292
repository.updateSessionAction(RangingSessionAction.OnError(RangingSessionFailedReason.getReason(reason)))
276293
}
277294

278295
override fun onStarted(peer: RangingDevice, technology: Int) {
296+
Timber.tag(tag).log(Log.Level.APPLICATION, "Ranging session started with peer: $peer, technology: $technology")
279297
previousRangingData.clear()
280298
repository.updateSessionAction(RangingSessionAction.OnStart)
281299
}
@@ -293,6 +311,7 @@ class ChannelSoundingManager(
293311
@SuppressLint("MissingPermission") // Permission was already verified when the session was opened.
294312
override fun onStopped(peer: RangingDevice, technology: Int) {
295313
isSessionOpen = false
314+
Timber.tag(tag).log(Log.Level.APPLICATION, "Ranging session stopped")
296315
previousRangingData.clear()
297316
if (closing) {
298317
// We requested this stop as part of closeSession() - finish the teardown.
@@ -308,13 +327,21 @@ class ChannelSoundingManager(
308327
activeSession = null
309328
closing = false
310329
isSessionOpen = false
311-
unregisterCapabilitiesCallback()
330+
if (reason == RangingSessionFailedReason.LOCAL_REQUEST.reason) {
331+
Timber.tag(tag).log(Log.Level.APPLICATION, "Ranging session closed (local request)")
332+
} else {
333+
Timber.tag(tag).e("Ranging session closed with reason: $reason")
334+
}
312335
previousRangingData.clear()
313336

314337
val restart = pendingRestart
315338
pendingRestart = null
316339
when {
317-
restart != null -> restart()
340+
restart != null -> {
341+
Timber.tag(tag).v("Restarting session...")
342+
restart()
343+
}
344+
318345
reason == RangingSessionFailedReason.LOCAL_REQUEST.reason ->
319346
repository.updateSessionAction(RangingSessionAction.OnClosed)
320347

@@ -324,12 +351,6 @@ class ChannelSoundingManager(
324351
}
325352
}
326353

327-
@RequiresApi(Build.VERSION_CODES.BAKLAVA)
328-
private fun unregisterCapabilitiesCallback() {
329-
capabilitiesCallback?.let { rangingManager?.unregisterCapabilitiesCallback(it) }
330-
capabilitiesCallback = null
331-
}
332-
333354
@RequiresApi(Build.VERSION_CODES.BAKLAVA)
334355
private fun hasRangingPermission(): Boolean =
335356
context.checkSelfPermission(Manifest.permission.RANGING) == PackageManager.PERMISSION_GRANTED

0 commit comments

Comments
 (0)