~aleteoryx/muditaos

ref: 8204cba1ab2566d53736e729b3929df59f0889cd muditaos/module-sys/Service/Bus.hpp -rw-r--r-- 1.7 KiB
8204cba1 — Wojtek Rzepecki [EGD-5575] Add battery file test 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
// 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 <memory>
#include <map>
#include <queue>
#include "Channel.hpp"
#include "Common.hpp"
#include "Message.hpp"

namespace sys
{
    class Service;
    class Message;

    constexpr uint32_t defaultCmdTimeout = 5000;
    class Bus
    {

      public:
        // Send message directly to specified service (unicast type)
        static bool SendUnicast(std::shared_ptr<Message> msg, const std::string &service, Service *s);

        // Send message directly to specified service with timeout
        static SendResult SendUnicast(std::shared_ptr<Message> msg,
                                      const std::string &service,
                                      Service *s,
                                      uint32_t timeout);

        // Send message to specific channel
        static void SendMulticast(std::shared_ptr<Message> msg, BusChannels channel, Service *source);

        // Send message to all registered services ()
        static void SendBroadcast(std::shared_ptr<Message> msg, Service *source);

        // Register new service
        static void Add(std::shared_ptr<Service> service);

        // Unregister specified service
        static void Remove(std::shared_ptr<Service> service);

        static void SendResponse(std::shared_ptr<Message> response, std::shared_ptr<Message> request, Service *sender);

      private:
        Bus();
        ~Bus();

        static std::map<BusChannels, Channel> channels;
        static std::map<std::string, std::shared_ptr<Service>> servicesRegistered;

        static uint64_t uniqueMsgId;
        static uint64_t unicastMsgId;
    };

} // namespace sys