~aleteoryx/muditaos

ref: 0f36b07f60d8c57bf9fb4c4fb4e9677f9ee6f339 muditaos/module-apps/application-settings/windows/Fota.cpp -rw-r--r-- 10.8 KiB
0f36b07f — Pawel Paprocki [EGD-4467] Support for deleting entries in FI DB (#1175) 5 years ago
                                                                                
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Fota.hpp"
#include "FotaWindow.hpp"

#include <service-cellular/CellularServiceAPI.hpp>
#include <service-fota/FotaServiceAPI.hpp>
#include <service-fota/FotaMessages.hpp>

#include <gui/widgets/Label.hpp>

#include <numeric>

std::string Fota::urlPrefix   = "http://mudita-pure-firmware-test.s3.eu-central-1.amazonaws.com/";
std::string Fota::versionFile = "fota.txt";

Fota::Fota(gui::FotaWindow *parent) : currentState(State::Disconnected), parent(parent)
{
    app = std::shared_ptr<app::Application>(parent->getApplication(),
                                            [](app::Application *) {}); /// with deleter that doesn't delete.
    app->busChannels.push_back(sys::BusChannels::ServiceFotaNotifications);
    registerHandlers();
    sys::Bus::Add(std::static_pointer_cast<sys::Service>(app));

    getCurrentVersion();
    parent->statusLabel->setText(getStateString());
}

Fota::~Fota()
{
    sys::Bus::Remove(std::static_pointer_cast<sys::Service>(app));
}

void Fota::next()
{
    switch (currentState) {
    case State::Disconnected:
        configureAPN();
        break;
    case State::Configuring:
        break;
    case State::Configured:
        connect();
        break;

    case State::Connecting:
        break;
    case State::Connected:
        downloadInfo();
        break;

    case State::DownloadingInfo:
        break;
    case State::DownloadedInfo:
        break;

    case State::ParsingInfo:
        break;
    case State::NeedUpdate:
        update();
        break;

    case State::Updating:
        break;
    case State::Finished:
        reboot();
        break;
    case State::Reboot:
        break;
    }
    parent->statusLabel->setText(getStateString());
}

std::string Fota::getStateString()
{
    switch (currentState) {
    case State::Disconnected:
        return std::string("Disconnected");
    case State::Configuring:
        return std::string("Configuring APN");
    case State::Configured:
        return std::string("Connect to APN");
    case State::Connecting:
        return std::string("Connecting...");
    case State::Connected:
        return std::string("Connected");
    case State::DownloadingInfo:
        return std::string("Downloading firmware info...");
    case State::DownloadedInfo:
        return std::string("Downloading info finished");
    case State::ParsingInfo:
        return std::string("Checking for firmware update...");
    case State::NeedUpdate:
        return std::string("Update");
    case State::Updating:
        return std::string("Updating...");
    case State::Finished:
        return std::string("Updating Finished! Reboot!");
    case State::Reboot:
        return std::string("Rebooting...");
    }
    return std::string("Unsupported state:") + std::to_string(static_cast<int>(currentState));
}

void Fota::configureAPN()
{
    FotaService::APN::Config apnConfig;
    apnConfig.contextId  = 1;
    apnConfig.apn        = "plus";
    apnConfig.authMethod = FotaService::APN::AuthMethod::NONE;
    FotaService::API::Configure(app.get(), apnConfig);
    parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, false);
    currentState = State::Configuring;
}

void Fota::connect()
{
    currentState = State::Connecting;
    // do the connection
    parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, false);
    FotaService::API::Connect(app.get());
}

void Fota::downloadInfo()
{
    currentState = State::DownloadingInfo;
    parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, false);
    FotaService::API::HTTPGET(app.get(), urlPrefix + versionFile);
}

void Fota::parseInfo(std::string data)
{
    currentState = State::ParsingInfo;
    updateStatus();
    std::istringstream inputData(data);
    std::string line;
    /* version mapping file, each line is divided this way:
     * <currentVersion>:<newVersion>:<fileName>
     */
    const unsigned char noItemsInLine(3);
    const unsigned char currentVersionPos(0);
    const unsigned char newVersionPos(1);
    const unsigned char fileNamePos(2);
    while (std::getline(inputData, line, '\n')) {
        std::istringstream lineInput(line);
        std::string subItem;
        std::vector<std::string> items;
        items.reserve(noItemsInLine);
        while (std::getline(lineInput, subItem, ':')) {
            items.push_back(subItem);
        }
        if (items.size() == noItemsInLine) {
            versionMap[items[currentVersionPos]] = {items[newVersionPos], items[fileNamePos]};
        }
    }

    LOG_DEBUG("parsed Data:");
    for (auto item : versionMap) {
        LOG_DEBUG("%s %s->%s\n\t\t%s",
                  (item.first == currentFirmwareVersion ? "*" : " "),
                  item.first.c_str(),
                  item.second.newVersion.c_str(),
                  item.second.file.c_str());
        if (item.first == currentFirmwareVersion) {
            newFirmwareVersion = item.second.newVersion;
            break;
        }
    }
    // do the parsing
}

