File: flipperzero.cpp

package info (click to toggle)
qflipper 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,320 kB
  • sloc: cpp: 18,500; sh: 247; ansic: 191; xml: 38; python: 14; makefile: 5
file content (270 lines) | stat: -rw-r--r-- 8,656 bytes parent folder | download | duplicates (2)
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
#include "flipperzero.h"

#include <QUrl>
#include <QDebug>
#include <QTimer>
#include <QLoggingCategory>

#include "preferences.h"
#include "flipperupdates.h"

#include "devicestate.h"

#include "protobufsession.h"
#include "utilityinterface.h"
#include "recoveryinterface.h"

#include "toplevel/wirelessstackupdateoperation.h"
#include "toplevel/firmwareinstalloperation.h"
#include "toplevel/settingsrestoreoperation.h"
#include "toplevel/settingsbackupoperation.h"
#include "toplevel/fullupdateoperation.h"
#include "toplevel/factoryresetoperation.h"
#include "toplevel/fullrepairoperation.h"
#include "toplevel/legacyupdateoperation.h"

#include "utility/storageinforefreshoperation.h"

Q_LOGGING_CATEGORY(CAT_DEVICE, "DEV")

#define CHANNEL_DEVELOPMENT "development"
#define CHANNEL_RELEASE_CANDIDATE "release-candidate"
#define CHANNEL_RELEASE "release"

using namespace Flipper;
using namespace Updates;
using namespace Zero;

FlipperZero::FlipperZero(const Zero::DeviceInfo &info, QObject *parent):
    QObject(parent),
    m_state(new DeviceState(info, this)),
    m_rpc(new ProtobufSession(info.portInfo, this)),
    m_recovery(new RecoveryInterface(m_state, this)),
    m_utility(new UtilityInterface(m_state, m_rpc, this))
{
    connect(m_state, &DeviceState::deviceInfoChanged, this, &FlipperZero::onDeviceInfoChanged);
    connect(m_state, &DeviceState::deviceInfoChanged, this, &FlipperZero::deviceStateChanged);

    connect(m_rpc, &ProtobufSession::sessionStateChanged, this, &FlipperZero::onSessionStatusChanged);

    onDeviceInfoChanged();
}

DeviceState *FlipperZero::deviceState() const
{
    return m_state;
}

ProtobufSession *FlipperZero::rpc() const
{
    return m_rpc;
}

UtilityInterface *FlipperZero::utility() const
{
    return m_utility;
}

// TODO: Handle -rcxx suffixes correctly
bool FlipperZero::canUpdate(const Updates::VersionInfo &versionInfo) const
{
    const auto &storageInfo = m_state->deviceInfo().storage;
    const auto &radioStackVersion = m_state->deviceInfo().radioVersion;

    const auto noAssets = storageInfo.isExternalPresent && !storageInfo.isAssetsInstalled;
    const auto noRadioStack = radioStackVersion.isEmpty();

    if(noAssets || noRadioStack) {
        return true;
    }

    static const auto DEVELOPMENT = QStringLiteral("development");
    static const auto RELEASE_CANDIDATE = QStringLiteral("release-candidate");
    static const auto RELEASE = QStringLiteral("release");

    const auto &firmwareInfo = m_state->deviceInfo().firmware;

    const auto &deviceChannel = firmwareInfo.channel;
    const auto &deviceVersion = (deviceChannel == QStringLiteral("development")) ?
                firmwareInfo.commit :
                firmwareInfo.version;

    const auto &deviceDate = firmwareInfo.date;

    const auto &serverChannel = globalPrefs->firmwareUpdateChannel();
    const auto &serverVersion = versionInfo.number();
    const auto &serverDate = versionInfo.date();

    if(deviceChannel == RELEASE) {
        if(serverChannel == RELEASE) {
            return VersionInfo::compare(deviceVersion, serverVersion) < 0;
        } else if(serverChannel == RELEASE_CANDIDATE) {
            return VersionInfo::compare(deviceVersion, serverVersion) < 0;
        } else if(serverChannel == DEVELOPMENT) {
            return deviceDate <= serverDate;
        }

    } else if(deviceChannel == RELEASE_CANDIDATE) {
        if(serverChannel == RELEASE) {
            return VersionInfo::compare(deviceVersion, serverVersion) <= 0;
        } else if(serverChannel == RELEASE_CANDIDATE) {
            return VersionInfo::compare(deviceVersion, serverVersion) < 0;
        } else if(serverChannel == DEVELOPMENT) {
            return deviceDate <= serverDate;
        }

    } else if(deviceChannel == DEVELOPMENT) {
        if(serverChannel == RELEASE) {
            return deviceDate <= serverDate;
        } else if(serverChannel == RELEASE_CANDIDATE) {
            return deviceDate <= serverDate;
        } else if(serverChannel == DEVELOPMENT) {
            return (deviceVersion != serverVersion) && (deviceDate <= serverDate);
        }
    }

    return false;
}

