~aleteoryx/muditaos

ref: d97688135029446a9f07d0d0884e247f00682ead muditaos/module-sys/Service/Service.hpp -rw-r--r-- 6.1 KiB
d9768813 — Radoslaw Wicik [EGD-5823] Fix Use full hash in artefacts 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "Timer.hpp"
#include "BusProxy.hpp"
#include "Common.hpp"  // for ReturnCodes, ServicePriority, BusChannels
#include "Mailbox.hpp" // for Mailbox
#include "Message.hpp" // for MessagePointer
#include "ServiceManifest.hpp"
#include "thread.hpp" // for Thread
#include <algorithm>  // for find, max
#include <cstdint>    // for uint32_t, uint64_t
#include <functional> // for function
#include <iterator>   // for end
#include <map>        // for map
#include <memory>     // for allocator, shared_ptr, enable_shared_from_this
#include <string>     // for string
#include <typeindex>  // for type_index
#include <utility>    // for pair
#include <vector>     // for vector<>::iterator, vector
#include <typeinfo>   // for connect by type

namespace sys
{
    class Timer;
} // namespace sys
namespace sys
{
    struct Proxy;
} // namespace sys

namespace sys
{
    using MessageHandler = std::function<MessagePointer(Message *)>;

    class Service : public cpp_freertos::Thread, public std::enable_shared_from_this<Service>
    {
      public:
        Service(std::string name,
                std::string parent       = "",
                uint32_t stackDepth      = 4096,
                ServicePriority priority = ServicePriority::Idle);

        ~Service() override;

        void StartService();
        void CloseService();

        // Invoked for not processed already messages
        // override should in in either callback, function or whatever...
        [[deprecated("Use connect method instead.")]] virtual MessagePointer DataReceivedHandler(
            DataMessage *msg, ResponseMessage *resp) = 0;

        // Invoked during initialization
        virtual ReturnCodes InitHandler() = 0;

        /**
         * @brief Handler to gently release system resources, such as worker
         * threads. All other resources should be freed in a destructor.
         *
         * @return ReturnCodes
         */
        virtual ReturnCodes DeinitHandler() = 0;

        virtual ReturnCodes SwitchPowerModeHandler(const ServicePowerMode mode) = 0;

        /**
         * @brief Ends stops service's thread and its timers.
         */
        virtual void CloseHandler() final;

        std::string parent;

        BusProxy bus;

        Mailbox<std::shared_ptr<Message>> mailbox;

        uint32_t pingTimestamp;

        bool isReady;

        std::vector<std::pair<uint64_t, uint32_t>> staleUniqueMsg;

        /// connect: register message handler
        bool connect(const std::type_info &type, MessageHandler handler);
        bool connect(Message *msg, MessageHandler handler);
        bool connect(Message &&msg, MessageHandler handler);

      protected:
        bool enableRunLoop;

        void Run() override;

        std::map<std::type_index, MessageHandler> message_handlers;

      private:
        /// first point of enttry on messages - actually used method in run
        /// First calls message_handlers
        /// If not - fallback to DataReceivedHandler
        auto MessageEntry(Message *message, ResponseMessage *response) -> MessagePointer;
        auto HandleMessage(Message *message) -> MessagePointer;
        auto HandleResponse(ResponseMessage *message) -> MessagePointer;
        /// Execute a message handler functor, if found one.
        /// \param message  Request message
        /// \return A pair of:
        /// - True if message handler called, false otherwise.
        /// - A response message on success, nullptr otherwise.
        auto ExecuteMessageHandler(Message *message) -> std::pair<bool, MessagePointer>;

        friend Proxy;

        class Timers
        {

            friend Timer;

          private:
            std::vector<Timer *> list;
            auto attach(Timer *timer)
            {
                list.push_back(timer);
            }

            void detach(Timer *timer)
            {
                auto it = std::find(list.begin(), list.end(), timer);

                if (it != list.end()) {
                    list.erase(it);
                }
            }

            auto findTimer(const std::string &timerName) const
            {
                return std::find_if(
                    list.begin(), list.end(), [&, timerName](auto &el) -> bool { return el->getName() == timerName; });
            }

          public:
            void stop();

            [[nodiscard]] auto get(Timer *timer) const
            {
                return std::find(list.begin(), list.end(), timer);
            }

            [[nodiscard]] auto timerExists(const std::string &timerName) const
            {
                return findTimer(timerName) != list.end();
            }

            void detachTimer(const std::string &timerName)
            {
                auto it = findTimer(timerName);

                if (it != list.end()) {
                    (*it)->stop();
                    detach((*it));
                }
            }

            [[nodiscard]] auto noTimer() const
            {
                return std::end(list);
            }

        } timers;

      public:
        [[nodiscard]] auto getTimers() -> auto &
        {
            return timers;
        }

        [[nodiscard]] auto timerExists(const std::string &timerName) -> bool
        {
            return timers.timerExists(timerName);
        }

        void detachTimer(const std::string &timerName)
        {
            timers.detachTimer(timerName);
        }

        auto TimerHandle(SystemMessage &message) -> ReturnCodes;
    };

    /// proxy has one objective - be friend for Service, so that Message which is not a friend could access
    /// one and only one entrypoint to messages entrypoint (MessageEntry)
    /// MessageEntry calls overridable DataReceivedHandler for Service instance and all Calls that are 100% neccessary
    /// for service
    struct Proxy
    {
        static auto handleMessage(Service *service, Message *message, ResponseMessage *response = nullptr)
            -> MessagePointer;
        static auto handleSystemMessage(Service *service, SystemMessage *message) -> MessagePointer;
    };
} // namespace sys