void Fota::checkIfUpdateRequired()
{
    if (newFirmwareVersion.empty()) {
        parent->getBottomBar()->setActive(gui::BottomBar::Side::RIGHT, true);
        currentState = State::Finished;
    }
    else {
        parent->newFirmwareLabelText->setVisible(true);
        parent->newFirmwareLabel->setText(newFirmwareVersion);
        parent->newFirmwareLabel->setVisible(true);
        parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, true);
        currentState = State::NeedUpdate;
    }
    updateStatus();
}

void Fota::update()
{
    currentState = State::Updating;
    parent->getBottomBar()->setActive(gui::BottomBar::Side::RIGHT, false);
    parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, false);
    parent->downloadProgress->setVisible(true);
    FotaService::API::FotaStart(app.get(), urlPrefix + versionMap[currentFirmwareVersion].file);
}

void Fota::reboot()
{
    currentState = State::Reboot;
    parent->statusLabel->setText(getStateString());
    auto msg = std::make_shared<sys::SystemManagerCmd>(sys::Code::Reboot);
    sys::Bus::SendUnicast(std::move(msg), service::name::system_manager, app.get());
}

void Fota::registerHandlers()
{
    LOG_DEBUG("Registrng handles");
    handleInternetNotification();
    handleHTTPResponse();
    handleFotaProgres();
    handleFotaFinished();
}

void Fota::handleInternetNotification()
{
    LOG_DEBUG("handle IntrntNotificationMassage");
    app->connect(FotaService::NotificationMessage(), [&](sys::Message *req) {
        LOG_DEBUG("IntrntNotificationMassage");
        if (auto msg = dynamic_cast<FotaService::NotificationMessage *>(req)) {
            LOG_DEBUG("IntrntNotificationMassage: %s", msg->c_str());
            switch (msg->type) {
            case FotaService::NotificationMessage::Type::NotReady:
                break;
            case FotaService::NotificationMessage::Type::Configured:
                currentState = State::Configured;
                parent->statusLabel->setText(getStateString());
                parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, true);
                app->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
                break;
            case FotaService::NotificationMessage::Type::Connected:
                currentState = State::Connected;
                parent->statusLabel->setText(getStateString());
                parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, true);
                app->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
                break;
            case FotaService::NotificationMessage::Type::Disconnected:
                currentState = State::Disconnected;
                break;
            case FotaService::NotificationMessage::Type::RequestProcessed:
                break;
            case FotaService::NotificationMessage::Type::ServiceReady:
                break;
            }
        }
        return std::make_shared<sys::ResponseMessage>();
    });
}

void Fota::handleHTTPResponse()
{
    LOG_DEBUG("Handling http response");
    app->connect(FotaService::HTTPResponseMessage(), [&](sys::Message *req) {
        if (auto msg = dynamic_cast<FotaService::HTTPResponseMessage *>(req)) {
            LOG_DEBUG("HTTP Response to: %s", msg->url.c_str());
            LOG_DEBUG("HTPP AT Error   : %s", FotaService::toString(msg->httpError).c_str());
            LOG_DEBUG(
                "response headers:\n\t%s",
                std::accumulate(msg->responseHeaders.begin(), msg->responseHeaders.end(), std::string("\n\t")).c_str());
            LOG_DEBUG("response data   :\n%s", msg->body.c_str());
            const std::string contentLength("Content-Length: ");
            std::string data;
            for (auto &header : msg->responseHeaders) {
                auto sizeBegin = header.find(contentLength);
                if (sizeBegin != std::string::npos) {
                    try {
                        unsigned long len = std::stoul(header.substr(sizeBegin + contentLength.size()));
                        data              = msg->body.substr(0, len);
                    }
                    catch (const std::exception &e) {
                        LOG_ERROR("Fota::handleHTTPResponse exception %s", e.what());
                        return std::make_shared<sys::ResponseMessage>();
                    }
                    break;
                }
            }

            currentState = State::DownloadedInfo;
            parseInfo(std::move(data));
            checkIfUpdateRequired();
        }
        return std::make_shared<sys::ResponseMessage>();
    });
}

void Fota::handleFotaProgres()
{
    app->connect(FotaService::FOTAProgres(), [&](sys::Message *req) {
        if (auto msg = dynamic_cast<FotaService::FOTAProgres *>(req)) {
            parent->downloadProgress->setValue(msg->progress);
            app->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }
        return std::make_shared<sys::ResponseMessage>();
    });
}

void Fota::handleFotaFinished()
{
    app->connect(FotaService::FOTAFinished(), [&](sys::Message *) {
        LOG_DEBUG("Done!");
        currentState = State::Finished;
        parent->statusLabel->setText(getStateString());
        parent->currentFirmwareLabel->setText(newFirmwareVersion);
        parent->newFirmwareLabelText->setVisible(false);
        parent->newFirmwareLabel->setVisible(false);
        parent->downloadProgress->setVisible(false);
        parent->getBottomBar()->setActive(gui::BottomBar::Side::CENTER, true);
        app->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        return std::make_shared<sys::ResponseMessage>();
    });
}

void Fota::getCurrentVersion()
{
    CellularServiceAPI::GetFirmwareVersion(parent->getApplication(), currentFirmwareVersion);
}

void Fota::updateStatus()
{
    parent->statusLabel->setText(getStateString());
    app->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
}