io.dronefleet.mavlink.protocol.util.CrcX25 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mavlink-protocol Show documentation
Show all versions of mavlink-protocol Show documentation
A library for communication using the Mavlink1 and Mavlink2 protocols
package io.dronefleet.mavlink.protocol.util;
import java.nio.charset.StandardCharsets;
public class CrcX25 {
private int crc;
public CrcX25() {
crc = 0xffff;
}
public void accumulate(String str) {
accumulate(str.getBytes(StandardCharsets.UTF_8));
}
public void accumulate(byte[] bytes) {
accumulate(bytes, 0, bytes.length);
}
public void accumulate(byte[] bytes, int offset, int length) {
for (int i = offset; i < length; i++) {
accumulate(bytes[i]);
}
}
public void accumulate(int b) {
b = b ^ (crc & 0xff);
b ^= (b << 4) & 0xff;
b &= 0xff;
crc = (crc >> 8) ^ (b << 8) ^ (b << 3) ^ (b >> 4);
}
public int get() {
return crc & 0xffff;
}
}