~aleteoryx/muditaos

muditaos/module-sys/Service/Service.cpp -rw-r--r-- 9.4 KiB
a405cad6Aleteoryx trim readme 6 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <Service/Service.hpp>
#include "FreeRTOSConfig.h"    // for configASSERT
#include "MessageType.hpp"     // for MessageType, MessageType::MessageType...
#include "Service/Mailbox.hpp" // for Mailbox
#include <Service/Message.hpp> // for Message, MessagePointer, DataMessage, Resp...
#include "Timers/SystemTimer.hpp"
#include "Timers/TimerHandle.hpp"  // for Timer
#include "Timers/TimerMessage.hpp" // for TimerMessage
#include <log/debug.hpp>           // for DEBUG_SERVICE_MESSAGES
#include <log/log.hpp>             // for LOG_ERROR, LOG_DEBUG, LOG_FATAL
#include "mutex.hpp"               // for cpp_freertos
#include "portmacro.h"             // for UBaseType_t
#include "thread.hpp"              // for Thread
#include "ticks.hpp"               // for Ticks
#include <algorithm>               // for remove_if
#include <cstdint>                 // for uint32_t, uint64_t, UINT32_MAX
#include <iosfwd>                  // for std
#include <typeinfo>                // for type_info
#include <system/Constants.hpp>

// this could use Scoped() class from utils to print execution time too
void debug_msg(sys::Service *srvc, const sys::Message *ptr)
{
#if (DEBUG_SERVICE_MESSAGES > 0)

    const char *realname = typeid(*ptr).name();

    assert(srvc);
    assert(ptr);

    if (xPortIsInsideInterrupt()) {
        throw std::runtime_error("message sent from irq");
    }
    if (xTaskGetCurrentTaskHandle() == nullptr) {
        throw std::runtime_error("message sent from crit section");
    }

    LOG_DEBUG("([%s] -> [%s] (%s) data: %s | %s",
              ptr ? ptr->sender.c_str() : "",
              srvc ? srvc->GetName().c_str() : "",
              realname,
              std::string(*ptr).c_str(),
              ptr->to_string().c_str());
#else
#endif
}

#if (DEBUG_SERVICE_MESSAGES > 0)
#define log_debug(...) LOG_DEBUG(__VA_ARGS__)
#else
#define log_debug(...)
#endif

namespace sys
{
    using namespace cpp_freertos;
    using namespace std;

    Service::Service(
        std::string name, std::string parent, uint32_t stackDepth, ServicePriority priority, Watchdog &watchdog)
        : cpp_freertos::Thread(name, stackDepth / 4 /* Stack depth in bytes */, static_cast<UBaseType_t>(priority)),
          parent(parent), bus(this, watchdog), mailbox(this), watchdog(watchdog), isReady(false), enableRunLoop(false)
    {}

    Service::~Service()
    {
        enableRunLoop = false;
        LOG_DEBUG("%s", (GetName() + ":Service base destructor").c_str());
    }

    void Service::StartService()
    {
        bus.connect();
        enableRunLoop = true;
        if (!Start()) {
            LOG_FATAL("Start service failed!:%s", GetName().c_str());
            configASSERT(0);
        }
    }

    void Service::CloseService()
    {
        bus.disconnect();
    }

    void Service::Run()
    {
        while (enableRunLoop) {
            processBus();
        }
        CloseService();
    };

    void Service::processBus()
    {
        if (auto msg = mailbox.pop(); msg) {
            const bool respond  = msg->type != Message::Type::Response && GetName() != msg->sender;
            currentlyProcessing = msg;
            auto response       = msg->Execute(this);
            if (response == nullptr || !respond) {
                return;
            }
            bus.sendResponse(response, msg);
        }
    }

    auto Service::MessageEntry(Message *message, ResponseMessage *response) -> MessagePointer
    {
        return response == nullptr ? HandleMessage(message) : HandleResponse(response);
    }

    auto Service::HandleMessage(Message *message) -> MessagePointer
    {
        debug_msg(this, message);

        if (const auto &[handled, ret] = ExecuteMessageHandler(message); handled) {
            return ret;
        }
        if (auto dataMsg = dynamic_cast<DataMessage *>(message); dataMsg != nullptr) {
            return DataReceivedHandler(dataMsg, nullptr);
        }

        LOG_ERROR("Failed to handle message of type [%s]", typeid(*message).name());
        return nullptr;
    }

    auto Service::HandleResponse(ResponseMessage *message) -> MessagePointer
    {
        debug_msg(this, message);

        if (const auto &[handled, ret] = ExecuteMessageHandler(message); handled) {
            return ret;
        }
        DataMessage dummy(MessageType::MessageTypeUninitialized);
        return DataReceivedHandler(&dummy, message);
    }

