1 /**
2  * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
3  * (C) 2016 - 2019 Stanislav Angelovic <angelovic.s@gmail.com>
4  *
5  * @file TestAdaptor.cpp
6  *
7  * Created on: May 23, 2020
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 #include "TestProxy.h"
28 #include <thread>
29 #include <chrono>
30 #include <atomic>
31 
32 namespace sdbus { namespace test {
33 
TestProxy(std::string destination,std::string objectPath)34 TestProxy::TestProxy(std::string destination, std::string objectPath)
35     : ProxyInterfaces(std::move(destination), std::move(objectPath))
36 {
37     getProxy().uponSignal("signalWithoutRegistration").onInterface(sdbus::test::INTERFACE_NAME).call([this](const sdbus::Struct<std::string, sdbus::Struct<sdbus::Signature>>& s){ this->onSignalWithoutRegistration(s); });
38 
39     registerProxy();
40 }
41 
TestProxy(sdbus::IConnection & connection,std::string destination,std::string objectPath)42 TestProxy::TestProxy(sdbus::IConnection& connection, std::string destination, std::string objectPath)
43     : ProxyInterfaces(connection, std::move(destination), std::move(objectPath))
44 {
45     getProxy().uponSignal("signalWithoutRegistration").onInterface(sdbus::test::INTERFACE_NAME).call([this](const sdbus::Struct<std::string, sdbus::Struct<sdbus::Signature>>& s){ this->onSignalWithoutRegistration(s); });
46 
47     registerProxy();
48 }
49 
~TestProxy()50 TestProxy::~TestProxy()
51 {
52     unregisterProxy();
53 }
54 
onSimpleSignal()55 void TestProxy::onSimpleSignal()
56 {
57     m_signalMsg = getProxy().getCurrentlyProcessedMessage();
58     m_signalMemberName = m_signalMsg->getMemberName();
59 
60     m_gotSimpleSignal = true;
61 }
62 
onSignalWithMap(const std::map<int32_t,std::string> & aMap)63 void TestProxy::onSignalWithMap(const std::map<int32_t, std::string>& aMap)
64 {
65     m_mapFromSignal = aMap;
66     m_gotSignalWithMap = true;
67 }
68 
onSignalWithVariant(const sdbus::Variant & aVariant)69 void TestProxy::onSignalWithVariant(const sdbus::Variant& aVariant)
70 {
71     m_variantFromSignal = aVariant.get<double>();
72     m_gotSignalWithVariant = true;
73 }
74 
onSignalWithoutRegistration(const sdbus::Struct<std::string,sdbus::Struct<sdbus::Signature>> & s)75 void TestProxy::onSignalWithoutRegistration(const sdbus::Struct<std::string, sdbus::Struct<sdbus::Signature>>& s)
76 {
77     m_signatureFromSignal[std::get<0>(s)] = static_cast<std::string>(std::get<0>(std::get<1>(s)));
78     m_gotSignalWithSignature = true;
79 }
80 
onDoOperationReply(uint32_t returnValue,const sdbus::Error * error)81 void TestProxy::onDoOperationReply(uint32_t returnValue, const sdbus::Error* error)
82 {
83     if (m_DoOperationClientSideAsyncReplyHandler)
84         m_DoOperationClientSideAsyncReplyHandler(returnValue, error);
85 }
86 
onPropertiesChanged(const std::string & interfaceName,const std::map<std::string,sdbus::Variant> & changedProperties,const std::vector<std::string> & invalidatedProperties)87 void TestProxy::onPropertiesChanged( const std::string& interfaceName
88                                    , const std::map<std::string, sdbus::Variant>& changedProperties
89                                    , const std::vector<std::string>& invalidatedProperties )
90 {
91     if (m_onPropertiesChangedHandler)
92         m_onPropertiesChangedHandler(interfaceName, changedProperties, invalidatedProperties);
93 }
94 
installDoOperationClientSideAsyncReplyHandler(std::function<void (uint32_t res,const sdbus::Error * err)> handler)95 void TestProxy::installDoOperationClientSideAsyncReplyHandler(std::function<void(uint32_t res, const sdbus::Error* err)> handler)
96 {
97     m_DoOperationClientSideAsyncReplyHandler = std::move(handler);
98 }
99 
doOperationWithTimeout(const std::chrono::microseconds & timeout,uint32_t param)100 uint32_t TestProxy::doOperationWithTimeout(const std::chrono::microseconds &timeout, uint32_t param)
101 {
102     using namespace std::chrono_literals;
103     uint32_t result;
104     getProxy().callMethod("doOperation").onInterface(sdbus::test::INTERFACE_NAME).withTimeout(timeout).withArguments(param).storeResultsTo(result);
105     return result;
106 }
107 
doOperationClientSideAsync(uint32_t param)108 sdbus::PendingAsyncCall TestProxy::doOperationClientSideAsync(uint32_t param)
109 {
110     return getProxy().callMethodAsync("doOperation")
111                      .onInterface(sdbus::test::INTERFACE_NAME)
112                      .withArguments(param)
113                      .uponReplyInvoke([this](const sdbus::Error* error, uint32_t returnValue)
114                                       {
115                                           this->onDoOperationReply(returnValue, error);
116                                       });
117 }
118 
doErroneousOperationClientSideAsync()119 void TestProxy::doErroneousOperationClientSideAsync()
120 {
121     getProxy().callMethodAsync("throwError")
122               .onInterface(sdbus::test::INTERFACE_NAME)
123               .uponReplyInvoke([this](const sdbus::Error* error)
124                                {
125                                    this->onDoOperationReply(0, error);
126                                });
127 }
128 
doOperationClientSideAsyncWithTimeout(const std::chrono::microseconds & timeout,uint32_t param)129 void TestProxy::doOperationClientSideAsyncWithTimeout(const std::chrono::microseconds &timeout, uint32_t param)
130 {
131     using namespace std::chrono_literals;
132     getProxy().callMethodAsync("doOperation")
133               .onInterface(sdbus::test::INTERFACE_NAME)
134               .withTimeout(timeout)
135               .withArguments(param)
136               .uponReplyInvoke([this](const sdbus::Error* error, uint32_t returnValue)
137                                {
138                                    this->onDoOperationReply(returnValue, error);
139                                });
140 }
141 
callNonexistentMethod()142 int32_t TestProxy::callNonexistentMethod()
143 {
144     int32_t result;
145     getProxy().callMethod("callNonexistentMethod").onInterface(sdbus::test::INTERFACE_NAME).storeResultsTo(result);
146     return result;
147 }
148 
callMethodOnNonexistentInterface()149 int32_t TestProxy::callMethodOnNonexistentInterface()
150 {
151     int32_t result;
152     getProxy().callMethod("someMethod").onInterface("sdbuscpp.interface.that.does.not.exist").storeResultsTo(result);
153     return result;
154 }
155 
setStateProperty(const std::string & value)156 void TestProxy::setStateProperty(const std::string& value)
157 {
158     getProxy().setProperty("state").onInterface(sdbus::test::INTERFACE_NAME).toValue(value);
159 }
160 
161 }}
162