~aleteoryx/muditaos

ref: 39255b2d45ffacb9de27d211c79d6a6763d69bcf muditaos/module-services/service-fota/FotaServiceAPI.cpp -rw-r--r-- 3.6 KiB
39255b2d — Paweł Joński [BH-743] AlarmEvent UT 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
107
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "service-fota/FotaMessages.hpp"
#include "service-fota/FotaServiceAPI.hpp"
#include "service-fota/ServiceFota.hpp"

#include <MessageType.hpp>
#include <log.hpp>

#include <memory>
#include <sstream>
#include <string>

namespace FotaService
{

    bool API::Configure(sys::Service *serv, const APN::Config &config)
    {
        LOG_DEBUG("FOTA - Internet Config called");
        std::shared_ptr<ConfigureAPNMessage> msg = std::make_shared<ConfigureAPNMessage>(config);
        return serv->bus.sendUnicast(std::move(msg), service::name::fota);
    }

    bool API::Connect(sys::Service *serv)
    {
        LOG_DEBUG("FOTA - Internet connection called");
        auto msg = std::make_shared<ConnectMessage>();
        return serv->bus.sendUnicast(std::move(msg), service::name::fota);
    }

    bool API::Disconnect(sys::Service *serv)
    {
        std::shared_ptr<InternetRequestMessage> msg =
            std::make_shared<InternetRequestMessage>(MessageType::FotaInternetDisconnect);
        return serv->bus.sendUnicast(std::move(msg), service::name::fota);
    }

    void API::HTTPGET(sys::Service *serv, const std::string &url)
    {
        LOG_DEBUG("HTTP GET API called");
        std::shared_ptr<HTTPRequestMessage> msg = std::make_shared<HTTPRequestMessage>();
        msg->url                                = url;
        msg->method                             = FotaService::HTTPMethod::GET;
        serv->bus.sendUnicast(std::move(msg), service::name::fota);
    }

    void API::FotaStart(sys::Service *serv, const std::string &url)
    {
        LOG_DEBUG("Fota Start: %s", url.c_str());
        std::shared_ptr<FotaService::FOTAStart> msg = std::make_shared<FotaService::FOTAStart>();

        msg->url = url;

        serv->bus.sendUnicast(std::move(msg), service::name::fota);
    }

    void API::sendRawProgress(sys::Service *serv, const std::string &rawQind)
    {
        LOG_DEBUG("Fota sending Raw progress");
        std::shared_ptr<FotaService::FOTARawProgress> msg = std::make_shared<FotaService::FOTARawProgress>();
        msg->qindRaw                                      = rawQind;
        serv->bus.sendUnicast(std::move(msg), service::name::fota);
    }

    std::string APN::toString(APN::AuthMethod authMethod)
    {
        switch (authMethod) {
        case AuthMethod::NONE:
            return "NONE";
        case AuthMethod::PAP:
            return "PAP";
        case AuthMethod::CHAP:
            return "CHAP";
        case AuthMethod::AUTO:
            return "AUTO";
        }
        return std::to_string(static_cast<int>(authMethod));
    }

    std::string APN::Config::toString() const
    {
        std::ostringstream out;
        out << static_cast<int>(contextId) << "," << (type == ContextType::ipv4 ? "ipv4" : "ipv4v6") << "," << apn
            << "," << username << "," << password << "," << APN::toString(authMethod) << ","
            << (activated ? "Activated" : "Deactivated") << "," << ip << ",";
        return out.str();
    }

    std::string toString(HTTPErrors error)
    {
        switch (error) {
        case HTTPErrors::OK:
            return "OK";
        case HTTPErrors::UnknowError:
            return "UnknowError";
        case HTTPErrors::OpenFailed:
            return "OpenFailed";
        case HTTPErrors::URLFailed:
            return "URLFailed";
        case HTTPErrors::GetFailed:
            return "GetFailed";
        }
        return "unknown (" + std::to_string(static_cast<int>(error)) + ")";
    }

} // namespace FotaService