~aleteoryx/muditaos

ref: 92f028ddb66d22ad9936cecf3f34c5aa052faf65 muditaos/module-sys/Service/Message.hpp -rw-r--r-- 4.9 KiB
92f028dd — Radoslaw Wicik [EGD-6649] Fix SMS Record tests 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
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
// 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 <module-utils/magic_enum/include/magic_enum.hpp>
#include "Common.hpp"
#include "MessageType.hpp"
#include "MessageForward.hpp"

#include <cstdint>
#include <memory>
#include <string>

namespace sys
{
    SendResult CreateSendResult(ReturnCodes retCode, MessagePointer msg);

    inline constexpr std::uint64_t invalidMessageUid = std::numeric_limits<uint64_t>().max();

    using MessageUIDType = std::uint64_t;

    class MessageUID
    {
      protected:
        MessageUIDType value = 0;

      public:
        [[nodiscard]] MessageUIDType get() const noexcept;
        [[nodiscard]] MessageUIDType getNext() noexcept;
    };

    class Message
    {
      public:
        enum class TransmissionType
        {
            Unspecified,
            Unicast,
            Multicast,
            Broadcast
        };

        enum class Type
        {
            Unspecified,
            System,
            Data,
            Response
        };

        explicit Message(Type type);
        Message(Type type, BusChannel channel);
        virtual ~Message() noexcept = default;

        virtual MessagePointer Execute(Service *service);

        virtual explicit operator std::string() const
        {
            return {"{}"};
        }

        MessageUIDType id          = invalidMessageUid;
        MessageUIDType uniID       = invalidMessageUid;
        Type type                  = Type::Unspecified;
        TransmissionType transType = TransmissionType::Unspecified;
        BusChannel channel         = BusChannel::Unknown;
        std::string sender         = "Unknown";

        [[nodiscard]] std::string to_string() const
        {
            return "| ID:" + std::to_string(id) + " | uniID: " + std::to_string(uniID) +
                   " | Type: " + std::string(magic_enum::enum_name(type)) +
                   " | TransmissionType: " + std::string(magic_enum::enum_name(transType)) +
                   " | Channel: " + std::string(magic_enum::enum_name(channel)) + " | Sender: " + sender + " |";
        }

        /**
         * Validate base message have all essential fields set.
         * @return Return validation result.
         */
        [[nodiscard]] bool ValidateMessage() const noexcept;

        /**
         * Validate if response message have all essential fields set.
         * @return Return validation result.
         */
        void ValidateResponseMessage() const;

        /**
         * Validate if message sent via Unicast have all essential fields set.
         * @return Return validation result.
         */
        void ValidateUnicastMessage() const;

        /**
         * Validate if message sent via Broadcast have all essential fields set.
         * @return Return validation result.
         */
        void ValidateBroadcastMessage() const;

        /**
         * Validate if message sent via Multicast have all essential fields set.
         * @return Return validation result.
         */
        void ValidateMulticastMessage() const;
    };

    enum class SystemMessageType
    {
        Ping,
        SwitchPowerMode,
        Start,
        Timer,
        Exit,
        ServiceCloseReason
    };

    class SystemMessage : public Message
    {
      public:
        explicit SystemMessage(SystemMessageType systemMessageType,
                               ServicePowerMode pwrMode = ServicePowerMode::Active);

        MessagePointer Execute(Service *service) final;

        SystemMessageType systemMessageType;
        ServicePowerMode powerMode;
    };

    class ServiceCloseReasonMessage : public SystemMessage
    {
      public:
        explicit ServiceCloseReasonMessage(CloseReason closeReason);

        [[nodiscard]] CloseReason getCloseReason() const noexcept;

      private:
        const CloseReason closeReason;
    };

    class DataMessage : public Message
    {
      public:
        explicit DataMessage(MessageType messageType);
        explicit DataMessage(BusChannel channel);
        DataMessage();

        // This field must by provided by the class that inherits DataMessage
        MessageType messageType = MessageType::MessageTypeUninitialized;
    };

    class ReadyToCloseMessage : public DataMessage
    {};

    class ResponseMessage : public Message
    {
      public:
        explicit ResponseMessage(ReturnCodes retCode    = ReturnCodes::Success,
                                 MessageType responseTo = MessageType::MessageTypeUninitialized);

        MessagePointer Execute(Service *service) final;

        ReturnCodes retCode;
        MessageType responseTo;
    };

    inline auto msgHandled() -> MessagePointer
    {
        return std::make_shared<ResponseMessage>();
    }

    inline auto msgNotHandled() -> MessagePointer
    {
        return std::make_shared<ResponseMessage>(ReturnCodes::Unresolved);
    }
} // namespace sys