@mentra/bluetooth-sdk 3.4.0-beta.1345 → 3.4.0-beta.1347
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/assets/nimo/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +198 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdater.kt +213 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocol.kt +112 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareTargetTest.kt +15 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdaterTest.kt +76 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocolTest.kt +77 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoReadinessDriverTest.kt +31 -1
- package/build/generated/releaseMetadata.js +5 -5
- package/build/generated/releaseMetadata.js.map +1 -1
- package/ios/MentraBluetoothSDK.podspec +3 -1
- package/ios/Resources/NimoFirmware/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
- package/ios/Source/sgcs/Nimo.swift +211 -5
- package/ios/Source/sgcs/NimoFirmwareUpdater.swift +260 -0
- package/ios/Source/sgcs/NimoOtaProtocol.swift +159 -0
- package/ios/Tests/NimoOtaProtocolTests.swift +78 -0
- package/package.json +2 -2
- package/src/generated/releaseMetadata.ts +5 -5
|
Binary file
|
|
@@ -3,9 +3,9 @@ package com.mentra.bluetoothsdk
|
|
|
3
3
|
/** Generated by release CI. Do not edit in a release checkout. */
|
|
4
4
|
internal object GeneratedReleaseMetadata {
|
|
5
5
|
const val FAMILY_BASE_VERSION: String = "3.4.0"
|
|
6
|
-
const val RELEASE_IDENTITY: String = "3.4.0-beta.
|
|
7
|
-
const val RELEASE_SET_ID: String = "mentra-3.4.0-beta.
|
|
8
|
-
const val SOURCE_COMMIT: String = "
|
|
9
|
-
const val OTA_MANIFEST_URL: String = "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.4.0/mentra-live-ota-3.4.0-beta.
|
|
10
|
-
const val OTA_MANIFEST_SHA256: String = "
|
|
6
|
+
const val RELEASE_IDENTITY: String = "3.4.0-beta.1347"
|
|
7
|
+
const val RELEASE_SET_ID: String = "mentra-3.4.0-beta.1347"
|
|
8
|
+
const val SOURCE_COMMIT: String = "9086db8ca1ff7a4b340f0b6cd54a82e3cafea65e"
|
|
9
|
+
const val OTA_MANIFEST_URL: String = "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.4.0/mentra-live-ota-3.4.0-beta.1347.json"
|
|
10
|
+
const val OTA_MANIFEST_SHA256: String = "4404546c1a7bd968296afc23cbf95e4832e2753a37735d3f23792168cc45bd19"
|
|
11
11
|
}
|
|
@@ -44,6 +44,9 @@ internal object NimoBLE {
|
|
|
44
44
|
val CHAR_TX: UUID = UUID.fromString("00002021-0000-1000-8000-00805F9B34FB")
|
|
45
45
|
val CHAR_RX: UUID = UUID.fromString("00002022-0000-1000-8000-00805F9B34FB")
|
|
46
46
|
val CHAR_MIC: UUID = UUID.fromString("00002025-0000-1000-8000-00805F9B34FB")
|
|
47
|
+
// Firmware update channel on the same service (see NimoOtaProtocol).
|
|
48
|
+
val CHAR_OTA_TX: UUID = UUID.fromString("00002001-0000-1000-8000-00805F9B34FB")
|
|
49
|
+
val CHAR_OTA_RX: UUID = UUID.fromString("00002002-0000-1000-8000-00805F9B34FB")
|
|
47
50
|
val CLIENT_CHARACTERISTIC_CONFIG: UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
|
|
48
51
|
|
|
49
52
|
const val NAME_PREFIX = "nimo"
|
|
@@ -927,6 +930,16 @@ class Nimo : SGCManager() {
|
|
|
927
930
|
private const val BATTERY_POLL_MS = 30_000L
|
|
928
931
|
private const val SET_TIME_MAX_ATTEMPTS = 3
|
|
929
932
|
private const val SET_TIME_RETRY_DELAY_MS = 500L
|
|
933
|
+
// Version replies normally arrive within a few hundred ms of the handshake.
|
|
934
|
+
private const val VERSION_WAIT_MS = 3_000L
|
|
935
|
+
// Let the wearer read the update notice before the display goes away.
|
|
936
|
+
private const val UPDATE_NOTICE_MS = 2_000L
|
|
937
|
+
private const val OTA_PREPARE_TIMEOUT_MS = 15_000L
|
|
938
|
+
// The glasses reboot after the update; force a reconnect if they never drop the link.
|
|
939
|
+
private const val POST_UPDATE_RECONNECT_MS = 120_000L
|
|
940
|
+
// Per app process, so a failing update cannot loop forever.
|
|
941
|
+
private const val MAX_UPDATE_ATTEMPTS = 3
|
|
942
|
+
private var updateAttempts = 0
|
|
930
943
|
}
|
|
931
944
|
|
|
932
945
|
init {
|
|
@@ -965,6 +978,8 @@ class Nimo : SGCManager() {
|
|
|
965
978
|
private var txChar: BluetoothGattCharacteristic? = null
|
|
966
979
|
private var rxChar: BluetoothGattCharacteristic? = null
|
|
967
980
|
private var micChar: BluetoothGattCharacteristic? = null
|
|
981
|
+
private var otaTxChar: BluetoothGattCharacteristic? = null
|
|
982
|
+
private var otaRxChar: BluetoothGattCharacteristic? = null
|
|
968
983
|
private var isDisconnecting = false
|
|
969
984
|
private var negotiatedMtu = 23
|
|
970
985
|
// Device search
|
|
@@ -1015,7 +1030,11 @@ class Nimo : SGCManager() {
|
|
|
1015
1030
|
// One queue owns characteristic writes and CCCD callbacks, including non-canvas commands.
|
|
1016
1031
|
private val writes = NimoGattQueue<BluetoothGattCharacteristic>(mainScheduler,
|
|
1017
1032
|
{ characteristic, bytes ->
|
|
1018
|
-
characteristic.writeType =
|
|
1033
|
+
characteristic.writeType =
|
|
1034
|
+
if (characteristic === otaTxChar &&
|
|
1035
|
+
characteristic.properties and BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE != 0)
|
|
1036
|
+
BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
|
|
1037
|
+
else BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
|
|
1019
1038
|
characteristic.value = bytes
|
|
1020
1039
|
gatt?.writeCharacteristic(characteristic) ?: false
|
|
1021
1040
|
}, ::abortTransport, NimoBLE.INTER_FRAME_DELAY_MS)
|
|
@@ -1030,6 +1049,16 @@ class Nimo : SGCManager() {
|
|
|
1030
1049
|
// Version info
|
|
1031
1050
|
private var firmwareVersionPacked: String = ""
|
|
1032
1051
|
private var firmwareVersionDetail: String = ""
|
|
1052
|
+
private var versionPackedReceived = false
|
|
1053
|
+
private var versionDetailReceived = false
|
|
1054
|
+
|
|
1055
|
+
// Firmware gate: the canvas only runs on the supported firmware; anything else is updated first.
|
|
1056
|
+
private enum class FirmwareState { UNKNOWN, CURRENT, UPDATING }
|
|
1057
|
+
private var firmwareState = FirmwareState.UNKNOWN
|
|
1058
|
+
private var firmwareUpdater: NimoFirmwareUpdater? = null
|
|
1059
|
+
private var cancelFirmwareTimer: (() -> Unit)? = null
|
|
1060
|
+
private var otaMtuPending: (() -> Unit)? = null
|
|
1061
|
+
private val updating get() = firmwareState == FirmwareState.UPDATING
|
|
1033
1062
|
|
|
1034
1063
|
// Mic audio (vendor GlassesAudioClient structure)
|
|
1035
1064
|
private val audioClient =
|
|
@@ -1113,6 +1142,8 @@ class Nimo : SGCManager() {
|
|
|
1113
1142
|
txChar = null
|
|
1114
1143
|
rxChar = null
|
|
1115
1144
|
micChar = null
|
|
1145
|
+
otaTxChar = null
|
|
1146
|
+
otaRxChar = null
|
|
1116
1147
|
resetSessionState()
|
|
1117
1148
|
|
|
1118
1149
|
DeviceStore.apply("glasses", "connected", false)
|
|
@@ -1186,6 +1217,10 @@ class Nimo : SGCManager() {
|
|
|
1186
1217
|
Bridge.log("NIMO: mic characteristic not available")
|
|
1187
1218
|
return
|
|
1188
1219
|
}
|
|
1220
|
+
if (enabled && updating) {
|
|
1221
|
+
Bridge.log("NIMO: mic stays off during the firmware update")
|
|
1222
|
+
return
|
|
1223
|
+
}
|
|
1189
1224
|
DeviceStore.apply("glasses", "micEnabled", enabled)
|
|
1190
1225
|
if (enabled) {
|
|
1191
1226
|
audioClient.start()
|
|
@@ -1846,6 +1881,8 @@ class Nimo : SGCManager() {
|
|
|
1846
1881
|
txChar = null
|
|
1847
1882
|
rxChar = null
|
|
1848
1883
|
micChar = null
|
|
1884
|
+
otaTxChar = null
|
|
1885
|
+
otaRxChar = null
|
|
1849
1886
|
resetSessionState()
|
|
1850
1887
|
|
|
1851
1888
|
DeviceStore.apply("glasses", "connected", false)
|
|
@@ -1861,8 +1898,9 @@ class Nimo : SGCManager() {
|
|
|
1861
1898
|
override fun onMtuChanged(g: BluetoothGatt, mtu: Int, status: Int) {
|
|
1862
1899
|
Bridge.log("NIMO: onMtuChanged mtu=$mtu status=$status")
|
|
1863
1900
|
mainHandler.post {
|
|
1864
|
-
if (g !== gatt
|
|
1865
|
-
negotiatedMtu = mtu.coerceIn(23, 517)
|
|
1901
|
+
if (g !== gatt) return@post
|
|
1902
|
+
if (status == BluetoothGatt.GATT_SUCCESS) negotiatedMtu = mtu.coerceIn(23, 517)
|
|
1903
|
+
otaMtuPending?.let { otaMtuPending = null; it() }
|
|
1866
1904
|
}
|
|
1867
1905
|
}
|
|
1868
1906
|
|
|
@@ -1892,8 +1930,11 @@ class Nimo : SGCManager() {
|
|
|
1892
1930
|
txChar = service.getCharacteristic(NimoBLE.CHAR_TX)
|
|
1893
1931
|
rxChar = service.getCharacteristic(NimoBLE.CHAR_RX)
|
|
1894
1932
|
micChar = service.getCharacteristic(NimoBLE.CHAR_MIC)
|
|
1933
|
+
otaTxChar = service.getCharacteristic(NimoBLE.CHAR_OTA_TX)
|
|
1934
|
+
otaRxChar = service.getCharacteristic(NimoBLE.CHAR_OTA_RX)
|
|
1895
1935
|
Bridge.log(
|
|
1896
|
-
"NIMO: chars tx=${txChar != null} rx=${rxChar != null} mic=${micChar != null}"
|
|
1936
|
+
"NIMO: chars tx=${txChar != null} rx=${rxChar != null} mic=${micChar != null} " +
|
|
1937
|
+
"ota=${otaTxChar != null && otaRxChar != null}"
|
|
1897
1938
|
)
|
|
1898
1939
|
if (txChar == null || rxChar == null) {
|
|
1899
1940
|
Bridge.log("NIMO: required characteristics missing")
|
|
@@ -1921,8 +1962,9 @@ class Nimo : SGCManager() {
|
|
|
1921
1962
|
handleRxPacket(data)
|
|
1922
1963
|
}
|
|
1923
1964
|
NimoBLE.CHAR_MIC -> {
|
|
1924
|
-
audioClient.enqueue(data)
|
|
1965
|
+
if (!updating) audioClient.enqueue(data)
|
|
1925
1966
|
}
|
|
1967
|
+
NimoBLE.CHAR_OTA_RX -> firmwareUpdater?.receive(data)
|
|
1926
1968
|
}
|
|
1927
1969
|
}
|
|
1928
1970
|
}
|
|
@@ -1970,6 +2012,8 @@ class Nimo : SGCManager() {
|
|
|
1970
2012
|
// ---------- Write Queue ----------
|
|
1971
2013
|
|
|
1972
2014
|
private fun enqueueWrite(char: BluetoothGattCharacteristic, bytes: ByteArray) {
|
|
2015
|
+
// The firmware update owns the link; ordinary commands wait for the next connection.
|
|
2016
|
+
if (updating) return
|
|
1973
2017
|
val expectedGatt = gatt ?: return
|
|
1974
2018
|
mainHandler.post {
|
|
1975
2019
|
if (gatt !== expectedGatt) return@post
|
|
@@ -1984,6 +2028,7 @@ class Nimo : SGCManager() {
|
|
|
1984
2028
|
|
|
1985
2029
|
private fun enqueueFrames(frames: List<ByteArray>, finalStarted: () -> Unit,
|
|
1986
2030
|
completed: () -> Unit): Boolean {
|
|
2031
|
+
if (updating) return false
|
|
1987
2032
|
val tx = txChar ?: return false
|
|
1988
2033
|
val connection = gatt ?: return false
|
|
1989
2034
|
// Coordinator and GATT callbacks are Main-confined: enqueue the complete chain atomically.
|
|
@@ -1994,7 +2039,7 @@ class Nimo : SGCManager() {
|
|
|
1994
2039
|
Bridge.log("NIMO: resetting transport: $reason")
|
|
1995
2040
|
closeGatt()
|
|
1996
2041
|
gatt = null
|
|
1997
|
-
txChar = null; rxChar = null; micChar = null
|
|
2042
|
+
txChar = null; rxChar = null; micChar = null; otaTxChar = null; otaRxChar = null
|
|
1998
2043
|
resetSessionState()
|
|
1999
2044
|
DeviceStore.apply("glasses", "connected", false)
|
|
2000
2045
|
DeviceStore.apply("glasses", "fullyBooted", false)
|
|
@@ -2145,12 +2190,147 @@ class Nimo : SGCManager() {
|
|
|
2145
2190
|
DeviceStore.apply("glasses", "fullyBooted", true)
|
|
2146
2191
|
DeviceStore.apply("glasses", "connectionState", ConnTypes.CONNECTED)
|
|
2147
2192
|
startTimers()
|
|
2193
|
+
// The canvas starts once the reported firmware is the supported one.
|
|
2194
|
+
cancelFirmwareTimer = mainScheduler.post(VERSION_WAIT_MS) {
|
|
2195
|
+
cancelFirmwareTimer = null
|
|
2196
|
+
checkFirmware(waited = true)
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2200
|
+
// ---------- Firmware check and update ----------
|
|
2201
|
+
|
|
2202
|
+
private fun canvasReadiness(ready: Boolean) {
|
|
2203
|
+
if (firmwareState == FirmwareState.CURRENT) canvas.readiness(ready)
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
/**
|
|
2207
|
+
* Runs once per connection after the version replies (or [VERSION_WAIT_MS]). The supported
|
|
2208
|
+
* firmware starts the canvas; anything else gets the bundled image, after a plain-text notice
|
|
2209
|
+
* that the stock firmware can show.
|
|
2210
|
+
*/
|
|
2211
|
+
private fun checkFirmware(waited: Boolean = false) {
|
|
2212
|
+
if (handshakeState != HandshakeState.READY || firmwareState != FirmwareState.UNKNOWN) return
|
|
2213
|
+
if (!waited && !(versionPackedReceived && versionDetailReceived)) return
|
|
2214
|
+
cancelFirmwareTimer?.invoke(); cancelFirmwareTimer = null
|
|
2215
|
+
if (!versionPackedReceived ||
|
|
2216
|
+
NimoFirmwareTarget.isCurrent(firmwareVersionPacked, firmwareVersionDetail)) {
|
|
2217
|
+
if (!versionPackedReceived) Bridge.log("NIMO: no firmware version reply; starting normally")
|
|
2218
|
+
startCanvas()
|
|
2219
|
+
return
|
|
2220
|
+
}
|
|
2221
|
+
Bridge.log("NIMO: firmware $firmwareVersionPacked '$firmwareVersionDetail' is not " +
|
|
2222
|
+
"${NimoFirmwareTarget.PACKED_VERSION}-${NimoFirmwareTarget.BUILD_TAG}")
|
|
2223
|
+
val firmware = if (updateAttempts < MAX_UPDATE_ATTEMPTS) loadFirmware() else null
|
|
2224
|
+
val connection = gatt
|
|
2225
|
+
if (firmware == null || connection == null || otaTxChar == null || otaRxChar == null) {
|
|
2226
|
+
Bridge.log("NIMO: firmware update unavailable (attempts=$updateAttempts); starting normally")
|
|
2227
|
+
startCanvas()
|
|
2228
|
+
return
|
|
2229
|
+
}
|
|
2230
|
+
updateAttempts++
|
|
2231
|
+
Bridge.log("NIMO: starting firmware update attempt $updateAttempts")
|
|
2232
|
+
showUpdateNotice(connection)
|
|
2233
|
+
audioClient.stop() // Its stop command must be queued before ordinary writes close.
|
|
2234
|
+
firmwareState = FirmwareState.UPDATING
|
|
2235
|
+
stopTimers()
|
|
2236
|
+
DeviceStore.apply("glasses", "micEnabled", false)
|
|
2237
|
+
// Prepare the channel after the notice frames drain and the wearer has had time to read it.
|
|
2238
|
+
cancelFirmwareTimer = mainScheduler.post(UPDATE_NOTICE_MS) {
|
|
2239
|
+
cancelFirmwareTimer = mainScheduler.post(OTA_PREPARE_TIMEOUT_MS) {
|
|
2240
|
+
cancelFirmwareTimer = null
|
|
2241
|
+
abortTransport("NIMO firmware update channel did not become ready")
|
|
2242
|
+
}
|
|
2243
|
+
prepareUpdateChannel(connection, firmware)
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
private fun startCanvas() {
|
|
2248
|
+
firmwareState = FirmwareState.CURRENT
|
|
2148
2249
|
// Launch checks peer readiness in firmware; do not wait up to a minute for
|
|
2149
2250
|
// the first unsolicited heartbeat just because peer status is still unknown.
|
|
2150
2251
|
canvas.activate()
|
|
2151
2252
|
canvas.readiness(twsConnected && peerCompanionReady != false)
|
|
2152
2253
|
}
|
|
2153
2254
|
|
|
2255
|
+
/** The pre-canvas text page (ASR note app); it renders on the stock firmware too. */
|
|
2256
|
+
private fun showUpdateNotice(connection: BluetoothGatt) {
|
|
2257
|
+
val tx = txChar ?: return
|
|
2258
|
+
val enter = NimoFrameCodec.encodeFrame(
|
|
2259
|
+
NimoProtocol.CMD_CONTROL_INSTRUCTION,
|
|
2260
|
+
NimoProtocol.CTRL_ENTER_APP,
|
|
2261
|
+
byteArrayOf(NimoProtocol.APP_ID_ASR_NOTE.toByte(), NimoProtocol.APP_MODE_STANDALONE.toByte()))
|
|
2262
|
+
val text = NimoFrameCodec.updateContentFrames(
|
|
2263
|
+
appId = NimoProtocol.APP_ID_ASR_NOTE,
|
|
2264
|
+
layoutId = 0,
|
|
2265
|
+
resId = 0,
|
|
2266
|
+
resType = NimoProtocol.WIDGET_TEXT_NEW,
|
|
2267
|
+
content = NimoFirmwareTarget.UPDATING_TEXT.toByteArray(Charsets.UTF_8))
|
|
2268
|
+
writes.enqueue(connection, tx, listOf(enter) + text)
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
private fun loadFirmware(): ByteArray? = try {
|
|
2272
|
+
val bytes = context.assets.open(NimoFirmwareTarget.ASSET_PATH).use { it.readBytes() }
|
|
2273
|
+
if (NimoFirmwareTarget.sha256(bytes) == NimoFirmwareTarget.SHA256) bytes
|
|
2274
|
+
else { Bridge.log("NIMO: bundled firmware hash mismatch"); null }
|
|
2275
|
+
} catch (error: Exception) {
|
|
2276
|
+
Bridge.log("NIMO: bundled firmware unavailable: ${error.message}")
|
|
2277
|
+
null
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
/** Waits for queued writes to finish, raises the MTU and subscribes to the OTA replies. */
|
|
2281
|
+
private fun prepareUpdateChannel(connection: BluetoothGatt, firmware: ByteArray) {
|
|
2282
|
+
if (gatt !== connection || !updating) return
|
|
2283
|
+
if (writes.pendingCount() > 0) {
|
|
2284
|
+
mainScheduler.post(20) { prepareUpdateChannel(connection, firmware) }
|
|
2285
|
+
return
|
|
2286
|
+
}
|
|
2287
|
+
val subscribe = {
|
|
2288
|
+
val rx = otaRxChar
|
|
2289
|
+
if (gatt === connection && updating && rx != null) {
|
|
2290
|
+
enableNotifications(connection, rx) { startUpdater(connection, firmware) }
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
if (negotiatedMtu - 3 > 20) { subscribe(); return }
|
|
2294
|
+
// The vendor warns against MTU requests during connect; now normal traffic is stopped.
|
|
2295
|
+
otaMtuPending = subscribe
|
|
2296
|
+
val started = try { connection.requestMtu(517) } catch (_: SecurityException) { false }
|
|
2297
|
+
if (!started) {
|
|
2298
|
+
otaMtuPending = null
|
|
2299
|
+
abortTransport("NIMO firmware update MTU request did not start")
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
private fun startUpdater(connection: BluetoothGatt, firmware: ByteArray) {
|
|
2304
|
+
if (gatt !== connection || !updating) return
|
|
2305
|
+
cancelFirmwareTimer?.invoke(); cancelFirmwareTimer = null
|
|
2306
|
+
val capacity = (negotiatedMtu - 3).coerceIn(20, 512)
|
|
2307
|
+
if (capacity <= 20) { abortTransport("NIMO firmware update needs a larger MTU"); return }
|
|
2308
|
+
Bridge.log("NIMO: firmware transfer starting (${firmware.size} bytes, write capacity $capacity)")
|
|
2309
|
+
val updater = NimoFirmwareUpdater(
|
|
2310
|
+
firmware, capacity, mainScheduler,
|
|
2311
|
+
write = { bytes, done ->
|
|
2312
|
+
val tx = otaTxChar
|
|
2313
|
+
if (gatt !== connection || tx == null ||
|
|
2314
|
+
!writes.enqueue(connection, tx, listOf(bytes), completed = { done(true) })) done(false)
|
|
2315
|
+
},
|
|
2316
|
+
progress = { Bridge.log("NIMO: firmware update $it%") },
|
|
2317
|
+
finished = finished@{ error ->
|
|
2318
|
+
if (gatt !== connection) return@finished
|
|
2319
|
+
firmwareUpdater = null
|
|
2320
|
+
if (error != null) {
|
|
2321
|
+
abortTransport("NIMO firmware update failed: $error")
|
|
2322
|
+
} else {
|
|
2323
|
+
Bridge.log("NIMO: firmware update sent; glasses are rebooting")
|
|
2324
|
+
cancelFirmwareTimer = mainScheduler.post(POST_UPDATE_RECONNECT_MS) {
|
|
2325
|
+
cancelFirmwareTimer = null
|
|
2326
|
+
if (gatt === connection) abortTransport("NIMO did not reboot after the firmware update")
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
})
|
|
2330
|
+
firmwareUpdater = updater
|
|
2331
|
+
updater.start()
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2154
2334
|
private fun handshakeFailed() {
|
|
2155
2335
|
handshakeState = HandshakeState.IDLE
|
|
2156
2336
|
gatt?.disconnect()
|
|
@@ -2165,6 +2345,12 @@ class Nimo : SGCManager() {
|
|
|
2165
2345
|
peerCompanionReady = null
|
|
2166
2346
|
canvasEncoder.invalidate()
|
|
2167
2347
|
canvas.disconnected()
|
|
2348
|
+
cancelFirmwareTimer?.invoke(); cancelFirmwareTimer = null
|
|
2349
|
+
firmwareUpdater?.cancel(); firmwareUpdater = null
|
|
2350
|
+
otaMtuPending = null
|
|
2351
|
+
firmwareState = FirmwareState.UNKNOWN
|
|
2352
|
+
versionPackedReceived = false
|
|
2353
|
+
versionDetailReceived = false
|
|
2168
2354
|
audioClient.stop()
|
|
2169
2355
|
// Nimo's decoder/stream belongs to this GATT session. Consumer demand
|
|
2170
2356
|
// remains in bluetooth.should_send_*; readiness must start a fresh stream
|
|
@@ -2286,7 +2472,7 @@ class Nimo : SGCManager() {
|
|
|
2286
2472
|
onTwsState((v[8].toInt() and 0xFF) >= 1)
|
|
2287
2473
|
// Both predicates were sampled by this device report. TWS-only
|
|
2288
2474
|
// reports and cached state cannot release a status-7 wait.
|
|
2289
|
-
if (handshakeState == HandshakeState.READY) {
|
|
2475
|
+
if (handshakeState == HandshakeState.READY && firmwareState == FirmwareState.CURRENT) {
|
|
2290
2476
|
canvas.confirmedReadiness(twsConnected && peerCompanionReady != false)
|
|
2291
2477
|
}
|
|
2292
2478
|
}
|
|
@@ -2313,7 +2499,7 @@ class Nimo : SGCManager() {
|
|
|
2313
2499
|
if (!connected && handshakeState == HandshakeState.READY) {
|
|
2314
2500
|
Bridge.log("NIMO: TWS service dropped mid-session (arm removed/off?)")
|
|
2315
2501
|
}
|
|
2316
|
-
if (handshakeState == HandshakeState.READY)
|
|
2502
|
+
if (handshakeState == HandshakeState.READY) canvasReadiness(connected && peerCompanionReady != false)
|
|
2317
2503
|
}
|
|
2318
2504
|
|
|
2319
2505
|
private fun handleInputEvent(code: Int) {
|
|
@@ -2378,16 +2564,18 @@ class Nimo : SGCManager() {
|
|
|
2378
2564
|
val micro = (v ushr 12) and 0x1FF
|
|
2379
2565
|
val build = v and 0xFFF
|
|
2380
2566
|
firmwareVersionPacked = "$major.$minor.$micro.$build"
|
|
2567
|
+
versionPackedReceived = true
|
|
2381
2568
|
emitVersionInfo()
|
|
2569
|
+
checkFirmware()
|
|
2382
2570
|
}
|
|
2383
2571
|
}
|
|
2384
2572
|
NimoProtocol.GET_VERSION_DETAIL -> {
|
|
2385
2573
|
var end = data.size
|
|
2386
2574
|
while (end > 0 && data[end - 1].toInt() == 0) end--
|
|
2387
2575
|
firmwareVersionDetail = String(data, 0, end, Charsets.UTF_8).trim()
|
|
2388
|
-
|
|
2389
|
-
}
|
|
2576
|
+
versionDetailReceived = true
|
|
2390
2577
|
emitVersionInfo()
|
|
2578
|
+
checkFirmware()
|
|
2391
2579
|
}
|
|
2392
2580
|
NimoProtocol.GET_TWS_STATUS -> {
|
|
2393
2581
|
if (data.isNotEmpty()) onTwsState((data[0].toInt() and 0xFF) >= 1)
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
package com.mentra.bluetoothsdk.sgcs
|
|
2
|
+
|
|
3
|
+
import java.security.MessageDigest
|
|
4
|
+
|
|
5
|
+
/** The one NIMO firmware the app supports: the vendor's 537cf1 dynamic-v1 build. */
|
|
6
|
+
internal object NimoFirmwareTarget {
|
|
7
|
+
const val PACKED_VERSION = "0.1.1.1"
|
|
8
|
+
const val BUILD_TAG = "537cf1"
|
|
9
|
+
const val SHA256 = "5e23c574e3dfd8c27172f4f9a33475e6efd0d33e7230bfcce8f4fe8ca59fe433"
|
|
10
|
+
const val ASSET_PATH = "nimo/nimo-firmware-0.1.1.1-537cf1.bin"
|
|
11
|
+
const val UPDATING_TEXT = "Updating your NIMO glasses.\n\nKeep the Mentra app open."
|
|
12
|
+
|
|
13
|
+
/** A missing detail string (older firmware) is judged on the packed version alone. */
|
|
14
|
+
fun isCurrent(packedVersion: String, detail: String): Boolean =
|
|
15
|
+
packedVersion == PACKED_VERSION && (detail.isEmpty() || detail.contains("-$BUILD_TAG-"))
|
|
16
|
+
|
|
17
|
+
fun sha256(bytes: ByteArray): String =
|
|
18
|
+
MessageDigest.getInstance("SHA-256").digest(bytes).joinToString("") { "%02x".format(it.toInt() and 255) }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Single-attempt NIMO OTA over the 2001/2002 channel. Main-thread confined; [scheduler] posts there.
|
|
23
|
+
* [finished] gets null once the reboot command was sent, otherwise the failure reason.
|
|
24
|
+
* No failure path sends reset; the next connection decides again from the reported version.
|
|
25
|
+
*/
|
|
26
|
+
internal class NimoFirmwareUpdater(
|
|
27
|
+
firmware: ByteArray,
|
|
28
|
+
writeCapacity: Int,
|
|
29
|
+
private val scheduler: NimoScheduler,
|
|
30
|
+
private val write: (ByteArray, (Boolean) -> Unit) -> Unit,
|
|
31
|
+
private val progress: (Int) -> Unit,
|
|
32
|
+
private val finished: (String?) -> Unit,
|
|
33
|
+
) {
|
|
34
|
+
private class Pending(val command: Int, val sequence: Int, val callback: (Result<ByteArray>) -> Unit)
|
|
35
|
+
|
|
36
|
+
private val firmware = firmware.copyOf()
|
|
37
|
+
private val capacity = writeCapacity.coerceAtMost(512)
|
|
38
|
+
private val decoder = NimoOtaProtocol.Decoder()
|
|
39
|
+
private var sequence = 0
|
|
40
|
+
private var pending: Pending? = null
|
|
41
|
+
private var cancelTimer: (() -> Unit)? = null
|
|
42
|
+
private var cancelDelay: (() -> Unit)? = null
|
|
43
|
+
private var done = false
|
|
44
|
+
private var crc = false
|
|
45
|
+
private val covered = BooleanArray(firmware.size)
|
|
46
|
+
private var coveredCount = 0
|
|
47
|
+
private var lastPercent = -1
|
|
48
|
+
private var stalls = 0
|
|
49
|
+
private var transferStarted = 0L
|
|
50
|
+
private var syncPending = 0
|
|
51
|
+
private var syncFailures = 0
|
|
52
|
+
|
|
53
|
+
fun start() {
|
|
54
|
+
exchange(NimoOtaProtocol.INFO, ByteArray(4) { 255.toByte() }) { info ->
|
|
55
|
+
checkDeviceInfo(info)
|
|
56
|
+
exchange(NimoOtaProtocol.FILE_OFFSET) { body ->
|
|
57
|
+
val header = NimoOtaProtocol.firmwareSlice(firmware, NimoOtaProtocol.fileOffset(body))
|
|
58
|
+
exchange(NimoOtaProtocol.CAN_UPDATE, header) { result ->
|
|
59
|
+
// 0 = can update, 3 = same image already staged; anything else is a refusal.
|
|
60
|
+
require(result.contentEquals(byteArrayOf(0)) || result.contentEquals(byteArrayOf(3))) {
|
|
61
|
+
"Glasses refused the firmware (battery or prerequisites)"
|
|
62
|
+
}
|
|
63
|
+
exchange(NimoOtaProtocol.ENTER) { entryBody ->
|
|
64
|
+
val entry = NimoOtaProtocol.enterResult(entryBody)
|
|
65
|
+
crc = entry.crc
|
|
66
|
+
transferStarted = System.currentTimeMillis()
|
|
67
|
+
sendBlock(entry.slice)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fun receive(data: ByteArray) {
|
|
75
|
+
if (done) return
|
|
76
|
+
try {
|
|
77
|
+
for (reply in decoder.feed(data)) {
|
|
78
|
+
val expected = pending ?: continue
|
|
79
|
+
if (expected.command != reply.command || expected.sequence != reply.sequence) continue
|
|
80
|
+
pending = null
|
|
81
|
+
cancelTimer?.invoke(); cancelTimer = null
|
|
82
|
+
if (reply.status == 0) expected.callback(Result.success(reply.body))
|
|
83
|
+
else expected.callback(Result.failure(IllegalStateException("OTA command ${reply.command} returned status ${reply.status}")))
|
|
84
|
+
}
|
|
85
|
+
} catch (error: Exception) { fail(error) }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The connection went away; stop without reporting. */
|
|
89
|
+
fun cancel() {
|
|
90
|
+
done = true
|
|
91
|
+
clearTimers()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private fun checkDeviceInfo(body: ByteArray) {
|
|
95
|
+
val fields = NimoOtaProtocol.deviceInfo(body)
|
|
96
|
+
val batteries = fields[2]
|
|
97
|
+
require(fields[3]?.contentEquals(byteArrayOf(1)) == true && fields[5]?.contentEquals(byteArrayOf(1)) == true &&
|
|
98
|
+
batteries?.size == 2) { "Both arms must be connected and ready to update" }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private fun sendBlock(slice: NimoOtaProtocol.Slice) {
|
|
102
|
+
check(System.currentTimeMillis() - transferStarted <= 20 * 60_000L) { "Transfer exceeded 20 minutes" }
|
|
103
|
+
val parts = NimoOtaProtocol.blockParts(firmware, slice, crc, capacity)
|
|
104
|
+
sendPart(parts, 0) { body ->
|
|
105
|
+
val next = NimoOtaProtocol.blockResult(body)
|
|
106
|
+
var added = 0
|
|
107
|
+
for (index in slice.offset.toInt() until slice.offset.toInt() + slice.length) {
|
|
108
|
+
if (!covered[index]) { covered[index] = true; added++ }
|
|
109
|
+
}
|
|
110
|
+
coveredCount += added
|
|
111
|
+
stalls = if (added == 0) stalls + 1 else 0
|
|
112
|
+
check(stalls < 5) { "Five repeated blocks without progress" }
|
|
113
|
+
val percent = coveredCount * 100 / firmware.size
|
|
114
|
+
if (percent != lastPercent) { lastPercent = percent; progress(percent) }
|
|
115
|
+
if (next.slice.offset == 0L && next.slice.length == 0) {
|
|
116
|
+
exchange(NimoOtaProtocol.VALIDATE, timeoutMs = 30_000) { validation ->
|
|
117
|
+
require(validation.contentEquals(byteArrayOf(0))) { "Glasses rejected the transferred image" }
|
|
118
|
+
pollSync()
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
NimoOtaProtocol.firmwareSlice(firmware, next.slice)
|
|
122
|
+
delay(next.delayMs.toLong()) { sendBlock(next.slice) }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private fun sendPart(parts: List<ByteArray>, index: Int, completion: (ByteArray) -> Unit) {
|
|
128
|
+
if (index == parts.lastIndex) { exchange(NimoOtaProtocol.BLOCK, parts[index], 30_000, completion); return }
|
|
129
|
+
val packet = nextPacket(NimoOtaProtocol.BLOCK, parts[index])
|
|
130
|
+
write(packet) { ok ->
|
|
131
|
+
if (done) return@write
|
|
132
|
+
if (!ok) fail(IllegalStateException("OTA write failed"))
|
|
133
|
+
else delay(5) { sendPart(parts, index + 1, completion) }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Wait for the right arm to copy the image to the left arm, then reboot both. */
|
|
138
|
+
private fun pollSync() {
|
|
139
|
+
exchangeResult(NimoOtaProtocol.SYNC, timeoutMs = 8_000) { result ->
|
|
140
|
+
val body = result.getOrNull()
|
|
141
|
+
if (body?.contentEquals(byteArrayOf(0)) == true) {
|
|
142
|
+
val packet = nextPacket(NimoOtaProtocol.RESET, byteArrayOf(0))
|
|
143
|
+
write(packet) { }
|
|
144
|
+
done = true
|
|
145
|
+
clearTimers()
|
|
146
|
+
finished(null)
|
|
147
|
+
return@exchangeResult
|
|
148
|
+
}
|
|
149
|
+
if (body?.contentEquals(byteArrayOf(1)) == true) syncPending++ else syncFailures++
|
|
150
|
+
if (syncPending >= 180 || syncFailures >= 8) fail(IllegalStateException("Arms did not finish synchronizing; no reboot sent"))
|
|
151
|
+
else delay(1_000) { pollSync() }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private fun exchange(command: Int, params: ByteArray = byteArrayOf(), timeoutMs: Long = 15_000,
|
|
156
|
+
completion: (ByteArray) -> Unit) {
|
|
157
|
+
exchangeResult(command, params, timeoutMs) { result ->
|
|
158
|
+
try { completion(result.getOrThrow()) } catch (error: Exception) { fail(error) }
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private fun exchangeResult(command: Int, params: ByteArray = byteArrayOf(), timeoutMs: Long,
|
|
163
|
+
completion: (Result<ByteArray>) -> Unit) {
|
|
164
|
+
if (done) return
|
|
165
|
+
try {
|
|
166
|
+
val packet = nextPacket(command, params)
|
|
167
|
+
val entry = Pending(command, sequence, completion)
|
|
168
|
+
pending = entry
|
|
169
|
+
cancelTimer = scheduler.post(timeoutMs) {
|
|
170
|
+
if (pending === entry && !done) {
|
|
171
|
+
pending = null; cancelTimer = null
|
|
172
|
+
completion(Result.failure(IllegalStateException("OTA command $command timed out")))
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
write(packet) { ok ->
|
|
176
|
+
if (!ok && pending === entry && !done) {
|
|
177
|
+
pending = null
|
|
178
|
+
cancelTimer?.invoke(); cancelTimer = null
|
|
179
|
+
completion(Result.failure(IllegalStateException("OTA write failed")))
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
} catch (error: Exception) { completion(Result.failure(error)) }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private fun nextPacket(command: Int, params: ByteArray = byteArrayOf()): ByteArray {
|
|
186
|
+
sequence = (sequence + 1) and 255
|
|
187
|
+
val packet = NimoOtaProtocol.request(command, sequence, params)
|
|
188
|
+
require(packet.size <= capacity) { "OTA request exceeds the negotiated write capacity" }
|
|
189
|
+
return packet
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private fun delay(delayMs: Long, callback: () -> Unit) {
|
|
193
|
+
cancelDelay?.invoke()
|
|
194
|
+
cancelDelay = scheduler.post(delayMs) {
|
|
195
|
+
cancelDelay = null
|
|
196
|
+
if (!done) try { callback() } catch (error: Exception) { fail(error) }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun clearTimers() {
|
|
201
|
+
cancelTimer?.invoke(); cancelTimer = null
|
|
202
|
+
cancelDelay?.invoke(); cancelDelay = null
|
|
203
|
+
pending = null
|
|
204
|
+
decoder.reset()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private fun fail(error: Throwable) {
|
|
208
|
+
if (done) return
|
|
209
|
+
done = true
|
|
210
|
+
clearTimers()
|
|
211
|
+
finished(error.message ?: "NIMO firmware update failed")
|
|
212
|
+
}
|
|
213
|
+
}
|