    auto Service::ExecuteMessageHandler(Message *message) -> std::pair<bool, MessagePointer>
    {
        const auto idx = type_index(typeid(*message));
        if (const auto handler = message_handlers.find(idx); handler != message_handlers.end()) {
            const auto &handlerFunction = handler->second;
            if (handlerFunction == nullptr) {
                return {true, nullptr};
            }
            return {true, handlerFunction(message)};
        }
        return {false, nullptr};
    }

    bool Service::connect(const type_info &type, MessageHandler handler)
    {
        auto idx = type_index(type);
        if (message_handlers.find(idx) == message_handlers.end()) {
            log_debug("Registering new message handler on %s", type.name());
            message_handlers[idx] = std::move(handler);
            return true;
        }
        LOG_ERROR("Handler for: %s already registered!", type.name());
        return false;
    }

    bool Service::connect(Message *msg, MessageHandler handler)
    {
        auto &type = typeid(*msg);
        return connect(type, handler);
    }

    bool Service::connect(Message &&msg, MessageHandler handler)
    {
        return connect(&msg, handler);
    }

    bool Service::disconnect(const std::type_info &type)
    {
        auto iter = message_handlers.find(type_index(type));
        if (iter == std::end(message_handlers)) {
            return false;
        }
        message_handlers.erase(iter);
        return true;
    }

    void Service::CloseHandler()
    {
        timers.stop();
        enableRunLoop = false;
    }

    auto Service::ProcessCloseReason(CloseReason closeReason) -> void{};

    auto Service::ProcessCloseReasonHandler(CloseReason closeReason) -> void
    {
        ProcessCloseReason(closeReason);
        // to reject all messages except system ones
        isReady = false;
        sendCloseReadyMessage(this);
    };

    auto Service::TimerHandle(SystemMessage &message) -> ReturnCodes
    {
        auto timer_message = dynamic_cast<sys::TimerMessage *>(&message);
        if (timer_message == nullptr) {
            LOG_ERROR("Wrong message in system message handler");
            return ReturnCodes::Failure;
        }

        auto sysTimer = timer_message->getTimer();
        auto timer    = timers.get(sysTimer);
        if (timer == nullptr) {
            LOG_ERROR("No such timer registered in Service");
            return ReturnCodes::Failure;
        }
        timer->onTimeout();
        return ReturnCodes::Success;
    }

    void Service::Timers::attach(timer::SystemTimer *timer)
    {
        list.push_back(timer);
    }

    void Service::Timers::detach(timer::SystemTimer *timer)
    {
        const auto it = std::find(list.begin(), list.end(), timer);
        if (it != list.end()) {
            list.erase(it);
        }
    }

    void Service::Timers::stop()
    {
        for (auto timer : list) {
            timer->stop();
        }
    }

    auto Service::Timers::get(timer::SystemTimer *timer) noexcept -> timer::SystemTimer *
    {
        auto it = std::find(list.begin(), list.end(), timer);
        if (it == list.end()) {
            return nullptr;
        }
        return *it;
    }

    void Service::sendCloseReadyMessage(Service *service)
    {
        auto msg = std::make_shared<sys::ReadyToCloseMessage>();
        service->bus.sendUnicast(std::move(msg), service::name::system_manager);
    }

    std::string Service::getCurrentProcessing()
    {
        return currentlyProcessing ? std::string(typeid(*currentlyProcessing).name()) : "nothing in progress";
    }

    auto Proxy::handleMessage(Service *service, Message *message, ResponseMessage *response) -> MessagePointer
    {
        if (service->isReady) {
            return service->MessageEntry(message, response);
        }
        return std::make_shared<ResponseMessage>(ReturnCodes::ServiceDoesntExist);
    }

    auto Proxy::handleSystemMessage(Service *service, SystemMessage *message) -> MessagePointer
    {
        auto ret = ReturnCodes::Success;
        switch (message->systemMessageType) {
        case SystemMessageType::SwitchPowerMode:
            service->SwitchPowerModeHandler(message->powerMode);
            break;
        case SystemMessageType::Exit:
            ret = service->DeinitHandler();
            service->CloseHandler();
            break;
        case SystemMessageType::Timer:
            ret = service->TimerHandle(*message);
            break;
        case SystemMessageType::Start:
            ret = service->InitHandler();
            if (ret == ReturnCodes::Success) {
                service->isReady = true;
            }
            break;
        case SystemMessageType::ServiceCloseReason:
            service->ProcessCloseReasonHandler(static_cast<ServiceCloseReasonMessage *>(message)->getCloseReason());
            break;
        }
        return std::make_shared<ResponseMessage>(ret);
    }

    bool Service::isConnected(std::type_index idx)
    {
        return message_handlers.find(idx) != message_handlers.end();
    }
} // namespace sys