Pebble: some cleanups and simplifications for datalogging via PebbleKit

master
Andreas Shimokawa 2017-02-20 22:09:00 +01:00
parent ad9cfae6f9
commit 2dbda6138b
4 changed files with 43 additions and 36 deletions

View File

@ -13,5 +13,5 @@ public class GBDeviceEventDataLogging extends GBDeviceEvent {
public long timestamp; public long timestamp;
public long tag; public long tag;
public byte pebbleDataType; public byte pebbleDataType;
public Object data; public Object[] data;
} }

View File

@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging; import nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging;
class DatalogSession { class DatalogSession {
@ -37,38 +36,43 @@ class DatalogSession {
return taginfo; return taginfo;
} }
GBDeviceEvent[] handleMessageForPebbleKit(ByteBuffer buf, int length) { GBDeviceEventDataLogging handleMessageForPebbleKit(ByteBuffer buf, int length) {
if (0 != (length % itemSize)) { if (0 != (length % itemSize)) {
LOG.warn("invalid length"); LOG.warn("invalid length");
return null; return null;
} }
int packetCount = length / itemSize; int packetCount = length / itemSize;
GBDeviceEvent[] gbDeviceEvents = new GBDeviceEvent[packetCount + 1]; // pad for ack
if (packetCount <= 0) {
LOG.warn("invalid number of datalog elements");
return null;
}
GBDeviceEventDataLogging dataLogging = new GBDeviceEventDataLogging();
dataLogging.command = GBDeviceEventDataLogging.COMMAND_RECEIVE_DATA;
dataLogging.appUUID = uuid;
dataLogging.timestamp = timestamp & 0xffffffffL;
dataLogging.tag = tag;
dataLogging.pebbleDataType = itemType;
dataLogging.data = new Object[packetCount];
for (int i = 0; i < packetCount; i++) { for (int i = 0; i < packetCount; i++) {
GBDeviceEventDataLogging dataLogging = new GBDeviceEventDataLogging();
switch (itemType) { switch (itemType) {
case PebbleProtocol.TYPE_BYTEARRAY: case PebbleProtocol.TYPE_BYTEARRAY:
byte[] itemData = new byte[itemSize]; byte[] itemData = new byte[itemSize];
buf.get(itemData); buf.get(itemData);
dataLogging.data = itemData; dataLogging.data[i] = itemData;
break; break;
case PebbleProtocol.TYPE_UINT: case PebbleProtocol.TYPE_UINT:
dataLogging.data = buf.getInt() & 0xffffffffL; dataLogging.data[i] = buf.getInt() & 0xffffffffL;
break; break;
case PebbleProtocol.TYPE_INT: case PebbleProtocol.TYPE_INT:
dataLogging.data = buf.getInt(); dataLogging.data[i] = buf.getInt();
break; break;
} }
dataLogging.command = GBDeviceEventDataLogging.COMMAND_RECEIVE_DATA;
dataLogging.appUUID = uuid;
dataLogging.timestamp = timestamp & 0xffffffffL;
dataLogging.tag = tag;
dataLogging.pebbleDataType = itemType;
gbDeviceEvents[i] = dataLogging;
} }
return gbDeviceEvents; return dataLogging;
} }
} }

View File

