@mentra/bluetooth-sdk 3.5.0-dev.1343 → 3.5.0-dev.1350

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.
@@ -0,0 +1,159 @@
1
+ import Foundation
2
+
3
+ /// NIMO's OTA channel (7033/2001/2002), distinct from the normal 0xBF command channel.
4
+ enum NimoOtaProtocol {
5
+ static let info: UInt8 = 0x02
6
+ static let reset: UInt8 = 0x03
7
+ static let fileOffsetCommand: UInt8 = 0xE1
8
+ static let canUpdate: UInt8 = 0xE2
9
+ static let enter: UInt8 = 0xE3
10
+ static let block: UInt8 = 0xE5
11
+ static let validate: UInt8 = 0xE6
12
+ static let sync: UInt8 = 0xE8
13
+ private static let header: [UInt8] = [0x70, 0x07, 0x6E]
14
+
15
+ struct ProtocolError: Error, LocalizedError {
16
+ let message: String
17
+ var errorDescription: String? {
18
+ message
19
+ }
20
+ }
21
+
22
+ struct Response {
23
+ let command: UInt8
24
+ let sequence: UInt8
25
+ let status: UInt8
26
+ let body: Data
27
+ }
28
+
29
+ struct Slice: Equatable {
30
+ let offset: UInt64
31
+ let length: Int
32
+ }
33
+
34
+ struct EnterResult {
35
+ let slice: Slice
36
+ let crc: Bool
37
+ }
38
+
39
+ struct BlockResult {
40
+ let slice: Slice
41
+ let delayMs: Int
42
+ }
43
+
44
+ static func request(_ command: UInt8, sequence: UInt8, params: Data = Data()) throws -> Data {
45
+ try require(params.count <= 65534, "Invalid OTA request")
46
+ let length = params.count + 1
47
+ return Data(header + [0xC0, command, UInt8(length >> 8), UInt8(truncatingIfNeeded: length), sequence]) + params + Data([0x33])
48
+ }
49
+
50
+ /// Bounded incremental decoder. Malformed input invalidates the exchange; it is never silently skipped.
51
+ final class Decoder {
52
+ private var buffer: [UInt8] = []
53
+ func reset() {
54
+ buffer.removeAll(keepingCapacity: true)
55
+ }
56
+
57
+ func feed(_ bytes: Data) throws -> [Response] {
58
+ var result: [Response] = []
59
+ do {
60
+ for byte in bytes {
61
+ buffer.append(byte)
62
+ if buffer.count <= 3 { try require(byte == header[buffer.count - 1], "Invalid OTA header") }
63
+ if buffer.count == 4 { try require(buffer[3] == 0, "Invalid OTA direction") }
64
+ if buffer.count >= 7 {
65
+ let length = u16(buffer, 5)
66
+ try require((2 ... 4096).contains(length), "Invalid OTA response length")
67
+ if buffer.count == length + 8 {
68
+ try require(byte == 0x33, "Invalid OTA footer")
69
+ result.append(Response(command: buffer[4], sequence: buffer[8], status: buffer[7], body: Data(buffer[9 ..< buffer.count - 1])))
70
+ reset()
71
+ }
72
+ }
73
+ }
74
+ } catch { reset(); throw error }
75
+ return result
76
+ }
77
+ }
78
+
79
+ static func deviceInfo(_ body: Data) throws -> [UInt8: Data] {
80
+ let bytes = [UInt8](body)
81
+ var fields: [UInt8: Data] = [:]
82
+ var index = 0
83
+ while index < bytes.count {
84
+ let length = Int(bytes[index])
85
+ try require(length >= 1 && length <= bytes.count - index - 1, "Truncated OTA device info")
86
+ let type = bytes[index + 1]
87
+ try require(fields[type] == nil, "Duplicate OTA device info")
88
+ fields[type] = Data(bytes[index + 2 ..< index + 1 + length])
89
+ index += 1 + length
90
+ }
91
+ return fields
92
+ }
93
+
94
+ static func fileOffset(_ body: Data) throws -> Slice {
95
+ let bytes = [UInt8](body)
96
+ try require(bytes.count == 6, "Invalid OTA file offset")
97
+ return Slice(offset: u32(bytes, 0), length: u16(bytes, 4))
98
+ }
99
+
100
+ static func enterResult(_ body: Data) throws -> EnterResult {
101
+ let bytes = [UInt8](body)
102
+ try require(bytes.count == 8 && bytes[0] == 0 && bytes[7] <= 1, "OTA upgrade entry refused")
103
+ return EnterResult(slice: Slice(offset: u32(bytes, 1), length: u16(bytes, 5)), crc: bytes[7] == 1)
104
+ }
105
+
106
+ static func blockResult(_ body: Data) throws -> BlockResult {
107
+ let bytes = [UInt8](body)
108
+ try require(bytes.count == 9 && bytes[0] == 0, "OTA block refused")
109
+ let delay = u16(bytes, 7)
110
+ try require(delay <= 30000, "Invalid OTA block delay")
111
+ return BlockResult(slice: Slice(offset: u32(bytes, 1), length: u16(bytes, 5)), delayMs: delay)
112
+ }
113
+
114
+ static func firmwareSlice(_ firmware: Data, _ slice: Slice) throws -> Data {
115
+ try require(slice.offset <= UInt64(firmware.count) && slice.length > 0 &&
116
+ UInt64(slice.length) <= UInt64(firmware.count) - slice.offset, "OTA slice exceeds firmware")
117
+ let start = firmware.startIndex + Int(slice.offset)
118
+ return firmware.subdata(in: start ..< start + slice.length)
119
+ }
120
+
121
+ /// Each chunk gets its own CRC and sequence; only the final chunk gets a block response.
122
+ static func blockParts(_ firmware: Data, slice: Slice, crc: Bool, writeCapacity: Int) throws -> [Data] {
123
+ let data = try firmwareSlice(firmware, slice)
124
+ try require((20 ... 512).contains(writeCapacity), "Unsupported OTA write capacity")
125
+ let chunkSize = min(496, writeCapacity - 16) - (crc ? 4 : 0)
126
+ try require(chunkSize > 0, "OTA write capacity too small for CRC")
127
+ return stride(from: 0, to: data.count, by: chunkSize).map { start in
128
+ let piece = data.subdata(in: start ..< min(start + chunkSize, data.count))
129
+ return u32Bytes(slice.offset + UInt64(start)) + piece + (crc ? u32Bytes(UInt64(crc32(piece))) : Data())
130
+ }
131
+ }
132
+
133
+ static func crc32(_ data: Data) -> UInt32 {
134
+ var value: UInt32 = 0xFFFF_FFFF
135
+ for byte in data {
136
+ value ^= UInt32(byte)
137
+ for _ in 0 ..< 8 {
138
+ value = (value >> 1) ^ (value & 1 == 1 ? 0xEDB8_8320 : 0)
139
+ }
140
+ }
141
+ return value ^ 0xFFFF_FFFF
142
+ }
143
+
144
+ private static func require(_ valid: Bool, _ message: String) throws {
145
+ if !valid { throw ProtocolError(message: message) }
146
+ }
147
+
148
+ private static func u16(_ bytes: [UInt8], _ offset: Int) -> Int {
149
+ Int(bytes[offset]) << 8 | Int(bytes[offset + 1])
150
+ }
151
+
152
+ private static func u32(_ bytes: [UInt8], _ offset: Int) -> UInt64 {
153
+ (0 ..< 4).reduce(UInt64(0)) { ($0 << 8) | UInt64(bytes[offset + $1]) }
154
+ }
155
+
156
+ private static func u32Bytes(_ value: UInt64) -> Data {
157
+ Data([24, 16, 8, 0].map { UInt8(truncatingIfNeeded: value >> $0) })
158
+ }
159
+ }
@@ -0,0 +1,78 @@
1
+ import Foundation
2
+ @testable import MentraBluetoothSDK
3
+ import XCTest
4
+
5
+ final class NimoOtaProtocolTests: XCTestCase {
6
+ private func bytes(_ hex: String) -> Data {
7
+ let chars = Array(hex)
8
+ return Data(stride(from: 0, to: chars.count, by: 2).map { UInt8(String(chars[$0 ... $0 + 1]), radix: 16)! })
9
+ }
10
+
11
+ private func fixtures() throws -> [String: Any] {
12
+ let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
13
+ return try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: root.appendingPathComponent("test-fixtures/nimo-ota.json"))) as? [String: Any])
14
+ }
15
+
16
+ func testCapturedRequestsAndResponsesAtEveryNotificationSplit() throws {
17
+ let fixture = try fixtures()
18
+ for item in try XCTUnwrap(fixture["requests"] as? [[String: Any]]) {
19
+ XCTAssertEqual(try NimoOtaProtocol.request(UInt8(XCTUnwrap(item["command"] as? Int)), sequence: UInt8(XCTUnwrap(item["sequence"] as? Int)), params: bytes(XCTUnwrap(item["params"] as? String))), try bytes(XCTUnwrap(item["frame"] as? String)))
20
+ }
21
+ let responses = try XCTUnwrap(fixture["responses"] as? [[String: Any]])
22
+ var combined = Data()
23
+ for item in responses {
24
+ let frame = try bytes(XCTUnwrap(item["frame"] as? String))
25
+ combined += frame
26
+ for split in 0 ... frame.count {
27
+ let decoder = NimoOtaProtocol.Decoder()
28
+ let actual = try decoder.feed(frame.prefix(split)) + decoder.feed(frame.dropFirst(split))
29
+ XCTAssertEqual(actual.count, 1)
30
+ XCTAssertEqual(Int(actual[0].command), item["command"] as? Int)
31
+ XCTAssertEqual(Int(actual[0].sequence), item["sequence"] as? Int)
32
+ XCTAssertEqual(Int(actual[0].status), item["status"] as? Int)
33
+ XCTAssertEqual(actual[0].body, try bytes(XCTUnwrap(item["body"] as? String)))
34
+ }
35
+ }
36
+ XCTAssertEqual(try NimoOtaProtocol.Decoder().feed(combined).count, responses.count)
37
+ }
38
+
39
+ func testSmallPhoneWritesUsePerChunkCrcAndRequestedOffset() throws {
40
+ let fixture = try fixtures()
41
+ let firmware = try bytes(XCTUnwrap(fixture["firmware"] as? String))
42
+ for item in try XCTUnwrap(fixture["blocks"] as? [[String: Any]]) {
43
+ let capacity = try XCTUnwrap(item["capacity"] as? Int)
44
+ let parts = try NimoOtaProtocol.blockParts(firmware, slice: .init(offset: UInt64(XCTUnwrap(item["offset"] as? Int)), length: XCTUnwrap(item["length"] as? Int)), crc: XCTUnwrap(item["crc"] as? Bool), writeCapacity: capacity)
45
+ XCTAssertEqual(parts, (item["parts"] as? [String])?.map(bytes))
46
+ for (index, part) in parts.enumerated() {
47
+ XCTAssertLessThanOrEqual(try NimoOtaProtocol.request(0xE5, sequence: UInt8(index), params: part).count, capacity)
48
+ }
49
+ }
50
+ XCTAssertEqual(NimoOtaProtocol.crc32(Data("123456789".utf8)), 0xCBF4_3926)
51
+ }
52
+
53
+ func testRejectsMalformedFramesAndUnboundedDeviceRequests() throws {
54
+ for hex in ["71", "70076ec0", "70076e00020001", "70076e00021001", "70076e0003000200fa00"] {
55
+ XCTAssertThrowsError(try NimoOtaProtocol.Decoder().feed(bytes(hex)))
56
+ }
57
+ for hex in ["00", "0201", "01000100"] {
58
+ XCTAssertThrowsError(try NimoOtaProtocol.deviceInfo(bytes(hex)))
59
+ }
60
+ for slice in [NimoOtaProtocol.Slice(offset: 100, length: 1), .init(offset: 0, length: 0), .init(offset: UInt64.max, length: Int.max)] {
61
+ XCTAssertThrowsError(try NimoOtaProtocol.firmwareSlice(Data(count: 100), slice))
62
+ }
63
+ XCTAssertThrowsError(try NimoOtaProtocol.blockParts(Data(count: 100), slice: .init(offset: 0, length: 100), crc: true, writeCapacity: 20))
64
+ XCTAssertThrowsError(try NimoOtaProtocol.enterResult(bytes("0100000012100001")))
65
+ XCTAssertThrowsError(try NimoOtaProtocol.blockResult(bytes("000000000000007531")))
66
+ }
67
+
68
+ func testCapturedPreflightAndAuthoritativeBlockRequests() throws {
69
+ let info = try NimoOtaProtocol.deviceInfo(bytes("0600000e000e0205010000020103026464020301020400020501"))
70
+ XCTAssertEqual(info[1], bytes("00000201"))
71
+ XCTAssertEqual(info[2], bytes("6464"))
72
+ let entered = try NimoOtaProtocol.enterResult(bytes("0000000012100001"))
73
+ XCTAssertEqual(entered.slice, .init(offset: 18, length: 4096))
74
+ XCTAssertTrue(entered.crc)
75
+ XCTAssertEqual(try NimoOtaProtocol.blockResult(bytes("000000101210000000")).slice, .init(offset: 4114, length: 4096))
76
+ XCTAssertEqual(try NimoOtaProtocol.blockResult(Data(count: 9)).slice, .init(offset: 0, length: 0))
77
+ }
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/bluetooth-sdk",
3
- "version": "3.5.0-dev.1343",
3
+ "version": "3.5.0-dev.1350",
4
4
  "description": "SDK for communicating with smart glasses",
