diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandFWHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandFWHelper.java index 34591cff..6f1b707b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandFWHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandFWHelper.java @@ -67,19 +67,4 @@ public class MiBandFWHelper { } return false; } - - //thanks http://stackoverflow.com/questions/13209364/convert-c-crc16-to-java-crc16 - public int getCRC16(byte[] seq) { - int crc = 0xFFFF; - - for (int j = 0; j < seq.length; j++) { - crc = ((crc >>> 8) | (crc << 8)) & 0xffff; - crc ^= (seq[j] & 0xff);//byte to int, trunc sign - crc ^= ((crc & 0xff) >> 4); - crc ^= (crc << 12) & 0xffff; - crc ^= ((crc & 0xFF) << 5) & 0xffff; - } - crc &= 0xffff; - return crc; - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java index 7332323a..f508f5dd 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java @@ -1,5 +1,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices.miband; +import nodomain.freeyourgadget.gadgetbridge.util.CheckSums; + /** * Created by UgoRaffaele on 30/01/2015. */ @@ -76,7 +78,7 @@ public class UserInfo { byte[] crcSequence = new byte[19]; System.arraycopy(sequence, 0, crcSequence, 0, crcSequence.length); - sequence[19] = (byte) ((getCRC8(crcSequence) ^ Integer.parseInt(address.substring(address.length() - 2), 16)) & 0xff); + sequence[19] = (byte) ((CheckSums.getCRC8(crcSequence) ^ Integer.parseInt(address.substring(address.length() - 2), 16)) & 0xff); this.data = sequence; } @@ -109,24 +111,4 @@ public class UserInfo { public byte[] getData() { return this.data; } - - protected int getCRC8(byte[] seq) { - int len = seq.length; - int i = 0; - byte crc = 0x00; - - while (len-- > 0) { - byte extract = seq[i++]; - for (byte tempI = 8; tempI != 0; tempI--) { - byte sum = (byte) ((crc & 0xff) ^ (extract & 0xff)); - sum = (byte) ((sum & 0xff) & 0x01); - crc = (byte) ((crc & 0xff) >>> 1); - if (sum != 0) { - crc = (byte) ((crc & 0xff) ^ 0x8c); - } - extract = (byte) ((extract & 0xff) >>> 1); - } - } - return (crc & 0xff); - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java index e21df1af..19bfc619 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java @@ -40,6 +40,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.AbortTransactio import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction; +import nodomain.freeyourgadget.gadgetbridge.util.CheckSums; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; @@ -602,7 +603,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { int newFwVersion = mFwHelper.getFirmwareVersion(); int oldFwVersion = mDeviceInfo.getFirmwareVersion(); - int checksum = (Integer.decode("0x" + mMacOctets[4]) << 8 | Integer.decode("0x" + mMacOctets[5])) ^ mFwHelper.getCRC16(mFwHelper.getFw()); + int checksum = (Integer.decode("0x" + mMacOctets[4]) << 8 | Integer.decode("0x" + mMacOctets[5])) ^ CheckSums.getCRC16(mFwHelper.getFw()); if (sendFirmwareInfo(oldFwVersion, newFwVersion, mFwHelper.getFw().length, checksum)) { firmwareInfoSent = true; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CheckSums.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CheckSums.java new file mode 100644 index 00000000..a4c57745 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CheckSums.java @@ -0,0 +1,38 @@ +package nodomain.freeyourgadget.gadgetbridge.util; + +public class CheckSums { + public static int getCRC8(byte[] seq) { + int len = seq.length; + int i = 0; + byte crc = 0x00; + + while (len-- > 0) { + byte extract = seq[i++]; + for (byte tempI = 8; tempI != 0; tempI--) { + byte sum = (byte) ((crc & 0xff) ^ (extract & 0xff)); + sum = (byte) ((sum & 0xff) & 0x01); + crc = (byte) ((crc & 0xff) >>> 1); + if (sum != 0) { + crc = (byte) ((crc & 0xff) ^ 0x8c); + } + extract = (byte) ((extract & 0xff) >>> 1); + } + } + return (crc & 0xff); + } + + //thanks http://stackoverflow.com/questions/13209364/convert-c-crc16-to-java-crc16 + public static int getCRC16(byte[] seq) { + int crc = 0xFFFF; + + for (int j = 0; j < seq.length; j++) { + crc = ((crc >>> 8) | (crc << 8)) & 0xffff; + crc ^= (seq[j] & 0xff);//byte to int, trunc sign + crc ^= ((crc & 0xff) >> 4); + crc ^= (crc << 12) & 0xffff; + crc ^= ((crc & 0xFF) << 5) & 0xffff; + } + crc &= 0xffff; + return crc; + } +}