2015-08-03 23:51:53 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband;
|
2015-04-19 22:20:47 +02:00
|
|
|
|
2015-05-01 01:26:12 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-10-18 20:54:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
|
|
|
|
|
|
|
|
import java.util.Locale;
|
2015-05-01 01:26:12 +02:00
|
|
|
|
2015-04-19 22:20:47 +02:00
|
|
|
public class DeviceInfo extends AbstractInfo {
|
2015-10-18 20:54:22 +02:00
|
|
|
public final String deviceId;
|
|
|
|
public final int profileVersion;
|
|
|
|
public final int fwVersion;
|
|
|
|
public final int hwVersion;
|
|
|
|
public final int feature;
|
|
|
|
public final int appearance;
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isChecksumCorrect(byte[] data) {
|
|
|
|
int crc8 = CheckSums.getCRC8(new byte[]{data[0], data[1], data[2], data[3], data[4], data[5], data[6]});
|
|
|
|
return data[7] == (crc8 ^ data[3] & 255);
|
|
|
|
}
|
|
|
|
|
2015-04-19 22:20:47 +02:00
|
|
|
public DeviceInfo(byte[] data) {
|
|
|
|
super(data);
|
2015-10-18 20:54:22 +02:00
|
|
|
|
|
|
|
if ((data.length == 16 || data.length == 20) && isChecksumCorrect(data)) {
|
|
|
|
deviceId = String.format("%02X%02X%02X%02X%02X%02X%02X%02X", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
|
|
|
|
profileVersion = getInt(data, 8);
|
|
|
|
fwVersion = getInt(data, 12);
|
|
|
|
hwVersion = Integer.decode("0x" + deviceId.substring(12, 14)).intValue();
|
|
|
|
feature = Integer.decode("0x" + deviceId.substring(8, 10)).intValue();
|
|
|
|
appearance = Integer.decode("0x" + deviceId.substring(10, 12)).intValue();
|
|
|
|
} else {
|
|
|
|
deviceId = "crc error";
|
|
|
|
profileVersion = -1;
|
|
|
|
fwVersion = -1;
|
|
|
|
hwVersion = -1;
|
|
|
|
feature = -1;
|
|
|
|
appearance = -1;
|
|
|
|
}
|
2015-04-19 22:20:47 +02:00
|
|
|
}
|
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
public static int getInt(byte[] data, int from, int len) {
|
|
|
|
int ret = 0;
|
|
|
|
for(int i = 0; i < len; ++i) {
|
|
|
|
ret |= (data[from + i] & 255) << i * 8;
|
2015-04-19 22:20:47 +02:00
|
|
|
}
|
2015-10-18 20:54:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getInt(byte[] data, int from) {
|
|
|
|
return getInt(data, from, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHumanFirmwareVersion() {
|
|
|
|
if (fwVersion == -1)
|
|
|
|
return GBApplication.getContext().getString(R.string._unknown_);
|
|
|
|
|
|
|
|
return String.format(Locale.US, "%d.%d.%d.%d",
|
|
|
|
fwVersion >> 24 & 255,
|
|
|
|
fwVersion >> 16 & 255,
|
|
|
|
fwVersion >> 8 & 255,
|
|
|
|
fwVersion & 255);
|
2015-04-19 22:20:47 +02:00
|
|
|
}
|
2015-07-23 17:14:51 +02:00
|
|
|
|
|
|
|
public int getFirmwareVersion() {
|
2015-10-18 20:54:22 +02:00
|
|
|
return fwVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "DeviceInfo{" +
|
|
|
|
"deviceId='" + deviceId + '\'' +
|
|
|
|
", profileVersion=" + profileVersion +
|
|
|
|
", fwVersion=" + fwVersion +
|
|
|
|
", hwVersion=" + hwVersion +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isMili1A() {
|
|
|
|
return (this.feature & 255) == 5 && (this.appearance & 255) == 0 || (this.feature & 255) == 0 && (this.hwVersion & 255) == 208;
|
2015-07-23 17:14:51 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 22:20:47 +02:00
|
|
|
}
|