From 72dcb3250e01d82136de77795d7dfc74ae21bbce Mon Sep 17 00:00:00 2001 From: "Martin.JM" Date: Mon, 10 Jun 2024 14:06:52 +0200 Subject: [PATCH] Hauwei: Extend debug request to allow setting the sliced flag --- .../devices/huawei/HuaweiPacket.java | 4 ++ .../devices/huawei/requests/DebugRequest.java | 32 ++++++++++++--- .../huawei/TestDebugRequestParser.java | 41 +++++++++++++------ 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java index 289745423..7f784b622 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiPacket.java @@ -788,6 +788,10 @@ public class HuaweiPacket { this.isEncrypted = b; } + public void setSliced(boolean b) { + this.isSliced = b; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/DebugRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/DebugRequest.java index 6a3e7e780..fb90dc02e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/DebugRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/DebugRequest.java @@ -48,16 +48,18 @@ public class DebugRequest extends Request { } /* - DebugString := [service_id] "," [command id] "," [encryptflag] ("," [tlv])* + DebugString := [service_id] "," [command id] "," [encryptflag] "," [sliceflag] ("," [tlv])* service_id := int | "0x" hex command_id := int | "0x" hex - encryptflag := "true" + encryptflag := [boolean] + sliceflag := [boolean] + boolean := "true" | "t" | "false" | "f" - tlv := "(" [tag] "," [typevalue] ")" + tlv := "(" [tag] "," [typevalue] ")" ("," [tlv])* tag := int | "0x" hex typevalue := [type] [value] @@ -100,10 +102,8 @@ public class DebugRequest extends Request { current = nextComma + 1; nextComma = debugString.indexOf(',', current); - if (debugString.length() - current < 2) + if (nextComma < 1 || debugString.length() - current < 2) throw new RequestCreationException("Invalid debug command"); - if (nextComma < 0) - nextComma = debugString.length(); // For no TLVs switch (debugString.substring(current, nextComma)) { case "true": @@ -118,6 +118,26 @@ public class DebugRequest extends Request { throw new RequestCreationException("Boolean is not a boolean"); } + current = nextComma + 1; + nextComma = debugString.indexOf(',', current); + if (debugString.length() - current < 1) + throw new RequestCreationException("Invalid debug command"); + if (nextComma < 0) + nextComma = debugString.length(); // For no TLVs + + switch (debugString.substring(current, nextComma)) { + case "true": + case "t": + packet.setSliced(true); + break; + case "false": + case "f": + packet.setSliced(false); + break; + default: + throw new RequestCreationException("Boolean is not a boolean"); + } + current = nextComma + 1; if (current < debugString.length()) { diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/TestDebugRequestParser.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/TestDebugRequestParser.java index 1b20d1f89..d3e0f53e8 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/TestDebugRequestParser.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/TestDebugRequestParser.java @@ -181,7 +181,22 @@ public class TestDebugRequestParser { expected.setEncryption(false); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false"); + + Assert.assertEquals(expected, packet); + } + + @Test + public void emptyPacketShortBooleans() throws Request.RequestCreationException { + DebugRequest debugRequest = new DebugRequest(supportProvider); + + HuaweiPacket expected = new HuaweiPacket(supportProvider.getParamsProvider()); + expected.serviceId = 1; + expected.commandId = 1; + expected.setEncryption(false); + expected.complete = true; + + HuaweiPacket packet = debugRequest.parseDebugString("1,1,f,f"); Assert.assertEquals(expected, packet); } @@ -197,7 +212,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1)); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,/)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,/)"); Assert.assertEquals(expected, packet); } @@ -213,7 +228,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, (byte) 1)); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,B1)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,B1)"); Assert.assertEquals(expected, packet); } @@ -229,7 +244,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, (short) 1)); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,S1)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,S1)"); Assert.assertEquals(expected, packet); } @@ -245,7 +260,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, (int) 1)); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,I1)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,I1)"); Assert.assertEquals(expected, packet); } @@ -261,7 +276,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, true)); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,b1)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,b1)"); Assert.assertEquals(expected, packet); } @@ -277,7 +292,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, new byte[] {(byte) 0xCA, (byte) 0xFE})); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,aCAFE)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,aCAFE)"); Assert.assertEquals(expected, packet); } @@ -293,7 +308,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, new byte[] {0x79, 0x65, 0x73})); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(1,-yes)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(1,-yes)"); Assert.assertEquals(expected, packet); } @@ -309,7 +324,7 @@ public class TestDebugRequestParser { expected.setTlv(new HuaweiTLV().put(1, new byte[] {0x79, 0x65, 0x73})); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("0x01,0x1,false,(0x01,-yes)"); + HuaweiPacket packet = debugRequest.parseDebugString("0x01,0x1,false,false,(0x01,-yes)"); Assert.assertEquals(expected, packet); } @@ -324,7 +339,7 @@ public class TestDebugRequestParser { expected.setEncryption(false); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("0xff,255,false"); + HuaweiPacket packet = debugRequest.parseDebugString("0xff,255,false,false"); Assert.assertEquals(expected, packet); } @@ -344,7 +359,7 @@ public class TestDebugRequestParser { )); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(129,(1,/),(2,/))"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(129,(1,/),(2,/))"); Assert.assertEquals(expected, packet); } @@ -367,7 +382,7 @@ public class TestDebugRequestParser { )); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(129,(129,(129,(1,/))))"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(129,(129,(129,(1,/))))"); Assert.assertEquals(expected, packet); } @@ -389,7 +404,7 @@ public class TestDebugRequestParser { ); expected.complete = true; - HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,(129,(1,/),(2,b1)),(1,b1)"); + HuaweiPacket packet = debugRequest.parseDebugString("1,1,false,false,(129,(1,/),(2,b1)),(1,b1)"); Assert.assertEquals(expected, packet); }