huawei: file upload: SendFileUploadComplete 28 07

* refactoring for AsynchronousResponse - use single method to handle
all FilUpload related packets
This commit is contained in:
Vitaliy Tomin 2024-04-01 16:35:09 +08:00
parent 2d915e7346
commit 68e81a4887
4 changed files with 104 additions and 44 deletions

View File

@ -68,7 +68,7 @@ public class FileUpload {
}
public static class FileuploadConsultAck {
public static class FileUploadConsultAck {
public static final byte id = 0x04;
public static class Request extends HuaweiPacket {
public Request(ParamsProvider paramsProvider) {
@ -122,4 +122,38 @@ public class FileUpload {
}
}
public static class FileUploadResult {
public static final byte id = 0x07;
public static class Request extends HuaweiPacket {
public Request(ParamsProvider paramsProvider) {
super(paramsProvider);
this.serviceId = FileUpload.id;
this.commandId = id;
this.tlv = new HuaweiTLV()
.put(0x7f, 0x000186A0) //ok
.put(0x01, (byte) 0x01); // filetype 1 - watchface, 2 -music
this.complete = true;
}
}
public static class Response extends HuaweiPacket {
byte status = 0;
public Response (ParamsProvider paramsProvider) {
super(paramsProvider);
}
@Override
public void parseTlv() throws HuaweiPacket.ParseException {
if (this.tlv.contains(0x02) && this.tlv.getBytes(0x02).length == 1)
this.status = this.tlv.getByte(0x02);
}
}
}
}

View File

@ -52,6 +52,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.Request;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.GetPhoneInfoRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadComplete;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendMenstrualModifyTimeRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadAck;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadChunk;
@ -104,9 +105,7 @@ public class AsynchronousResponse {
handleMenstrualModifyTime(response);
handleWeatherCheck(response);
handleGpsRequest(response);
handleWatchfaceHash(response);
handleWatchfaceAck(response);
handeWatchfaceNextChunk(response);
handleFileUpload(response);
} catch (Request.ResponseParseException e) {
LOG.error("Response parse exception", e);
}
@ -391,47 +390,46 @@ public class AsynchronousResponse {
}
}
private void handleWatchfaceHash(HuaweiPacket response) {
if (response.serviceId == FileUpload.id && response.commandId == FileUpload.FileHashSend.id) {
try {
SendFileUploadHash sendFileUploadHash = new SendFileUploadHash(this.support, this.support.huaweiUploadManager);
sendFileUploadHash.doPerform();
} catch (IOException e) {
LOG.error("Could not send watchface hash request", e);
}
private void handleFileUpload(HuaweiPacket response) throws Request.ResponseParseException {
if (response.serviceId == FileUpload.id) {
if (response.commandId == FileUpload.FileHashSend.id) {
try {
SendFileUploadHash sendFileUploadHash = new SendFileUploadHash(this.support, this.support.huaweiUploadManager);
sendFileUploadHash.doPerform();
} catch (IOException e) {
LOG.error("Could not send fileupload hash request", e);
}
} else if (response.commandId == FileUpload.FileUploadConsultAck.id) {
try {
SendFileUploadAck sendFileUploadAck = new SendFileUploadAck(this.support);
sendFileUploadAck.doPerform();
} catch (IOException e) {
LOG.error("Could not send fileupload ack request", e);
}
} else if (response.commandId == FileUpload.FileNextChunkParams.id) {
if (!(response instanceof FileUpload.FileNextChunkParams))
throw new Request.ResponseTypeMismatchException(response, FileUpload.FileNextChunkParams.class);
FileUpload.FileNextChunkParams resp = (FileUpload.FileNextChunkParams) response;
support.huaweiUploadManager.setUploadChunkSize(resp.nextchunkSize);
support.huaweiUploadManager.setCurrentUploadPosition(resp.bytesUploaded);
try {
SendFileUploadChunk sendFileUploadChunk = new SendFileUploadChunk(this.support, this.support.huaweiUploadManager);
sendFileUploadChunk.doPerform();
} catch (IOException e) {
LOG.error("Could not send fileupload next chunk request", e);
}
} else if (response.commandId == FileUpload.FileUploadResult.id) {
try {
SendFileUploadComplete sendFileUploadComplete = new SendFileUploadComplete(this.support);
sendFileUploadComplete.doPerform();
} catch (IOException e) {
LOG.error("Could not send fileupload result request", e);
}
}
}
}
private void handleWatchfaceAck(HuaweiPacket response) {
if (response.serviceId == FileUpload.id && response.commandId == FileUpload.FileuploadConsultAck.id) {
try {
SendFileUploadAck sendFileUploadAck = new SendFileUploadAck(this.support);
sendFileUploadAck.doPerform();
} catch (IOException e) {
LOG.error("Could not send watchface ack request", e);
}
}
}
//FIXME: implement sliced raw serialization and add handeWatchfaceNextChunk to handleResponse list
private void handeWatchfaceNextChunk(HuaweiPacket response) throws Request.ResponseParseException {
if (response.serviceId == FileUpload.id && response.commandId == FileUpload.FileNextChunkParams.id) {
if (!(response instanceof FileUpload.FileNextChunkParams))
throw new Request.ResponseTypeMismatchException(response, FileUpload.FileNextChunkParams.class);
FileUpload.FileNextChunkParams resp = (FileUpload.FileNextChunkParams) response;
support.huaweiUploadManager.setUploadChunkSize(resp.nextchunkSize);
support.huaweiUploadManager.setCurrentUploadPosition(resp.bytesUploaded);
try {
SendFileUploadChunk sendFileUploadChunk = new SendFileUploadChunk(this.support, this.support.huaweiUploadManager);
sendFileUploadChunk.doPerform();
} catch (IOException e) {
LOG.error("Could not send watchface next chunk request", e);
}
}
}
private void handleWeatherCheck(HuaweiPacket response) {
if (response.serviceId == Weather.id && response.commandId == 0x04) {
// Send back ok

View File

@ -10,13 +10,13 @@ public class SendFileUploadAck extends Request {
public SendFileUploadAck(HuaweiSupportProvider support) {
super(support);
this.serviceId = FileUpload.id;
this.commandId = FileUpload.FileuploadConsultAck.id;
this.commandId = FileUpload.FileUploadConsultAck.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
return new FileUpload.FileuploadConsultAck.Request(this.paramsProvider ).serialize();
return new FileUpload.FileUploadConsultAck.Request(this.paramsProvider ).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
}

View File

@ -0,0 +1,28 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
public class SendFileUploadComplete extends Request {
public SendFileUploadComplete(HuaweiSupportProvider support) {
super(support);
this.serviceId = FileUpload.id;
this.commandId = FileUpload.FileUploadResult.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
return new FileUpload.FileUploadResult.Request(this.paramsProvider).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
}
}
}