node-mavlink 1.0.3 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +18 -0
- package/dist/lib/mavlink.d.ts +13 -0
- package/dist/lib/mavlink.js +118 -49
- package/examples/mavlink-v2-3412-packets.bin +0 -0
- package/examples/send-receive-file.ts +38 -0
- package/lib/mavlink.ts +125 -56
- package/package.json +3 -3
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"version": "0.2.0",
|
3
|
+
"configurations": [
|
4
|
+
{
|
5
|
+
"name": "Example",
|
6
|
+
"type": "node",
|
7
|
+
"request": "launch",
|
8
|
+
"runtimeExecutable": "node",
|
9
|
+
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
|
10
|
+
|
11
|
+
"args": ["send-receive-file.ts", "--example", "hello"],
|
12
|
+
|
13
|
+
"cwd": "${workspaceRoot}/examples",
|
14
|
+
"internalConsoleOptions": "openOnSessionStart",
|
15
|
+
"skipFiles": ["<node_internals>/**", "node_modules/**"],
|
16
|
+
}
|
17
|
+
]
|
18
|
+
}
|
package/dist/lib/mavlink.d.ts
CHANGED
@@ -189,16 +189,29 @@ export declare class MavLinkPacket {
|
|
189
189
|
export declare class MavLinkPacketSplitter extends Transform {
|
190
190
|
private buffer;
|
191
191
|
private verbose;
|
192
|
+
private _validPackagesCount;
|
192
193
|
private _unknownPackagesCount;
|
193
194
|
private _invalidPackagesCount;
|
194
195
|
constructor(opts?: {}, verbose?: boolean);
|
195
196
|
_transform(chunk: Buffer, encoding: any, callback: TransformCallback): void;
|
197
|
+
private findStartOfPacket;
|
198
|
+
private getPacketProtocol;
|
199
|
+
private readPacketLength;
|
200
|
+
private validatePacket;
|
196
201
|
/**
|
197
202
|
* Checks if the buffer contains the entire message with signature
|
198
203
|
*
|
199
204
|
* @param buffer buffer with the message
|
200
205
|
*/
|
201
206
|
private isV2Signed;
|
207
|
+
/**
|
208
|
+
* Number of invalid packages
|
209
|
+
*/
|
210
|
+
get validPackages(): number;
|
211
|
+
/**
|
212
|
+
* Reset the number of valid packages
|
213
|
+
*/
|
214
|
+
resetValidPackagesCount(): void;
|
202
215
|
/**
|
203
216
|
* Number of invalid packages
|
204
217
|
*/
|
package/dist/lib/mavlink.js
CHANGED
@@ -234,6 +234,13 @@ MavLinkProtocolV2.PAYLOAD_OFFSET = 10;
|
|
234
234
|
MavLinkProtocolV2.INCOMPATIBILITY_FLAGS = 0;
|
235
235
|
MavLinkProtocolV2.COMPATIBILITY_FLAGS = 0;
|
236
236
|
MavLinkProtocolV2.IFLAG_SIGNED = 0x01;
|
237
|
+
/**
|
238
|
+
* Registry of known protocols by STX
|
239
|
+
*/
|
240
|
+
const KNOWN_PROTOCOLS_BY_STX = {
|
241
|
+
[MavLinkProtocolV1.START_BYTE]: MavLinkProtocolV1,
|
242
|
+
[MavLinkProtocolV2.START_BYTE]: MavLinkProtocolV2,
|
243
|
+
};
|
237
244
|
/**
|
238
245
|
* MavLink packet signature definition
|
239
246
|
*/
|
@@ -358,6 +365,15 @@ class MavLinkPacket {
|
|
358
365
|
}
|
359
366
|
}
|
360
367
|
exports.MavLinkPacket = MavLinkPacket;
|
368
|
+
/**
|
369
|
+
* This enum describes the different ways validation of a buffer can end
|
370
|
+
*/
|
371
|
+
var PacketValidationResult;
|
372
|
+
(function (PacketValidationResult) {
|
373
|
+
PacketValidationResult[PacketValidationResult["VALID"] = 0] = "VALID";
|
374
|
+
PacketValidationResult[PacketValidationResult["INVALID"] = 1] = "INVALID";
|
375
|
+
PacketValidationResult[PacketValidationResult["UNKNOWN"] = 2] = "UNKNOWN";
|
376
|
+
})(PacketValidationResult || (PacketValidationResult = {}));
|
361
377
|
/**
|
362
378
|
* A transform stream that splits the incomming data stream into chunks containing full MavLink messages
|
363
379
|
*/
|
@@ -366,84 +382,125 @@ class MavLinkPacketSplitter extends stream_1.Transform {
|
|
366
382
|
super(opts);
|
367
383
|
this.buffer = Buffer.from([]);
|
368
384
|
this.verbose = false;
|
385
|
+
this._validPackagesCount = 0;
|
369
386
|
this._unknownPackagesCount = 0;
|
370
387
|
this._invalidPackagesCount = 0;
|
371
388
|
this.verbose = verbose;
|
372
389
|
}
|
373
390
|
_transform(chunk, encoding, callback) {
|
374
391
|
this.buffer = Buffer.concat([this.buffer, chunk]);
|
375
|
-
while (
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
Protocol = MavLinkProtocolV1;
|
381
|
-
}
|
382
|
-
else {
|
383
|
-
startByteFirstOffset = this.buffer.indexOf(MavLinkProtocolV2.START_BYTE);
|
384
|
-
if (startByteFirstOffset >= 0) {
|
385
|
-
Protocol = MavLinkProtocolV2;
|
386
|
-
}
|
387
|
-
else {
|
388
|
-
// start byte not found - skipping
|
389
|
-
break;
|
390
|
-
}
|
392
|
+
while (this.buffer.byteLength > 0) {
|
393
|
+
const offset = this.findStartOfPacket(this.buffer);
|
394
|
+
if (offset === null) {
|
395
|
+
// start of the package was not found - need more data
|
396
|
+
break;
|
391
397
|
}
|
392
398
|
// fast-forward the buffer to the first start byte
|
393
|
-
if (
|
394
|
-
this.buffer = this.buffer.slice(
|
399
|
+
if (offset > 0) {
|
400
|
+
this.buffer = this.buffer.slice(offset);
|
395
401
|
}
|
402
|
+
// get protocol this buffer is encoded with
|
403
|
+
const Protocol = this.getPacketProtocol(this.buffer);
|
396
404
|
// check if the buffer contains at least the minumum size of data
|
397
405
|
if (this.buffer.length < Protocol.PAYLOAD_OFFSET + MavLinkProtocol.CHECKSUM_LENGTH) {
|
398
406
|
// current buffer shorter than the shortest message - skipping
|
399
407
|
break;
|
400
408
|
}
|
401
409
|
// check if the current buffer contains the entire message
|
402
|
-
const
|
403
|
-
const expectedBufferLength = Protocol.PAYLOAD_OFFSET
|
404
|
-
+ payloadLength
|
405
|
-
+ MavLinkProtocol.CHECKSUM_LENGTH
|
406
|
-
+ (this.isV2Signed(this.buffer) ? MavLinkPacketSignature.SIGNATURE_LENGTH : 0);
|
410
|
+
const expectedBufferLength = this.readPacketLength(this.buffer, Protocol);
|
407
411
|
if (this.buffer.length < expectedBufferLength) {
|
408
412
|
// current buffer is not fully retrieved yet - skipping
|
409
413
|
break;
|
410
414
|
}
|
411
415
|
// retrieve the buffer based on payload size
|
412
416
|
const buffer = this.buffer.slice(0, expectedBufferLength);
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
const protocol = new Protocol();
|
417
|
-
const header = protocol.header(buffer);
|
418
|
-
const magic = mavlink_mappings_2.MSG_ID_MAGIC_NUMBER[header.msgid];
|
419
|
-
if (magic) {
|
420
|
-
const crc = protocol.crc(buffer);
|
421
|
-
const trim = this.isV2Signed(buffer)
|
422
|
-
? MavLinkPacketSignature.SIGNATURE_LENGTH + MavLinkProtocol.CHECKSUM_LENGTH
|
423
|
-
: MavLinkProtocol.CHECKSUM_LENGTH;
|
424
|
-
const crc2 = (0, mavlink_mappings_1.x25crc)(buffer, 1, trim, magic);
|
425
|
-
if (crc === crc2) {
|
426
|
-
// CRC matches - accept this packet
|
417
|
+
switch (this.validatePacket(buffer, Protocol)) {
|
418
|
+
case PacketValidationResult.VALID:
|
419
|
+
this._validPackagesCount++;
|
427
420
|
this.push(buffer);
|
428
|
-
|
429
|
-
|
430
|
-
|
421
|
+
// truncate the buffer to remove the current message
|
422
|
+
this.buffer = this.buffer.slice(expectedBufferLength);
|
423
|
+
break;
|
424
|
+
case PacketValidationResult.INVALID:
|
431
425
|
this._invalidPackagesCount++;
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
426
|
+
// truncate the buffer to remove the wrongly identified STX
|
427
|
+
this.buffer = this.buffer.slice(1);
|
428
|
+
break;
|
429
|
+
case PacketValidationResult.UNKNOWN:
|
430
|
+
this._unknownPackagesCount++;
|
431
|
+
// truncate the buffer to remove the current message
|
432
|
+
this.buffer = this.buffer.slice(expectedBufferLength);
|
433
|
+
break;
|
434
|
+
}
|
435
|
+
}
|
436
|
+
callback(null);
|
437
|
+
}
|
438
|
+
findStartOfPacket(buffer) {
|
439
|
+
const stxv1 = buffer.indexOf(MavLinkProtocolV1.START_BYTE);
|
440
|
+
const stxv2 = buffer.indexOf(MavLinkProtocolV2.START_BYTE);
|
441
|
+
if (stxv1 >= 0 && stxv2 >= 0) {
|
442
|
+
// in the current buffer both STX v1 and v2 are found - get the first one
|
443
|
+
if (stxv1 < stxv2) {
|
444
|
+
return stxv1;
|
437
445
|
}
|
438
446
|
else {
|
439
|
-
|
440
|
-
|
447
|
+
return stxv2;
|
448
|
+
}
|
449
|
+
}
|
450
|
+
else if (stxv1 >= 0) {
|
451
|
+
// in the current buffer STX v1 is found
|
452
|
+
return stxv1;
|
453
|
+
}
|
454
|
+
else if (stxv2 >= 0) {
|
455
|
+
// in the current buffer STX v2 is found
|
456
|
+
return stxv2;
|
457
|
+
}
|
458
|
+
else {
|
459
|
+
// no STX found
|
460
|
+
return null;
|
461
|
+
}
|
462
|
+
}
|
463
|
+
getPacketProtocol(buffer) {
|
464
|
+
return KNOWN_PROTOCOLS_BY_STX[buffer.readUInt8(0)] || null;
|
465
|
+
}
|
466
|
+
readPacketLength(buffer, Protocol) {
|
467
|
+
// check if the current buffer contains the entire message
|
468
|
+
const payloadLength = buffer.readUInt8(1);
|
469
|
+
return Protocol.PAYLOAD_OFFSET
|
470
|
+
+ payloadLength
|
471
|
+
+ MavLinkProtocol.CHECKSUM_LENGTH
|
472
|
+
+ (this.isV2Signed(buffer) ? MavLinkPacketSignature.SIGNATURE_LENGTH : 0);
|
473
|
+
}
|
474
|
+
validatePacket(buffer, Protocol) {
|
475
|
+
const protocol = new Protocol();
|
476
|
+
const header = protocol.header(buffer);
|
477
|
+
const magic = mavlink_mappings_2.MSG_ID_MAGIC_NUMBER[header.msgid];
|
478
|
+
if (magic) {
|
479
|
+
const crc = protocol.crc(buffer);
|
480
|
+
const trim = this.isV2Signed(buffer)
|
481
|
+
? MavLinkPacketSignature.SIGNATURE_LENGTH + MavLinkProtocol.CHECKSUM_LENGTH
|
482
|
+
: MavLinkProtocol.CHECKSUM_LENGTH;
|
483
|
+
const crc2 = (0, mavlink_mappings_1.x25crc)(buffer, 1, trim, magic);
|
484
|
+
if (crc === crc2) {
|
485
|
+
// this is a proper message that is known and has been validated for corrupted data
|
486
|
+
return PacketValidationResult.VALID;
|
487
|
+
}
|
488
|
+
else {
|
489
|
+
// CRC mismatch
|
441
490
|
if (this.verbose) {
|
442
|
-
console.error(
|
491
|
+
console.error('CRC error; expected', crc2, `(0x${crc2.toString(16).padStart(4, '0')})`, 'got', crc, `(0x${crc.toString(16).padStart(4, '0')})`, '; msgid:', header.msgid, ', magic:', magic);
|
492
|
+
(0, mavlink_mappings_1.dump)(buffer);
|
443
493
|
}
|
494
|
+
return PacketValidationResult.INVALID;
|
444
495
|
}
|
445
496
|
}
|
446
|
-
|
497
|
+
else {
|
498
|
+
// unknown message (as in not generated from the XML sources)
|
499
|
+
if (this.verbose) {
|
500
|
+
console.error(`Unknown message with id ${header.msgid} (magic number not found) - skipping`);
|
501
|
+
}
|
502
|
+
return PacketValidationResult.UNKNOWN;
|
503
|
+
}
|
447
504
|
}
|
448
505
|
/**
|
449
506
|
* Checks if the buffer contains the entire message with signature
|
@@ -457,6 +514,18 @@ class MavLinkPacketSplitter extends stream_1.Transform {
|
|
457
514
|
return !!(flags & MavLinkProtocolV2.IFLAG_SIGNED);
|
458
515
|
}
|
459
516
|
}
|
517
|
+
/**
|
518
|
+
* Number of invalid packages
|
519
|
+
*/
|
520
|
+
get validPackages() {
|
521
|
+
return this._validPackagesCount;
|
522
|
+
}
|
523
|
+
/**
|
524
|
+
* Reset the number of valid packages
|
525
|
+
*/
|
526
|
+
resetValidPackagesCount() {
|
527
|
+
this, this._validPackagesCount = 0;
|
528
|
+
}
|
460
529
|
/**
|
461
530
|
* Number of invalid packages
|
462
531
|
*/
|
Binary file
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env -S npx ts-node
|
2
|
+
|
3
|
+
import { createReadStream } from 'fs'
|
4
|
+
import { MavLinkPacketSplitter, MavLinkPacketParser, MavLinkPacket } from '..'
|
5
|
+
import { minimal, common, ardupilotmega, uavionix, icarous } from '..'
|
6
|
+
|
7
|
+
const file = createReadStream('./mavlink-v2-3412-packets.bin')
|
8
|
+
|
9
|
+
const splitter = new MavLinkPacketSplitter({}, true)
|
10
|
+
|
11
|
+
const reader = file
|
12
|
+
.pipe(splitter)
|
13
|
+
.pipe(new MavLinkPacketParser())
|
14
|
+
|
15
|
+
|
16
|
+
// create a registry of mappings between a message id and a data class
|
17
|
+
const REGISTRY = {
|
18
|
+
...minimal.REGISTRY,
|
19
|
+
...common.REGISTRY,
|
20
|
+
...ardupilotmega.REGISTRY,
|
21
|
+
...uavionix.REGISTRY,
|
22
|
+
...icarous.REGISTRY,
|
23
|
+
}
|
24
|
+
|
25
|
+
reader.on('data', packet => {
|
26
|
+
const clazz = REGISTRY[packet.header.msgid]
|
27
|
+
if (clazz) {
|
28
|
+
const data = packet.protocol.data(packet.payload, clazz)
|
29
|
+
console.log(data)
|
30
|
+
}
|
31
|
+
})
|
32
|
+
|
33
|
+
file.on('close', () => {
|
34
|
+
console.log('\n\nNumber of invalid packages:', splitter.invalidPackages)
|
35
|
+
console.log('Number of unknown packages:', splitter.unknownPackagesCount)
|
36
|
+
console.log('\nTotal number of consumed packets:', splitter.validPackages)
|
37
|
+
})
|
38
|
+
|
package/lib/mavlink.ts
CHANGED
@@ -297,6 +297,14 @@ export class MavLinkProtocolV2 extends MavLinkProtocol {
|
|
297
297
|
}
|
298
298
|
}
|
299
299
|
|
300
|
+
/**
|
301
|
+
* Registry of known protocols by STX
|
302
|
+
*/
|
303
|
+
const KNOWN_PROTOCOLS_BY_STX = {
|
304
|
+
[MavLinkProtocolV1.START_BYTE]: MavLinkProtocolV1,
|
305
|
+
[MavLinkProtocolV2.START_BYTE]: MavLinkProtocolV2,
|
306
|
+
}
|
307
|
+
|
300
308
|
/**
|
301
309
|
* MavLink packet signature definition
|
302
310
|
*/
|
@@ -434,12 +442,18 @@ export class MavLinkPacket {
|
|
434
442
|
}
|
435
443
|
}
|
436
444
|
|
445
|
+
/**
|
446
|
+
* This enum describes the different ways validation of a buffer can end
|
447
|
+
*/
|
448
|
+
enum PacketValidationResult { VALID, INVALID, UNKNOWN }
|
449
|
+
|
437
450
|
/**
|
438
451
|
* A transform stream that splits the incomming data stream into chunks containing full MavLink messages
|
439
452
|
*/
|
440
453
|
export class MavLinkPacketSplitter extends Transform {
|
441
454
|
private buffer = Buffer.from([])
|
442
455
|
private verbose = false
|
456
|
+
private _validPackagesCount = 0
|
443
457
|
private _unknownPackagesCount = 0
|
444
458
|
private _invalidPackagesCount = 0
|
445
459
|
|
@@ -451,28 +465,21 @@ export class MavLinkPacketSplitter extends Transform {
|
|
451
465
|
_transform(chunk: Buffer, encoding, callback: TransformCallback) {
|
452
466
|
this.buffer = Buffer.concat([ this.buffer, chunk ])
|
453
467
|
|
454
|
-
while (
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
if (startByteFirstOffset >= 0) {
|
460
|
-
Protocol = MavLinkProtocolV1
|
461
|
-
} else {
|
462
|
-
startByteFirstOffset = this.buffer.indexOf(MavLinkProtocolV2.START_BYTE)
|
463
|
-
if (startByteFirstOffset >= 0) {
|
464
|
-
Protocol = MavLinkProtocolV2
|
465
|
-
} else {
|
466
|
-
// start byte not found - skipping
|
467
|
-
break
|
468
|
-
}
|
468
|
+
while (this.buffer.byteLength > 0) {
|
469
|
+
const offset = this.findStartOfPacket(this.buffer)
|
470
|
+
if (offset === null) {
|
471
|
+
// start of the package was not found - need more data
|
472
|
+
break
|
469
473
|
}
|
470
474
|
|
471
475
|
// fast-forward the buffer to the first start byte
|
472
|
-
if (
|
473
|
-
this.buffer = this.buffer.slice(
|
476
|
+
if (offset > 0) {
|
477
|
+
this.buffer = this.buffer.slice(offset)
|
474
478
|
}
|
475
479
|
|
480
|
+
// get protocol this buffer is encoded with
|
481
|
+
const Protocol = this.getPacketProtocol(this.buffer)
|
482
|
+
|
476
483
|
// check if the buffer contains at least the minumum size of data
|
477
484
|
if (this.buffer.length < Protocol.PAYLOAD_OFFSET + MavLinkProtocol.CHECKSUM_LENGTH) {
|
478
485
|
// current buffer shorter than the shortest message - skipping
|
@@ -480,12 +487,7 @@ export class MavLinkPacketSplitter extends Transform {
|
|
480
487
|
}
|
481
488
|
|
482
489
|
// check if the current buffer contains the entire message
|
483
|
-
const
|
484
|
-
const expectedBufferLength = Protocol.PAYLOAD_OFFSET
|
485
|
-
+ payloadLength
|
486
|
-
+ MavLinkProtocol.CHECKSUM_LENGTH
|
487
|
-
+ (this.isV2Signed(this.buffer) ? MavLinkPacketSignature.SIGNATURE_LENGTH : 0)
|
488
|
-
|
490
|
+
const expectedBufferLength = this.readPacketLength(this.buffer, Protocol)
|
489
491
|
if (this.buffer.length < expectedBufferLength) {
|
490
492
|
// current buffer is not fully retrieved yet - skipping
|
491
493
|
break
|
@@ -494,44 +496,97 @@ export class MavLinkPacketSplitter extends Transform {
|
|
494
496
|
// retrieve the buffer based on payload size
|
495
497
|
const buffer = this.buffer.slice(0, expectedBufferLength)
|
496
498
|
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
// validate message checksum including the magic byte
|
501
|
-
const protocol = new Protocol()
|
502
|
-
const header = protocol.header(buffer)
|
503
|
-
const magic = MSG_ID_MAGIC_NUMBER[header.msgid]
|
504
|
-
if (magic) {
|
505
|
-
const crc = protocol.crc(buffer)
|
506
|
-
const trim = this.isV2Signed(buffer)
|
507
|
-
? MavLinkPacketSignature.SIGNATURE_LENGTH + MavLinkProtocol.CHECKSUM_LENGTH
|
508
|
-
: MavLinkProtocol.CHECKSUM_LENGTH
|
509
|
-
const crc2 = x25crc(buffer, 1, trim, magic)
|
510
|
-
if (crc === crc2) {
|
511
|
-
// CRC matches - accept this packet
|
499
|
+
switch (this.validatePacket(buffer, Protocol)) {
|
500
|
+
case PacketValidationResult.VALID:
|
501
|
+
this._validPackagesCount++
|
512
502
|
this.push(buffer)
|
513
|
-
|
514
|
-
|
503
|
+
// truncate the buffer to remove the current message
|
504
|
+
this.buffer = this.buffer.slice(expectedBufferLength)
|
505
|
+
break
|
506
|
+
case PacketValidationResult.INVALID:
|
515
507
|
this._invalidPackagesCount++
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
508
|
+
// truncate the buffer to remove the wrongly identified STX
|
509
|
+
this.buffer = this.buffer.slice(1)
|
510
|
+
break
|
511
|
+
case PacketValidationResult.UNKNOWN:
|
512
|
+
this._unknownPackagesCount++
|
513
|
+
// truncate the buffer to remove the current message
|
514
|
+
this.buffer = this.buffer.slice(expectedBufferLength)
|
515
|
+
break
|
516
|
+
}
|
517
|
+
}
|
518
|
+
|
519
|
+
callback(null)
|
520
|
+
}
|
521
|
+
|
522
|
+
private findStartOfPacket(buffer: Buffer) {
|
523
|
+
const stxv1 = buffer.indexOf(MavLinkProtocolV1.START_BYTE)
|
524
|
+
const stxv2 = buffer.indexOf(MavLinkProtocolV2.START_BYTE)
|
525
|
+
|
526
|
+
if (stxv1 >= 0 && stxv2 >= 0) {
|
527
|
+
// in the current buffer both STX v1 and v2 are found - get the first one
|
528
|
+
if (stxv1 < stxv2) {
|
529
|
+
return stxv1
|
525
530
|
} else {
|
526
|
-
|
527
|
-
|
531
|
+
return stxv2
|
532
|
+
}
|
533
|
+
} else if (stxv1 >= 0) {
|
534
|
+
// in the current buffer STX v1 is found
|
535
|
+
return stxv1
|
536
|
+
} else if (stxv2 >= 0) {
|
537
|
+
// in the current buffer STX v2 is found
|
538
|
+
return stxv2
|
539
|
+
} else {
|
540
|
+
// no STX found
|
541
|
+
return null
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
private getPacketProtocol(buffer: Buffer) {
|
546
|
+
return KNOWN_PROTOCOLS_BY_STX[buffer.readUInt8(0)] || null
|
547
|
+
}
|
548
|
+
|
549
|
+
private readPacketLength(buffer: Buffer, Protocol: MavLinkProtocolConstructor) {
|
550
|
+
// check if the current buffer contains the entire message
|
551
|
+
const payloadLength = buffer.readUInt8(1)
|
552
|
+
return Protocol.PAYLOAD_OFFSET
|
553
|
+
+ payloadLength
|
554
|
+
+ MavLinkProtocol.CHECKSUM_LENGTH
|
555
|
+
+ (this.isV2Signed(buffer) ? MavLinkPacketSignature.SIGNATURE_LENGTH : 0)
|
556
|
+
}
|
557
|
+
|
558
|
+
private validatePacket(buffer: Buffer, Protocol: MavLinkProtocolConstructor) {
|
559
|
+
const protocol = new Protocol()
|
560
|
+
const header = protocol.header(buffer)
|
561
|
+
const magic = MSG_ID_MAGIC_NUMBER[header.msgid]
|
562
|
+
if (magic) {
|
563
|
+
const crc = protocol.crc(buffer)
|
564
|
+
const trim = this.isV2Signed(buffer)
|
565
|
+
? MavLinkPacketSignature.SIGNATURE_LENGTH + MavLinkProtocol.CHECKSUM_LENGTH
|
566
|
+
: MavLinkProtocol.CHECKSUM_LENGTH
|
567
|
+
const crc2 = x25crc(buffer, 1, trim, magic)
|
568
|
+
if (crc === crc2) {
|
569
|
+
// this is a proper message that is known and has been validated for corrupted data
|
570
|
+
return PacketValidationResult.VALID
|
571
|
+
} else {
|
572
|
+
// CRC mismatch
|
528
573
|
if (this.verbose) {
|
529
|
-
console.error(
|
574
|
+
console.error(
|
575
|
+
'CRC error; expected', crc2, `(0x${crc2.toString(16).padStart(4, '0')})`,
|
576
|
+
'got', crc, `(0x${crc.toString(16).padStart(4, '0')})`,
|
577
|
+
'; msgid:', header.msgid, ', magic:', magic
|
578
|
+
)
|
579
|
+
dump(buffer)
|
530
580
|
}
|
581
|
+
return PacketValidationResult.INVALID
|
582
|
+
}
|
583
|
+
} else {
|
584
|
+
// unknown message (as in not generated from the XML sources)
|
585
|
+
if (this.verbose) {
|
586
|
+
console.error(`Unknown message with id ${header.msgid} (magic number not found) - skipping`)
|
531
587
|
}
|
588
|
+
return PacketValidationResult.UNKNOWN
|
532
589
|
}
|
533
|
-
|
534
|
-
callback(null)
|
535
590
|
}
|
536
591
|
|
537
592
|
/**
|
@@ -550,7 +605,21 @@ export class MavLinkPacketSplitter extends Transform {
|
|
550
605
|
/**
|
551
606
|
* Number of invalid packages
|
552
607
|
*/
|
553
|
-
|
608
|
+
get validPackages() {
|
609
|
+
return this._validPackagesCount
|
610
|
+
}
|
611
|
+
|
612
|
+
/**
|
613
|
+
* Reset the number of valid packages
|
614
|
+
*/
|
615
|
+
resetValidPackagesCount() {
|
616
|
+
this,this._validPackagesCount = 0
|
617
|
+
}
|
618
|
+
|
619
|
+
/**
|
620
|
+
* Number of invalid packages
|
621
|
+
*/
|
622
|
+
get invalidPackages() {
|
554
623
|
return this._invalidPackagesCount
|
555
624
|
}
|
556
625
|
|
@@ -564,7 +633,7 @@ export class MavLinkPacketSplitter extends Transform {
|
|
564
633
|
/**
|
565
634
|
* Number of invalid packages
|
566
635
|
*/
|
567
|
-
|
636
|
+
get unknownPackagesCount() {
|
568
637
|
return this._unknownPackagesCount
|
569
638
|
}
|
570
639
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "node-mavlink",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.7",
|
4
4
|
"author": "Matthias Hryniszak <padcom@gmail.com>",
|
5
5
|
"license": "LGPL",
|
6
6
|
"description": "MavLink definitions and parsing library",
|
@@ -22,13 +22,13 @@
|
|
22
22
|
"main": "dist/index.js",
|
23
23
|
"types": "dist/index.d.ts",
|
24
24
|
"dependencies": {
|
25
|
-
"mavlink-mappings": "^1.0.
|
25
|
+
"mavlink-mappings": "^1.0.6-20220104"
|
26
26
|
},
|
27
27
|
"scripts": {
|
28
28
|
"build": "tsc"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
|
-
"@types/node": "^15.
|
31
|
+
"@types/node": "^15.14.9",
|
32
32
|
"@types/serialport": "^8.0.1",
|
33
33
|
"@types/xml2js": "^0.4.8",
|
34
34
|
"serialport": "^9.0.7",
|