From 66c677bcf94397beccdafa2f94193a22d1f5178c Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Thu, 31 Oct 2019 01:57:23 +0100 Subject: [PATCH] added request --- .../requests/fossil/FilePrepareRequest.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePrepareRequest.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePrepareRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePrepareRequest.java new file mode 100644 index 000000000..ab25e0cee --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePrepareRequest.java @@ -0,0 +1,73 @@ +package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil; + +import android.bluetooth.BluetoothGattCharacteristic; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.UUID; + +import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Request; + +public class FilePrepareRequest extends Request { + private boolean isFinished = false; + private short handle; + + public FilePrepareRequest(short fileHandle) { + this.handle = fileHandle; + ByteBuffer buffer = this.createBuffer(); + buffer.putShort(fileHandle); + + this.data = buffer.array(); + } + + @Override + public void handleResponse(BluetoothGattCharacteristic characteristic) { + super.handleResponse(characteristic); + + if(!characteristic.getUuid().toString().equals(this.getRequestUUID().toString())){ + throw new RuntimeException("wrong response UUID"); + } + + byte[] value = characteristic.getValue(); + + byte type = (byte)(value[0] & 0x0F); + + if(type != 9) throw new RuntimeException("wrong response type"); + + if(value.length != 4) throw new RuntimeException("wrong response length"); + + ByteBuffer buffer = ByteBuffer.wrap(value); + buffer.order(ByteOrder.LITTLE_ENDIAN); + + if(this.handle != buffer.getShort(1)) throw new RuntimeException("wrong response handle"); + + byte status = buffer.get(3); + + if(status != 0) throw new RuntimeException("wrong response status"); + + this.isFinished = true; + + this.onPrepare(); + } + + public void onPrepare(){} + + @Override + public byte[] getStartSequence() { + return new byte[]{9}; + } + + @Override + public int getPayloadLength() { + return 3; + } + + public boolean isFinished(){ + return this.isFinished; + } + + @Override + public UUID getRequestUUID() { + return UUID.fromString("3dda0003-957f-7d4a-34a6-74696673696d"); + } +}