Skip to content

Commit

Permalink
Add parsing of char. Fix infinite loop when parsing messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
trygve55 committed Feb 26, 2021
1 parent 768143b commit b2eb3e0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gardsteinsvik/node-mavlink",
"version": "0.1.6",
"version": "0.1.7",
"description": "A library for parsing and packing MAVLink 2 messages",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
11 changes: 8 additions & 3 deletions src/mavlink-parser-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export abstract class MAVLinkParserBase {

this.buffer = Buffer.concat([this.buffer, bytes]);

while (this.state == ParserState.WaitingForMagicByte && this.buffer.indexOf(this.start_marker) > -1) { // allow parsing multiple messages from a single call
while ((this.state == ParserState.WaitingForMagicByte && this.buffer.indexOf(this.start_marker) > -1) || this.state != ParserState.WaitingForMagicByte) { // allow parsing multiple messages from a single call
if (this.state == ParserState.WaitingForMagicByte) {
// look for the defined magic byte
let message_start = this.buffer.indexOf(this.start_marker);
Expand Down Expand Up @@ -73,10 +73,10 @@ export abstract class MAVLinkParserBase {
const message = this.parseMessage(this.buffer.slice(0, this.expected_packet_length));
if (message) {
messages.push(message);
this.buffer = this.buffer.slice(this.expected_packet_length);
}
this.buffer = this.buffer.slice(this.expected_packet_length);
} catch (e) {
//this.buffer = this.buffer.slice(1);
this.buffer = this.buffer.slice(1);
throw e;
} finally {
// something was complete. Regardless of a complete message, drop the magic byte and restart.
Expand All @@ -85,6 +85,11 @@ export abstract class MAVLinkParserBase {
}
}
}

// Checking if nothing more can be done
if (this.state == ParserState.WaitingForHeaderComplete || this.state == ParserState.WaitingForMessageComplete) {
break;
}
}

if (this.state == ParserState.WaitingForMagicByte) {
Expand Down
7 changes: 6 additions & 1 deletion src/mavlink-v1/mavlink-parser-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ export class MAVLinkParserV1 extends MAVLinkParserBase {
case "double":
return bytes.readDoubleLE(start);
case "char":
return bytes.toString('ascii', start, 1);
let nullTerminator = bytes.indexOf(0x00, start)
if (nullTerminator == -1) {
return bytes.toString('ascii', start);
} else {
return bytes.toString('ascii', start, nullTerminator);
}
}
}
}
7 changes: 6 additions & 1 deletion src/mavlink-v2/mavlink-parser-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ export class MAVLinkParserV2 extends MAVLinkParserBase {
case "double":
return bytes.readDoubleLE(start);
case "char":
return bytes.toString('ascii', start, 1);
let nullTerminator = bytes.indexOf(0x00, start)
if (nullTerminator == -1) {
return bytes.toString('ascii', start);
} else {
return bytes.toString('ascii', start, nullTerminator);
}
}
}
}

0 comments on commit b2eb3e0

Please sign in to comment.