aimedb: Refactor codec

This commit is contained in:
Tau
2019-11-06 17:28:29 -05:00
parent bc8185b26c
commit b278c6bf08
6 changed files with 213 additions and 124 deletions
+35 -33
View File
@@ -1,10 +1,22 @@
import { Transform } from "stream";
const registerLevels = {
none: 0,
portal: 1,
segaid: 2,
};
import { AimeResponse, RegisterLevel } from "./response";
const registerLevels = new Map<RegisterLevel, number>();
registerLevels.set("none", 0);
registerLevels.set("portal", 1);
registerLevels.set("segaid", 2);
function begin(length: number) {
const buf = Buffer.alloc(length);
buf.writeUInt16LE(0xa13e, 0x0000); // Magic?
buf.writeUInt16LE(0x3087, 0x0002); // ???
buf.writeUInt16LE(length, 0x0006);
return buf;
}
export class Encoder extends Transform {
constructor() {
@@ -14,35 +26,25 @@ export class Encoder extends Transform {
});
}
static init(length) {
const buf = Buffer.alloc(length);
_transform(msg: AimeResponse, encoding, callback) {
console.log("Aimedb: Encode", msg);
buf.writeUInt16LE(0xa13e, 0x0000); // Magic?
buf.writeUInt16LE(0x3087, 0x0002); // ???
buf.writeUInt16LE(length, 0x0006);
let buf: Buffer;
return buf;
}
_transform(chunk, encoding, callback) {
console.log("Aimedb: Encode", chunk);
let buf;
switch (chunk.cmd) {
switch (msg.type) {
case "hello":
buf = Encoder.init(0x0020);
buf = begin(0x0020);
buf.writeUInt16LE(0x0065, 0x0004); // cmd code
buf.writeUInt16LE(chunk.status, 0x0008);
buf.writeUInt16LE(msg.status, 0x0008);
break;
case "campaign":
// Still figuring this out...
buf = Encoder.init(0x0200);
buf = begin(0x0200);
buf.writeUInt16LE(0x000c, 0x0004); // cmd code
buf.writeUInt16LE(chunk.status, 0x0008);
buf.writeUInt16LE(msg.status, 0x0008);
// Campaign array starts at 0x20
// Element size is 0xA0
@@ -53,31 +55,31 @@ export class Encoder extends Transform {
// -1 aime id means card is not registered
// register level does not seem to matter
buf = Encoder.init(0x0130);
buf = begin(0x0130);
buf.writeUInt16LE(0x0010, 0x0004); // cmd code
buf.writeUInt16LE(chunk.status, 0x0008);
buf.writeInt32LE(chunk.aimeId || -1, 0x0020);
buf.writeUInt8(registerLevels[chunk.registerLevel], 0x0024);
buf.writeUInt16LE(msg.status, 0x0008);
buf.writeInt32LE(msg.aimeId || -1, 0x0020);
buf.writeUInt8(registerLevels[msg.registerLevel], 0x0024);
break;
case "register":
buf = Encoder.init(0x0030);
buf = begin(0x0030);
buf.writeUInt16LE(0x0006, 0x0004); // cmd code
buf.writeUInt16LE(chunk.status, 0x0008);
buf.writeInt32LE(chunk.aimeId, 0x0020);
buf.writeUInt16LE(msg.status, 0x0008);
buf.writeInt32LE(msg.aimeId, 0x0020);
break;
case "log":
buf = Encoder.init(0x0020);
buf = begin(0x0020);
buf.writeUInt16LE(0x000a, 0x0004); // cmd code
buf.writeUInt16LE(chunk.status, 0x0008);
buf.writeUInt16LE(msg.status, 0x0008);
break;
default:
return callback(new Error(`Unimplemented response: ${chunk.cmd}`));
return callback(new Error("Unimplemented response type"));
}
console.log("Aimedb: Send", buf);