Search in sources :

Example 1 with GBDeviceEventDataLogging

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging in project Gadgetbridge by Freeyourgadget.

the class PebbleProtocol method decodeDatalog.

private GBDeviceEvent[] decodeDatalog(ByteBuffer buf, short length) {
    byte command = buf.get();
    byte id = buf.get();
    GBDeviceEvent[] devEvtsDataLogging = null;
    switch(command) {
        case DATALOG_TIMEOUT:
            LOG.info("DATALOG TIMEOUT. id=" + (id & 0xff) + " - ignoring");
            return null;
        case DATALOG_SENDDATA:
            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) {
                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) {
                    devEvtsDataLogging = datalogSession.handleMessageForPebbleKit(buf, length - 10);
                } else {
                    devEvtsDataLogging = datalogSession.handleMessage(buf, length - 10);
                }
            }
            break;
        case DATALOG_OPENSESSION:
            UUID uuid = getUUID(buf);
            buf.order(ByteOrder.LITTLE_ENDIAN);
            int timestamp = buf.getInt();
            int log_tag = buf.getInt();
            byte item_type = buf.get();
            short item_size = buf.getShort();
            LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag=" + log_tag + ", item_type=" + item_type + ", itemSize=" + item_size);
            if (!mDatalogSessions.containsKey(id)) {
                if (uuid.equals(UUID_ZERO) && log_tag == 78) {
                    mDatalogSessions.put(id, new DatalogSessionAnalytics(id, uuid, timestamp, log_tag, item_type, item_size, getDevice()));
                } else if (uuid.equals(UUID_ZERO) && log_tag == 81) {
                    mDatalogSessions.put(id, new DatalogSessionHealthSteps(id, uuid, timestamp, log_tag, item_type, item_size, getDevice()));
                } else if (uuid.equals(UUID_ZERO) && log_tag == 83) {
                    mDatalogSessions.put(id, new DatalogSessionHealthSleep(id, uuid, timestamp, log_tag, item_type, item_size, getDevice()));
                } else if (uuid.equals(UUID_ZERO) && log_tag == 84) {
                    mDatalogSessions.put(id, new DatalogSessionHealthOverlayData(id, uuid, timestamp, log_tag, item_type, item_size, getDevice()));
                } else if (uuid.equals(UUID_ZERO) && log_tag == 85) {
                    mDatalogSessions.put(id, new DatalogSessionHealthHR(id, uuid, timestamp, log_tag, item_type, item_size, getDevice()));
                } else {
                    mDatalogSessions.put(id, new DatalogSession(id, uuid, timestamp, log_tag, item_type, item_size));
                }
            }
            devEvtsDataLogging = new GBDeviceEvent[] { null };
            break;
        case DATALOG_CLOSE:
            LOG.info("DATALOG_CLOSE. id=" + (id & 0xff));
            datalogSession = mDatalogSessions.get(id);
            if (datalogSession != null) {
                if (!datalogSession.uuid.equals(UUID_ZERO) && datalogSession.getClass().equals(DatalogSession.class) && mEnablePebbleKit) {
                    GBDeviceEventDataLogging dataLogging = new GBDeviceEventDataLogging();
                    dataLogging.command = GBDeviceEventDataLogging.COMMAND_FINISH_SESSION;
                    dataLogging.appUUID = datalogSession.uuid;
                    dataLogging.tag = datalogSession.tag;
                    devEvtsDataLogging = new GBDeviceEvent[] { dataLogging, null };
                }
                if (datalogSession.uuid.equals(UUID_ZERO) && (datalogSession.tag == 81 || datalogSession.tag == 83 || datalogSession.tag == 84)) {
                    GB.signalActivityDataFinish();
                }
                mDatalogSessions.remove(id);
            }
            break;
        default:
            LOG.info("unknown DATALOG command: " + (command & 0xff));
            break;
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    if (devEvtsDataLogging != null) {
        // append ack
        LOG.info("sending ACK (0x85)");
        sendBytes.encodedBytes = encodeDatalog(id, DATALOG_ACK);
        devEvtsDataLogging[devEvtsDataLogging.length - 1] = sendBytes;
    } else {
        LOG.info("sending NACK (0x86)");
        sendBytes.encodedBytes = encodeDatalog(id, DATALOG_NACK);
        devEvtsDataLogging = new GBDeviceEvent[] { sendBytes };
    }
    return devEvtsDataLogging;
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) GBDeviceEventDataLogging(nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) UUID(java.util.UUID)

Example 2 with GBDeviceEventDataLogging

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging in project Gadgetbridge by Freeyourgadget.

the class DatalogSession method handleMessageForPebbleKit.

GBDeviceEvent[] handleMessageForPebbleKit(ByteBuffer buf, int length) {
    if (0 != (length % itemSize)) {
        LOG.warn("invalid length");
        return null;
    }
    int packetCount = length / itemSize;
    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++) {
        switch(itemType) {
            case PebbleProtocol.TYPE_BYTEARRAY:
                byte[] itemData = new byte[itemSize];
                buf.get(itemData);
                dataLogging.data[i] = itemData;
                break;
            case PebbleProtocol.TYPE_UINT:
                dataLogging.data[i] = buf.getInt() & 0xffffffffL;
                break;
            case PebbleProtocol.TYPE_INT:
                dataLogging.data[i] = buf.getInt();
                break;
        }
    }
    return new GBDeviceEvent[] { dataLogging, null };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) GBDeviceEventDataLogging(nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging)

