import { encodeCmd , decodeCmd } from '../Helpers' ; /**
 * The header of an ADB message. A header is made of 6 fields, each one with 4 bytes:
 *
 * - command: The command that this message represents.
 * - arg0: The meaning depends on the command.
 * - arg1: The meaning depends on the command.
 * - length: The length of the data part of the message.
 * - checksum: Checksum for the data part of the message. Only used in version 0x01000000 of the
 *   protocol.
 * - magic: a checksum for the command. Effectivelly, `command ^ 0xffffffff`.
 */ export class MessageHeader { param { string } cmd param { number } arg0 param { number } arg1 param { number } length param { number } checksum constructor ( readonly cmd , readonly arg0 , readonly arg1 , readonly length , readonly checksum ) { } /**
     * Converts the MessageHeader into a  DataView .
     *  returns { DataView } toDataView ( ) { const view = new DataView ( new ArrayBuffer ( 24 ) ) ; const rawCmd = encodeCmd ( this . cmd ) ; const magic = rawCmd ^ 0xffffffff ; view . setUint32 ( 0 , rawCmd , true ) ; view . setUint32 ( 4 , this . arg0 , true ) ; view . setUint32 ( 8 , this . arg1 , true ) ; view . setUint32 ( 12 , this . length , true ) ; view . setUint32 ( 16 , this . checksum , true ) ; view . setUint32 ( 20 , magic , true ) ; return view ; } /**
     * Creates a header from a  DataView .
     *  param { DataView } data the  DataView that will be used to create the header.
     *  param { boolean } useChecksum static parse ( data , useChecksum = false ) { const cmd = data . getUint32 ( 0 , true ) ; const arg0 = data . getUint32 ( 4 , true ) ; const arg1 = data . getUint32 ( 8 , true ) ; const len = data . getUint32 ( 12 , true ) ; const checksum = data . getUint32 ( 16 , true ) ; if ( useChecksum && data . byteLength > 20 ) { const magic = data . getUint32 ( 20 , true ) ; if ( ( cmd ^ magic ) !== - 1 ) { throw new Error ( 'magic mismatch' ) ; } } return new MessageHeader ( decodeCmd ( cmd ) , arg0 , arg1 , len , checksum ) ; } }