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& commonApiAddress,
102 const std::string& dbusBusName,
103 const std::string& dbusObjectPath,
104 const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection,
105 const std::shared_ptr<TestStub>& testStub) :
106 TestStubAdapterHelper(
110 "org.genivi.CommonAPI.DBus.TestInterface",
115 virtual void fireTestAttributeChanged(const int32_t& testValue) {
116 std::cout << "TestDBusStubAdapter::fireTestAttributeChanged(" << testValue << ")\n";
120 virtual const char* getMethodsDBusIntrospectionXmlData() const;
123 const char* TestDBusStubAdapter::getMethodsDBusIntrospectionXmlData() const {
124 return "<method name=\"GetEmptyResponse\">\n"
126 "<method name=\"GetDeepCopy\">\n"
127 "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
128 "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
130 "<method name=\"GetDeepCopies\">\n"
131 "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
132 "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
134 "<method name=\"GetShallowCopy\">\n"
135 "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
136 "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
138 "<method name=\"GetShallowCopies\">\n"
139 "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
140 "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
142 "<method name=\"SetTestAttribute\">\n"
143 "<arg type=\"i\" name=\"value\" direction=\"in\"/>\n"
144 "<arg type=\"i\" name=\"value\" direction=\"out\"/>\n"
150 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
153 std::tuple<> > getEmptyResponseStubDispatcher(&TestStub::getEmptyResponse, "");
154 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
157 std::tuple<int32_t> > getDeepCopyStubDispatcher(&TestStub::getDeepCopy, "i");
158 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
160 std::tuple<std::vector<int32_t>>,
161 std::tuple<std::vector<int32_t>> > getDeepCopiesStubDispatcher(&TestStub::getDeepCopies, "ai");
162 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
165 std::tuple<int32_t> > getShallowCopyStubDispatcher(&TestStub::getShallowCopy, "i");
166 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
168 std::tuple<std::vector<int32_t>>,
169 std::tuple<std::vector<int32_t>> > getShallowCopiesStubDispatcher(&TestStub::getShallowCopies, "ai");
170 static CommonAPI::DBus::DBusSetObservableAttributeStubDispatcher<TestStub, int32_t> setTestAttributeStubDispatcher(
171 &TestStub::getTestAttribute,
172 &TestStubRemoteEventHandler::onRemoteSetTestAttribute,
173 &TestStubRemoteEventHandler::onRemoteTestAttributeChanged,
174 &TestStubAdapter::fireTestAttributeChanged,
179 const TestStubAdapterHelper::StubDispatcherTable TestStubAdapterHelper::stubDispatcherTable_ = {
180 { {"GetEmptyResponse", ""}, &getEmptyResponseStubDispatcher },
181 { {"GetDeepCopy", "i"}, &getDeepCopyStubDispatcher },
182 { {"GetDeepCopies", "ai"}, &getDeepCopiesStubDispatcher },
183 { {"GetShallowCopy", "i"}, &getShallowCopyStubDispatcher },
184 { {"GetShallowCopies", "ai"}, &getShallowCopiesStubDispatcher },
185 { {"SetTestAttribute", "i"}, &setTestAttributeStubDispatcher }
189 auto dbusConnection = CommonAPI::DBus::DBusConnection::getSessionBus();
191 if (!dbusConnection->isConnected())
192 dbusConnection->connect();
194 assert(dbusConnection->isConnected());
196 const bool serviceNameAcquired = dbusConnection->requestServiceNameAndBlock(
197 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter");
198 assert(serviceNameAcquired);
200 auto testStub = std::make_shared<TestStub>();
201 auto testStubAdapter = std::make_shared<TestDBusStubAdapter>(
202 "my:common.api:address.for.dbus",
203 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
204 "/common/api/dbus/TestDBusInterfaceAdapter",
207 testStubAdapter->init();
209 auto dbusMessageCall = CommonAPI::DBus::DBusMessage::createMethodCall(
210 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
211 testStubAdapter->getObjectPath().c_str(),
212 testStubAdapter->getServiceId().c_str(),
215 const bool messageSent = dbusConnection->sendDBusMessage(dbusMessageCall);
218 for (int i = 0; i < 10; i++) {
219 dbusConnection->readWriteDispatch(100);
222 assert(dispatchedMessageCount > 0);