5
5
  "main": "build/index.js",
6
6
  "react-native": "src/index.ts",
@@ -115,7 +115,7 @@
115
115
  "registry": "https://registry.npmjs.org/"
116
116
  },
117
117
  "dependencies": {
118
- "@mentra/cloud-protocol": "3.5.0-dev.1343"
118
+ "@mentra/cloud-protocol": "3.5.0-dev.1350"
119
119
  },
120
120
  "devDependencies": {
121
121
  "@types/node": "^25.9.3",
@@ -12,9 +12,9 @@ export interface BluetoothSdkReleaseMetadata {
12
12
  export const BLUETOOTH_SDK_RELEASE_METADATA: Readonly<BluetoothSdkReleaseMetadata> = Object.freeze({
13
13
  "schemaVersion": 1,
14
14
  "familyBaseVersion": "3.5.0",
15
- "releaseIdentity": "3.5.0-dev.1343",
16
- "releaseSetId": "mentra-3.5.0-dev.1343",
17
- "sourceCommit": "f44dd5ef27d36b951b032019cad29966f0079c24",
18
- "otaManifestUrl": "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1343.json",
19
- "otaManifestSha256": "412ab91a6bee5feb7591d72f05dd92d9196861e0be36a365f5d8189893548a89"
15
+ "releaseIdentity": "3.5.0-dev.1350",
16
+ "releaseSetId": "mentra-3.5.0-dev.1350",
17
+ "sourceCommit": "ad1dd67b452f999e348f5fa5f16346645d1d62c4",
18
+ "otaManifestUrl": "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1350.json",
19
+ "otaManifestSha256": "87dfda2a48da9adceec1436dd3084a2f3dedb413b39cd21618162e66b11602a9"
20
20
  })