1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Huami2021FirmwareInfo: use ZipFile functions where possible, separate firmware type handling to different functions for readability.

merge conflict fix
This commit is contained in:
MPeter 2022-09-01 15:10:45 +02:00
parent 81ef7698f6
commit e7bd1620fe

View File

@ -29,16 +29,12 @@ import java.util.Arrays;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions; import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.ZipUtils; import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo { public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
private static final Logger LOG = LoggerFactory.getLogger(Huami2021FirmwareInfo.class); private static final Logger LOG = LoggerFactory.getLogger(Huami2021FirmwareInfo.class);
public static final byte[] ZIP_HEADER = new byte[]{
0x50, 0x4B, 0x03, 0x04
};
public Huami2021FirmwareInfo(final byte[] bytes) { public Huami2021FirmwareInfo(final byte[] bytes) {
super(bytes); super(bytes);
} }
@ -56,43 +52,49 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
@Override @Override
protected HuamiFirmwareType determineFirmwareType(final byte[] bytes) { protected HuamiFirmwareType determineFirmwareType(final byte[] bytes) {
if (ArrayUtils.equals(bytes, UIHHContainer.UIHH_HEADER, 0)) { if (ArrayUtils.equals(bytes, UIHHContainer.UIHH_HEADER, 0)) {
final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes); return handleUihhPackage(bytes);
if (uihh == null) { } else if (ZipFile.isZipFile(bytes)) {
LOG.warn("Invalid UIHH file"); return handleZipPackage(bytes);
return HuamiFirmwareType.INVALID; } else {
} return HuamiFirmwareType.INVALID;
}
UIHHContainer.FileEntry uihhFirmwareZipFile = null; }
boolean hasChangelog = false;
for (final UIHHContainer.FileEntry file : uihh.getFiles()) {
switch(file.getType()) {
case FIRMWARE_ZIP:
uihhFirmwareZipFile = file;
continue;
case FIRMWARE_CHANGELOG:
hasChangelog = true;
continue;
default:
LOG.warn("Unexpected file for {}", file.getType());
}
}
if (uihhFirmwareZipFile != null && hasChangelog) {
final byte[] firmwareBin = ZipUtils.getFileFromZip(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
if (isCompatibleFirmwareBin(firmwareBin)) {
return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG;
}
}
private HuamiFirmwareType handleUihhPackage(byte[] bytes) {
final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes);
if (uihh == null) {
LOG.warn("Invalid UIHH file");
return HuamiFirmwareType.INVALID; return HuamiFirmwareType.INVALID;
} }
if (!ArrayUtils.equals(bytes, ZIP_HEADER, 0)) { UIHHContainer.FileEntry uihhFirmwareZipFile = null;
return HuamiFirmwareType.INVALID; boolean hasChangelog = false;
for (final UIHHContainer.FileEntry file : uihh.getFiles()) {
switch(file.getType()) {
case FIRMWARE_ZIP:
uihhFirmwareZipFile = file;
continue;
case FIRMWARE_CHANGELOG:
hasChangelog = true;
continue;
default:
LOG.warn("Unexpected file for {}", file.getType());
}
} }
final byte[] firmwareBin = ZipUtils.getFileFromZip(bytes, "META/firmware.bin"); if (uihhFirmwareZipFile != null && hasChangelog) {
byte[] firmwareBin = ZipFile.tryReadFileQuick(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
if (isCompatibleFirmwareBin(firmwareBin)) {
return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG;
}
}
return HuamiFirmwareType.INVALID;
}
private HuamiFirmwareType handleZipPackage(byte[] bytes) {
final byte[] firmwareBin = ZipFile.tryReadFileQuick(bytes, "META/firmware.bin");
if (isCompatibleFirmwareBin(firmwareBin)) { if (isCompatibleFirmwareBin(firmwareBin)) {
return HuamiFirmwareType.FIRMWARE; return HuamiFirmwareType.FIRMWARE;
} }
@ -191,7 +193,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
} }
public String getFirmwareVersion(final byte[] fwbytes) { public String getFirmwareVersion(final byte[] fwbytes) {
final byte[] firmwareBin = ZipUtils.getFileFromZip(fwbytes, "META/firmware.bin"); final byte[] firmwareBin = ZipFile.tryReadFileQuick(fwbytes, "META/firmware.bin");
if (firmwareBin == null) { if (firmwareBin == null) {
LOG.warn("Failed to read firmware.bin"); LOG.warn("Failed to read firmware.bin");
@ -224,7 +226,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
} }
public String getAppName() { public String getAppName() {
final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json"); final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json");
if (appJsonBin == null) { if (appJsonBin == null) {
LOG.warn("Failed to get app.json from zip"); LOG.warn("Failed to get app.json from zip");
return null; return null;
@ -248,7 +250,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
} }
public String getAppType() { public String getAppType() {
final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json"); final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json");
if (appJsonBin == null) { if (appJsonBin == null) {
LOG.warn("Failed to get app.json from zip"); LOG.warn("Failed to get app.json from zip");
return null; return null;