node-mavlink 1.0.4 → 1.0.8
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/README.md +9 -1
- package/dist/lib/mavlink.d.ts +8 -0
- package/dist/lib/mavlink.js +106 -63
- package/examples/send-receive-file.ts +18 -2
- package/lib/mavlink.ts +109 -67
- package/package.json +2 -2
package/README.md
CHANGED
@@ -38,7 +38,10 @@ That's it! That is all it takes to read the raw data. But it doesn't end there -
|
|
38
38
|
Each message consists of multiple fields that contain specific data. Parsing the data is also very easy.
|
39
39
|
|
40
40
|
```javascript
|
41
|
-
import {
|
41
|
+
import {
|
42
|
+
minimal, common, ardupilotmega, uavionix, icarous,
|
43
|
+
asluav, development, matrixpilot, paparazzi, ualberta,
|
44
|
+
} from 'node-mavlink'
|
42
45
|
|
43
46
|
// create a registry of mappings between a message id and a data class
|
44
47
|
const REGISTRY = {
|
@@ -47,6 +50,11 @@ const REGISTRY = {
|
|
47
50
|
...ardupilotmega.REGISTRY,
|
48
51
|
...uavionix.REGISTRY,
|
49
52
|
...icarous.REGISTRY,
|
53
|
+
...asluav.REGISTRY,
|
54
|
+
...development.REGISTRY,
|
55
|
+
...matrixpilot.REGISTRY,
|
56
|
+
...paparazzi.REGISTRY,
|
57
|
+
...ualberta.REGISTRY,
|
50
58
|
}
|
51
59
|
|
52
60
|
reader.on('data', packet => {
|
package/dist/lib/mavlink.d.ts
CHANGED
@@ -194,6 +194,10 @@ export declare class MavLinkPacketSplitter extends Transform {
|
|
194
194
|
private _invalidPackagesCount;
|
195
195
|
constructor(opts?: {}, verbose?: boolean);
|
196
196
|
_transform(chunk: Buffer, encoding: any, callback: TransformCallback): void;
|
197
|
+
private findStartOfPacket;
|
198
|
+
private getPacketProtocol;
|
199
|
+
private readPacketLength;
|
200
|
+
private validatePacket;
|
197
201
|
/**
|
198
202
|
* Checks if the buffer contains the entire message with signature
|
199
203
|
*
|
@@ -204,6 +208,10 @@ export declare class MavLinkPacketSplitter extends Transform {
|
|
204
208
|
* Number of invalid packages
|
205
209
|
*/
|
206
210
|
get validPackages(): number;
|
211
|
+
/**
|
212
|
+
* Reset the number of valid packages
|
213
|
+
*/
|
214
|
+
resetValidPackagesCount(): void;
|
207
215
|
/**
|
208
216
|
* Number of invalid packages
|
209
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
|
*/
|
@@ -373,97 +389,118 @@ class MavLinkPacketSplitter extends stream_1.Transform {
|
|
373
389
|
}
|
374
390
|
_transform(chunk, encoding, callback) {
|
375
391
|
this.buffer = Buffer.concat([this.buffer, chunk]);
|
376
|
-
while (
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
const stxv1 = this.buffer.indexOf(MavLinkProtocolV1.START_BYTE);
|
381
|
-
const stxv2 = this.buffer.indexOf(MavLinkProtocolV2.START_BYTE);
|
382
|
-
if (stxv1 >= 0 && stxv2 >= 0) {
|
383
|
-
// in the current buffer both STX v1 and v2 are found - get the first one
|
384
|
-
if (stxv1 < stxv2) {
|
385
|
-
Protocol = MavLinkProtocolV1;
|
386
|
-
startByteFirstOffset = stxv1;
|
387
|
-
}
|
388
|
-
else if (stxv2 < stxv1) {
|
389
|
-
Protocol = MavLinkProtocolV2;
|
390
|
-
startByteFirstOffset = stxv2;
|
391
|
-
}
|
392
|
-
}
|
393
|
-
else if (stxv1 >= 0) {
|
394
|
-
// in the current buffer both STX v1 is found
|
395
|
-
Protocol = MavLinkProtocolV1;
|
396
|
-
startByteFirstOffset = stxv1;
|
397
|
-
}
|
398
|
-
else if (stxv2 >= 0) {
|
399
|
-
// in the current buffer both STX v2 is found
|
400
|
-
Protocol = MavLinkProtocolV2;
|
401
|
-
startByteFirstOffset = stxv2;
|
402
|
-
}
|
403
|
-
else {
|
404
|
-
// no STX found - continue gathering the data
|
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
|
405
396
|
break;
|
406
397
|
}
|
407
398
|
// fast-forward the buffer to the first start byte
|
408
|
-
if (
|
409
|
-
this.buffer = this.buffer.slice(
|
399
|
+
if (offset > 0) {
|
400
|
+
this.buffer = this.buffer.slice(offset);
|
410
401
|
}
|
402
|
+
// get protocol this buffer is encoded with
|
403
|
+
const Protocol = this.getPacketProtocol(this.buffer);
|
411
404
|
// check if the buffer contains at least the minumum size of data
|
412
405
|
if (this.buffer.length < Protocol.PAYLOAD_OFFSET + MavLinkProtocol.CHECKSUM_LENGTH) {
|
413
406
|
// current buffer shorter than the shortest message - skipping
|
414
407
|
break;
|
415
408
|
}
|
416
409
|
// check if the current buffer contains the entire message
|
417
|
-
const
|
418
|
-
const expectedBufferLength = Protocol.PAYLOAD_OFFSET
|
419
|
-
+ payloadLength
|
420
|
-
+ MavLinkProtocol.CHECKSUM_LENGTH
|
421
|
-
+ (this.isV2Signed(this.buffer) ? MavLinkPacketSignature.SIGNATURE_LENGTH : 0);
|
410
|
+
const expectedBufferLength = this.readPacketLength(this.buffer, Protocol);
|
422
411
|
if (this.buffer.length < expectedBufferLength) {
|
423
412
|
// current buffer is not fully retrieved yet - skipping
|
424
413
|
break;
|
425
414
|
}
|
426
415
|
// retrieve the buffer based on payload size
|
427
416
|
const buffer = this.buffer.slice(0, expectedBufferLength);
|
428
|
-
|
429
|
-
|
430
|
-
const header = protocol.header(buffer);
|
431
|
-
const magic = mavlink_mappings_2.MSG_ID_MAGIC_NUMBER[header.msgid];
|
432
|
-
if (magic) {
|
433
|
-
const crc = protocol.crc(buffer);
|
434
|
-
const trim = this.isV2Signed(buffer)
|
435
|
-
? MavLinkPacketSignature.SIGNATURE_LENGTH + MavLinkProtocol.CHECKSUM_LENGTH
|
436
|
-
: MavLinkProtocol.CHECKSUM_LENGTH;
|
437
|
-
const crc2 = (0, mavlink_mappings_1.x25crc)(buffer, 1, trim, magic);
|
438
|
-
if (crc === crc2) {
|
439
|
-
// CRC matches - accept this packet
|
417
|
+
switch (this.validatePacket(buffer, Protocol)) {
|
418
|
+
case PacketValidationResult.VALID:
|
440
419
|
this._validPackagesCount++;
|
441
420
|
this.push(buffer);
|
442
421
|
// truncate the buffer to remove the current message
|
443
422
|
this.buffer = this.buffer.slice(expectedBufferLength);
|
444
|
-
|
445
|
-
|
446
|
-
// CRC mismatch - skip packet
|
423
|
+
break;
|
424
|
+
case PacketValidationResult.INVALID:
|
447
425
|
this._invalidPackagesCount++;
|
448
|
-
|
449
|
-
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);
|
450
|
-
(0, mavlink_mappings_1.dump)(buffer);
|
451
|
-
}
|
452
|
-
// truncate the buffer to remove the current message
|
426
|
+
// truncate the buffer to remove the wrongly identified STX
|
453
427
|
this.buffer = this.buffer.slice(1);
|
454
|
-
|
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;
|
445
|
+
}
|
446
|
+
else {
|
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;
|
455
487
|
}
|
456
488
|
else {
|
457
|
-
//
|
458
|
-
this._unknownPackagesCount++;
|
459
|
-
// truncate the buffer to remove the current message
|
460
|
-
this.buffer = this.buffer.slice(expectedBufferLength);
|
489
|
+
// CRC mismatch
|
461
490
|
if (this.verbose) {
|
462
|
-
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);
|
463
493
|
}
|
494
|
+
return PacketValidationResult.INVALID;
|
464
495
|
}
|
465
496
|
}
|
466
|
-
|
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
|
+
}
|
467
504
|
}
|
468
505
|
/**
|
469
506
|
* Checks if the buffer contains the entire message with signature
|
@@ -483,6 +520,12 @@ class MavLinkPacketSplitter extends stream_1.Transform {
|
|
483
520
|
get validPackages() {
|
484
521
|
return this._validPackagesCount;
|
485
522
|
}
|
523
|
+
/**
|
524
|
+
* Reset the number of valid packages
|
525
|
+
*/
|
526
|
+
resetValidPackagesCount() {
|
527
|
+
this, this._validPackagesCount = 0;
|
528
|
+
}
|
486
529
|
/**
|
487
530
|
* Number of invalid packages
|
488
531
|
*/
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
import { createReadStream } from 'fs'
|
4
4
|
import { MavLinkPacketSplitter, MavLinkPacketParser, MavLinkPacket } from '..'
|
5
|
+
import { minimal, common, ardupilotmega, uavionix, icarous } from '..'
|
5
6
|
|
6
7
|
const file = createReadStream('./mavlink-v2-3412-packets.bin')
|
7
8
|
|
@@ -11,8 +12,22 @@ const reader = file
|
|
11
12
|
.pipe(splitter)
|
12
13
|
.pipe(new MavLinkPacketParser())
|
13
14
|
|
14
|
-
|
15
|
-
|
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
|
+
}
|
16
31
|
})
|
17
32
|
|
18
33
|
file.on('close', () => {
|
@@ -20,3 +35,4 @@ file.on('close', () => {
|
|
20
35
|
console.log('Number of unknown packages:', splitter.unknownPackagesCount)
|
21
36
|
console.log('\nTotal number of consumed packets:', splitter.validPackages)
|
22
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,6 +442,11 @@ 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
|
*/
|
@@ -452,42 +465,21 @@ export class MavLinkPacketSplitter extends Transform {
|
|
452
465
|
_transform(chunk: Buffer, encoding, callback: TransformCallback) {
|
453
466
|
this.buffer = Buffer.concat([ this.buffer, chunk ])
|
454
467
|
|
455
|
-
while (
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
let startByteFirstOffset
|
460
|
-
|
461
|
-
const stxv1 = this.buffer.indexOf(MavLinkProtocolV1.START_BYTE)
|
462
|
-
const stxv2 = this.buffer.indexOf(MavLinkProtocolV2.START_BYTE)
|
463
|
-
|
464
|
-
if (stxv1 >= 0 && stxv2 >= 0) {
|
465
|
-
// in the current buffer both STX v1 and v2 are found - get the first one
|
466
|
-
if (stxv1 < stxv2) {
|
467
|
-
Protocol = MavLinkProtocolV1
|
468
|
-
startByteFirstOffset = stxv1
|
469
|
-
} else if (stxv2 < stxv1) {
|
470
|
-
Protocol = MavLinkProtocolV2
|
471
|
-
startByteFirstOffset = stxv2
|
472
|
-
}
|
473
|
-
} else if (stxv1 >= 0) {
|
474
|
-
// in the current buffer both STX v1 is found
|
475
|
-
Protocol = MavLinkProtocolV1
|
476
|
-
startByteFirstOffset = stxv1
|
477
|
-
} else if (stxv2 >= 0) {
|
478
|
-
// in the current buffer both STX v2 is found
|
479
|
-
Protocol = MavLinkProtocolV2
|
480
|
-
startByteFirstOffset = stxv2
|
481
|
-
} else {
|
482
|
-
// no STX found - continue gathering the data
|
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
|
483
472
|
break
|
484
473
|
}
|
485
474
|
|
486
475
|
// fast-forward the buffer to the first start byte
|
487
|
-
if (
|
488
|
-
this.buffer = this.buffer.slice(
|
476
|
+
if (offset > 0) {
|
477
|
+
this.buffer = this.buffer.slice(offset)
|
489
478
|
}
|
490
479
|
|
480
|
+
// get protocol this buffer is encoded with
|
481
|
+
const Protocol = this.getPacketProtocol(this.buffer)
|
482
|
+
|
491
483
|
// check if the buffer contains at least the minumum size of data
|
492
484
|
if (this.buffer.length < Protocol.PAYLOAD_OFFSET + MavLinkProtocol.CHECKSUM_LENGTH) {
|
493
485
|
// current buffer shorter than the shortest message - skipping
|
@@ -495,12 +487,7 @@ export class MavLinkPacketSplitter extends Transform {
|
|
495
487
|
}
|
496
488
|
|
497
489
|
// check if the current buffer contains the entire message
|
498
|
-
const
|
499
|
-
const expectedBufferLength = Protocol.PAYLOAD_OFFSET
|
500
|
-
+ payloadLength
|
501
|
-
+ MavLinkProtocol.CHECKSUM_LENGTH
|
502
|
-
+ (this.isV2Signed(this.buffer) ? MavLinkPacketSignature.SIGNATURE_LENGTH : 0)
|
503
|
-
|
490
|
+
const expectedBufferLength = this.readPacketLength(this.buffer, Protocol)
|
504
491
|
if (this.buffer.length < expectedBufferLength) {
|
505
492
|
// current buffer is not fully retrieved yet - skipping
|
506
493
|
break
|
@@ -509,49 +496,97 @@ export class MavLinkPacketSplitter extends Transform {
|
|
509
496
|
// retrieve the buffer based on payload size
|
510
497
|
const buffer = this.buffer.slice(0, expectedBufferLength)
|
511
498
|
|
512
|
-
|
513
|
-
|
514
|
-
const header = protocol.header(buffer)
|
515
|
-
const magic = MSG_ID_MAGIC_NUMBER[header.msgid]
|
516
|
-
if (magic) {
|
517
|
-
const crc = protocol.crc(buffer)
|
518
|
-
const trim = this.isV2Signed(buffer)
|
519
|
-
? MavLinkPacketSignature.SIGNATURE_LENGTH + MavLinkProtocol.CHECKSUM_LENGTH
|
520
|
-
: MavLinkProtocol.CHECKSUM_LENGTH
|
521
|
-
const crc2 = x25crc(buffer, 1, trim, magic)
|
522
|
-
if (crc === crc2) {
|
523
|
-
// CRC matches - accept this packet
|
499
|
+
switch (this.validatePacket(buffer, Protocol)) {
|
500
|
+
case PacketValidationResult.VALID:
|
524
501
|
this._validPackagesCount++
|
525
502
|
this.push(buffer)
|
526
503
|
// truncate the buffer to remove the current message
|
527
504
|
this.buffer = this.buffer.slice(expectedBufferLength)
|
528
|
-
|
529
|
-
|
505
|
+
break
|
506
|
+
case PacketValidationResult.INVALID:
|
530
507
|
this._invalidPackagesCount++
|
531
|
-
|
532
|
-
console.error(
|
533
|
-
'CRC error; expected', crc2, `(0x${crc2.toString(16).padStart(4, '0')})`,
|
534
|
-
'got', crc, `(0x${crc.toString(16).padStart(4, '0')})`,
|
535
|
-
'; msgid:', header.msgid, ', magic:', magic
|
536
|
-
)
|
537
|
-
dump(buffer)
|
538
|
-
}
|
539
|
-
// truncate the buffer to remove the current message
|
508
|
+
// truncate the buffer to remove the wrongly identified STX
|
540
509
|
this.buffer = this.buffer.slice(1)
|
541
|
-
|
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
|
542
530
|
} else {
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
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
|
+
}
|
547
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
|
548
573
|
if (this.verbose) {
|
549
|
-
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)
|
550
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`)
|
551
587
|
}
|
588
|
+
return PacketValidationResult.UNKNOWN
|
552
589
|
}
|
553
|
-
|
554
|
-
callback(null)
|
555
590
|
}
|
556
591
|
|
557
592
|
/**
|
@@ -574,6 +609,13 @@ export class MavLinkPacketSplitter extends Transform {
|
|
574
609
|
return this._validPackagesCount
|
575
610
|
}
|
576
611
|
|
612
|
+
/**
|
613
|
+
* Reset the number of valid packages
|
614
|
+
*/
|
615
|
+
resetValidPackagesCount() {
|
616
|
+
this,this._validPackagesCount = 0
|
617
|
+
}
|
618
|
+
|
577
619
|
/**
|
578
620
|
* Number of invalid packages
|
579
621
|
*/
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "node-mavlink",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.8",
|
4
4
|
"author": "Matthias Hryniszak <padcom@gmail.com>",
|
5
5
|
"license": "LGPL",
|
6
6
|
"description": "MavLink definitions and parsing library",
|
@@ -22,7 +22,7 @@
|
|
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"
|