From cde10a6dce2c50cc5df70c78dd4bfb3e05cfdb6c Mon Sep 17 00:00:00 2001 From: Patrick Rudolph Date: Mon, 2 Sep 2024 18:03:57 +0200 Subject: [PATCH] Huawei: GetHiChainRequest: Add error handling Parse the error code and throw an exception instead of doing strange things due to invalid step ID being used. To investigate issue #4061. Signed-off-by: Patrick Rudolph --- .../gadgetbridge/devices/huawei/packets/DeviceConfig.java | 4 ++++ .../service/devices/huawei/requests/GetHiChainRequest.java | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java index 796668ba1..066758119 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/DeviceConfig.java @@ -1225,6 +1225,7 @@ public class DeviceConfig { public Step2Data step2Data; public Step3Data step3Data; public Step4Data step4Data; + public int errorCode = 0; public Response(ParamsProvider paramsProvider) { super(paramsProvider); @@ -1253,6 +1254,9 @@ public class DeviceConfig { this.step = 0x03; this.step3Data = new Step3Data(jsonPayload); } + if (jsonPayload.has("errorCode")) { + this.errorCode = jsonPayload.getInt("errorCode"); + } } catch (JSONException e) { throw new JsonException("", e); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetHiChainRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetHiChainRequest.java index fab47b303..c65727146 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetHiChainRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetHiChainRequest.java @@ -163,9 +163,10 @@ public class GetHiChainRequest extends Request { if (!(receivedPacket instanceof HiChain.Response)) throw new ResponseTypeMismatchException(receivedPacket, HiChain.Response.class); - // TODO: handle failure codes - HiChain.Response response = (HiChain.Response)receivedPacket; + if (response.errorCode != 0) { + throw new ResponseParseException("Got errorCode " + response.errorCode); + } step = response.step; LOG.debug("Response operationCode: " + operationCode + " - step: " + step);