Baichuan message header coder.
Every Baichuan message starts with a little-endian header. Its length
depends on the message class: the two MODERN_WITH_OFFSET classes carry a
trailing payload offset and are 24 bytes, every other class is 20 bytes.
offset size field 0 4 magic f0 de bc 0a 4 4 cmdId u32le 8 4 bodyLength u32le 12 1 channelId u8 0/251 push, 1-100 channel + 1, 250 host 13 3 messageId u24le 16 2 status u16le 24-byte classes: status code encryption 20-byte classes: encryption marker 18 2 messageClass u16be 20 4 payloadOffset u32le 24-byte classes only
Encode and decode the nonce request header
Encode and decode the nonce request header
import { assertEquals } from "@std/assert"; import { decode, encode } from "@hertzg/binstruct"; import { BAICHUAN_MESSAGE_CLASS, baichuanHeader, } from "@hertzg/reolink-api/encoding/header"; const header = { cmdId: 1, bodyLength: 0, channelId: 250, messageId: 1, encryption: 0xdc12, messageClass: BAICHUAN_MESSAGE_CLASS.LEGACY, }; const bytes = encode(baichuanHeader(), header); assertEquals(bytes.length, 20); assertEquals(decode(baichuanHeader(), bytes), header);
Creates a coder for a BaichuanHeader.
Returns the header length in bytes for a message class.
& (
| { encryption: number; status?: never; payloadOffset?: never; }
A decoded Baichuan message header, without the magic.
The four bytes f0 de bc 0a that open every message, read as u32le.
Message classes (bytes 18-19, read as u16be). The class decides the
header length, see headerLength.
-
LEGACY: number
20-byte header with an encryption marker, used for the nonce request.
-
MODERN: number
20-byte header with an encryption marker, used for replies to it.
-
MODERN_WITH_OFFSET: number
24-byte header with a status code and payload offset.
-
MODERN_WITH_OFFSET_ALT: number
Also 24 bytes with a status code and payload offset.
Usage
import * as mod from "encoding/header.ts";