cards: read EF.ICCID at equip and auto-select the matching preset (v2.2.16)
- Server: _decode_iccid (nibble-swapped E.118 digits, trailing-F pad) and _read_iccid (best-effort MF/2FE2 read through the parent-scoped select helper; the previous selection is restored, the read never raises). The equip path records the digit string before the TERMINAL PROFILE, i.e. before any CAT session is active; a startup with a card does the same. The value is cleared on card removal and exposed as /api/status 'iccid' (only while connected). - PWA: when a connected status update reports a *new* ICCID, the matching card preset is selected in both SCP80 views - Secured Packet (sp-card-sel + form fill) and RAM (ram-card-sel + _ramCardIdx). Matching normalizes digits and accepts the raw EF hex form, leading zeros ignored; a manual choice for the same card is kept until the next equip, and a card removal re-arms the auto-selection. The status line shows the ICCID. - Tests: tests/test_iccid.py (decode variants, model + probe read, equip recording, disconnect clearing) and frontend/tests/cards_iccid.test.js (normalize, find, select, no-override, card swap); the card_state test harness stubs the new hook and covers the guard reset. - Docs: api.md /api/status fields, help EN+RU (SCP80 intro + Cards tab), READMEs, AGENTS. SW cache otaman-v183.
This commit is contained in:
@@ -23,7 +23,8 @@ function extractFunc(src, name) {
|
||||
|
||||
let code = 'var _pysimCardStateKey = null;\nvar _pysimCardSession = null;\n'
|
||||
+ 'var _pysimServerAvailable = null;\nvar _pysimCardEquipped = false;\n'
|
||||
+ 'var _pysimProactiveSeq = null;\nvar _pysimStkSig = null;\nvar _pysimAdmVerified = null;\n';
|
||||
+ 'var _pysimProactiveSeq = null;\nvar _pysimStkSig = null;\nvar _pysimAdmVerified = null;\n'
|
||||
+ 'var _cardsAutoIccid = null;\n';
|
||||
code += extractFunc(html, 'pysimCardStateUpdate') + '\n';
|
||||
code += extractFunc(html, 'pysimAvailabilityState') + '\n';
|
||||
code += extractFunc(html, 'pysimControlDisabled') + '\n';
|
||||
@@ -53,12 +54,13 @@ function fakeIndicator() {
|
||||
function setup() {
|
||||
const el = { textContent: 'status line', innerHTML: '' };
|
||||
const adm = fakeIndicator();
|
||||
const calls = { connected: [], resets: [], refreshStatus: [], proactive: 0 };
|
||||
const calls = { connected: [], resets: [], refreshStatus: [], proactive: 0, autoIccid: [] };
|
||||
_pysimCardStateKey = null;
|
||||
_pysimCardSession = null;
|
||||
_pysimProactiveSeq = null;
|
||||
_pysimAdmVerified = null;
|
||||
_pysimServerAvailable = null;
|
||||
_cardsAutoIccid = null;
|
||||
globalThis.document = {
|
||||
getElementById: id => id === 'state-indicator-adm' ? adm : el,
|
||||
querySelectorAll: () => [],
|
||||
@@ -68,6 +70,7 @@ function setup() {
|
||||
globalThis.pysimApplyAvailability = () => {};
|
||||
globalThis.isViewVisible = () => true;
|
||||
globalThis.pysimProactiveLogRender = () => { calls.proactive++; };
|
||||
globalThis.cardsAutoSelectByIccid = iccid => { calls.autoIccid.push(iccid); return -1; };
|
||||
return { el, adm, calls };
|
||||
}
|
||||
|
||||
@@ -106,9 +109,17 @@ test('unchanged state key does not touch the UI again', () => {
|
||||
|
||||
test('connected restores the UI and reloads card data', () => {
|
||||
const { calls } = setup();
|
||||
pysimCardStateUpdate(status({ connected: true, card_present: true, card_session: 2 }));
|
||||
pysimCardStateUpdate(status({ connected: true, card_present: true, card_session: 2, iccid: '8970119000004600098' }));
|
||||
assert.deepStrictEqual(calls.connected, [true]);
|
||||
assert.deepStrictEqual(calls.resets, [true]);
|
||||
assert.deepStrictEqual(calls.autoIccid, ['8970119000004600098']);
|
||||
});
|
||||
|
||||
test('a disconnect clears the ICCID auto-selection guard', () => {
|
||||
setup();
|
||||
_cardsAutoIccid = '8970119000004600098';
|
||||
pysimCardStateUpdate(status({ connected: false, card_present: false, card_session: 5 }));
|
||||
assert.strictEqual(_cardsAutoIccid, null);
|
||||
});
|
||||
|
||||
test('card session change triggers a data reset', () => {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
const { test } = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const html = fs.readFileSync(path.join(__dirname, '..', 'index.html'), 'utf8');
|
||||
|
||||
function extractFunc(src, name) {
|
||||
const re = new RegExp('function\\s+' + name + '\\s*\\([^)]*\\)\\s*\\{');
|
||||
const m = re.exec(src);
|
||||
if (!m) throw new Error('function ' + name + ' not found');
|
||||
let i = m.index + m[0].length - 1;
|
||||
let depth = 0;
|
||||
for (; i < src.length; i++) {
|
||||
if (src[i] === '{') depth++;
|
||||
else if (src[i] === '}') {
|
||||
depth--;
|
||||
if (depth === 0) break;
|
||||
}
|
||||
}
|
||||
return src.slice(m.index, i + 1);
|
||||
}
|
||||
|
||||
let code = '';
|
||||
for (const fn of ['swapNibbles', 'encIccid', 'decIccid',
|
||||
'cardsNormIccid', 'cardsFindByIccid', 'cardsAutoSelectByIccid']) {
|
||||
code += extractFunc(html, fn) + '\n';
|
||||
}
|
||||
eval(code);
|
||||
|
||||
// live-card style raw EF.ICCID content (nibble-swapped digits + 'F' pad)
|
||||
const RAW_HEX = '980711090000640090F8';
|
||||
const DIGITS = '8970119000004600098';
|
||||
|
||||
function fakeDoc() {
|
||||
const els = { 'sp-card-sel': { value: '' }, 'ram-card-sel': { value: '' } };
|
||||
globalThis.document = { getElementById: id => els[id] || null };
|
||||
return els;
|
||||
}
|
||||
|
||||
test('cardsNormIccid accepts digits, separators and raw EF hex', () => {
|
||||
assert.strictEqual(cardsNormIccid(DIGITS), DIGITS);
|
||||
assert.strictEqual(cardsNormIccid(' 89 70 1190-0000 4600 098 '), DIGITS);
|
||||
assert.strictEqual(cardsNormIccid(RAW_HEX), DIGITS);
|
||||
assert.strictEqual(cardsNormIccid(RAW_HEX.toLowerCase()), DIGITS);
|
||||
assert.strictEqual(cardsNormIccid(''), '');
|
||||
assert.strictEqual(cardsNormIccid(null), '');
|
||||
});
|
||||
|
||||
test('cardsFindByIccid matches regardless of the stored form', () => {
|
||||
globalThis.cards = [
|
||||
{ name: 'A', iccid: '1111111111111111111' },
|
||||
{ name: 'B', iccid: RAW_HEX },
|
||||
{ name: 'C', iccid: '89 70 1190 0000 4600 098' },
|
||||
];
|
||||
assert.strictEqual(cardsFindByIccid(DIGITS), 1);
|
||||
assert.strictEqual(cardsFindByIccid('1111111111111111111'), 0);
|
||||
assert.strictEqual(cardsFindByIccid('2222222222222222222'), -1);
|
||||
assert.strictEqual(cardsFindByIccid(''), -1);
|
||||
});
|
||||
|
||||
test('cardsAutoSelectByIccid selects the preset in both SCP80 views', () => {
|
||||
const els = fakeDoc();
|
||||
const applied = [];
|
||||
globalThis.cardsApply = idx => applied.push(idx);
|
||||
globalThis.cards = [{ name: 'A', iccid: RAW_HEX }, { name: 'B', iccid: '' }];
|
||||
globalThis._ramCardIdx = null;
|
||||
globalThis._cardsAutoIccid = null;
|
||||
const idx = cardsAutoSelectByIccid(DIGITS);
|
||||
assert.strictEqual(idx, 0);
|
||||
assert.strictEqual(els['sp-card-sel'].value, '0');
|
||||
assert.strictEqual(els['ram-card-sel'].value, '0');
|
||||
assert.deepStrictEqual(applied, ['0']);
|
||||
assert.strictEqual(globalThis._ramCardIdx, 0);
|
||||
});
|
||||
|
||||
test('cardsAutoSelectByIccid does not override a manual choice for the same card', () => {
|
||||
const els = fakeDoc();
|
||||
const applied = [];
|
||||
globalThis.cardsApply = idx => applied.push(idx);
|
||||
globalThis.cards = [{ name: 'A', iccid: DIGITS }, { name: 'B', iccid: '' }];
|
||||
globalThis._ramCardIdx = null;
|
||||
globalThis._cardsAutoIccid = null;
|
||||
assert.strictEqual(cardsAutoSelectByIccid(DIGITS), 0);
|
||||
// user picks the empty preset B for this card, then status polls repeat
|
||||
els['sp-card-sel'].value = '1';
|
||||
els['ram-card-sel'].value = '1';
|
||||
assert.strictEqual(cardsAutoSelectByIccid(DIGITS), -1);
|
||||
assert.strictEqual(els['sp-card-sel'].value, '1');
|
||||
assert.deepStrictEqual(applied, ['0']);
|
||||
});
|
||||
|
||||
test('cardsAutoSelectByIccid reacts to a card swap and to unreadable ICCIDs', () => {
|
||||
fakeDoc();
|
||||
globalThis.cardsApply = () => {};
|
||||
globalThis.cards = [{ name: 'A', iccid: DIGITS }, { name: 'B', iccid: '89390100000129506903' }];
|
||||
globalThis._cardsAutoIccid = null;
|
||||
assert.strictEqual(cardsAutoSelectByIccid(DIGITS), 0);
|
||||
assert.strictEqual(cardsAutoSelectByIccid('89390100000129506903'), 1);
|
||||
// ICCID not readable: reset the guard, keep the current selection
|
||||
assert.strictEqual(cardsAutoSelectByIccid(null), -1);
|
||||
assert.strictEqual(globalThis._cardsAutoIccid, null);
|
||||
assert.strictEqual(cardsAutoSelectByIccid(DIGITS), 0);
|
||||
assert.strictEqual(cardsAutoSelectByIccid('1234567890123456789'), -1);
|
||||
});
|
||||
|
||||
test('the card-state update wires the ICCID into the preset selection', () => {
|
||||
assert.ok(html.includes('cardsAutoSelectByIccid(status.iccid);'));
|
||||
assert.ok(html.includes('_cardsAutoIccid = null;'));
|
||||
assert.ok(html.includes("(data.iccid ? ' | ICCID: <b>' + esc(data.iccid) + '</b>' : '')"));
|
||||
});
|
||||
Reference in New Issue
Block a user