2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
|
2015-07-23 17:14:51 +02:00
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
2015-08-04 23:02:36 +02:00
|
|
|
import java.io.IOException;
|
2015-07-23 17:14:51 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2015-08-04 23:02:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
|
|
|
|
2015-07-23 17:14:51 +02:00
|
|
|
public class MiBandFWHelper {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MiBandFWHelper.class);
|
|
|
|
|
|
|
|
private final Uri uri;
|
|
|
|
private final ContentResolver cr;
|
|
|
|
private byte[] fw;
|
|
|
|
|
2015-07-28 17:30:20 +02:00
|
|
|
private final int firmwareVersionBuild = 1056;
|
2015-07-23 17:14:51 +02:00
|
|
|
private final int firmwareVersionRevision = 1057;
|
2015-07-28 17:30:20 +02:00
|
|
|
private final int firmwareVersionMinor = 1058;
|
|
|
|
private final int firmwareVersionMajor = 1059;
|
2015-07-23 17:14:51 +02:00
|
|
|
|
2015-10-22 00:53:27 +02:00
|
|
|
/**
|
|
|
|
* Provides a different notification API which is also used on Mi1A devices.
|
|
|
|
*/
|
|
|
|
public static final int FW_16779790 = 16779790;
|
|
|
|
|
2015-07-23 17:14:51 +02:00
|
|
|
private final int[] whitelistedFirmwareVersion = {
|
2015-07-28 17:30:20 +02:00
|
|
|
16779534, // 1.0.9.14 tested by developer
|
2015-07-28 22:03:53 +02:00
|
|
|
16779547, //1.0.9.27 tested by developer
|
2015-08-30 00:21:51 +02:00
|
|
|
16779568, //1.0.9.48 tested by developer
|
|
|
|
16779585, //1.0.9.65 tested by developer
|
2015-09-17 16:03:15 +02:00
|
|
|
16779779, //1.0.10.3 reported on the wiki
|
|
|
|
16779782, //1.0.10.6 reported on the wikiew
|
|
|
|
16779787, //1.0.10.11 tested by developer
|
2015-10-22 00:53:27 +02:00
|
|
|
//FW_16779790, //1.0.10.14 reported on the wiki (vibration does not work currently)
|
2015-07-23 17:14:51 +02:00
|
|
|
};
|
|
|
|
|
2015-08-04 23:02:36 +02:00
|
|
|
public MiBandFWHelper(Uri uri, Context context) throws IOException {
|
2015-07-23 17:14:51 +02:00
|
|
|
this.uri = uri;
|
|
|
|
cr = context.getContentResolver();
|
2015-11-23 22:46:12 +01:00
|
|
|
if (cr == null) {
|
|
|
|
throw new IOException("No content resolver");
|
|
|
|
}
|
2015-07-23 17:14:51 +02:00
|
|
|
|
2015-10-06 16:56:01 +02:00
|
|
|
String pebblePattern = ".*\\.(pbw|pbz|pbl)";
|
2015-08-31 17:40:46 +02:00
|
|
|
|
|
|
|
if (uri.getPath().matches(pebblePattern)) {
|
|
|
|
throw new IOException("Firmware has a filename that looks like a Pebble app/firmware.");
|
|
|
|
}
|
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
try (InputStream in = new BufferedInputStream(cr.openInputStream(uri))) {
|
2015-08-04 23:02:36 +02:00
|
|
|
this.fw = FileUtils.readAll(in, 1024 * 1024); // 1 MB
|
2015-08-04 23:08:40 +02:00
|
|
|
if (fw.length <= firmwareVersionMajor || fw[firmwareVersionMajor] != 1) {
|
2015-08-04 23:02:36 +02:00
|
|
|
throw new IOException("Firmware major version should be 1, probably this isn't a MiBand firmware.");
|
|
|
|
}
|
2015-07-23 17:14:51 +02:00
|
|
|
} catch (Exception e) {
|
2015-08-04 23:02:36 +02:00
|
|
|
throw new IOException("Error reading firmware file: " + uri.toString(), e);
|
2015-07-23 17:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getFirmwareVersion() {
|
|
|
|
return (fw[firmwareVersionMajor] << 24) | (fw[firmwareVersionMinor] << 16) | (fw[firmwareVersionRevision] << 8) | fw[firmwareVersionBuild];
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHumanFirmwareVersion() {
|
|
|
|
return String.format(Locale.US, "%d.%d.%d.%d", fw[firmwareVersionMajor], fw[firmwareVersionMinor], fw[firmwareVersionRevision], fw[firmwareVersionBuild]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getFw() {
|
|
|
|
return fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFirmwareWhitelisted() {
|
|
|
|
for (int wlf : whitelistedFirmwareVersion) {
|
|
|
|
if (wlf == getFirmwareVersion()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-28 17:30:20 +02:00
|
|
|
}
|