1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00

Garmin: fix protobuf chunked fragments handling

remove chunk from the map once it is complete if it is incoming, fix the removal of outgoing chunks
also return a warn log message if the protobuf status is not OK.
This commit is contained in:
Daniele Gobbetti 2024-05-20 18:00:40 +02:00
parent 5224244f74
commit 9c4da444cf
2 changed files with 20 additions and 12 deletions

View File

@ -78,6 +78,7 @@ public class ProtocolBufferHandler implements MessageHandler {
if (protobufFragment.isComplete()) { //message is now complete if (protobufFragment.isComplete()) { //message is now complete
LOG.info("Received protobuf message #{}, {}B: {}", message.getRequestId(), protobufFragment.totalLength, GB.hexdump(protobufFragment.fragmentBytes, 0, protobufFragment.totalLength)); LOG.info("Received protobuf message #{}, {}B: {}", message.getRequestId(), protobufFragment.totalLength, GB.hexdump(protobufFragment.fragmentBytes, 0, protobufFragment.totalLength));
chunkedFragmentsMap.remove(message.getRequestId());
final GdiSmartProto.Smart smart; final GdiSmartProto.Smart smart;
try { try {
@ -127,22 +128,21 @@ public class ProtocolBufferHandler implements MessageHandler {
} }
private ProtobufMessage processIncoming(ProtobufStatusMessage statusMessage) { private ProtobufMessage processIncoming(ProtobufStatusMessage statusMessage) {
LOG.info("Processing protobuf status message #{}@{}: status={}, error={}", statusMessage.getRequestId(), statusMessage.getDataOffset(), statusMessage.getProtobufChunkStatus(), statusMessage.getProtobufStatusCode());
//TODO: check status and react accordingly, right now we blindly proceed to next chunk //TODO: check status and react accordingly, right now we blindly proceed to next chunk
if (statusMessage.isOK()) { if (statusMessage.isOK()) {
DataTransferHandler.onDataChunkSuccessfullyReceived(statusMessage.getRequestId()); DataTransferHandler.onDataChunkSuccessfullyReceived(statusMessage.getRequestId());
}
if (chunkedFragmentsMap.containsKey(statusMessage.getRequestId()) && statusMessage.isOK()) { if (chunkedFragmentsMap.containsKey(statusMessage.getRequestId())) {
final ProtobufFragment protobufFragment = chunkedFragmentsMap.get(statusMessage.getRequestId()); final ProtobufFragment protobufFragment = chunkedFragmentsMap.get(statusMessage.getRequestId());
LOG.debug("Protobuf message #{} found in queue: {}", statusMessage.getRequestId(), GB.hexdump(protobufFragment.fragmentBytes)); LOG.debug("Protobuf message #{} found in queue: {}", statusMessage.getRequestId(), GB.hexdump(protobufFragment.fragmentBytes));
if (protobufFragment.totalLength <= (statusMessage.getDataOffset() + maxChunkSize)) { if (protobufFragment.totalLength <= (statusMessage.getDataOffset() + maxChunkSize)) {
chunkedFragmentsMap.remove(protobufFragment); chunkedFragmentsMap.remove(statusMessage.getRequestId());
return null; return null;
} }
return protobufFragment.getNextChunk(statusMessage); return protobufFragment.getNextChunk(statusMessage);
} }
}
return null; return null;
} }
@ -403,7 +403,7 @@ public class ProtocolBufferHandler implements MessageHandler {
private ProtobufMessage prepareProtobufMessage(byte[] bytes, GFDIMessage.GarminMessage garminMessage, int requestId) { private ProtobufMessage prepareProtobufMessage(byte[] bytes, GFDIMessage.GarminMessage garminMessage, int requestId) {
if (bytes == null || bytes.length == 0) if (bytes == null || bytes.length == 0)
return null; return null;
LOG.info("Preparing protobuf message. Type{}, #{}, {}B: {}", garminMessage, 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) { if (bytes.length > maxChunkSize) {
chunkedFragmentsMap.put(requestId, new ProtobufFragment(bytes)); chunkedFragmentsMap.put(requestId, new ProtobufFragment(bytes));

View File

@ -34,7 +34,15 @@ public class ProtobufStatusMessage extends GFDIStatusMessage {
final ProtobufChunkStatus protobufStatus = ProtobufChunkStatus.fromCode(reader.readByte()); final ProtobufChunkStatus protobufStatus = ProtobufChunkStatus.fromCode(reader.readByte());
final ProtobufStatusCode error = ProtobufStatusCode.fromCode(reader.readByte()); final ProtobufStatusCode error = ProtobufStatusCode.fromCode(reader.readByte());
return new ProtobufStatusMessage(garminMessage, status, requestID, dataOffset, protobufStatus, error, false); final ProtobufStatusMessage statusMessage = new ProtobufStatusMessage(garminMessage, status, requestID, dataOffset, protobufStatus, error, false);
if (statusMessage.isOK()) {
LOG.info("Processing protobuf status message #{}@{}: status={}, error={}", statusMessage.getRequestId(), statusMessage.getDataOffset(), statusMessage.getProtobufChunkStatus(), statusMessage.getProtobufStatusCode());
} else {
LOG.warn("Processing protobuf status message #{}@{}: status={}, error={}", statusMessage.getRequestId(), statusMessage.getDataOffset(), statusMessage.getProtobufChunkStatus(), statusMessage.getProtobufStatusCode());
}
return statusMessage;
} }
public int getDataOffset() { public int getDataOffset() {