Example 3 with GBDeviceEventDataLogging

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging in project Gadgetbridge by Freeyourgadget.

the class PebbleIoThread method evaluateGBDeviceEventPebble.

// FIXME: parts are supposed to be generic code
private boolean evaluateGBDeviceEventPebble(GBDeviceEvent deviceEvent) {
    if (deviceEvent instanceof GBDeviceEventVersionInfo) {
        if (prefs.getBoolean("datetime_synconconnect", true)) {
            LOG.info("syncing time");
            write(mPebbleProtocol.encodeSetTime());
        }
        write(mPebbleProtocol.encodeEnableAppLogs(prefs.getBoolean("pebble_enable_applogs", false)));
        write(mPebbleProtocol.encodeReportDataLogSessions());
        gbDevice.setState(GBDevice.State.INITIALIZED);
        return false;
    } else if (deviceEvent instanceof GBDeviceEventAppManagement) {
        GBDeviceEventAppManagement appMgmt = (GBDeviceEventAppManagement) deviceEvent;
        switch(appMgmt.type) {
            case DELETE:
                // right now on the Pebble we also receive this on a failed/successful installation ;/
                switch(appMgmt.event) {
                    case FAILURE:
                        if (mIsInstalling) {
                            if (mInstallState == PebbleAppInstallState.WAIT_SLOT) {
                                // get the free slot
                                writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
                            } else {
                                finishInstall(true);
                            }
                        } else {
                            LOG.info("failure removing app");
                        }
                        break;
                    case SUCCESS:
                        if (mIsInstalling) {
                            if (mInstallState == PebbleAppInstallState.WAIT_SLOT) {
                                // get the free slot
                                writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
                            } else {
                                finishInstall(false);
                                // refresh app list
                                write(mPebbleProtocol.encodeAppInfoReq());
                            }
                        } else {
                            LOG.info("successfully removed app");
                            write(mPebbleProtocol.encodeAppInfoReq());
                        }
                        break;
                    default:
                        break;
                }
                break;
            case INSTALL:
                switch(appMgmt.event) {
                    case FAILURE:
                        // TODO: report to Installer
                        LOG.info("failure installing app");
                        finishInstall(true);
                        break;
                    case SUCCESS:
                        setToken(appMgmt.token);
                        break;
                    case REQUEST:
                        LOG.info("APPFETCH request: " + appMgmt.uuid + " / " + appMgmt.token);
                        try {
                            installApp(Uri.fromFile(new File(PebbleUtils.getPbwCacheDir(), appMgmt.uuid.toString() + ".pbw")), appMgmt.token);
                        } catch (IOException e) {
                            LOG.error("Error installing app: " + e.getMessage(), e);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case START:
                LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid);
                if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
                    if (mPebbleProtocol.hasAppMessageHandler(appMgmt.uuid)) {
                        WebViewSingleton.getInstance().stopJavascriptInterface();
                    } else {
                        WebViewSingleton.getInstance().runJavascriptInterface(gbDevice, appMgmt.uuid);
                    }
                }
                mPebbleActiveAppTracker.markAppOpened(appMgmt.uuid);
                break;
            case STOP:
                mPebbleActiveAppTracker.markAppClosed(appMgmt.uuid);
                break;
            default:
                break;
        }
        return true;
    } else if (deviceEvent instanceof GBDeviceEventAppInfo) {
        LOG.info("Got event for APP_INFO");
        GBDeviceEventAppInfo appInfoEvent = (GBDeviceEventAppInfo) deviceEvent;
        setInstallSlot(appInfoEvent.freeSlot);
        return false;
    } else if (deviceEvent instanceof GBDeviceEventAppMessage) {
        if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
            sendAppMessageJS((GBDeviceEventAppMessage) deviceEvent);
        }
        if (mEnablePebblekit) {
            LOG.info("Got AppMessage event");
            if (mPebbleKitSupport != null && ((GBDeviceEventAppMessage) deviceEvent).type == GBDeviceEventAppMessage.TYPE_APPMESSAGE) {
                mPebbleKitSupport.sendAppMessageIntent((GBDeviceEventAppMessage) deviceEvent);
            }
        }
    } else if (deviceEvent instanceof GBDeviceEventDataLogging) {
        if (mEnablePebblekit) {
            LOG.info("Got Datalogging event");
            if (mPebbleKitSupport != null) {
                mPebbleKitSupport.sendDataLoggingIntent((GBDeviceEventDataLogging) deviceEvent);
            }
        }
    }
    return false;
}
Also used : GBDeviceEventAppManagement(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement) GBDeviceEventAppMessage(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage) GBDeviceEventVersionInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo) GBDeviceEventAppInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo) IOException(java.io.IOException) GBDeviceEventDataLogging(nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging) File(java.io.File)

Aggregations

GBDeviceEventDataLogging (nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging)3 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)2 File (java.io.File)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 GBDeviceEventAppInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo)1 GBDeviceEventAppManagement (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement)1 GBDeviceEventAppMessage (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage)1 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)1 GBDeviceEventVersionInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo)1