1 /**
2  * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
3  * (C) 2016 - 2019 Stanislav Angelovic <angelovic.s@gmail.com>
4  *
5  * @file TestAdaptor.h
6  *
7  * Created on: Jan 2, 2017
8  * Project: sdbus-c++
9  * Description: High-level D-Bus IPC C++ library based on sd-bus
10  *
11  * This file is part of sdbus-c++.
12  *
13  * sdbus-c++ is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * sdbus-c++ is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #ifndef SDBUS_CPP_INTEGRATIONTESTS_TESTPROXY_H_
28 #define SDBUS_CPP_INTEGRATIONTESTS_TESTPROXY_H_
29 
30 #include "integrationtests-proxy.h"
31 #include "Defs.h"
32 #include <thread>
33 #include <chrono>
34 #include <atomic>
35 
36 namespace sdbus { namespace test {
37 
38 class ObjectManagerTestProxy final : public sdbus::ProxyInterfaces< sdbus::ObjectManager_proxy >
39 {
40 public:
ObjectManagerTestProxy(sdbus::IConnection & connection,std::string destination,std::string objectPath)41     ObjectManagerTestProxy(sdbus::IConnection& connection, std::string destination, std::string objectPath)
42         : ProxyInterfaces(connection, std::move(destination), std::move(objectPath))
43     {
44         registerProxy();
45     }
46 
~ObjectManagerTestProxy()47     ~ObjectManagerTestProxy()
48     {
49         unregisterProxy();
50     }
51 protected:
onInterfacesAdded(const sdbus::ObjectPath & objectPath,const std::map<std::string,std::map<std::string,sdbus::Variant>> & interfacesAndProperties)52     void onInterfacesAdded(const sdbus::ObjectPath& objectPath, const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) override
53     {
54         if (m_onInterfacesAddedHandler)
55             m_onInterfacesAddedHandler(objectPath, interfacesAndProperties);
56     }
57 
onInterfacesRemoved(const sdbus::ObjectPath & objectPath,const std::vector<std::string> & interfaces)58     void onInterfacesRemoved(const sdbus::ObjectPath& objectPath, const std::vector<std::string>& interfaces) override
59     {
60         if (m_onInterfacesRemovedHandler)
61             m_onInterfacesRemovedHandler(objectPath, interfaces);
62     }
63 
64 public: // for tests
65     std::function<void(const sdbus::ObjectPath&, const std::map<std::string, std::map<std::string, sdbus::Variant>>&)> m_onInterfacesAddedHandler;
66     std::function<void(const sdbus::ObjectPath&, const std::vector<std::string>&)> m_onInterfacesRemovedHandler;
67 };
68 
69 class TestProxy final : public sdbus::ProxyInterfaces< org::sdbuscpp::integrationtests_proxy
70                                                      , sdbus::Peer_proxy
71                                                      , sdbus::Introspectable_proxy
72                                                      , sdbus::Properties_proxy >
73 {
74 public:
75     TestProxy(std::string destination, std::string objectPath);
76     TestProxy(sdbus::IConnection& connection, std::string destination, std::string objectPath);
77     ~TestProxy();
78 
79 protected:
80     void onSimpleSignal() override;
81     void onSignalWithMap(const std::map<int32_t, std::string>& aMap) override;
82     void onSignalWithVariant(const sdbus::Variant& aVariant) override;
83 
84     void onSignalWithoutRegistration(const sdbus::Struct<std::string, sdbus::Struct<sdbus::Signature>>& s);
85     void onDoOperationReply(uint32_t returnValue, const sdbus::Error* error);
86 
87     // Signals of standard D-Bus interfaces
88     void onPropertiesChanged( const std::string& interfaceName
89                             , const std::map<std::string, sdbus::Variant>& changedProperties
90                             , const std::vector<std::string>& invalidatedProperties ) override;
91 
92 public:
93     void installDoOperationClientSideAsyncReplyHandler(std::function<void(uint32_t res, const sdbus::Error* err)> handler);
94     uint32_t doOperationWithTimeout(const std::chrono::microseconds &timeout, uint32_t param);
95     sdbus::PendingAsyncCall doOperationClientSideAsync(uint32_t param);
96     void doErroneousOperationClientSideAsync();
97     void doOperationClientSideAsyncWithTimeout(const std::chrono::microseconds &timeout, uint32_t param);
98     int32_t callNonexistentMethod();
99     int32_t callMethodOnNonexistentInterface();
100     void setStateProperty(const std::string& value);
101 
102 //private:
103 public: // for tests
104     int m_SimpleSignals = 0;
105     std::atomic<bool> m_gotSimpleSignal{false};
106     std::atomic<bool> m_gotSignalWithMap{false};
107     std::map<int32_t, std::string> m_mapFromSignal;
108     std::atomic<bool> m_gotSignalWithVariant{false};
109     double m_variantFromSignal;
110     std::atomic<bool> m_gotSignalWithSignature{false};
111     std::map<std::string, std::string> m_signatureFromSignal;
112 
113     std::function<void(uint32_t res, const sdbus::Error* err)> m_DoOperationClientSideAsyncReplyHandler;
114     std::function<void(const std::string&, const std::map<std::string, sdbus::Variant>&, const std::vector<std::string>&)> m_onPropertiesChangedHandler;
115 
116     const Message* m_signalMsg{};
117     std::string m_signalMemberName;
118 };
119 
120 }}
121 
122 #endif /* SDBUS_CPP_INTEGRATIONTESTS_TESTPROXY_H_ */
123