summaryrefslogtreecommitdiffstats
path: root/src/b2qt-flashing-wizard/device_page.cpp
blob: 97a6def7d5d881f5a2d644c4b4990ae36f1f3ce2 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use the contact form at
** http://qt.digia.com/
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** the contact form at http://qt.digia.com/
**
****************************************************************************/

#include "device_page.h"
#include "common.h"
#include "mainwindow.h" // for Page_ enum
#include "platforminfo.h"
#include <QDebug>
#include <QDir>
#include <QLabel>
#include <QLayout>
#include <QListWidget>
#include <QMessageBox>
#include <QProcess>
#include <QRadioButton>
#include <QTimer>

extern QString G_SDKDIR;
extern QString G_device;
extern PlatformInfo G_platforminfo;
extern QString G_mode;

QLabel *createErrorLabel(QWidget *parent);

DevicePage::DevicePage(QWidget *parent)
    : QWizardPage(parent)
    , mListWidget(new QListWidget(this))
    , mError(createErrorLabel(this))
    , mLayout(new QVBoxLayout(this))
{
    setTitle(tr("Device"));
    setSubTitle(tr("Select a device to be used"));
    mLayout->addWidget(mListWidget);
    mLayout->addSpacerItem(new QSpacerItem(40,40,QSizePolicy::Minimum, QSizePolicy::Expanding));
    mLayout->addWidget(mError);
    setLayout(mLayout);
    connect(mListWidget, &QListWidget::itemClicked, this, &DevicePage::itemSelected);

    QTimer *timer = new QTimer(this);
    timer->setInterval(1000);
    connect(timer, &QTimer::timeout, this, &DevicePage::updateDeviceList);
    timer->start();
}

DevicePage::~DevicePage()
{
}

QList<DevicePage::DeviceInfo> DevicePage::list() const
{
    QList<DeviceInfo> diList;

    QProcess process;
    process.start(G_SDKDIR + "/Tools/b2qt/adb", QStringList() << "devices" << "-l");
    process.waitForFinished();

    QList<QByteArray> lines = process.readAll().split('\n');
    foreach (const QByteArray &ba, lines) {
        if (ba.startsWith("List of"))
            continue;
        if (ba.isEmpty())
            continue;
        if (ba.startsWith("192.168.56.101:"))
            continue;
        QList<QByteArray> token = ba.simplified().split(' ');

        if (token.size() == 6){
            DeviceInfo d;

            if (token[5].startsWith("device:")) {
                QString deviceName = token[5].mid(7);
                if (deviceName == "grouper")
                    d.name = "Nexus7";
                else if (deviceName == "flo")
                    d.name = "Nexus7v2";
                else
                    d.name = deviceName;
            }
            d.serial = token[0];
            d.mode = "adb";
            if (token[1] == "device")
                d.state = "ready";
            else
                d.state = "not ready";
            diList.append(d);
        } else if (token.size() == 3) {
            DeviceInfo d;
            d.serial = token[0];
            d.mode = "adb";
            d.name = "Unknown device";
            if (token[1] == "offline")
                d.state = "not ready";
            else {
                qDebug() << "Unknown state:" << token[1];
                continue;
            }
            diList.append(d);
        } else {
            qDebug() << "Invalid token count:" << token.size() << token;
            continue;
        }

    }

    process.start(G_SDKDIR + "/Tools/b2qt/fastboot", QStringList("devices") << "-l");
    process.waitForFinished();
    lines = process.readAll().split('\n');
    foreach (const QByteArray &ba, lines) {
        if (ba.isEmpty())
            continue;

        QList<QByteArray> token = ba.simplified().split(' ');

        if (token.size() != 3) {
            qDebug() << "Invalid token count:" << token;
            continue;
        }


        DeviceInfo d;
        d.serial = token[0];

        QProcess p2;
        p2.start(G_SDKDIR + "/Tools/b2qt/fastboot", QStringList() << "-s" << d.serial << "getvar" << "product");
        if (!p2.waitForFinished()) {
            qDebug() << "Could not get product type";
            d.name = "Unknown";
        }

        QString productString = p2.readAllStandardError().split('\n')[0].simplified();
        if (productString.startsWith("product: ")) {
            QString productName = productString.mid(9);
            if (productName == "grouper")
                d.name = "Nexus7";
            else if (productName == "flo")
                d.name = "Nexus7v2";
            else
                d.name = "Unknown";
        } else
            d.name = "Unknown";

        d.mode = "fastboot";
        d.state = "ready";
        diList.append(d);
    }

    return diList;
}

bool DevicePage::isComplete() const
{
    int index = mListWidget->currentRow();
    if (index < 0 || index >= mListItems.size()) {
        return false;
    }

    return true;
}

void DevicePage::itemSelected()
{
    emit completeChanged();
}

void DevicePage::updateDeviceList()
{
    if (wizard()->currentPage() != this)
        return;

    QList<DeviceInfo> diList = list();

    QStringList currentDevices;

    foreach (const DeviceInfo &di, diList)
        currentDevices += di.serial;

    QMutableMapIterator<QString, QListWidgetItem*> iter(mListItems);
    while (iter.hasNext()) {
        iter.next();
        if (!currentDevices.contains(iter.key())) {
            mDeviceInfo.remove(iter.key());
            delete iter.value();
            iter.remove();
        }
    }

    foreach (const DeviceInfo &di, diList) {
        QString text = "<h3>%1</h3><table style=\"margin-left:10px\"><tr><td>Serial number</td><td style=\"padding-left:10\">%2</td></tr><tr><td>Mode</td><td style=\"padding-left:10\">%3</td></tr><tr><td>State</td><td style=\"padding-left:10\">%4</td></tr></table>";
        text = text.arg(di.name, di.serial, di.mode, di.state);

        mDeviceInfo[di.serial] = di;
        if (mListItems.contains(di.serial)) {
            // update existing item
            QListWidgetItem *item = mListItems.value(di.serial);
            QLabel *label = qobject_cast<QLabel*>(mListWidget->itemWidget(item));
            label->setText(text);
        } else {
            // new item
            QListWidgetItem *item = new QListWidgetItem();
            item->setData(Qt::UserRole, di.serial);
            QLabel *label = new QLabel;
            label->setText(text);
            mListWidget->addItem(item);
            mListWidget->setItemWidget(item, label);
            item->setSizeHint(label->size());
            mListItems.insert(di.serial, item);
        }
    }

    if (mListWidget->count() == 0)
        mError->setText("No suitable device found");
    else
        mError->setText("");

    emit completeChanged();
}


void DevicePage::initializePage()
{
    mError->clear();
    updateDeviceList();
}

bool DevicePage::validatePage()
{
    QListWidgetItem *item = mListWidget->currentItem();
    if (!item) {
        return false;
    }

    QString serial = item->data(Qt::UserRole).toString();
    if (serial.isEmpty()) {
        return false;
    }

    if (!mDeviceInfo.contains(serial)) {
        return false;
    }

    DeviceInfo deviceInfo = mDeviceInfo[serial];
    if (deviceInfo.state != "ready") {
        QMessageBox::critical(this, tr("Wrong device status"), tr("The selected device is not ready."));
        return false;
    }

    if (deviceInfo.name.toLower() != G_platforminfo.board.toLower()) {
        QMessageBox::critical(this, tr("Wrong device type"), tr("The selected device is not compatible for this platform."));
        return false;
    }

    G_device = deviceInfo.serial;
    G_platforminfo.board  = deviceInfo.name.toLower();
    G_mode = deviceInfo.mode;
    return true;
}

int DevicePage::nextId() const
{
    return MainWindow::Page_Commit;
}