2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2023-2024 José Rebelo
|
2023-10-22 23:41:56 +02:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2023-10-23 19:47:13 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.xiaomi;
|
2023-10-22 23:41:56 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
|
|
|
|
|
|
|
|
public class XiaomiFWHelper {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(XiaomiFWHelper.class);
|
|
|
|
|
|
|
|
private final Uri uri;
|
|
|
|
private byte[] fw;
|
|
|
|
private boolean valid;
|
2023-10-23 19:47:13 +02:00
|
|
|
private boolean typeFirmware;
|
|
|
|
private boolean typeWatchface;
|
2023-10-22 23:41:56 +02:00
|
|
|
|
|
|
|
private String id;
|
|
|
|
private String name;
|
2023-10-23 19:47:13 +02:00
|
|
|
private String version;
|
2023-10-22 23:41:56 +02:00
|
|
|
|
|
|
|
public XiaomiFWHelper(final Uri uri, final Context context) {
|
|
|
|
this.uri = uri;
|
|
|
|
|
|
|
|
final UriHelper uriHelper;
|
|
|
|
try {
|
|
|
|
uriHelper = UriHelper.get(uri, context);
|
|
|
|
} catch (final IOException e) {
|
|
|
|
LOG.error("Failed to get uri helper for {}", uri, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final int maxExpectedFileSize = 1024 * 1024 * 128; // 64MB
|
|
|
|
|
|
|
|
if (uriHelper.getFileSize() > maxExpectedFileSize) {
|
|
|
|
LOG.warn("Firmware size is larger than the maximum expected file size of {}", maxExpectedFileSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try (final InputStream in = new BufferedInputStream(uriHelper.openInputStream())) {
|
|
|
|
this.fw = FileUtils.readAll(in, maxExpectedFileSize);
|
|
|
|
} catch (final IOException e) {
|
|
|
|
LOG.error("Failed to read bytes from {}", uri, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-23 19:47:13 +02:00
|
|
|
parseBytes();
|
2023-10-22 23:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isValid() {
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2023-10-23 19:47:13 +02:00
|
|
|
public boolean isWatchface() {
|
|
|
|
return typeWatchface;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFirmware() {
|
|
|
|
return typeFirmware;
|
|
|
|
}
|
|
|
|
|
2023-10-22 23:41:56 +02:00
|
|
|
public String getDetails() {
|
2023-10-23 19:47:13 +02:00
|
|
|
return name != null ? name : (version != null ? version : "UNKNOWN");
|
2023-10-22 23:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getBytes() {
|
|
|
|
return fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2023-10-23 19:47:13 +02:00
|
|
|
public String getVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2023-10-22 23:41:56 +02:00
|
|
|
public void unsetFwBytes() {
|
|
|
|
this.fw = null;
|
|
|
|
}
|
|
|
|
|
2023-10-23 19:47:13 +02:00
|
|
|
private void parseBytes() {
|
|
|
|
if (parseAsWatchface()) {
|
|
|
|
assert id != null;
|
|
|
|
valid = true;
|
|
|
|
typeWatchface = true;
|
|
|
|
} else if (parseAsFirmware()) {
|
|
|
|
assert version != null;
|
|
|
|
valid = true;
|
|
|
|
typeFirmware = true;
|
|
|
|
} else {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean parseAsWatchface() {
|
2023-10-22 23:41:56 +02:00
|
|
|
if (fw[0] != (byte) 0x5A || fw[1] != (byte) 0xA5) {
|
|
|
|
LOG.warn("File header not a watchface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = StringUtils.untilNullTerminator(fw, 0x28);
|
|
|
|
name = StringUtils.untilNullTerminator(fw, 0x68);
|
|
|
|
|
|
|
|
if (id == null) {
|
|
|
|
LOG.warn("id not found in {}", uri);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == null) {
|
|
|
|
LOG.warn("name not found in {}", uri);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Integer.parseInt(id);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
LOG.warn("Id {} not a number", id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-23 19:47:13 +02:00
|
|
|
|
|
|
|
private boolean parseAsFirmware() {
|
|
|
|
// TODO parse and set version
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-22 23:41:56 +02:00
|
|
|
}
|