feat(devices): DT-008 reader discovery + auto-configuration over the vendor UDP protocol
Build & push images / images (push) Successful in 3m7s
Build & push images / images (push) Successful in 3m7s
The Dingtian DT-008's Windows-only config tool speaks a small binary UDP protocol (reader :24680 unicast get/set, :24582 broadcast search/set-network), reversed in wiki/raw/dingtian_qr_linux (PROTOCOL.md + dingtian_qr.py, copied to scripts/). Reading park-2's readers with it from the booth found both in WG26 output mode — the "beeps but captures nothing" enrolment failure: they never pushed HTTP at all. The app now speaks the protocol itself (packages/devices dingtian-qr-config.ts, Node dgram, host-network container) and the dingtian-qr-reader driver uses it: - discover(): first driver to implement the wizard's Scan capability; rows show serial, IP, output mode, current server target, card format, firmware. - preconditions: output type must be HTTP (fixable — save switches it); typed serial must equal the reported one and the reader must answer on UDP (both block the save). - configureInputPush(): set-network the reader's server to this booth's IP + listen port, confirmed by polling (the reader applies it without an ack and is silent for seconds). - usesPushAuth=false: the scan GET carries no credentials, so no push secret is stored. - a reader saved without an IP warns instead of failing (legacy vendor-tool setup). Verified on the bench unit: WG26→HTTP, target 8080→8081→8080, struct byte-identical after. 15 protocol tests against a fake reader + 17 adapter tests. Wiki: device-discovery, first-run-setup, dingtian-dt008-reader, site-device-installation §2 (SSH procedure; the "needs a Windows laptop" claim withdrawn), new source page dingtian-qr-linux-tool.
This commit is contained in:
@@ -0,0 +1,346 @@
|
||||
import { createSocket, type Socket } from "node:dgram";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
QR_CMD,
|
||||
QR_OUTPUT_HTTP,
|
||||
buildDeviceInfo,
|
||||
checksum,
|
||||
discoverReaders,
|
||||
exchange,
|
||||
frameHeader,
|
||||
parseDeviceInfo,
|
||||
parseFirmware,
|
||||
parseFrame,
|
||||
parseParams,
|
||||
readFirmware,
|
||||
readParams,
|
||||
setNetwork,
|
||||
writeSetting,
|
||||
ipToBytes,
|
||||
} from "./dingtian-qr-config.js";
|
||||
|
||||
// The DT-008 configuration protocol, against a FAKE reader on loopback that behaves like
|
||||
// the real one (wiki/raw/dingtian_qr_linux/PROTOCOL.md): header + payload must arrive as
|
||||
// two datagrams, every write is acked with cmd+1 and a status byte, search answers with
|
||||
// a 96-byte DeviceInfo, set-network is applied WITHOUT a reply. Byte layouts below are
|
||||
// the worked examples and the reference capture from the bench reader HC002721.
|
||||
|
||||
// --- reference bytes -------------------------------------------------------
|
||||
|
||||
/** Build a 720-byte Params struct like the bench capture (HTTP, 10.0.10.204:8080, 8H…). */
|
||||
function referenceParams(over: Partial<{ outputType: number; server: string; serverPort: number }> = {}): Buffer {
|
||||
const b = Buffer.alloc(720);
|
||||
b.writeUInt32LE(0, 0x000); // static IP
|
||||
ipToBytes("10.0.10.12").copy(b, 0x004);
|
||||
ipToBytes("255.255.255.0").copy(b, 0x008);
|
||||
ipToBytes("10.0.10.1").copy(b, 0x00c);
|
||||
ipToBytes("8.8.8.8").copy(b, 0x010);
|
||||
ipToBytes("8.8.4.4").copy(b, 0x014);
|
||||
b.write(over.server ?? "10.0.10.204", 0x018, "utf8");
|
||||
b.writeUInt32LE(over.serverPort ?? 8080, 0x048);
|
||||
b.writeUInt32LE(0, 0x04c); // PHP
|
||||
b.writeUInt8(over.outputType ?? 3, 0x198); // HTTP
|
||||
b.writeUInt8(2, 0x19c); // 8H
|
||||
b.writeUInt8(1, 0x1a0); // add Enter
|
||||
b.writeUInt8(0, 0x1a1);
|
||||
b.writeUInt32LE(1, 0x1a4); // ASCII
|
||||
b.writeUInt32LE(1, 0x1c8);
|
||||
b.writeUInt32LE(1, 0x1ec); // restart 12 h
|
||||
b.writeUInt32LE(0, 0x1f0); // normal
|
||||
b.writeUInt32LE(0, 0x1f8); // buzzer off
|
||||
b.writeUInt32LE(20, 0x1fc); // 2000 µs
|
||||
b.writeUInt32LE(4, 0x200); // 400 µs
|
||||
b.writeUInt32LE(9600, 0x204);
|
||||
b.writeUInt32LE(8, 0x208);
|
||||
b.writeUInt32LE(1, 0x20c);
|
||||
b.writeUInt32LE(2, 0x210); // parity none
|
||||
b.writeUInt32LE(4, 0x214); // QR ASCII
|
||||
b.writeUInt32LE(1, 0x218); // fill light open
|
||||
b.writeUInt32LE(1, 0x288);
|
||||
b.writeUInt32LE(1, 0x28c);
|
||||
b.write("K:", 0x290, "utf8");
|
||||
return b;
|
||||
}
|
||||
|
||||
function referenceFirmware(serial = "HC002721"): Buffer {
|
||||
const b = Buffer.alloc(64);
|
||||
b.write("doorqrcode", 0);
|
||||
b.write("ver_client1_1.1", 16);
|
||||
b.write("v3s", 32);
|
||||
b.write("h42", 40);
|
||||
b.write(serial, 48);
|
||||
return b;
|
||||
}
|
||||
|
||||
// --- fake reader -----------------------------------------------------------
|
||||
|
||||
interface FakeReader {
|
||||
readonly unicast: number;
|
||||
readonly broadcast: number;
|
||||
readonly writes: { cmd: number; value: number }[];
|
||||
params: Buffer;
|
||||
singleDatagramFrames: number;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
/** A loopback reader. One socket plays both ports (unicast + "broadcast"). */
|
||||
async function fakeReader(params = referenceParams(), serial = "HC002721"): Promise<FakeReader> {
|
||||
const bind = (): Promise<Socket> =>
|
||||
new Promise((res) => {
|
||||
const s = createSocket("udp4");
|
||||
s.bind(0, "127.0.0.1", () => res(s));
|
||||
});
|
||||
const uni = await bind();
|
||||
const bc = await bind();
|
||||
const state: FakeReader = {
|
||||
unicast: uni.address().port,
|
||||
broadcast: bc.address().port,
|
||||
writes: [],
|
||||
params,
|
||||
singleDatagramFrames: 0,
|
||||
close: () => {
|
||||
uni.close();
|
||||
bc.close();
|
||||
},
|
||||
};
|
||||
|
||||
const reply = (sock: Socket, rinfo: { address: string; port: number }, cmd: number, payload: Buffer) => {
|
||||
// Real replies: header + payload in ONE datagram, id arbitrary.
|
||||
sock.send(Buffer.concat([frameHeader(cmd, payload, 0x1234), payload]), rinfo.port, rinfo.address);
|
||||
};
|
||||
const deviceInfo = () => {
|
||||
const p = parseParams(state.params);
|
||||
return buildDeviceInfo({ ...p, serial });
|
||||
};
|
||||
|
||||
const handle = (sock: Socket, rinfo: { address: string; port: number }, cmd: number, payload: Buffer) => {
|
||||
switch (cmd) {
|
||||
case QR_CMD.firmware:
|
||||
return reply(sock, rinfo, 1, referenceFirmware(serial));
|
||||
case QR_CMD.getParams:
|
||||
return reply(sock, rinfo, 31, state.params);
|
||||
case QR_CMD.search:
|
||||
return reply(sock, rinfo, 3, deviceInfo());
|
||||
case QR_CMD.setNetwork: {
|
||||
// Applied only when the serial matches; the real reader sends NO ack.
|
||||
const info = parseDeviceInfo(payload);
|
||||
if (info.serial !== serial) return;
|
||||
const next = Buffer.from(state.params);
|
||||
next.fill(0, 0x018, 0x048);
|
||||
next.write(info.server, 0x018, "utf8");
|
||||
next.writeUInt32LE(info.serverPort, 0x048);
|
||||
next.writeUInt32LE(info.httpLanguage, 0x04c);
|
||||
state.params = next;
|
||||
return;
|
||||
}
|
||||
case QR_CMD.outputType: {
|
||||
const v = payload.readUInt32LE(0);
|
||||
state.writes.push({ cmd, value: v });
|
||||
if (v < 1 || v > 10) return reply(sock, rinfo, cmd + 1, Buffer.from([1])); // rejected
|
||||
state.params = Buffer.from(state.params);
|
||||
state.params.writeUInt8(v, 0x198);
|
||||
return reply(sock, rinfo, cmd + 1, Buffer.from([0]));
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Two-datagram write discipline, like the hardware: a header announcing len > 0 waits
|
||||
// for the payload datagram; a header WITH payload glued on is a single-datagram frame
|
||||
// → counted and ignored.
|
||||
for (const sock of [uni, bc]) {
|
||||
let pending: { cmd: number; len: number } | null = null;
|
||||
sock.on("message", (msg, rinfo) => {
|
||||
if (pending) {
|
||||
const p = pending;
|
||||
pending = null;
|
||||
if (msg.length === p.len) handle(sock, rinfo, p.cmd, msg);
|
||||
return;
|
||||
}
|
||||
if (msg.length < 17 || msg.subarray(0, 4).toString("ascii") !== "XXXX") return;
|
||||
const cmd = msg.readUInt32LE(8);
|
||||
const len = msg.readUInt32LE(12);
|
||||
if (msg.length > 17) {
|
||||
state.singleDatagramFrames++;
|
||||
return;
|
||||
}
|
||||
if (len === 0) handle(sock, rinfo, cmd, Buffer.alloc(0));
|
||||
else pending = { cmd, len };
|
||||
});
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
let reader: FakeReader | null = null;
|
||||
const opts = () => ({
|
||||
timeoutMs: 300,
|
||||
retries: 0,
|
||||
ports: { unicast: reader!.unicast, broadcast: reader!.broadcast },
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
reader?.close();
|
||||
reader = null;
|
||||
});
|
||||
|
||||
// --- framing (worked examples from PROTOCOL.md §2) -------------------------
|
||||
|
||||
describe("DT-008 config frame", () => {
|
||||
it("search request: magic, id 0, cmd 2, len 0, checksum 0xFF", () => {
|
||||
expect(frameHeader(QR_CMD.search).toString("hex")).toBe("58585858" + "00000000" + "02000000" + "00000000" + "ff");
|
||||
});
|
||||
|
||||
it("set buzzer on (cmd 18, u32 1): checksum 0xFF − 1 = 0xFE", () => {
|
||||
const payload = Buffer.from([1, 0, 0, 0]);
|
||||
expect(checksum(payload)).toBe(0xfe);
|
||||
expect(frameHeader(18, payload).toString("hex")).toBe("58585858" + "00000000" + "12000000" + "04000000" + "fe");
|
||||
});
|
||||
|
||||
it("parses a reader ack (cmd 19, one status byte) and rejects a bad checksum", () => {
|
||||
const ack = Buffer.from("58585858" + "00000000" + "13000000" + "01000000" + "ff" + "00", "hex");
|
||||
expect(parseFrame(ack)).toEqual({ id: 0, cmd: 19, payload: Buffer.from([0]) });
|
||||
const bad = Buffer.from(ack);
|
||||
bad[16] = 0x00;
|
||||
expect(parseFrame(bad)).toBeNull();
|
||||
expect(parseFrame(Buffer.from("YYYY", "ascii"))).toBeNull();
|
||||
});
|
||||
|
||||
it("IPv4 is stored little-endian (bytes reversed): 10.0.10.12 → 0c 0a 00 0a", () => {
|
||||
expect(ipToBytes("10.0.10.12").toString("hex")).toBe("0c0a000a");
|
||||
expect(() => ipToBytes("10.0.10")).toThrow(/IPv4/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DT-008 blocks", () => {
|
||||
it("decodes the reference 720-byte params", () => {
|
||||
const p = parseParams(referenceParams());
|
||||
expect(p).toMatchObject({
|
||||
dhcp: 0,
|
||||
ip: "10.0.10.12",
|
||||
netmask: "255.255.255.0",
|
||||
gateway: "10.0.10.1",
|
||||
dns1: "8.8.8.8",
|
||||
dns2: "8.8.4.4",
|
||||
server: "10.0.10.204",
|
||||
serverPort: 8080,
|
||||
httpLanguage: 0,
|
||||
outputType: QR_OUTPUT_HTTP,
|
||||
cardInputFormat: 2,
|
||||
lastAddEnter: 1,
|
||||
restartInterval: 1,
|
||||
buzzer: 0,
|
||||
highPulseUs: 2000,
|
||||
lowPulseUs: 400,
|
||||
baudrate: 9600,
|
||||
parity: 2,
|
||||
qrOutputFormat: 4,
|
||||
fillLight: 1,
|
||||
cardPrefix: "K:",
|
||||
qrPrefix: "",
|
||||
});
|
||||
});
|
||||
|
||||
it("DeviceInfo round-trips (serial, addresses, server, port, language)", () => {
|
||||
const info = {
|
||||
serial: "HC009E84",
|
||||
ip: "10.0.10.7",
|
||||
netmask: "255.255.255.0",
|
||||
gateway: "0.0.0.0",
|
||||
dns1: "0.0.0.0",
|
||||
dns2: "0.0.0.0",
|
||||
server: "10.0.10.101",
|
||||
serverPort: 3000,
|
||||
httpLanguage: 1,
|
||||
};
|
||||
const b = buildDeviceInfo(info);
|
||||
expect(b.length).toBe(96);
|
||||
expect(b.subarray(0, 8).toString("ascii")).toBe("HC009E84");
|
||||
expect(b.readUInt32LE(88)).toBe(3000);
|
||||
expect(parseDeviceInfo(b)).toEqual(info);
|
||||
});
|
||||
|
||||
it("decodes FirmwareInfo", () => {
|
||||
expect(parseFirmware(referenceFirmware())).toEqual({
|
||||
model: "doorqrcode",
|
||||
firmware: "ver_client1_1.1",
|
||||
chip: "v3s",
|
||||
hardware: "h42",
|
||||
serial: "HC002721",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// --- transport against the fake reader ------------------------------------
|
||||
|
||||
describe("DT-008 config transport", () => {
|
||||
it("readFirmware / readParams: unicast cmd 0 and 30, reply cmd+1", async () => {
|
||||
reader = await fakeReader();
|
||||
expect((await readFirmware("127.0.0.1", opts()))?.serial).toBe("HC002721");
|
||||
const p = await readParams("127.0.0.1", opts());
|
||||
expect(p?.server).toBe("10.0.10.204");
|
||||
expect(p?.outputType).toBe(3);
|
||||
expect(reader.singleDatagramFrames).toBe(0);
|
||||
});
|
||||
|
||||
it("writeSetting sends header THEN payload (two datagrams) and returns the status byte", async () => {
|
||||
reader = await fakeReader(referenceParams({ outputType: 6 })); // WG26, like park-2
|
||||
expect(await writeSetting("127.0.0.1", QR_CMD.outputType, QR_OUTPUT_HTTP, opts())).toBe(0);
|
||||
expect(reader.writes).toEqual([{ cmd: 6, value: 3 }]);
|
||||
expect(parseParams(reader.params).outputType).toBe(3);
|
||||
expect(reader.singleDatagramFrames).toBe(0);
|
||||
});
|
||||
|
||||
it("an out-of-range value is acknowledged with status 1 and changes nothing", async () => {
|
||||
reader = await fakeReader();
|
||||
expect(await writeSetting("127.0.0.1", QR_CMD.outputType, 42, opts())).toBe(1);
|
||||
expect(parseParams(reader.params).outputType).toBe(3);
|
||||
});
|
||||
|
||||
it("no reader → null after the retries (never throws)", async () => {
|
||||
reader = await fakeReader();
|
||||
const dead = { ...opts(), ports: { unicast: 1, broadcast: 1 } }; // nobody listens on :1
|
||||
expect(await exchange("127.0.0.1", QR_CMD.getParams, undefined, { ...dead, retries: 1 })).toBeNull();
|
||||
expect(await readParams("127.0.0.1", dead)).toBeNull();
|
||||
});
|
||||
|
||||
it("discoverReaders: cmd 2 to each broadcast, collects cmd 3 DeviceInfo, dedupes by serial", async () => {
|
||||
reader = await fakeReader();
|
||||
const found = await discoverReaders({ ...opts(), broadcasts: ["127.0.0.1", "127.0.0.1"] });
|
||||
expect(found).toHaveLength(1);
|
||||
expect(found[0]).toMatchObject({
|
||||
serial: "HC002721",
|
||||
ip: "10.0.10.12",
|
||||
server: "10.0.10.204",
|
||||
serverPort: 8080,
|
||||
replyFrom: "127.0.0.1",
|
||||
});
|
||||
});
|
||||
|
||||
it("discoverReaders with no broadcast targets returns [] without touching the network", async () => {
|
||||
expect(await discoverReaders({ broadcasts: [], timeoutMs: 50 })).toEqual([]);
|
||||
});
|
||||
|
||||
it("setNetwork: broadcast cmd 32; the matching reader applies it silently (re-read to confirm)", async () => {
|
||||
reader = await fakeReader();
|
||||
const before = parseParams(reader.params);
|
||||
await setNetwork(
|
||||
{ ...before, serial: "HC002721", server: "10.0.10.101", serverPort: 3000 },
|
||||
"127.0.0.1",
|
||||
opts(),
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
const after = await readParams("127.0.0.1", opts());
|
||||
expect(after?.server).toBe("10.0.10.101");
|
||||
expect(after?.serverPort).toBe(3000);
|
||||
expect(after?.ip).toBe(before.ip); // network block preserved
|
||||
});
|
||||
|
||||
it("setNetwork for another serial is ignored by this reader", async () => {
|
||||
reader = await fakeReader();
|
||||
const before = parseParams(reader.params);
|
||||
await setNetwork({ ...before, serial: "HC00FFFF", server: "1.2.3.4", serverPort: 1 }, "127.0.0.1", opts());
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
expect((await readParams("127.0.0.1", opts()))?.server).toBe("10.0.10.204");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user