1 /* Copyright (C) 2013 BMW Group
2 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3 * Author: Juergen Gehring (juergen.gehring@bmw.de)
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <CommonAPI/DBus/DBusFunctionalHash.h>
8 #include <CommonAPI/DBus/DBusStubAdapterHelper.h>
14 static uint32_t dispatchedMessageCount;
16 class TestStubRemoteEventHandler {
18 virtual ~TestStubRemoteEventHandler() { }
20 virtual bool onRemoteSetTestAttribute(int32_t testValue) = 0;
22 virtual void onRemoteTestAttributeChanged() = 0;
25 class TestStubAdapter: virtual public CommonAPI::StubAdapter {
27 virtual void fireTestAttributeChanged(const int32_t& testValue) = 0;
30 class TestStub: public CommonAPI::Stub<TestStubAdapter, TestStubRemoteEventHandler> {
32 TestStub(): remoteEventHandler_(this) {
35 virtual TestStubRemoteEventHandler* initStubAdapter(const std::shared_ptr<TestStubAdapter>& stubAdapter) {
36 return &remoteEventHandler_;
39 void getEmptyResponse() {
40 std::cout << "onGetEmptyResponse()\n";
41 dispatchedMessageCount++;
44 void getDeepCopy(int32_t inValue, int32_t& outValue) {
46 std::cout << "getDeepCopy(inValue=" << inValue << ", outValue=" << outValue << ")\n";
47 dispatchedMessageCount++;
50 void getDeepCopies(std::vector<int32_t> inValues, std::vector<int32_t>& outValues) {
52 std::cout << "getDeepCopies(inValues=" << inValues.size() << ", outValues=" << outValues.size() << ")\n";
53 dispatchedMessageCount++;
56 void getShallowCopy(int32_t inValue, int32_t& outValue) {
58 std::cout << "getShallowCopy(inValue=" << inValue << ", outValue=" << outValue << ")\n";
59 dispatchedMessageCount++;
62 void getShallowCopies(std::vector<int32_t> inValues, std::vector<int32_t>& outValues) {
64 std::cout << "getShallowCopies(inValues=" << inValues.size() << ", outValues=" << outValues.size() << ")\n";
65 dispatchedMessageCount++;
68 const int32_t& getTestAttribute() {
69 return testAttributeValue_;
73 class RemoteEventHandler: public TestStubRemoteEventHandler {
75 RemoteEventHandler(TestStub* stub): stub_(stub) {
78 virtual bool onRemoteSetTestAttribute(int32_t testValue) {
79 std::cout << "RemoteEventHandler::onRemoteSetTestAttribute(" << testValue << "): oldValue = " << stub_->testAttributeValue_ << "\n";
80 const bool valueChanged = (stub_->testAttributeValue_ != testValue);
81 stub_->testAttributeValue_ = testValue;
85 virtual void onRemoteTestAttributeChanged() {
86 std::cout << "RemoteEventHandler::onRemoteTestAttributeChanged()\n";
93 RemoteEventHandler remoteEventHandler_;
94 int32_t testAttributeValue_;
97 typedef CommonAPI::DBus::DBusStubAdapterHelper<TestStub> TestStubAdapterHelper;
99 class TestDBusStubAdapter: public TestStubAdapter, public TestStubAdapterHelper {
101 TestDBusStubAdapter(const std::string& dbusBusName,
102 const std::string& dbusObjectPath,
103 const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection,
104 const std::shared_ptr<TestStub>& testStub) :
105 TestStubAdapterHelper(
108 "org.genivi.CommonAPI.DBus.TestInterface",
113 virtual void fireTestAttributeChanged(const int32_t& testValue) {
114 std::cout << "TestDBusStubAdapter::fireTestAttributeChanged(" << testValue << ")\n";
118 virtual const char* getMethodsDBusIntrospectionXmlData() const;
121 const char* TestDBusStubAdapter::getMethodsDBusIntrospectionXmlData() const {
122 return "<method name=\"GetEmptyResponse\">\n"
124 "<method name=\"GetDeepCopy\">\n"
125 "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
126 "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
128 "<method name=\"GetDeepCopies\">\n"
129 "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
130 "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
132 "<method name=\"GetShallowCopy\">\n"
133 "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
134 "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
136 "<method name=\"GetShallowCopies\">\n"
137 "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
138 "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
140 "<method name=\"SetTestAttribute\">\n"
141 "<arg type=\"i\" name=\"value\" direction=\"in\"/>\n"
142 "<arg type=\"i\" name=\"value\" direction=\"out\"/>\n"
148 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
151 std::tuple<> > getEmptyResponseStubDispatcher(&TestStub::getEmptyResponse, "");
152 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
155 std::tuple<int32_t> > getDeepCopyStubDispatcher(&TestStub::getDeepCopy, "i");
156 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
158 std::tuple<std::vector<int32_t>>,
159 std::tuple<std::vector<int32_t>> > getDeepCopiesStubDispatcher(&TestStub::getDeepCopies, "ai");
160 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
163 std::tuple<int32_t> > getShallowCopyStubDispatcher(&TestStub::getShallowCopy, "i");
164 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
166 std::tuple<std::vector<int32_t>>,
167 std::tuple<std::vector<int32_t>> > getShallowCopiesStubDispatcher(&TestStub::getShallowCopies, "ai");
168 static CommonAPI::DBus::DBusSetObservableAttributeStubDispatcher<TestStub, int32_t> setTestAttributeStubDispatcher(
169 &TestStub::getTestAttribute,
170 &TestStubRemoteEventHandler::onRemoteSetTestAttribute,
171 &TestStubRemoteEventHandler::onRemoteTestAttributeChanged,
172 &TestStubAdapter::fireTestAttributeChanged,
177 const TestStubAdapterHelper::StubDispatcherTable TestStubAdapterHelper::stubDispatcherTable_ = {
178 { {"GetEmptyResponse", ""}, &getEmptyResponseStubDispatcher },
179 { {"GetDeepCopy", "i"}, &getDeepCopyStubDispatcher },
180 { {"GetDeepCopies", "ai"}, &getDeepCopiesStubDispatcher },
181 { {"GetShallowCopy", "i"}, &getShallowCopyStubDispatcher },
182 { {"GetShallowCopies", "ai"}, &getShallowCopiesStubDispatcher },
183 { {"SetTestAttribute", "i"}, &setTestAttributeStubDispatcher }
187 auto dbusConnection = CommonAPI::DBus::DBusConnection::getSessionBus();
189 if (!dbusConnection->isConnected())
190 dbusConnection->connect();
192 assert(dbusConnection->isConnected());
194 const bool serviceNameAcquired = dbusConnection->requestServiceNameAndBlock(
195 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter");
196 assert(serviceNameAcquired);
198 auto testStub = std::make_shared<TestStub>();
199 auto testStubAdapter = std::make_shared<TestDBusStubAdapter>(
200 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
201 "/common/api/dbus/TestDBusInterfaceAdapter",
204 testStubAdapter->init();
206 auto dbusMessageCall = CommonAPI::DBus::DBusMessage::createMethodCall(
207 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
208 testStubAdapter->getObjectPath().c_str(),
209 testStubAdapter->getServiceId().c_str(),
212 const bool messageSent = dbusConnection->sendDBusMessage(dbusMessageCall);
215 for (int i = 0; i < 10; i++)
216 dbusConnection->readWriteDispatch(100);
218 // while (dbusConnection->readWriteDispatch(100))
221 assert(dispatchedMessageCount > 0);