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()
here
Andreas Shimokawa 2016-02-09 00:49:42 +01:00
parent b01a517813
commit 12a5b53f00
4 changed files with 28 additions and 68 deletions

View File

@ -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
}
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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<Integer, DatalogHandler> mDatalogHandlers = new HashMap<>();
{
mDatalogHandlers.put(81, new DatalogHandlerHealth(81, PebbleProtocol.this));
}
private final HashMap<Byte, DatalogSession> 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: