From 4054326535b40fb3604d9c86148d0b020dfaf6b9 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Wed, 27 Mar 2024 09:15:28 +0100 Subject: [PATCH] Garmin protocol: use message enum instead of id in GFDI Messages --- .../devices/garmin/ProtocolBufferHandler.java | 12 +++++----- .../garmin/messages/ConfigurationMessage.java | 13 +++++----- .../messages/CurrentTimeRequestMessage.java | 13 +++++----- .../messages/DeviceInformationMessage.java | 13 +++++----- .../messages/FindMyPhoneRequestMessage.java | 13 +++++----- .../garmin/messages/FitDataMessage.java | 13 +++++----- .../garmin/messages/FitDefinitionMessage.java | 14 +++++------ .../devices/garmin/messages/GFDIMessage.java | 14 +++++++---- .../MusicControlCapabilitiesMessage.java | 13 +++++----- .../MusicControlEntityUpdateMessage.java | 3 ++- .../garmin/messages/MusicControlMessage.java | 11 ++++----- .../garmin/messages/ProtobufMessage.java | 24 +++++++++---------- .../messages/SetDeviceSettingsMessage.java | 3 ++- .../garmin/messages/SystemEventMessage.java | 3 ++- .../garmin/messages/WeatherMessage.java | 11 ++++----- .../messages/status/FitDataStatusMessage.java | 10 ++++---- .../status/FitDefinitionStatusMessage.java | 9 ++++--- .../messages/status/GFDIStatusMessage.java | 22 ++++++++--------- .../messages/status/GenericStatusMessage.java | 12 ++++++---- .../status/ProtobufStatusMessage.java | 19 +++++++-------- 20 files changed, 125 insertions(+), 120 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java index 1158ca0fb..f03373a4a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java @@ -225,30 +225,30 @@ public class ProtocolBufferHandler { if (null == protobufPayload) return null; final int requestId = getNextProtobufRequestId(); - return prepareProtobufMessage(protobufPayload.toByteArray(), GFDIMessage.GarminMessage.PROTOBUF_REQUEST.getId(), requestId); + return prepareProtobufMessage(protobufPayload.toByteArray(), GFDIMessage.GarminMessage.PROTOBUF_REQUEST, requestId); } private ProtobufMessage prepareProtobufResponse(GdiSmartProto.Smart protobufPayload, int requestId) { if (null == protobufPayload) return null; - return prepareProtobufMessage(protobufPayload.toByteArray(), GFDIMessage.GarminMessage.PROTOBUF_RESPONSE.getId(), requestId); + return prepareProtobufMessage(protobufPayload.toByteArray(), GFDIMessage.GarminMessage.PROTOBUF_RESPONSE, requestId); } - private ProtobufMessage prepareProtobufMessage(byte[] bytes, int messageType, int requestId) { + private ProtobufMessage prepareProtobufMessage(byte[] bytes, GFDIMessage.GarminMessage garminMessage, int requestId) { if (bytes == null || bytes.length == 0) return null; - LOG.info("Preparing protobuf message. Type{}, #{}, {}B: {}", messageType, requestId, bytes.length, GB.hexdump(bytes, 0, bytes.length)); + LOG.info("Preparing protobuf message. Type{}, #{}, {}B: {}", garminMessage, requestId, bytes.length, GB.hexdump(bytes, 0, bytes.length)); if (bytes.length > maxChunkSize) { chunkedFragmentsMap.put(requestId, new ProtobufFragment(bytes)); - return new ProtobufMessage(messageType, + return new ProtobufMessage(garminMessage, requestId, 0, bytes.length, maxChunkSize, ArrayUtils.subarray(bytes, 0, maxChunkSize)); } - return new ProtobufMessage(messageType, requestId, 0, bytes.length, bytes.length, bytes); + return new ProtobufMessage(garminMessage, requestId, 0, bytes.length, bytes.length, bytes); } class ProtobufFragment { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ConfigurationMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ConfigurationMessage.java index 572c1abb3..f55168bfc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ConfigurationMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ConfigurationMessage.java @@ -8,11 +8,10 @@ import nodomain.freeyourgadget.gadgetbridge.devices.vivomovehr.GarminCapability; public class ConfigurationMessage extends GFDIMessage { public final Set OUR_CAPABILITIES = GarminCapability.ALL_CAPABILITIES; private final byte[] incomingConfigurationPayload; - private final int messageType; private final byte[] ourConfigurationPayload = GarminCapability.setToBinary(OUR_CAPABILITIES); - public ConfigurationMessage(int messageType, byte[] configurationPayload) { - this.messageType = messageType; + public ConfigurationMessage(GarminMessage garminMessage, byte[] configurationPayload) { + this.garminMessage = garminMessage; if (configurationPayload.length > 255) throw new IllegalArgumentException("Too long payload"); this.incomingConfigurationPayload = configurationPayload; @@ -20,12 +19,12 @@ public class ConfigurationMessage extends GFDIMessage { Set capabilities = GarminCapability.setFromBinary(configurationPayload); LOG.info("Received configuration message; capabilities: {}", GarminCapability.setToString(capabilities)); - this.statusMessage = this.getStatusMessage(messageType); + this.statusMessage = this.getStatusMessage(); } - public static ConfigurationMessage parseIncoming(MessageReader reader, int messageType) { + public static ConfigurationMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int endOfPayload = reader.readByte(); - ConfigurationMessage configurationMessage = new ConfigurationMessage(messageType, reader.readBytes(endOfPayload - reader.getPosition())); + ConfigurationMessage configurationMessage = new ConfigurationMessage(garminMessage, reader.readBytes(endOfPayload - reader.getPosition())); reader.warnIfLeftover(); return configurationMessage; } @@ -34,7 +33,7 @@ public class ConfigurationMessage extends GFDIMessage { protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // placeholder for packet size - writer.writeShort(messageType); + writer.writeShort(garminMessage.getId()); writer.writeByte(ourConfigurationPayload.length); writer.writeBytes(ourConfigurationPayload); return true; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java index e6de88ed3..2f100a5a1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java @@ -8,19 +8,18 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.GarminTimeUti public class CurrentTimeRequestMessage extends GFDIMessage { private final int referenceID; - private final int messageType; - public CurrentTimeRequestMessage(int messageType, int referenceID) { - this.messageType = messageType; + public CurrentTimeRequestMessage(int referenceID, GarminMessage garminMessage) { + this.garminMessage = garminMessage; this.referenceID = referenceID; - this.statusMessage = this.getStatusMessage(messageType); + this.statusMessage = this.getStatusMessage(); } - public static CurrentTimeRequestMessage parseIncoming(MessageReader reader, int messageType) { + public static CurrentTimeRequestMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int referenceID = reader.readInt(); reader.warnIfLeftover(); - return new CurrentTimeRequestMessage(messageType, referenceID); + return new CurrentTimeRequestMessage(referenceID, garminMessage); } @Override @@ -38,7 +37,7 @@ public class CurrentTimeRequestMessage extends GFDIMessage { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below writer.writeShort(GarminMessage.RESPONSE.getId()); - writer.writeShort(messageType); + writer.writeShort(this.garminMessage.getId()); writer.writeByte(Status.ACK.ordinal()); writer.writeInt(referenceID); writer.writeInt(garminTimestamp); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java index 282fadbf6..7ed3af986 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java @@ -12,7 +12,6 @@ public class DeviceInformationMessage extends GFDIMessage { final int ourUnitNumber = -1; final int ourSoftwareVersion = 7791; final int ourMaxPacketSize = -1; - private final int messageType; private final int incomingProtocolVersion; private final int ourProtocolVersion = 150; private final int incomingProductNumber; @@ -25,8 +24,8 @@ public class DeviceInformationMessage extends GFDIMessage { private final String deviceModel; // dual-pairing flags & MAC addresses... - public DeviceInformationMessage(int messageType, int protocolVersion, int productNumber, String unitNumber, int softwareVersion, int maxPacketSize, String bluetoothFriendlyName, String deviceName, String deviceModel) { - this.messageType = messageType; + public DeviceInformationMessage(GarminMessage garminMessage, int protocolVersion, int productNumber, String unitNumber, int softwareVersion, int maxPacketSize, String bluetoothFriendlyName, String deviceName, String deviceModel) { + this.garminMessage = garminMessage; this.incomingProtocolVersion = protocolVersion; this.incomingProductNumber = productNumber; this.incomingUnitNumber = unitNumber; @@ -35,9 +34,11 @@ public class DeviceInformationMessage extends GFDIMessage { this.bluetoothFriendlyName = bluetoothFriendlyName; this.deviceName = deviceName; this.deviceModel = deviceModel; + + this.statusMessage = getStatusMessage(); } - public static DeviceInformationMessage parseIncoming(MessageReader reader, int messageType) { + public static DeviceInformationMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int protocolVersion = reader.readShort(); final int productNumber = reader.readShort(); final String unitNumber = Long.toString(reader.readInt() & 0xFFFFFFFFL); @@ -48,7 +49,7 @@ public class DeviceInformationMessage extends GFDIMessage { final String deviceModel = reader.readString(); reader.warnIfLeftover(); - return new DeviceInformationMessage(messageType, protocolVersion, productNumber, unitNumber, softwareVersion, maxPacketSize, bluetoothFriendlyName, deviceName, deviceModel); + return new DeviceInformationMessage(garminMessage, protocolVersion, productNumber, unitNumber, softwareVersion, maxPacketSize, bluetoothFriendlyName, deviceName, deviceModel); } @Override @@ -59,7 +60,7 @@ public class DeviceInformationMessage extends GFDIMessage { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // placeholder for packet size writer.writeShort(GarminMessage.RESPONSE.getId()); - writer.writeShort(messageType); + writer.writeShort(this.garminMessage.getId()); writer.writeByte(Status.ACK.ordinal()); writer.writeShort(ourProtocolVersion); writer.writeShort(ourProductNumber); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FindMyPhoneRequestMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FindMyPhoneRequestMessage.java index 29133ec10..93b299028 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FindMyPhoneRequestMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FindMyPhoneRequestMessage.java @@ -6,24 +6,25 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone; public class FindMyPhoneRequestMessage extends GFDIMessage { private final int duration; - private final int messageType; - public FindMyPhoneRequestMessage(int messageType, int duration) { - this.messageType = messageType; + public FindMyPhoneRequestMessage(GarminMessage garminMessage, int duration) { + this.garminMessage = garminMessage; this.duration = duration; + + this.statusMessage = getStatusMessage(); } - public static FindMyPhoneRequestMessage parseIncoming(MessageReader reader, int messageType) { + public static FindMyPhoneRequestMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int duration = reader.readByte(); reader.warnIfLeftover(); - return new FindMyPhoneRequestMessage(messageType, duration); + return new FindMyPhoneRequestMessage(garminMessage, duration); } @Override public GBDeviceEvent getGBDeviceEvent() { final GBDeviceEventFindPhone findPhoneEvent = new GBDeviceEventFindPhone(); - findPhoneEvent.event = messageType == GarminMessage.FIND_MY_PHONE.getId() ? GBDeviceEventFindPhone.Event.START : GBDeviceEventFindPhone.Event.STOP; + findPhoneEvent.event = garminMessage == GarminMessage.FIND_MY_PHONE ? GBDeviceEventFindPhone.Event.START : GBDeviceEventFindPhone.Event.STOP; return findPhoneEvent; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDataMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDataMessage.java index db75a56fe..0299af594 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDataMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDataMessage.java @@ -9,19 +9,18 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordHea public class FitDataMessage extends GFDIMessage { private final List recordDataList; - private final int messageType; - public FitDataMessage(List recordDataList, int messageType) { + public FitDataMessage(List recordDataList, GarminMessage garminMessage) { this.recordDataList = recordDataList; - this.messageType = messageType; + this.garminMessage = garminMessage; } public FitDataMessage(List recordDataList) { this.recordDataList = recordDataList; - this.messageType = GarminMessage.FIT_DATA.getId(); + this.garminMessage = GarminMessage.FIT_DATA; } - public static FitDataMessage parseIncoming(MessageReader reader, int messageType) { + public static FitDataMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { List recordDataList = new ArrayList<>(); @@ -34,7 +33,7 @@ public class FitDataMessage extends GFDIMessage { recordDataList.add(recordData); } - return new FitDataMessage(recordDataList, messageType); + return new FitDataMessage(recordDataList, garminMessage); } public List getRecordDataList() { @@ -45,7 +44,7 @@ public class FitDataMessage extends GFDIMessage { protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below - writer.writeShort(messageType); + writer.writeShort(this.garminMessage.getId()); for (RecordData recordData : recordDataList) { recordData.generateOutgoingDataPayload(writer); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDefinitionMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDefinitionMessage.java index 791de9731..1bf34e896 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDefinitionMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/FitDefinitionMessage.java @@ -9,19 +9,19 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.RecordHea public class FitDefinitionMessage extends GFDIMessage { private final List recordDefinitions; - private final int messageType; - public FitDefinitionMessage(List recordDefinitions, int messageType) { + public FitDefinitionMessage(List recordDefinitions, GarminMessage garminMessage) { this.recordDefinitions = recordDefinitions; - this.messageType = messageType; + this.garminMessage = garminMessage; + this.statusMessage = this.getStatusMessage(); } public FitDefinitionMessage(List recordDefinitions) { this.recordDefinitions = recordDefinitions; - this.messageType = GarminMessage.FIT_DEFINITION.getId(); + this.garminMessage = GarminMessage.FIT_DEFINITION; } - public static FitDefinitionMessage parseIncoming(MessageReader reader, int messageType) { + public static FitDefinitionMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { List recordDefinitions = new ArrayList<>(); while (!reader.isEndOfPayload()) { @@ -29,7 +29,7 @@ public class FitDefinitionMessage extends GFDIMessage { recordDefinitions.add(RecordDefinition.parseIncoming(reader, recordHeader)); } - return new FitDefinitionMessage(recordDefinitions, messageType); + return new FitDefinitionMessage(recordDefinitions, garminMessage); } public List getRecordDefinitions() { @@ -40,7 +40,7 @@ public class FitDefinitionMessage extends GFDIMessage { protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below - writer.writeShort(messageType); + writer.writeShort(garminMessage.getId()); for (RecordDefinition recordDefinition : recordDefinitions) { recordDefinition.generateOutgoingPayload(writer); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java index f992436bb..3a517a897 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java @@ -1,5 +1,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages; +import androidx.annotation.Nullable; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,15 +32,16 @@ public abstract class GFDIMessage { protected static final Logger LOG = LoggerFactory.getLogger(GFDIMessage.class); protected final ByteBuffer response = ByteBuffer.allocate(1000); protected GFDIStatusMessage statusMessage; + protected GarminMessage garminMessage; public static GFDIMessage parseIncoming(byte[] message) { final MessageReader messageReader = new MessageReader(message); final int messageType = messageReader.readShort(); try { -// Class objectClass = GarminMessage.getClassFromId(messageType); - Method m = GarminMessage.getClassFromId(messageType).getMethod("parseIncoming", MessageReader.class, int.class); - return GarminMessage.getClassFromId(messageType).cast(m.invoke(null, messageReader, messageType)); + GarminMessage garminMessage = GarminMessage.fromId(messageType); + Method m = garminMessage.objectClass.getMethod("parseIncoming", MessageReader.class, GarminMessage.class); + return garminMessage.objectClass.cast(m.invoke(null, messageReader, garminMessage)); } catch (Exception e) { LOG.error("UNHANDLED GFDI MESSAGE TYPE {}, MESSAGE {}", messageType, message); return new UnhandledMessage(messageType); @@ -61,8 +64,8 @@ public abstract class GFDIMessage { return packet; } - protected GFDIStatusMessage getStatusMessage(int messageType) { - return new GenericStatusMessage(messageType, Status.ACK); + protected GFDIStatusMessage getStatusMessage() { + return new GenericStatusMessage(garminMessage, Status.ACK); } public GBDeviceEvent getGBDeviceEvent() { @@ -116,6 +119,7 @@ public abstract class GFDIMessage { return null; } + @Nullable public static GarminMessage fromId(final int id) { for (final GarminMessage garminMessage : GarminMessage.values()) { if (garminMessage.getId() == id) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlCapabilitiesMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlCapabilitiesMessage.java index ea61e7989..72c8ce618 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlCapabilitiesMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlCapabilitiesMessage.java @@ -4,19 +4,18 @@ public class MusicControlCapabilitiesMessage extends GFDIMessage { private final int supportedCapabilities; private final GarminMusicControlCommand[] commands = GarminMusicControlCommand.values(); - private final int messageType; - public MusicControlCapabilitiesMessage(int messageType, int supportedCapabilities) { - this.messageType = messageType; + public MusicControlCapabilitiesMessage(GarminMessage garminMessage, int supportedCapabilities) { + this.garminMessage = garminMessage; this.supportedCapabilities = supportedCapabilities; - this.statusMessage = this.getStatusMessage(messageType); + this.statusMessage = this.getStatusMessage(); } - public static MusicControlCapabilitiesMessage parseIncoming(MessageReader reader, int messageType) { + public static MusicControlCapabilitiesMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int supportedCapabilities = reader.readByte(); reader.warnIfLeftover(); - return new MusicControlCapabilitiesMessage(messageType, supportedCapabilities); + return new MusicControlCapabilitiesMessage(garminMessage, supportedCapabilities); } @Override @@ -27,7 +26,7 @@ public class MusicControlCapabilitiesMessage extends GFDIMessage { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below writer.writeShort(GarminMessage.RESPONSE.getId()); - writer.writeShort(messageType); + writer.writeShort(this.garminMessage.getId()); writer.writeByte(Status.ACK.ordinal()); writer.writeByte(commands.length); for (GarminMusicControlCommand command : commands) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlEntityUpdateMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlEntityUpdateMessage.java index 58d1d82ba..7d2299eca 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlEntityUpdateMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlEntityUpdateMessage.java @@ -10,6 +10,7 @@ public class MusicControlEntityUpdateMessage extends GFDIMessage { public MusicControlEntityUpdateMessage(Map attributes) { this.attributes = attributes; + this.garminMessage = GarminMessage.MUSIC_CONTROL_ENTITY_UPDATE; } @@ -17,7 +18,7 @@ public class MusicControlEntityUpdateMessage extends GFDIMessage { protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below - writer.writeShort(GarminMessage.MUSIC_CONTROL_ENTITY_UPDATE.getId()); + writer.writeShort(this.garminMessage.getId()); for (Map.Entry entry : attributes.entrySet()) { MusicEntity a = entry.getKey(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlMessage.java index 05d50569b..50f6e681a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/MusicControlMessage.java @@ -6,12 +6,11 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicContr public class MusicControlMessage extends GFDIMessage { private static final MusicControlCapabilitiesMessage.GarminMusicControlCommand[] commands = MusicControlCapabilitiesMessage.GarminMusicControlCommand.values(); - final int messageType; private final GBDeviceEventMusicControl event; - public MusicControlMessage(int messageType, MusicControlCapabilitiesMessage.GarminMusicControlCommand command) { + public MusicControlMessage(GarminMessage garminMessage, MusicControlCapabilitiesMessage.GarminMusicControlCommand command) { this.event = new GBDeviceEventMusicControl(); - this.messageType = messageType; + this.garminMessage = garminMessage; switch (command) { case TOGGLE_PLAY_PAUSE: event.event = GBDeviceEventMusicControl.Event.PLAYPAUSE; @@ -24,14 +23,14 @@ public class MusicControlMessage extends GFDIMessage { break; } - this.statusMessage = this.getStatusMessage(messageType); + this.statusMessage = this.getStatusMessage(); } - public static MusicControlMessage parseIncoming(MessageReader reader, int messageType) { + public static MusicControlMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { MusicControlCapabilitiesMessage.GarminMusicControlCommand command = commands[reader.readByte()]; reader.warnIfLeftover(); - return new MusicControlMessage(messageType, command); + return new MusicControlMessage(garminMessage, command); } public GBDeviceEventMusicControl getGBDeviceEvent() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ProtobufMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ProtobufMessage.java index 9f660408b..e9bacd46f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ProtobufMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/ProtobufMessage.java @@ -11,15 +11,14 @@ public class ProtobufMessage extends GFDIMessage { private final int requestId; - private final int messageType; private final int dataOffset; private final int totalProtobufLength; private final int protobufDataLength; private final byte[] messageBytes; private final boolean sendOutgoing; - public ProtobufMessage(int messageType, int requestId, int dataOffset, int totalProtobufLength, int protobufDataLength, byte[] messageBytes, boolean sendOutgoing) { - this.messageType = messageType; + public ProtobufMessage(GarminMessage garminMessage, int requestId, int dataOffset, int totalProtobufLength, int protobufDataLength, byte[] messageBytes, boolean sendOutgoing) { + this.garminMessage = garminMessage; this.requestId = requestId; this.dataOffset = dataOffset; this.totalProtobufLength = totalProtobufLength; @@ -28,20 +27,21 @@ public class ProtobufMessage extends GFDIMessage { this.sendOutgoing = sendOutgoing; if (isComplete()) { - this.statusMessage = new GenericStatusMessage(messageType, GFDIMessage.Status.ACK); + this.statusMessage = new GenericStatusMessage(garminMessage, GFDIMessage.Status.ACK); } else { - this.statusMessage = new ProtobufStatusMessage(messageType, GFDIMessage.Status.ACK, requestId, dataOffset, KEPT, NO_ERROR); + this.statusMessage = new ProtobufStatusMessage(garminMessage, GFDIMessage.Status.ACK, requestId, dataOffset, KEPT, NO_ERROR); } } - public ProtobufMessage(int messageType, int requestId, int dataOffset, int totalProtobufLength, int protobufDataLength, byte[] messageBytes) { - this(messageType, requestId, dataOffset, totalProtobufLength, protobufDataLength, messageBytes, true); + + public ProtobufMessage(GarminMessage garminMessage, int requestId, int dataOffset, int totalProtobufLength, int protobufDataLength, byte[] messageBytes) { + this(garminMessage, requestId, dataOffset, totalProtobufLength, protobufDataLength, messageBytes, true); } public void setStatusMessage(ProtobufStatusMessage protobufStatusMessage) { this.statusMessage = protobufStatusMessage; } - public static ProtobufMessage parseIncoming(MessageReader reader, int messageType) { + public static ProtobufMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int requestID = reader.readShort(); final int dataOffset = reader.readInt(); final int totalProtobufLength = reader.readInt(); @@ -49,15 +49,15 @@ public class ProtobufMessage extends GFDIMessage { final byte[] messageBytes = reader.readBytes(protobufDataLength); reader.warnIfLeftover(); - return new ProtobufMessage(messageType, requestID, dataOffset, totalProtobufLength, protobufDataLength, messageBytes, false); + return new ProtobufMessage(garminMessage, requestID, dataOffset, totalProtobufLength, protobufDataLength, messageBytes, false); } public int getRequestId() { return requestId; } - public int getMessageType() { - return messageType; + public GarminMessage getMessageType() { + return garminMessage; } public int getDataOffset() { @@ -84,7 +84,7 @@ public class ProtobufMessage extends GFDIMessage { protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below - writer.writeShort(messageType); + writer.writeShort(garminMessage.getId()); writer.writeShort(requestId); writer.writeInt(dataOffset); writer.writeInt(totalProtobufLength); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SetDeviceSettingsMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SetDeviceSettingsMessage.java index 429f3b058..46ef639f2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SetDeviceSettingsMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SetDeviceSettingsMessage.java @@ -6,6 +6,7 @@ public class SetDeviceSettingsMessage extends GFDIMessage { private final Map settings; public SetDeviceSettingsMessage(Map settings) { + this.garminMessage = GarminMessage.DEVICE_SETTINGS; this.settings = settings; final int settingsCount = settings.size(); if (settingsCount == 0) throw new IllegalArgumentException("Empty settings"); @@ -17,7 +18,7 @@ public class SetDeviceSettingsMessage extends GFDIMessage { protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below - writer.writeShort(GarminMessage.DEVICE_SETTINGS.getId()); + writer.writeShort(this.garminMessage.getId()); writer.writeByte(settings.size()); for (Map.Entry settingPair : settings.entrySet()) { final GarminDeviceSetting setting = settingPair.getKey(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SystemEventMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SystemEventMessage.java index 1bc87588c..4f4343244 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SystemEventMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/SystemEventMessage.java @@ -8,13 +8,14 @@ public class SystemEventMessage extends GFDIMessage { public SystemEventMessage(GarminSystemEventType eventType, Object value) { this.eventType = eventType; this.value = value; + this.garminMessage = GarminMessage.SYSTEM_EVENT; } @Override protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below - writer.writeShort(GarminMessage.SYSTEM_EVENT.getId()); + writer.writeShort(this.garminMessage.getId()); writer.writeByte(eventType.ordinal()); if (value instanceof String) { writer.writeString((String) value); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/WeatherMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/WeatherMessage.java index 4919d13af..582762a65 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/WeatherMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/WeatherMessage.java @@ -11,17 +11,16 @@ public class WeatherMessage extends GFDIMessage { private final int latitude; private final int longitude; private final int hoursOfForecast; - private final int messageType; private final List weatherDefinitions; - public WeatherMessage(int format, int latitude, int longitude, int hoursOfForecast, int messageType) { + public WeatherMessage(int format, int latitude, int longitude, int hoursOfForecast, GarminMessage garminMessage) { this.format = format; this.latitude = latitude; this.longitude = longitude; this.hoursOfForecast = hoursOfForecast; - this.messageType = messageType; + this.garminMessage = garminMessage; weatherDefinitions = new ArrayList<>(3); @@ -29,17 +28,17 @@ public class WeatherMessage extends GFDIMessage { weatherDefinitions.add(GlobalDefinitionsEnum.HOURLY_WEATHER_FORECAST.getRecordDefinition()); weatherDefinitions.add(GlobalDefinitionsEnum.DAILY_WEATHER_FORECAST.getRecordDefinition()); - this.statusMessage = this.getStatusMessage(messageType); + this.statusMessage = this.getStatusMessage(); } - public static WeatherMessage parseIncoming(MessageReader reader, int messageType) { + public static WeatherMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final int format = reader.readByte(); final int latitude = reader.readInt(); final int longitude = reader.readInt(); final int hoursOfForecast = reader.readByte(); - return new WeatherMessage(format, latitude, longitude, hoursOfForecast, messageType); + return new WeatherMessage(format, latitude, longitude, hoursOfForecast, garminMessage); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDataStatusMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDataStatusMessage.java index 6772d9b7b..b791830f3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDataStatusMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDataStatusMessage.java @@ -8,10 +8,10 @@ public class FitDataStatusMessage extends GFDIStatusMessage { private final Status status; private final FitDataStatusCode fitDataStatusCode; - private final int messageType; - public FitDataStatusMessage(int messageType, Status status, FitDataStatusCode fitDataStatusCode) { - this.messageType = messageType; + + public FitDataStatusMessage(GarminMessage garminMessage, Status status, FitDataStatusCode fitDataStatusCode) { + this.garminMessage = garminMessage; this.status = status; this.fitDataStatusCode = fitDataStatusCode; switch (fitDataStatusCode) { @@ -23,12 +23,12 @@ public class FitDataStatusMessage extends GFDIStatusMessage { } } - public static FitDataStatusMessage parseIncoming(MessageReader reader, int messageType) { + public static FitDataStatusMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final Status status = Status.fromCode(reader.readByte()); final FitDataStatusCode fitDataStatusCode = FitDataStatusCode.fromCode(reader.readByte()); reader.warnIfLeftover(); - return new FitDataStatusMessage(messageType, status, fitDataStatusCode); + return new FitDataStatusMessage(garminMessage, status, fitDataStatusCode); } public enum FitDataStatusCode { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDefinitionStatusMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDefinitionStatusMessage.java index 9bfefdd19..ef3bc50a5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDefinitionStatusMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/FitDefinitionStatusMessage.java @@ -8,10 +8,9 @@ public class FitDefinitionStatusMessage extends GFDIStatusMessage { private final Status status; private final FitDefinitionStatusCode fitDefinitionStatusCode; - private final int messageType; - public FitDefinitionStatusMessage(int messageType, Status status, FitDefinitionStatusCode fitDefinitionStatusCode) { - this.messageType = messageType; + public FitDefinitionStatusMessage(GarminMessage garminMessage, Status status, FitDefinitionStatusCode fitDefinitionStatusCode) { + this.garminMessage = garminMessage; this.status = status; this.fitDefinitionStatusCode = fitDefinitionStatusCode; switch (fitDefinitionStatusCode) { @@ -23,12 +22,12 @@ public class FitDefinitionStatusMessage extends GFDIStatusMessage { } } - public static FitDefinitionStatusMessage parseIncoming(MessageReader reader, int messageType) { + public static FitDefinitionStatusMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final Status status = Status.fromCode(reader.readByte()); final FitDefinitionStatusCode fitDefinitionStatusCode = FitDefinitionStatusCode.fromCode(reader.readByte()); reader.warnIfLeftover(); - return new FitDefinitionStatusMessage(messageType, status, fitDefinitionStatusCode); + return new FitDefinitionStatusMessage(garminMessage, status, fitDefinitionStatusCode); } public enum FitDefinitionStatusCode { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GFDIStatusMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GFDIStatusMessage.java index 18e770714..08af79a41 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GFDIStatusMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GFDIStatusMessage.java @@ -7,25 +7,25 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.Mess public abstract class GFDIStatusMessage extends GFDIMessage { private Status status; - public static GFDIStatusMessage parseIncoming(MessageReader reader, int messageType) { - final GarminMessage garminMessage = GFDIMessage.GarminMessage.fromId(reader.readShort()); - if (GarminMessage.PROTOBUF_REQUEST.equals(garminMessage) || GarminMessage.PROTOBUF_RESPONSE.equals(garminMessage)) { - return ProtobufStatusMessage.parseIncoming(reader, messageType); - } else if (GarminMessage.FIT_DEFINITION.equals(garminMessage)) { - return FitDefinitionStatusMessage.parseIncoming(reader, messageType); - } else if (GarminMessage.FIT_DATA.equals(garminMessage)) { - return FitDataStatusMessage.parseIncoming(reader, messageType); + public static GFDIStatusMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { + final GarminMessage originalGarminMessage = GFDIMessage.GarminMessage.fromId(reader.readShort()); + if (GarminMessage.PROTOBUF_REQUEST.equals(originalGarminMessage) || GarminMessage.PROTOBUF_RESPONSE.equals(originalGarminMessage)) { + return ProtobufStatusMessage.parseIncoming(reader, garminMessage); + } else if (GarminMessage.FIT_DEFINITION.equals(originalGarminMessage)) { + return FitDefinitionStatusMessage.parseIncoming(reader, garminMessage); + } else if (GarminMessage.FIT_DATA.equals(originalGarminMessage)) { + return FitDataStatusMessage.parseIncoming(reader, garminMessage); } else { final Status status = Status.fromCode(reader.readByte()); if (Status.ACK == status) { - LOG.info("Received ACK for message {}", garminMessage.name()); + LOG.info("Received ACK for message {}", originalGarminMessage.name()); } else { - LOG.warn("Received {} for message {}", status, garminMessage.name()); + LOG.warn("Received {} for message {}", status, originalGarminMessage.name()); } reader.warnIfLeftover(); - return new GenericStatusMessage(messageType, status); + return new GenericStatusMessage(garminMessage, status); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GenericStatusMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GenericStatusMessage.java index b9afe5733..7ec377773 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GenericStatusMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/GenericStatusMessage.java @@ -4,20 +4,24 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.Mess public class GenericStatusMessage extends GFDIStatusMessage { - private final int messageType; + private int messageType; // for unsupported message types private final Status status; - public GenericStatusMessage(int originalRequestID, Status status) { - this.messageType = originalRequestID; + public GenericStatusMessage(GarminMessage originalMessage, Status status) { + this.garminMessage = originalMessage; this.status = status; } + public GenericStatusMessage(int messageType, Status status) { + this.messageType = messageType; + this.status = status; + } @Override protected boolean generateOutgoing() { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below writer.writeShort(GarminMessage.RESPONSE.getId()); - writer.writeShort(messageType); + writer.writeShort(messageType != 0 ? messageType : garminMessage.getId()); writer.writeByte(status.ordinal()); return true; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/ProtobufStatusMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/ProtobufStatusMessage.java index 3d775bf88..d9fdf63ce 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/ProtobufStatusMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/status/ProtobufStatusMessage.java @@ -12,15 +12,14 @@ public class ProtobufStatusMessage extends GFDIStatusMessage { private final int dataOffset; private final ProtobufChunkStatus protobufChunkStatus; private final ProtobufStatusCode protobufStatusCode; - private final int messageType; private final boolean sendOutgoing; - public ProtobufStatusMessage(int messageType, Status status, int requestId, int dataOffset, ProtobufChunkStatus protobufChunkStatus, ProtobufStatusCode protobufStatusCode) { - this(messageType, status, requestId, dataOffset, protobufChunkStatus, protobufStatusCode, true); + public ProtobufStatusMessage(GarminMessage garminMessage, Status status, int requestId, int dataOffset, ProtobufChunkStatus protobufChunkStatus, ProtobufStatusCode protobufStatusCode) { + this(garminMessage, status, requestId, dataOffset, protobufChunkStatus, protobufStatusCode, true); } - public ProtobufStatusMessage(int messageType, Status status, int requestId, int dataOffset, ProtobufChunkStatus protobufChunkStatus, ProtobufStatusCode protobufStatusCode, boolean sendOutgoing) { - this.messageType = messageType; + public ProtobufStatusMessage(GarminMessage garminMessage, Status status, int requestId, int dataOffset, ProtobufChunkStatus protobufChunkStatus, ProtobufStatusCode protobufStatusCode, boolean sendOutgoing) { + this.garminMessage = garminMessage; this.status = status; this.requestId = requestId; this.dataOffset = dataOffset; @@ -29,7 +28,7 @@ public class ProtobufStatusMessage extends GFDIStatusMessage { this.sendOutgoing = sendOutgoing; } - public static ProtobufStatusMessage parseIncoming(MessageReader reader, int messageType) { + public static ProtobufStatusMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) { final Status status = Status.fromCode(reader.readByte()); final int requestID = reader.readShort(); final int dataOffset = reader.readInt(); @@ -37,7 +36,7 @@ public class ProtobufStatusMessage extends GFDIStatusMessage { final ProtobufStatusCode error = ProtobufStatusCode.fromCode(reader.readByte()); reader.warnIfLeftover(); - return new ProtobufStatusMessage(messageType, status, requestID, dataOffset, protobufStatus, error, false); + return new ProtobufStatusMessage(garminMessage, status, requestID, dataOffset, protobufStatus, error, false); } public int getDataOffset() { @@ -52,8 +51,8 @@ public class ProtobufStatusMessage extends GFDIStatusMessage { return protobufStatusCode; } - public int getMessageType() { - return messageType; + public GarminMessage getMessageType() { + return garminMessage; } public int getRequestId() { @@ -71,7 +70,7 @@ public class ProtobufStatusMessage extends GFDIStatusMessage { final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below writer.writeShort(GarminMessage.RESPONSE.getId()); - writer.writeShort(messageType); + writer.writeShort(garminMessage.getId()); writer.writeByte(status.ordinal()); writer.writeShort(requestId); writer.writeInt(dataOffset);