Skip to content

Commit

Permalink
Merge pull request #15 from PX4/pr-forwarding
Browse files Browse the repository at this point in the history
mavlink: re-use sequence for forwarded messages
  • Loading branch information
julianoes committed Apr 21, 2021
2 parents 899e70d + 7c73be2 commit c083807
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/me/drton/jmavlib/mavlink/MAVLinkMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MAVLinkMessage {
public final int msgID;
private final byte[] payload;
private final ByteBuffer payloadBB;
private byte sequence = 0;
public byte sequence = 0;
private byte compatFlags = 0; // mavlink 2 only
private byte incompatFlags = 0; // mavlink 2 only
public final int systemID;
Expand All @@ -30,6 +30,7 @@ public class MAVLinkMessage {
private Charset charset = Charset.forName("latin1");
public int protocolVersion = 1;
private boolean signingEnabled = false; // not fully supported, this is only for msg length
public boolean forwarded = false; // message is forwarded and we should not change the sequence number

/**
* Create empty message by message ID (for filling and sending)
Expand Down Expand Up @@ -163,8 +164,7 @@ public MAVLinkMessage(MAVLinkSchema schema, ByteBuffer buffer)
}
}

public ByteBuffer encode(byte sequence) {
this.sequence = sequence;
public ByteBuffer encode() {
ByteBuffer buf = ByteBuffer.allocate(payload.length + getNonPayloadLength());
buf.order(schema.getByteOrder());
if (protocolVersion == 2) {
Expand All @@ -177,7 +177,7 @@ public ByteBuffer encode(byte sequence) {
buf.put((byte) 0); // incompatFlags
buf.put((byte) 0); // compatFlags
}
buf.put(sequence);
buf.put(this.sequence);
buf.put((byte) systemID);
buf.put((byte) componentID);
if (protocolVersion == 2) {
Expand Down
5 changes: 4 additions & 1 deletion src/me/drton/jmavlib/mavlink/MAVLinkStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public void setDebug(boolean debug) {
* @throws IOException on IO error
*/
public void write(MAVLinkMessage msg) throws IOException {
channel.write(msg.encode(txSeq++));
if (!msg.forwarded) {
msg.sequence = txSeq++;
}
channel.write(msg.encode());
}

/**
Expand Down

0 comments on commit c083807

Please sign in to comment.