code cleanup and crc handling

This commit is contained in:
Daniel Dakhno
2019-11-16 03:02:29 +01:00
parent 56d5b95181
commit 1bdaa55937
4 changed files with 97 additions and 89 deletions

View File

@@ -53,13 +53,13 @@ public class FossilWatchAdapter extends WatchAdapter {
public void initialize() { public void initialize() {
playPairingAnimation(); playPairingAnimation();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
queueWrite(new RequestMtuRequest(512)); queueWrite(new RequestMtuRequest(512), false);
} }
queueWrite(new ConfigurationGetRequest(this)); queueWrite(new ConfigurationGetRequest(this), false);
syncNotificationSettings(); syncNotificationSettings();
queueWrite(new SetDeviceStateRequest(GBDevice.State.INITIALIZED)); queueWrite(new SetDeviceStateRequest(GBDevice.State.INITIALIZED), false);
} }
public int getMTU() { public int getMTU() {
@@ -70,7 +70,7 @@ public class FossilWatchAdapter extends WatchAdapter {
@Override @Override
public void playPairingAnimation() { public void playPairingAnimation() {
queueWrite(new AnimationRequest()); queueWrite(new AnimationRequest(), false);
} }
@Override @Override
@@ -79,7 +79,7 @@ public class FossilWatchAdapter extends WatchAdapter {
log("package name in notification not set"); log("package name in notification not set");
return; return;
} }
queueWrite(new PlayNotificationRequest(config.getPackageName(), this)); queueWrite(new PlayNotificationRequest(config.getPackageName(), this), false);
} }
@Override @Override
@@ -94,7 +94,7 @@ public class FossilWatchAdapter extends WatchAdapter {
(short) (millis % 1000), (short) (millis % 1000),
(short) ((zone.getRawOffset() + (zone.inDaylightTime(new Date()) ? 1 : 0)) / 60000) (short) ((zone.getRawOffset() + (zone.inDaylightTime(new Date()) ? 1 : 0)) / 60000)
), ),
this) this), false
); );
} }
@@ -107,7 +107,7 @@ public class FossilWatchAdapter extends WatchAdapter {
(byte) 0x08, (byte) 0x01, (byte) 0x14, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0xFE, (byte) 0x08, (byte) 0x00, (byte) 0x93, (byte) 0x00, (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0xBF, (byte) 0xD5, (byte) 0x54, (byte) 0xD1, (byte) 0x08, (byte) 0x01, (byte) 0x14, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0xFE, (byte) 0x08, (byte) 0x00, (byte) 0x93, (byte) 0x00, (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0xBF, (byte) 0xD5, (byte) 0x54, (byte) 0xD1,
(byte) 0x00 (byte) 0x00
}, this); }, this);
queueWrite(uploadFileRequest); queueWrite(uploadFileRequest, false);
} }
@Override @Override
@@ -115,12 +115,12 @@ public class FossilWatchAdapter extends WatchAdapter {
queueWrite(new ConfigurationPutRequest( queueWrite(new ConfigurationPutRequest(
new ConfigurationPutRequest.CurrentStepCountConfigItem(Math.min(999999, (int) (1000000 * progress))), new ConfigurationPutRequest.CurrentStepCountConfigItem(Math.min(999999, (int) (1000000 * progress))),
this this
)); ), false);
} }
@Override @Override
public void setHands(short hour, short minute) { public void setHands(short hour, short minute) {
queueWrite(new MoveHandsRequest(false, minute, hour, (short) -1)); queueWrite(new MoveHandsRequest(false, minute, hour, (short) -1), false);
} }
@@ -136,17 +136,17 @@ public class FossilWatchAdapter extends WatchAdapter {
@Override @Override
public void requestHandsControl() { public void requestHandsControl() {
queueWrite(new RequestHandControlRequest()); queueWrite(new RequestHandControlRequest(), false);
} }
@Override @Override
public void releaseHandsControl() { public void releaseHandsControl() {
queueWrite(new ReleaseHandsControlRequest()); queueWrite(new ReleaseHandsControlRequest(), false);
} }
@Override @Override
public void setStepGoal(int stepGoal) { public void setStepGoal(int stepGoal) {
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.DailyStepGoalConfigItem(stepGoal), this)); queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.DailyStepGoalConfigItem(stepGoal), this), false);
} }
@Override @Override
@@ -155,7 +155,7 @@ public class FossilWatchAdapter extends WatchAdapter {
queueWrite( queueWrite(
new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[]{vibrationItem}, this) new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[]{vibrationItem}, this), false
); );
// queueWrite(new FileVerifyRequest((short) 0x0800)); // queueWrite(new FileVerifyRequest((short) 0x0800));
} }
@@ -182,7 +182,7 @@ public class FossilWatchAdapter extends WatchAdapter {
getDeviceSupport().getDevice().setState(GBDevice.State.INITIALIZED); getDeviceSupport().getDevice().setState(GBDevice.State.INITIALIZED);
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext()); getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
} }
}); }, false);
} catch (GBException e) { } catch (GBException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -190,7 +190,7 @@ public class FossilWatchAdapter extends WatchAdapter {
@Override @Override
public void onTestNewFunction() { public void onTestNewFunction() {
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[0], this)); queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[0], this), false);
} }
@Override @Override
@@ -280,11 +280,7 @@ public class FossilWatchAdapter extends WatchAdapter {
return true; return true;
} }
} }
try { queueNextRequest();
queueWrite(requestQueue.remove(0));
} catch (IndexOutOfBoundsException e) {
log("requestsQueue empty");
}
} }
} }
return true; return true;
@@ -305,13 +301,6 @@ public class FossilWatchAdapter extends WatchAdapter {
} }
} }
private void log(String message) {
Log.d("FossilWatchAdapter", message);
}
public void queueWrite(Request request) {
this.queueWrite(request, false);
}
@Override @Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
@@ -322,38 +311,40 @@ public class FossilWatchAdapter extends WatchAdapter {
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext()); getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
((RequestMtuRequest) fossilRequest).setFinished(true); ((RequestMtuRequest) fossilRequest).setFinished(true);
try { queueNextRequest();
queueWrite(requestQueue.remove(0));
} catch (IndexOutOfBoundsException e) {
}
} }
//TODO split to multiple methods instead of switch public void queueWrite(RequestMtuRequest request, boolean priorise) {
public void queueWrite(Request request, boolean priorise) {
if(request instanceof RequestMtuRequest){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
new TransactionBuilder("requestMtu") new TransactionBuilder("requestMtu")
.requestMtu(512) .requestMtu(512)
.queue(getDeviceSupport().getQueue()); .queue(getDeviceSupport().getQueue());
this.fossilRequest = (FossilRequest) request; this.fossilRequest = request;
} }
return;
}else if(request instanceof SetDeviceStateRequest){
log("setting device state: " + ((SetDeviceStateRequest)request).getDeviceState());
getDeviceSupport().getDevice().setState(((SetDeviceStateRequest)request).getDeviceState());
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
try {
queueWrite(requestQueue.remove(0));
} catch (IndexOutOfBoundsException e) {
} }
return;
} else if (request.isBasicRequest()) { private void log(String message) {
try { Log.d("FossilWatchAdapter", message);
queueWrite(requestQueue.remove(0));
} catch (IndexOutOfBoundsException e) {
} }
public void queueWrite(SetDeviceStateRequest request, boolean priorise) {
if (fossilRequest != null && !fossilRequest.isFinished()) {
log("queing request: " + request.getName());
if (priorise) {
requestQueue.add(0, request);
} else { } else {
requestQueue.add(request);
}
return;
}
log("setting device state: " + request.getDeviceState());
getDeviceSupport().getDevice().setState(request.getDeviceState());
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
queueNextRequest();
}
public void queueWrite(FossilRequest request, boolean priorise) {
if (fossilRequest != null && !fossilRequest.isFinished()) { if (fossilRequest != null && !fossilRequest.isFinished()) {
log("queing request: " + request.getName()); log("queing request: " + request.getName());
if (priorise) { if (priorise) {
@@ -364,10 +355,31 @@ public class FossilWatchAdapter extends WatchAdapter {
return; return;
} }
log("executing request: " + request.getName()); log("executing request: " + request.getName());
this.fossilRequest = request;
if (request instanceof FossilRequest) this.fossilRequest = (FossilRequest) request;
}
new TransactionBuilder(request.getClass().getSimpleName()).write(getDeviceSupport().getCharacteristic(request.getRequestUUID()), request.getRequestData()).queue(getDeviceSupport().getQueue()); new TransactionBuilder(request.getClass().getSimpleName()).write(getDeviceSupport().getCharacteristic(request.getRequestUUID()), request.getRequestData()).queue(getDeviceSupport().getQueue());
} }
public void queueWrite(Request request, boolean priorise) {
new TransactionBuilder(request.getClass().getSimpleName()).write(getDeviceSupport().getCharacteristic(request.getRequestUUID()), request.getRequestData()).queue(getDeviceSupport().getQueue());
queueNextRequest();
}
void queueWrite(Request request) {
if (request instanceof SetDeviceStateRequest)
queueWrite((SetDeviceStateRequest) request, false);
else if (request instanceof RequestMtuRequest)
queueWrite((RequestMtuRequest) request, false);
else if (request instanceof FossilRequest) queueWrite((FossilRequest) request, false);
else queueWrite(request, false);
}
private void queueNextRequest() {
try {
Request request = requestQueue.remove(0);
queueWrite(request);
} catch (IndexOutOfBoundsException e) {
log("requestsQueue empty");
}
}
} }

