Skip to content

Commit

Permalink
Flash firmware from HTTP URI
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Apr 2, 2024
1 parent e2b38a9 commit 27cb960
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import net.pipe01.pinepartner.scripting.PluginManager
import net.pipe01.pinepartner.scripting.ScriptDependencies
import net.pipe01.pinepartner.utils.runJobThrowing
import java.io.ByteArrayInputStream
import java.net.URL
import java.time.Duration
import java.time.LocalDateTime
import java.time.ZoneOffset
Expand Down Expand Up @@ -282,11 +283,14 @@ class BackgroundService : Service() {
Log.d(TAG, "Flashing watch $address with $uri")

val device = deviceManager.get(address) ?: throw ServiceException("Device not found")

runJobThrowing(CoroutineScope(Dispatchers.IO), onStart = {
transferJobs[jobId] = TransferJob(it, TransferProgress(0f, null, null, false))
}) {
contentResolver.openInputStream(uri)!!.use { stream ->
if (uri.scheme?.startsWith("http") == true) {
URL(uri.toString()).openStream()
} else {
contentResolver.openInputStream(uri)!!
}.use { stream ->
device.flashDFU(stream, this) {
transferJobs[jobId]?.progress = TransferProgress(
it.totalProgress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
package net.pipe01.pinepartner.utils

import android.net.Uri
import fuel.Fuel
import fuel.get
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

data class InfiniTimeRelease(
val version: String,
val name: String,
val resourcesUri: Uri,
val firmwareUri: Uri,
)

@Serializable
private data class GitHubRelease(
val tag_name: String,
val name: String,
val assets: List<GitHubAsset>,
)

@Serializable
private data class GitHubAsset(
val name: String,
val browser_download_url: String,
)

private val json = Json { ignoreUnknownKeys = true }

// This endpoint caches the response of https://api.github.com/repos/InfiniTimeOrg/InfiniTime/releases
private const val endpoint = "https://pipe01.net/pinepartner-proxy/releases"

suspend fun getInfiniTimeReleases(): List<InfiniTimeRelease> {
val resp = Fuel.get(endpoint).body

val ghReleases = json.decodeFromString<List<GitHubRelease>>(resp)

return ghReleases.mapNotNull {
InfiniTimeRelease(
version = it.tag_name,
name = it.name.trim(),
resourcesUri = Uri.parse(it.assets.firstOrNull { it.name.startsWith("infinitime-resources-") }?.browser_download_url ?: return@mapNotNull null),
firmwareUri = Uri.parse(it.assets.firstOrNull { it.name.startsWith("pinetime-mcuboot-app-dfu-") }?.browser_download_url ?: return@mapNotNull null),
)
}
}

0 comments on commit 27cb960

Please sign in to comment.