Files
faceclaw/app/g2/firmware-compat.ts
T

107 lines
4.3 KiB
TypeScript

/**
* Compatibility check for the glasses firmware. Faceclaw requires the custom
* firmware: version >= 2.2.4.34 with the "imgz" extension token in the CFW
* capability string ("EVENCFW/1 img576 imgz ..."). Stock firmware sends no
* capability string at all.
*/
import { type FirmwareInfo } from "../native/faceclaw-communicator";
const MIN_FIRMWARE_VERSION = [2, 2, 4, 34];
const REQUIRED_FIRMWARE_EXTENSION = "imgz";
// The stock firmware release Faceclaw's custom image is built from. Stock at or
// below this can be flashed with our patched image; a newer stock version is
// unrecognized (its layout may differ from what our patch set targets).
export const FLASHABLE_STOCK_VERSION = [2, 2, 6, 10];
export const FLASHABLE_STOCK_VERSION_TEXT = FLASHABLE_STOCK_VERSION.join(".");
function parseDottedVersion(version: string): number[] {
return version
.trim()
.split(".")
.map((part) => {
const value = parseInt(part, 10);
return Number.isFinite(value) ? value : 0;
});
}
/** Standard component-wise compare; missing components count as 0. */
function compareVersions(a: number[], b: number[]): number {
const length = Math.max(a.length, b.length);
for (let i = 0; i < length; i++) {
const delta = (a[i] ?? 0) - (b[i] ?? 0);
if (delta !== 0) return delta;
}
return 0;
}
/**
* Human-readable explanation of why this firmware cannot run Faceclaw, or
* null if it is compatible. Returns null when no version was reported at all
* (no data is not evidence of incompatibility).
*/
export function firmwareIncompatibilityMessage(info: FirmwareInfo): string | null {
const reportedVersions = [info.leftVersion, info.rightVersion].filter((v) => v.trim().length > 0);
if (reportedVersions.length === 0) return null;
const minVersionText = MIN_FIRMWARE_VERSION.join(".");
const versionsText = `L=${info.leftVersion || "unknown"} R=${info.rightVersion || "unknown"}`;
if (reportedVersions.some((v) => compareVersions(parseDottedVersion(v), MIN_FIRMWARE_VERSION) < 0)) {
return (
`The glasses report firmware ${versionsText}, but Faceclaw requires the modified firmware, ` +
`version ${minVersionText} or newer. Displaying images will not work until the glasses firmware is updated.`
);
}
const tokens = info.capabilities.trim().split(/\s+/);
if (!tokens.includes(REQUIRED_FIRMWARE_EXTENSION)) {
return (
`The glasses firmware (${versionsText}) does not advertise the "${REQUIRED_FIRMWARE_EXTENSION}" extension` +
`${info.capabilities.trim() ? ` (reported: ${info.capabilities.trim()})` : ", which suggests stock firmware"}. ` +
`Faceclaw requires the modified firmware with compressed-image support.`
);
}
return null;
}
/** True when the glasses advertise Faceclaw's custom-firmware image extension. */
export function hasCustomFirmware(info: FirmwareInfo): boolean {
return info.capabilities.trim().split(/\s+/).includes(REQUIRED_FIRMWARE_EXTENSION);
}
/** The higher of the two arms' reported versions, or "" if none reported. */
export function reportedFirmwareVersion(info: FirmwareInfo): string {
const versions = [info.leftVersion, info.rightVersion].map((v) => v.trim()).filter(Boolean);
if (versions.length === 0) return "";
return versions.reduce((highest, current) =>
compareVersions(parseDottedVersion(current), parseDottedVersion(highest)) > 0 ? current : highest,
);
}
/**
* How the pre-flash firmware check should treat the connected glasses:
* - "custom": Faceclaw's firmware is already installed — nothing to flash.
* - "flashable-stock": stock firmware at or below the version we build from.
* - "newer-stock": stock firmware newer than we recognize — flash only on override.
* - "unknown": no version could be read (treated as a probe/connection failure).
*/
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock" | "unknown";
export function classifyOnboardingFirmware(info: FirmwareInfo): {
kind: OnboardingFirmwareKind;
version: string;
} {
if (hasCustomFirmware(info)) {
return { kind: "custom", version: reportedFirmwareVersion(info) };
}
const version = reportedFirmwareVersion(info);
if (!version) {
return { kind: "unknown", version: "" };
}
const comparison = compareVersions(parseDottedVersion(version), FLASHABLE_STOCK_VERSION);
return { kind: comparison <= 0 ? "flashable-stock" : "newer-stock", version };
}