Skip to content

Commit 223f674

Browse files
committed
functional fw updates
1 parent 8cf9cf7 commit 223f674

File tree

8 files changed

+32
-22
lines changed

8 files changed

+32
-22
lines changed

gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
kotlin.code.style=official
22

33
group=io.rebble.libpebblecommon
4-
version=0.1.11
4+
version=0.1.12
55
org.gradle.jvmargs=-Xms128M -Xmx1G -XX:ReservedCodeCacheSize=200M
66
kotlin.native.binary.memoryModel=experimental
77
kotlin.mpp.androidSourceSetLayoutVersion=2
88
kotlinVersion=1.8.10
9-
agpVersion=7.4.0
9+
agpVersion=7.4.0
10+
android.disableAutomaticComponentCreation=true

src/commonMain/kotlin/io/rebble/libpebblecommon/ProtocolHandlerImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,4 @@ class ProtocolHandlerImpl() : ProtocolHandler {
158158
}
159159
return true
160160
}
161-
162161
}

src/commonMain/kotlin/io/rebble/libpebblecommon/metadata/pbz/manifest/PbzManifest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data class PbzManifest(
1111
val generatedBy: String,
1212
val debug: Debug,
1313
val firmware: PbzFirmware,
14-
val resources: SystemResources,
14+
val resources: SystemResources?,
1515
@SerialName("js_tooling")
1616
val jsTooling: JsTooling,
1717
val type: String

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/PutBytes.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package io.rebble.libpebblecommon.packets
33
import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry
44
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
55
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
6-
import io.rebble.libpebblecommon.structmapper.SBytes
7-
import io.rebble.libpebblecommon.structmapper.SNullTerminatedString
8-
import io.rebble.libpebblecommon.structmapper.SUByte
9-
import io.rebble.libpebblecommon.structmapper.SUInt
6+
import io.rebble.libpebblecommon.structmapper.*
107

118
sealed class PutBytesOutgoingPacket(command: PutBytesCommand) :
129
PebblePacket(ProtocolEndpoint.PUT_BYTES) {
@@ -42,7 +39,7 @@ class PutBytesInit(
4239
val objectSize = SUInt(m, objectSize)
4340
val objectType = SUByte(m, objectType.value)
4441
val bank = SUByte(m, bank)
45-
val filename = SNullTerminatedString(m, filename)
42+
val filename = SOptional(m, SNullTerminatedString(StructMapper(), filename), filename.isNotEmpty())
4643
}
4744

4845
/**

src/commonMain/kotlin/io/rebble/libpebblecommon/packets/System.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,18 +397,22 @@ open class SystemMessage(message: Message) : SystemPacket(endpoint) {
397397
FirmwareUpdateStartResponse(0x0au)
398398
}
399399

400-
val command = SUByte(m, message.value)
400+
val command = SUByte(m, 0x0u)
401+
val messageType = SUByte(m, message.value)
401402

402403
init {
403-
type = command.get()
404+
type = messageType.get()
404405
}
405406

406407
companion object {
407408
val endpoint = ProtocolEndpoint.SYSTEM_MESSAGE
408409
}
409410

410411
class NewFirmwareAvailable: SystemMessage(Message.NewFirmwareAvailable)
411-
class FirmwareUpdateStart: SystemMessage(Message.FirmwareUpdateStart)
412+
class FirmwareUpdateStart(bytesAlreadyTransferred: UInt, bytesToSend: UInt): SystemMessage(Message.FirmwareUpdateStart) {
413+
val bytesAlreadyTransferred = SUInt(m, bytesAlreadyTransferred, endianness = '<')
414+
val bytesToSend = SUInt(m, bytesToSend, endianness = '<')
415+
}
412416
class FirmwareUpdateComplete: SystemMessage(Message.FirmwareUpdateComplete)
413417
class FirmwareUpdateFailed: SystemMessage(Message.FirmwareUpdateFailed)
414418
class FirmwareUpToDate: SystemMessage(Message.FirmwareUpToDate)
@@ -504,6 +508,7 @@ fun systemPacketsRegister() {
504508
PacketRegistry.register(PingPong.endpoint, PingPong.Message.Ping.value) { PingPong.Ping() }
505509
PacketRegistry.register(PingPong.endpoint, PingPong.Message.Pong.value) { PingPong.Pong() }
506510

511+
PacketRegistry.registerCustomTypeOffset(SystemMessage.endpoint, 5) // command is always 0, type is next byte
507512
PacketRegistry.register(
508513
SystemMessage.endpoint,
509514
SystemMessage.Message.FirmwareUpdateStartResponse.value

src/commonMain/kotlin/io/rebble/libpebblecommon/protocolhelpers/ProtocolEndpoint.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.rebble.libpebblecommon.protocolhelpers
33
import co.touchlab.kermit.Logger
44

55
enum class ProtocolEndpoint(val value: UShort) {
6+
RECOVERY(0u),
67
TIME(11u),
78
WATCH_VERSION(16u),
89
PHONE_VERSION(17u),

src/commonMain/kotlin/io/rebble/libpebblecommon/services/PutBytesService.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.rebble.libpebblecommon.util.DataBuffer
1212
import io.rebble.libpebblecommon.util.getPutBytesMaximumDataSize
1313
import kotlinx.coroutines.channels.Channel
1414
import kotlinx.coroutines.withTimeout
15+
import kotlin.math.log
1516

1617
class PutBytesService(private val protocolHandler: ProtocolHandler) : ProtocolService {
1718
val receivedMessages = Channel<PutBytesResponse>(Channel.BUFFERED)
@@ -22,6 +23,7 @@ class PutBytesService(private val protocolHandler: ProtocolHandler) : ProtocolSe
2223
data class PutBytesProgress(
2324
val count: Int,
2425
val total: Int,
26+
val delta: Int,
2527
val cookie: UInt
2628
)
2729

@@ -87,14 +89,15 @@ class PutBytesService(private val protocolHandler: ProtocolHandler) : ProtocolSe
8789
watchVersion: WatchVersion.WatchVersionResponse,
8890
crc: Long,
8991
size: UInt,
90-
type: ObjectType,
91-
fileName: String
92+
bank: UByte,
93+
type: ObjectType
9294
) {
9395
logger.i { "Send FW part $type ${type.value}" }
9496
send(
95-
PutBytesInit(size, type, 0u, fileName)
97+
PutBytesInit(size, type, bank, "")
9698
)
9799

100+
logger.d { "Putting byte array" }
98101
val cookie = awaitCookieAndPutByteArray(
99102
blob,
100103
crc,
@@ -121,10 +124,10 @@ class PutBytesService(private val protocolHandler: ProtocolHandler) : ProtocolSe
121124
val cookie = awaitAck().cookie.get()
122125
lastCookie = cookie
123126
progressUpdates.trySend(
124-
PutBytesProgress(0, totalToPut, cookie)
127+
PutBytesProgress(0, totalToPut, 0, cookie)
125128
)
126129

127-
val maxDataSize = getPutBytesMaximumDataSize(watchVersion)
130+
val maxDataSize = if (watchVersion.running.isRecovery.get()) 2000 else getPutBytesMaximumDataSize(watchVersion)
128131
val buffer = DataBuffer(byteArray.asUByteArray())
129132
val crcCalculator = Crc32Calculator()
130133

@@ -142,7 +145,7 @@ class PutBytesService(private val protocolHandler: ProtocolHandler) : ProtocolSe
142145
awaitAck()
143146
totalBytes += dataToRead
144147
progressUpdates.trySend(
145-
PutBytesProgress(totalBytes, totalToPut, cookie)
148+
PutBytesProgress(totalBytes, totalToPut, dataToRead, cookie)
146149
)
147150
}
148151
val calculatedCrc = crcCalculator.finalize()

src/commonMain/kotlin/io/rebble/libpebblecommon/services/SystemService.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.rebble.libpebblecommon.services
22

3+
import co.touchlab.kermit.Logger
34
import io.rebble.libpebblecommon.PacketPriority
45
import io.rebble.libpebblecommon.ProtocolHandler
56
import io.rebble.libpebblecommon.packets.*
@@ -26,6 +27,7 @@ class SystemService(private val protocolHandler: ProtocolHandler) : ProtocolServ
2627
protocolHandler.registerReceiveCallback(ProtocolEndpoint.PHONE_VERSION, this::receive)
2728
protocolHandler.registerReceiveCallback(ProtocolEndpoint.WATCH_VERSION, this::receive)
2829
protocolHandler.registerReceiveCallback(ProtocolEndpoint.FCT_REG, this::receive)
30+
protocolHandler.registerReceiveCallback(ProtocolEndpoint.SYSTEM_MESSAGE, this::receive)
2931
}
3032

3133
/**
@@ -55,13 +57,11 @@ class SystemService(private val protocolHandler: ProtocolHandler) : ProtocolServ
5557
return SInt(StructMapper()).also { it.fromBytes(DataBuffer(modelBytes)) }.get()
5658
}
5759

58-
suspend fun firmwareUpdateStart(): UByte {
60+
suspend fun firmwareUpdateStart(bytesAlreadyTransferred: UInt, bytesToSend: UInt): UByte {
5961
val callback = CompletableDeferred<SystemMessage.FirmwareUpdateStartResponse>()
6062
firmwareUpdateStartResponseCallback = callback
61-
send(SystemMessage.FirmwareUpdateStart())
62-
63+
send(SystemMessage.FirmwareUpdateStart(bytesAlreadyTransferred, bytesToSend))
6364
val response = callback.await()
64-
6565
return response.response.get()
6666
}
6767

@@ -89,6 +89,10 @@ class SystemService(private val protocolHandler: ProtocolHandler) : ProtocolServ
8989
send(res) // Cannot be low priority
9090
}
9191
}
92+
is SystemMessage.FirmwareUpdateStartResponse -> {
93+
firmwareUpdateStartResponseCallback?.complete(packet)
94+
firmwareUpdateStartResponseCallback = null
95+
}
9296
else -> receivedMessages.trySend(packet)
9397
}
9498
}

0 commit comments

Comments
 (0)