@ -140,30 +140,31 @@ class PebbleKitSupport {
switch (dataLogging.command) { switch (dataLogging.command) {
case GBDeviceEventDataLogging.COMMAND_RECEIVE_DATA: case GBDeviceEventDataLogging.COMMAND_RECEIVE_DATA:
intent.setAction(PEBBLEKIT_ACTION_DL_RECEIVE_DATA_NEW); intent.setAction(PEBBLEKIT_ACTION_DL_RECEIVE_DATA_NEW);
intent.putExtra("pbl_data_id", dataLogTransactionId++);
intent.putExtra("pbl_data_type", dataLogging.pebbleDataType); intent.putExtra("pbl_data_type", dataLogging.pebbleDataType);
switch (dataLogging.pebbleDataType) { for (Object dataObject : dataLogging.data) {
case PebbleProtocol.TYPE_BYTEARRAY: intent.putExtra("pbl_data_id", dataLogTransactionId++);
intent.putExtra("pbl_data_object", Base64.encodeToString((byte[]) dataLogging.data, Base64.NO_WRAP)); switch (dataLogging.pebbleDataType) {
break; case PebbleProtocol.TYPE_BYTEARRAY:
case PebbleProtocol.TYPE_UINT: intent.putExtra("pbl_data_object", Base64.encodeToString((byte[]) dataObject, Base64.NO_WRAP));
intent.putExtra("pbl_data_object", (Long) dataLogging.data); break;
break; case PebbleProtocol.TYPE_UINT:
case PebbleProtocol.TYPE_INT: intent.putExtra("pbl_data_object", (Long) dataObject);
intent.putExtra("pbl_data_object", (Integer) dataLogging.data); break;
break; case PebbleProtocol.TYPE_INT:
intent.putExtra("pbl_data_object", (Integer) dataObject);
break;
}
LOG.info("broadcasting datalogging to uuid " + dataLogging.appUUID + " tag: " + dataLogging.tag + "transaction id: " + dataLogTransactionId + " type: " + dataLogging.pebbleDataType);
mContext.sendBroadcast(intent);
} }
LOG.info("broadcasting datalogging to uuid " + dataLogging.appUUID + " tag: " + dataLogging.tag + "transaction id: " + dataLogTransactionId + " type: " + dataLogging.pebbleDataType);
break; break;
case GBDeviceEventDataLogging.COMMAND_FINISH_SESSION: case GBDeviceEventDataLogging.COMMAND_FINISH_SESSION:
intent.setAction(PEBBLEKIT_ACTION_DL_FINISH_SESSION); intent.setAction(PEBBLEKIT_ACTION_DL_FINISH_SESSION);
LOG.info("broadcasting datalogging finish session to uuid " + dataLogging.appUUID + " tag: " + dataLogging.tag); LOG.info("broadcasting datalogging finish session to uuid " + dataLogging.appUUID + " tag: " + dataLogging.tag);
mContext.sendBroadcast(intent);
break; break;
default: default:
LOG.warn("invalid datalog command"); LOG.warn("invalid datalog command");
return;
} }
mContext.sendBroadcast(intent);
} }
} }

View File

@ -2218,7 +2218,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
boolean ack = true; boolean ack = true;
byte command = buf.get(); byte command = buf.get();
byte id = buf.get(); byte id = buf.get();
GBDeviceEvent[] devEvts = new GBDeviceEvent[1]; GBDeviceEventDataLogging devEvtDataLogging = null;
switch (command) { switch (command) {
case DATALOG_TIMEOUT: case DATALOG_TIMEOUT:
LOG.info("DATALOG TIMEOUT. id=" + (id & 0xff) + " - ignoring"); LOG.info("DATALOG TIMEOUT. id=" + (id & 0xff) + " - ignoring");
@ -2232,7 +2232,10 @@ public class PebbleProtocol extends GBDeviceProtocol {
if (datalogSession != null) { if (datalogSession != null) {
LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag + datalogSession.getTaginfo() + ", itemSize=" + datalogSession.itemSize + ", itemType=" + datalogSession.itemType); LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag + datalogSession.getTaginfo() + ", itemSize=" + datalogSession.itemSize + ", itemType=" + datalogSession.itemType);
if (!datalogSession.uuid.equals(UUID_ZERO) && datalogSession.getClass().equals(DatalogSession.class) && mEnablePebbleKit) { if (!datalogSession.uuid.equals(UUID_ZERO) && datalogSession.getClass().equals(DatalogSession.class) && mEnablePebbleKit) {
devEvts = datalogSession.handleMessageForPebbleKit(buf, length - 10); devEvtDataLogging = datalogSession.handleMessageForPebbleKit(buf, length - 10);
if (devEvtDataLogging == null) {
ack = false;
}
} else { } else {
ack = datalogSession.handleMessage(buf, length - 10); ack = datalogSession.handleMessage(buf, length - 10);
} }
@ -2269,7 +2272,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
dataLogging.command = GBDeviceEventDataLogging.COMMAND_FINISH_SESSION; dataLogging.command = GBDeviceEventDataLogging.COMMAND_FINISH_SESSION;
dataLogging.appUUID = datalogSession.uuid; dataLogging.appUUID = datalogSession.uuid;
dataLogging.tag = datalogSession.tag; dataLogging.tag = datalogSession.tag;
devEvts = new GBDeviceEvent[]{dataLogging, null}; devEvtDataLogging = dataLogging;
} }
mDatalogSessions.remove(id); mDatalogSessions.remove(id);
} }
@ -2287,8 +2290,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
sendBytes.encodedBytes = encodeDatalog(id, DATALOG_NACK); sendBytes.encodedBytes = encodeDatalog(id, DATALOG_NACK);
} }
// append ack/nack // append ack/nack
devEvts[devEvts.length - 1] = sendBytes; return new GBDeviceEvent[]{devEvtDataLogging, sendBytes};
return devEvts;
} }
private GBDeviceEvent decodeAppReorder(ByteBuffer buf) { private GBDeviceEvent decodeAppReorder(ByteBuffer buf) {