Fix #17. Support cameras with short prefixes

This commit is contained in:
David Ventura
2024-05-05 18:07:34 +02:00
parent d5d4161dd8
commit 760dbc5820
2 changed files with 28 additions and 4 deletions
+4 -3
View File
@@ -4,7 +4,6 @@ import { create_P2pRdy, SendListWifi, SendUsrChk, DevSerial } from "./impl.js";
import { Session } from "./session.js"; import { Session } from "./session.js";
import { u16_swap, u32_swap } from "./utils.js"; import { u16_swap, u32_swap } from "./utils.js";
import { logger } from "./logger.js"; import { logger } from "./logger.js";
import { hexdump } from "./hexdump.js";
import { config } from "./settings.js"; import { config } from "./settings.js";
export const notImpl = (session: Session, dv: DataView) => { export const notImpl = (session: Session, dv: DataView) => {
@@ -33,11 +32,13 @@ export const handle_P2PRdy = (session: Session, _: DataView) => {
}; };
export const makeP2pRdy = (dev: DevSerial): DataView => { export const makeP2pRdy = (dev: DevSerial): DataView => {
const len = dev.prefix.length + dev.suffix.length + 8;
const outbuf = new DataView(new Uint8Array(0x14).buffer); // 8 = serial u64 const outbuf = new DataView(new Uint8Array(0x14).buffer); // 8 = serial u64
// The protocol seems to expect 4 bytes -- check the regression test
// `replies properly to PunchPkt with 3-letters-long prefix` for a case with a real device
const devPrefixLength = 4;
outbuf.add(0).writeString(dev.prefix); outbuf.add(0).writeString(dev.prefix);
outbuf.add(4).writeU64(dev.serialU64); outbuf.add(4).writeU64(dev.serialU64);
outbuf.add(8 + dev.prefix.length).writeString(dev.suffix); outbuf.add(8 + devPrefixLength).writeString(dev.suffix);
return create_P2pRdy(outbuf); return create_P2pRdy(outbuf);
}; };
+24 -1
View File
@@ -2,7 +2,7 @@ import "../shim.ts";
import assert from "assert"; import assert from "assert";
import { hexdump } from "../hexdump.js"; import { makeP2pRdy } from "../handlers.js";
import { XqBytesDec, XqBytesEnc } from "../func_replacements.js"; import { XqBytesDec, XqBytesEnc } from "../func_replacements.js";
import { parse_PunchPkt, SendDevStatus, SendStartVideo, SendUsrChk, SendWifiDetails } from "../impl.ts"; import { parse_PunchPkt, SendDevStatus, SendStartVideo, SendUsrChk, SendWifiDetails } from "../impl.ts";
import { placeholderTypes, sprintf } from "../utils.js"; import { placeholderTypes, sprintf } from "../utils.js";
@@ -122,6 +122,7 @@ describe("module", () => {
}); });
const hstrToBA = (hs) => new Uint8Array(hs.match(/../g).map((h) => parseInt(h, 16))).buffer; const hstrToBA = (hs) => new Uint8Array(hs.match(/../g).map((h) => parseInt(h, 16))).buffer;
const BATohstr = (ba) => [...new Uint8Array(ba.buffer)].map((b) => b.toString(16).padStart(2, "0")).join("");
describe("parse packet", () => { describe("parse packet", () => {
it("parses PunchPkt", () => { it("parses PunchPkt", () => {
const in_pkt_str = "f14100144241544400000000000262ca574f4e4a4d000000"; const in_pkt_str = "f14100144241544400000000000262ca574f4e4a4d000000";
@@ -136,6 +137,28 @@ describe("parse packet", () => {
assert.deepEqual(parse_PunchPkt(pkt), expected); assert.deepEqual(parse_PunchPkt(pkt), expected);
}); });
{
const in_pkt_str = "f14100145848410000000000000003e24b4d4d4542000000";
const pkt = new DataView(hstrToBA(in_pkt_str));
it("parses PunchPkt when prefix is 3 letters long", () => {
const expected = {
prefix: "XHA",
serial: "994",
suffix: "KMMEB",
serialU64: BigInt(994),
devId: "XHA994KMMEB",
};
assert.deepEqual(parse_PunchPkt(pkt), expected);
});
// https://github.com/DavidVentura/cam-reverse/issues/17#issuecomment-2094819873
it("replies properly to PunchPkt with 3-letters-long prefix", () => {
const dev = parse_PunchPkt(pkt);
const p2prdy = makeP2pRdy(dev);
let p2pstr = BATohstr(p2prdy);
assert.deepEqual(in_pkt_str.slice(8), p2pstr.slice(8));
});
}
}); });
describe("make packet", () => { describe("make packet", () => {
it("builds a good SendUsrChk", () => { it("builds a good SendUsrChk", () => {