huawei: fileupload: Use unitSize for file serialization

* unitSize recieved in 28 04 response tag 5 and means content length
in one 28 06 packet
This commit is contained in:
Vitaliy Tomin 2024-04-02 21:57:44 +08:00
parent cd6bc10239
commit 8fd8f2324d
4 changed files with 76 additions and 11 deletions

View File

@ -673,28 +673,26 @@ public class HuaweiPacket {
return retv;
}
public List<byte[]> serializeFileChunk(byte[] fileChunk, int uploadPosition) {
public List<byte[]> serializeFileChunk(byte[] fileChunk, int uploadPosition, short unitSize) {
List<byte[]> retv = new ArrayList<>();
int headerLength = 5; // Magic + (short)(bodyLength + 1) + 0x00
int sliceHeaderLenght =7;
int bodyHeaderLength = 2; // sID + cID
int footerLength = 2; //CRC16
int sliceSize = Math.min(paramsProvider.getSliceSize(), paramsProvider.getMtu()); // at least on magicwatch2 slize size reported bigger than MTU and upload fail
int maxBodySize = sliceSize - headerLength - sliceHeaderLenght - footerLength;
int packetCount = (int) Math.ceil(((double) fileChunk.length ) / (double) maxBodySize);
int packetCount = (int) Math.ceil(((double) fileChunk.length ) / (double) unitSize);
ByteBuffer buffer = ByteBuffer.wrap(fileChunk);
byte fileType = 0x01; //TODO: 1 - watchface, 2 - music
int sliceStart = uploadPosition;
for (int i = 0; i < packetCount; i++) {
short packetSize = (short) Math.min(sliceSize, buffer.remaining() + headerLength + sliceHeaderLenght + footerLength);
short contentSize = (short) Math.min(unitSize, buffer.remaining());
short packetSize = (short)(contentSize + headerLength + sliceHeaderLenght + footerLength);
ByteBuffer packet = ByteBuffer.allocate(packetSize);
short contentSize = (short) (packetSize - headerLength - sliceHeaderLenght - footerLength);
int start = packet.position();
packet.put((byte) 0x5a); // Magic byte
packet.putShort((short) (packetSize - headerLength)); // Length

View File

@ -401,8 +401,19 @@ public class AsynchronousResponse {
}
} else if (response.commandId == FileUpload.FileUploadConsultAck.id) {
if (!(response instanceof FileUpload.FileUploadConsultAck.Response))
throw new Request.ResponseTypeMismatchException(response, FileUpload.FileUploadConsultAck.Response.class);
throw new Request.ResponseTypeMismatchException(response, FileUpload.FileUploadConsultAck.Response.class);
FileUpload.FileUploadConsultAck.Response resp = (FileUpload.FileUploadConsultAck.Response) response;
support.huaweiUploadManager.setFileId(resp.file_id);
support.huaweiUploadManager.setProtocolVersion(resp.protocolVersion);
support.huaweiUploadManager.setAppWaitTime(resp.app_wait_time);
support.huaweiUploadManager.setBitmapEnable(resp.bitmap_enable);
support.huaweiUploadManager.setUnitSize(resp.unit_size);
support.huaweiUploadManager.setMaxApplyDataSize(resp.max_apply_data_size);
support.huaweiUploadManager.setInterval(resp.interval);
support.huaweiUploadManager.setReceivedFileSize(resp.received_file_size);
support.huaweiUploadManager.setNoEncrypt(resp.no_encrypt);
try {
SendFileUploadAck sendFileUploadAck = new SendFileUploadAck(this.support, resp.no_encrypt);
sendFileUploadAck.doPerform();

View File

@ -27,6 +27,21 @@ public class HuaweiUploadManager {
String watchfaceName = "413493857"; //FIXME generate random name
String watchfaceVersion = "1.0.0"; //FIXME generate random version
//ack values set from 28 4 response
byte fileId = 0;
String protocolVersion = "";
short appWaitTime = 0;
byte bitmapEnable = 0;
short unitSize = 0;
int maxApplyDataSize = 0;
short interval =0;
int receivedFileSize =0;
byte noEncrypt = 0;
public HuaweiUploadManager(HuaweiSupportProvider support) {
this.support=support;
}
@ -108,4 +123,44 @@ public class HuaweiUploadManager {
System.arraycopy(watchfaceBin, currentUploadPosition, ret, 0, uploadChunkSize);
return ret;
}
public void setFileId(byte fileId) {
this.fileId = fileId;
}
public void setProtocolVersion(String protocolVersion) {
this.protocolVersion = protocolVersion;
}
public void setInterval(short interval) {
this.interval = interval;
}
public void setMaxApplyDataSize(int maxApplyDataSize) {
this.maxApplyDataSize = maxApplyDataSize;
}
public void setAppWaitTime(short appWaitTime) {
this.appWaitTime = appWaitTime;
}
public void setBitmapEnable(byte bitmapEnable) {
this.bitmapEnable = bitmapEnable;
}
public void setUnitSize(short unitSize) {
this.unitSize = unitSize;
}
public short getUnitSize() {
return unitSize;
}
public void setNoEncrypt(byte noEncrypt) {
this.noEncrypt = noEncrypt;
}
public void setReceivedFileSize(int receivedFileSize) {
this.receivedFileSize = receivedFileSize;
}
}

View File

@ -21,7 +21,8 @@ public class SendFileUploadChunk extends Request {
//FIXME need new package type with raw data chunks ?
return new FileUpload.FileNextChunkSend(this.paramsProvider).serializeFileChunk(
huaweiUploadManager.getCurrentChunk(),
huaweiUploadManager.getCurrentUploadPosition()
huaweiUploadManager.getCurrentUploadPosition(),
huaweiUploadManager.getUnitSize()
);
}