mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-29 12:05:53 +01:00
code cleanup and crc handling
This commit is contained in:
parent
56d5b95181
commit
1bdaa55937
@ -53,24 +53,24 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
public void initialize() {
|
||||
playPairingAnimation();
|
||||
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();
|
||||
|
||||
queueWrite(new SetDeviceStateRequest(GBDevice.State.INITIALIZED));
|
||||
queueWrite(new SetDeviceStateRequest(GBDevice.State.INITIALIZED), false);
|
||||
}
|
||||
|
||||
public int getMTU(){
|
||||
if(this.MTU < 0) throw new RuntimeException("MTU not configured");
|
||||
public int getMTU() {
|
||||
if (this.MTU < 0) throw new RuntimeException("MTU not configured");
|
||||
|
||||
return this.MTU;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playPairingAnimation() {
|
||||
queueWrite(new AnimationRequest());
|
||||
queueWrite(new AnimationRequest(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -79,7 +79,7 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
log("package name in notification not set");
|
||||
return;
|
||||
}
|
||||
queueWrite(new PlayNotificationRequest(config.getPackageName(), this));
|
||||
queueWrite(new PlayNotificationRequest(config.getPackageName(), this), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -94,7 +94,7 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
(short) (millis % 1000),
|
||||
(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) 0x00
|
||||
}, this);
|
||||
queueWrite(uploadFileRequest);
|
||||
queueWrite(uploadFileRequest, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -115,12 +115,12 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
queueWrite(new ConfigurationPutRequest(
|
||||
new ConfigurationPutRequest.CurrentStepCountConfigItem(Math.min(999999, (int) (1000000 * progress))),
|
||||
this
|
||||
));
|
||||
), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
public void requestHandsControl() {
|
||||
queueWrite(new RequestHandControlRequest());
|
||||
queueWrite(new RequestHandControlRequest(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseHandsControl() {
|
||||
queueWrite(new ReleaseHandsControlRequest());
|
||||
queueWrite(new ReleaseHandsControlRequest(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStepGoal(int stepGoal) {
|
||||
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.DailyStepGoalConfigItem(stepGoal), this));
|
||||
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.DailyStepGoalConfigItem(stepGoal), this), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVibrationStrength(short strength) {
|
||||
ConfigurationPutRequest.ConfigItem vibrationItem = new ConfigurationPutRequest.VibrationStrengthConfigItem((byte)strength);
|
||||
ConfigurationPutRequest.ConfigItem vibrationItem = new ConfigurationPutRequest.VibrationStrengthConfigItem((byte) strength);
|
||||
|
||||
|
||||
queueWrite(
|
||||
new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[]{vibrationItem}, this)
|
||||
new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[]{vibrationItem}, this), false
|
||||
);
|
||||
// queueWrite(new FileVerifyRequest((short) 0x0800));
|
||||
}
|
||||
@ -182,7 +182,7 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
getDeviceSupport().getDevice().setState(GBDevice.State.INITIALIZED);
|
||||
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
|
||||
}
|
||||
});
|
||||
}, false);
|
||||
} catch (GBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -190,7 +190,7 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
|
||||
@Override
|
||||
public void onTestNewFunction() {
|
||||
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[0], this));
|
||||
queueWrite(new ConfigurationPutRequest(new ConfigurationPutRequest.ConfigItem[0], this), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -256,10 +256,10 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
if (fossilRequest != null) {
|
||||
boolean requestFinished;
|
||||
try {
|
||||
if(characteristic.getUuid().toString().equals("3dda0003-957f-7d4a-34a6-74696673696d")){
|
||||
byte requestType = (byte)(characteristic.getValue()[0] & 0x0F);
|
||||
if (characteristic.getUuid().toString().equals("3dda0003-957f-7d4a-34a6-74696673696d")) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@ -280,19 +280,15 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
try {
|
||||
queueWrite(requestQueue.remove(0));
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
log("requestsQueue empty");
|
||||
}
|
||||
queueNextRequest();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void handleBackgroundCharacteristic(BluetoothGattCharacteristic characteristic){
|
||||
private void handleBackgroundCharacteristic(BluetoothGattCharacteristic characteristic) {
|
||||
byte[] value = characteristic.getValue();
|
||||
switch (value[1]){
|
||||
switch (value[1]) {
|
||||
case 2: {
|
||||
byte syncId = value[2];
|
||||
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
|
||||
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().sendDeviceUpdateIntent(getContext());
|
||||
|
||||
((RequestMtuRequest)fossilRequest).setFinished(true);
|
||||
try {
|
||||
queueWrite(requestQueue.remove(0));
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
((RequestMtuRequest) fossilRequest).setFinished(true);
|
||||
queueNextRequest();
|
||||
}
|
||||
|
||||
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
|
||||
public void queueWrite(Request request, boolean priorise) {
|
||||
if(request instanceof RequestMtuRequest){
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
new TransactionBuilder("requestMtu")
|
||||
.requestMtu(512)
|
||||
.queue(getDeviceSupport().getQueue());
|
||||
private void log(String message) {
|
||||
Log.d("FossilWatchAdapter", message);
|
||||
}
|
||||
|
||||
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;
|
||||
}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());
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ public class FileCloseAndPutRequest extends FileCloseRequest {
|
||||
super.onFilePut(success);
|
||||
FileCloseAndPutRequest.this.onFilePut(success);
|
||||
}
|
||||
});
|
||||
}, false);
|
||||
}
|
||||
|
||||
public void onFilePut(boolean success){
|
||||
|
@ -7,6 +7,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEQueue;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||||
@ -25,12 +26,12 @@ public class FilePutRequest extends FossilRequest {
|
||||
|
||||
private short handle;
|
||||
|
||||
public int packetIndex = 0;
|
||||
|
||||
private FossilWatchAdapter adapter;
|
||||
|
||||
byte[] file;
|
||||
|
||||
int fullCRC;
|
||||
|
||||
public FilePutRequest(short handle, byte[] file, FossilWatchAdapter adapter) {
|
||||
this.handle = handle;
|
||||
this.adapter = adapter;
|
||||
@ -94,15 +95,8 @@ public class FilePutRequest extends FossilRequest {
|
||||
throw new RuntimeException("wrong response handle");
|
||||
}
|
||||
|
||||
CRC32C realCrc = new CRC32C();
|
||||
byte[] data = packets.get(packetIndex);
|
||||
realCrc.update(data, 1, data.length - 1);
|
||||
|
||||
if (crc != (int) realCrc.getValue()) {
|
||||
// this.state = UploadState.ERROR;
|
||||
// log("wrong crc");
|
||||
// TODO CRC
|
||||
// break;
|
||||
if (crc != this.fullCRC) {
|
||||
throw new RuntimeException("file upload exception: wrong crc");
|
||||
}
|
||||
|
||||
|
||||
@ -200,6 +194,11 @@ public class FilePutRequest extends FossilRequest {
|
||||
|
||||
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);
|
||||
|
||||
for (int i = 0; i < packetCount; i++) {
|
||||
|
@ -54,10 +54,7 @@ public class NotificationFilterPutRequest extends FilePutRequest {
|
||||
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 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{
|
||||
|
Loading…
Reference in New Issue
Block a user