View File

@@ -28,7 +28,7 @@ public class FileCloseAndPutRequest extends FileCloseRequest {
super.onFilePut(success); super.onFilePut(success);
FileCloseAndPutRequest.this.onFilePut(success); FileCloseAndPutRequest.this.onFilePut(success);
} }
}); }, false);
} }
public void onFilePut(boolean success){ public void onFilePut(boolean success){

View File

@@ -7,6 +7,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
import java.util.zip.CRC32;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEQueue; import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEQueue;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@@ -25,12 +26,12 @@ public class FilePutRequest extends FossilRequest {
private short handle; private short handle;
public int packetIndex = 0;
private FossilWatchAdapter adapter; private FossilWatchAdapter adapter;
byte[] file; byte[] file;
int fullCRC;
public FilePutRequest(short handle, byte[] file, FossilWatchAdapter adapter) { public FilePutRequest(short handle, byte[] file, FossilWatchAdapter adapter) {
this.handle = handle; this.handle = handle;
this.adapter = adapter; this.adapter = adapter;
@@ -94,15 +95,8 @@ public class FilePutRequest extends FossilRequest {
throw new RuntimeException("wrong response handle"); throw new RuntimeException("wrong response handle");
} }
CRC32C realCrc = new CRC32C(); if (crc != this.fullCRC) {
byte[] data = packets.get(packetIndex); throw new RuntimeException("file upload exception: wrong crc");
realCrc.update(data, 1, data.length - 1);
if (crc != (int) realCrc.getValue()) {
// this.state = UploadState.ERROR;
// log("wrong crc");
// TODO CRC
// break;
} }
@@ -200,6 +194,11 @@ public class FilePutRequest extends FossilRequest {
byte[] data = buffer.array(); byte[] data = buffer.array();
CRC32 fullCRC = new CRC32();
fullCRC.update(data);
this.fullCRC = (int) fullCRC.getValue();
int packetCount = (int) Math.ceil(data.length / (float) maxPacketSize); int packetCount = (int) Math.ceil(data.length / (float) maxPacketSize);
for (int i = 0; i < packetCount; i++) { for (int i = 0; i < packetCount; i++) {

View File

@@ -54,10 +54,7 @@ public class NotificationFilterPutRequest extends FilePutRequest {
buffer.put(config.getVibration().getValue()); buffer.put(config.getVibration().getValue());
} }
// return new byte[]{(byte) 0x19, (byte) 0x00, (byte) 0x04, (byte) 0x04, (byte) 0xCC, (byte) 0x6A, (byte) 0x8C, (byte) 0x17, (byte) 0x80, (byte) 0x01, (byte) 0x02, (byte) 0xC1, (byte) 0x01, (byte) 0xFF, (byte) 0xC2, (byte) 0x08, (byte) 0x77, (byte) 0x00, (byte) 0x77, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0x10, (byte) 0x27, (byte) 0xC3, (byte) 0x01, (byte) 0x04, (byte) 0x19, (byte) 0x00, (byte) 0x04, (byte) 0x04, (byte) 0xCC, (byte) 0x6A, (byte) 0x8C, (byte) 0x17, (byte) 0x80, (byte) 0x01, (byte) 0x02, (byte) 0xC1, (byte) 0x01, (byte) 0xFF, (byte) 0xC2, (byte) 0x08, (byte) 0x77, (byte) 0x00, (byte) 0x77, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0x10, (byte) 0x27, (byte) 0xC3, (byte) 0x01, (byte) 0x04};
return buffer.array(); return buffer.array();
// return new byte[]{1};
// return new byte[]{0x19, (byte) 0x00, (byte) 0x04, (byte) 0x04, (byte) 0xCC, (byte) 0x6A, (byte) 0x8C, (byte) 0x17, (byte) 0x80, (byte) 0x01, (byte) 0x02, (byte) 0xC1, (byte) 0x01, (byte) 0xFF, (byte) 0xC2, (byte) 0x08, (byte) 0x59, (byte) 0x00, (byte) 0x59, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0x10, (byte) 0x27, (byte) 0xC3, (byte) 0x01, (byte) 0x04, (byte) 0x19, (byte) 0x00, (byte) 0x04, (byte) 0x04, (byte) 0xCC, (byte) 0x6A, (byte) 0x8C, (byte) 0x17, (byte) 0x80, (byte) 0x01, (byte) 0x02, (byte) 0xC1, (byte) 0x01, (byte) 0xFF, (byte) 0xC2, (byte) 0x08, (byte) 0x59, (byte) 0x00, (byte) 0x59, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0x10, (byte) 0x27, (byte) 0xC3, (byte) 0x01, (byte) 0x04};
} }
enum PacketID{ enum PacketID{