bool FlipperZero::canInstall(const Updates::VersionInfo &versionInfo) const
{
    Q_UNUSED(versionInfo)

    const auto &deviceChannel = m_state->deviceInfo().firmware.channel;
    const auto &serverChannel = globalPrefs->firmwareUpdateChannel();

    return deviceChannel != serverChannel;
}

bool FlipperZero::canRepair(const Updates::VersionInfo &versionInfo) const
{
    Q_UNUSED(versionInfo)
    return m_state->isRecoveryMode();
}

void FlipperZero::fullUpdate(const Updates::VersionInfo &versionInfo)
{
    const auto &storageInfo = m_state->deviceInfo().storage;
    const auto &protobufInfo = m_state->deviceInfo().protobuf;
    const auto isLegacyUpdate = !storageInfo.isExternalPresent || ((protobufInfo.versionMajor == 0) && (protobufInfo.versionMinor < 7));

    if(isLegacyUpdate) {
        registerOperation(new LegacyUpdateOperation(m_recovery, m_utility, m_state, versionInfo, this));
    } else {
        registerOperation(new FullUpdateOperation(m_utility, m_state, versionInfo, this));
    }
}

void FlipperZero::fullRepair(const Updates::VersionInfo &versionInfo)
{
    registerOperation(new FullRepairOperation(m_recovery, m_utility, m_state, versionInfo, this));
}

void FlipperZero::createBackup(const QUrl &backupUrl)
{
    registerOperation(new SettingsBackupOperation(m_utility, m_state, backupUrl, this));
}

void FlipperZero::restoreBackup(const QUrl &backupUrl)
{
    registerOperation(new SettingsRestoreOperation(m_utility, m_state, backupUrl, this));
}

void FlipperZero::factoryReset()
{
    registerOperation(new FactoryResetOperation(m_utility, m_state, this));
}

void FlipperZero::installFirmware(const QUrl &fileUrl)
{
    if(fileUrl.fileName().endsWith(QStringLiteral(".tgz"))) {
        registerOperation(new FullUpdateOperation(m_utility, m_state, fileUrl, this));
    } else {
        registerOperation(new FirmwareInstallOperation(m_recovery, m_utility, m_state, fileUrl.toLocalFile(), this));
    }
}

void FlipperZero::installWirelessStack(const QUrl &fileUrl)
{
    registerOperation(new WirelessStackUpdateOperation(m_recovery, m_utility, m_state, fileUrl.toLocalFile(), this));
}

void FlipperZero::installFUS(const QUrl &fileUrl, uint32_t address)
{
    registerOperation(new FUSUpdateOperation(m_recovery, m_utility, m_state, fileUrl.toLocalFile(), address, this));
}

void FlipperZero::refreshStorageInfo()
{
    m_utility->refreshStorageInfo();
}

void FlipperZero::finalizeOperation()
{
    // TODO: write a better implementation that would:
    // 1. Check if the port is open and functional
    // 2. Test if the RPC session is up an running
    // 3. Open RPC session if necessary

    if(m_state->isError()) {
        m_state->clearError();
    }
}

void FlipperZero::onDeviceInfoChanged()
{
    if(m_state->isOnline()) {
        // Most likely Storage info update
        return;
    } else if(m_state->isRecoveryMode()) {
        // Recovery mode, not using Protobuf
        m_state->setOnline(true);
        return;
    }

    // Perform Protobuf session initialisation
    const auto &deviceInfo = m_state->deviceInfo();

    qCDebug(CAT_DEVICE).noquote() << "Version:" << deviceInfo.firmware.version << "commit:"
                                  << deviceInfo.firmware.commit << "radio:" << deviceInfo.radioVersion;

    const auto &pb = deviceInfo.protobuf;
    const auto &pi = deviceInfo.portInfo;

    m_rpc->setMajorVersion(pb.versionMajor);
    m_rpc->setMinorVersion(pb.versionMinor);
    m_rpc->setSerialPort(pi);

    // 100 ms delay to prevent race condition in Flipper
    QTimer::singleShot(100, m_rpc, &ProtobufSession::startSession);
}

void FlipperZero::onSessionStatusChanged()
{
    if(m_rpc->isError()) {
        m_state->setError(m_rpc->error(), m_rpc->errorString());
        emit operationFinished();

    } else if(m_rpc->isSessionUp()) {
        m_state->setOnline(true);
    }
}

void FlipperZero::registerOperation(AbstractOperation *operation)
{
    connect(operation, &AbstractOperation::finished, this, [=]() {
        if(operation->isError()) {
            qCCritical(CAT_DEVICE).noquote() << operation->description() << "ERROR:" << operation->errorString();
            m_state->setError(operation->error(), operation->errorString());

        } else {
            qCInfo(CAT_DEVICE).noquote() << operation->description() << "SUCCESS";
        }

        operation->deleteLater();
        emit operationFinished();
    });

    qCInfo(CAT_DEVICE).noquote() << operation->description() << "START";
    operation->start();
}