mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-06 04:44:33 +01:00
- After the install's restart the installer asks the FM-1 for its Bluetooth part: INFO's tag 'X' (firmware protocol
1.3), else VEL op 4's length (protocol 1.2, V10.6z7 .. V10.7.21), else not checked. Missing: Install once more (from
JIANT its own loader writes the part), asked again; still missing: 'btmissing' with what to do. COPY REPORT has a
"bluetooth part:" line.
- Update mode with a loader that is not JIANT's (ota-FM-1_900): Install does not resume JIANT through it
('foreignLoader': finish Return to official with Resume ticked, then Install); Return to official without the box
says 'stockNeedResume' instead of sending to Install. tip_stockNeedBackup reworded (es/en/ja).
- tools/patch_installer_downloads.py also brings the published installers fm1diag.js's flows and report and the
texts' block from this checkout; tests/patch_installer_test.py runs it on pages built from this checkout and from
e6bf12cd / 1d13ffc0, as built and as the previous tool patched them (idempotent, node --check); the workflow runs
the test before patching.
- web/test_web.mjs: the simulated FM-1 tells its Bluetooth part (tag or VEL), the new flows.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
307 lines
18 KiB
JavaScript
307 lines
18 KiB
JavaScript
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
|
|
//
|
|
// (JIANT 0.9.11) The web installer's decisions, out of the page so web/test_web.mjs runs them against simulated FM-1s:
|
|
// what firmware the connected FM-1 runs (fwKind), the two flows (Install, Return to official V15), the browser and
|
|
// MIDI checks, the official file's checks, and the report the page copies (COPY REPORT). Every refusal is an error
|
|
// with a code (fm1ota.js fail) the page translates and explains (its troubleshooting box).
|
|
import { fail, sleep } from "./fm1ota.js";
|
|
import { BackupConnection, captureBackup } from "./fm1backup.js";
|
|
import { productOf } from "./fm1pkg.js";
|
|
|
|
/* ------------------------------------------------------------- what the FM-1 runs --- */
|
|
/* its identity (the update protocol's handshake, "FM-1_015"):
|
|
* FM-1_015 official V15 -> Install writes over it
|
|
* FM-1_001..014 an older official firmware -> Return to official V15 first (no backup: it cannot make one)
|
|
* FM-1_016 official V16 (2026-10-21; its layout grew, its update loader is V15's): the beta
|
|
* installer writes JIANT over it (allowV16), the stable one asks to wait
|
|
* FM-1_017..099 an official firmware newer than V16 (none known yet): Return to official first
|
|
* FM-1_7XY Skiff (a Felucca fork: tools/build.py PRODUCT "FM-1_7" X Y)
|
|
* FM-1_9XY JIANT, Felucca or another Felucca fork: the editor's INFO tells which
|
|
* anything else another custom firmware */
|
|
export function classifyIdentity(text) {
|
|
const m = /^FM-1_(\d{3})$/.exec(String(text || ""));
|
|
if (!m) return text ? "otherfw" : "none";
|
|
const n = +m[1];
|
|
if (n === 15) return "stock";
|
|
if (n === 16) return "stock16";
|
|
if (n < 15) return "oldstock";
|
|
if (n < 100) return "newstock";
|
|
if (n >= 700 && n < 800) return "skiff";
|
|
if (n >= 900) return "felucca9";
|
|
return "otherfw";
|
|
}
|
|
/* (add15-beta6) a JIANT whose M-UPGRADE entry takes official V16's head: the add15 line from beta6 */
|
|
export function jiantTakesV16(version) {
|
|
const v = String(version || ""), m = /add(\d+)-beta(\d+)/i.exec(v), n = /\bV(\d+)\b/.exec(v); /* (V10 and later: "JIANT V10") */
|
|
return (!!m && (+m[1] > 15 || (+m[1] === 15 && +m[2] >= 6))) || (!!n && +n[1] >= 10);
|
|
}
|
|
export function infoVersion(a) {
|
|
const z = a.indexOf(0);
|
|
return z < 0 ? "" : String.fromCharCode(...a.slice(0, z));
|
|
}
|
|
/* (0.6.62) JIANT: its tag 0x4A in INFO, or an older JIANT's six tracks with a v0.x version */
|
|
export function isJiantInfo(a) {
|
|
const z = a.indexOf(0); if (z < 0) return false;
|
|
const version = String.fromCharCode(...a.slice(0, z));
|
|
if (!/^(FELUCCA|JIANT) /i.test(version)) return false;
|
|
let pos = z + 1; const engines = a[pos]; pos += 5;
|
|
for (let i = 0; i < engines; i++) { const end = a.indexOf(0, pos); if (end < 0) return false; pos = end + 1; }
|
|
const ntrk = a[pos], ext = a.slice(pos + 1);
|
|
for (let i = 0; i + 2 < ext.length; i++) if (ext[i] === 0x4A && ext[i + 1] === 1 && ext[i + 2] === 1) return true;
|
|
return ntrk === 6 && /^(FELUCCA|JIANT) v0\./i.test(version);
|
|
}
|
|
/* the complete backup (cmds 65..67, INFO 42 01 bit 0): JIANT, Felucca 1.0.3 and later, Skiff (all lay INFO out alike) */
|
|
export function archiveInfo(a) {
|
|
let pos = a.indexOf(0); if (pos < 0) throw new Error("Invalid INFO");
|
|
const version = String.fromCharCode(...a.slice(0, pos)); pos++;
|
|
const engines = a[pos]; pos += 5;
|
|
for (let i = 0; i < engines; i++) { const end = a.indexOf(0, pos); if (end < 0) throw new Error("Invalid INFO"); pos = end + 1; }
|
|
const ext = a.slice(pos + 1);
|
|
const supported = /^(FELUCCA|JIANT) /i.test(version) && ext[0] === 16 && ext[1] === 0x55 && ext[2] === 1 &&
|
|
ext[4] === 0x4d && ext[5] === 1 && ext[8] === 0x42 && ext[9] === 1 && !!(ext[10] & 1);
|
|
return { version, supported };
|
|
}
|
|
|
|
/* the running firmware: { kind, text (identity), version (INFO's), backup (a complete backup can be taken), port };
|
|
* kind: none, stock, stock16, oldstock, newstock, jiant, felucca, skiff, otherfw. A Felucca-family firmware (7XY, 9XY) is asked
|
|
* the editor's INFO (retried: a busy firmware answers late); no answer: otherfw (or skiff), no backup */
|
|
export async function fwKind(up) {
|
|
const dev = await up.find((id) => !/^ota-/i.test(id.model), { tries: up.t.findTries });
|
|
if (!dev) return { kind: "none" };
|
|
const text = dev.id.text, cls = classifyIdentity(text), out = { kind: cls, text, version: "", backup: false, port: dev.name };
|
|
const input = dev.link.input, output = dev.link.output;
|
|
dev.link.close();
|
|
Object.defineProperty(out, "ports", { value: { input, output }, enumerable: false });
|
|
if (cls !== "felucca9" && cls !== "skiff") return out;
|
|
const link = new BackupConnection(input, output);
|
|
try {
|
|
const a = await link.request([1, []], { timeout: up.t.info, retries: up.t.infoTries - 1 });
|
|
out.version = infoVersion(a);
|
|
out.backup = archiveInfo(a).supported;
|
|
if (cls === "skiff" || /skiff/i.test(out.version)) out.kind = "skiff";
|
|
else if (isJiantInfo(a)) out.kind = "jiant";
|
|
else if (/^FELUCCA v?1\./i.test(out.version)) out.kind = "felucca";
|
|
else out.kind = "otherfw";
|
|
} catch (e) {
|
|
out.kind = cls === "skiff" ? "skiff" : "otherfw"; out.version = "(no INFO answer)";
|
|
} finally { link.close(); }
|
|
return out;
|
|
}
|
|
const fwName = (fw) => [fw.text, fw.version].filter(Boolean).join(" ");
|
|
|
|
/* ------------------------------------------------------------------ the two flows --- */
|
|
/* ctx: { up (fm1ota.js Updater), image, product (Install) or stock {image, product} (Return), recovery (the resume box),
|
|
* state (the report: flow, fw, step), ui: { say(key, arg), step(key, n), confirm(key, arg) -> bool,
|
|
* save(archive, fw), openStock() } }
|
|
* -> "done" or "cancel"; anything else is thrown with a code */
|
|
const otaOf = (up) => up.find((id) => /^ota-/i.test(id.model));
|
|
/* (V10.7.22) JIANT's update loader (firmware/loader/loader.c FELUCCA_ID), the only one that writes the package's
|
|
* Bluetooth part (its third entry: GEN, the songs' saving, Bluetooth, USB HOST). Another one waiting in update mode (the
|
|
* official loader a stopped return to official, or a stopped official update, leaves) would write JIANT's app alone:
|
|
* the FM-1 then says BLUETOOTH PART MISSING */
|
|
const JIANT_LOADER = "ota-FM-1_900";
|
|
/* (V10.7.22) INFO's Bluetooth part: the tag 58 01 s (1 this build's, 2 missing or another build's) right before the
|
|
* protocol's 43 02 1 x and the last tag (59 01 63); 0: no tag (firmware before it) */
|
|
export function infoBt(a) {
|
|
const n = a.length;
|
|
return n >= 10 && a[n - 10] === 0x58 && a[n - 9] === 1 && a[n - 7] === 0x43 && a[n - 6] === 2 && a[n - 3] === 0x59 ? a[n - 8] : 0;
|
|
}
|
|
/* the protocol's minor version (43 02 01 x right before the last tag), -1 without it (V10.6z2 and older) */
|
|
export function infoProto(a) {
|
|
const n = a.length;
|
|
return n >= 7 && a[n - 7] === 0x43 && a[n - 6] === 2 && a[n - 5] === 1 && a[n - 3] === 0x59 ? a[n - 4] : -1;
|
|
}
|
|
/* the Bluetooth part of the firmware just installed (up.installedPorts): "ok", "missing" or "unknown" (no answer, not
|
|
* JIANT, or a JIANT before protocol 1.2: nothing to check). INFO's tag when it has it, else (protocol 1.2, V10.6z6 ..
|
|
* V10.7.21) VEL op 4, which only reads: the keys' block after its 9 bytes only when the part is there */
|
|
export async function btPart(up) {
|
|
const p = up.installedPorts;
|
|
if (!p) return "unknown";
|
|
const link = new BackupConnection(p.input, p.output), o = { timeout: up.t.info, retries: up.t.infoTries - 1 };
|
|
try {
|
|
const a = await link.request([1, []], o), s = infoBt(a);
|
|
if (s) return s === 1 ? "ok" : "missing";
|
|
if (!isJiantInfo(a) || infoProto(a) < 2) return "unknown";
|
|
return (await link.request([81, [4]], o)).length > 9 ? "ok" : "missing";
|
|
} catch (_) {
|
|
return "unknown";
|
|
} finally { link.close(); }
|
|
}
|
|
/* (V10.7.22) after the write the Bluetooth part is asked for; missing: Install once more (from JIANT, JIANT's own loader
|
|
* writes it; the app's sectors, already equal, are skipped), asked again; still missing: btmissing */
|
|
async function btCheck(ctx) {
|
|
const { up, ui, state } = ctx;
|
|
ui.say("btcheck");
|
|
const bt = state.bt = await btPart(up);
|
|
if (bt !== "missing") return "done";
|
|
ui.say("btrepair");
|
|
await up.install(ctx.image, ctx.product, ui.step);
|
|
const again = await btPart(up);
|
|
state.bt = `missing, installed again: ${again}`;
|
|
if (again === "missing") throw fail("btmissing", "installed twice, the FM-1 still reports its Bluetooth part missing");
|
|
return "done";
|
|
}
|
|
|
|
export async function runInstall(ctx) {
|
|
const { up, ui, state } = ctx;
|
|
state.flow = "install";
|
|
state.bt = "";
|
|
const ota = await otaOf(up);
|
|
if (ota) { // in update mode (an interrupted install): the write goes on
|
|
ota.link.close();
|
|
state.fw = { kind: "loader", text: ota.id.text };
|
|
if (ota.id.text !== JIANT_LOADER) // (V10.7.22) not JIANT's loader: it would leave JIANT without its Bluetooth part
|
|
throw fail("foreignLoader", `update mode with ${ota.id.text}, not JIANT's loader (${JIANT_LOADER}): nothing was written`, ota.id.text);
|
|
ui.say("resuming", ota.id.text);
|
|
if (!(await up.resume(ctx.image, ui.step, ctx.product))) throw fail("stopped", "the write in update mode stopped");
|
|
return btCheck(ctx);
|
|
}
|
|
ui.say("checking");
|
|
const fw = state.fw = await fwKind(up);
|
|
if (fw.kind === "none") throw up.notFound();
|
|
if (fw.kind === "stock" || fw.kind === "jiant" || (fw.kind === "stock16" && ctx.allowV16)) {
|
|
await up.install(ctx.image, ctx.product, ui.step);
|
|
return btCheck(ctx);
|
|
}
|
|
if (fw.kind === "stock16") throw fail("stock16", `nothing was written: this FM-1 runs ${fwName(fw)} (stock16)`, fwName(fw));
|
|
ui.openStock();
|
|
throw fail(fw.kind, `nothing was written: this FM-1 runs ${fwName(fw)} (${fw.kind})`, fwName(fw));
|
|
}
|
|
|
|
export async function runStock(ctx) {
|
|
const { up, ui, state, stock } = ctx;
|
|
state.flow = "stock";
|
|
state.bt = "";
|
|
const ota = await otaOf(up);
|
|
if (ota) {
|
|
ota.link.close();
|
|
state.fw = { kind: "loader", text: ota.id.text };
|
|
if (!ctx.recovery) // (V10.7.22) JIANT's loader: Install finishes JIANT (a backup, then back);
|
|
// another (a stopped return to official): only Resume finishes it
|
|
throw ota.id.text === JIANT_LOADER ? fail("stockNeedBackup", "update mode: no backup can be taken there")
|
|
: fail("stockNeedResume", `update mode with ${ota.id.text}: no backup can be taken there, Install cannot finish it`, ota.id.text);
|
|
if (!(await ui.confirm("stockConfirm"))) return "cancel";
|
|
if (!(await up.resume(stock.image, ui.step, stock.product))) throw fail("stopped", "the write in update mode stopped");
|
|
return "done";
|
|
}
|
|
ui.say("checking");
|
|
const fw = state.fw = await fwKind(up);
|
|
if (fw.kind === "none") throw up.notFound();
|
|
/* an official firmware: nothing to back up (it does not answer the editor's backup), straight to V15 */
|
|
if (fw.kind === "stock" || fw.kind === "stock16" || fw.kind === "oldstock" || fw.kind === "newstock") {
|
|
if (!(await ui.confirm(fw.kind === "stock" ? "stockAgainConfirm" : "officialConfirm", fw.text))) return "cancel";
|
|
await up.install(stock.image, stock.product, ui.step);
|
|
return "done";
|
|
}
|
|
/* (add15-beta6) official V16 over JIANT: only a JIANT that takes V16's head (add15-beta6 and later); an older one
|
|
* refuses it after a few requests, so say so before anything (install the beta first) */
|
|
if (stock.product === "FM-1_016" && fw.kind === "jiant" && !jiantTakesV16(fw.version))
|
|
throw fail("needV16Jiant", `${fwName(fw)}: its update entry predates V16 (add15-beta6)`, fwName(fw));
|
|
/* JIANT without the complete backup (before 0.3): update JIANT first, its songs then come along */
|
|
if (fw.kind === "jiant" && !fw.backup) throw fail("stockUnsupported", `${fwName(fw)}: no complete backup`, fwName(fw));
|
|
let archive = null;
|
|
if (fw.backup) {
|
|
/* Felucca, Skiff and other forks: every object they list (their samples, Skiff's pattern bank), restorable with
|
|
* their own editor; JIANT: its archive, restorable with the JIANT editor */
|
|
const link = new BackupConnection(fw.ports.input, fw.ports.output);
|
|
try {
|
|
ui.say("backup", "0%");
|
|
archive = await captureBackup((r, o) => link.request(r, o), fw.version || fw.text,
|
|
(done, total) => ui.say("backup", `${Math.round(done / Math.max(1, total) * 100)}%`), { complete: fw.kind !== "jiant" });
|
|
} catch (e) {
|
|
state.backupError = e.message;
|
|
if (!(await ui.confirm("backupFailConfirm", e.message))) throw fail("backupfail", e.message, e.message);
|
|
} finally { link.close(); }
|
|
}
|
|
if (archive) {
|
|
archive.identity = fw.text;
|
|
await ui.save(archive, fw);
|
|
if (!(await ui.confirm("stockConfirm"))) return "cancel";
|
|
} else if (!state.backupError && !(await ui.confirm("nobackupConfirm", fwName(fw)))) return "cancel";
|
|
await up.install(stock.image, stock.product, ui.step);
|
|
return "done";
|
|
}
|
|
|
|
/* --------------------------------------------------------------- browser and MIDI --- */
|
|
export function envOf(nav = {}) {
|
|
const ua = String(nav.userAgent || ""), uad = nav.userAgentData;
|
|
const touchMac = /Macintosh/.test(ua) && (nav.maxTouchPoints || 0) > 1;
|
|
const os = /iPhone|iPad|iPod/.test(ua) || touchMac ? "ios" : /Android/.test(ua) ? "android" : /CrOS/.test(ua) ? "chromeos"
|
|
: /Windows/.test(ua) ? "windows" : /Mac OS X|Macintosh/.test(ua) ? "mac" : /Linux/.test(ua) ? "linux" : "other";
|
|
const brands = uad && Array.isArray(uad.brands) ? uad.brands.map((b) => b.brand).join(", ") : "";
|
|
const browser = os === "ios" ? "ios" : nav.brave || /Brave/.test(brands) ? "brave" : /Edg\//.test(ua) ? "edge"
|
|
: /OPR\/|Opera/.test(ua) ? "opera" : /SamsungBrowser/.test(ua) ? "samsung" : /Firefox\//.test(ua) ? "firefox"
|
|
: /Chrome\/|Chromium\//.test(ua) ? "chrome" : /Safari\//.test(ua) ? "safari" : "other";
|
|
return { ua, os, browser, brands, webmidi: typeof nav.requestMIDIAccess === "function", webusb: !!nav.usb,
|
|
lang: nav.language || "" };
|
|
}
|
|
/* no WebMIDI at all: which message (iPhone/iPad: no browser has it; Safari: none; Firefox: an old one) */
|
|
export function nomidiCode(env) {
|
|
return env.os === "ios" ? "nomidiIos" : env.browser === "safari" ? "nomidiSafari" : env.browser === "firefox" ? "nomidiFirefox" : "nomidi";
|
|
}
|
|
/* MIDI with SysEx; refused: blocked (the browser no longer asks: where to allow it) or denied (the prompt dismissed); a
|
|
* MIDI service that failed to start (Windows, after sleep) is asked again once */
|
|
export async function midiAccess(nav, log = () => {}) {
|
|
const env = envOf(nav);
|
|
if (!env.webmidi) throw fail(nomidiCode(env), `no WebMIDI in this browser (${env.browser}, ${env.os})`);
|
|
let err = null;
|
|
for (let k = 0; k < 2; k++) {
|
|
try { return await nav.requestMIDIAccess({ sysex: true }); }
|
|
catch (e) {
|
|
err = e; log(`requestMIDIAccess: ${e && e.name} ${e && e.message}`);
|
|
if (e && (e.name === "SecurityError" || e.name === "NotAllowedError")) break;
|
|
await sleep(700);
|
|
}
|
|
}
|
|
let state = "";
|
|
try { state = (await nav.permissions.query({ name: "midi", sysex: true })).state; } catch (_) {}
|
|
const refused = err && (err.name === "SecurityError" || err.name === "NotAllowedError");
|
|
throw fail(state === "denied" ? "blocked" : refused ? "denied" : "midifail", `MIDI: ${err && err.name} ${err && err.message} (permission ${state || "?"})`, err && err.name);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- the official file --- */
|
|
/* why a file picked for Return to official V15 is not FM-1.fwsc of V15 (size 699956): the ZIP as downloaded, another
|
|
* official version, a JIANT / Felucca package, a modified file. null: worth the full check (validateStockPackage) */
|
|
export function stockFileProblem(name, bytes, v15size, allowV16 = false) {
|
|
const n = String(name || "");
|
|
if (bytes.length >= 4 && bytes[0] === 0x50 && bytes[1] === 0x4B && bytes[2] === 3 && bytes[3] === 4) return { code: "stockZip", detail: n };
|
|
if (/\.(rar|7z|exe|dmg|pkg|apk)$/i.test(n)) return { code: "stockZip", detail: n };
|
|
let product = "";
|
|
try { product = bytes.length > 960 ? productOf(bytes) : ""; } catch (_) {}
|
|
if (/^FM-1_9\d\d$|^FM-1_7\d\d$/.test(product) || /^(felucca|jiant|skiff)/i.test(n)) return { code: "stockCustom", detail: `${n} ${product}`.trim() };
|
|
if (allowV16 && product === "FM-1_016") return null; /* (beta) official V16: the full check next */
|
|
if (/^FM-1_0\d\d$/.test(product) && product !== "FM-1_015") return { code: "stockOtherVer", detail: `${n}: ${product}` };
|
|
if (bytes.length !== v15size) return { code: "stockNotV15", detail: `${n}: ${bytes.length} B` };
|
|
return null;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- the report --- */
|
|
export function makeReport({ env, page = "", installer = "", state = {}, scan = null, permission = "", error = null, log = "" }) {
|
|
const fw = state.fw || {};
|
|
const lines = [
|
|
"JIANT installer report",
|
|
`date: ${new Date().toISOString()}`,
|
|
`page: ${page}`,
|
|
`installer: ${installer}`,
|
|
`browser: ${env.browser} | os: ${env.os} | lang: ${env.lang}${env.brands ? " | brands: " + env.brands : ""}`,
|
|
`user agent: ${env.ua}`,
|
|
`webmidi: ${env.webmidi ? "yes" : "NO"} | webusb: ${env.webusb ? "yes" : "no"} | midi permission: ${permission || "?"}`,
|
|
`flow: ${state.flow || "-"} | last step: ${state.step || "-"}`,
|
|
`firmware: ${fw.kind || "-"} | identity: ${fw.text || "-"} | INFO: ${fw.version || "-"} | backup: ${fw.backup ? "yes" : "no"}${fw.port ? " | port: " + fw.port : ""}`,
|
|
];
|
|
if (state.backupError) lines.push(`backup error: ${state.backupError}`);
|
|
if (state.bt) lines.push(`bluetooth part: ${state.bt}`);
|
|
if (scan) {
|
|
lines.push(`midi inputs probed (${scan.ports.length}):`);
|
|
for (const p of scan.ports) lines.push(` - ${p.name}${p.manufacturer ? " [" + p.manufacturer + "]" : ""} ${p.state}: ${p.result}`);
|
|
if (state.inputs) lines.push(`midi inputs: ${state.inputs.join(" | ") || "(none)"}`);
|
|
lines.push(`midi outputs: ${scan.outputs.join(" | ") || "(none)"}`);
|
|
} else if (state.inputs) lines.push(`midi inputs: ${state.inputs.join(" | ") || "(none)"}`);
|
|
if (state.editorTabs) lines.push(`editor tabs open: ${state.editorTabs}`);
|
|
lines.push(`error: ${error ? `${error.code || "-"}: ${error.message || error}` : "-"}`);
|
|
const tail = String(log).trim().split("\n").slice(-40);
|
|
lines.push("log:", ...tail.map((l) => " " + l));
|
|
return lines.join("\n");
|
|
}
|