summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusProxyConnection.hpp
blob: 177bbe8327011e992b520c2e06635605dae2bf86 (plain)
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
// Copyright (C) 2013-2020 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_DBUS_DBUSPROXYCONNECTION_HPP_
#define COMMONAPI_DBUS_DBUSPROXYCONNECTION_HPP_

#include <cstdint>
#include <functional>
#include <future>
#include <memory>
#include <set>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

#include <CommonAPI/Attribute.hpp>
#include <CommonAPI/Event.hpp>
#include <CommonAPI/Types.hpp>
#include <CommonAPI/DBus/DBusConfig.hpp>
#include <CommonAPI/DBus/DBusError.hpp>
#include <CommonAPI/DBus/DBusMessage.hpp>
#include <CommonAPI/DBus/DBusFunctionalHash.hpp>

namespace CommonAPI {
namespace DBus {

typedef std::function<void(const DBusMessage&)> DBusMessageHandler;

class DBusDaemonProxy;
class DBusServiceRegistry;
class DBusObjectManager;
class DBusProxy;

class DBusProxyConnection {
 public:
    class DBusMessageReplyAsyncHandler {
     public:
       virtual ~DBusMessageReplyAsyncHandler() {}
       virtual std::future<CallStatus> getFuture() = 0;
       virtual void onDBusMessageReply(const CallStatus&, const DBusMessage&) = 0;
       virtual void setExecutionStarted() = 0;
       virtual bool getExecutionStarted() = 0;
       virtual void setExecutionFinished() = 0;
       virtual bool getExecutionFinished() = 0;
       virtual void setTimeoutOccurred() = 0;
       virtual bool getTimeoutOccurred() = 0;
       virtual void setHasToBeDeleted() = 0;
       virtual bool hasToBeDeleted() = 0;
       virtual void lock() = 0;
       virtual void unlock() = 0;
    };

    class DBusSignalHandler;

    // objectPath, interfaceName, interfaceMemberName, interfaceMemberSignature
    typedef std::tuple<std::string, std::string, std::string, std::string> DBusSignalHandlerPath;
    typedef std::unordered_map<DBusSignalHandlerPath,
                                std::map<const DBusSignalHandler*,
                                    std::weak_ptr<DBusProxyConnection::DBusSignalHandler>>> DBusSignalHandlerTable;
    typedef std::unordered_multimap<std::string, std::pair<const DBusSignalHandler*,
                                        std::weak_ptr<DBusSignalHandler>>> DBusOMSignalHandlerTable;
    typedef DBusSignalHandlerPath DBusSignalHandlerToken;

    class DBusSignalHandler {
     public:
        virtual ~DBusSignalHandler() {}
        virtual void onSignalDBusMessage(const DBusMessage&) = 0;
        virtual void onInitialValueSignalDBusMessage(const DBusMessage&, const uint32_t) {};
        virtual void onSpecificError(const CommonAPI::CallStatus, const uint32_t ) {};
        virtual void setSubscriptionToken(const DBusSignalHandlerToken, const uint32_t) {};
    };

    typedef Event<AvailabilityStatus> ConnectionStatusEvent;

    virtual ~DBusProxyConnection() {}

    virtual bool isConnected() const = 0;

    virtual ConnectionStatusEvent& getConnectionStatusEvent() = 0;

    virtual bool sendDBusMessage(const DBusMessage& dbusMessage) const = 0;

    virtual bool sendDBusMessageWithReplyAsync(
            const DBusMessage& dbusMessage,
            std::unique_ptr<DBusMessageReplyAsyncHandler> dbusMessageReplyAsyncHandler,
            const CommonAPI::CallInfo *_info) const = 0;

    virtual DBusMessage sendDBusMessageWithReplyAndBlock(
            const DBusMessage& dbusMessage,
            DBusError& dbusError,
            const CommonAPI::CallInfo *_info) const = 0;

    virtual DBusSignalHandlerToken addSignalMemberHandler(
            const std::string& objectPath,
            const std::string& interfaceName,
            const std::string& interfaceMemberName,
            const std::string& interfaceMemberSignature,
            std::weak_ptr<DBusSignalHandler> dbusSignalHandler,
            const bool justAddFilter = false) = 0;

    virtual void subscribeForSelectiveBroadcast(const std::string& objectPath,
                                                                  const std::string& interfaceName,
                                                                  const std::string& interfaceMemberName,
                                                                  const std::string& interfaceMemberSignature,
                                                                  std::weak_ptr<DBusSignalHandler> dbusSignalHandler,
                                                                  DBusProxy* callingProxy,
                                                                  uint32_t tag) = 0;

    virtual void unsubscribeFromSelectiveBroadcast(const std::string& eventName,
                                                  DBusProxyConnection::DBusSignalHandlerToken subscription,
                                                  DBusProxy* callingProxy,
                                                  const DBusSignalHandler* dbusSignalHandler) = 0;

    virtual bool removeSignalMemberHandler(const DBusSignalHandlerToken& dbusSignalHandlerToken,
                                           const DBusSignalHandler* dbusSignalHandler = NULL) = 0;

    virtual bool addObjectManagerSignalMemberHandler(const std::string& dbusBusName,
                                                     std::weak_ptr<DBusSignalHandler> dbusSignalHandler) = 0;
    virtual bool removeObjectManagerSignalMemberHandler(const std::string& dbusBusName,
                                                        const DBusSignalHandler* dbusSignalHandler) = 0;

    virtual const std::shared_ptr<DBusObjectManager> getDBusObjectManager() = 0;

    virtual void registerObjectPath(const std::string& objectPath) = 0;
    virtual void unregisterObjectPath(const std::string& objectPath) = 0;

    virtual bool requestServiceNameAndBlock(const std::string& serviceName) const = 0;
    virtual bool releaseServiceName(const std::string& serviceName) const = 0;

    typedef std::function<bool(const DBusMessage&)> DBusObjectPathMessageHandler;

    virtual void setObjectPathMessageHandler(DBusObjectPathMessageHandler) = 0;
    virtual bool isObjectPathMessageHandlerSet() = 0;

    virtual bool hasDispatchThread() = 0;

    virtual void sendPendingSelectiveSubscription(
            DBusProxy* proxy, std::string interfaceMemberName,
            std::weak_ptr<DBusSignalHandler> dbusSignalHandler, uint32_t tag,
            std::string interfaceMemberSignature) = 0;

    virtual void pushDBusMessageReplyToMainLoop(const DBusMessage& reply,
                             std::unique_ptr<DBusMessageReplyAsyncHandler> dbusMessageReplyAsyncHandler) = 0;

    template<class DBusConnection, class Function, class... Arguments>
    void proxyPushFunctionToMainLoop(Function&& _function, Arguments&& ... _args) {
        static_cast<DBusConnection*>(this)->proxyPushFunctionToMainLoop(std::forward<Function>(_function), std::forward<Arguments>(_args) ...);
    }

    virtual void addSignalStateHandler(
            std::shared_ptr<DBusProxyConnection::DBusSignalHandler> _handler,
            const uint32_t _subscription) = 0;

    virtual void removeSignalStateHandler(
                std::shared_ptr<DBusProxyConnection::DBusSignalHandler> _handler,
                const uint32_t _tag, bool _remove_all) = 0;

    virtual void handleSignalStates() = 0;
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_DBUSPROXYCONNECTION_HPP_