From d23c931f95694be12ace53fe0cbed8c3c9b0f2b9 Mon Sep 17 00:00:00 2001 From: Me7c7 Date: Sun, 20 Oct 2024 10:28:59 +0300 Subject: [PATCH] Huawei: device specific fixes. Watch 3 for example --- .../devices/huawei/HuaweiInstallHandler.java | 2 +- .../devices/huawei/HuaweiMusicUtils.java | 10 +-- .../devices/huawei/HuaweiPacket.java | 75 ++++--------------- .../devices/huawei/HuaweiTLV.java | 10 +++ .../devices/huawei/packets/FileUpload.java | 20 ++--- .../devices/huawei/packets/MusicControl.java | 15 ++-- .../devices/huawei/HuaweiUploadManager.java | 2 +- 7 files changed, 47 insertions(+), 87 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiInstallHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiInstallHandler.java index cfb038c77..dc5e4fb9b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiInstallHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiInstallHandler.java @@ -95,7 +95,7 @@ public class HuaweiInstallHandler implements InstallHandler { LOG.info("unknownBitrate {}", restrictions.unknownBitrate); if(currentMusicInfo.getChannels() > restrictions.channels) { - LOG.error("Not supported channels couunt {} > {}", currentMusicInfo.getChannels(), restrictions.channels); + LOG.error("Not supported channels count {} > {}", currentMusicInfo.getChannels(), restrictions.channels); return false; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiMusicUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiMusicUtils.java index f4c9770f0..2b8f2c028 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiMusicUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiMusicUtils.java @@ -60,12 +60,12 @@ public class HuaweiMusicUtils { } public static class MusicCapabilities { - public short availableSpace = 0; + public int availableSpace = 0; public List supportedFormats = null; - public short maxMusicCount = 0; - public short maxPlaylistCount = 0; - public short currentMusicCount = 0; // TODO: not sure - public byte unknown = 0; // TODO: not sure + public int maxMusicCount = 0; + public int maxPlaylistCount = 0; + public int currentMusicCount = 0; // TODO: not sure + public int unknown = 0; // TODO: not sure public List formatsRestrictions = null; public List pageStruct = null; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java index 95314647e..e653a2c5d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java @@ -827,12 +827,10 @@ public class HuaweiPacket { return retv; } - public List serializeFileChunk(byte[] fileChunk, int uploadPosition, short unitSize, byte fileId, boolean isEncrypted) throws SerializeException { + public List serializeFileChunk(byte[] fileChunk, int uploadPosition, int unitSize, byte fileId, boolean isEncrypted) throws SerializeException { List retv = new ArrayList<>(); - int headerLength = 5; // Magic + (short)(bodyLength + 1) + 0x00 - int sliceHeaderLength = 6; - - int footerLength = 2; //CRC16 + final int sliceHeaderLength = 6; + final int packageHeaderAndFooterLength = 6; int packetCount = (int) Math.ceil(((double) fileChunk.length) / (double) unitSize); @@ -842,7 +840,7 @@ public class HuaweiPacket { for (int i = 0; i < packetCount; i++) { - short contentSize = (short) Math.min(unitSize, buffer.remaining()); + int contentSize = Math.min(unitSize, buffer.remaining()); ByteBuffer payload = ByteBuffer.allocate(contentSize + sliceHeaderLength); payload.put(fileId); // Slice @@ -870,45 +868,23 @@ public class HuaweiPacket { throw new HuaweiPacket.SerializeException("new payload is null"); } - short packetSize = (short)(new_payload.length + sliceHeaderLength + footerLength); - ByteBuffer packet = ByteBuffer.allocate(packetSize); - - int start = packet.position(); - packet.put((byte) 0x5a); // Magic byte - packet.putShort((short) (packetSize - headerLength)); // Length - - packet.put((byte) 0x00); - packet.put(this.serviceId); - packet.put(this.commandId); - - packet.put(new_payload); - - int length = packet.position() - start; - if (length != packetSize - footerLength) { - throw new HuaweiPacket.SerializeException(String.format(GBApplication.getLanguage(), "Packet lengths don't match! %d != %d", length, packetSize + headerLength)); + if ((new_payload.length + packageHeaderAndFooterLength) > paramsProvider.getSliceSize()) { + retv.addAll(serializeSliced(new_payload)); + } else { + retv.addAll(serializeUnsliced(new_payload)); } - byte[] complete = new byte[length]; - packet.position(start); - packet.get(complete, 0, length); - int crc16 = CheckSums.getCRC16(complete, 0x0000); - - packet.putShort((short) crc16); // CRC16 - sliceStart += contentSize; - retv.add(packet.array()); } return retv; } public List serializeFileChunk1c(byte[] fileChunk, short transferSize, int packetCount) throws SerializeException { List retv = new ArrayList<>(); - int headerLength = 4; // Magic + (short)(bodyLength + 1) + 0x00 - int bodyHeaderLength = 2; // sID + cID - int footerLength = 2; //CRC16 - int subHeaderLength = 1; + final int subHeaderLength = 1; + final int packageHeaderAndFooterLength = 6; ByteBuffer buffer = ByteBuffer.wrap(fileChunk); @@ -925,34 +901,11 @@ public class HuaweiPacket { byte[] new_payload = payload.array(); - int bodyLength = bodyHeaderLength + new_payload.length; - - short packetSize = (short)(headerLength + bodyLength + footerLength); - ByteBuffer packet = ByteBuffer.allocate(packetSize); - - int start = packet.position(); - packet.put((byte) 0x5a); // Magic byte - packet.putShort((short) (bodyLength + 1)); // Length - - packet.put((byte) 0x00); - packet.put(this.serviceId); - packet.put(this.commandId); - - packet.put(new_payload); - - int length = packet.position() - start; - if (length != packetSize - footerLength) { - throw new HuaweiPacket.SerializeException(String.format(GBApplication.getLanguage(), "Packet lengths don't match! %d != %d", length, packetSize + headerLength)); + if ((new_payload.length + packageHeaderAndFooterLength) > paramsProvider.getSliceSize()) { + retv.addAll(serializeSliced(new_payload)); + } else { + retv.addAll(serializeUnsliced(new_payload)); } - - byte[] complete = new byte[length]; - packet.position(start); - packet.get(complete, 0, length); - int crc16 = CheckSums.getCRC16(complete, 0x0000); - - packet.putShort((short) crc16); // CRC16 - - retv.add(packet.array()); } return retv; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java index c56cb157d..9edcf7412 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java @@ -243,6 +243,16 @@ public class HuaweiTLV { return ByteBuffer.wrap(getBytes(tag)).getShort(); } + public Integer getAsInteger(int tag) throws HuaweiPacket.MissingTagException { + byte[] bytes = getBytes(tag); + if(bytes.length == 1) { + return bytes[0] & 0xFF; + } else if(bytes.length == 2) { + return ByteBuffer.wrap(getBytes(tag)).getShort() & 0xFFFF; + } + return ByteBuffer.wrap(getBytes(tag)).getInt(); + } + public String getString(int tag) throws HuaweiPacket.MissingTagException { return new String(getBytes(tag), StandardCharsets.UTF_8); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileUpload.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileUpload.java index ab9ecc053..b8eae703f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileUpload.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileUpload.java @@ -26,11 +26,11 @@ public class FileUpload { public static class FileUploadParams { public byte file_id = 0; public String protocolVersion = ""; - public short app_wait_time = 0; - public byte bitmap_enable = 0; - public short unit_size = 0; + public int app_wait_time = 0; + public int bitmap_enable = 0; + public int unit_size = 0; public int max_apply_data_size = 0; - public short interval = 0; + public int interval = 0; public int received_file_size = 0; public byte no_encrypt = 0; } @@ -65,7 +65,7 @@ public class FileUpload { this.tlv = new HuaweiTLV() .put(0x01, fileName) .put(0x02, fileSize) - .put(0x03, (byte) fileType); + .put(0x03, fileType); if (fileType == Filetype.watchface) { String watchfaceName = fileName.split("_")[0]; @@ -164,11 +164,11 @@ public class FileUpload { public void parseTlv() throws HuaweiPacket.ParseException { this.fileUploadParams.file_id = this.tlv.getByte(0x01); this.fileUploadParams.protocolVersion = this.tlv.getString(0x02); - this.fileUploadParams.app_wait_time = this.tlv.getShort(0x03); - this.fileUploadParams.bitmap_enable = this.tlv.getByte(0x04); - this.fileUploadParams.unit_size = this.tlv.getShort(0x05); - this.fileUploadParams.max_apply_data_size = this.tlv.getInteger(0x06); - this.fileUploadParams.interval = this.tlv.getShort(0x07); + this.fileUploadParams.app_wait_time = this.tlv.getAsInteger(0x03); + this.fileUploadParams.bitmap_enable = this.tlv.getAsInteger(0x04); + this.fileUploadParams.unit_size = this.tlv.getAsInteger(0x05); + this.fileUploadParams.max_apply_data_size = this.tlv.getAsInteger(0x06); + this.fileUploadParams.interval = this.tlv.getAsInteger(0x07); this.fileUploadParams.received_file_size = this.tlv.getInteger(0x08); if (this.tlv.contains(0x09)) // optional for older devices this.fileUploadParams.no_encrypt = this.tlv.getByte(0x09); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/MusicControl.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/MusicControl.java index 24ce8fbd8..3509d71ab 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/MusicControl.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/MusicControl.java @@ -18,9 +18,6 @@ package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets; import static nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiMusicUtils.parseFormatBits; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.List; @@ -266,15 +263,15 @@ public class MusicControl { // LOG.info("Unknown: " + this.tlv.getShort(0x01)); if (this.tlv.contains(0x02)) - params.availableSpace = this.tlv.getShort(0x02); + params.availableSpace = this.tlv.getAsInteger(0x02); if (this.tlv.contains(0x03)) { byte[] formatBits = this.tlv.getBytes(0x03); params.supportedFormats = parseFormatBits(formatBits); } if (this.tlv.contains(0x04)) - params.maxMusicCount = this.tlv.getShort(0x04); + params.maxMusicCount = this.tlv.getAsInteger(0x04); if (this.tlv.contains(0x05)) - params.currentMusicCount = this.tlv.getByte(0x05); + params.currentMusicCount = this.tlv.getAsInteger(0x05); if (this.tlv.contains(0x86)) { params.pageStruct = new ArrayList<>(); @@ -327,15 +324,15 @@ public class MusicControl { @Override public void parseTlv() throws ParseException { if (this.tlv.contains(0x01)) - params.availableSpace = this.tlv.getShort(0x01); + params.availableSpace = this.tlv.getAsInteger(0x01); if (this.tlv.contains(0x02)) { byte[] formatBits = this.tlv.getBytes(0x02); params.supportedFormats = parseFormatBits(formatBits); } if (this.tlv.contains(0x03)) - params.maxMusicCount = this.tlv.getShort(0x03); + params.maxMusicCount = this.tlv.getAsInteger(0x03); if (this.tlv.contains(0x04)) - params.maxPlaylistCount = this.tlv.getShort(0x04); + params.maxPlaylistCount = this.tlv.getAsInteger(0x04); if (this.tlv.contains(0x05)) params.unknown = this.tlv.getByte(0x05); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiUploadManager.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiUploadManager.java index 2e98ba2fe..384901054 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiUploadManager.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiUploadManager.java @@ -75,7 +75,7 @@ public class HuaweiUploadManager { this.fileUploadParams = params; } - public short getUnitSize() { + public int getUnitSize() { return fileUploadParams.unit_size; }