duml-packet
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -28,2 +28,8 @@ "use strict"; | ||
emitter; | ||
/** | ||
* | ||
* @param {PacketOptions} packet | ||
* @param {boolean} [autoCalculate=true] if the underlying packet should be auto generated based on the data (fix any broken crcs, lengths, etc) | ||
* @returns {Packet} | ||
*/ | ||
constructor(packet, autoCalculate = true) { | ||
@@ -58,3 +64,8 @@ // If any raw types have been passed, then they will take presidence | ||
} | ||
this.sequenceID = packet.sequenceID ? packet.sequenceID : 0x0000; | ||
if (packet.sequenceID !== undefined) { | ||
this.sequenceID = packet.sequenceID; | ||
} | ||
else { | ||
this.randomSequenceID(); | ||
} | ||
this.commandTypeRaw = packet.commandTypeRaw ? packet.commandTypeRaw : 0x00; | ||
@@ -154,2 +165,16 @@ if (packet.commandTypeRaw) { | ||
} | ||
/** | ||
* Randomize the Sequence ID for this packet | ||
*/ | ||
randomSequenceID() { | ||
this.sequenceID = Math.floor(Math.random() * 65534) + 1; | ||
} | ||
/** | ||
* (Re)Calculate the packet based on field changes. | ||
* | ||
* @param {boolean} [generate=true] if the packet should use the provided values (length, crc, etc) | ||
* or generate based on the length of command payload and generate a new crc. The default | ||
* action is to generate, this is likely only something to turn off if you know what you are | ||
* doing and need to fuzz something. | ||
*/ | ||
calculatePacket(generate = true) { | ||
@@ -189,2 +214,5 @@ if (generate) { | ||
} | ||
/** | ||
* @returns {boolean} whether the packet has valid crcs (header and whole packet) | ||
*/ | ||
isValid() { | ||
@@ -194,2 +222,5 @@ return (this.crcHead === (0, crc_1.crc8Wire)(this.raw.subarray(0, 3)) && | ||
} | ||
/** | ||
* @returns {string} an easy to parse (longish) one line string to represent the packet | ||
*/ | ||
toShortString() { | ||
@@ -208,2 +239,5 @@ let commandSubType = 'UNKNOWN'; | ||
} | ||
/** | ||
* @returns {string} a multi-line string to represent all the values of the packet in a readable format | ||
*/ | ||
toLongString() { | ||
@@ -231,11 +265,31 @@ let commandSubType = 'UNKNOWN'; | ||
} | ||
/** | ||
* @returns {string} a hex representation of the underlying packet as a string | ||
*/ | ||
toHexString() { | ||
return this.toBuffer().toString('hex'); | ||
} | ||
/** | ||
* @returns {Buffer} the raw buffer representation of the packet | ||
*/ | ||
toBuffer() { | ||
return this.raw; | ||
} | ||
/** | ||
* Create a new Packet object from a provided Buffer. | ||
* | ||
* @param {Buffer} buffer data that represents the packet | ||
* @param {boolean} [autoCalculate=true] if the underlying packet should be auto generated based on the data (fix any broken crcs, lengths, etc) | ||
* @returns {Packet} | ||
*/ | ||
static fromBuffer(buffer, autoCalculate = true) { | ||
return new Packet({ raw: buffer }, autoCalculate); | ||
} | ||
/** | ||
* Create a new Packet object from a provided hex string. | ||
* | ||
* @param {string} hexString data that represents the packet | ||
* @param {boolean} [autoCalculate=true] if the underlying packet should be auto generated based on the data (fix any broken crcs, lengths, etc) | ||
* @returns {Packet} | ||
*/ | ||
static fromHexString(hexString, autoCalculate = true) { | ||
@@ -242,0 +296,0 @@ return new Packet({ raw: Buffer.from(hexString, 'hex') }, autoCalculate); |
{ | ||
"name": "duml-packet", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "For use when parsing and modifying DUML packets for DJI drones", | ||
@@ -41,12 +41,11 @@ "main": "dist/packet.js", | ||
"@types/chai": "^4.3.3", | ||
"@types/mocha": "^9.1.1", | ||
"@types/yargs": "^17.0.11", | ||
"@typescript-eslint/eslint-plugin": "^5.33.1", | ||
"@types/mocha": "^10.0.0", | ||
"@types/yargs": "^17.0.13", | ||
"@typescript-eslint/eslint-plugin": "^5.39.0", | ||
"arkit": "^1.6.4", | ||
"chai": "^4.3.6", | ||
"eslint": "^8.22.0", | ||
"eslint": "^8.24.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"mocha": "^10.0.0", | ||
"nodemon": "^2.0.12", | ||
"nyc": "^15.1.0", | ||
@@ -57,7 +56,7 @@ "pkg": "^5.8.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "4.7.4" | ||
"typescript": "4.8.4" | ||
}, | ||
"dependencies": { | ||
"yargs": "^17.5.1" | ||
"yargs": "^17.6.0" | ||
} | ||
} |
@@ -6,2 +6,11 @@ import { Packet } from './packet'; | ||
describe('packet tests', () => { | ||
it('can parse heartbeat packets correctly', () => { | ||
const expected = Buffer.from( | ||
'552c0436030a2b3f00000ec320203139303530205b442d4f53445d646973706c61795f6d6f646520360073e85528040d030a2c3f00000ea320203139303530205b442d52435d3120312028307c30297c30008128', | ||
'hex', | ||
); | ||
const packet = Packet.fromBuffer(expected); | ||
packet.toShortString(); | ||
}); | ||
it('should not nerf destinationRaw', () => { | ||
@@ -218,2 +227,56 @@ // This is to prevent regression where we improperly parsed sourceType and destinationType | ||
}); | ||
it('generates a sequence ID if one is not given for a new packet', () => { | ||
const unexpected = 0x00; | ||
const packet = new Packet({ | ||
version: 0x1, | ||
length: 0x0e, | ||
crcHead: 0x66, | ||
sourceRaw: 0x2a, | ||
destinationRaw: 0x28, | ||
commandTypeRaw: 0x40, | ||
commandSet: 0x00, | ||
command: GeneralTypes.GET_CFG_FILE, | ||
commandPayload: Buffer.from('01', 'hex'), | ||
crc: 0xc854, | ||
}); | ||
expect(packet.sequenceID, 'expected a non-empty sequence id').to.not.equal(unexpected); | ||
}); | ||
it('keeps a sequence ID if one is given for a new packet', () => { | ||
let expected = 0x00; | ||
let packet = new Packet({ | ||
version: 0x1, | ||
length: 0x0e, | ||
crcHead: 0x66, | ||
sourceRaw: 0x2a, | ||
destinationRaw: 0x28, | ||
sequenceID: 0x0000, | ||
commandTypeRaw: 0x40, | ||
commandSet: 0x00, | ||
command: GeneralTypes.GET_CFG_FILE, | ||
commandPayload: Buffer.from('01', 'hex'), | ||
crc: 0xc854, | ||
}); | ||
expect(packet.sequenceID, 'expected a "zero" sequence id').to.equal(expected); | ||
expected = 0x01; | ||
packet = new Packet({ | ||
version: 0x1, | ||
length: 0x0e, | ||
crcHead: 0x66, | ||
sourceRaw: 0x2a, | ||
destinationRaw: 0x28, | ||
sequenceID: 0x0001, | ||
commandTypeRaw: 0x40, | ||
commandSet: 0x00, | ||
command: GeneralTypes.GET_CFG_FILE, | ||
commandPayload: Buffer.from('01', 'hex'), | ||
crc: 0xc854, | ||
}); | ||
expect(packet.sequenceID, 'expected a specific sequence id').to.equal(expected); | ||
}); | ||
}); |
@@ -77,2 +77,8 @@ import { EventEmitter } from 'events'; | ||
/** | ||
* | ||
* @param {PacketOptions} packet | ||
* @param {boolean} [autoCalculate=true] if the underlying packet should be auto generated based on the data (fix any broken crcs, lengths, etc) | ||
* @returns {Packet} | ||
*/ | ||
constructor(packet: PacketOptions, autoCalculate = true) { | ||
@@ -111,3 +117,7 @@ // If any raw types have been passed, then they will take presidence | ||
this.sequenceID = packet.sequenceID ? packet.sequenceID : 0x0000; | ||
if (packet.sequenceID !== undefined) { | ||
this.sequenceID = packet.sequenceID; | ||
} else { | ||
this.randomSequenceID(); | ||
} | ||
@@ -237,2 +247,17 @@ this.commandTypeRaw = packet.commandTypeRaw ? packet.commandTypeRaw : 0x00; | ||
/** | ||
* Randomize the Sequence ID for this packet | ||
*/ | ||
randomSequenceID() { | ||
this.sequenceID = Math.floor(Math.random() * 65534) + 1; | ||
} | ||
/** | ||
* (Re)Calculate the packet based on field changes. | ||
* | ||
* @param {boolean} [generate=true] if the packet should use the provided values (length, crc, etc) | ||
* or generate based on the length of command payload and generate a new crc. The default | ||
* action is to generate, this is likely only something to turn off if you know what you are | ||
* doing and need to fuzz something. | ||
*/ | ||
calculatePacket(generate = true) { | ||
@@ -285,2 +310,5 @@ if (generate) { | ||
/** | ||
* @returns {boolean} whether the packet has valid crcs (header and whole packet) | ||
*/ | ||
isValid(): boolean { | ||
@@ -293,2 +321,5 @@ return ( | ||
/** | ||
* @returns {string} an easy to parse (longish) one line string to represent the packet | ||
*/ | ||
toShortString(): string { | ||
@@ -311,2 +342,5 @@ let commandSubType = 'UNKNOWN'; | ||
/** | ||
* @returns {string} a multi-line string to represent all the values of the packet in a readable format | ||
*/ | ||
toLongString(): string { | ||
@@ -344,2 +378,5 @@ let commandSubType = 'UNKNOWN'; | ||
/** | ||
* @returns {string} a hex representation of the underlying packet as a string | ||
*/ | ||
toHexString(): string { | ||
@@ -349,2 +386,5 @@ return this.toBuffer().toString('hex'); | ||
/** | ||
* @returns {Buffer} the raw buffer representation of the packet | ||
*/ | ||
toBuffer(): Buffer { | ||
@@ -354,7 +394,21 @@ return this.raw; | ||
static fromBuffer(buffer: Buffer, autoCalculate = true) { | ||
/** | ||
* Create a new Packet object from a provided Buffer. | ||
* | ||
* @param {Buffer} buffer data that represents the packet | ||
* @param {boolean} [autoCalculate=true] if the underlying packet should be auto generated based on the data (fix any broken crcs, lengths, etc) | ||
* @returns {Packet} | ||
*/ | ||
static fromBuffer(buffer: Buffer, autoCalculate = true): Packet { | ||
return new Packet({ raw: buffer }, autoCalculate); | ||
} | ||
static fromHexString(hexString: string, autoCalculate = true) { | ||
/** | ||
* Create a new Packet object from a provided hex string. | ||
* | ||
* @param {string} hexString data that represents the packet | ||
* @param {boolean} [autoCalculate=true] if the underlying packet should be auto generated based on the data (fix any broken crcs, lengths, etc) | ||
* @returns {Packet} | ||
*/ | ||
static fromHexString(hexString: string, autoCalculate = true): Packet { | ||
return new Packet({ raw: Buffer.from(hexString, 'hex') }, autoCalculate); | ||
@@ -361,0 +415,0 @@ } |
Sorry, the diff of this file is not supported yet
94361
16
1536
Updatedyargs@^17.6.0