Fossil Hybrid: reformatted requests to avoid raw files

This commit is contained in:
Daniel Dakhno 2020-10-17 03:58:04 +02:00
parent 45df4cd35c
commit d3dc6c4c5f
6 changed files with 25 additions and 69 deletions

View File

@ -22,12 +22,13 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileLookupAndGetRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRequest;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class AlarmsGetRequest extends FileLookupAndGetRequest { public class AlarmsGetRequest extends FileGetRequest {
public AlarmsGetRequest(FossilWatchAdapter adapter) { public AlarmsGetRequest(FossilWatchAdapter adapter) {
super((byte) 0x0A, adapter); super(FileHandle.ALARMS, adapter);
} }
@Override @Override
@ -35,14 +36,10 @@ public class AlarmsGetRequest extends FileLookupAndGetRequest {
ByteBuffer buffer = ByteBuffer.wrap(fileData); ByteBuffer buffer = ByteBuffer.wrap(fileData);
buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.order(ByteOrder.LITTLE_ENDIAN);
short handle = buffer.getShort(0); int length = fileData.length / 3;
if(handle != (short) 0x0A00) throw new RuntimeException("wrong alarm handle");
int length = buffer.getInt(8) / 3;
Alarm[] alarms = new Alarm[length]; Alarm[] alarms = new Alarm[length];
for (int i = 0; i < length; i++){ for (int i = 0; i < length; i++){
buffer.position(12 + i * 3);
byte[] alarmBytes = new byte[]{ byte[] alarmBytes = new byte[]{
buffer.get(), buffer.get(),
buffer.get(), buffer.get(),
@ -55,20 +52,5 @@ public class AlarmsGetRequest extends FileLookupAndGetRequest {
} }
public void handleAlarms(Alarm[] alarms){ public void handleAlarms(Alarm[] alarms){
Alarm[] alarms2 = new Alarm[alarms.length];
for(int i = 0; i < alarms.length; i++){
alarms2[i] = Alarm.fromBytes(alarms[i].getData());
}
// TODO: This does nothing currently!
}
@Override
public void handleFileLookupError(FILE_LOOKUP_ERROR error) {
if(error == FILE_LOOKUP_ERROR.FILE_EMPTY){
GB.toast("alarm file empty yet", Toast.LENGTH_LONG, GB.ERROR);
}else{
throw new RuntimeException("strange lookup stuff");
}
} }
} }

View File

@ -22,29 +22,25 @@ import java.nio.ByteOrder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.buttonconfig.ConfigPayload; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.buttonconfig.ConfigPayload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRequest;
public class ButtonConfigurationGetRequest extends FileGetRawRequest { public class ButtonConfigurationGetRequest extends FileGetRequest {
public ButtonConfigurationGetRequest(FossilWatchAdapter adapter) { public ButtonConfigurationGetRequest(FossilWatchAdapter adapter) {
super(FileHandle.SETTINGS_BUTTONS, adapter); super(FileHandle.SETTINGS_BUTTONS, adapter);
} }
@Override @Override
public void handleFileRawData(byte[] fileData) { public void handleFileData(byte[] fileData) {
log("fileData"); log("fileData");
ByteBuffer buffer = ByteBuffer.wrap(fileData); ByteBuffer buffer = ByteBuffer.wrap(fileData);
buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.order(ByteOrder.LITTLE_ENDIAN);
short fileHandle = buffer.getShort(0); byte count = buffer.get(3);
// TODO check file handle
// if(fileData != )
byte count = buffer.get(15);
ConfigPayload[] configs = new ConfigPayload[count]; ConfigPayload[] configs = new ConfigPayload[count];
buffer.position(16); buffer.position(4);
for(int i = 0; i < count; i++){ for(int i = 0; i < count; i++){
int buttonIndex = buffer.get() >> 4; int buttonIndex = buffer.get() >> 4;
int entryCount = buffer.get(); int entryCount = buffer.get();

View File

@ -21,24 +21,18 @@ import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRequest;
public class ConfigurationGetRequest extends FileGetRawRequest { public class ConfigurationGetRequest extends FileGetRequest {
public ConfigurationGetRequest(FossilWatchAdapter adapter) { public ConfigurationGetRequest(FossilWatchAdapter adapter) {
super(FileHandle.CONFIGURATION, adapter); super(FileHandle.CONFIGURATION, adapter);
} }
@Override @Override
public void handleFileRawData(byte[] fileData) { public void handleFileData(byte[] fileData) {
byte[] data = new byte[fileData.length - 12 - 4];
System.arraycopy(fileData, 12, data, 0, data.length);
log("config file: " + getAdapter().arrayToString(fileData));
log("config file: " + getAdapter().arrayToString(data));
GBDevice device = getAdapter().getDeviceSupport().getDevice(); GBDevice device = getAdapter().getDeviceSupport().getDevice();
ConfigurationPutRequest.ConfigItem[] items = ConfigurationPutRequest.parsePayload(data); ConfigurationPutRequest.ConfigItem[] items = ConfigurationPutRequest.parsePayload(fileData);
for(ConfigurationPutRequest.ConfigItem item : items){ for(ConfigurationPutRequest.ConfigItem item : items){
if(item instanceof ConfigurationPutRequest.VibrationStrengthConfigItem){ if(item instanceof ConfigurationPutRequest.VibrationStrengthConfigItem){
device.addDeviceInfo(new GenericItem(QHybridSupport.ITEM_VIBRATION_STRENGTH, String.valueOf(((ConfigurationPutRequest.VibrationStrengthConfigItem) item).getValue()))); device.addDeviceInfo(new GenericItem(QHybridSupport.ITEM_VIBRATION_STRENGTH, String.valueOf(((ConfigurationPutRequest.VibrationStrengthConfigItem) item).getValue())));

View File

@ -6,9 +6,9 @@ import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRequest;
public class GetDeviceInfoRequest extends FileGetRawRequest { public class GetDeviceInfoRequest extends FileGetRequest {
enum INFO_CLASS{ enum INFO_CLASS{
SUPPORTED_FILE_VERSIONS((short) 0x0a, SupportedFileVersionsInfo.class), SUPPORTED_FILE_VERSIONS((short) 0x0a, SupportedFileVersionsInfo.class),
; ;
@ -41,14 +41,13 @@ public class GetDeviceInfoRequest extends FileGetRawRequest {
} }
@Override @Override
public void handleFileRawData(byte[] fileData) { public void handleFileData(byte[] fileData) {
ByteBuffer buffer = ByteBuffer.wrap(fileData); ByteBuffer buffer = ByteBuffer.wrap(fileData);
buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.position(12);
ArrayList<DeviceInfo> deviceInfos = new ArrayList<>(); ArrayList<DeviceInfo> deviceInfos = new ArrayList<>();
while(buffer.remaining() > 4){ while(buffer.remaining() > 0){
short type = buffer.getShort(); short type = buffer.getShort();
int length = buffer.get(); int length = buffer.get();
byte[] payload = new byte[length]; byte[] payload = new byte[length];

View File

@ -21,29 +21,16 @@ import java.nio.ByteOrder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRequest;
import nodomain.freeyourgadget.gadgetbridge.util.CRC32C; import nodomain.freeyourgadget.gadgetbridge.util.CRC32C;
public class NotificationFilterGetRequest extends FileGetRawRequest { public class NotificationFilterGetRequest extends FileGetRequest {
public NotificationFilterGetRequest(FossilWatchAdapter adapter) { public NotificationFilterGetRequest(FossilWatchAdapter adapter) {
super(FileHandle.NOTIFICATION_FILTER, adapter); super(FileHandle.NOTIFICATION_FILTER, adapter);
} }
@Override @Override
public void handleFileRawData(byte[] fileData) { public void handleFileData(byte[] fileData) {
log("handleFileData");
ByteBuffer buffer = ByteBuffer.wrap(fileData);
buffer.order(ByteOrder.LITTLE_ENDIAN);
byte[] data = new byte[fileData.length - 12 - 4];
System.arraycopy(fileData, 12, data, 0, data.length);
CRC32C crc32c = new CRC32C();
crc32c.update(data,0,data.length);
if((int) crc32c.getValue() != buffer.getInt(fileData.length - 4)){
throw new RuntimeException("CRC invalid");
}
} }
} }

View File

@ -6,20 +6,18 @@ import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRequest;
public abstract class TranslationsGetRequest extends FileGetRawRequest { public abstract class TranslationsGetRequest extends FileGetRequest {
public TranslationsGetRequest(FossilWatchAdapter adapter) { public TranslationsGetRequest(FossilWatchAdapter adapter) {
super(FileHandle.ASSET_TRANSLATIONS, adapter); super(FileHandle.ASSET_TRANSLATIONS, adapter);
} }
@Override @Override
public void handleFileRawData(byte[] fileData) { public void handleFileData(byte[] fileData) {
ByteBuffer buffer = ByteBuffer.wrap(fileData); ByteBuffer buffer = ByteBuffer.wrap(fileData);
buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.position(12);
byte[] localeBytes = new byte[5]; byte[] localeBytes = new byte[5];
buffer.get(localeBytes); buffer.get(localeBytes);
String locale = new String(localeBytes); String locale = new String(localeBytes);
@ -28,7 +26,7 @@ public abstract class TranslationsGetRequest extends FileGetRawRequest {
ArrayList<TranslationItem> translations = new ArrayList<>(); ArrayList<TranslationItem> translations = new ArrayList<>();
while(buffer.remaining() > 4){ while(buffer.remaining() > 0){
int originalLength = buffer.getShort() - 1; // subtracting null terminator int originalLength = buffer.getShort() - 1; // subtracting null terminator
byte[] originalBytes = new byte[originalLength]; byte[] originalBytes = new byte[originalLength];
buffer.get(originalBytes); buffer.get(originalBytes);