~aleteoryx/muditaos

ref: 64cfe81ec546ed75b7be1cffaef781aa3b893236 muditaos/module-services/service-desktop/endpoints/update/UpdateEndpoint.cpp -rw-r--r-- 3.9 KiB
64cfe81e — Marek Niepieklo [CP-427] Set Mudita USB VID as default 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "UpdateEndpoint.hpp"
#include "UpdateMuditaOS.hpp"

#include <service-desktop/DesktopMessages.hpp>
#include <service-desktop/ServiceDesktop.hpp>
#include <endpoints/Context.hpp>
#include <endpoints/messages/MessageHelper.hpp>

#include <json11.hpp>
#include <purefs/filesystem_paths.hpp>

#include <filesystem>
#include <memory>

using namespace parserFSM;

auto UpdateEndpoint::handle(Context &context) -> void
{
    switch (context.getMethod()) {
    case http::Method::post:
        run(context);
        break;
    case http::Method::get:
        getUpdates(context);
        break;
    default:
        break;
    }
}

auto UpdateEndpoint::run(Context &context) -> sys::ReturnCodes
{
    std::string cmd = context.getBody()[parserFSM::json::updateprocess::command].string_value();
    if (cmd == parserFSM::json::updateprocess::commands::abort) {
        auto owner        = static_cast<ServiceDesktop *>(ownerServicePtr);
        auto currentState = owner->updateOS->status;
        if (currentState <= updateos::UpdateState::ExtractingFiles) {
            owner->updateOS->setUpdateAbortFlag(true);
            context.setResponseBody(json11::Json::object({{parserFSM::json::updateprocess::updateAborted, true}}));
            context.setResponseStatus(http::Code::OK);
            MessageHandler::putToSendQueue(context.createSimpleResponse());
            return sys::ReturnCodes::Success;
        }
        else {
            context.setResponseBody(json11::Json::object({{parserFSM::json::updateprocess::updateAborted, false}}));
            context.setResponseStatus(http::Code::NotAcceptable);
            MessageHandler::putToSendQueue(context.createSimpleResponse());
            return sys::ReturnCodes::Failure;
        }
    }

    std::string fileName = context.getBody()["fileName"].string_value();
    auto path            = purefs::dir::getUpdatesOSPath() / fileName;
    auto fileExists      = std::filesystem::exists(path.c_str());
    if (fileExists) {
        context.setResponseBody(json11::Json::object({{parserFSM::json::updateReady, true}}));

        auto msg = std::make_shared<sdesktop::UpdateOsMessage>(fileName, context.getUuid());
        ownerServicePtr->bus.sendUnicast(msg, service::name::service_desktop);
        MessageHandler::putToSendQueue(context.createSimpleResponse());
        return sys::ReturnCodes::Success;
    }
    else {
        context.setResponseBody(json11::Json::object({{parserFSM::json::updateReady, false}}));
        context.setResponseStatus(http::Code::InternalServerError);
        MessageHandler::putToSendQueue(context.createSimpleResponse());
        return sys::ReturnCodes::Failure;
    }
}

auto UpdateEndpoint::getUpdates(Context &context) -> sys::ReturnCodes
{
    const auto updatesOSPath = purefs::dir::getUpdatesOSPath();
    std::error_code errorCode;
    struct DirectoryEntry
    {
        std::string fileName;
        uint32_t fileSize;
        json11::Json to_json() const
        {
            return (json11::Json::object{{"name", fileName}, {"size", std::to_string(fileSize)}});
        }
    };

    auto dirEntryVector = std::vector<DirectoryEntry>();
    for (const auto &p : fs::directory_iterator(updatesOSPath, errorCode)) {
        if (errorCode) {
            LOG_WARN("can't get directory contents for %s, \"%s\"", updatesOSPath.c_str(), errorCode.message().c_str());
            return sys::ReturnCodes::Failure;
        }

        if (!p.is_directory() && p.path().extension() == updateos::extension::update) {
            dirEntryVector.push_back(DirectoryEntry{p.path().string(), static_cast<uint32_t>(p.file_size())});
        }
    }

    json11::Json fileList = dirEntryVector;
    context.setResponseBody(json11::Json::object{{parserFSM::json::updateFileList, fileList}});

    MessageHandler::putToSendQueue(context.createSimpleResponse());

    return sys::ReturnCodes::Success;
}