~aleteoryx/muditaos

muditaos/module-sys/Service/Message.cpp -rw-r--r-- 3.2 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
// 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/Message.hpp>
#include <Service/Service.hpp>

namespace sys
{
    SendResult CreateSendResult(ReturnCodes retCode, MessagePointer msg)
    {
        return std::make_pair(retCode, msg);
    }

    MessageUIDType MessageUID::get() const noexcept
    {
        return value;
    }

    MessageUIDType MessageUID::getNext() noexcept
    {
        if (value == invalidMessageUid) {
            value = 0;
        }
        return value++;
    }

    Message::Message(Type type) : type(type)
    {}

    Message::Message(Type type, BusChannel channel) : type(type), channel{channel}
    {}

    MessagePointer Message::Execute(Service *service)
    {
        return Proxy::handleMessage(service, this);
    }

    bool Message::ValidateMessage() const noexcept
    {
        return !(id == invalidMessageUid || type == Message::Type::Unspecified || sender == "Unknown");
    }

    void Message::ValidateUnicastMessage() const
    {
        if (!ValidateMessage() || uniID == invalidMessageUid || transType != Message::TransmissionType::Unicast) {
            throw std::runtime_error("Message unicast lack essential fields");
        }
    }

    void Message::ValidateResponseMessage() const
    {
        if (!ValidateMessage()) {
            throw std::runtime_error("Message response lack essential fields");
        }
    }

    void Message::ValidateBroadcastMessage() const
    {
        if (!ValidateMessage() || transType != Message::TransmissionType::Broadcast) {
            throw std::runtime_error("Message broadcast lack essential fields");
        }
    }

    void Message::ValidateMulticastMessage() const
    {
        if (!ValidateMessage() || channel == BusChannel::Unknown || transType != Message::TransmissionType::Multicast) {
            throw std::runtime_error("Message multicast lack essential fields");
        }
    }

    SystemMessage::SystemMessage(SystemMessageType systemMessageType, ServicePowerMode pwrMode)
        : Message(Type::System, BusChannel::System), systemMessageType(systemMessageType), powerMode(pwrMode)
    {}

    MessagePointer SystemMessage::Execute(Service *service)
    {
        return Proxy::handleSystemMessage(service, this);
    }

    ServiceCloseReasonMessage::ServiceCloseReasonMessage(CloseReason closeReason)
        : SystemMessage(SystemMessageType::ServiceCloseReason), closeReason(closeReason)
    {}

    CloseReason ServiceCloseReasonMessage::getCloseReason() const noexcept
    {
        return closeReason;
    }

    DataMessage::DataMessage(MessageType messageType) : Message(Type::Data), messageType{messageType}
    {}

    DataMessage::DataMessage(BusChannel channel) : Message(Type::Data, channel)
    {}

    DataMessage::DataMessage() : Message(Type::Data)
    {}

    ResponseMessage::ResponseMessage(ReturnCodes retCode, MessageType responseTo)
        : Message(Type::Response), retCode(retCode), responseTo(responseTo)
    {}

    MessagePointer ResponseMessage::Execute(Service *service)
    {
        return Proxy::handleMessage(service, nullptr, this);
    }

} // namespace sys