summaryrefslogtreecommitdiff
path: root/daemon/appmsgmanager.cpp
blob: 9eb948eecb8a272211c138b4cb583b0f2c3057ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "appmsgmanager.h"
#include "unpacker.h"

// TODO D-Bus server for non JS kit apps!!!!

AppMsgManager::AppMsgManager(WatchConnector *watch, QObject *parent)
    : QObject(parent), watch(watch)
{
    watch->setEndpointHandler(WatchConnector::watchLAUNCHER,
                              [this](const QByteArray &data) {
        if (data.at(0) == WatchConnector::appmsgPUSH) {
            logger()->debug() << "LAUNCHER is PUSHing" << data.toHex();
            Unpacker u(data);
            u.skip(1); // skip data.at(0) which we just already checked above.
            uint transaction = u.read<quint8>();
            QUuid uuid = u.readUuid();
            WatchConnector::Dict dict = u.readDict();
            if (u.bad() || !dict.contains(1)) {
                logger()->warn() << "Failed to parse LAUNCHER message";
                return true;
            }

            switch (dict.value(1).toInt()) {
            case WatchConnector::launcherSTARTED:
                logger()->debug() << "App starting in watch:" << uuid;
                this->watch->sendMessage(WatchConnector::watchLAUNCHER,
                                         buildAckMessage(transaction));
                emit appStarted(uuid);
                break;
            case WatchConnector::launcherSTOPPED:
                logger()->debug() << "App stopping in watch:" << uuid;
                this->watch->sendMessage(WatchConnector::watchLAUNCHER,
                                         buildAckMessage(transaction));
                emit appStopped(uuid);
                break;
            default:
                logger()->warn() << "LAUNCHER pushed unknown message:" << uuid << dict;
                this->watch->sendMessage(WatchConnector::watchLAUNCHER,
                                         buildNackMessage(transaction));
                break;
            }
        }

        return true;
    });

    watch->setEndpointHandler(WatchConnector::watchAPPLICATION_MESSAGE,
                              [this](const QByteArray &data) {
        switch (data.at(0)) {
        case WatchConnector::appmsgPUSH:
            break;
        }

        return true;
    });
}

void AppMsgManager::send(const QUuid &uuid, const QVariantMap &data)
{
    // TODO
}

QByteArray AppMsgManager::buildAckMessage(uint transaction)
{
    QByteArray ba(2, Qt::Uninitialized);
    ba[0] = WatchConnector::appmsgACK;
    ba[1] = transaction;
    return ba;
}

QByteArray AppMsgManager::buildNackMessage(uint transaction)
{
    QByteArray ba(2, Qt::Uninitialized);
    ba[0] = WatchConnector::appmsgNACK;
    ba[1] = transaction;
    return ba;
}