~aleteoryx/muditaos

ref: 140cce4c8981263fcc14fc55a37354a2d555eb33 muditaos/module-services/service-fota/ServiceFota.hpp -rw-r--r-- 3.9 KiB
140cce4c — Lucjan Bryndza [EGD-4029] Prevent call handler recursively (#927) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "api/FotaServiceAPI.hpp"

#include <service-cellular/ServiceCellular.hpp>

#include <Modem/TS0710/DLC_channel.h>
#include <Modem/TS0710/TS0710.h>
#include <Service/Service.hpp>
#include <Service/Message.hpp>

#include <memory>
#include <sstream>

namespace FotaService
{

    class Service : public sys::Service
    {

      public:
        enum class State
        {
            Idle,
            Connecting,
            Connected,
            QHTTPREAD,
            FOTAUpdate,
            Failed
        };

        Service();

        ~Service();

        sys::Message_t DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp = nullptr) override;

        sys::ReturnCodes InitHandler() override;

        sys::ReturnCodes DeinitHandler() override;

        sys::ReturnCodes SwitchPowerModeHandler(const sys::ServicePowerMode /*mode*/) override final
        {
            return sys::ReturnCodes::Success;
        }

        void registerMessageHandlers();

        static const char *serviceName;

      private:
        /** Get access to data channel
         */
        sys::Message_t handleCellularGetChannelResponseMessage(sys::DataMessage *req, sys::ResponseMessage *response);
        /** Do nothing until celular finishes it's startup
         */
        sys::Message_t handleServiceCellularNotifications(sys::DataMessage *req, sys::ResponseMessage *response);
        sys::Message_t handleConfigureAPN(sys::DataMessage *req, sys::ResponseMessage *response);
        sys::Message_t handleConnect(sys::DataMessage *req, sys::ResponseMessage *response);
        sys::Message_t handleHttpGet(sys::DataMessage *req, sys::ResponseMessage *response);
        /** Send fota update command to modem
         */
        sys::Message_t handleFotaStart(sys::DataMessage *req, sys::ResponseMessage *response);
        /** Handle URC from modem, support for asynchronious commands
         */
        void handleChannelNotifications(std::string &data);
        /** Handle fota progress notification (in cellular)
         */
        sys::Message_t handleRawProgress(sys::DataMessage *req, sys::ResponseMessage *response);

        std::unique_ptr<sys::Timer> connectionTimer;
        void getApnConfiguration();
        void getConfig();
        void getActiveCotext();
        void stopActiveContext();
        void setState();
        bool isHTTPS(const std::string &url) const;
        void normalizeUrl(std::string &url) const;
        std::string prepareQIACT(unsigned char contextId);
        std::string prepareQICSGPcmd(const APN::Config &apn);
        std::string prepareQICSGPquery(const APN::Config &apn);
        std::string prepareQIDEACT(unsigned char contextId);
        std::string prepareQFOTADLcmd(const std::string &url);
        std::string prepareQHTTPGET(unsigned int timeout = 20);
        std::string prepareQHTTPREAD(unsigned int timeout = 20);
        std::string prepareQHTTPURL(const std::string &url);
        void setupSSLContext();
        bool openURL(const std::string &url);
        at::Result sendAndLogError(const std::string &msg) const;
        at::Result sendAndLogError(const std::string &msg, uint32_t timeout) const;
        void logIfError(const at::Result &result, const std::string &cmdString) const;
        void parseResponse();
        void parseQIACT(const at::Result &result);
        void parseQIND(const std::string &message);
        void sendProgress(unsigned int progress, const std::string &receiver);
        void sendFotaFinshed(const std::string &receiver);

        State state              = State ::Idle;
        DLC_channel *dataChannel = nullptr;
        APN::ContextMap contextMap;
        std::string url;
        std::string file;
        std::string receiverServiceName;
        unsigned char currentApnContext = 0;
    };

} // namespace FotaService