Huawei: watchface upload: fix parse WatchfaceNextChunkParams

* prepare for HuaweiPacket.serializeFileChunk()
This commit is contained in:
Vitaliy Tomin 2024-03-31 23:09:02 +08:00
parent 00af1c6895
commit 0229a24dc6
5 changed files with 38 additions and 9 deletions

View File

@ -40,6 +40,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FindPhone;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FitnessData;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.MusicControl;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
public class HuaweiPacket {
@ -539,6 +540,14 @@ public class HuaweiPacket {
this.isEncrypted = this.attemptDecrypt(); // Helps with debugging
return this;
}
case WatchfaceUpload.id:
switch(this.commandId) {
case WatchfaceUpload.WatchfaceNextChunkParams.id:
return new WatchfaceUpload.WatchfaceNextChunkParams(paramsProvider).fromPacket(this);
default:
this.isEncrypted = this.attemptDecrypt(); // Helps with debugging
return this;
}
default:
this.isEncrypted = this.attemptDecrypt(); // Helps with debugging
return this;
@ -662,6 +671,19 @@ public class HuaweiPacket {
return retv;
}
public List<byte[]> serializeFileChunk(byte[] fileChunk) {
List<byte[]> retv = new ArrayList<>();
int headerLength = 4; // Magic + (short)(bodyLength + 1) + 0x00
int bodyHeaderLength = 2; // sID + cID
int footerLength = 2; //CRC16
int maxBodySize = paramsProvider.getSliceSize() - headerLength - footerLength;
int packetCount = (int) Math.ceil(((double) fileChunk.length + (double) bodyHeaderLength) / (double) maxBodySize);
for (int i = 0; i < packetCount; i++) {
}
return retv;
}
public List<byte[]> serialize() throws CryptoException {
// TODO: necessary for this to work:
// - serviceId

View File

@ -73,6 +73,8 @@ public class WatchfaceUpload {
public static class Request extends HuaweiPacket {
public Request(ParamsProvider paramsProvider) {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.commandId = id;
this.tlv = new HuaweiTLV()
.put(0x7f, 0x000186A0) //ok
.put(0x01, (byte) 0x01)
@ -97,7 +99,7 @@ public class WatchfaceUpload {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.commandId = id;
this.complete = true;
}
@Override
public void parseTlv() throws HuaweiPacket.ParseException {
@ -116,6 +118,7 @@ public class WatchfaceUpload {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.commandId = id;
this.complete = true;
}
}

View File

@ -106,6 +106,7 @@ public class AsynchronousResponse {
handleGpsRequest(response);
handleWatchfaceHash(response);
handleWatchfaceAck(response);
handeWatchfaceNextChunk(response);
} catch (Request.ResponseParseException e) {
LOG.error("Response parse exception", e);
}

View File

@ -23,7 +23,7 @@ public class HuaweiWatchfaceManager {
int fileSize = 0;
int currentUploadPosition = 0;
int uploudChunkSize =0;
int uploadChunkSize =0;
String watchfaceName = "413493857"; //FIXME generate random name
String watchfaceVersion = "1.0.0"; //FIXME generate random version
@ -69,7 +69,7 @@ public class HuaweiWatchfaceManager {
}
currentUploadPosition = 0;
uploudChunkSize = 0;
uploadChunkSize = 0;
//TODO: generate random watchfaceName and watchfaceVersion
LOG.info("watchface loaded, SHA256: "+ GB.hexdump(watchfaceSHA256) + " watchfaceName: " + watchfaceName + " watchfaceVersion: "+watchfaceVersion);
@ -92,10 +92,16 @@ public class HuaweiWatchfaceManager {
}
public void setUploadChunkSize(int chunkSize) {
uploudChunkSize = chunkSize;
uploadChunkSize = chunkSize;
}
public void setCurrentUploadPosition (int pos) {
currentUploadPosition = pos;
}
public byte[] getCurrentChunk() {
byte[] ret = new byte[uploadChunkSize];
System.arraycopy(watchfaceBin, currentUploadPosition, ret, 0, uploadChunkSize);
return ret;
}
}

View File

@ -19,12 +19,9 @@ public class SendWatchfaceChunk extends Request {
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
//FIXME need new package type with raw data chunks ?
return new WatchfaceUpload.WatchfaceSendNextChunk(this.paramsProvider).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
}
return new WatchfaceUpload.WatchfaceSendNextChunk(this.paramsProvider).serializeFileChunk(huaweiWatchfaceManager.getCurrentChunk());
}
}