From 12a5b53f006e85cbf15f19549086247357028c01 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Tue, 9 Feb 2016 00:49:42 +0100 Subject: [PATCH] Pebble: Merge DatalogHandler and DataLog session Also: - pass the length of the payload and not of the whole datalog buffer to handleMessage(), simplifying DatalogSessionHealth::handleMessage() --- .../devices/pebble/DatalogHandler.java | 24 ------------ .../devices/pebble/DatalogSession.java | 10 +++++ ...rHealth.java => DatalogSessionHealth.java} | 24 +++++------- .../devices/pebble/PebbleProtocol.java | 38 ++++--------------- 4 files changed, 28 insertions(+), 68 deletions(-) delete mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandler.java rename app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/{DatalogHandlerHealth.java => DatalogSessionHealth.java} (84%) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandler.java deleted file mode 100644 index f4f251a5..00000000 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandler.java +++ /dev/null @@ -1,24 +0,0 @@ -package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; - -import java.nio.ByteBuffer; - -public class DatalogHandler { - protected final PebbleProtocol mPebbleProtocol; - protected final int mTag; - - DatalogHandler(int tag, PebbleProtocol pebbleProtocol) { - mTag = tag; - mPebbleProtocol = pebbleProtocol; - } - - public int getTag() { - return mTag; - } - - public String getTagInfo() { return null; } - - public boolean handleMessage(ByteBuffer datalogMessage, int length) { - return true;//ack the datalog transmission - } - -} \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java index 30ac869c..b17e7259 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java @@ -1,5 +1,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; +import java.nio.ByteBuffer; import java.util.UUID; class DatalogSession { @@ -8,6 +9,7 @@ class DatalogSession { final UUID uuid; final byte item_type; final short item_size; + String taginfo = "(unknown)"; DatalogSession(byte id, UUID uuid, int tag, byte item_type, short item_size) { this.id = id; @@ -16,4 +18,12 @@ class DatalogSession { this.item_type = item_type; this.item_size = item_size; } + + boolean handleMessage (ByteBuffer buf, int length) { + return true; + } + + String getTaginfo() { + return taginfo; + } } \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandlerHealth.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java similarity index 84% rename from app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandlerHealth.java rename to app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java index 58e37129..1def3cdb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogHandlerHealth.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import java.nio.ByteBuffer; import java.util.Date; +import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; @@ -19,25 +20,21 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.util.GB; -public class DatalogHandlerHealth extends DatalogHandler { +public class DatalogSessionHealth extends DatalogSession { - private final int preambleLength = 10; + private final int preambleLength = 10; // FIXME: this is 14 but if would break the code if corrected private final int packetLength = 99; - private static final Logger LOG = LoggerFactory.getLogger(DatalogHandlerHealth.class); + private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealth.class); - public DatalogHandlerHealth(int tag, PebbleProtocol pebbleProtocol) { - super(tag, pebbleProtocol); - } - - @Override - public String getTagInfo() { - return "(health)"; + public DatalogSessionHealth(byte id, UUID uuid, int tag, byte item_type, short item_size) { + super(id, uuid, tag, item_type, item_size); + taginfo = "(health)"; } @Override public boolean handleMessage(ByteBuffer datalogMessage, int length) { - LOG.info(GB.hexdump(datalogMessage.array(), preambleLength, length-preambleLength)); + LOG.info(GB.hexdump(datalogMessage.array(), datalogMessage.position(), length)); int unknownPacketPreamble, timestamp; byte unknownC, recordLength, recordNum; @@ -47,11 +44,10 @@ public class DatalogHandlerHealth extends DatalogHandler { byte steps, orientation; //possibly short intensity; // possibly - if (0 == ((length - preambleLength) % packetLength)) { // one datalog message may contain several packets - for (int packet = 0; packet < ((length - preambleLength) / packetLength); packet++) { + if (0 == (length % packetLength)) { // one datalog message may contain several packets + for (int packet = 0; packet < (length / packetLength); packet++) { beginOfPacketPosition = preambleLength + packet*packetLength; datalogMessage.position(beginOfPacketPosition); - unknownPacketPreamble = datalogMessage.getInt(); unknownA = datalogMessage.getShort(); timestamp = datalogMessage.getInt(); unknownC = datalogMessage.get(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index 1ab21c4c..41cfe311 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -37,7 +37,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol; -import nodomain.freeyourgadget.gadgetbridge.util.GB; public class PebbleProtocol extends GBDeviceProtocol { @@ -366,12 +365,6 @@ public class PebbleProtocol extends GBDeviceProtocol { } - private static final Map mDatalogHandlers = new HashMap<>(); - - { - mDatalogHandlers.put(81, new DatalogHandlerHealth(81, PebbleProtocol.this)); - } - private final HashMap mDatalogSessions = new HashMap<>(); private static byte[] encodeSimpleMessage(short endpoint, byte command) { @@ -1805,34 +1798,14 @@ public class PebbleProtocol extends GBDeviceProtocol { LOG.info("DATALOG TIMEOUT. id=" + (id & 0xff) + " - ignoring"); return null; case DATALOG_SENDDATA: - boolean doHexdump = false; buf.order(ByteOrder.LITTLE_ENDIAN); int items_left = buf.getInt(); int crc = buf.getInt(); DatalogSession datalogSession = mDatalogSessions.get(id); LOG.info("DATALOG SENDDATA. id=" + (id & 0xff) + ", items_left=" + items_left + ", total length=" + (length - 10)); if (datalogSession != null) { - String taginfo = ""; - if (datalogSession.uuid.equals(UUID_ZERO)) { - DatalogHandler datalogHandler = mDatalogHandlers.get(datalogSession.tag); - if (datalogHandler != null) { - taginfo = datalogHandler.getTagInfo(); - ack = datalogHandler.handleMessage(buf, length); - } else { - if (datalogSession.tag >= 78 && datalogSession.tag <= 80) { - taginfo = "(analytics?)"; - } else if (datalogSession.tag == 83) { - taginfo = "(health?)"; - doHexdump = true; - } else { - taginfo = "(unknown)"; - } - } - } - LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag + taginfo + ", item_size=" + datalogSession.item_size + ", item_type=" + datalogSession.item_type); - } - if (doHexdump) { - LOG.info(GB.hexdump(buf.array(), buf.position(), length - buf.position())); + LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag + datalogSession.getTaginfo() + ", item_size=" + datalogSession.item_size + ", item_type=" + datalogSession.item_type); + ack = datalogSession.handleMessage(buf, length - 10); } break; case DATALOG_OPENSESSION: @@ -1847,7 +1820,12 @@ public class PebbleProtocol extends GBDeviceProtocol { short item_size = buf.get(); LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag=" + log_tag + ", item_type=" + item_type + ", item_size=" + item_size); if (!mDatalogSessions.containsKey(id)) { - mDatalogSessions.put(id, new DatalogSession(id, uuid, log_tag, item_type, item_size)); + if (uuid.equals(UUID_ZERO) && log_tag == 81) { + mDatalogSessions.put(id, new DatalogSessionHealth(id, uuid, log_tag, item_type, item_size)); + } + else { + mDatalogSessions.put(id, new DatalogSession(id, uuid, log_tag, item_type, item_size)); + } } break; case DATALOG_CLOSE: