Garmin protocol: fix crash when stopping find phone

This commit is contained in:
José Rebelo 2024-04-13 14:35:19 +01:00
parent fa8f09e95a
commit 7249830013
4 changed files with 40 additions and 20 deletions

View File

@ -24,8 +24,6 @@ public class GarminByteBufferReader {
}
public int readByte() {
if (!byteBuffer.hasRemaining()) throw new IllegalStateException();
return Byte.toUnsignedInt(byteBuffer.get());
}
@ -34,32 +32,22 @@ public class GarminByteBufferReader {
}
public int readShort() {
if (byteBuffer.remaining() < 2) throw new IllegalStateException();
return Short.toUnsignedInt(byteBuffer.getShort());
}
public int readInt() {
if (byteBuffer.remaining() < 4) throw new IllegalStateException();
return byteBuffer.getInt();
}
public long readLong() {
if (byteBuffer.remaining() < 8) throw new IllegalStateException();
return byteBuffer.getLong();
}
public float readFloat32() {
if (byteBuffer.remaining() < 4) throw new IllegalStateException();
return byteBuffer.getFloat();
}
public double readFloat64() {
if (byteBuffer.remaining() < 8) throw new IllegalStateException();
return byteBuffer.getDouble();
}
@ -79,5 +67,4 @@ public class GarminByteBufferReader {
return bytes;
}
}

View File

@ -0,0 +1,29 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
public class FindMyPhoneCancelMessage extends GFDIMessage {
public FindMyPhoneCancelMessage(GarminMessage garminMessage) {
this.garminMessage = garminMessage;
this.statusMessage = getStatusMessage();
}
public static FindMyPhoneCancelMessage parseIncoming(MessageReader reader, GarminMessage garminMessage) {
return new FindMyPhoneCancelMessage(garminMessage);
}
@Override
public GBDeviceEvent getGBDeviceEvent() {
final GBDeviceEventFindPhone findPhoneEvent = new GBDeviceEventFindPhone();
findPhoneEvent.event = GBDeviceEventFindPhone.Event.STOP;
return findPhoneEvent;
}
@Override
protected boolean generateOutgoing() {
return false;
}
}

View File

@ -23,7 +23,7 @@ public class FindMyPhoneRequestMessage extends GFDIMessage {
@Override
public GBDeviceEvent getGBDeviceEvent() {
final GBDeviceEventFindPhone findPhoneEvent = new GBDeviceEventFindPhone();
findPhoneEvent.event = garminMessage == GarminMessage.FIND_MY_PHONE ? GBDeviceEventFindPhone.Event.START : GBDeviceEventFindPhone.Event.STOP;
findPhoneEvent.event = GBDeviceEventFindPhone.Event.START;
return findPhoneEvent;
}

View File

@ -46,11 +46,15 @@ public abstract class GFDIMessage {
final int messageType = messageReader.readShort();
try {
GarminMessage garminMessage = GarminMessage.fromId(messageType);
Method m = garminMessage.objectClass.getMethod("parseIncoming", MessageReader.class, GarminMessage.class);
final GarminMessage garminMessage = GarminMessage.fromId(messageType);
if (garminMessage == null) {
LOG.warn("Unknown message type {}, message {}", messageType, message);
return new UnhandledMessage(messageType);
}
final 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);
} catch (final Exception e) {
LOG.error("UNHANDLED GFDI MESSAGE TYPE {}, MESSAGE {}", messageType, message, e);
return new UnhandledMessage(messageType);
} finally {
messageReader.warnIfLeftover(messageType);
@ -110,8 +114,8 @@ public abstract class GFDIMessage {
NOTIFICATION_CONTROL(5034, NotificationControlMessage.class),
NOTIFICATION_DATA(5035, NotificationDataMessage.class),
NOTIFICATION_SUBSCRIPTION(5036, NotificationSubscriptionMessage.class),
FIND_MY_PHONE(5039, FindMyPhoneRequestMessage.class),
CANCEL_FIND_MY_PHONE(5040, FindMyPhoneRequestMessage.class),
FIND_MY_PHONE_REQUEST(5039, FindMyPhoneRequestMessage.class),
FIND_MY_PHONE_CANCEL(5040, FindMyPhoneCancelMessage.class),
MUSIC_CONTROL(5041, MusicControlMessage.class),
MUSIC_CONTROL_CAPABILITIES(5042, MusicControlCapabilitiesMessage.class),
PROTOBUF_REQUEST(5043, ProtobufMessage.class),