Skip to content

Commit

Permalink
Lock file system funs
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Mar 25, 2024
1 parent 8643fef commit e7b9b03
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions app/src/main/java/net/pipe01/pinepartner/devices/blefs/FS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import net.pipe01.pinepartner.devices.Device
import net.pipe01.pinepartner.devices.InfiniTime
import no.nordicsemi.android.common.core.DataByteArray
Expand All @@ -17,6 +19,9 @@ import java.time.Instant

private const val TAG = "BLEFS"

//TODO: Make this per-device
private val mutex = Mutex()

fun joinPaths(path1: String, path2: String): String {
return cleanPath(if (path1 == "") {
path2
Expand Down Expand Up @@ -55,27 +60,30 @@ private suspend fun Device.doRequest(
val done = CompletableDeferred<Unit>()
val start = CompletableDeferred<Unit>()

val job = coroutineScope.launch {
fileService
.getNotifications()
.onStart { start.complete(Unit) }
.collect {
val buf = ByteBuffer.wrap(it.value).order(ByteOrder.LITTLE_ENDIAN)

if (onReceiveResponse(buf, fileService)) {
done.complete(Unit)
mutex.withLock {
val job = coroutineScope.launch {
fileService
.getNotifications()
.onStart { start.complete(Unit) }
.collect {
val buf = ByteBuffer.wrap(it.value).order(ByteOrder.LITTLE_ENDIAN)

if (onReceiveResponse(buf, fileService)) {
done.complete(Unit)
}
}
}
}
}

start.await()

start.await()
delay(200) // Make sure the notifications are coming through

delay(1000)
val request = onBuildRequest()
fileService.write(DataByteArray(request.array()))
val request = onBuildRequest()
fileService.write(DataByteArray(request.array()))

done.await()
job.cancel()
done.await()
job.cancel()
}
}

@SuppressLint("MissingPermission")
Expand Down

0 comments on commit e7b9b03

Please sign in to comment.