forked from Blendman974/faceclaw
G1 support: Flashing within the Android app
This commit is contained in:
1 parent
345ba36c33
commit
38c52ebc31
37 files changed
+2819
-32
No files matched your search
@@ -0,0 +1,37 @@
|
||||
/** Keeping what's saved about a G1 current as an install changes its arms' addresses. */
|
||||
import {
|
||||
loadDeviceAddresses,
|
||||
loadPairedGlassesIdentity,
|
||||
saveDeviceAddresses,
|
||||
savePairedGlassesIdentity,
|
||||
} from "../g2/device-addresses";
|
||||
import type { EvenAdvertisement } from "../g2/even-advertisement";
|
||||
import type { GlassesPairCandidate } from "../g2/pairing-candidates";
|
||||
import type { G1Side } from "./flash-plan";
|
||||
|
||||
export function saveG1Pair(pair: GlassesPairCandidate): void {
|
||||
const stored = loadDeviceAddresses();
|
||||
saveDeviceAddresses({ left: pair.left!.address, right: pair.right!.address, ring: stored.ring });
|
||||
const identity = loadPairedGlassesIdentity();
|
||||
savePairedGlassesIdentity({
|
||||
serial: pair.serial ?? identity?.serial ?? "",
|
||||
leftName: pair.left!.name,
|
||||
rightName: pair.right!.name,
|
||||
leftAddress: pair.left!.address,
|
||||
rightAddress: pair.right!.address,
|
||||
ringName: identity?.ringName ?? "",
|
||||
ringAddress: stored.ring,
|
||||
pairedAtMs: identity?.pairedAtMs ?? Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/** Saved as soon as it's known: if anything stops the install after this, the next try and the session still find this arm. */
|
||||
export function saveG1Arm(side: G1Side, arm: EvenAdvertisement): void {
|
||||
saveDeviceAddresses({ ...loadDeviceAddresses(), [side]: arm.address });
|
||||
const identity = loadPairedGlassesIdentity();
|
||||
if (identity) {
|
||||
savePairedGlassesIdentity(side === "left"
|
||||
? { ...identity, leftName: arm.name, leftAddress: arm.address }
|
||||
: { ...identity, rightName: arm.name, rightAddress: arm.address });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Finding a G1's arms by listening for them. That's how an install knows what each arm runs before connecting to it,
|
||||
* and how it finds an arm again afterwards: an arm coming off Even's firmware has a new address.
|
||||
*/
|
||||
import { classifyAdvertisement, type EvenAdvertisement } from "../g2/even-advertisement";
|
||||
import { DiscoveryAggregator, type GlassesPairCandidate } from "../g2/pairing-candidates";
|
||||
import { DeviceDiscoveryBridge } from "../native/device-discovery";
|
||||
import { pickG1Pair, sameG1Serial, type G1Side, type G1Wanted } from "./flash-plan";
|
||||
|
||||
/** A serial should tell which arm is ours, but an arm that is the only one of its side around is good enough after this. */
|
||||
const ACCEPT_ANY_SERIAL_AFTER_MS = 45_000;
|
||||
|
||||
export class G1ArmScanner {
|
||||
private discovery: DeviceDiscoveryBridge | null = null;
|
||||
private finish: (() => void) | null = null;
|
||||
|
||||
/** Resolves with the wanted pair once both arms have been heard, or null after [timeoutMs]. */
|
||||
waitForPair(wanted: G1Wanted, timeoutMs: number): Promise<GlassesPairCandidate | null> {
|
||||
const aggregator = new DiscoveryAggregator();
|
||||
return this.listen<GlassesPairCandidate>(timeoutMs, (raw, done) => {
|
||||
if (aggregator.ingest(raw)) {
|
||||
const pair = pickG1Pair(aggregator.pairs(), wanted);
|
||||
if (pair) done(pair);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Resolves when an arm of this side on g1v0 is heard, or null after [timeoutMs]. */
|
||||
waitForG1v0Arm(side: G1Side, serial: string | null | undefined, timeoutMs: number): Promise<EvenAdvertisement | null> {
|
||||
const startedAt = Date.now();
|
||||
const others = new Map<string, EvenAdvertisement>();
|
||||
return this.listen<EvenAdvertisement>(timeoutMs, (raw, done) => {
|
||||
const arm = classifyAdvertisement(raw);
|
||||
if (!arm || arm.role !== side || !arm.g1Firmware || arm.g1Firmware === "stock") return;
|
||||
if (!serial || sameG1Serial(arm.serial, serial)) {
|
||||
done(arm);
|
||||
return;
|
||||
}
|
||||
others.set(arm.address, arm);
|
||||
if (others.size === 1 && Date.now() - startedAt >= ACCEPT_ANY_SERIAL_AFTER_MS) done(arm);
|
||||
});
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.finish?.();
|
||||
}
|
||||
|
||||
private listen<T>(timeoutMs: number, onRaw: (raw: Parameters<DiscoveryAggregator["ingest"]>[0], done: (value: T) => void) => void): Promise<T | null> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.discovery ??= new DeviceDiscoveryBridge();
|
||||
const discovery = this.discovery;
|
||||
if (!discovery.bluetoothEnabled) {
|
||||
reject(new Error("Bluetooth is off. Turn it on, then try again."));
|
||||
return;
|
||||
}
|
||||
let settled = false;
|
||||
const settle = (value: T | null, error?: Error) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
this.finish = null;
|
||||
discovery.stopScan();
|
||||
if (error) reject(error);
|
||||
else resolve(value);
|
||||
};
|
||||
const timer = setTimeout(() => settle(null), timeoutMs);
|
||||
this.finish = () => settle(null);
|
||||
const started = discovery.startScan({
|
||||
onAdvertisement: (raw) => onRaw(raw, (value) => settle(value)),
|
||||
onScanFailed: (_code, message) => settle(null, new Error(`The Bluetooth scan failed: ${message}`)),
|
||||
});
|
||||
if (!started) settle(null, new Error("Could not start the Bluetooth scan. Check that Bluetooth is on."));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Prepares the g1v0 image on the phone, as app/g2/firmware-builder.ts does for a G2: downloads Even's public firmware,
|
||||
* checks it, and puts the parts this firmware embeds back into what the app ships. The result is byte for byte the
|
||||
* image that was built, tested and signed, and it is checked to be. It does not touch the glasses.
|
||||
*/
|
||||
import { knownFolders } from "@nativescript/core";
|
||||
|
||||
import { firmwareSha256, writeFirmwareFile } from "../native/firmware-files";
|
||||
import { decodeBase64 } from "../util/base64";
|
||||
import { fetchWithUserAgent } from "../util/http";
|
||||
import { G1V0_IMAGE } from "./firmware/g1v0-image";
|
||||
import { G1V0_OWN_BYTES_BASE64 } from "./firmware/g1v0-own-bytes";
|
||||
import { assembleG1Image, decodeG1OwnBytes, G1FirmwareError, readZipEntry } from "./firmware-image";
|
||||
|
||||
export type G1BuildProgress = "downloading" | "verifying" | "assembling" | "writing";
|
||||
|
||||
export type BuiltG1Firmware = { path: string; bytes: number; version: string };
|
||||
|
||||
function sha256(bytes: Uint8Array): string {
|
||||
return firmwareSha256(bytes.slice().buffer);
|
||||
}
|
||||
|
||||
async function downloadStockZip(): Promise<Uint8Array> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetchWithUserAgent(G1V0_IMAGE.stockZipUrl);
|
||||
} catch (error) {
|
||||
throw new G1FirmwareError(`Could not reach Even's firmware server: ${(error as Error)?.message ?? error}`);
|
||||
}
|
||||
if (!response.ok) throw new G1FirmwareError(`The firmware download failed (HTTP ${response.status}).`);
|
||||
return new Uint8Array(await response.arrayBuffer());
|
||||
}
|
||||
|
||||
export async function buildG1Firmware(onProgress?: (progress: G1BuildProgress) => void): Promise<BuiltG1Firmware> {
|
||||
onProgress?.("downloading");
|
||||
const zip = await downloadStockZip();
|
||||
|
||||
onProgress?.("verifying");
|
||||
const zipHash = sha256(zip);
|
||||
if (zipHash !== G1V0_IMAGE.stockZipSha256) {
|
||||
throw new G1FirmwareError(`Even's firmware download failed verification.\nexpected ${G1V0_IMAGE.stockZipSha256}\ngot ${zipHash}`);
|
||||
}
|
||||
|
||||
onProgress?.("assembling");
|
||||
const own = decodeG1OwnBytes(G1V0_OWN_BYTES_BASE64, decodeBase64);
|
||||
const image = assembleG1Image(G1V0_IMAGE, own, (name) => readZipEntry(zip, name), sha256);
|
||||
|
||||
onProgress?.("writing");
|
||||
const path = `${knownFolders.documents().path}/g1v0_${G1V0_IMAGE.version}.bin`;
|
||||
writeFirmwareFile(path, image.slice().buffer);
|
||||
return { path, bytes: image.length, version: G1V0_IMAGE.version };
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Rebuilding the g1v0 image from what the app ships and Even's public firmware download. No I/O, so tests can run it
|
||||
* on real or made-up files. tools/g1_firmware_bundle.py is the other half.
|
||||
*/
|
||||
import { inflate, inflateRaw } from "pako";
|
||||
|
||||
import type { G1FirmwareImage } from "./firmware/g1v0-image";
|
||||
|
||||
export class G1FirmwareError extends Error {}
|
||||
|
||||
/** One file from a zip; throws when it's absent or in a form a firmware zip doesn't use. */
|
||||
export function readZipEntry(zip: Uint8Array, name: string): Uint8Array {
|
||||
const view = new DataView(zip.buffer, zip.byteOffset, zip.byteLength);
|
||||
const END_OF_DIRECTORY = 0x06054b50;
|
||||
const DIRECTORY_ENTRY = 0x02014b50;
|
||||
const LOCAL_HEADER = 0x04034b50;
|
||||
let end = zip.length - 22;
|
||||
while (end >= 0 && view.getUint32(end, true) !== END_OF_DIRECTORY) end--;
|
||||
if (end < 0) throw new G1FirmwareError("The firmware download is not a zip file.");
|
||||
const count = view.getUint16(end + 10, true);
|
||||
let at = view.getUint32(end + 16, true);
|
||||
for (let i = 0; i < count; i++) {
|
||||
if (at + 46 > zip.length || view.getUint32(at, true) !== DIRECTORY_ENTRY) break;
|
||||
const method = view.getUint16(at + 10, true);
|
||||
const packedSize = view.getUint32(at + 20, true);
|
||||
const nameLength = view.getUint16(at + 28, true);
|
||||
const extraLength = view.getUint16(at + 30, true);
|
||||
const commentLength = view.getUint16(at + 32, true);
|
||||
const headerAt = view.getUint32(at + 42, true);
|
||||
const entryName = String.fromCharCode(...zip.subarray(at + 46, at + 46 + nameLength));
|
||||
if (entryName === name || entryName.endsWith(`/${name}`)) {
|
||||
if (view.getUint32(headerAt, true) !== LOCAL_HEADER) throw new G1FirmwareError(`Bad zip entry for ${name}.`);
|
||||
const dataAt = headerAt + 30 + view.getUint16(headerAt + 26, true) + view.getUint16(headerAt + 28, true);
|
||||
const packed = zip.subarray(dataAt, dataAt + packedSize);
|
||||
if (method === 0) return packed.slice();
|
||||
if (method === 8) return inflateRaw(packed);
|
||||
throw new G1FirmwareError(`${name} uses zip method ${method}.`);
|
||||
}
|
||||
at += 46 + nameLength + extraLength + commentLength;
|
||||
}
|
||||
throw new G1FirmwareError(`The firmware download has no ${name}.`);
|
||||
}
|
||||
|
||||
export function decodeG1OwnBytes(base64: string, decode: (text: string) => Uint8Array): Uint8Array {
|
||||
return inflate(decode(base64));
|
||||
}
|
||||
|
||||
/**
|
||||
* The finished image. [fileBytes] gives one of Even's unpacked files by name, [sha256] a lowercase hex digest. Each part
|
||||
* and the result are checked, so a success is byte for byte the image that was built and signed.
|
||||
*/
|
||||
export function assembleG1Image(
|
||||
manifest: G1FirmwareImage,
|
||||
own: Uint8Array,
|
||||
fileBytes: (name: string) => Uint8Array,
|
||||
sha256: (bytes: Uint8Array) => string,
|
||||
): Uint8Array {
|
||||
const image = new Uint8Array(manifest.size);
|
||||
let position = 0;
|
||||
let ownAt = 0;
|
||||
for (const part of [...manifest.parts].sort((a, b) => a.offset - b.offset)) {
|
||||
const before = part.offset - position;
|
||||
image.set(own.subarray(ownAt, ownAt + before), position);
|
||||
ownAt += before;
|
||||
const bytes = fileBytes(part.file).subarray(part.fileOffset, part.fileOffset + part.length);
|
||||
if (bytes.length !== part.length || sha256(bytes) !== part.sha256) {
|
||||
throw new G1FirmwareError(`${part.file} in Even's download is not the one this firmware was built with.`);
|
||||
}
|
||||
image.set(bytes, part.offset);
|
||||
position = part.offset + part.length;
|
||||
}
|
||||
image.set(own.subarray(ownAt), position);
|
||||
if (ownAt + (manifest.size - position) !== own.length) {
|
||||
throw new G1FirmwareError("The firmware bundled with this app is damaged.");
|
||||
}
|
||||
const digest = sha256(image);
|
||||
if (digest !== manifest.sha256) {
|
||||
throw new G1FirmwareError(`The assembled firmware failed verification.\nexpected ${manifest.sha256}\ngot ${digest}`);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// AUTO-GENERATED by tools/g1_firmware_bundle.py -- do not edit by hand.
|
||||
// What the app installs on a G1, and the parts of it that come from Even's own download.
|
||||
|
||||
export type G1FirmwarePart = { offset: number; length: number; file: string; fileOffset: number; sha256: string };
|
||||
|
||||
export type G1FirmwareImage = {
|
||||
version: string;
|
||||
sha256: string;
|
||||
size: number;
|
||||
source: string;
|
||||
stockVersion: string;
|
||||
stockZipUrl: string;
|
||||
stockZipSha256: string;
|
||||
parts: G1FirmwarePart[];
|
||||
};
|
||||
|
||||
export const G1V0_IMAGE: G1FirmwareImage = {
|
||||
"version": "0.5.64",
|
||||
"sha256": "25e759efbe17093390386ac286865515d1f3f47efa8e40b5f054685fc7b2d6b6",
|
||||
"size": 574408,
|
||||
"source": "51596992c1f1",
|
||||
"stockVersion": "1.6.6",
|
||||
"stockZipUrl": "https://cdn.evenreal.co/firmware/dcfa510c090e476c10a6f6c348a22415.zip",
|
||||
"stockZipSha256": "748836f2d9d092b0783cbb7672fdccec4f730051cad27f095bbb156ac3e5d316",
|
||||
"parts": [
|
||||
{
|
||||
"offset": 263344,
|
||||
"length": 226020,
|
||||
"file": "net_core_app_update.bin",
|
||||
"fileOffset": 0,
|
||||
"sha256": "c277749be97a2763742db399afeb10ca6916059abd60d41bbc05c8892c002c77"
|
||||
},
|
||||
{
|
||||
"offset": 489392,
|
||||
"length": 352,
|
||||
"file": "app_update.bin",
|
||||
"fileOffset": 531288,
|
||||
"sha256": "92e73a6dd92da734bc21e8184d94d69e9a49e23056e378670b24334a305a953d"
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
// AUTO-GENERATED by tools/g1_firmware_bundle.py -- do not edit by hand.
|
||||
// The built g1v0 image without Even's parts (g1v0-image.ts), deflated.
|
||||
|
||||
export const G1V0_OWN_BYTES_BASE64 = "eNrsfIl/U1X2+H1Lkpc0tCktmC5gFgppg5guaKEgaVJelxTagswguKSAmoJLsC6ldSQsSgEZEUQq6FccdAYVpWVRSq2pgIP7JCwji0pdQ1upL20hL1vf79yXFpj5/v6C3+fXfu57527nnnvuOeeeu7xMP9z/MsJ/JEJ1HYwIIokZDf8J///v/+k/9bsSTcBEoz/fR6OMb8j/FW782/TP62neT2PwthP/P/x3wHyt6C4k9/s7rePcB66g9O/8g3ePOEdwqLyiu4Ms76507vej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+EYrNDh2B2HMaw6RJVP02rY27hfQp2LVe2oU6rhDsVXSNlvu4g7k1smsoVXB+YhDTzvHmPPK1qT8q6FOLlZpECvPhCODCrDY3MLitIa4u4kTLG2+UMVXKX+FI/unFh+DJ7G2XrHAUuuZ5oOaTNL7uDS+PF2gy2zLMXxYqynVfMdjTf0VF8+Tqwh8XwHNXRQg4QaKbYFDiIH8jMI3YJapNtRe76hr4XQpdlSnUiXaDtf236JNwuBl924P75lt3ENoXWW497FDYRR4tU3CMbjHiJXX/uJbzekfAx2h/elNUiM5DlSp7B95yONZ8+e9+H+m/6UXUzDe5iP6EepJmc+pMH7c
Line truncated
|
||||
@@ -0,0 +1,88 @@
|
||||
/** What installing firmware on a G1 has to decide. No I/O. */
|
||||
import type { GlassesPairCandidate } from "../g2/pairing-candidates";
|
||||
|
||||
export type G1Side = "left" | "right";
|
||||
|
||||
/** Left first: a right arm on g1v0 next to a left arm on Even's firmware would leave the left eye dark. */
|
||||
export const G1_SIDES: readonly G1Side[] = ["left", "right"];
|
||||
|
||||
export const G1_CASE_INSTRUCTIONS =
|
||||
"Put the glasses in their charging case with the lid open, and plug the case in. Stay nearby, and leave Faceclaw open.";
|
||||
|
||||
export const G1_INSTALL_NOTICE =
|
||||
"This replaces the firmware on both arms, about two minutes each. Going back to Even's firmware later takes a computer, and " +
|
||||
"may not be the version you have now. Installing custom firmware is not covered by warranty.";
|
||||
|
||||
/** What the native flow says when it finishes (G1ArmFlashFlow). */
|
||||
export type G1FlashResult = {
|
||||
flashed?: boolean;
|
||||
firmware?: "g1v0" | "stock";
|
||||
version?: string;
|
||||
safeMode?: boolean;
|
||||
inCase?: boolean;
|
||||
battery?: number | null;
|
||||
};
|
||||
|
||||
export function parseG1FlashResult(detail: string): G1FlashResult {
|
||||
try {
|
||||
const parsed = JSON.parse(detail);
|
||||
return parsed && typeof parsed === "object" ? (parsed as G1FlashResult) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/** Dotted versions, component by component; a missing component counts as 0. */
|
||||
export function compareG1Versions(a: string, b: string): number {
|
||||
const parts = (version: string) => version.split(".").map((part) => parseInt(part, 10) || 0);
|
||||
const left = parts(a);
|
||||
const right = parts(b);
|
||||
for (let i = 0; i < Math.max(left.length, right.length); i++) {
|
||||
const delta = (left[i] ?? 0) - (right[i] ?? 0);
|
||||
if (delta !== 0) return delta;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
export type G1Wanted = { serial?: string | null; left?: string; right?: string };
|
||||
|
||||
const serialOf = (serial: string | null | undefined) => (serial ?? "").trim().toUpperCase();
|
||||
|
||||
export function sameG1Serial(a: string | null | undefined, b: string | null | undefined): boolean {
|
||||
return serialOf(a) !== "" && serialOf(a) === serialOf(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* The pair being installed on, among those heard, once both arms have been. Its serial decides, as a flash changes an arm's
|
||||
* address; an address it was saved under also tells. Only with neither known does the only G1 around count: firmware
|
||||
* must never go to someone else's glasses because the wanted ones weren't heard.
|
||||
*/
|
||||
export function pickG1Pair(pairs: readonly GlassesPairCandidate[], wanted: G1Wanted): GlassesPairCandidate | null {
|
||||
const g1 = pairs.filter((pair) => pair.g1Firmware);
|
||||
const complete = (pair: GlassesPairCandidate | undefined) => (pair?.left && pair.right ? pair : null);
|
||||
const bySerial = g1.find((pair) => sameG1Serial(pair.serial, wanted.serial));
|
||||
if (bySerial) return complete(bySerial);
|
||||
const byAddress = g1.find((pair) =>
|
||||
(!!wanted.left && pair.left?.address === wanted.left) || (!!wanted.right && pair.right?.address === wanted.right));
|
||||
if (byAddress) return complete(byAddress);
|
||||
if (serialOf(wanted.serial) || wanted.left || wanted.right) return null;
|
||||
return g1.length === 1 ? complete(g1[0]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* What a check of the two arms found: "stock", Even's firmware on an arm; "safe-mode", g1v0 restarted repeatedly and only
|
||||
* updates work; "older", older than the app ships; "current", nothing to do.
|
||||
*/
|
||||
export type G1FirmwareState = "stock" | "safe-mode" | "older" | "current";
|
||||
|
||||
export function g1FirmwareState(arms: readonly G1FlashResult[], shipped: string): G1FirmwareState {
|
||||
if (arms.some((arm) => arm.firmware === "stock")) return "stock";
|
||||
if (arms.some((arm) => arm.safeMode)) return "safe-mode";
|
||||
return arms.some((arm) => compareG1Versions(arm.version ?? "", shipped) < 0) ? "older" : "current";
|
||||
}
|
||||
|
||||
/** Each arm counts for half of the bar. */
|
||||
export function g1FlashPercent(side: G1Side, bytesSent: number, bytesTotal: number): number {
|
||||
const within = bytesTotal > 0 ? Math.min(1, bytesSent / bytesTotal) : 0;
|
||||
return ((side === "right" ? 1 + within : within) / 2) * 100;
|
||||
}
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
declare module "pako" {
|
||||
export function inflate(data: Uint8Array): Uint8Array;
|
||||
export function inflateRaw(data: Uint8Array): Uint8Array;
|
||||
}
|
||||
Reference in new issue
Block a user