1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include <CommonAPI/DBus/DBusFunctionalHash.h>
5 #include <CommonAPI/DBus/DBusStubAdapterHelper.h>
11 static uint32_t dispatchedMessageCount;
13 class TestStubRemoteEventHandler {
15 virtual ~TestStubRemoteEventHandler() { }
17 virtual bool onRemoteSetTestAttribute(int32_t testValue) = 0;
19 virtual void onRemoteTestAttributeChanged() = 0;
22 class TestStubAdapter: virtual public CommonAPI::StubAdapter {
24 virtual void fireTestAttributeChanged(const int32_t& testValue) = 0;
27 class TestStub: public CommonAPI::Stub<TestStubAdapter, TestStubRemoteEventHandler> {
29 TestStub(): remoteEventHandler_(this) {
32 virtual TestStubRemoteEventHandler* initStubAdapter(const std::shared_ptr<TestStubAdapter>& stubAdapter) {
33 return &remoteEventHandler_;
36 void getEmptyResponse() {
37 std::cout << "onGetEmptyResponse()\n";
38 dispatchedMessageCount++;
41 void getDeepCopy(int32_t inValue, int32_t& outValue) {
43 std::cout << "getDeepCopy(inValue=" << inValue << ", outValue=" << outValue << ")\n";
44 dispatchedMessageCount++;
47 void getDeepCopies(std::vector<int32_t> inValues, std::vector<int32_t>& outValues) {
49 std::cout << "getDeepCopies(inValues=" << inValues.size() << ", outValues=" << outValues.size() << ")\n";
50 dispatchedMessageCount++;
53 void getShallowCopy(int32_t inValue, int32_t& outValue) {
55 std::cout << "getShallowCopy(inValue=" << inValue << ", outValue=" << outValue << ")\n";
56 dispatchedMessageCount++;
59 void getShallowCopies(std::vector<int32_t> inValues, std::vector<int32_t>& outValues) {
61 std::cout << "getShallowCopies(inValues=" << inValues.size() << ", outValues=" << outValues.size() << ")\n";
62 dispatchedMessageCount++;
65 const int32_t& getTestAttribute() {
66 return testAttributeValue_;
70 class RemoteEventHandler: public TestStubRemoteEventHandler {
72 RemoteEventHandler(TestStub* stub): stub_(stub) {
75 virtual bool onRemoteSetTestAttribute(int32_t testValue) {
76 std::cout << "RemoteEventHandler::onRemoteSetTestAttribute(" << testValue << "): oldValue = " << stub_->testAttributeValue_ << "\n";
77 const bool valueChanged = (stub_->testAttributeValue_ != testValue);
78 stub_->testAttributeValue_ = testValue;
82 virtual void onRemoteTestAttributeChanged() {
83 std::cout << "RemoteEventHandler::onRemoteTestAttributeChanged()\n";
90 RemoteEventHandler remoteEventHandler_;
91 int32_t testAttributeValue_;
94 typedef CommonAPI::DBus::DBusStubAdapterHelper<TestStub> TestStubAdapterHelper;
96 class TestDBusStubAdapter: public TestStubAdapter, public TestStubAdapterHelper {
98 TestDBusStubAdapter(const std::string& dbusBusName,
99 const std::string& dbusObjectPath,
100 const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection,
101 const std::shared_ptr<TestStub>& testStub) :
102 TestStubAdapterHelper(
105 "org.genivi.CommonAPI.DBus.TestInterface",
110 virtual void fireTestAttributeChanged(const int32_t& testValue) {
111 std::cout << "TestDBusStubAdapter::fireTestAttributeChanged(" << testValue << ")\n";
115 virtual const char* getMethodsDBusIntrospectionXmlData() const;
118 const char* TestDBusStubAdapter::getMethodsDBusIntrospectionXmlData() const {
119 return "<method name=\"GetEmptyResponse\">\n"
121 "<method name=\"GetDeepCopy\">\n"
122 "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
123 "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
125 "<method name=\"GetDeepCopies\">\n"
126 "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
127 "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
129 "<method name=\"GetShallowCopy\">\n"
130 "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
131 "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
133 "<method name=\"GetShallowCopies\">\n"
134 "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
135 "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
137 "<method name=\"SetTestAttribute\">\n"
138 "<arg type=\"i\" name=\"value\" direction=\"in\"/>\n"
139 "<arg type=\"i\" name=\"value\" direction=\"out\"/>\n"
145 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
148 std::tuple<> > getEmptyResponseStubDispatcher(&TestStub::getEmptyResponse, "");
149 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
152 std::tuple<int32_t> > getDeepCopyStubDispatcher(&TestStub::getDeepCopy, "i");
153 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
155 std::tuple<std::vector<int32_t>>,
156 std::tuple<std::vector<int32_t>> > getDeepCopiesStubDispatcher(&TestStub::getDeepCopies, "ai");
157 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
160 std::tuple<int32_t> > getShallowCopyStubDispatcher(&TestStub::getShallowCopy, "i");
161 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
163 std::tuple<std::vector<int32_t>>,
164 std::tuple<std::vector<int32_t>> > getShallowCopiesStubDispatcher(&TestStub::getShallowCopies, "ai");
165 static CommonAPI::DBus::DBusSetObservableAttributeStubDispatcher<TestStub, int32_t> setTestAttributeStubDispatcher(
166 &TestStub::getTestAttribute,
167 &TestStubRemoteEventHandler::onRemoteSetTestAttribute,
168 &TestStubRemoteEventHandler::onRemoteTestAttributeChanged,
169 &TestStubAdapter::fireTestAttributeChanged,
174 const TestStubAdapterHelper::StubDispatcherTable TestStubAdapterHelper::stubDispatcherTable_ = {
175 { {"GetEmptyResponse", ""}, &getEmptyResponseStubDispatcher },
176 { {"GetDeepCopy", "i"}, &getDeepCopyStubDispatcher },
177 { {"GetDeepCopies", "ai"}, &getDeepCopiesStubDispatcher },
178 { {"GetShallowCopy", "i"}, &getShallowCopyStubDispatcher },
179 { {"GetShallowCopies", "ai"}, &getShallowCopiesStubDispatcher },
180 { {"SetTestAttribute", "i"}, &setTestAttributeStubDispatcher }
184 auto dbusConnection = CommonAPI::DBus::DBusConnection::getSessionBus();
186 if (!dbusConnection->isConnected())
187 dbusConnection->connect();
189 assert(dbusConnection->isConnected());
191 const bool serviceNameAcquired = dbusConnection->requestServiceNameAndBlock(
192 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter");
193 assert(serviceNameAcquired);
195 auto testStub = std::make_shared<TestStub>();
196 auto testStubAdapter = std::make_shared<TestDBusStubAdapter>(
197 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
198 "/common/api/dbus/TestDBusInterfaceAdapter",
201 testStubAdapter->init();
203 auto dbusMessageCall = CommonAPI::DBus::DBusMessage::createMethodCall(
204 "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
205 testStubAdapter->getObjectPath().c_str(),
206 testStubAdapter->getServiceId().c_str(),
209 const bool messageSent = dbusConnection->sendDBusMessage(dbusMessageCall);
212 for (int i = 0; i < 10; i++)
213 dbusConnection->readWriteDispatch(100);
215 // while (dbusConnection->readWriteDispatch(100))
218 assert(dispatchedMessageCount > 0);