1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 06:22:55 +01:00

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,24 +53,24 @@ 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() {
if(this.MTU < 0) throw new RuntimeException("MTU not configured"); if (this.MTU < 0) throw new RuntimeException("MTU not configured");
return this.MTU; return this.MTU;
} }
@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,26 +136,26 @@ 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
public void setVibrationStrength(short strength) { public void setVibrationStrength(short strength) {
ConfigurationPutRequest.ConfigItem vibrationItem = new ConfigurationPutRequest.VibrationStrengthConfigItem((byte)strength); ConfigurationPutRequest.ConfigItem vibrationItem = new ConfigurationPutRequest.VibrationStrengthConfigItem((byte) strength);
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
@ -256,10 +256,10 @@ public class FossilWatchAdapter extends WatchAdapter {
if (fossilRequest != null) { if (fossilRequest != null) {
boolean requestFinished; boolean requestFinished;
try { try {
if(characteristic.getUuid().toString().equals("3dda0003-957f-7d4a-34a6-74696673696d")){ if (characteristic.getUuid().toString().equals("3dda0003-957f-7d4a-34a6-74696673696d")) {
byte requestType = (byte)(characteristic.getValue()[0] & 0x0F); byte requestType = (byte) (characteristic.getValue()[0] & 0x0F);
if(requestType != 0x0A && requestType != fossilRequest.getType()){ if (requestType != 0x0A && requestType != fossilRequest.getType()) {
// throw new RuntimeException("Answer type " + requestType + " does not match current request " + fossilRequest.getType()); // throw new RuntimeException("Answer type " + requestType + " does not match current request " + fossilRequest.getType());
} }
} }
@ -280,19 +280,15 @@ 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;
} }
private void handleBackgroundCharacteristic(BluetoothGattCharacteristic characteristic){ private void handleBackgroundCharacteristic(BluetoothGattCharacteristic characteristic) {
byte[] value = characteristic.getValue(); byte[] value = characteristic.getValue();
switch (value[1]){ switch (value[1]) {
case 2: { case 2: {
byte syncId = value[2]; byte syncId = value[2];
getDeviceSupport().getDevice().addDeviceInfo(new GenericItem(QHybridSupport.ITEM_LAST_HEARTBEAT, DateFormat.getTimeInstance().format(new Date()))); getDeviceSupport().getDevice().addDeviceInfo(new GenericItem(QHybridSupport.ITEM_LAST_HEARTBEAT, DateFormat.getTimeInstance().format(new Date())));
@ -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) {
@ -321,53 +310,76 @@ public class FossilWatchAdapter extends WatchAdapter {
getDeviceSupport().getDevice().addDeviceInfo(new GenericItem(ITEM_MTU, String.valueOf(mtu))); getDeviceSupport().getDevice().addDeviceInfo(new GenericItem(ITEM_MTU, String.valueOf(mtu)));
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) {
public void queueWrite(RequestMtuRequest request, boolean priorise) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
new TransactionBuilder("requestMtu")
.requestMtu(512)
.queue(getDeviceSupport().getQueue());
this.fossilRequest = request;
} }
} }
//TODO split to multiple methods instead of switch private void log(String message) {
public void queueWrite(Request request, boolean priorise) { Log.d("FossilWatchAdapter", message);
if(request instanceof RequestMtuRequest){ }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
new TransactionBuilder("requestMtu")
.requestMtu(512)
.queue(getDeviceSupport().getQueue());
this.fossilRequest = (FossilRequest) request; public void queueWrite(SetDeviceStateRequest request, boolean priorise) {
if (fossilRequest != null && !fossilRequest.isFinished()) {
log("queing request: " + request.getName());
if (priorise) {
requestQueue.add(0, request);
} else {
requestQueue.add(request);
} }
return; 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()) {
try {
queueWrite(requestQueue.remove(0));
} catch (IndexOutOfBoundsException e) {
}
} else {
if (fossilRequest != null && !fossilRequest.isFinished()) {
log("queing request: " + request.getName());
if (priorise) {
requestQueue.add(0, request);
} else {
requestQueue.add(request);
}
return;
}
log("executing request: " + request.getName());
if (request instanceof FossilRequest) this.fossilRequest = (FossilRequest) request;
} }
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()) {
log("queing request: " + request.getName());
if (priorise) {
requestQueue.add(0, request);
} else {
requestQueue.add(request);
}
return;
}
log("executing request: " + request.getName());
this.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{