summaryrefslogtreecommitdiff
path: root/daemon/bankmanager.cpp
blob: 194ec7714c86056afd8c97a8df039e451b3d6e32 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "unpacker.h"
#include "packer.h"
#include "bankmanager.h"

BankManager::BankManager(WatchConnector *watch, AppManager *apps, QObject *parent) :
    QObject(parent), watch(watch), apps(apps)
{
    connect(watch, &WatchConnector::connectedChanged, this, &BankManager::handleWatchConnected);
}

int BankManager::numSlots() const
{
    return _slots.size();
}

bool BankManager::uploadApp(const QUuid &uuid, int slot)
{
    AppInfo info = apps->info(uuid);
    if (info.uuid() != uuid) {
        logger()->warn() << "uuid" << uuid << "is not installed";
        return false;
    }
    if (slot == -1) {
        slot = findUnusedSlot();
        if (slot == -1) {
            logger()->warn() << "no free slots!";
            return false;
        }
    }
    if (slot < 0 || slot > _slots.size()) {
        logger()->warn() << "invalid slot index";
        return false;
    }

    // TODO

    return false;
}

bool BankManager::unloadApp(int slot)
{
    if (slot < 0 || slot > _slots.size()) {
        logger()->warn() << "invalid slot index";
        return false;
    }
    if (!_slots[slot].used) {
        logger()->warn() << "slot is empty";
        return false;
    }

    logger()->debug() << "going to unload app" << _slots[slot].name << "in slot" << slot;

    int installId = _slots[slot].id;

    QByteArray msg;
    msg.reserve(2 * sizeof(quint32));
    Packer p(&msg);
    p.write<quint8>(WatchConnector::appmgrREMOVE_APP);
    p.write<quint32>(installId);
    p.write<quint32>(slot);

    watch->sendMessage(WatchConnector::watchAPP_MANAGER, msg,
                       [this](const QByteArray &data) {
        Unpacker u(data);
        if (u.read<quint8>() != WatchConnector::appmgrREMOVE_APP) {
            return false;
        }

        uint result = u.read<quint32>();
        switch (result) {
        case 1: /* Success */
            logger()->debug() << "sucessfully unloaded app";
            break;
        default:
            logger()->warn() << "could not unload app. result code:" << result;
            break;
        }

        QMetaObject::invokeMethod(this, "refresh", Qt::QueuedConnection);

        return true;
    });

    return true; // Operation in progress
}

void BankManager::refresh()
{
    logger()->debug() << "refreshing bank status";

    watch->sendMessage(WatchConnector::watchAPP_MANAGER,
                       QByteArray(1, WatchConnector::appmgrGET_APPBANK_STATUS),
                       [this](const QByteArray &data) {
        if (data.at(0) != WatchConnector::appmgrGET_APPBANK_STATUS) {
            return false;
        }

        if (data.size() < 9) {
            logger()->warn() << "invalid getAppbankStatus response";
            return true;
        }

        Unpacker u(data);

        u.skip(sizeof(quint8));

        unsigned int num_banks = u.read<quint32>();
        unsigned int apps_installed = u.read<quint32>();

        logger()->debug() << "Bank status:" << apps_installed << "/" << num_banks;

        _slots.resize(num_banks);
        for (unsigned int i = 0; i < num_banks; i++) {
            _slots[i].used = false;
            _slots[i].id = 0;
            _slots[i].name.clear();
            _slots[i].company.clear();
            _slots[i].flags = 0;
            _slots[i].version = 0;
            _slots[i].uuid = QUuid();
        }

        for (unsigned int i = 0; i < apps_installed; i++) {
           unsigned int id = u.read<quint32>();
           int index = u.read<quint32>();
           QString name = u.readFixedString(32);
           QString company = u.readFixedString(32);
           unsigned int flags = u.read<quint32>();
           unsigned short version = u.read<quint16>();

           if (index < 0 || index >= _slots.size()) {
               logger()->warn() << "Invalid slot index" << index;
               continue;
           }

           if (u.bad()) {
               logger()->warn() << "short read";
               return true;
           }

           _slots[index].used = true;
           _slots[index].id = id;
           _slots[index].name = name;
           _slots[index].company = company;
           _slots[index].flags = flags;
           _slots[index].version = version;

           AppInfo info = apps->info(name);
           QUuid uuid = info.uuid();
           _slots[index].uuid = uuid;

           logger()->debug() << index << id << name << company << flags << version << uuid;
        }

        emit this->slotsChanged();

        return true;
    });
}

int BankManager::findUnusedSlot() const
{
    for (int i = 0; i < _slots.size(); ++i) {
        if (!_slots[i].used) {
            return i;
        }
    }

    return -1;
}

void BankManager::handleWatchConnected()
{
    if (watch->isConnected()) {
        refresh();
    }
}

#if 0

void WatchConnector::getAppbankStatus(const std::function<void(const QString &s)>& callback)
{
    sendMessage(watchAPP_MANAGER, QByteArray(1, appmgrGET_APPBANK_STATUS),
                [this, callback](const QByteArray &data) {

    });
}

void WatchConnector::getAppbankUuids(const function<void(const QList<QUuid> &)>& callback)
{
    sendMessage(watchAPP_MANAGER, QByteArray(1, appmgrGET_APPBANK_UUIDS),
                [this, callback](const QByteArray &data) {
        if (data.at(0) != appmgrGET_APPBANK_UUIDS) {
            return false;
        }
        logger()->debug() << "getAppbankUuids response" << data.toHex();

        if (data.size() < 5) {
            logger()->warn() << "invalid getAppbankUuids response";
            return true;
        }

        Unpacker u(data);

        u.skip(sizeof(quint8));

        unsigned int apps_installed = u.read<quint32>();

        logger()->debug() << apps_installed;

        QList<QUuid> uuids;

        for (unsigned int i = 0; i < apps_installed; i++) {
           QUuid uuid = u.readUuid();

           logger()->debug() << uuid.toString();

           if (u.bad()) {
               logger()->warn() << "short read";
               return true;
           }

           uuids.push_back(uuid);
        }

        logger()->debug() << "finished";

        callback(uuids);

        return true;
    });
}
#endif